/** * Create order * Note that this->ref can be set or empty. If empty, we will use "(PROV)" * * @param User $user Objet user that make creation * @param int $notrigger Disable all triggers * @return int <0 if KO, >0 if OK */ function create($user, $notrigger = 0) { global $conf, $langs, $mysoc, $hookmanager; $error = 0; // Clean parameters $this->brouillon = 1; // set command as draft dol_syslog(get_class($this) . "::create user="******"::create " . $this->error, LOG_WARNING); $this->db->rollback(); return -1; } } $soc = new Societe($this->db); $result = $soc->fetch($this->socid); if ($result < 0) { $this->error = "Failed to fetch company"; dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR); return -2; } if (!empty($conf->global->COMMANDE_REQUIRE_SOURCE) && $this->source < 0) { $this->error = $langs->trans("ErrorFieldRequired", $langs->trans("Source")); dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR); return -1; } // $date_commande is deprecated $date = $this->date_commande ? $this->date_commande : $this->date; $now = dol_now(); $this->db->begin(); $sql = "INSERT INTO " . MAIN_DB_PREFIX . "commande ("; $sql .= " ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client, ref_int"; $sql .= ", model_pdf, fk_cond_reglement, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address"; $sql .= ", fk_shipping_method"; $sql .= ", fk_warehouse"; $sql .= ", remise_absolue, remise_percent"; $sql .= ", fk_incoterms, location_incoterms"; $sql .= ", entity"; $sql .= ")"; $sql .= " VALUES ('(PROV)'," . $this->socid . ", '" . $this->db->idate($now) . "', " . $user->id; $sql .= ", " . ($this->fk_project > 0 ? $this->fk_project : "null"); $sql .= ", '" . $this->db->idate($date) . "'"; $sql .= ", " . ($this->source >= 0 && $this->source != '' ? $this->db->escape($this->source) : 'null'); $sql .= ", '" . $this->db->escape($this->note_private) . "'"; $sql .= ", '" . $this->db->escape($this->note_public) . "'"; $sql .= ", " . ($this->ref_ext ? "'" . $this->db->escape($this->ref_ext) . "'" : "null"); $sql .= ", " . ($this->ref_client ? "'" . $this->db->escape($this->ref_client) . "'" : "null"); $sql .= ", " . ($this->ref_int ? "'" . $this->db->escape($this->ref_int) . "'" : "null"); $sql .= ", '" . $this->db->escape($this->modelpdf) . "'"; $sql .= ", " . ($this->cond_reglement_id > 0 ? "'" . $this->cond_reglement_id . "'" : "null"); $sql .= ", " . ($this->mode_reglement_id > 0 ? "'" . $this->mode_reglement_id . "'" : "null"); $sql .= ", " . ($this->fk_account > 0 ? $this->fk_account : 'NULL'); $sql .= ", " . ($this->availability_id > 0 ? "'" . $this->availability_id . "'" : "null"); $sql .= ", " . ($this->demand_reason_id > 0 ? "'" . $this->demand_reason_id . "'" : "null"); $sql .= ", " . ($this->date_livraison ? "'" . $this->db->idate($this->date_livraison) . "'" : "null"); $sql .= ", " . ($this->fk_delivery_address > 0 ? $this->fk_delivery_address : 'NULL'); $sql .= ", " . ($this->shipping_method_id > 0 ? $this->shipping_method_id : 'NULL'); $sql .= ", " . ($this->warehouse_id > 0 ? $this->warehouse_id : 'NULL'); $sql .= ", " . ($this->remise_absolue > 0 ? $this->db->escape($this->remise_absolue) : 'NULL'); $sql .= ", " . ($this->remise_percent > 0 ? $this->db->escape($this->remise_percent) : 0); $sql .= ", " . (int) $this->fk_incoterms; $sql .= ", '" . $this->db->escape($this->location_incoterms) . "'"; $sql .= ", " . $conf->entity; $sql .= ")"; dol_syslog(get_class($this) . "::create", LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'commande'); if ($this->id) { $fk_parent_line = 0; $num = count($this->lines); /* * Insert products details into db */ for ($i = 0; $i < $num; $i++) { // Reset fk_parent_line for no child products and special product if ($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line) || $this->lines[$i]->product_type == 9) { $fk_parent_line = 0; } $result = $this->addline($this->lines[$i]->desc, $this->lines[$i]->subprice, $this->lines[$i]->qty, $this->lines[$i]->tva_tx, $this->lines[$i]->localtax1_tx, $this->lines[$i]->localtax2_tx, $this->lines[$i]->fk_product, $this->lines[$i]->remise_percent, $this->lines[$i]->info_bits, $this->lines[$i]->fk_remise_except, 'HT', 0, $this->lines[$i]->date_start, $this->lines[$i]->date_end, $this->lines[$i]->product_type, $this->lines[$i]->rang, $this->lines[$i]->special_code, $fk_parent_line, $this->lines[$i]->fk_fournprice, $this->lines[$i]->pa_ht, $this->lines[$i]->label, $this->lines[$i]->array_options, $this->lines[$i]->fk_unit, $this->element, $this->lines[$i]->id); if ($result < 0) { if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) { $this->error = $this->db->lasterror(); dol_print_error($this->db); } $this->db->rollback(); return -1; } // Defined the new fk_parent_line if ($result > 0 && $this->lines[$i]->product_type == 9) { $fk_parent_line = $result; } } // update ref $initialref = '(PROV' . $this->id . ')'; if (!empty($this->ref)) { $initialref = $this->ref; } $sql = 'UPDATE ' . MAIN_DB_PREFIX . "commande SET ref='" . $this->db->escape($initialref) . "' WHERE rowid=" . $this->id; if ($this->db->query($sql)) { if ($this->id) { $this->ref = $initialref; // Add object linked if (is_array($this->linked_objects) && !empty($this->linked_objects)) { foreach ($this->linked_objects as $origin => $origin_id) { $ret = $this->add_object_linked($origin, $origin_id); if (!$ret) { dol_print_error($this->db); $error++; } // TODO mutualiser if ($origin == 'propal' && $origin_id) { // On recupere les differents contact interne et externe $prop = new Propal($this->db); $prop->fetch($origin_id); // We get ids of sales representatives of proposal $this->userid = $prop->getIdcontact('internal', 'SALESREPFOLL'); if ($this->userid) { //On passe le commercial suivi propale en commercial suivi commande $this->add_contact($this->userid[0], 'SALESREPFOLL', 'internal'); } // We get ids of customer follower of proposal $this->contactid = $prop->getIdcontact('external', 'CUSTOMER'); if ($this->contactid) { //On passe le contact client suivi propale en contact client suivi commande $this->add_contact($this->contactid[0], 'CUSTOMER', 'external'); } } } } } if (!$error) { //$action='create'; // Actions on extra fields (by external module or standard code) // TODO le hook fait double emploi avec le trigger !! /*$hookmanager->initHooks(array('orderdao')); $parameters=array('socid'=>$this->id); $reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks if (empty($reshook)) { if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used {*/ $result = $this->insertExtraFields(); if ($result < 0) { $error++; } /* } } else if ($reshook < 0) $error++;*/ } if (!$error && !$notrigger) { // Call trigger $result = $this->call_trigger('ORDER_CREATE', $user); if ($result < 0) { $error++; } // End call triggers } if (!$error) { $this->db->commit(); return $this->id; } else { $this->db->rollback(); return -1 * $error; } } else { $this->error = $this->db->lasterror(); $this->db->rollback(); return -1; } } } else { dol_print_error($this->db); $this->db->rollback(); return -1; } }
/** * Create order * Note that this->ref can be set or empty. If empty, we will use "(PROV)" * @param user Objet user that make creation * @param notrigger Disable all triggers * @return int <0 if KO, >0 if OK */ function create($user, $notrigger = 0) { global $conf, $langs, $mysoc; $error = 0; // Clean parameters $this->brouillon = 1; // On positionne en mode brouillon la commande dol_syslog("Commande::create user="******"Failed to fetch company"; dol_syslog("Commande::create " . $this->error, LOG_ERR); return -2; } if (!empty($conf->global->COMMANDE_REQUIRE_SOURCE) && $this->source < 0) { $this->error = $langs->trans("ErrorFieldRequired", $langs->trans("Source")); dol_syslog("Commande::create " . $this->error, LOG_ERR); return -1; } if (!$remise) { $remise = 0; } if (!$this->fk_project) { $this->fk_project = 0; } // $date_commande is deprecated $date = $this->date_commande ? $this->date_commande : $this->date; $this->db->begin(); $sql = "INSERT INTO " . MAIN_DB_PREFIX . "commande ("; $sql .= " ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note, note_public, ref_client, ref_int"; $sql .= ", model_pdf, fk_cond_reglement, fk_mode_reglement, fk_availability, fk_demand_reason, date_livraison, fk_adresse_livraison"; $sql .= ", remise_absolue, remise_percent"; $sql .= ", entity"; $sql .= ")"; $sql .= " VALUES ('(PROV)'," . $this->socid . ", " . $this->db->idate(gmmktime()) . ", " . $user->id . ", " . $this->fk_project; $sql .= ", " . $this->db->idate($date); $sql .= ", " . ($this->source >= 0 && $this->source != '' ? $this->source : 'null'); $sql .= ", '" . $this->db->escape($this->note) . "'"; $sql .= ", '" . $this->db->escape($this->note_public) . "'"; $sql .= ", '" . $this->db->escape($this->ref_client) . "'"; $sql .= ", " . ($this->ref_int ? "'" . $this->db->escape($this->ref_int) . "'" : "null"); $sql .= ", '" . $this->modelpdf . "'"; $sql .= ", " . ($this->cond_reglement_id > 0 ? "'" . $this->cond_reglement_id . "'" : "null"); $sql .= ", " . ($this->mode_reglement_id > 0 ? "'" . $this->mode_reglement_id . "'" : "null"); $sql .= ", " . ($this->availability_id > 0 ? "'" . $this->availability_id . "'" : "null"); $sql .= ", " . ($this->demand_reason_id > 0 ? "'" . $this->demand_reason_id . "'" : "null"); $sql .= ", " . ($this->date_livraison ? "'" . $this->db->idate($this->date_livraison) . "'" : "null"); $sql .= ", " . ($this->fk_delivery_address > 0 ? $this->fk_delivery_address : 'NULL'); $sql .= ", " . ($this->remise_absolue > 0 ? $this->remise_absolue : 'NULL'); $sql .= ", '" . $this->remise_percent . "'"; $sql .= ", " . $conf->entity; $sql .= ")"; dol_syslog("Commande::create sql=" . $sql); $resql = $this->db->query($sql); if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'commande'); if ($this->id) { $fk_parent_line = 0; $num = sizeof($this->lines); /* * Insertion du detail des produits dans la base */ for ($i = 0; $i < $num; $i++) { // Reset fk_parent_line for no child products and special product if ($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line) || $this->lines[$i]->product_type == 9) { $fk_parent_line = 0; } $result = $this->addline($this->id, $this->lines[$i]->desc, $this->lines[$i]->subprice, $this->lines[$i]->qty, $this->lines[$i]->tva_tx, $this->lines[$i]->localtax1_tx, $this->lines[$i]->localtax2_tx, $this->lines[$i]->fk_product, $this->lines[$i]->remise_percent, $this->lines[$i]->info_bits, $this->lines[$i]->fk_remise_except, 'HT', 0, $this->lines[$i]->date_start, $this->lines[$i]->date_end, $this->lines[$i]->product_type, $this->lines[$i]->rang, $this->lines[$i]->special_code, $fk_parent_line); if ($result < 0) { $this->error = $this->db->lasterror(); dol_print_error($this->db); $this->db->rollback(); return -1; } // Defined the new fk_parent_line if ($result > 0 && $this->lines[$i]->product_type == 9) { $fk_parent_line = $result; } } // Mise a jour ref $sql = 'UPDATE ' . MAIN_DB_PREFIX . "commande SET ref='(PROV" . $this->id . ")' WHERE rowid=" . $this->id; if ($this->db->query($sql)) { if ($this->id) { $this->ref = "(PROV" . $this->id . ")"; // Add linked object and contacts if ($this->origin && $this->origin_id) { $ret = $this->add_object_linked(); if (!$ret) { dol_print_error($this->db); } // TODO mutualiser if ($this->origin == 'propal') { // On recupere les differents contact interne et externe $prop = new Propal($this->db, $this->socid, $this->origin_id); // On recupere le commercial suivi propale $this->userid = $prop->getIdcontact('internal', 'SALESREPFOLL'); if ($this->userid) { //On passe le commercial suivi propale en commercial suivi commande $this->add_contact($this->userid[0], 'SALESREPFOLL', 'internal'); } // On recupere le contact client suivi propale $this->contactid = $prop->getIdcontact('external', 'CUSTOMER'); if ($this->contactid) { //On passe le contact client suivi propale en contact client suivi commande $this->add_contact($this->contactid[0], 'CUSTOMER', 'external'); } } } } if (!$notrigger) { // Appel des triggers include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"; $interface = new Interfaces($this->db); $result = $interface->run_triggers('ORDER_CREATE', $this, $user, $langs, $conf); if ($result < 0) { $error++; $this->errors = $interface->errors; } // Fin appel triggers } $this->db->commit(); return $this->id; } else { $this->db->rollback(); return -1; } } } else { dol_print_error($this->db); $this->db->rollback(); return -1; } }
/** * Create order * Note that this->ref can be set or empty. If empty, we will use "(PROV)" * * @param User $user Objet user that make creation * @param int $notrigger Disable all triggers * @return int <0 if KO, >0 if OK */ function create($user, $notrigger = 0) { global $conf, $langs, $mysoc, $hookmanager; $error = 0; // Clean parameters $this->brouillon = 1; // On positionne en mode brouillon la commande dol_syslog(get_class($this) . "::create user="******"::create " . $this->error, LOG_WARNING); $this->db->rollback(); return -1; } } $soc = new Societe($this->db); $result = $soc->fetch($this->socid); if ($result < 0) { $this->error = "Failed to fetch company"; dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR); return -2; } if (!empty($conf->global->COMMANDE_REQUIRE_SOURCE) && $this->source < 0) { $this->error = $langs->trans("ErrorFieldRequired", $langs->trans("Source")); dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR); return -1; } // $date_commande is deprecated $date = $this->date_commande ? $this->date_commande : $this->date; $now = dol_now(); $this->db->begin(); $sql = "INSERT INTO " . MAIN_DB_PREFIX . "commande ("; $sql .= " ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_client, ref_int"; $sql .= ", model_pdf, fk_cond_reglement, fk_mode_reglement, fk_availability, fk_input_reason, date_livraison, fk_delivery_address"; $sql .= ", remise_absolue, remise_percent"; $sql .= ", entity"; $sql .= ")"; $sql .= " VALUES ('(PROV)'," . $this->socid . ", '" . $this->db->idate($now) . "', " . $user->id; $sql .= ", " . ($this->fk_project ? $this->fk_project : "null"); $sql .= ", '" . $this->db->idate($date) . "'"; $sql .= ", " . ($this->source >= 0 && $this->source != '' ? $this->source : 'null'); $sql .= ", '" . $this->db->escape($this->note_private) . "'"; $sql .= ", '" . $this->db->escape($this->note_public) . "'"; $sql .= ", '" . $this->db->escape($this->ref_client) . "'"; $sql .= ", " . ($this->ref_int ? "'" . $this->db->escape($this->ref_int) . "'" : "null"); $sql .= ", '" . $this->modelpdf . "'"; $sql .= ", " . ($this->cond_reglement_id > 0 ? "'" . $this->cond_reglement_id . "'" : "null"); $sql .= ", " . ($this->mode_reglement_id > 0 ? "'" . $this->mode_reglement_id . "'" : "null"); $sql .= ", " . ($this->availability_id > 0 ? "'" . $this->availability_id . "'" : "null"); $sql .= ", " . ($this->demand_reason_id > 0 ? "'" . $this->demand_reason_id . "'" : "null"); $sql .= ", " . ($this->date_livraison ? "'" . $this->db->idate($this->date_livraison) . "'" : "null"); $sql .= ", " . ($this->fk_delivery_address > 0 ? $this->fk_delivery_address : 'NULL'); $sql .= ", " . ($this->remise_absolue > 0 ? $this->remise_absolue : 'NULL'); $sql .= ", " . ($this->remise_percent > 0 ? $this->remise_percent : 0); $sql .= ", " . $conf->entity; $sql .= ")"; dol_syslog(get_class($this) . "::create sql=" . $sql); $resql = $this->db->query($sql); if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'commande'); if ($this->id) { $fk_parent_line = 0; $num = count($this->lines); /* * Insertion du detail des produits dans la base */ for ($i = 0; $i < $num; $i++) { // Reset fk_parent_line for no child products and special product if ($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line) || $this->lines[$i]->product_type == 9) { $fk_parent_line = 0; } $result = $this->addline($this->lines[$i]->desc, $this->lines[$i]->subprice, $this->lines[$i]->qty, $this->lines[$i]->tva_tx, $this->lines[$i]->localtax1_tx, $this->lines[$i]->localtax2_tx, $this->lines[$i]->fk_product, $this->lines[$i]->remise_percent, $this->lines[$i]->info_bits, $this->lines[$i]->fk_remise_except, 'HT', 0, $this->lines[$i]->date_start, $this->lines[$i]->date_end, $this->lines[$i]->product_type, $this->lines[$i]->rang, $this->lines[$i]->special_code, $fk_parent_line, $this->lines[$i]->fk_fournprice, $this->lines[$i]->pa_ht, $this->lines[$i]->label, $this->lines[$i]->array_options); if ($result < 0) { $this->error = $this->db->lasterror(); dol_print_error($this->db); $this->db->rollback(); return -1; } // Defined the new fk_parent_line if ($result > 0 && $this->lines[$i]->product_type == 9) { $fk_parent_line = $result; } } // Mise a jour ref $sql = 'UPDATE ' . MAIN_DB_PREFIX . "commande SET ref='(PROV" . $this->id . ")' WHERE rowid=" . $this->id; if ($this->db->query($sql)) { if ($this->id) { $this->ref = "(PROV" . $this->id . ")"; // Add object linked if (is_array($this->linked_objects) && !empty($this->linked_objects)) { foreach ($this->linked_objects as $origin => $origin_id) { $ret = $this->add_object_linked($origin, $origin_id); if (!$ret) { dol_print_error($this->db); $error++; } // TODO mutualiser if ($origin == 'propal' && $origin_id) { // On recupere les differents contact interne et externe $prop = new Propal($this->db, $this->socid, $origin_id); // On recupere le commercial suivi propale $this->userid = $prop->getIdcontact('internal', 'SALESREPFOLL'); if ($this->userid) { //On passe le commercial suivi propale en commercial suivi commande $this->add_contact($this->userid[0], 'SALESREPFOLL', 'internal'); } // On recupere le contact client suivi propale $this->contactid = $prop->getIdcontact('external', 'CUSTOMER'); if ($this->contactid) { //On passe le contact client suivi propale en contact client suivi commande $this->add_contact($this->contactid[0], 'CUSTOMER', 'external'); } } } } } if (!$error) { // Actions on extra fields (by external module or standard code) // FIXME le hook fait double emploi avec le trigger !! $hookmanager->initHooks(array('orderdao')); $parameters = array('socid' => $this->id); $reshook = $hookmanager->executeHooks('insertExtraFields', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks if (empty($reshook)) { if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) { $result = $this->insertExtraFields(); if ($result < 0) { $error++; } } } else { if ($reshook < 0) { $error++; } } } if (!$notrigger) { // Appel des triggers include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; $interface = new Interfaces($this->db); $result = $interface->run_triggers('ORDER_CREATE', $this, $user, $langs, $conf); if ($result < 0) { $error++; $this->errors = $interface->errors; } // Fin appel triggers } if (!$error) { $this->db->commit(); return $this->id; } foreach ($this->errors as $errmsg) { dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR); $this->error .= $this->error ? ', ' . $errmsg : $errmsg; } $this->db->rollback(); return -1 * $error; } else { $this->db->rollback(); return -1; } } } else { dol_print_error($this->db); $this->db->rollback(); return -1; } }