36 include_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
37 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
39 require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
40 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
41 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
52 public $element =
'commande';
57 public $table_element =
'commande';
62 public $table_element_line =
'commandedet';
67 public $class_element_line =
'OrderLine';
72 public $fk_element =
'fk_commande';
77 public $picto =
'order';
83 public $ismultientitymanaged = 1;
89 public $restrictiononfksoc = 1;
109 public $ref_customer;
135 public $cond_reglement_code;
140 public $cond_reglement_doc;
145 public $deposit_percent;
155 public $mode_reglement;
160 public $mode_reglement_id;
165 public $mode_reglement_code;
171 public $availability_id;
177 public $availability_code;
183 public $availability;
188 public $demand_reason_id;
193 public $demand_reason_code;
205 public $date_commande;
212 public $date_livraison;
217 public $delivery_date;
222 public $fk_remise_except;
229 public $remise_absolue;
232 public $special_code;
238 public $warehouse_id;
240 public $extraparams = array();
242 public $linked_objects = array();
247 public $user_author_id;
262 public $lines = array();
268 public $fk_multicurrency;
273 public $multicurrency_code;
274 public $multicurrency_tx;
275 public $multicurrency_total_ht;
276 public $multicurrency_total_tva;
277 public $multicurrency_total_ttc;
292 public $online_payment_url;
323 public $fields = array(
324 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
325 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>20,
'index'=>1),
326 'ref' =>array(
'type'=>
'varchar(30)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'showoncombobox'=>1,
'position'=>25),
327 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefExt',
'enabled'=>1,
'visible'=>0,
'position'=>26),
328 'ref_client' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefCustomer',
'enabled'=>1,
'visible'=>-1,
'position'=>28),
329 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'isModEnabled("societe")',
'visible'=>-1,
'notnull'=>1,
'position'=>20),
330 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label'=>
'Project',
'enabled'=>
"isModEnabled('project')",
'visible'=>-1,
'position'=>25),
331 'date_commande' =>array(
'type'=>
'date',
'label'=>
'Date',
'enabled'=>1,
'visible'=>1,
'position'=>60),
332 'date_valid' =>array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>62),
333 'date_cloture' =>array(
'type'=>
'datetime',
'label'=>
'DateClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>65),
334 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>85),
335 'fk_user_cloture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
336 'source' =>array(
'type'=>
'smallint(6)',
'label'=>
'Source',
'enabled'=>1,
'visible'=>-1,
'position'=>95),
339 'remise_absolue' =>array(
'type'=>
'double',
'label'=>
'CustomerRelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>115),
341 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'VAT',
'enabled'=>1,
'visible'=>-1,
'position'=>125,
'isameasure'=>1),
342 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax1',
'enabled'=>1,
'visible'=>-1,
'position'=>130,
'isameasure'=>1),
343 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax2',
'enabled'=>1,
'visible'=>-1,
'position'=>135,
'isameasure'=>1),
344 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalHT',
'enabled'=>1,
'visible'=>-1,
'position'=>140,
'isameasure'=>1),
345 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>145,
'isameasure'=>1),
346 'note_private' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>150),
347 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>155),
348 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'PDFTemplate',
'enabled'=>1,
'visible'=>0,
'position'=>160),
350 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'BankAccount',
'enabled'=>
'isModEnabled("banque")',
'visible'=>-1,
'position'=>170),
351 'fk_currency' =>array(
'type'=>
'varchar(3)',
'label'=>
'MulticurrencyID',
'enabled'=>1,
'visible'=>-1,
'position'=>175),
352 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
353 'deposit_percent' =>array(
'type'=>
'varchar(63)',
'label'=>
'DepositPercent',
'enabled'=>1,
'visible'=>-1,
'position'=>181),
354 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
355 'date_livraison' =>array(
'type'=>
'date',
'label'=>
'DateDeliveryPlanned',
'enabled'=>1,
'visible'=>-1,
'position'=>190),
356 'fk_shipping_method' =>array(
'type'=>
'integer',
'label'=>
'ShippingMethod',
'enabled'=>1,
'visible'=>-1,
'position'=>195),
357 'fk_warehouse' =>array(
'type'=>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label'=>
'Fk warehouse',
'enabled'=>
'isModEnabled("stock")',
'visible'=>-1,
'position'=>200),
358 'fk_availability' =>array(
'type'=>
'integer',
'label'=>
'Availability',
'enabled'=>1,
'visible'=>-1,
'position'=>205),
359 'fk_input_reason' =>array(
'type'=>
'integer',
'label'=>
'InputReason',
'enabled'=>1,
'visible'=>-1,
'position'=>210),
361 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>225),
362 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>230),
363 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLabel',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>235),
364 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>240),
365 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'MulticurrencyCurrency',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>245),
366 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyRate',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>250,
'isameasure'=>1),
367 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>255,
'isameasure'=>1),
368 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>260,
'isameasure'=>1),
369 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>265,
'isameasure'=>1),
370 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>-1,
'position'=>270),
371 'module_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSModule',
'enabled'=>1,
'visible'=>-1,
'position'=>275),
372 'pos_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSTerminal',
'enabled'=>1,
'visible'=>-1,
'position'=>280),
373 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-1,
'position'=>300),
374 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>302),
375 'date_creation' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>304),
376 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>306),
377 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>400),
378 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>-1,
'position'=>500),
403 const STATUS_ACCEPTED = 2;
430 global $langs, $conf;
431 $langs->load(
"order");
433 if (!empty($conf->global->COMMANDE_ADDON)) {
436 $file = $conf->global->COMMANDE_ADDON.
".php";
437 $classname = $conf->global->COMMANDE_ADDON;
440 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
441 foreach ($dirmodels as $reldir) {
445 $mybool |= @include_once $dir.$file;
448 if ($mybool ===
false) {
453 $obj =
new $classname();
454 $numref = $obj->getNextValue($soc, $this);
459 $this->error = $obj->error;
464 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_COMMANDE_ADDON_NotDefined");
478 public function valid($user, $idwarehouse = 0, $notrigger = 0)
480 global $conf, $langs;
482 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
487 if ($this->statut == self::STATUS_VALIDATED) {
488 dol_syslog(get_class($this).
"::valid action abandonned: already validated", LOG_WARNING);
492 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
493 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate)))) {
494 $this->error =
'NotEnoughPermissions';
495 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
505 $soc->fetch($this->socid);
508 $result = $soc->set_as_client();
511 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
519 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
520 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
521 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
522 $sql .=
" date_valid='".$this->db->idate($now).
"',";
523 $sql .=
" fk_user_valid = ".($user->id > 0 ? (int) $user->id :
"null").
",";
524 $sql .=
" fk_user_modif = ".((int) $user->id);
525 $sql .=
" WHERE rowid = ".((int) $this->
id);
527 dol_syslog(get_class($this).
"::valid", LOG_DEBUG);
528 $resql = $this->db->query(
$sql);
531 $this->error = $this->db->lasterror();
537 if ($result >= 0 &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
538 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
539 $langs->load(
"agenda");
542 $cpt = count($this->lines);
543 for ($i = 0; $i < $cpt; $i++) {
544 if ($this->lines[$i]->fk_product > 0) {
546 $mouvP->origin = &$this;
547 $mouvP->setOrigin($this->element, $this->
id);
549 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"OrderValidatedInDolibarr", $num));
552 $this->error = $mouvP->error;
562 if (!$error && !$notrigger) {
572 $this->oldref = $this->ref;
575 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
577 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'commande/".$this->db->escape($this->newref).
"'";
578 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'commande/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
579 $resql = $this->db->query(
$sql);
581 $error++; $this->error = $this->db->lasterror();
587 $dirsource = $conf->commande->multidir_output[$this->entity].
'/'.$oldref;
588 $dirdest = $conf->commande->multidir_output[$this->entity].
'/'.$newref;
589 if (!$error && file_exists($dirsource)) {
590 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
592 if (@rename($dirsource, $dirdest)) {
595 $listoffiles =
dol_dir_list($conf->commande->multidir_output[$this->entity].
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
596 foreach ($listoffiles as $fileentry) {
597 $dirsource = $fileentry[
'name'];
598 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
599 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
600 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
601 @rename($dirsource, $dirdest);
612 $this->brouillon = 0;
619 $this->db->rollback();
635 global $conf, $langs;
640 if ($this->statut <= self::STATUS_DRAFT) {
644 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
645 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate)))) {
646 $this->error =
'Permission denied';
654 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
655 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT.
",";
656 $sql .=
" fk_user_modif = ".((int) $user->id);
657 $sql .=
" WHERE rowid = ".((int) $this->
id);
659 if ($this->db->query(
$sql)) {
661 $this->oldcopy = clone $this;
665 if (
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
668 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
669 $langs->load(
"agenda");
671 $num = count($this->lines);
672 for ($i = 0; $i < $num; $i++) {
673 if ($this->lines[$i]->fk_product > 0) {
675 $mouvP->origin = &$this;
676 $mouvP->setOrigin($this->element, $this->
id);
678 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderBackToDraftInDolibarr", $this->ref));
680 $error++; $this->error = $mouvP->error;
break;
688 $result = $this->
call_trigger(
'ORDER_UNVALIDATE', $user);
699 $this->db->rollback();
703 $this->error = $this->db->error();
704 $this->db->rollback();
723 if ($this->statut != self::STATUS_CANCELED && $this->statut != self::STATUS_CLOSED) {
724 dol_syslog(get_class($this).
"::set_reopen order has not status closed", LOG_WARNING);
730 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
731 $sql .=
' SET fk_statut='.self::STATUS_VALIDATED.
', facture=0,';
732 $sql .=
" fk_user_modif = ".((int) $user->id);
733 $sql .=
" WHERE rowid = ".((int) $this->
id);
735 dol_syslog(get_class($this).
"::set_reopen", LOG_DEBUG);
736 $resql = $this->db->query(
$sql);
746 $this->error = $this->db->lasterror();
757 foreach ($this->errors as $errmsg) {
758 dol_syslog(get_class($this).
"::set_reopen ".$errmsg, LOG_ERR);
759 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
761 $this->db->rollback();
773 public function cloture($user, $notrigger = 0)
779 $usercanclose = ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
780 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->close)));
783 if ($this->statut == self::STATUS_CLOSED) {
790 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
791 $sql .=
' SET fk_statut = '.self::STATUS_CLOSED.
',';
792 $sql .=
' fk_user_cloture = '.((int) $user->id).
',';
793 $sql .=
" date_cloture = '".$this->db->idate($now).
"',";
794 $sql .=
" fk_user_modif = ".((int) $user->id);
797 if ($this->db->query(
$sql)) {
813 $this->db->rollback();
817 $this->error = $this->db->lasterror();
819 $this->db->rollback();
833 public function cancel($idwarehouse = -1)
835 global $conf, $user, $langs;
841 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
842 $sql .=
" SET fk_statut = ".self::STATUS_CANCELED.
",";
843 $sql .=
" fk_user_modif = ".((int) $user->id);
844 $sql .=
" WHERE rowid = ".((int) $this->
id);
845 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
847 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
848 if ($this->db->query(
$sql)) {
850 if (
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
851 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
852 $langs->load(
"agenda");
854 $num = count($this->lines);
855 for ($i = 0; $i < $num; $i++) {
856 if ($this->lines[$i]->fk_product > 0) {
858 $mouvP->setOrigin($this->element, $this->
id);
860 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderCanceledInDolibarr", $this->ref));
863 $this->error = $mouvP->error;
884 foreach ($this->errors as $errmsg) {
885 dol_syslog(get_class($this).
"::cancel ".$errmsg, LOG_ERR);
886 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
888 $this->db->rollback();
892 $this->error = $this->db->error();
893 $this->db->rollback();
906 public function create($user, $notrigger = 0)
908 global $conf, $langs, $mysoc;
912 $this->brouillon = 1;
915 $date = ($this->date_commande ? $this->date_commande : $this->date);
916 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
919 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
924 if (empty($this->fk_multicurrency)) {
925 $this->multicurrency_code = $conf->currency;
926 $this->fk_multicurrency = 0;
927 $this->multicurrency_tx = 1;
930 dol_syslog(get_class($this).
"::create user=".$user->id);
933 if (!empty($this->
ref)) {
936 $this->error =
'ErrorRefAlreadyExists';
937 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
938 $this->db->rollback();
944 $result = $soc->fetch($this->socid);
946 $this->error =
"Failed to fetch company";
947 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
950 if (!empty($conf->global->ORDER_REQUIRE_SOURCE) && $this->source < 0) {
951 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Source"));
952 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
960 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande (";
961 $sql .=
" ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client";
962 $sql .=
", model_pdf, fk_cond_reglement, deposit_percent, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address";
963 $sql .=
", fk_shipping_method";
964 $sql .=
", fk_warehouse";
965 $sql .=
", remise_absolue, remise_percent";
966 $sql .=
", fk_incoterms, location_incoterms";
967 $sql .=
", entity, module_source, pos_source";
968 $sql .=
", fk_multicurrency";
969 $sql .=
", multicurrency_code";
970 $sql .=
", multicurrency_tx";
972 $sql .=
" VALUES ('(PROV)', ".((int) $this->socid).
", '".$this->db->idate($now).
"', ".((int) $user->id);
973 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
974 $sql .=
", '".$this->db->idate($date).
"'";
975 $sql .=
", ".($this->source >= 0 && $this->source !=
'' ? $this->db->escape($this->source) :
'null');
976 $sql .=
", '".$this->db->escape($this->note_private).
"'";
977 $sql .=
", '".$this->db->escape($this->note_public).
"'";
978 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
979 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
980 $sql .=
", '".$this->db->escape($this->model_pdf).
"'";
981 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
982 $sql .=
", ".(!empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
"null");
983 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
984 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
985 $sql .=
", ".($this->availability_id > 0 ? ((int) $this->availability_id) :
"null");
986 $sql .=
", ".($this->demand_reason_id > 0 ? ((int) $this->demand_reason_id) :
"null");
987 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
988 $sql .=
", ".($this->fk_delivery_address > 0 ? ((int) $this->fk_delivery_address) :
'NULL');
989 $sql .=
", ".(!empty($this->shipping_method_id) && $this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) :
'NULL');
990 $sql .=
", ".(!empty($this->warehouse_id) && $this->warehouse_id > 0 ? ((int) $this->warehouse_id) :
'NULL');
991 $sql .=
", ".($this->remise_absolue > 0 ? $this->db->escape($this->remise_absolue) :
'NULL');
992 $sql .=
", ".($this->remise_percent > 0 ? $this->db->escape($this->remise_percent) : 0);
993 $sql .=
", ".(int) $this->fk_incoterms;
994 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
995 $sql .=
", ".setEntity($this);
996 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
997 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
998 $sql .=
", ".(int) $this->fk_multicurrency;
999 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1000 $sql .=
", ".(float) $this->multicurrency_tx;
1003 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1004 $resql = $this->db->query(
$sql);
1006 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commande');
1009 $fk_parent_line = 0;
1010 $num = count($this->lines);
1015 for ($i = 0; $i < $num; $i++) {
1016 $line = $this->lines[$i];
1020 if (!is_object($line)) {
1021 $line = (object) $line;
1025 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1026 $fk_parent_line = 0;
1030 $vatrate = $line->tva_tx;
1031 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', $vatrate)) {
1032 $vatrate .=
' ('.$line->vat_src_code.
')';
1035 if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
1036 $originid = $line->origin_id;
1037 $origintype = $line->origin;
1039 $originid = $line->id;
1040 $origintype = $this->element;
1044 if (empty($line->ref_ext)) {
1045 $line->ref_ext =
'';
1053 $line->localtax1_tx,
1054 $line->localtax2_tx,
1056 $line->remise_percent,
1058 $line->fk_remise_except,
1063 $line->product_type,
1065 $line->special_code,
1067 $line->fk_fournprice,
1070 $line->array_options,
1079 if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) {
1080 $this->error = $this->db->lasterror();
1081 $this->errors[] = $this->error;
1084 $this->db->rollback();
1088 if ($result > 0 && $line->product_type == 9) {
1089 $fk_parent_line = $result;
1096 $initialref =
'(PROV'.$this->id.
')';
1097 if (!empty($this->
ref)) {
1098 $initialref = $this->ref;
1101 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"commande SET ref='".$this->db->escape($initialref).
"' WHERE rowid=".((int) $this->
id);
1102 if ($this->db->query(
$sql)) {
1103 $this->
ref = $initialref;
1105 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1106 $this->linked_objects = $this->linkedObjectsIds;
1110 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1111 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1112 if (is_array($tmp_origin_id)) {
1113 foreach ($tmp_origin_id as $origin_id) {
1116 $this->error = $this->db->lasterror();
1122 $origin_id = $tmp_origin_id;
1125 $this->error = $this->db->lasterror();
1132 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) {
1133 $originforcontact = $this->origin;
1134 $originidforcontact = $this->origin_id;
1135 if ($originforcontact ==
'shipping') {
1136 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1138 $exp->fetch($this->origin_id);
1139 $exp->fetchObjectLinked();
1140 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1141 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1142 $originforcontact =
'commande';
1143 if (is_object($value)) {
1144 $originidforcontact = $value->id;
1146 $originidforcontact = $value;
1153 $sqlcontact =
"SELECT ctc.code, ctc.source, ec.fk_socpeople FROM ".MAIN_DB_PREFIX.
"element_contact as ec, ".MAIN_DB_PREFIX.
"c_type_contact as ctc";
1154 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact).
"'";
1156 $resqlcontact = $this->db->query($sqlcontact);
1157 if ($resqlcontact) {
1158 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1160 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1174 if (!$error && !$notrigger) {
1184 $this->db->commit();
1187 $this->db->rollback();
1191 $this->error = $this->db->lasterror();
1192 $this->db->rollback();
1200 $this->db->rollback();
1215 global $conf, $user, $hookmanager;
1222 foreach ($this->lines as $line) {
1223 $line->fetch_optionals();
1227 $objFrom = clone $this;
1230 if (!empty($socid) && $socid != $this->socid) {
1231 $objsoc =
new Societe($this->db);
1233 if ($objsoc->fetch($socid) > 0) {
1234 $this->socid = $objsoc->id;
1235 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1236 $this->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent :
null);
1237 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1238 $this->fk_project = 0;
1239 $this->fk_delivery_address = 0;
1250 $this->user_author_id = $user->id;
1251 $this->user_valid = 0;
1252 $this->user_validation_id = 0;
1254 $this->date_commande =
dol_now();
1255 $this->date_creation =
'';
1256 $this->date_validation =
'';
1257 if (empty($conf->global->MAIN_KEEP_REF_CUSTOMER_ON_CLONING)) {
1258 $this->ref_client =
'';
1259 $this->ref_customer =
'';
1263 $num = count($this->lines);
1264 for ($i = 0; $i < $num; $i++) {
1265 $this->lines[$i]->ref_ext =
'';
1269 $this->context[
'createfromclone'] =
'createfromclone';
1270 $result = $this->
create($user);
1284 if ($this->socid == $objFrom->socid) {
1293 if (is_object($hookmanager)) {
1294 $parameters = array(
'objFrom'=>$objFrom);
1296 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1304 unset($this->context[
'createfromclone']);
1308 $this->db->commit();
1311 $this->db->rollback();
1326 global $conf, $hookmanager;
1334 $this->date_commande =
dol_now();
1337 $num = count($object->lines);
1338 for ($i = 0; $i < $num; $i++) {
1341 $line->libelle = $object->lines[$i]->libelle;
1342 $line->label = $object->lines[$i]->label;
1343 $line->desc = $object->lines[$i]->desc;
1344 $line->price = $object->lines[$i]->price;
1345 $line->subprice = $object->lines[$i]->subprice;
1346 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1347 $line->tva_tx = $object->lines[$i]->tva_tx;
1348 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1349 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1350 $line->qty = $object->lines[$i]->qty;
1351 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1352 $line->remise_percent = $object->lines[$i]->remise_percent;
1353 $line->fk_product = $object->lines[$i]->fk_product;
1354 $line->info_bits = $object->lines[$i]->info_bits;
1355 $line->product_type = $object->lines[$i]->product_type;
1356 $line->rang = $object->lines[$i]->rang;
1357 $line->special_code = $object->lines[$i]->special_code;
1358 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1359 $line->fk_unit = $object->lines[$i]->fk_unit;
1361 $line->date_start = $object->lines[$i]->date_start;
1362 $line->date_end = $object->lines[$i]->date_end;
1364 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1365 $marginInfos =
getMarginInfos($object->lines[$i]->subprice, $object->lines[$i]->remise_percent, $object->lines[$i]->tva_tx, $object->lines[$i]->localtax1_tx, $object->lines[$i]->localtax2_tx, $object->lines[$i]->fk_fournprice, $object->lines[$i]->pa_ht);
1366 $line->pa_ht = $marginInfos[0];
1367 $line->marge_tx = $marginInfos[1];
1368 $line->marque_tx = $marginInfos[2];
1370 $line->origin = $object->element;
1371 $line->origin_id = $object->lines[$i]->id;
1374 $object->lines[$i]->fetch_optionals();
1375 foreach ($object->lines[$i]->array_options as $options_key => $value) {
1376 $line->array_options[$options_key] = $value;
1379 $this->lines[$i] = $line;
1382 $this->entity = $object->entity;
1383 $this->socid = $object->socid;
1384 $this->fk_project = $object->fk_project;
1385 $this->cond_reglement_id = $object->cond_reglement_id;
1386 $this->deposit_percent = $object->deposit_percent;
1387 $this->mode_reglement_id = $object->mode_reglement_id;
1388 $this->fk_account = $object->fk_account;
1389 $this->availability_id = $object->availability_id;
1390 $this->demand_reason_id = $object->demand_reason_id;
1391 $this->date_livraison = $object->date_livraison;
1392 $this->delivery_date = $object->date_livraison;
1393 $this->shipping_method_id = $object->shipping_method_id;
1394 $this->warehouse_id = $object->warehouse_id;
1395 $this->fk_delivery_address = $object->fk_delivery_address;
1396 $this->contact_id = $object->contact_id;
1397 $this->ref_client = $object->ref_client;
1398 $this->ref_customer = $object->ref_client;
1400 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1401 $this->note_private = $object->note_private;
1402 $this->note_public = $object->note_public;
1405 $this->origin = $object->element;
1406 $this->origin_id = $object->id;
1409 if (!empty($conf->multicurrency->enabled)) {
1410 if (!empty($object->multicurrency_code)) {
1411 $this->multicurrency_code = $object->multicurrency_code;
1413 if (!empty($conf->global->MULTICURRENCY_USE_ORIGIN_TX) && !empty($object->multicurrency_tx)) {
1414 $this->multicurrency_tx = $object->multicurrency_tx;
1417 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1419 $this->fk_multicurrency = $tmparray[0];
1420 $this->multicurrency_tx = $tmparray[1];
1424 if (empty($this->fk_multicurrency)) {
1425 $this->multicurrency_code = $conf->currency;
1426 $this->fk_multicurrency = 0;
1427 $this->multicurrency_tx = 1;
1432 $object->fetch_optionals();
1435 $element_extrafields = $e->fetch_name_optionals_label($this->table_element);
1437 foreach ($object->array_options as $options_key => $value) {
1438 if (array_key_exists(str_replace(
'options_',
'', $options_key), $element_extrafields)) {
1439 $this->array_options[$options_key] = $value;
1443 $this->linked_objects[$this->origin] = $this->origin_id;
1444 if (isset($object->other_linked_objects) && is_array($object->other_linked_objects) && !empty($object->other_linked_objects)) {
1445 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1448 $ret = $this->
create($user);
1452 $hookmanager->initHooks(array(
'orderdao'));
1454 $parameters = array(
'objFrom'=>$object);
1456 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1464 if (!empty($conf->global->ORDER_VALID_AFTER_CLOSE_PROPAL)) {
1466 $this->
valid($user);
1518 public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0,
$remise_percent = 0, $info_bits = 0, $fk_remise_except = 0, $price_base_type =
'HT', $pu_ttc = 0, $date_start =
'', $date_end =
'', $type = 0, $rang = -1, $special_code = 0, $fk_parent_line = 0, $fk_fournprice =
null, $pa_ht = 0, $label =
'', $array_options = 0, $fk_unit =
null, $origin =
'', $origin_id = 0, $pu_ht_devise = 0, $ref_ext =
'', $noupdateafterinsertline = 0)
1520 global $mysoc, $conf, $langs, $user;
1522 $logtext =
"::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent";
1523 $logtext .=
", info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, date_start=$date_start";
1524 $logtext .=
", date_end=$date_end, type=$type special_code=$special_code, fk_unit=$fk_unit, origin=$origin, origin_id=$origin_id, pu_ht_devise=$pu_ht_devise, ref_ext=$ref_ext";
1525 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
1527 if ($this->statut == self::STATUS_DRAFT) {
1528 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1538 if (empty($info_bits)) {
1544 if (empty($txtva)) {
1547 if (empty($txlocaltax1)) {
1550 if (empty($txlocaltax2)) {
1553 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1554 $fk_parent_line = 0;
1556 if (empty($this->fk_multicurrency)) {
1557 $this->fk_multicurrency = 0;
1559 if (empty($ref_ext)) {
1566 $pu_ht_devise =
price2num($pu_ht_devise);
1569 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1574 if ($price_base_type ==
'HT') {
1579 $label = trim($label);
1580 $desc = trim($desc);
1587 if ($date_start && $date_end && $date_start > $date_end) {
1588 $langs->load(
"errors");
1589 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1595 $product_type = $type;
1596 if (!empty($fk_product) && $fk_product > 0) {
1597 $product =
new Product($this->db);
1598 $result = $product->fetch($fk_product);
1599 $product_type = $product->type;
1601 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) {
1602 $langs->load(
"errors");
1603 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
1604 $this->errors[] = $this->error;
1605 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
1606 $this->db->rollback();
1620 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1621 $vat_src_code = $reg[1];
1622 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1625 $tabprice =
calcul_price_total($qty, $pu,
$remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
1635 $total_ht = $tabprice[0];
1636 $total_tva = $tabprice[1];
1637 $total_ttc = $tabprice[2];
1638 $total_localtax1 = $tabprice[9];
1639 $total_localtax2 = $tabprice[10];
1640 $pu_ht = $tabprice[3];
1643 $multicurrency_total_ht = $tabprice[16];
1644 $multicurrency_total_tva = $tabprice[17];
1645 $multicurrency_total_ttc = $tabprice[18];
1646 $pu_ht_devise = $tabprice[19];
1650 if ($ranktouse == -1) {
1651 $rangmax = $this->
line_max($fk_parent_line);
1652 $ranktouse = $rangmax + 1;
1661 $price = $pu - $remise;
1667 $this->line->context = $this->context;
1669 $this->line->fk_commande = $this->id;
1670 $this->line->label = $label;
1671 $this->line->desc = $desc;
1672 $this->line->qty = $qty;
1673 $this->line->ref_ext = $ref_ext;
1675 $this->line->vat_src_code = $vat_src_code;
1676 $this->line->tva_tx = $txtva;
1677 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1678 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1679 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1680 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1681 $this->line->fk_product = $fk_product;
1682 $this->line->product_type = $product_type;
1683 $this->line->fk_remise_except = $fk_remise_except;
1685 $this->line->subprice = $pu_ht;
1686 $this->line->rang = $ranktouse;
1687 $this->line->info_bits = $info_bits;
1688 $this->line->total_ht = $total_ht;
1689 $this->line->total_tva = $total_tva;
1690 $this->line->total_localtax1 = $total_localtax1;
1691 $this->line->total_localtax2 = $total_localtax2;
1692 $this->line->total_ttc = $total_ttc;
1693 $this->line->special_code = $special_code;
1694 $this->line->origin = $origin;
1695 $this->line->origin_id = $origin_id;
1696 $this->line->fk_parent_line = $fk_parent_line;
1697 $this->line->fk_unit = $fk_unit;
1699 $this->line->date_start = $date_start;
1700 $this->line->date_end = $date_end;
1702 $this->line->fk_fournprice = $fk_fournprice;
1703 $this->line->pa_ht = $pa_ht;
1706 $this->line->fk_multicurrency = $this->fk_multicurrency;
1707 $this->line->multicurrency_code = $this->multicurrency_code;
1708 $this->line->multicurrency_subprice = $pu_ht_devise;
1709 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1710 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1711 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1714 $this->line->price = $price;
1716 if (is_array($array_options) && count($array_options) > 0) {
1717 $this->line->array_options = $array_options;
1720 $result = $this->line->insert($user);
1723 if (!empty($fk_parent_line)) {
1725 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
1726 $linecount = count($this->lines);
1727 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
1733 if (empty($noupdateafterinsertline)) {
1738 $this->db->commit();
1739 return $this->line->id;
1741 $this->db->rollback();
1745 $this->error = $this->line->error;
1746 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1747 $this->db->rollback();
1751 dol_syslog(get_class($this).
"::addline status of order must be Draft to allow use of ->addline()", LOG_ERR);
1774 global $conf, $mysoc;
1780 if ($idproduct > 0) {
1781 $prod =
new Product($this->db);
1782 $prod->fetch($idproduct);
1786 if (empty($tva_tx)) {
1791 $localtax1_tx =
get_localtax($tva_tx, 1, $this->thirdparty, $mysoc, $tva_npr);
1792 $localtax2_tx =
get_localtax($tva_tx, 2, $this->thirdparty, $mysoc, $tva_npr);
1795 if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
1796 $price = $prod->multiprices[$this->thirdparty->price_level];
1798 $price = $prod->price;
1803 $line->context = $this->context;
1805 $line->fk_product = $idproduct;
1806 $line->desc = $prod->description;
1808 $line->subprice = $price;
1810 $line->vat_src_code = $vat_src_code;
1811 $line->tva_tx = $tva_tx;
1812 $line->localtax1_tx = $localtax1_tx;
1813 $line->localtax2_tx = $localtax2_tx;
1814 $line->ref = $prod->ref;
1815 $line->libelle = $prod->label;
1816 $line->product_desc = $prod->description;
1817 $line->fk_unit = $prod->fk_unit;
1821 $line->date_start = $date_start;
1824 $line->date_end = $date_end;
1827 $this->lines[] = $line;
1860 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
1863 if (empty($id) && empty($ref) && empty($ref_ext)) {
1867 $sql =
'SELECT c.rowid, c.entity, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_user_valid, c.fk_user_modif, c.fk_statut';
1868 $sql .=
', c.amount_ht, c.total_ht, c.total_ttc, c.total_tva, c.localtax1 as total_localtax1, c.localtax2 as total_localtax2, c.fk_cond_reglement, c.deposit_percent, c.fk_mode_reglement, c.fk_availability, c.fk_input_reason';
1869 $sql .=
', c.fk_account';
1870 $sql .=
', c.date_commande, c.date_valid, c.tms';
1871 $sql .=
', c.date_livraison as delivery_date';
1872 $sql .=
', c.fk_shipping_method';
1873 $sql .=
', c.fk_warehouse';
1874 $sql .=
', c.fk_projet as fk_project, c.remise_percent, c.remise, c.remise_absolue, c.source, c.facture as billed';
1875 $sql .=
', c.note_private, c.note_public, c.ref_client, c.ref_ext, c.model_pdf, c.last_main_doc, c.fk_delivery_address, c.extraparams';
1876 $sql .=
', c.fk_incoterms, c.location_incoterms';
1877 $sql .=
", c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc";
1878 $sql .=
", c.module_source, c.pos_source";
1879 $sql .=
", i.libelle as label_incoterms";
1880 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
1881 $sql .=
', cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle, cr.libelle_facture as cond_reglement_libelle_doc';
1882 $sql .=
', ca.code as availability_code, ca.label as availability_label';
1883 $sql .=
', dr.code as demand_reason_code';
1884 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
1885 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON c.fk_cond_reglement = cr.rowid';
1886 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON c.fk_mode_reglement = p.id';
1887 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON c.fk_availability = ca.rowid';
1888 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON c.fk_input_reason = dr.rowid';
1889 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
1892 $sql .=
" WHERE c.rowid=".((int) $id);
1894 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
1898 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
1901 $sql .=
" AND c.ref_ext='".$this->db->escape($ref_ext).
"'";
1904 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1905 $result = $this->db->query(
$sql);
1907 $obj = $this->db->fetch_object($result);
1909 $this->
id = $obj->rowid;
1910 $this->entity = $obj->entity;
1912 $this->
ref = $obj->ref;
1913 $this->ref_client = $obj->ref_client;
1914 $this->ref_customer = $obj->ref_client;
1915 $this->ref_ext = $obj->ref_ext;
1917 $this->socid = $obj->fk_soc;
1918 $this->thirdparty =
null;
1920 $this->fk_project = $obj->fk_project;
1921 $this->project =
null;
1923 $this->statut = $obj->fk_statut;
1924 $this->status = $obj->fk_statut;
1926 $this->user_author_id = $obj->fk_user_author;
1927 $this->user_creation_id = $obj->fk_user_author;
1928 $this->user_validation_id = $obj->fk_user_valid;
1929 $this->user_valid = $obj->fk_user_valid;
1930 $this->user_modification_id = $obj->fk_user_modif;
1931 $this->user_modification = $obj->fk_user_modif;
1932 $this->total_ht = $obj->total_ht;
1933 $this->total_tva = $obj->total_tva;
1934 $this->total_localtax1 = $obj->total_localtax1;
1935 $this->total_localtax2 = $obj->total_localtax2;
1936 $this->total_ttc = $obj->total_ttc;
1937 $this->date = $this->db->jdate($obj->date_commande);
1938 $this->date_commande = $this->db->jdate($obj->date_commande);
1939 $this->date_creation = $this->db->jdate($obj->date_creation);
1940 $this->date_validation = $this->db->jdate($obj->date_valid);
1941 $this->date_modification = $this->db->jdate($obj->tms);
1942 $this->remise = $obj->remise;
1943 $this->remise_percent = $obj->remise_percent;
1944 $this->remise_absolue = $obj->remise_absolue;
1945 $this->source = $obj->source;
1946 $this->billed = $obj->billed;
1947 $this->note = $obj->note_private;
1948 $this->note_private = $obj->note_private;
1949 $this->note_public = $obj->note_public;
1950 $this->model_pdf = $obj->model_pdf;
1951 $this->modelpdf = $obj->model_pdf;
1952 $this->last_main_doc = $obj->last_main_doc;
1953 $this->mode_reglement_id = $obj->fk_mode_reglement;
1954 $this->mode_reglement_code = $obj->mode_reglement_code;
1955 $this->mode_reglement = $obj->mode_reglement_libelle;
1956 $this->cond_reglement_id = $obj->fk_cond_reglement;
1957 $this->cond_reglement_code = $obj->cond_reglement_code;
1958 $this->cond_reglement = $obj->cond_reglement_libelle;
1959 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1960 $this->deposit_percent = $obj->deposit_percent;
1961 $this->fk_account = $obj->fk_account;
1962 $this->availability_id = $obj->fk_availability;
1963 $this->availability_code = $obj->availability_code;
1965 $this->demand_reason_id = $obj->fk_input_reason;
1966 $this->demand_reason_code = $obj->demand_reason_code;
1967 $this->date_livraison = $this->db->jdate($obj->delivery_date);
1968 $this->delivery_date = $this->db->jdate($obj->delivery_date);
1969 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method :
null;
1970 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse :
null;
1971 $this->fk_delivery_address = $obj->fk_delivery_address;
1972 $this->module_source = $obj->module_source;
1973 $this->pos_source = $obj->pos_source;
1976 $this->fk_incoterms = $obj->fk_incoterms;
1977 $this->location_incoterms = $obj->location_incoterms;
1978 $this->label_incoterms = $obj->label_incoterms;
1981 $this->fk_multicurrency = $obj->fk_multicurrency;
1982 $this->multicurrency_code = $obj->multicurrency_code;
1983 $this->multicurrency_tx = $obj->multicurrency_tx;
1984 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1985 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1986 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1988 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
1990 $this->lines = array();
1992 if ($this->statut == self::STATUS_DRAFT) {
1993 $this->brouillon = 1;
2000 $this->db->free($result);
2009 $this->error =
'Order with id '.$id.
' not found sql='.
$sql;
2013 $this->error = $this->db->error();
2031 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2032 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
2037 $result = $remise->fetch($idremise);
2040 if ($remise->fk_facture) {
2041 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2042 $this->db->rollback();
2048 $line->fk_commande = $this->id;
2049 $line->fk_remise_except = $remise->id;
2050 $line->desc = $remise->description;
2051 $line->vat_src_code = $remise->vat_src_code;
2052 $line->tva_tx = $remise->tva_tx;
2053 $line->subprice = -$remise->amount_ht;
2054 $line->price = -$remise->amount_ht;
2055 $line->fk_product = 0;
2057 $line->remise_percent = 0;
2059 $line->info_bits = 2;
2061 $line->total_ht = -$remise->amount_ht;
2062 $line->total_tva = -$remise->amount_tva;
2063 $line->total_ttc = -$remise->amount_ttc;
2065 $result = $line->insert();
2069 $this->db->commit();
2072 $this->db->rollback();
2076 $this->error = $line->error;
2077 $this->errors = $line->errors;
2078 $this->db->rollback();
2082 $this->db->rollback();
2096 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2099 global $langs, $conf;
2101 $this->lines = array();
2103 $sql =
'SELECT l.rowid, l.fk_product, l.fk_parent_line, l.product_type, l.fk_commande, l.label as custom_label, l.description, l.price, l.qty, l.vat_src_code, l.tva_tx, l.ref_ext,';
2104 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.fk_remise_except, l.remise_percent, l.subprice, l.fk_product_fournisseur_price as fk_fournprice, l.buy_price_ht as pa_ht, l.rang, l.info_bits, l.special_code,';
2105 $sql .=
' l.total_ht, l.total_ttc, l.total_tva, l.total_localtax1, l.total_localtax2, l.date_start, l.date_end,';
2106 $sql .=
' l.fk_unit,';
2107 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2108 $sql .=
' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label as product_label, p.tosell as product_tosell, p.tobuy as product_tobuy, p.tobatch as product_tobatch, p.barcode as product_barcode,';
2109 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units';
2110 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as l';
2111 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (p.rowid = l.fk_product)';
2112 $sql .=
' WHERE l.fk_commande = '.((int) $this->
id);
2113 if ($only_product) {
2114 $sql .=
' AND p.fk_product_type = 0';
2116 $sql .=
' ORDER BY l.rang, l.rowid';
2118 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
2119 $result = $this->db->query(
$sql);
2121 $num = $this->db->num_rows($result);
2125 $objp = $this->db->fetch_object($result);
2129 $line->rowid = $objp->rowid;
2130 $line->id = $objp->rowid;
2131 $line->fk_commande = $objp->fk_commande;
2132 $line->commande_id = $objp->fk_commande;
2133 $line->label = $objp->custom_label;
2134 $line->desc = $objp->description;
2135 $line->description = $objp->description;
2136 $line->product_type = $objp->product_type;
2137 $line->qty = $objp->qty;
2138 $line->ref_ext = $objp->ref_ext;
2140 $line->vat_src_code = $objp->vat_src_code;
2141 $line->tva_tx = $objp->tva_tx;
2142 $line->localtax1_tx = $objp->localtax1_tx;
2143 $line->localtax2_tx = $objp->localtax2_tx;
2144 $line->localtax1_type = $objp->localtax1_type;
2145 $line->localtax2_type = $objp->localtax2_type;
2146 $line->total_ht = $objp->total_ht;
2147 $line->total_ttc = $objp->total_ttc;
2148 $line->total_tva = $objp->total_tva;
2149 $line->total_localtax1 = $objp->total_localtax1;
2150 $line->total_localtax2 = $objp->total_localtax2;
2151 $line->subprice = $objp->subprice;
2152 $line->fk_remise_except = $objp->fk_remise_except;
2153 $line->remise_percent = $objp->remise_percent;
2154 $line->price = $objp->price;
2155 $line->fk_product = $objp->fk_product;
2156 $line->fk_fournprice = $objp->fk_fournprice;
2157 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2158 $line->pa_ht = $marginInfos[0];
2159 $line->marge_tx = $marginInfos[1];
2160 $line->marque_tx = $marginInfos[2];
2161 $line->rang = $objp->rang;
2162 $line->info_bits = $objp->info_bits;
2163 $line->special_code = $objp->special_code;
2164 $line->fk_parent_line = $objp->fk_parent_line;
2166 $line->ref = $objp->product_ref;
2167 $line->libelle = $objp->product_label;
2169 $line->product_ref = $objp->product_ref;
2170 $line->product_label = $objp->product_label;
2171 $line->product_tosell = $objp->product_tosell;
2172 $line->product_tobuy = $objp->product_tobuy;
2173 $line->product_desc = $objp->product_desc;
2174 $line->product_tobatch = $objp->product_tobatch;
2175 $line->product_barcode = $objp->product_barcode;
2177 $line->fk_product_type = $objp->fk_product_type;
2178 $line->fk_unit = $objp->fk_unit;
2180 $line->weight = $objp->weight;
2181 $line->weight_units = $objp->weight_units;
2182 $line->volume = $objp->volume;
2183 $line->volume_units = $objp->volume_units;
2185 $line->date_start = $this->db->jdate($objp->date_start);
2186 $line->date_end = $this->db->jdate($objp->date_end);
2189 $line->fk_multicurrency = $objp->fk_multicurrency;
2190 $line->multicurrency_code = $objp->multicurrency_code;
2191 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2192 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2193 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2194 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2196 $line->fetch_optionals();
2199 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2200 $tmpproduct =
new Product($this->db);
2201 $tmpproduct->fetch($objp->fk_product);
2202 $tmpproduct->getMultiLangs();
2204 $line->multilangs = $tmpproduct->multilangs;
2207 $this->lines[$i] = $line;
2212 $this->db->free($result);
2216 $this->error = $this->db->error();
2230 foreach ($this->lines as $line) {
2231 if ($line->product_type == 0) {
2246 foreach ($this->lines as $line) {
2247 if ($line->product_type == 1) {
2263 $sql =
'SELECT COUNT(DISTINCT ed.fk_expedition) as nb';
2264 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2265 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2267 $sql .=
' ed.fk_origin_line = cd.rowid';
2268 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2271 dol_syslog(get_class($this).
"::getNbOfShipments", LOG_DEBUG);
2272 $resql = $this->db->query(
$sql);
2274 $obj = $this->db->fetch_object($resql);
2279 $this->db->free($resql);
2282 $this->error = $this->db->lasterror();
2297 $this->expeditions = array();
2299 $sql =
'SELECT cd.rowid, cd.fk_product,';
2300 $sql .=
' sum(ed.qty) as qty';
2301 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2302 if ($filtre_statut >= 0) {
2303 $sql .=
' '.MAIN_DB_PREFIX.
'expedition as e,';
2305 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2307 if ($filtre_statut >= 0) {
2308 $sql .=
' ed.fk_expedition = e.rowid AND';
2310 $sql .=
' ed.fk_origin_line = cd.rowid';
2311 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2312 if ($fk_product > 0) {
2313 $sql .=
' AND cd.fk_product = '.((int) $fk_product);
2315 if ($filtre_statut >= 0) {
2316 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
2318 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
2321 dol_syslog(get_class($this).
"::loadExpeditions", LOG_DEBUG);
2322 $resql = $this->db->query(
$sql);
2324 $num = $this->db->num_rows($resql);
2327 $obj = $this->db->fetch_object($resql);
2328 $this->expeditions[$obj->rowid] = $obj->qty;
2331 $this->db->free($resql);
2334 $this->error = $this->db->lasterror();
2346 $sql =
'SELECT count(*)';
2347 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expedition as e';
2348 $sql .=
', '.MAIN_DB_PREFIX.
'element_element as el';
2349 $sql .=
' WHERE el.fk_source = '.((int) $this->
id);
2350 $sql .=
" AND el.sourcetype = 'commande'";
2351 $sql .=
" AND el.fk_target = e.rowid";
2352 $sql .=
" AND el.targettype = 'shipping'";
2354 $resql = $this->db->query(
$sql);
2356 $row = $this->db->fetch_row($resql);
2377 $this->stocks = array();
2380 $array_of_product = array();
2384 if (count($array_of_product)) {
2385 $sql =
"SELECT fk_product, sum(ps.reel) as total";
2386 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_stock as ps";
2387 $sql .=
" WHERE ps.fk_product IN (".$this->db->sanitize(join(
',', $array_of_product)).
")";
2388 $sql .=
' GROUP BY fk_product';
2389 $resql = $this->db->query(
$sql);
2391 $num = $this->db->num_rows($resql);
2394 $obj = $this->db->fetch_object($resql);
2395 $this->stocks[$obj->fk_product] = $obj->total;
2398 $this->db->free($resql);
2414 if ($this->statut == self::STATUS_DRAFT) {
2420 $line->context = $this->context;
2423 $line->fetch($lineid);
2425 if ($id > 0 && $line->fk_commande != $id) {
2426 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2431 $staticline = clone $line;
2432 $line->oldline = $staticline;
2434 if ($line->delete($user) > 0) {
2438 $this->db->commit();
2441 $this->db->rollback();
2442 $this->error = $this->db->lasterror();
2446 $this->db->rollback();
2447 $this->error = $line->error;
2451 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
2470 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
2471 return $this->
setDiscount($user, $remise, $notrigger);
2485 $remise = trim($remise) ?trim($remise) : 0;
2487 if ($user->hasRight(
'commande',
'creer')) {
2494 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2495 $sql .=
' SET remise_percent = '.((float) $remise);
2496 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.((int) self::STATUS_DRAFT);
2499 $resql = $this->db->query(
$sql);
2501 $this->errors[] = $this->db->error();
2506 $this->oldcopy = clone $this;
2507 $this->remise_percent = $remise;
2511 if (!$notrigger && empty($error)) {
2521 $this->db->commit();
2524 foreach ($this->errors as $errmsg) {
2525 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2526 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2528 $this->db->rollback();
2549 if (empty($remise)) {
2555 if ($user->hasRight(
'commande',
'creer')) {
2560 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2561 $sql .=
' SET remise_absolue = '.((float) $remise);
2565 $resql = $this->db->query(
$sql);
2567 $this->errors[] = $this->db->error();
2572 $this->oldcopy = clone $this;
2573 $this->remise_absolue = $remise;
2577 if (!$notrigger && empty($error)) {
2587 $this->db->commit();
2590 foreach ($this->errors as $errmsg) {
2591 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2592 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2594 $this->db->rollback();
2615 if ($user->hasRight(
'commande',
'creer')) {
2620 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2621 $sql .=
" SET date_commande = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
2622 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut = ".((int) self::STATUS_DRAFT);
2625 $resql = $this->db->query(
$sql);
2627 $this->errors[] = $this->db->error();
2632 $this->oldcopy = clone $this;
2633 $this->date = $date;
2636 if (!$notrigger && empty($error)) {
2646 $this->db->commit();
2649 foreach ($this->errors as $errmsg) {
2650 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2651 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2653 $this->db->rollback();
2687 if ($user->hasRight(
'commande',
'creer')) {
2692 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2693 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2694 $sql .=
" WHERE rowid = ".((int) $this->
id);
2697 $resql = $this->db->query(
$sql);
2699 $this->errors[] = $this->db->error();
2704 $this->oldcopy = clone $this;
2705 $this->date_livraison = $delivery_date;
2706 $this->delivery_date = $delivery_date;
2709 if (!$notrigger && empty($error)) {
2719 $this->db->commit();
2722 foreach ($this->errors as $errmsg) {
2723 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2724 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2726 $this->db->rollback();
2748 public function liste_array($shortlist = 0, $draft = 0, $excluser =
'', $socid = 0, $limit = 0, $offset = 0, $sortfield =
'c.date_commande', $sortorder =
'DESC')
2755 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2756 $sql .=
" c.rowid as cid, c.ref";
2757 if (empty($user->rights->societe->client->voir) && !$socid) {
2758 $sql .=
", sc.fk_soc, sc.fk_user";
2760 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"commande as c";
2761 if (empty($user->rights->societe->client->voir) && !$socid) {
2762 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2764 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2765 $sql .=
" AND c.fk_soc = s.rowid";
2766 if (empty($user->rights->societe->client->voir) && !$socid) {
2767 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2770 $sql .=
" AND s.rowid = ".((int) $socid);
2773 $sql .=
" AND c.fk_statut = ".self::STATUS_DRAFT;
2775 if (is_object($excluser)) {
2776 $sql .=
" AND c.fk_user_author <> ".((int) $excluser->id);
2778 $sql .= $this->db->order($sortfield, $sortorder);
2779 $sql .= $this->db->plimit($limit, $offset);
2781 $result = $this->db->query(
$sql);
2783 $numc = $this->db->num_rows($result);
2786 while ($i < $numc) {
2787 $obj = $this->db->fetch_object($result);
2789 if ($shortlist == 1) {
2790 $ga[$obj->cid] = $obj->ref;
2791 } elseif ($shortlist == 2) {
2792 $ga[$obj->cid] = $obj->ref.
' ('.$obj->name.
')';
2794 $ga[$i][
'id'] = $obj->cid;
2795 $ga[$i][
'ref'] = $obj->ref;
2796 $ga[$i][
'name'] = $obj->name;
2819 dol_syslog(
'Commande::availability('.$availability_id.
')');
2820 if ($this->statut >= self::STATUS_DRAFT) {
2825 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2826 $sql .=
' SET fk_availability = '.((int) $availability_id);
2827 $sql .=
' WHERE rowid='.((int) $this->
id);
2830 $resql = $this->db->query(
$sql);
2832 $this->errors[] = $this->db->error();
2837 $this->oldcopy = clone $this;
2838 $this->availability_id = $availability_id;
2841 if (!$notrigger && empty($error)) {
2851 $this->db->commit();
2854 foreach ($this->errors as $errmsg) {
2855 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2856 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2858 $this->db->rollback();
2862 $error_str =
'Command status do not meet requirement '.$this->statut;
2864 $this->error = $error_str;
2865 $this->errors[] = $this->error;
2883 dol_syslog(
'Commande::demand_reason('.$demand_reason_id.
')');
2884 if ($this->statut >= self::STATUS_DRAFT) {
2889 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2890 $sql .=
' SET fk_input_reason = '.((int) $demand_reason_id);
2891 $sql .=
' WHERE rowid='.((int) $this->
id);
2894 $resql = $this->db->query(
$sql);
2896 $this->errors[] = $this->db->error();
2901 $this->oldcopy = clone $this;
2902 $this->demand_reason_id = $demand_reason_id;
2905 if (!$notrigger && empty($error)) {
2915 $this->db->commit();
2918 foreach ($this->errors as $errmsg) {
2919 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2920 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2922 $this->db->rollback();
2926 $error_str =
'order status do not meet requirement '.$this->statut;
2928 $this->error = $error_str;
2929 $this->errors[] = $this->error;
2946 if ($user->hasRight(
'commande',
'creer')) {
2951 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET';
2952 $sql .=
' ref_client = '.(empty($ref_client) ?
'NULL' :
"'".$this->db->escape($ref_client).
"'");
2953 $sql .=
' WHERE rowid = '.((int) $this->
id);
2955 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2956 $resql = $this->db->query(
$sql);
2958 $this->errors[] = $this->db->error();
2963 $this->oldcopy = clone $this;
2964 $this->ref_client = $ref_client;
2965 $this->ref_customer = $ref_client;
2968 if (!$notrigger && empty($error)) {
2977 $this->db->commit();
2980 foreach ($this->errors as $errmsg) {
2981 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2982 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2984 $this->db->rollback();
3003 if ($this->billed) {
3009 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 1';
3012 dol_syslog(get_class($this).
"::classifyBilled", LOG_DEBUG);
3013 if ($this->db->query(
$sql)) {
3015 $this->oldcopy = clone $this;
3019 if (!$notrigger && empty($error)) {
3021 $result = $this->
call_trigger(
'ORDER_CLASSIFY_BILLED', $user);
3029 $this->db->commit();
3032 foreach ($this->errors as $errmsg) {
3033 dol_syslog(get_class($this).
"::classifyBilled ".$errmsg, LOG_ERR);
3034 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3036 $this->db->rollback();
3040 $this->error = $this->db->error();
3041 $this->db->rollback();
3059 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 0';
3062 dol_syslog(get_class($this).
"::classifyUnBilled", LOG_DEBUG);
3063 if ($this->db->query(
$sql)) {
3065 $this->oldcopy = clone $this;
3069 if (!$notrigger && empty($error)) {
3071 $result = $this->
call_trigger(
'ORDER_CLASSIFY_UNBILLED', $user);
3081 $this->db->commit();
3084 foreach ($this->errors as $errmsg) {
3085 dol_syslog(get_class($this).
"::classifyUnBilled ".$errmsg, LOG_ERR);
3086 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3088 $this->db->rollback();
3092 $this->error = $this->db->error();
3093 $this->db->rollback();
3129 public function updateline($rowid, $desc, $pu, $qty,
$remise_percent, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $price_base_type =
'HT', $info_bits = 0, $date_start =
'', $date_end =
'', $type = 0, $fk_parent_line = 0, $skip_update_total = 0, $fk_fournprice =
null, $pa_ht = 0, $label =
'', $special_code = 0, $array_options = 0, $fk_unit =
null, $pu_ht_devise = 0, $notrigger = 0, $ref_ext =
'', $rang = 0)
3131 global $conf, $mysoc, $langs, $user;
3133 dol_syslog(get_class($this).
"::updateline id=$rowid, desc=$desc, pu=$pu, qty=$qty, remise_percent=$remise_percent, txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, price_base_type=$price_base_type, info_bits=$info_bits, date_start=$date_start, date_end=$date_end, type=$type, fk_parent_line=$fk_parent_line, pa_ht=$pa_ht, special_code=$special_code, ref_ext=$ref_ext");
3134 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3141 if (empty($info_bits)) {
3144 if (empty($txtva)) {
3147 if (empty($txlocaltax1)) {
3150 if (empty($txlocaltax2)) {
3156 if (empty($special_code) || $special_code == 3) {
3159 if (empty($ref_ext)) {
3163 if ($date_start && $date_end && $date_start > $date_end) {
3164 $langs->load(
"errors");
3165 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3173 $pu_ht_devise =
price2num($pu_ht_devise);
3174 if (!preg_match(
'/\((.*)\)/', $txtva)) {
3192 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3193 $vat_src_code = $reg[1];
3194 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3197 $tabprice =
calcul_price_total($qty, $pu,
$remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
3199 $total_ht = $tabprice[0];
3200 $total_tva = $tabprice[1];
3201 $total_ttc = $tabprice[2];
3202 $total_localtax1 = $tabprice[9];
3203 $total_localtax2 = $tabprice[10];
3204 $pu_ht = $tabprice[3];
3205 $pu_tva = $tabprice[4];
3206 $pu_ttc = $tabprice[5];
3209 $multicurrency_total_ht = $tabprice[16];
3210 $multicurrency_total_tva = $tabprice[17];
3211 $multicurrency_total_ttc = $tabprice[18];
3212 $pu_ht_devise = $tabprice[19];
3216 if ($price_base_type ==
'TTC') {
3217 $subprice = $pu_ttc;
3224 $price = ($pu - $remise);
3229 $line->fetch($rowid);
3230 $line->fetch_optionals();
3232 if (!empty($line->fk_product)) {
3233 $product =
new Product($this->db);
3234 $result = $product->fetch($line->fk_product);
3235 $product_type = $product->type;
3237 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) {
3238 $langs->load(
"errors");
3239 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
3240 $this->errors[] = $this->error;
3242 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
3244 $this->db->rollback();
3249 $staticline = clone $line;
3251 $line->oldline = $staticline;
3252 $this->line = $line;
3253 $this->line->context = $this->context;
3254 $this->line->rang = $rang;
3257 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
3258 $rangmax = $this->
line_max($fk_parent_line);
3259 $this->line->rang = $rangmax + 1;
3262 $this->line->id = $rowid;
3263 $this->line->label = $label;
3264 $this->line->desc = $desc;
3265 $this->line->qty = $qty;
3266 $this->line->ref_ext = $ref_ext;
3268 $this->line->vat_src_code = $vat_src_code;
3269 $this->line->tva_tx = $txtva;
3270 $this->line->localtax1_tx = $txlocaltax1;
3271 $this->line->localtax2_tx = $txlocaltax2;
3272 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3273 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3275 $this->line->subprice = $subprice;
3276 $this->line->info_bits = $info_bits;
3277 $this->line->special_code = $special_code;
3278 $this->line->total_ht = $total_ht;
3279 $this->line->total_tva = $total_tva;
3280 $this->line->total_localtax1 = $total_localtax1;
3281 $this->line->total_localtax2 = $total_localtax2;
3282 $this->line->total_ttc = $total_ttc;
3283 $this->line->date_start = $date_start;
3284 $this->line->date_end = $date_end;
3285 $this->line->product_type = $type;
3286 $this->line->fk_parent_line = $fk_parent_line;
3287 $this->line->skip_update_total = $skip_update_total;
3288 $this->line->fk_unit = $fk_unit;
3290 $this->line->fk_fournprice = $fk_fournprice;
3291 $this->line->pa_ht = $pa_ht;
3294 $this->line->multicurrency_subprice = $pu_ht_devise;
3295 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
3296 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
3297 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
3300 $this->line->price = $price;
3302 if (is_array($array_options) && count($array_options) > 0) {
3304 foreach ($array_options as $key => $value) {
3305 $this->line->array_options[$key] = $array_options[$key];
3309 $result = $this->line->update($user, $notrigger);
3312 if (!empty($fk_parent_line)) {
3319 $this->db->commit();
3322 $this->error = $this->line->error;
3324 $this->db->rollback();
3328 $this->error = get_class($this).
"::updateline Order status makes operation forbidden";
3329 $this->errors = array(
'OrderStatusMakeOperationForbidden');
3348 if (isset($this->
ref)) {
3349 $this->
ref = trim($this->
ref);
3351 if (isset($this->ref_client)) {
3352 $this->ref_client = trim($this->ref_client);
3354 if (isset($this->ref_customer)) {
3355 $this->ref_customer = trim($this->ref_customer);
3357 if (isset($this->note) || isset($this->note_private)) {
3358 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
3360 if (isset($this->note_public)) {
3361 $this->note_public = trim($this->note_public);
3363 if (isset($this->model_pdf)) {
3364 $this->model_pdf = trim($this->model_pdf);
3366 if (isset($this->import_key)) {
3367 $this->import_key = trim($this->import_key);
3369 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
3375 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande SET";
3377 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
3378 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
3379 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
3380 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
3381 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
3382 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
3383 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
3384 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
3385 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
3386 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
3387 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
3388 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
3389 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id :
"null").
",";
3390 $sql .=
" fk_user_valid=".((isset($this->user_valid) && $this->user_valid > 0) ? $this->user_valid :
"null").
",";
3391 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
3392 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
3393 $sql .=
" deposit_percent=".(!empty($this->deposit_percent) ? strval($this->deposit_percent) :
"null").
",";
3394 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
3395 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
3396 $sql .=
" fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id :
"null").
",";
3397 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
3398 $sql .=
" fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id :
"null").
",";
3399 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
3400 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
3401 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
3402 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
3404 $sql .=
" WHERE rowid=".((int) $this->
id);
3408 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3409 $resql = $this->db->query(
$sql);
3411 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
3421 if (!$error && !$notrigger) {
3432 foreach ($this->errors as $errmsg) {
3433 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3434 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3436 $this->db->rollback();
3439 $this->db->commit();
3451 public function delete($user, $notrigger = 0)
3453 global $conf, $langs;
3454 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3458 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
3473 $this->errors[] = $langs->trans(
'SomeShipmentExists');
3478 if (!$error && !empty($this->table_element_line)) {
3479 $tabletodelete = $this->table_element_line;
3480 $sqlef =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
"_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id).
")";
3481 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3482 if (!$this->db->query($sqlef) || !$this->db->query(
$sql)) {
3484 $this->error = $this->db->lasterror();
3485 $this->errors[] = $this->error;
3486 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3511 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3517 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
3518 $res = $this->db->query(
$sql);
3521 $this->error = $this->db->lasterror();
3522 $this->errors[] = $this->error;
3523 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3538 if ($conf->commande->multidir_output[$this->entity] && !empty($this->
ref)) {
3539 $dir = $conf->commande->multidir_output[$this->entity].
"/".$ref;
3540 $file = $dir.
"/".$ref.
".pdf";
3541 if (file_exists($file)) {
3545 $this->error =
'ErrorFailToDeleteFile';
3546 $this->errors[] = $this->error;
3547 $this->db->rollback();
3551 if (file_exists($dir)) {
3554 $this->error =
'ErrorFailToDeleteDir';
3555 $this->errors[] = $this->error;
3556 $this->db->rollback();
3564 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3565 $this->db->commit();
3568 $this->db->rollback();
3585 global $conf, $langs;
3589 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.date_livraison as delivery_date, c.fk_statut, c.total_ht";
3590 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as c";
3591 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3592 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
3593 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3598 if ($mode ==
'toship') {
3600 $sql .=
" AND c.fk_statut IN (" . self::STATUS_VALIDATED .
"," . self::STATUS_SHIPMENTONPROCESS .
")";
3602 if ($mode ==
'tobill') {
3604 $sql .=
" AND c.fk_statut IN (" . self::STATUS_VALIDATED .
"," . self::STATUS_SHIPMENTONPROCESS .
", " . self::STATUS_CLOSED .
") AND c.facture = 0";
3606 if ($mode ==
'shippedtobill') {
3608 $sql .=
" AND c.fk_statut IN (" . self::STATUS_CLOSED .
") AND c.facture = 0";
3611 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3614 $resql = $this->db->query(
$sql);
3617 $label = $labelShort = $url =
'';
3618 if ($mode ==
'toship') {
3619 $delay_warning = $conf->commande->client->warning_delay / 60 / 60 / 24;
3620 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=-2&mainmenu=commercial&leftmenu=orders';
3621 $label = $langs->transnoentitiesnoconv(
"OrdersToProcess");
3622 $labelShort = $langs->transnoentitiesnoconv(
"Opened");
3624 if ($mode ==
'tobill') {
3625 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=-3&search_billed=0&mainmenu=commercial&leftmenu=orders';
3626 $label = $langs->trans(
"OrdersToBill");
3627 $labelShort = $langs->trans(
"ToBill");
3629 if ($mode ==
'shippedtobill') {
3630 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=3&search_billed=0&mainmenu=commercial&leftmenu=orders';
3631 $label = $langs->trans(
"OrdersToBill");
3632 $labelShort = $langs->trans(
"StatusOrderDelivered").
' '.$langs->trans(
"and").
' '.$langs->trans(
"ToBill");
3636 $response->warning_delay = $delay_warning;
3637 $response->label = $label;
3638 $response->labelShort = $labelShort;
3639 $response->url = $url;
3642 $generic_commande =
new Commande($this->db);
3644 while ($obj = $this->db->fetch_object($resql)) {
3645 $response->nbtodo++;
3646 $response->total += $obj->total_ht;
3648 $generic_commande->statut = $obj->fk_statut;
3649 $generic_commande->date_commande = $this->db->jdate($obj->date_commande);
3650 $generic_commande->date = $this->db->jdate($obj->date_commande);
3651 $generic_commande->date_livraison = $this->db->jdate($obj->delivery_date);
3652 $generic_commande->delivery_date = $this->db->jdate($obj->delivery_date);
3654 if ($mode ==
'toship' && $generic_commande->hasDelay()) {
3655 $response->nbtodolate++;
3661 $this->error = $this->db->error();
3675 $label = $langs->trans(
'OrderSource'.$this->source);
3677 if ($label ==
'OrderSource') {
3691 return $this->
LibStatut($this->statut, $this->billed, $mode);
3704 public function LibStatut($status, $billed, $mode, $donotshowbilled = 0)
3707 global $langs, $conf, $hookmanager;
3710 if (empty($donotshowbilled)) {
3711 $billedtext .= ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3716 if ($status == self::STATUS_CANCELED) {
3717 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderCanceled');
3718 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderCanceledShort');
3719 $statusType =
'status9';
3720 } elseif ($status == self::STATUS_DRAFT) {
3721 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDraft');
3722 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDraftShort');
3723 $statusType =
'status0';
3724 } elseif ($status == self::STATUS_VALIDATED) {
3725 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderValidated').$billedtext;
3726 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderValidatedShort').$billedtext;
3727 $statusType =
'status1';
3728 } elseif ($status == self::STATUS_SHIPMENTONPROCESS) {
3729 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderSent').$billedtext;
3730 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderSentShort').$billedtext;
3731 $labelTooltip = $langs->transnoentitiesnoconv(
"StatusOrderSent");
3732 if (!empty($this->delivery_date)) {
3733 $labelTooltip .=
' - '.$langs->transnoentitiesnoconv(
"DateDeliveryPlanned").dol_print_date($this->delivery_date,
'day').$billedtext;
3735 $statusType =
'status4';
3736 } elseif ($status == self::STATUS_CLOSED && (!$billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3737 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderToBill');
3738 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderToBillShort');
3739 $statusType =
'status4';
3740 } elseif ($status == self::STATUS_CLOSED && ($billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3741 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderProcessed').$billedtext;
3742 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderProcessedShort').$billedtext;
3743 $statusType =
'status6';
3744 } elseif ($status == self::STATUS_CLOSED && (!empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3745 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDelivered');
3746 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDeliveredShort');
3747 $statusType =
'status6';
3749 $labelStatus = $langs->transnoentitiesnoconv(
'Unknown');
3750 $labelStatusShort =
'';
3755 $parameters = array(
3756 'status' => $status,
3758 'billed' => $billed,
3759 'donotshowbilled' => $donotshowbilled
3762 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
3765 return $hookmanager->resPrint;
3768 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', array(
'tooltip' => $labelTooltip));
3779 global $conf, $langs, $user;
3781 $langs->load(
'orders');
3783 $nofetch = !empty($params[
'nofetch']);
3785 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
3786 return [
'optimize' => $langs->trans(
"Order")];
3789 if ($user->hasRight(
'commande',
'lire')) {
3790 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Order").
'</u>';
3791 if (isset($this->statut)) {
3792 $datas[
'status'] =
' '.$this->getLibStatut(5);
3794 $datas[
'Ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
3796 $langs->load(
'companies');
3797 if (empty($this->thirdparty)) {
3800 $datas[
'customer'] =
'<br><b>'.$langs->trans(
'Customer').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
3802 $datas[
'RefCustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.(empty($this->ref_customer) ? (empty($this->ref_client) ?
'' : $this->ref_client) : $this->ref_customer);
3804 $langs->load(
'project');
3805 if (empty($this->project)) {
3808 $datas[
'project'] =
'<br><b>'.$langs->trans(
'Project').
':</b> '.$this->project->getNomUrl(1,
'', 0, 1);
3812 if (!empty($this->total_ht)) {
3813 $datas[
'AmountHT'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
3815 if (!empty($this->total_tva)) {
3816 $datas[
'VAT'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
3818 if (!empty($this->total_ttc)) {
3819 $datas[
'AmountTTC'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
3821 if (!empty($this->date)) {
3822 $datas[
'Date'] =
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
3824 if (!empty($this->delivery_date)) {
3825 $datas[
'DeliveryDate'] =
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
3845 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0, $target =
'')
3847 global $conf, $langs, $user, $hookmanager;
3849 if (!empty($conf->dol_no_mouse_hover)) {
3855 if (
isModEnabled(
"expedition") && ($option ==
'1' || $option ==
'2')) {
3856 $url = DOL_URL_ROOT.
'/expedition/shipment.php?id='.$this->id;
3858 $url = DOL_URL_ROOT.
'/commande/card.php?id='.$this->id;
3861 if (!$user->rights->commande->lire) {
3865 if ($option !==
'nolink') {
3867 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3868 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
3869 $add_save_lastsearch_values = 1;
3871 if ($add_save_lastsearch_values) {
3872 $url .=
'&save_lastsearch_values=1';
3881 'objecttype' => $this->element,
3882 'option' => $option,
3885 $classfortooltip =
'classfortooltip';
3888 $classfortooltip =
'classforajaxtooltip';
3889 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
3896 if (empty($notooltip) && $user->hasRight(
'commande',
'lire')) {
3897 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
3898 $label = $langs->trans(
"Order");
3899 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
3901 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
3902 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
3904 $target_value = array(
'_self',
'_blank',
'_parent',
'_top');
3905 if (in_array($target, $target_value)) {
3906 $linkclose .=
' target="'.dol_escape_htmltag($target).
'"';
3910 $linkstart =
'<a href="'.$url.
'"';
3911 $linkstart .= $linkclose.
'>';
3914 if ($option ===
'nolink') {
3919 $result .= $linkstart;
3921 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
3923 if ($withpicto != 2) {
3924 $result .= $this->ref;
3926 $result .= $linkend;
3928 if ($addlinktonotes) {
3929 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3932 $result .=
' <span class="note inline-block">';
3933 $result .=
'<a href="'.DOL_URL_ROOT.
'/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3938 $result .=
'</span>';
3943 $hookmanager->initHooks(array($this->element .
'dao'));
3944 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
3945 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3947 $result = $hookmanager->resPrint;
3949 $result .= $hookmanager->resPrint;
3963 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem,';
3964 $sql .=
' date_valid as datev,';
3965 $sql .=
' date_cloture as datecloture,';
3966 $sql .=
' fk_user_author, fk_user_valid, fk_user_cloture';
3967 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
3968 $sql .=
' WHERE c.rowid = '.((int) $id);
3969 $result = $this->db->query(
$sql);
3971 if ($this->db->num_rows($result)) {
3972 $obj = $this->db->fetch_object($result);
3973 $this->
id = $obj->rowid;
3974 if ($obj->fk_user_author) {
3975 $this->user_creation_id = $obj->fk_user_author;
3977 if ($obj->fk_user_valid) {
3978 $this->user_validation_id = $obj->fk_user_valid;
3980 if ($obj->fk_user_cloture) {
3981 $this->user_closing_id = $obj->fk_user_cloture;
3984 $this->date_creation = $this->db->jdate($obj->datec);
3985 $this->date_modification = $this->db->jdate($obj->datem);
3986 $this->date_validation = $this->db->jdate($obj->datev);
3987 $this->date_cloture = $this->db->jdate($obj->datecloture);
3990 $this->db->free($result);
4006 global $conf, $langs;
4008 dol_syslog(get_class($this).
"::initAsSpecimen");
4013 $sql =
"SELECT rowid";
4014 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
4015 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
4016 $sql .= $this->db->plimit(100);
4018 $resql = $this->db->query(
$sql);
4020 $num_prods = $this->db->num_rows($resql);
4022 while ($i < $num_prods) {
4024 $row = $this->db->fetch_row($resql);
4025 $prodids[$i] = $row[0];
4031 $this->
ref =
'SPECIMEN';
4032 $this->specimen = 1;
4033 $this->entity = $conf->entity;
4035 $this->date = time();
4036 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
4037 $this->cond_reglement_code =
'RECEP';
4038 $this->mode_reglement_code =
'CHQ';
4039 $this->availability_code =
'DSP';
4040 $this->demand_reason_code =
'SRC_00';
4042 $this->note_public =
'This is a comment (public)';
4043 $this->note_private =
'This is a comment (private)';
4045 $this->multicurrency_tx = 1;
4046 $this->multicurrency_code = $conf->currency;
4051 while ($xnbp < $nbp) {
4054 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
4056 $line->subprice = 100;
4060 $line->total_ht = 50;
4061 $line->total_ttc = 60;
4062 $line->total_tva = 10;
4063 $line->remise_percent = 50;
4065 $line->total_ht = 100;
4066 $line->total_ttc = 120;
4067 $line->total_tva = 20;
4068 $line->remise_percent = 0;
4070 if ($num_prods > 0) {
4071 $prodid = mt_rand(1, $num_prods);
4072 $line->fk_product = $prodids[$prodid];
4073 $line->product_ref =
'SPECIMEN';
4076 $this->lines[$xnbp] = $line;
4078 $this->total_ht += $line->total_ht;
4079 $this->total_tva += $line->total_tva;
4080 $this->total_ttc += $line->total_ttc;
4098 $this->nb = array();
4101 $sql =
"SELECT count(co.rowid) as nb";
4102 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as co";
4103 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
4104 if (empty($user->rights->societe->client->voir) && !$user->socid) {
4105 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
4106 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
4109 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'commande').
")";
4111 $resql = $this->db->query(
$sql);
4113 while ($obj = $this->db->fetch_object($resql)) {
4114 $this->nb[
"orders"] = $obj->nb;
4116 $this->db->free($resql);
4120 $this->error = $this->db->error();
4146 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
4148 global $conf, $langs;
4150 $langs->load(
"orders");
4151 $outputlangs->load(
"products");
4154 $modele =
'einstein';
4156 if (!empty($this->model_pdf)) {
4157 $modele = $this->model_pdf;
4158 } elseif (!empty($this->modelpdf)) {
4159 $modele = $this->modelpdf;
4160 } elseif (!empty($conf->global->COMMANDE_ADDON_PDF)) {
4161 $modele = $conf->global->COMMANDE_ADDON_PDF;
4165 $modelpath =
"core/modules/commande/doc/";
4167 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
4220 return max($this->date, $this->delivery_date) < ($now - $conf->commande->client->warning_delay);
4230 global $conf, $langs;
4232 if (empty($this->delivery_date)) {
4233 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
4235 $text = $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->date_livraison,
'day');
4237 $text .=
' '.($conf->commande->client->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->client->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
4252 public $element =
'commandedet';
4254 public $table_element =
'commandedet';
4262 public $fk_commande;
4270 public $commande_id;
4272 public $fk_parent_line;
4284 public $fk_remise_except;
4290 public $fk_fournprice;
4310 public $skip_update_total;
4331 $sql =
'SELECT cd.rowid, cd.fk_commande, cd.fk_parent_line, cd.fk_product, cd.product_type, cd.label as custom_label, cd.description, cd.price, cd.qty, cd.tva_tx, cd.localtax1_tx, cd.localtax2_tx,';
4332 $sql .=
' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
4333 $sql .=
' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.fk_product_fournisseur_price as fk_fournprice, cd.buy_price_ht as pa_ht, cd.rang, cd.special_code,';
4334 $sql .=
' cd.fk_unit,';
4335 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
4336 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
4337 $sql .=
' cd.date_start, cd.date_end, cd.vat_src_code';
4338 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as cd';
4339 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
4340 $sql .=
' WHERE cd.rowid = '.((int) $rowid);
4341 $result = $this->db->query(
$sql);
4343 $objp = $this->db->fetch_object($result);
4346 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.
$sql;
4350 $this->
rowid = $objp->rowid;
4351 $this->
id = $objp->rowid;
4352 $this->fk_commande = $objp->fk_commande;
4353 $this->fk_parent_line = $objp->fk_parent_line;
4354 $this->label = $objp->custom_label;
4355 $this->desc = $objp->description;
4356 $this->qty = $objp->qty;
4357 $this->
price = $objp->price;
4358 $this->subprice = $objp->subprice;
4359 $this->ref_ext = $objp->ref_ext;
4360 $this->vat_src_code = $objp->vat_src_code;
4361 $this->tva_tx = $objp->tva_tx;
4362 $this->localtax1_tx = $objp->localtax1_tx;
4363 $this->localtax2_tx = $objp->localtax2_tx;
4364 $this->remise = $objp->remise;
4365 $this->remise_percent = $objp->remise_percent;
4366 $this->fk_remise_except = $objp->fk_remise_except;
4367 $this->fk_product = $objp->fk_product;
4368 $this->product_type = $objp->product_type;
4369 $this->info_bits = $objp->info_bits;
4370 $this->special_code = $objp->special_code;
4371 $this->total_ht = $objp->total_ht;
4372 $this->total_tva = $objp->total_tva;
4373 $this->total_localtax1 = $objp->total_localtax1;
4374 $this->total_localtax2 = $objp->total_localtax2;
4375 $this->total_ttc = $objp->total_ttc;
4376 $this->fk_fournprice = $objp->fk_fournprice;
4377 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
4378 $this->pa_ht = $marginInfos[0];
4379 $this->marge_tx = $marginInfos[1];
4380 $this->marque_tx = $marginInfos[2];
4381 $this->special_code = $objp->special_code;
4382 $this->rang = $objp->rang;
4384 $this->
ref = $objp->product_ref;
4386 $this->product_ref = $objp->product_ref;
4387 $this->product_label = $objp->product_label;
4388 $this->product_desc = $objp->product_desc;
4389 $this->product_tobatch = $objp->product_tobatch;
4390 $this->fk_unit = $objp->fk_unit;
4392 $this->date_start = $this->db->jdate($objp->date_start);
4393 $this->date_end = $this->db->jdate($objp->date_end);
4395 $this->fk_multicurrency = $objp->fk_multicurrency;
4396 $this->multicurrency_code = $objp->multicurrency_code;
4397 $this->multicurrency_subprice = $objp->multicurrency_subprice;
4398 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
4399 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
4400 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
4402 $this->db->free($result);
4406 $this->error = $this->db->lasterror();
4418 public function delete(
User $user, $notrigger = 0)
4420 global $conf, $langs;
4424 if (empty($this->
id) && !empty($this->
rowid)) {
4425 $this->
id = $this->rowid;
4429 $sqlCheckShipmentLine =
"SELECT";
4430 $sqlCheckShipmentLine .=
" ed.rowid";
4431 $sqlCheckShipmentLine .=
" FROM ".MAIN_DB_PREFIX.
"expeditiondet ed";
4432 $sqlCheckShipmentLine .=
" WHERE ed.fk_origin_line = ".((int) $this->
id);
4434 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
4435 if (!$resqlCheckShipmentLine) {
4437 $this->error = $this->db->lasterror();
4438 $this->errors[] = $this->error;
4440 $langs->load(
'errors');
4441 $num = $this->db->num_rows($resqlCheckShipmentLine);
4444 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
4445 $this->error = $langs->trans(
'ErrorRecordAlreadyExists').
' : '.$langs->trans(
'ShipmentLine').
' '.$objCheckShipmentLine->rowid;
4446 $this->errors[] = $this->error;
4448 $this->db->free($resqlCheckShipmentLine);
4451 dol_syslog(__METHOD__.
'Error ; '.$this->error, LOG_ERR);
4457 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"commandedet WHERE rowid = ".((int) $this->
id);
4460 $resql = $this->db->query(
$sql);
4462 if (!$error && !$notrigger) {
4464 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
4476 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
4481 $this->db->commit();
4485 foreach ($this->errors as $errmsg) {
4486 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
4487 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4489 $this->db->rollback();
4492 $this->error = $this->db->lasterror();
4504 public function insert($user =
null, $notrigger = 0)
4506 global $langs, $conf;
4510 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4512 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
4515 if (empty($this->tva_tx)) {
4518 if (empty($this->localtax1_tx)) {
4519 $this->localtax1_tx = 0;
4521 if (empty($this->localtax2_tx)) {
4522 $this->localtax2_tx = 0;
4524 if (empty($this->localtax1_type)) {
4525 $this->localtax1_type = 0;
4527 if (empty($this->localtax2_type)) {
4528 $this->localtax2_type = 0;
4530 if (empty($this->total_localtax1)) {
4531 $this->total_localtax1 = 0;
4533 if (empty($this->total_localtax2)) {
4534 $this->total_localtax2 = 0;
4536 if (empty($this->rang)) {
4539 if (empty($this->remise_percent)) {
4540 $this->remise_percent = 0;
4542 if (empty($this->info_bits)) {
4543 $this->info_bits = 0;
4545 if (empty($this->special_code)) {
4546 $this->special_code = 0;
4548 if (empty($this->fk_parent_line)) {
4549 $this->fk_parent_line = 0;
4551 if (empty($this->pa_ht)) {
4554 if (empty($this->ref_ext)) {
4555 $this->ref_ext =
'';
4559 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4560 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4564 $this->pa_ht = $result;
4569 if ($this->product_type < 0) {
4576 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'commandedet';
4577 $sql .=
' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
4578 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
4579 $sql .=
' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
4580 $sql .=
' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
4581 $sql .=
' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
4583 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
4585 $sql .=
" VALUES (".$this->fk_commande.
",";
4586 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
4587 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
4588 $sql .=
" '".$this->db->escape($this->desc).
"',";
4589 $sql .=
" '".price2num($this->qty).
"',";
4590 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
4591 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
4592 $sql .=
" '".price2num($this->tva_tx).
"',";
4593 $sql .=
" '".price2num($this->localtax1_tx).
"',";
4594 $sql .=
" '".price2num($this->localtax2_tx).
"',";
4595 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
4596 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
4597 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
4598 $sql .=
" '".$this->db->escape($this->product_type).
"',";
4599 $sql .=
" '".price2num($this->remise_percent).
"',";
4600 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice) :
"null").
",";
4601 $sql .=
" ".($this->price !=
'' ?
"'".price2num($this->
price).
"'" :
"null").
",";
4602 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
4603 $sql .=
' '.((int) $this->special_code).
',';
4604 $sql .=
' '.((int) $this->rang).
',';
4605 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
4606 $sql .=
' '.price2num($this->pa_ht).
',';
4607 $sql .=
" ".((int) $this->info_bits).
",";
4608 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
4609 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
4610 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
4611 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
4612 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
4613 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
4614 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
',';
4615 $sql .=
' '.(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
4616 $sql .=
", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) :
'NULL');
4617 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
4618 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
4619 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
4620 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
4621 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
4624 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
4625 $resql = $this->db->query(
$sql);
4627 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
4628 $this->
rowid = $this->id;
4637 if (!$error && !$notrigger) {
4639 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
4647 $this->db->commit();
4651 foreach ($this->errors as $errmsg) {
4652 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
4653 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4655 $this->db->rollback();
4658 $this->error = $this->db->error();
4659 $this->db->rollback();
4673 global $conf, $langs;
4677 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4680 if (empty($this->tva_tx)) {
4683 if (empty($this->localtax1_tx)) {
4684 $this->localtax1_tx = 0;
4686 if (empty($this->localtax2_tx)) {
4687 $this->localtax2_tx = 0;
4689 if (empty($this->localtax1_type)) {
4690 $this->localtax1_type = 0;
4692 if (empty($this->localtax2_type)) {
4693 $this->localtax2_type = 0;
4695 if (empty($this->qty)) {
4698 if (empty($this->total_localtax1)) {
4699 $this->total_localtax1 = 0;
4701 if (empty($this->total_localtax2)) {
4702 $this->total_localtax2 = 0;
4704 if (empty($this->marque_tx)) {
4705 $this->marque_tx = 0;
4707 if (empty($this->marge_tx)) {
4708 $this->marge_tx = 0;
4710 if (empty($this->remise_percent)) {
4711 $this->remise_percent = 0;
4713 if (empty($this->remise)) {
4716 if (empty($this->info_bits)) {
4717 $this->info_bits = 0;
4719 if (empty($this->special_code)) {
4720 $this->special_code = 0;
4722 if (empty($this->product_type)) {
4723 $this->product_type = 0;
4725 if (empty($this->fk_parent_line)) {
4726 $this->fk_parent_line = 0;
4728 if (empty($this->pa_ht)) {
4731 if (empty($this->ref_ext)) {
4732 $this->ref_ext =
'';
4736 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4737 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4741 $this->pa_ht = $result;
4748 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4749 $sql .=
" description='".$this->db->escape($this->desc).
"'";
4750 $sql .=
" , label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
4751 $sql .=
" , vat_src_code=".(!empty($this->vat_src_code) ?
"'".$this->db->escape($this->vat_src_code).
"'" :
"''");
4752 $sql .=
" , tva_tx=".price2num($this->tva_tx);
4753 $sql .=
" , localtax1_tx=".price2num($this->localtax1_tx);
4754 $sql .=
" , localtax2_tx=".price2num($this->localtax2_tx);
4755 $sql .=
" , localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
4756 $sql .=
" , localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
4757 $sql .=
" , qty=".price2num($this->qty);
4758 $sql .=
" , ref_ext='".$this->db->escape($this->ref_ext).
"'";
4759 $sql .=
" , subprice=".price2num($this->subprice);
4760 $sql .=
" , remise_percent=".price2num($this->remise_percent);
4761 $sql .=
" , price=".price2num($this->
price);
4762 $sql .=
" , remise=".price2num($this->remise);
4763 if (empty($this->skip_update_total)) {
4764 $sql .=
" , total_ht=".price2num($this->total_ht);
4765 $sql .=
" , total_tva=".price2num($this->total_tva);
4766 $sql .=
" , total_ttc=".price2num($this->total_ttc);
4767 $sql .=
" , total_localtax1=".price2num($this->total_localtax1);
4768 $sql .=
" , total_localtax2=".price2num($this->total_localtax2);
4770 $sql .=
" , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null");
4771 $sql .=
" , buy_price_ht='".price2num($this->pa_ht).
"'";
4772 $sql .=
" , info_bits=".((int) $this->info_bits);
4773 $sql .=
" , special_code=".((int) $this->special_code);
4774 $sql .=
" , date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
4775 $sql .=
" , date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
4776 $sql .=
" , product_type=".$this->product_type;
4777 $sql .=
" , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line :
"null");
4778 if (!empty($this->rang)) {
4779 $sql .=
", rang=".((int) $this->rang);
4781 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
4784 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice);
4785 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
4786 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
4787 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
4789 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4791 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
4792 $resql = $this->db->query(
$sql);
4795 $this->
id = $this->rowid;
4802 if (!$error && !$notrigger) {
4804 $result = $this->
call_trigger(
'LINEORDER_MODIFY', $user);
4812 $this->db->commit();
4816 foreach ($this->errors as $errmsg) {
4817 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
4818 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4820 $this->db->rollback();
4823 $this->error = $this->db->error();
4824 $this->db->rollback();
4842 if (empty($this->total_localtax1)) {
4843 $this->total_localtax1 = 0;
4845 if (empty($this->total_localtax2)) {
4846 $this->total_localtax2 = 0;
4850 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4851 $sql .=
" total_ht='".price2num($this->total_ht).
"'";
4852 $sql .=
",total_tva='".price2num($this->total_tva).
"'";
4853 $sql .=
",total_localtax1='".price2num($this->total_localtax1).
"'";
4854 $sql .=
",total_localtax2='".price2num($this->total_localtax2).
"'";
4855 $sql .=
",total_ttc='".price2num($this->total_ttc).
"'";
4856 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4858 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
4860 $resql = $this->db->query(
$sql);
4862 $this->db->commit();
4865 $this->error = $this->db->error();
4866 $this->db->rollback();