Exemplo n.º 1
0
 /**
  * Event function to add the SalesOrder Data
  * @param object $evctl
  */
 public function eventAddRecord(EventControler $evctl)
 {
     $permission = $_SESSION["do_crm_action_permission"]->action_permitted('add', 14);
     if (true === $permission) {
         $do_process_plugins = new CRMPluginProcessor();
         $do_process_plugins->process_action_plugins((int) $evctl->idmodule, $evctl, 1);
         if (strlen($do_process_plugins->get_error()) > 2) {
             $_SESSION["do_crm_messages"]->set_message('error', $do_process_plugins->get_error());
             $next_page = NavigationControl::getNavigationLink($evctl->module, "add");
             $dis = new Display($next_page);
             $evctl->setDisplayNext($dis);
         } else {
             $do_crm_fields = new CRMFields();
             $crm_fields = $do_crm_fields->get_field_information_by_module_as_array((int) $evctl->idmodule);
             // Insert the data into the related tables
             $table_entity = 'sales_order';
             $table_entity_custom = 'sales_order_custom_fld';
             $table_entity_to_grp = 'sales_order_to_grp_rel';
             $table_entity_address = 'sales_order_address';
             $entity_data_array = array();
             $custom_data_array = array();
             $addr_data_array = array();
             $assigned_to_as_group = false;
             foreach ($crm_fields as $crm_fields) {
                 $field_name = $crm_fields["field_name"];
                 if ($field_name == 'assigned_to' && $crm_fields["table_name"] == $table_entity && $crm_fields["idblock"] > 0) {
                     $field_name = 'iduser';
                 }
                 $field_value = $do_crm_fields->convert_field_value_onsave($crm_fields, $evctl);
                 if (is_array($field_value) && count($field_value) > 0) {
                     if ($field_value["field_type"] == 15) {
                         $value = $field_value["value"];
                         $assigned_to_as_group = $field_value["assigned_to_as_group"];
                         $group_id = $field_value["group_id"];
                     } elseif ($field_value["field_type"] == 12) {
                         $value = $field_value["name"];
                         $avatar_array[] = $field_value;
                     }
                 } else {
                     $value = $field_value;
                 }
                 if ($crm_fields["table_name"] == $table_entity && $crm_fields["idblock"] > 0) {
                     $entity_data_array[$field_name] = $value;
                 } elseif ($crm_fields["table_name"] == $table_entity_custom && $crm_fields["idblock"] > 0) {
                     $custom_data_array[$field_name] = $value;
                 } elseif ($crm_fields["table_name"] == $table_entity_address && $crm_fields["idblock"] > 0) {
                     $addr_data_array[$field_name] = $value;
                 }
             }
             //add to entity table
             $this->insert($table_entity, $entity_data_array);
             $id_entity = $this->getInsertId();
             if ($id_entity > 0) {
                 //update quotes other information
                 $upd_array = array("sales_order_number" => $id_entity, "sales_order_key" => md5(microtime() . $id_entity), "terms_condition" => $evctl->terms_cond, "net_total" => $evctl->net_total_lines, "discount_type" => $evctl->final_discount_type, "discount_value" => $evctl->final_discount_val, "discounted_amount" => $evctl->final_discounted_total, "tax_values" => $evctl->final_tax_val, "taxed_amount" => $evctl->final_tax_amount, "shipping_handling_charge" => $evctl->final_ship_hand_charge, "shipping_handling_tax_values" => $evctl->final_ship_hand_tax_val, "shipping_handling_taxed_amount" => $evctl->final_ship_hand_tax_amount, "final_adjustment_type" => $evctl->final_adjustment, "final_adjustment_amount" => $evctl->final_adjustment_val, "grand_total" => $evctl->grand_total);
                 $this->update(array($this->primary_key => $id_entity), $table_entity, $upd_array);
                 //adding the added_on
                 $u_qry = "\n\t\t\t\t\tupdate `" . $this->getTable() . "` \n\t\t\t\t\tset \n\t\t\t\t\t`added_on` = ? \n\t\t\t\t\twhere `" . $this->primary_key . "` = ?\n\t\t\t\t\t";
                 $this->query($u_qry, array(date("Y-m-d H:i:s"), $id_entity));
                 $custom_data_array["idsales_order"] = $id_entity;
                 $addr_data_array["idsales_order"] = $id_entity;
                 $this->insert($table_entity_custom, $custom_data_array);
                 $this->insert($table_entity_address, $addr_data_array);
                 //If the assigned_to to set as group then it goes to the table entity group relation table
                 if ($assigned_to_as_group === true) {
                     $this->insert($table_entity_to_grp, array("idsales_order" => $id_entity, "idgroup" => $group_id));
                 }
                 // record the data history
                 $do_data_history = new DataHistory();
                 $do_data_history->add_history($id_entity, (int) $evctl->idmodule, 'add');
                 //record the feed
                 $feed_other_assigne = array();
                 if ($assigned_to_as_group === true) {
                     $feed_other_assigne = array("related" => "group", "data" => array("key" => "newgroup", "val" => $group_id));
                 }
                 $do_feed_queue = new LiveFeedQueue();
                 $do_feed_queue->add_feed_queue($id_entity, (int) $evctl->idmodule, $evctl->subject, 'add', $feed_other_assigne);
                 $do_line_items = new Lineitems();
                 $param_array = (array) $evctl;
                 $do_line_items->add_line_items((int) $evctl->idmodule, $id_entity, $param_array["params"]);
                 // process after add plugin
                 $do_process_plugins->process_action_plugins((int) $evctl->idmodule, $evctl, 2, $id_entity);
                 $_SESSION["do_crm_messages"]->set_message('success', _('New Sales Order has been added successfully ! '));
                 $next_page = NavigationControl::getNavigationLink($evctl->module, "detail");
                 $dis = new Display($next_page);
                 $dis->addParam("sqrecord", $id_entity);
                 $evctl->setDisplayNext($dis);
             } else {
                 $_SESSION["do_crm_messages"]->set_message('error', _('Operation failed due to query error !'));
                 $next_page = $evctl->error_page;
                 $dis = new Display($next_page);
                 $evctl->setDisplayNext($dis);
             }
         }
     } else {
         $_SESSION["do_crm_messages"]->set_message('error', _('You do not have permission to add record ! '));
         $next_page = NavigationControl::getNavigationLink($evctl->module, "list");
         $dis = new Display($next_page);
         $evctl->setDisplayNext($dis);
     }
 }