34 include_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
35 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
36 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
37 require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
38 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
49 public $element =
'commande';
54 public $table_element =
'commande';
59 public $table_element_line =
'commandedet';
64 public $class_element_line =
'OrderLine';
69 public $fk_element =
'fk_commande';
74 public $picto =
'order';
80 public $ismultientitymanaged = 1;
86 public $restrictiononfksoc = 1;
129 public $cond_reglement_code;
139 public $mode_reglement;
144 public $mode_reglement_id;
149 public $mode_reglement_code;
155 public $availability_id;
161 public $availability_code;
167 public $availability;
169 public $demand_reason_id;
170 public $demand_reason_code;
181 public $date_commande;
187 public $date_livraison;
189 public $delivery_date;
194 public $fk_remise_except;
196 public $remise_percent;
197 public $remise_absolue;
200 public $special_code;
203 public $extraparams = array();
205 public $linked_objects = array();
210 public $user_author_id;
220 public $lines = array();
226 public $fk_multicurrency;
228 public $multicurrency_code;
229 public $multicurrency_tx;
230 public $multicurrency_total_ht;
231 public $multicurrency_total_tva;
232 public $multicurrency_total_ttc;
273 public $fields = array(
274 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
275 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>20,
'index'=>1),
276 'ref' =>array(
'type'=>
'varchar(30)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'showoncombobox'=>1,
'position'=>25),
277 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefExt',
'enabled'=>1,
'visible'=>0,
'position'=>26),
278 'ref_int' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefInt',
'enabled'=>1,
'visible'=>0,
'position'=>27),
279 'ref_client' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefCustomer',
'enabled'=>1,
'visible'=>-1,
'position'=>28),
280 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>20),
281 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:fk_statut=1',
'label'=>
'Project',
'enabled'=>1,
'visible'=>-1,
'position'=>25),
282 'date_creation' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-1,
'position'=>55),
283 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>56),
284 'date_valid' =>array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>60),
285 'date_cloture' =>array(
'type'=>
'datetime',
'label'=>
'DateClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>65),
286 'date_commande' =>array(
'type'=>
'date',
'label'=>
'Date',
'enabled'=>1,
'visible'=>-1,
'position'=>70),
287 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-1,
'position'=>75),
288 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>80),
289 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>85),
290 'fk_user_cloture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
291 'source' =>array(
'type'=>
'smallint(6)',
'label'=>
'Source',
'enabled'=>1,
'visible'=>-1,
'position'=>95),
293 'remise_percent' =>array(
'type'=>
'double',
'label'=>
'RelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>110),
294 'remise_absolue' =>array(
'type'=>
'double',
'label'=>
'CustomerRelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>115),
296 'tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'VAT',
'enabled'=>1,
'visible'=>-1,
'position'=>125,
'isameasure'=>1),
297 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax1',
'enabled'=>1,
'visible'=>-1,
'position'=>130,
'isameasure'=>1),
298 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax2',
'enabled'=>1,
'visible'=>-1,
'position'=>135,
'isameasure'=>1),
299 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalHT',
'enabled'=>1,
'visible'=>-1,
'position'=>140,
'isameasure'=>1),
300 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>145,
'isameasure'=>1),
301 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>150),
302 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>155),
303 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'PDFTemplate',
'enabled'=>1,
'visible'=>0,
'position'=>160),
305 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'BankAccount',
'enabled'=>1,
'visible'=>-1,
'position'=>170),
306 'fk_currency' =>array(
'type'=>
'varchar(3)',
'label'=>
'MulticurrencyID',
'enabled'=>1,
'visible'=>-1,
'position'=>175),
307 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
308 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
309 'date_livraison' =>array(
'type'=>
'date',
'label'=>
'DateDeliveryPlanned',
'enabled'=>1,
'visible'=>-1,
'position'=>190),
310 'fk_shipping_method' =>array(
'type'=>
'integer',
'label'=>
'ShippingMethod',
'enabled'=>1,
'visible'=>-1,
'position'=>195),
311 'fk_warehouse' =>array(
'type'=>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label'=>
'Fk warehouse',
'enabled'=>1,
'visible'=>-1,
'position'=>200),
312 'fk_availability' =>array(
'type'=>
'integer',
'label'=>
'Availability',
'enabled'=>1,
'visible'=>-1,
'position'=>205),
313 'fk_input_reason' =>array(
'type'=>
'integer',
'label'=>
'InputReason',
'enabled'=>1,
'visible'=>-1,
'position'=>210),
315 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>225),
316 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>230),
317 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLabel',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>235),
318 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>240),
319 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'MulticurrencyCurrency',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>245),
320 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyRate',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>250,
'isameasure'=>1),
321 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>255,
'isameasure'=>1),
322 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>260,
'isameasure'=>1),
323 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>265,
'isameasure'=>1),
324 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>-1,
'position'=>270),
325 'module_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSModule',
'enabled'=>1,
'visible'=>-1,
'position'=>275),
326 'pos_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSTerminal',
'enabled'=>1,
'visible'=>-1,
'position'=>280),
327 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>-1,
'position'=>500),
328 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>900),
353 const STATUS_ACCEPTED = 2;
371 $this->remise_percent = 0;
383 global $langs, $conf;
384 $langs->load(
"order");
386 if (!empty($conf->global->COMMANDE_ADDON))
390 $file = $conf->global->COMMANDE_ADDON.
".php";
391 $classname = $conf->global->COMMANDE_ADDON;
394 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
395 foreach ($dirmodels as $reldir)
400 $mybool |= @include_once $dir.$file;
403 if ($mybool ===
false)
409 $obj =
new $classname();
410 $numref = $obj->getNextValue($soc, $this);
416 $this->error = $obj->error;
421 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_COMMANDE_ADDON_NotDefined");
435 public function valid($user, $idwarehouse = 0, $notrigger = 0)
437 global $conf, $langs;
439 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
444 if ($this->
statut == self::STATUS_VALIDATED)
446 dol_syslog(get_class($this).
"::valid action abandonned: already validated", LOG_WARNING);
450 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
451 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate))))
453 $this->error =
'NotEnoughPermissions';
454 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
464 $soc->fetch($this->socid);
467 $result = $soc->set_as_client();
470 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->ref) || empty($this->ref)))
479 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
480 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
481 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
482 $sql .=
" date_valid='".$this->db->idate($now).
"',";
483 $sql .=
" fk_user_valid = ".$user->id;
484 $sql .=
" WHERE rowid = ".$this->id;
486 dol_syslog(get_class($this).
"::valid", LOG_DEBUG);
491 $this->error = $this->
db->lasterror();
497 if ($result >= 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
498 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
499 $langs->load(
"agenda");
502 $cpt = count($this->lines);
503 for ($i = 0; $i < $cpt; $i++) {
504 if ($this->lines[$i]->fk_product > 0) {
506 $mouvP->origin = &$this;
508 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"OrderValidatedInDolibarr", $num));
511 $this->error = $mouvP->error;
519 if (!$error && !$notrigger) {
522 if ($result < 0) $error++;
527 $this->oldref = $this->ref;
530 if (preg_match(
'/^[\(]?PROV/i', $this->ref))
533 $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).
"'";
534 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->ref).
"%' AND filepath = 'commande/".$this->
db->escape($this->ref).
"' and entity = ".$conf->entity;
536 if (!
$resql) { $error++; $this->error = $this->
db->lasterror(); }
541 $dirsource = $conf->commande->multidir_output[$this->entity].
'/'.$oldref;
542 $dirdest = $conf->commande->multidir_output[$this->entity].
'/'.$newref;
543 if (!$error && file_exists($dirsource))
545 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
547 if (@rename($dirsource, $dirdest))
551 $listoffiles =
dol_dir_list($conf->commande->multidir_output[$this->entity].
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
552 foreach ($listoffiles as $fileentry)
554 $dirsource = $fileentry[
'name'];
555 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
556 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
557 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
558 @rename($dirsource, $dirdest);
569 $this->
statut = self::STATUS_VALIDATED;
570 $this->brouillon = 0;
578 $this->
db->rollback();
594 global $conf, $langs;
599 if ($this->
statut <= self::STATUS_DRAFT)
604 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
605 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate))))
607 $this->error =
'Permission denied';
615 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
616 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT;
617 $sql .=
" WHERE rowid = ".$this->id;
619 if ($this->
db->query($sql))
623 $this->oldcopy = clone $this;
627 if (!empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1)
631 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
632 $langs->load(
"agenda");
634 $num = count($this->lines);
635 for ($i = 0; $i < $num; $i++)
637 if ($this->lines[$i]->fk_product > 0)
640 $mouvP->origin = &$this;
642 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderBackToDraftInDolibarr", $this->ref));
643 if ($result < 0) { $error++; $this->error = $mouvP->error;
break; }
650 $result = $this->
call_trigger(
'ORDER_UNVALIDATE', $user);
651 if ($result < 0) $error++;
655 $this->
statut = self::STATUS_DRAFT;
659 $this->
db->rollback();
663 $this->error = $this->
db->error();
664 $this->
db->rollback();
683 if ($this->
statut != self::STATUS_CANCELED && $this->
statut != self::STATUS_CLOSED)
685 dol_syslog(get_class($this).
"::set_reopen order has not status closed", LOG_WARNING);
691 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
692 $sql .=
' SET fk_statut='.self::STATUS_VALIDATED.
', facture=0';
693 $sql .=
' WHERE rowid = '.$this->id;
695 dol_syslog(get_class($this).
"::set_reopen", LOG_DEBUG);
701 if ($result < 0) $error++;
705 $this->error = $this->
db->lasterror();
711 $this->
statut = self::STATUS_VALIDATED;
717 foreach ($this->errors as $errmsg)
719 dol_syslog(get_class($this).
"::set_reopen ".$errmsg, LOG_ERR);
720 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
722 $this->
db->rollback();
734 public function cloture($user, $notrigger = 0)
740 $usercanclose = ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
741 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->close)));
745 if ($this->
statut == self::STATUS_CLOSED)
753 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
754 $sql .=
' SET fk_statut = '.self::STATUS_CLOSED.
',';
755 $sql .=
' fk_user_cloture = '.$user->id.
',';
756 $sql .=
" date_cloture = '".$this->db->idate($now).
"'";
757 $sql .=
' WHERE rowid = '.$this->id.
' AND fk_statut > '.self::STATUS_DRAFT;
759 if ($this->
db->query($sql))
765 if ($result < 0) $error++;
771 $this->
statut = self::STATUS_CLOSED;
776 $this->
db->rollback();
780 $this->error = $this->
db->lasterror();
782 $this->
db->rollback();
796 public function cancel($idwarehouse = -1)
798 global $conf, $user, $langs;
804 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
805 $sql .=
" SET fk_statut = ".self::STATUS_CANCELED;
806 $sql .=
" WHERE rowid = ".$this->id;
807 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
809 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
810 if ($this->
db->query($sql))
813 if (!empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1)
815 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
816 $langs->load(
"agenda");
818 $num = count($this->lines);
819 for ($i = 0; $i < $num; $i++)
821 if ($this->lines[$i]->fk_product > 0)
825 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderCanceledInDolibarr", $this->ref));
829 $this->error = $mouvP->error;
840 if ($result < 0) $error++;
846 $this->
statut = self::STATUS_CANCELED;
850 foreach ($this->errors as $errmsg)
852 dol_syslog(get_class($this).
"::cancel ".$errmsg, LOG_ERR);
853 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
855 $this->
db->rollback();
859 $this->error = $this->
db->error();
860 $this->
db->rollback();
873 public function create($user, $notrigger = 0)
875 global $conf, $langs;
879 $this->brouillon = 1;
882 $date = ($this->date_commande ? $this->date_commande : $this->date);
883 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
886 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) list($this->fk_multicurrency, $this->multicurrency_tx) =
MultiCurrency::getIdAndTxFromCode($this->
db, $this->multicurrency_code, $date);
888 if (empty($this->fk_multicurrency))
890 $this->multicurrency_code = $conf->currency;
891 $this->fk_multicurrency = 0;
892 $this->multicurrency_tx = 1;
895 dol_syslog(get_class($this).
"::create user=".$user->id);
898 if (!empty($this->ref))
900 $result = self::isExistingObject($this->element, 0, $this->ref);
903 $this->error =
'ErrorRefAlreadyExists';
904 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
905 $this->
db->rollback();
911 $result = $soc->fetch($this->socid);
914 $this->error =
"Failed to fetch company";
915 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
918 if (!empty($conf->global->ORDER_REQUIRE_SOURCE) && $this->source < 0)
920 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Source"));
921 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
929 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande (";
930 $sql .=
" ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client, ref_int";
931 $sql .=
", model_pdf, fk_cond_reglement, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address";
932 $sql .=
", fk_shipping_method";
933 $sql .=
", fk_warehouse";
934 $sql .=
", remise_absolue, remise_percent";
935 $sql .=
", fk_incoterms, location_incoterms";
936 $sql .=
", entity, module_source, pos_source";
937 $sql .=
", fk_multicurrency";
938 $sql .=
", multicurrency_code";
939 $sql .=
", multicurrency_tx";
941 $sql .=
" VALUES ('(PROV)', ".$this->socid.
", '".$this->
db->idate($now).
"', ".$user->id;
942 $sql .=
", ".($this->fk_project > 0 ? $this->fk_project :
"null");
943 $sql .=
", '".$this->db->idate($date).
"'";
944 $sql .=
", ".($this->source >= 0 && $this->source !=
'' ? $this->
db->escape($this->source) :
'null');
945 $sql .=
", '".$this->db->escape($this->note_private).
"'";
946 $sql .=
", '".$this->db->escape($this->note_public).
"'";
947 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
948 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
949 $sql .=
", ".($this->ref_int ?
"'".$this->db->escape($this->ref_int).
"'" :
"null");
950 $sql .=
", '".$this->db->escape($this->model_pdf).
"'";
951 $sql .=
", ".($this->cond_reglement_id > 0 ? $this->cond_reglement_id :
"null");
952 $sql .=
", ".($this->mode_reglement_id > 0 ? $this->mode_reglement_id :
"null");
953 $sql .=
", ".($this->fk_account > 0 ? $this->fk_account :
'NULL');
954 $sql .=
", ".($this->availability_id > 0 ? $this->availability_id :
"null");
955 $sql .=
", ".($this->demand_reason_id > 0 ? $this->demand_reason_id :
"null");
956 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
957 $sql .=
", ".($this->fk_delivery_address > 0 ? $this->fk_delivery_address :
'NULL');
958 $sql .=
", ".(!empty($this->shipping_method_id) && $this->shipping_method_id > 0 ? $this->shipping_method_id :
'NULL');
959 $sql .=
", ".(!empty($this->warehouse_id) && $this->warehouse_id > 0 ? $this->warehouse_id :
'NULL');
960 $sql .=
", ".($this->remise_absolue > 0 ? $this->
db->escape($this->remise_absolue) :
'NULL');
961 $sql .=
", ".($this->remise_percent > 0 ? $this->
db->escape($this->remise_percent) : 0);
962 $sql .=
", ".(int) $this->fk_incoterms;
963 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
964 $sql .=
", ".setEntity($this);
965 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
966 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
967 $sql .=
", ".(int) $this->fk_multicurrency;
968 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
969 $sql .=
", ".(double) $this->multicurrency_tx;
972 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
976 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'commande');
981 $num = count($this->lines);
986 for ($i = 0; $i < $num; $i++)
988 $line = $this->lines[$i];
992 if (!is_object($line)) $line = (object) $line;
995 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1000 $vatrate = $line->tva_tx;
1001 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', $vatrate)) $vatrate .=
' ('.$line->vat_src_code.
')';
1003 if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
1004 $originid = $line->origin_id;
1005 $origintype = $line->origin;
1007 $originid = $line->id;
1008 $origintype = $this->element;
1012 if (empty($line->ref_ext)) {
1013 $line->ref_ext =
'';
1021 $line->localtax1_tx,
1022 $line->localtax2_tx,
1024 $line->remise_percent,
1026 $line->fk_remise_except,
1031 $line->product_type,
1033 $line->special_code,
1035 $line->fk_fournprice,
1038 $line->array_options,
1047 if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER)
1049 $this->error = $this->
db->lasterror();
1050 $this->errors[] = $this->error;
1053 $this->
db->rollback();
1057 if ($result > 0 && $line->product_type == 9) {
1058 $fk_parent_line = $result;
1063 $initialref =
'(PROV'.$this->id.
')';
1064 if (!empty($this->ref)) $initialref = $this->ref;
1066 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"commande SET ref='".$this->
db->escape($initialref).
"' WHERE rowid=".$this->id;
1067 if ($this->
db->query($sql))
1069 $this->ref = $initialref;
1071 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects))
1073 $this->linked_objects = $this->linkedObjectsIds;
1077 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects))
1079 foreach ($this->linked_objects as $origin => $tmp_origin_id)
1081 if (is_array($tmp_origin_id))
1083 foreach ($tmp_origin_id as $origin_id)
1088 $this->error = $this->
db->lasterror();
1094 $origin_id = $tmp_origin_id;
1098 $this->error = $this->
db->lasterror();
1105 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id))
1107 $originforcontact = $this->origin;
1108 $originidforcontact = $this->origin_id;
1109 if ($originforcontact ==
'shipping')
1111 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1113 $exp->fetch($this->origin_id);
1114 $exp->fetchObjectLinked();
1115 if (count($exp->linkedObjectsIds[
'commande']) > 0)
1117 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value)
1119 $originforcontact =
'commande';
1120 if (is_object($value)) $originidforcontact = $value->id;
1121 else $originidforcontact = $value;
1127 $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";
1128 $sqlcontact .=
" WHERE element_id = ".$originidforcontact.
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->
db->escape($originforcontact).
"'";
1130 $resqlcontact = $this->
db->query($sqlcontact);
1133 while ($objcontact = $this->
db->fetch_object($resqlcontact))
1136 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1144 if ($result < 0) $error++;
1147 if (!$error && !$notrigger)
1151 if ($result < 0) $error++;
1157 $this->
db->commit();
1160 $this->
db->rollback();
1164 $this->error = $this->
db->lasterror();
1165 $this->
db->rollback();
1171 $this->
db->rollback();
1186 global $conf, $user, $hookmanager;
1193 foreach ($this->lines as $line)
1194 $line->fetch_optionals();
1197 $objFrom = clone $this;
1200 if (!empty($socid) && $socid != $this->socid)
1204 if ($objsoc->fetch($socid) > 0)
1206 $this->socid = $objsoc->id;
1207 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1208 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1209 $this->fk_project = 0;
1210 $this->fk_delivery_address = 0;
1218 $this->
statut = self::STATUS_DRAFT;
1221 $this->user_author_id = $user->id;
1222 $this->user_valid =
'';
1224 $this->date_commande =
dol_now();
1225 $this->date_creation =
'';
1226 $this->date_validation =
'';
1227 if (empty($conf->global->MAIN_KEEP_REF_CUSTOMER_ON_CLONING)) $this->ref_client =
'';
1230 $num = count($this->lines);
1231 for ($i = 0; $i < $num; $i++)
1233 $this->lines[$i]->ref_ext =
'';
1237 $this->context[
'createfromclone'] =
'createfromclone';
1238 $result = $this->
create($user);
1239 if ($result < 0) $error++;
1253 if ($this->socid == $objFrom->socid)
1263 if (is_object($hookmanager))
1265 $parameters = array(
'objFrom'=>$objFrom);
1267 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1268 if ($reshook < 0) $error++;
1272 unset($this->context[
'createfromclone']);
1277 $this->
db->commit();
1280 $this->
db->rollback();
1295 global $conf, $hookmanager;
1302 $this->date_commande =
dol_now();
1305 $num = count($object->lines);
1306 for ($i = 0; $i < $num; $i++)
1310 $line->libelle = $object->lines[$i]->libelle;
1311 $line->label = $object->lines[$i]->label;
1312 $line->desc = $object->lines[$i]->desc;
1313 $line->price = $object->lines[$i]->price;
1314 $line->subprice = $object->lines[$i]->subprice;
1315 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1316 $line->tva_tx = $object->lines[$i]->tva_tx;
1317 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1318 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1319 $line->qty = $object->lines[$i]->qty;
1320 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1321 $line->remise_percent = $object->lines[$i]->remise_percent;
1322 $line->fk_product = $object->lines[$i]->fk_product;
1323 $line->info_bits = $object->lines[$i]->info_bits;
1324 $line->product_type = $object->lines[$i]->product_type;
1325 $line->rang = $object->lines[$i]->rang;
1326 $line->special_code = $object->lines[$i]->special_code;
1327 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1328 $line->fk_unit = $object->lines[$i]->fk_unit;
1330 $line->date_start = $object->lines[$i]->date_start;
1331 $line->date_end = $object->lines[$i]->date_end;
1333 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1334 $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);
1335 $line->pa_ht = $marginInfos[0];
1336 $line->marge_tx = $marginInfos[1];
1337 $line->marque_tx = $marginInfos[2];
1340 $object->lines[$i]->fetch_optionals();
1341 foreach ($object->lines[$i]->array_options as $options_key => $value)
1342 $line->array_options[$options_key] = $value;
1344 $this->lines[$i] = $line;
1347 $this->entity = $object->entity;
1348 $this->socid = $object->socid;
1349 $this->fk_project = $object->fk_project;
1350 $this->cond_reglement_id = $object->cond_reglement_id;
1351 $this->mode_reglement_id = $object->mode_reglement_id;
1352 $this->fk_account = $object->fk_account;
1353 $this->availability_id = $object->availability_id;
1354 $this->demand_reason_id = $object->demand_reason_id;
1355 $this->date_livraison = $object->date_livraison;
1356 $this->delivery_date = $object->date_livraison;
1357 $this->shipping_method_id = $object->shipping_method_id;
1358 $this->warehouse_id = $object->warehouse_id;
1359 $this->fk_delivery_address = $object->fk_delivery_address;
1360 $this->contact_id = $object->contact_id;
1361 $this->ref_client = $object->ref_client;
1363 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN))
1365 $this->note_private = $object->note_private;
1366 $this->note_public = $object->note_public;
1369 $this->origin = $object->element;
1370 $this->origin_id = $object->id;
1373 $object->fetch_optionals();
1376 $element_extrafields = $e->fetch_name_optionals_label($this->table_element);
1378 foreach ($object->array_options as $options_key => $value) {
1379 if (array_key_exists(str_replace(
'options_',
'', $options_key), $element_extrafields)) {
1380 $this->array_options[$options_key] = $value;
1384 $this->linked_objects[$this->origin] = $this->origin_id;
1385 if (is_array($object->other_linked_objects) && !empty($object->other_linked_objects))
1387 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1390 $ret = $this->
create($user);
1395 $hookmanager->initHooks(array(
'orderdao'));
1397 $parameters = array(
'objFrom'=>$object);
1399 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1400 if ($reshook < 0) $error++;
1405 if (!empty($conf->global->ORDER_VALID_AFTER_CLOSE_PROPAL))
1408 $this->
valid($user);
1455 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 =
'')
1457 global $mysoc, $conf, $langs, $user;
1459 $logtext =
"::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent";
1460 $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";
1461 $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";
1462 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
1464 if ($this->
statut == self::STATUS_DRAFT)
1466 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1470 if (empty($remise_percent)) $remise_percent = 0;
1471 if (empty($qty)) $qty = 0;
1472 if (empty($info_bits)) $info_bits = 0;
1473 if (empty($rang)) $rang = 0;
1474 if (empty($txtva)) $txtva = 0;
1475 if (empty($txlocaltax1)) $txlocaltax1 = 0;
1476 if (empty($txlocaltax2)) $txlocaltax2 = 0;
1477 if (empty($fk_parent_line) || $fk_parent_line < 0) $fk_parent_line = 0;
1478 if (empty($this->fk_multicurrency)) $this->fk_multicurrency = 0;
1479 if (empty($ref_ext)) $ref_ext =
'';
1481 $remise_percent =
price2num($remise_percent);
1484 $pu_ht_devise =
price2num($pu_ht_devise);
1487 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1492 if ($price_base_type ==
'HT')
1498 $label = trim($label);
1499 $desc = trim($desc);
1502 if ($type < 0)
return -1;
1504 if ($date_start && $date_end && $date_start > $date_end) {
1505 $langs->load(
"errors");
1506 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1512 $product_type = $type;
1513 if (!empty($fk_product))
1516 $result = $product->fetch($fk_product);
1517 $product_type = $product->type;
1519 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty)
1521 $langs->load(
"errors");
1522 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
1523 $this->errors[] = $this->error;
1524 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
1525 $this->
db->rollback();
1526 return self::STOCK_NOT_ENOUGH_FOR_ORDER;
1539 if (preg_match(
'/\((.*)\)/', $txtva, $reg))
1541 $vat_src_code = $reg[1];
1542 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1545 $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);
1555 $total_ht = $tabprice[0];
1556 $total_tva = $tabprice[1];
1557 $total_ttc = $tabprice[2];
1558 $total_localtax1 = $tabprice[9];
1559 $total_localtax2 = $tabprice[10];
1560 $pu_ht = $tabprice[3];
1563 $multicurrency_total_ht = $tabprice[16];
1564 $multicurrency_total_tva = $tabprice[17];
1565 $multicurrency_total_ttc = $tabprice[18];
1566 $pu_ht_devise = $tabprice[19];
1570 if ($ranktouse == -1)
1572 $rangmax = $this->
line_max($fk_parent_line);
1573 $ranktouse = $rangmax + 1;
1580 if ($remise_percent > 0)
1582 $remise = round(($pu * $remise_percent / 100), 2);
1583 $price = $pu - $remise;
1589 $this->line->context = $this->context;
1591 $this->line->fk_commande = $this->id;
1592 $this->line->label = $label;
1593 $this->line->desc = $desc;
1594 $this->line->qty = $qty;
1595 $this->line->ref_ext = $ref_ext;
1597 $this->line->vat_src_code = $vat_src_code;
1598 $this->line->tva_tx = $txtva;
1599 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1600 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1601 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1602 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1603 $this->line->fk_product = $fk_product;
1604 $this->line->product_type = $product_type;
1605 $this->line->fk_remise_except = $fk_remise_except;
1606 $this->line->remise_percent = $remise_percent;
1607 $this->line->subprice = $pu_ht;
1608 $this->line->rang = $ranktouse;
1609 $this->line->info_bits = $info_bits;
1610 $this->line->total_ht = $total_ht;
1611 $this->line->total_tva = $total_tva;
1612 $this->line->total_localtax1 = $total_localtax1;
1613 $this->line->total_localtax2 = $total_localtax2;
1614 $this->line->total_ttc = $total_ttc;
1615 $this->line->special_code = $special_code;
1616 $this->line->origin = $origin;
1617 $this->line->origin_id = $origin_id;
1618 $this->line->fk_parent_line = $fk_parent_line;
1619 $this->line->fk_unit = $fk_unit;
1621 $this->line->date_start = $date_start;
1622 $this->line->date_end = $date_end;
1624 $this->line->fk_fournprice = $fk_fournprice;
1625 $this->line->pa_ht = $pa_ht;
1628 $this->line->fk_multicurrency = $this->fk_multicurrency;
1629 $this->line->multicurrency_code = $this->multicurrency_code;
1630 $this->line->multicurrency_subprice = $pu_ht_devise;
1631 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1632 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1633 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1636 $this->line->price = $price;
1637 $this->line->remise = $remise;
1639 if (is_array($array_options) && count($array_options) > 0) {
1640 $this->line->array_options = $array_options;
1643 $result = $this->line->insert($user);
1647 if (!empty($fk_parent_line)) $this->
line_order(
true,
'DESC');
1653 $this->
db->commit();
1654 return $this->line->id;
1656 $this->
db->rollback();
1660 $this->error = $this->line->error;
1661 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1662 $this->
db->rollback();
1666 dol_syslog(get_class($this).
"::addline status of order must be Draft to allow use of ->addline()", LOG_ERR);
1686 public function add_product($idproduct, $qty, $remise_percent = 0.0, $date_start =
'', $date_end =
'')
1689 global $conf, $mysoc;
1691 if (!$qty) $qty = 1;
1696 $prod->fetch($idproduct);
1700 if (empty($tva_tx)) $tva_npr = 0;
1703 $localtax1_tx =
get_localtax($tva_tx, 1, $this->thirdparty, $mysoc, $tva_npr);
1704 $localtax2_tx =
get_localtax($tva_tx, 2, $this->thirdparty, $mysoc, $tva_npr);
1707 if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
1708 $price = $prod->multiprices[$this->thirdparty->price_level];
1710 $price = $prod->price;
1715 $line->context = $this->context;
1717 $line->fk_product = $idproduct;
1718 $line->desc = $prod->description;
1720 $line->subprice = $price;
1721 $line->remise_percent = $remise_percent;
1722 $line->vat_src_code = $vat_src_code;
1723 $line->tva_tx = $tva_tx;
1724 $line->localtax1_tx = $localtax1_tx;
1725 $line->localtax2_tx = $localtax2_tx;
1726 $line->ref = $prod->ref;
1727 $line->libelle = $prod->label;
1728 $line->product_desc = $prod->description;
1729 $line->fk_unit = $prod->fk_unit;
1732 if ($date_start) { $line->date_start = $date_start; }
1733 if ($date_end) { $line->date_end = $date_end; }
1735 $this->lines[] = $line;
1768 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
1771 if (empty($id) && empty($ref) && empty($ref_ext))
return -1;
1773 $sql =
'SELECT c.rowid, c.entity, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_user_valid, c.fk_statut';
1774 $sql .=
', c.amount_ht, c.total_ht, c.total_ttc, c.tva as total_tva, c.localtax1 as total_localtax1, c.localtax2 as total_localtax2, c.fk_cond_reglement, c.fk_mode_reglement, c.fk_availability, c.fk_input_reason';
1775 $sql .=
', c.fk_account';
1776 $sql .=
', c.date_commande, c.date_valid, c.tms';
1777 $sql .=
', c.date_livraison as delivery_date';
1778 $sql .=
', c.fk_shipping_method';
1779 $sql .=
', c.fk_warehouse';
1780 $sql .=
', c.fk_projet as fk_project, c.remise_percent, c.remise, c.remise_absolue, c.source, c.facture as billed';
1781 $sql .=
', c.note_private, c.note_public, c.ref_client, c.ref_ext, c.ref_int, c.model_pdf, c.last_main_doc, c.fk_delivery_address, c.extraparams';
1782 $sql .=
', c.fk_incoterms, c.location_incoterms';
1783 $sql .=
", c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc";
1784 $sql .=
", c.module_source, c.pos_source";
1785 $sql .=
", i.libelle as label_incoterms";
1786 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
1787 $sql .=
', cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle, cr.libelle_facture as cond_reglement_libelle_doc';
1788 $sql .=
', ca.code as availability_code, ca.label as availability_label';
1789 $sql .=
', dr.code as demand_reason_code';
1790 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
1791 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON c.fk_cond_reglement = cr.rowid';
1792 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON c.fk_mode_reglement = p.id';
1793 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON c.fk_availability = ca.rowid';
1794 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON c.fk_input_reason = dr.rowid';
1795 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
1797 if ($id) $sql .=
" WHERE c.rowid=".$id;
1798 else $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
1800 if ($ref) $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
1801 if ($ref_ext) $sql .=
" AND c.ref_ext='".$this->db->escape($ref_ext).
"'";
1802 if ($notused) $sql .=
" AND c.ref_int='".$this->db->escape($notused).
"'";
1804 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1805 $result = $this->
db->query($sql);
1808 $obj = $this->
db->fetch_object($result);
1811 $this->
id = $obj->rowid;
1812 $this->entity = $obj->entity;
1814 $this->ref = $obj->ref;
1815 $this->ref_client = $obj->ref_client;
1816 $this->ref_customer = $obj->ref_client;
1817 $this->ref_ext = $obj->ref_ext;
1818 $this->ref_int = $obj->ref_int;
1820 $this->socid = $obj->fk_soc;
1821 $this->thirdparty = null;
1823 $this->fk_project = $obj->fk_project;
1824 $this->project = null;
1826 $this->
statut = $obj->fk_statut;
1827 $this->status = $obj->fk_statut;
1829 $this->user_author_id = $obj->fk_user_author;
1830 $this->user_valid = $obj->fk_user_valid;
1831 $this->total_ht = $obj->total_ht;
1832 $this->total_tva = $obj->total_tva;
1833 $this->total_localtax1 = $obj->total_localtax1;
1834 $this->total_localtax2 = $obj->total_localtax2;
1835 $this->total_ttc = $obj->total_ttc;
1836 $this->date = $this->
db->jdate($obj->date_commande);
1837 $this->date_commande = $this->
db->jdate($obj->date_commande);
1838 $this->date_creation = $this->
db->jdate($obj->date_creation);
1839 $this->date_validation = $this->
db->jdate($obj->date_valid);
1840 $this->date_modification = $this->
db->jdate($obj->tms);
1841 $this->remise = $obj->remise;
1842 $this->remise_percent = $obj->remise_percent;
1843 $this->remise_absolue = $obj->remise_absolue;
1844 $this->source = $obj->source;
1845 $this->billed = $obj->billed;
1846 $this->
note = $obj->note_private;
1847 $this->note_private = $obj->note_private;
1848 $this->note_public = $obj->note_public;
1849 $this->model_pdf = $obj->model_pdf;
1850 $this->modelpdf = $obj->model_pdf;
1851 $this->last_main_doc = $obj->last_main_doc;
1852 $this->mode_reglement_id = $obj->fk_mode_reglement;
1853 $this->mode_reglement_code = $obj->mode_reglement_code;
1854 $this->mode_reglement = $obj->mode_reglement_libelle;
1855 $this->cond_reglement_id = $obj->fk_cond_reglement;
1856 $this->cond_reglement_code = $obj->cond_reglement_code;
1857 $this->cond_reglement = $obj->cond_reglement_libelle;
1858 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1859 $this->fk_account = $obj->fk_account;
1860 $this->availability_id = $obj->fk_availability;
1861 $this->availability_code = $obj->availability_code;
1863 $this->demand_reason_id = $obj->fk_input_reason;
1864 $this->demand_reason_code = $obj->demand_reason_code;
1865 $this->date_livraison = $this->
db->jdate($obj->delivery_date);
1866 $this->delivery_date = $this->
db->jdate($obj->delivery_date);
1867 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method : null;
1868 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse : null;
1869 $this->fk_delivery_address = $obj->fk_delivery_address;
1870 $this->module_source = $obj->module_source;
1871 $this->pos_source = $obj->pos_source;
1874 $this->fk_incoterms = $obj->fk_incoterms;
1875 $this->location_incoterms = $obj->location_incoterms;
1876 $this->label_incoterms = $obj->label_incoterms;
1879 $this->fk_multicurrency = $obj->fk_multicurrency;
1880 $this->multicurrency_code = $obj->multicurrency_code;
1881 $this->multicurrency_tx = $obj->multicurrency_tx;
1882 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1883 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1884 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1886 $this->extraparams = (array) json_decode($obj->extraparams,
true);
1888 $this->lines = array();
1890 if ($this->
statut == self::STATUS_DRAFT) $this->brouillon = 1;
1896 $this->
db->free($result);
1906 $this->error =
'Order with id '.$id.
' not found sql='.$sql;
1910 $this->error = $this->
db->error();
1928 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1929 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1934 $result = $remise->fetch($idremise);
1938 if ($remise->fk_facture)
1940 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
1941 $this->
db->rollback();
1947 $line->fk_commande = $this->id;
1948 $line->fk_remise_except = $remise->id;
1949 $line->desc = $remise->description;
1950 $line->vat_src_code = $remise->vat_src_code;
1951 $line->tva_tx = $remise->tva_tx;
1952 $line->subprice = -$remise->amount_ht;
1953 $line->price = -$remise->amount_ht;
1954 $line->fk_product = 0;
1957 $line->remise_percent = 0;
1959 $line->info_bits = 2;
1961 $line->total_ht = -$remise->amount_ht;
1962 $line->total_tva = -$remise->amount_tva;
1963 $line->total_ttc = -$remise->amount_ttc;
1965 $result = $line->insert();
1971 $this->
db->commit();
1974 $this->
db->rollback();
1978 $this->error = $line->error;
1979 $this->errors = $line->errors;
1980 $this->
db->rollback();
1984 $this->
db->rollback();
1998 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2000 global $langs, $conf;
2002 $this->lines = array();
2004 $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,';
2005 $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,';
2006 $sql .=
' l.total_ht, l.total_ttc, l.total_tva, l.total_localtax1, l.total_localtax2, l.date_start, l.date_end,';
2007 $sql .=
' l.fk_unit,';
2008 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2009 $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,';
2010 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units';
2011 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as l';
2012 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (p.rowid = l.fk_product)';
2013 $sql .=
' WHERE l.fk_commande = '.$this->id;
2014 if ($only_product) $sql .=
' AND p.fk_product_type = 0';
2015 $sql .=
' ORDER BY l.rang, l.rowid';
2017 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
2018 $result = $this->
db->query($sql);
2021 $num = $this->
db->num_rows($result);
2026 $objp = $this->
db->fetch_object($result);
2030 $line->rowid = $objp->rowid;
2031 $line->id = $objp->rowid;
2032 $line->fk_commande = $objp->fk_commande;
2033 $line->commande_id = $objp->fk_commande;
2034 $line->label = $objp->custom_label;
2035 $line->desc = $objp->description;
2036 $line->description = $objp->description;
2037 $line->product_type = $objp->product_type;
2038 $line->qty = $objp->qty;
2039 $line->ref_ext = $objp->ref_ext;
2041 $line->vat_src_code = $objp->vat_src_code;
2042 $line->tva_tx = $objp->tva_tx;
2043 $line->localtax1_tx = $objp->localtax1_tx;
2044 $line->localtax2_tx = $objp->localtax2_tx;
2045 $line->localtax1_type = $objp->localtax1_type;
2046 $line->localtax2_type = $objp->localtax2_type;
2047 $line->total_ht = $objp->total_ht;
2048 $line->total_ttc = $objp->total_ttc;
2049 $line->total_tva = $objp->total_tva;
2050 $line->total_localtax1 = $objp->total_localtax1;
2051 $line->total_localtax2 = $objp->total_localtax2;
2052 $line->subprice = $objp->subprice;
2053 $line->fk_remise_except = $objp->fk_remise_except;
2054 $line->remise_percent = $objp->remise_percent;
2055 $line->price = $objp->price;
2056 $line->fk_product = $objp->fk_product;
2057 $line->fk_fournprice = $objp->fk_fournprice;
2058 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2059 $line->pa_ht = $marginInfos[0];
2060 $line->marge_tx = $marginInfos[1];
2061 $line->marque_tx = $marginInfos[2];
2062 $line->rang = $objp->rang;
2063 $line->info_bits = $objp->info_bits;
2064 $line->special_code = $objp->special_code;
2065 $line->fk_parent_line = $objp->fk_parent_line;
2067 $line->ref = $objp->product_ref;
2068 $line->libelle = $objp->product_label;
2070 $line->product_ref = $objp->product_ref;
2071 $line->product_label = $objp->product_label;
2072 $line->product_desc = $objp->product_desc;
2073 $line->product_tosell = $objp->product_tosell;
2074 $line->product_tobuy = $objp->product_tobuy;
2075 $line->product_tobatch = $objp->product_tobatch;
2076 $line->product_barcode = $objp->product_barcode;
2078 $line->fk_product_type = $objp->fk_product_type;
2079 $line->fk_unit = $objp->fk_unit;
2081 $line->weight = $objp->weight;
2082 $line->weight_units = $objp->weight_units;
2083 $line->volume = $objp->volume;
2084 $line->volume_units = $objp->volume_units;
2086 $line->date_start = $this->
db->jdate($objp->date_start);
2087 $line->date_end = $this->
db->jdate($objp->date_end);
2090 $line->fk_multicurrency = $objp->fk_multicurrency;
2091 $line->multicurrency_code = $objp->multicurrency_code;
2092 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2093 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2094 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2095 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2097 $line->fetch_optionals();
2100 if (!empty($conf->global->MAIN_MULTILANGS) && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2102 $line->fetch($objp->fk_product);
2103 $line->getMultiLangs();
2106 $this->lines[$i] = $line;
2111 $this->
db->free($result);
2115 $this->error = $this->
db->error();
2129 foreach ($this->lines as $line)
2131 if ($line->product_type == 0) $nb++;
2144 foreach ($this->lines as $line)
2146 if ($line->product_type == 1) $nb++;
2160 $sql =
'SELECT COUNT(DISTINCT ed.fk_expedition) as nb';
2161 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2162 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2164 $sql .=
' ed.fk_origin_line = cd.rowid';
2165 $sql .=
' AND cd.fk_commande ='.$this->id;
2168 dol_syslog(get_class($this).
"::getNbOfShipments", LOG_DEBUG);
2172 $obj = $this->
db->fetch_object(
$resql);
2173 if ($obj) $nb = $obj->nb;
2178 $this->error = $this->
db->lasterror();
2192 $this->expeditions = array();
2194 $sql =
'SELECT cd.rowid, cd.fk_product,';
2195 $sql .=
' sum(ed.qty) as qty';
2196 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2197 if ($filtre_statut >= 0) $sql .=
' '.MAIN_DB_PREFIX.
'expedition as e,';
2198 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2200 if ($filtre_statut >= 0) $sql .=
' ed.fk_expedition = e.rowid AND';
2201 $sql .=
' ed.fk_origin_line = cd.rowid';
2202 $sql .=
' AND cd.fk_commande ='.$this->id;
2203 if ($this->fk_product > 0) $sql .=
' AND cd.fk_product = '.$this->fk_product;
2204 if ($filtre_statut >= 0) $sql .=
' AND e.fk_statut >= '.$filtre_statut;
2205 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
2208 dol_syslog(get_class($this).
"::loadExpeditions", LOG_DEBUG);
2212 $num = $this->
db->num_rows(
$resql);
2216 $obj = $this->
db->fetch_object(
$resql);
2217 $this->expeditions[$obj->rowid] = $obj->qty;
2223 $this->error = $this->
db->lasterror();
2239 $sql =
'SELECT count(*)';
2240 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expedition as e';
2241 $sql .=
', '.MAIN_DB_PREFIX.
'element_element as el';
2242 $sql .=
' WHERE el.fk_source = '.$this->id;
2243 $sql .=
" AND el.fk_target = e.rowid";
2244 $sql .=
" AND el.targettype = 'shipping'";
2249 $row = $this->
db->fetch_row(
$resql);
2266 $this->stocks = array();
2269 $array_of_product = array();
2273 if (count($array_of_product))
2275 $sql =
"SELECT fk_product, sum(ps.reel) as total";
2276 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_stock as ps";
2277 $sql .=
" WHERE ps.fk_product IN (".join(
',', $array_of_product).
")";
2278 $sql .=
' GROUP BY fk_product ';
2282 $num = $this->
db->num_rows(
$resql);
2286 $obj = $this->
db->fetch_object(
$resql);
2287 $this->stocks[$obj->fk_product] = $obj->total;
2305 if ($this->
statut == self::STATUS_DRAFT)
2309 $sql =
"SELECT fk_product, qty";
2310 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet";
2311 $sql .=
" WHERE rowid = ".$lineid;
2313 $result = $this->
db->query($sql);
2316 $obj = $this->
db->fetch_object($result);
2321 $product->id = $obj->fk_product;
2327 $line->fetch($lineid);
2329 if ($line->delete($user) > 0)
2335 $this->
db->commit();
2338 $this->
db->rollback();
2339 $this->error = $this->
db->lasterror();
2343 $this->
db->rollback();
2344 $this->error = $line->error;
2348 $this->
db->rollback();
2352 $this->
db->rollback();
2353 $this->error = $this->
db->lasterror();
2357 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
2374 $remise = trim($remise) ?trim($remise) : 0;
2376 if ($user->rights->commande->creer)
2384 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2385 $sql .=
' SET remise_percent = '.$remise;
2386 $sql .=
' WHERE rowid = '.$this->id.
' AND fk_statut = '.self::STATUS_DRAFT.
' ;';
2392 $this->errors[] = $this->
db->error();
2398 $this->oldcopy = clone $this;
2399 $this->remise_percent = $remise;
2403 if (!$notrigger && empty($error))
2407 if ($result < 0) $error++;
2413 $this->
db->commit();
2416 foreach ($this->errors as $errmsg)
2418 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2419 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2421 $this->
db->rollback();
2440 $remise = trim($remise) ?trim($remise) : 0;
2442 if ($user->rights->commande->creer)
2450 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2451 $sql .=
' SET remise_absolue = '.$remise;
2452 $sql .=
' WHERE rowid = '.$this->id.
' AND fk_statut = '.self::STATUS_DRAFT.
' ;';
2458 $this->errors[] = $this->
db->error();
2464 $this->oldcopy = clone $this;
2465 $this->remise_absolue = $remise;
2469 if (!$notrigger && empty($error))
2473 if ($result < 0) $error++;
2479 $this->
db->commit();
2482 foreach ($this->errors as $errmsg)
2484 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2485 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2487 $this->
db->rollback();
2506 if ($user->rights->commande->creer)
2512 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2513 $sql .=
" SET date_commande = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
2514 $sql .=
" WHERE rowid = ".$this->id.
" AND fk_statut = ".self::STATUS_DRAFT;
2520 $this->errors[] = $this->
db->error();
2526 $this->oldcopy = clone $this;
2527 $this->date = $date;
2530 if (!$notrigger && empty($error))
2534 if ($result < 0) $error++;
2540 $this->
db->commit();
2543 foreach ($this->errors as $errmsg)
2545 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2546 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2548 $this->
db->rollback();
2582 if ($user->rights->commande->creer)
2588 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2589 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2590 $sql .=
" WHERE rowid = ".$this->id;
2596 $this->errors[] = $this->
db->error();
2602 $this->oldcopy = clone $this;
2603 $this->date_livraison = $delivery_date;
2604 $this->delivery_date = $delivery_date;
2607 if (!$notrigger && empty($error))
2611 if ($result < 0) $error++;
2617 $this->
db->commit();
2620 foreach ($this->errors as $errmsg)
2622 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2623 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2625 $this->
db->rollback();
2647 public function liste_array($shortlist = 0, $draft = 0, $excluser =
'', $socid = 0, $limit = 0, $offset = 0, $sortfield =
'c.date_commande', $sortorder =
'DESC')
2654 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2655 $sql .=
" c.rowid as cid, c.ref";
2656 if (!$user->rights->societe->client->voir && !$socid) $sql .=
", sc.fk_soc, sc.fk_user";
2657 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"commande as c";
2658 if (!$user->rights->societe->client->voir && !$socid) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2659 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2660 $sql .=
" AND c.fk_soc = s.rowid";
2661 if (!$user->rights->societe->client->voir && !$socid)
2663 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
2665 if ($socid) $sql .=
" AND s.rowid = ".$socid;
2666 if ($draft) $sql .=
" AND c.fk_statut = ".self::STATUS_DRAFT;
2667 if (is_object($excluser)) $sql .=
" AND c.fk_user_author <> ".$excluser->id;
2668 $sql .= $this->
db->order($sortfield, $sortorder);
2669 $sql .= $this->
db->plimit($limit, $offset);
2671 $result = $this->
db->query($sql);
2674 $numc = $this->
db->num_rows($result);
2680 $obj = $this->
db->fetch_object($result);
2682 if ($shortlist == 1)
2684 $ga[$obj->cid] = $obj->ref;
2685 } elseif ($shortlist == 2)
2687 $ga[$obj->cid] = $obj->ref.
' ('.$obj->name.
')';
2689 $ga[$i][
'id'] = $obj->cid;
2690 $ga[$i][
'ref'] = $obj->ref;
2691 $ga[$i][
'name'] = $obj->name;
2714 dol_syslog(
'Commande::availability('.$availability_id.
')');
2715 if ($this->
statut >= self::STATUS_DRAFT)
2721 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2722 $sql .=
' SET fk_availability = '.$availability_id;
2723 $sql .=
' WHERE rowid='.$this->id;
2729 $this->errors[] = $this->
db->error();
2735 $this->oldcopy = clone $this;
2736 $this->availability_id = $availability_id;
2739 if (!$notrigger && empty($error))
2743 if ($result < 0) $error++;
2749 $this->
db->commit();
2752 foreach ($this->errors as $errmsg)
2754 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2755 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2757 $this->
db->rollback();
2761 $error_str =
'Command status do not meet requirement '.$this->statut;
2763 $this->error = $error_str;
2764 $this->errors[] = $this->error;
2782 dol_syslog(
'Commande::demand_reason('.$demand_reason_id.
')');
2783 if ($this->
statut >= self::STATUS_DRAFT)
2789 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2790 $sql .=
' SET fk_input_reason = '.$demand_reason_id;
2791 $sql .=
' WHERE rowid='.$this->id;
2797 $this->errors[] = $this->
db->error();
2803 $this->oldcopy = clone $this;
2804 $this->demand_reason_id = $demand_reason_id;
2807 if (!$notrigger && empty($error))
2811 if ($result < 0) $error++;
2817 $this->
db->commit();
2820 foreach ($this->errors as $errmsg)
2822 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2823 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2825 $this->
db->rollback();
2829 $error_str =
'order status do not meet requirement '.$this->statut;
2831 $this->error = $error_str;
2832 $this->errors[] = $this->error;
2849 if ($user->rights->commande->creer)
2855 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET';
2856 $sql .=
' ref_client = '.(empty($ref_client) ?
'NULL' :
'\''.$this->db->escape($ref_client).
'\'');
2857 $sql .=
' WHERE rowid = '.$this->id;
2859 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2863 $this->errors[] = $this->
db->error();
2869 $this->oldcopy = clone $this;
2870 $this->ref_client = $ref_client;
2873 if (!$notrigger && empty($error))
2877 if ($result < 0) $error++;
2882 $this->
db->commit();
2885 foreach ($this->errors as $errmsg)
2887 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2888 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2890 $this->
db->rollback();
2916 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 1';
2917 $sql .=
' WHERE rowid = '.$this->id.
' AND fk_statut > '.self::STATUS_DRAFT;
2919 dol_syslog(get_class($this).
"::classifyBilled", LOG_DEBUG);
2920 if ($this->
db->query($sql))
2924 $this->oldcopy = clone $this;
2928 if (!$notrigger && empty($error))
2931 $result = $this->
call_trigger(
'ORDER_CLASSIFY_BILLED', $user);
2932 if ($result < 0) $error++;
2938 $this->
db->commit();
2941 foreach ($this->errors as $errmsg)
2943 dol_syslog(get_class($this).
"::classifyBilled ".$errmsg, LOG_ERR);
2944 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2946 $this->
db->rollback();
2950 $this->error = $this->
db->error();
2951 $this->
db->rollback();
2963 global $conf, $user, $langs;
2968 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 0';
2969 $sql .=
' WHERE rowid = '.$this->id.
' AND fk_statut > '.self::STATUS_DRAFT;
2971 dol_syslog(get_class($this).
"::classifyUnBilled", LOG_DEBUG);
2972 if ($this->
db->query($sql))
2976 $this->oldcopy = clone $this;
2981 $result = $this->
call_trigger(
'ORDER_CLASSIFY_UNBILLED', $user);
2982 if ($result < 0) $error++;
2989 $this->
db->commit();
2992 foreach ($this->errors as $errmsg)
2994 dol_syslog(get_class($this).
"::classifyUnBilled ".$errmsg, LOG_ERR);
2995 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2997 $this->
db->rollback();
3001 $this->error = $this->
db->error();
3002 $this->
db->rollback();
3037 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 =
'')
3039 global $conf, $mysoc, $langs, $user;
3041 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");
3042 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3047 if (empty($qty)) $qty = 0;
3048 if (empty($info_bits)) $info_bits = 0;
3049 if (empty($txtva)) $txtva = 0;
3050 if (empty($txlocaltax1)) $txlocaltax1 = 0;
3051 if (empty($txlocaltax2)) $txlocaltax2 = 0;
3052 if (empty($remise_percent)) $remise_percent = 0;
3053 if (empty($special_code) || $special_code == 3) $special_code = 0;
3054 if (empty($ref_ext)) $ref_ext =
'';
3056 if ($date_start && $date_end && $date_start > $date_end) {
3057 $langs->load(
"errors");
3058 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3062 $remise_percent =
price2num($remise_percent);
3066 $pu_ht_devise =
price2num($pu_ht_devise);
3067 if (!preg_match(
'/\((.*)\)/', $txtva)) {
3085 if (preg_match(
'/\((.*)\)/', $txtva, $reg))
3087 $vat_src_code = $reg[1];
3088 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3091 $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);
3093 $total_ht = $tabprice[0];
3094 $total_tva = $tabprice[1];
3095 $total_ttc = $tabprice[2];
3096 $total_localtax1 = $tabprice[9];
3097 $total_localtax2 = $tabprice[10];
3098 $pu_ht = $tabprice[3];
3099 $pu_tva = $tabprice[4];
3100 $pu_ttc = $tabprice[5];
3103 $multicurrency_total_ht = $tabprice[16];
3104 $multicurrency_total_tva = $tabprice[17];
3105 $multicurrency_total_ttc = $tabprice[18];
3106 $pu_ht_devise = $tabprice[19];
3110 if ($price_base_type ==
'TTC')
3112 $subprice = $pu_ttc;
3117 if ($remise_percent > 0)
3119 $remise = round(($pu * $remise_percent / 100), 2);
3120 $price = ($pu - $remise);
3125 $line->fetch($rowid);
3126 $line->fetch_optionals();
3128 if (!empty($line->fk_product))
3131 $result = $product->fetch($line->fk_product);
3132 $product_type = $product->type;
3134 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty)
3136 $langs->load(
"errors");
3137 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
3138 $this->errors[] = $this->error;
3139 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
3140 $this->
db->rollback();
3141 return self::STOCK_NOT_ENOUGH_FOR_ORDER;
3145 $staticline = clone $line;
3147 $line->oldline = $staticline;
3148 $this->line = $line;
3149 $this->line->context = $this->context;
3152 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line)
3154 $rangmax = $this->
line_max($fk_parent_line);
3155 $this->line->rang = $rangmax + 1;
3158 $this->line->id = $rowid;
3159 $this->line->label = $label;
3160 $this->line->desc = $desc;
3161 $this->line->qty = $qty;
3162 $this->line->ref_ext = $ref_ext;
3164 $this->line->vat_src_code = $vat_src_code;
3165 $this->line->tva_tx = $txtva;
3166 $this->line->localtax1_tx = $txlocaltax1;
3167 $this->line->localtax2_tx = $txlocaltax2;
3168 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3169 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3170 $this->line->remise_percent = $remise_percent;
3171 $this->line->subprice = $subprice;
3172 $this->line->info_bits = $info_bits;
3173 $this->line->special_code = $special_code;
3174 $this->line->total_ht = $total_ht;
3175 $this->line->total_tva = $total_tva;
3176 $this->line->total_localtax1 = $total_localtax1;
3177 $this->line->total_localtax2 = $total_localtax2;
3178 $this->line->total_ttc = $total_ttc;
3179 $this->line->date_start = $date_start;
3180 $this->line->date_end = $date_end;
3181 $this->line->product_type = $type;
3182 $this->line->fk_parent_line = $fk_parent_line;
3183 $this->line->skip_update_total = $skip_update_total;
3184 $this->line->fk_unit = $fk_unit;
3186 $this->line->fk_fournprice = $fk_fournprice;
3187 $this->line->pa_ht = $pa_ht;
3190 $this->line->multicurrency_subprice = $pu_ht_devise;
3191 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
3192 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
3193 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
3196 $this->line->price = $price;
3197 $this->line->remise = $remise;
3199 if (is_array($array_options) && count($array_options) > 0) {
3201 foreach ($array_options as $key => $value) {
3202 $this->line->array_options[$key] = $array_options[$key];
3206 $result = $this->line->update($user, $notrigger);
3210 if (!empty($fk_parent_line)) $this->
line_order(
true,
'DESC');
3215 $this->
db->commit();
3218 $this->error = $this->line->error;
3220 $this->
db->rollback();
3224 $this->error = get_class($this).
"::updateline Order status makes operation forbidden";
3225 $this->errors = array(
'OrderStatusMakeOperationForbidden');
3244 if (isset($this->ref)) $this->ref = trim($this->ref);
3245 if (isset($this->ref_client)) $this->ref_client = trim($this->ref_client);
3246 if (isset($this->
note) || isset($this->note_private)) $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->
note));
3247 if (isset($this->note_public)) $this->note_public = trim($this->note_public);
3248 if (isset($this->model_pdf)) $this->model_pdf = trim($this->model_pdf);
3249 if (isset($this->import_key)) $this->import_key = trim($this->import_key);
3250 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
3256 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande SET";
3258 $sql .=
" ref=".(isset($this->ref) ?
"'".$this->db->escape($this->ref).
"'" :
"null").
",";
3259 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
3260 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
3261 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
3262 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
3263 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
3264 $sql .=
" tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
3265 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
3266 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
3267 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
3268 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
3269 $sql .=
" fk_statut=".(isset($this->
statut) ? $this->
statut :
"null").
",";
3270 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id :
"null").
",";
3271 $sql .=
" fk_user_valid=".(isset($this->user_valid) ? $this->user_valid :
"null").
",";
3272 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
3273 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
3274 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
3275 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
3276 $sql .=
" fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id :
"null").
",";
3277 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
3278 $sql .=
" fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id :
"null").
",";
3279 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
3280 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
3281 $sql .=
" model_pdf=".(isset($this->modelpdf) ?
"'".$this->db->escape($this->modelpdf).
"'" :
"null").
",";
3282 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
"";
3284 $sql .=
" WHERE rowid=".$this->id;
3288 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3291 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
3303 if (!$error && !$notrigger)
3307 if ($result < 0) $error++;
3314 foreach ($this->errors as $errmsg)
3316 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3317 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3319 $this->
db->rollback();
3322 $this->
db->commit();
3334 public function delete($user, $notrigger = 0)
3336 global $conf, $langs;
3337 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3341 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
3348 if ($result < 0) { $error++; }
3355 $this->errors[] = $langs->trans(
'SomeShipmentExists');
3360 if (!$error && !empty($this->table_element_line)) {
3361 $tabletodelete = $this->table_element_line;
3362 $sqlef =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
"_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".$this->
id.
")";
3363 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".$this->id;
3364 if (!$this->
db->query($sqlef) || !$this->
db->query($sql)) {
3366 $this->error = $this->
db->lasterror();
3367 $this->errors[] = $this->error;
3368 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3375 if ($res < 0) $error++;
3381 if ($res < 0) $error++;
3389 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3395 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".$this->id;
3396 $res = $this->
db->query($sql);
3399 $this->error = $this->
db->lasterror();
3400 $this->errors[] = $this->error;
3401 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3416 if ($conf->commande->multidir_output[$this->entity] && !empty($this->ref)) {
3417 $dir = $conf->commande->multidir_output[$this->entity].
"/".$ref;
3418 $file = $dir.
"/".$ref.
".pdf";
3419 if (file_exists($file)) {
3423 $this->error =
'ErrorFailToDeleteFile';
3424 $this->errors[] = $this->error;
3425 $this->
db->rollback();
3429 if (file_exists($dir)) {
3432 $this->error =
'ErrorFailToDeleteDir';
3433 $this->errors[] = $this->error;
3434 $this->
db->rollback();
3442 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3443 $this->
db->commit();
3446 $this->
db->rollback();
3462 global $conf, $langs;
3466 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.date_livraison as delivery_date, c.fk_statut, c.total_ht";
3467 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as c";
3468 if (!$user->rights->societe->client->voir && !$user->socid)
3470 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
3471 $sql .=
" WHERE sc.fk_user = ".$user->id;
3474 $sql .= $clause.
" c.entity IN (".
getEntity(
'commande').
")";
3476 $sql .=
" AND ((c.fk_statut IN (".self::STATUS_VALIDATED.
",".self::STATUS_SHIPMENTONPROCESS.
")) OR (c.fk_statut = ".self::STATUS_CLOSED.
" AND c.facture = 0))";
3477 if ($user->socid) $sql .=
" AND c.fk_soc = ".$user->socid;
3483 $response->warning_delay = $conf->commande->client->warning_delay / 60 / 60 / 24;
3484 $response->label = $langs->trans(
"OrdersToProcess");
3485 $response->labelShort = $langs->trans(
"Opened");
3486 $response->url = DOL_URL_ROOT.
'/commande/list.php?search_status=-3&mainmenu=commercial&leftmenu=orders';
3491 while ($obj = $this->
db->fetch_object(
$resql))
3493 $response->nbtodo++;
3494 $response->total += $obj->total_ht;
3496 $generic_commande->statut = $obj->fk_statut;
3497 $generic_commande->date_commande = $this->
db->jdate($obj->date_commande);
3498 $generic_commande->date = $this->
db->jdate($obj->date_commande);
3499 $generic_commande->date_livraison = $this->
db->jdate($obj->delivery_date);
3500 $generic_commande->delivery_date = $this->
db->jdate($obj->delivery_date);
3502 if ($generic_commande->hasDelay()) {
3503 $response->nbtodolate++;
3509 $this->error = $this->
db->error();
3523 $label = $langs->trans(
'OrderSource'.$this->source);
3525 if ($label ==
'OrderSource')
return '';
3550 public function LibStatut($status, $billed, $mode, $donotshowbilled = 0)
3553 global $langs, $conf;
3556 if (empty($donotshowbilled)) $billedtext .= ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3560 if ($status == self::STATUS_CANCELED) {
3561 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderCanceled');
3562 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderCanceledShort');
3563 $statusType =
'status9';
3564 } elseif ($status == self::STATUS_DRAFT) {
3565 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDraft');
3566 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDraftShort');
3567 $statusType =
'status0';
3568 } elseif ($status == self::STATUS_VALIDATED) {
3569 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderValidated').$billedtext;
3570 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderValidatedShort').$billedtext;
3571 $statusType =
'status1';
3572 } elseif ($status == self::STATUS_SHIPMENTONPROCESS) {
3573 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderSent').$billedtext;
3574 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderSentShort').$billedtext;
3575 $labelTooltip = $langs->transnoentitiesnoconv(
"StatusOrderSent").
' - '.$langs->transnoentitiesnoconv(
"DateDeliveryPlanned").dol_print_date($this->date_livraison).$billedtext;
3576 $statusType =
'status4';
3577 } elseif ($status == self::STATUS_CLOSED && (!$billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3578 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderToBill');
3579 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderToBillShort');
3580 $statusType =
'status4';
3581 } elseif ($status == self::STATUS_CLOSED && ($billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3582 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderProcessed').$billedtext;
3583 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderProcessedShort').$billedtext;
3584 $statusType =
'status6';
3585 } elseif ($status == self::STATUS_CLOSED && (!empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3586 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDelivered');
3587 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDeliveredShort');
3588 $statusType =
'status6';
3590 $labelStatus = $langs->transnoentitiesnoconv(
'Unknown');
3591 $labelStatusShort =
'';
3596 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', array(
'tooltip' => $labelTooltip));
3612 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
3614 global $conf, $langs, $user;
3616 if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1;
3620 if (!empty($conf->expedition->enabled) && ($option ==
'1' || $option ==
'2')) $url = DOL_URL_ROOT.
'/expedition/shipment.php?id='.$this->id;
3621 else $url = DOL_URL_ROOT.
'/commande/card.php?id='.$this->id;
3623 if (!$user->rights->commande->lire)
3626 if ($option !==
'nolink')
3629 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3630 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/',
$_SERVER[
"PHP_SELF"])) $add_save_lastsearch_values = 1;
3631 if ($add_save_lastsearch_values) $url .=
'&save_lastsearch_values=1';
3634 if ($short)
return $url;
3638 if ($user->rights->commande->lire) {
3639 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Order").
'</u>';
3640 if (isset($this->
statut)) {
3641 $label .=
' '.$this->getLibStatut(5);
3643 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
3644 $label .=
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.($this->ref_customer ? $this->ref_customer : $this->ref_client);
3645 if (!empty($this->total_ht)) {
3646 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
3648 if (!empty($this->total_tva)) {
3649 $label .=
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
3651 if (!empty($this->total_ttc)) {
3652 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
3654 if (!empty($this->date)) {
3655 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'dayhour');
3657 if (!empty($this->delivery_date)) {
3658 $label .=
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
3663 if (empty($notooltip) && $user->rights->commande->lire)
3665 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
3667 $label = $langs->trans(
"Order");
3668 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
3670 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
3671 $linkclose .=
' class="classfortooltip"';
3674 $linkstart =
'<a href="'.$url.
'"';
3675 $linkstart .= $linkclose.
'>';
3678 if ($option ===
'nolink') {
3683 $result .= $linkstart;
3684 if ($withpicto) $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
3685 if ($withpicto != 2) $result .= $this->ref;
3686 $result .= $linkend;
3688 if ($addlinktonotes)
3690 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3694 $result .=
' <span class="note inline-block">';
3695 $result .=
'<a href="'.DOL_URL_ROOT.
'/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3700 $result .=
'</span>';
3716 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem,';
3717 $sql .=
' date_valid as datev,';
3718 $sql .=
' date_cloture as datecloture,';
3719 $sql .=
' fk_user_author, fk_user_valid, fk_user_cloture';
3720 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
3721 $sql .=
' WHERE c.rowid = '.$id;
3722 $result = $this->
db->query($sql);
3725 if ($this->
db->num_rows($result))
3727 $obj = $this->
db->fetch_object($result);
3728 $this->
id = $obj->rowid;
3729 if ($obj->fk_user_author)
3731 $cuser =
new User($this->
db);
3732 $cuser->fetch($obj->fk_user_author);
3733 $this->user_creation = $cuser;
3736 if ($obj->fk_user_valid)
3738 $vuser =
new User($this->
db);
3739 $vuser->fetch($obj->fk_user_valid);
3740 $this->user_validation = $vuser;
3743 if ($obj->fk_user_cloture)
3745 $cluser =
new User($this->
db);
3746 $cluser->fetch($obj->fk_user_cloture);
3747 $this->user_cloture = $cluser;
3750 $this->date_creation = $this->
db->jdate($obj->datec);
3751 $this->date_modification = $this->
db->jdate($obj->datem);
3752 $this->date_validation = $this->
db->jdate($obj->datev);
3753 $this->date_cloture = $this->
db->jdate($obj->datecloture);
3756 $this->
db->free($result);
3772 global $conf, $langs;
3774 dol_syslog(get_class($this).
"::initAsSpecimen");
3779 $sql =
"SELECT rowid";
3780 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
3781 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3782 $sql .= $this->
db->plimit(100);
3787 $num_prods = $this->
db->num_rows(
$resql);
3789 while ($i < $num_prods)
3792 $row = $this->
db->fetch_row(
$resql);
3793 $prodids[$i] = $row[0];
3799 $this->ref =
'SPECIMEN';
3800 $this->specimen = 1;
3802 $this->date = time();
3803 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
3804 $this->cond_reglement_code =
'RECEP';
3805 $this->mode_reglement_code =
'CHQ';
3806 $this->availability_code =
'DSP';
3807 $this->demand_reason_code =
'SRC_00';
3809 $this->note_public =
'This is a comment (public)';
3810 $this->note_private =
'This is a comment (private)';
3812 $this->multicurrency_tx = 1;
3813 $this->multicurrency_code = $conf->currency;
3818 while ($xnbp < $nbp)
3822 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
3824 $line->subprice = 100;
3829 $line->total_ht = 50;
3830 $line->total_ttc = 60;
3831 $line->total_tva = 10;
3832 $line->remise_percent = 50;
3834 $line->total_ht = 100;
3835 $line->total_ttc = 120;
3836 $line->total_tva = 20;
3837 $line->remise_percent = 0;
3841 $prodid = mt_rand(1, $num_prods);
3842 $line->fk_product = $prodids[$prodid];
3843 $line->product_ref =
'SPECIMEN';
3846 $this->lines[$xnbp] = $line;
3848 $this->total_ht += $line->total_ht;
3849 $this->total_tva += $line->total_tva;
3850 $this->total_ttc += $line->total_ttc;
3868 $this->nb = array();
3871 $sql =
"SELECT count(co.rowid) as nb";
3872 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as co";
3873 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
3874 if (!$user->rights->societe->client->voir && !$user->socid)
3876 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3877 $sql .=
" WHERE sc.fk_user = ".$user->id;
3880 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'commande').
")";
3885 while ($obj = $this->
db->fetch_object(
$resql))
3887 $this->nb[
"orders"] = $obj->nb;
3893 $this->error = $this->
db->error();
3919 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
3921 global $conf, $langs;
3923 $langs->load(
"orders");
3924 $outputlangs->load(
"products");
3927 $modele =
'einstein';
3929 if (!empty($this->model_pdf)) {
3930 $modele = $this->model_pdf;
3931 } elseif (!empty($this->modelpdf)) {
3932 $modele = $this->modelpdf;
3933 } elseif (!empty($conf->global->COMMANDE_ADDON_PDF)) {
3934 $modele = $conf->global->COMMANDE_ADDON_PDF;
3938 $modelpath =
"core/modules/commande/doc/";
3940 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3976 return max($this->date_commande, $this->date_livraison) < ($now - $conf->commande->client->warning_delay);
3986 global $conf, $langs;
3988 if (empty($this->date_livraison)) $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
3989 else $text = $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->date_livraison,
'day');
3990 $text .=
' '.($conf->commande->client->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->client->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
4005 public $element =
'commandedet';
4007 public $table_element =
'commandedet';
4015 public $fk_commande;
4023 public $commande_id;
4025 public $fk_parent_line;
4037 public $fk_remise_except;
4043 public $fk_fournprice;
4063 public $skip_update_total;
4084 $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,';
4085 $sql .=
' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
4086 $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,';
4087 $sql .=
' cd.fk_unit,';
4088 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
4089 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
4090 $sql .=
' cd.date_start, cd.date_end';
4091 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as cd';
4092 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
4093 $sql .=
' WHERE cd.rowid = '.$rowid;
4094 $result = $this->
db->query($sql);
4097 $objp = $this->
db->fetch_object($result);
4098 $this->
rowid = $objp->rowid;
4099 $this->
id = $objp->rowid;
4100 $this->fk_commande = $objp->fk_commande;
4101 $this->fk_parent_line = $objp->fk_parent_line;
4102 $this->label = $objp->custom_label;
4103 $this->desc = $objp->description;
4104 $this->qty = $objp->qty;
4105 $this->
price = $objp->price;
4106 $this->subprice = $objp->subprice;
4107 $this->ref_ext = $objp->ref_ext;
4108 $this->vat_src_code = $objp->vat_src_code;
4109 $this->tva_tx = $objp->tva_tx;
4110 $this->localtax1_tx = $objp->localtax1_tx;
4111 $this->localtax2_tx = $objp->localtax2_tx;
4112 $this->remise = $objp->remise;
4113 $this->remise_percent = $objp->remise_percent;
4114 $this->fk_remise_except = $objp->fk_remise_except;
4115 $this->fk_product = $objp->fk_product;
4116 $this->product_type = $objp->product_type;
4117 $this->info_bits = $objp->info_bits;
4118 $this->special_code = $objp->special_code;
4119 $this->total_ht = $objp->total_ht;
4120 $this->total_tva = $objp->total_tva;
4121 $this->total_localtax1 = $objp->total_localtax1;
4122 $this->total_localtax2 = $objp->total_localtax2;
4123 $this->total_ttc = $objp->total_ttc;
4124 $this->fk_fournprice = $objp->fk_fournprice;
4125 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
4126 $this->pa_ht = $marginInfos[0];
4127 $this->marge_tx = $marginInfos[1];
4128 $this->marque_tx = $marginInfos[2];
4129 $this->special_code = $objp->special_code;
4130 $this->rang = $objp->rang;
4132 $this->ref = $objp->product_ref;
4134 $this->product_ref = $objp->product_ref;
4135 $this->product_label = $objp->product_label;
4136 $this->product_desc = $objp->product_desc;
4137 $this->product_tobatch = $objp->product_tobatch;
4138 $this->fk_unit = $objp->fk_unit;
4140 $this->date_start = $this->
db->jdate($objp->date_start);
4141 $this->date_end = $this->
db->jdate($objp->date_end);
4143 $this->fk_multicurrency = $objp->fk_multicurrency;
4144 $this->multicurrency_code = $objp->multicurrency_code;
4145 $this->multicurrency_subprice = $objp->multicurrency_subprice;
4146 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
4147 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
4148 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
4150 $this->
db->free($result);
4154 $this->error = $this->
db->lasterror();
4166 public function delete(
User $user, $notrigger = 0)
4168 global $conf, $langs;
4173 $sqlCheckShipmentLine =
"SELECT";
4174 $sqlCheckShipmentLine .=
" ed.rowid";
4175 $sqlCheckShipmentLine .=
" FROM ".MAIN_DB_PREFIX.
"expeditiondet ed";
4176 $sqlCheckShipmentLine .=
" WHERE ed.fk_origin_line = ".$this->rowid;
4178 $resqlCheckShipmentLine = $this->
db->query($sqlCheckShipmentLine);
4179 if (!$resqlCheckShipmentLine) {
4181 $this->error = $this->
db->lasterror();
4182 $this->errors[] = $this->error;
4184 $langs->load(
'errors');
4185 $num = $this->
db->num_rows($resqlCheckShipmentLine);
4188 $objCheckShipmentLine = $this->
db->fetch_object($resqlCheckShipmentLine);
4189 $this->error = $langs->trans(
'ErrorRecordAlreadyExists').
' : '.$langs->trans(
'ShipmentLine').
' '.$objCheckShipmentLine->rowid;
4190 $this->errors[] = $this->error;
4192 $this->
db->free($resqlCheckShipmentLine);
4195 dol_syslog(__METHOD__.
'Error ; '.$this->error, LOG_ERR);
4201 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"commandedet WHERE rowid=".$this->rowid;
4210 $this->
id = $this->rowid;
4215 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
4219 if (!$error && !$notrigger)
4222 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
4223 if ($result < 0) $error++;
4228 $this->
db->commit();
4232 foreach ($this->errors as $errmsg)
4234 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
4235 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4237 $this->
db->rollback();
4240 $this->error = $this->
db->lasterror();
4252 public function insert($user = null, $notrigger = 0)
4254 global $langs, $conf;
4258 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4260 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
4263 if (empty($this->tva_tx)) $this->tva_tx = 0;
4264 if (empty($this->localtax1_tx)) $this->localtax1_tx = 0;
4265 if (empty($this->localtax2_tx)) $this->localtax2_tx = 0;
4266 if (empty($this->localtax1_type)) $this->localtax1_type = 0;
4267 if (empty($this->localtax2_type)) $this->localtax2_type = 0;
4268 if (empty($this->total_localtax1)) $this->total_localtax1 = 0;
4269 if (empty($this->total_localtax2)) $this->total_localtax2 = 0;
4270 if (empty($this->rang)) $this->rang = 0;
4271 if (empty($this->remise)) $this->remise = 0;
4272 if (empty($this->remise_percent)) $this->remise_percent = 0;
4273 if (empty($this->info_bits)) $this->info_bits = 0;
4274 if (empty($this->special_code)) $this->special_code = 0;
4275 if (empty($this->fk_parent_line)) $this->fk_parent_line = 0;
4276 if (empty($this->pa_ht)) $this->pa_ht = 0;
4277 if (empty($this->ref_ext)) $this->ref_ext =
'';
4280 if ($this->pa_ht == 0 && $pa_ht_isemptystring)
4282 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0)
4286 $this->pa_ht = $result;
4291 if ($this->product_type < 0)
return -1;
4296 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'commandedet';
4297 $sql .=
' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
4298 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
4299 $sql .=
' fk_product, product_type, remise_percent, subprice, price, remise, fk_remise_except,';
4300 $sql .=
' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
4301 $sql .=
' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
4303 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
4305 $sql .=
" VALUES (".$this->fk_commande.
",";
4306 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
4307 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
4308 $sql .=
" '".$this->db->escape($this->desc).
"',";
4309 $sql .=
" '".price2num($this->qty).
"',";
4310 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
4311 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
4312 $sql .=
" '".price2num($this->tva_tx).
"',";
4313 $sql .=
" '".price2num($this->localtax1_tx).
"',";
4314 $sql .=
" '".price2num($this->localtax2_tx).
"',";
4315 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
4316 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
4317 $sql .=
' '.(!empty($this->fk_product) ? $this->fk_product :
"null").
',';
4318 $sql .=
" '".$this->db->escape($this->product_type).
"',";
4319 $sql .=
" '".price2num($this->remise_percent).
"',";
4320 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice) :
"null").
",";
4321 $sql .=
" ".($this->price !=
'' ?
"'".price2num($this->
price).
"'" :
"null").
",";
4322 $sql .=
" '".price2num($this->remise).
"',";
4323 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
4324 $sql .=
' '.$this->special_code.
',';
4325 $sql .=
' '.$this->rang.
',';
4326 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
4327 $sql .=
' '.price2num($this->pa_ht).
',';
4328 $sql .=
" '".$this->db->escape($this->info_bits).
"',";
4329 $sql .=
" ".price2num($this->total_ht).
",";
4330 $sql .=
" ".price2num($this->total_tva).
",";
4331 $sql .=
" ".price2num($this->total_localtax1).
",";
4332 $sql .=
" ".price2num($this->total_localtax2).
",";
4333 $sql .=
" ".price2num($this->total_ttc).
",";
4334 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
4335 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
',';
4336 $sql .=
' '.(!$this->fk_unit ?
'NULL' : $this->fk_unit);
4337 $sql .=
", ".(!empty($this->fk_multicurrency) ? $this->fk_multicurrency :
'NULL');
4338 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
4339 $sql .=
", ".$this->multicurrency_subprice;
4340 $sql .=
", ".$this->multicurrency_total_ht;
4341 $sql .=
", ".$this->multicurrency_total_tva;
4342 $sql .=
", ".$this->multicurrency_total_ttc;
4345 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
4349 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
4350 $this->
rowid = $this->id;
4361 if (!$error && !$notrigger)
4364 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
4365 if ($result < 0) $error++;
4370 $this->
db->commit();
4374 foreach ($this->errors as $errmsg)
4376 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
4377 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4379 $this->
db->rollback();
4382 $this->error = $this->
db->error();
4383 $this->
db->rollback();
4397 global $conf, $langs;
4401 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4404 if (empty($this->tva_tx)) $this->tva_tx = 0;
4405 if (empty($this->localtax1_tx)) $this->localtax1_tx = 0;
4406 if (empty($this->localtax2_tx)) $this->localtax2_tx = 0;
4407 if (empty($this->localtax1_type)) $this->localtax1_type = 0;
4408 if (empty($this->localtax2_type)) $this->localtax2_type = 0;
4409 if (empty($this->qty)) $this->qty = 0;
4410 if (empty($this->total_localtax1)) $this->total_localtax1 = 0;
4411 if (empty($this->total_localtax2)) $this->total_localtax2 = 0;
4412 if (empty($this->marque_tx)) $this->marque_tx = 0;
4413 if (empty($this->marge_tx)) $this->marge_tx = 0;
4414 if (empty($this->remise)) $this->remise = 0;
4415 if (empty($this->remise_percent)) $this->remise_percent = 0;
4416 if (empty($this->info_bits)) $this->info_bits = 0;
4417 if (empty($this->special_code)) $this->special_code = 0;
4418 if (empty($this->product_type)) $this->product_type = 0;
4419 if (empty($this->fk_parent_line)) $this->fk_parent_line = 0;
4420 if (empty($this->pa_ht)) $this->pa_ht = 0;
4421 if (empty($this->ref_ext)) $this->ref_ext =
'';
4424 if ($this->pa_ht == 0 && $pa_ht_isemptystring)
4426 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0)
4430 $this->pa_ht = $result;
4437 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4438 $sql .=
" description='".$this->db->escape($this->desc).
"'";
4439 $sql .=
" , label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
4440 $sql .=
" , vat_src_code=".(!empty($this->vat_src_code) ?
"'".$this->db->escape($this->vat_src_code).
"'" :
"''");
4441 $sql .=
" , tva_tx=".price2num($this->tva_tx);
4442 $sql .=
" , localtax1_tx=".price2num($this->localtax1_tx);
4443 $sql .=
" , localtax2_tx=".price2num($this->localtax2_tx);
4444 $sql .=
" , localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
4445 $sql .=
" , localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
4446 $sql .=
" , qty=".price2num($this->qty);
4447 $sql .=
" , ref_ext='".$this->db->escape($this->ref_ext).
"'";
4448 $sql .=
" , subprice=".price2num($this->subprice).
"";
4449 $sql .=
" , remise_percent=".price2num($this->remise_percent).
"";
4450 $sql .=
" , price=".price2num($this->
price).
"";
4451 $sql .=
" , remise=".price2num($this->remise).
"";
4452 if (empty($this->skip_update_total))
4454 $sql .=
" , total_ht=".price2num($this->total_ht).
"";
4455 $sql .=
" , total_tva=".price2num($this->total_tva).
"";
4456 $sql .=
" , total_ttc=".price2num($this->total_ttc).
"";
4457 $sql .=
" , total_localtax1=".price2num($this->total_localtax1);
4458 $sql .=
" , total_localtax2=".price2num($this->total_localtax2);
4460 $sql .=
" , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null");
4461 $sql .=
" , buy_price_ht='".price2num($this->pa_ht).
"'";
4462 $sql .=
" , info_bits=".$this->info_bits;
4463 $sql .=
" , special_code=".$this->special_code;
4464 $sql .=
" , date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
4465 $sql .=
" , date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
4466 $sql .=
" , product_type=".$this->product_type;
4467 $sql .=
" , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line :
"null");
4468 if (!empty($this->rang)) $sql .=
", rang=".$this->rang;
4469 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
4472 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice).
"";
4473 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht).
"";
4474 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva).
"";
4475 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).
"";
4477 $sql .=
" WHERE rowid = ".$this->rowid;
4479 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
4485 $this->
id = $this->rowid;
4493 if (!$error && !$notrigger)
4496 $result = $this->
call_trigger(
'LINEORDER_UPDATE', $user);
4497 if ($result < 0) $error++;
4502 $this->
db->commit();
4506 foreach ($this->errors as $errmsg)
4508 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
4509 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4511 $this->
db->rollback();
4514 $this->error = $this->
db->error();
4515 $this->
db->rollback();
4533 if (empty($this->total_localtax1)) $this->total_localtax1 = 0;
4534 if (empty($this->total_localtax2)) $this->total_localtax2 = 0;
4537 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4538 $sql .=
" total_ht='".price2num($this->total_ht).
"'";
4539 $sql .=
",total_tva='".price2num($this->total_tva).
"'";
4540 $sql .=
",total_localtax1='".price2num($this->total_localtax1).
"'";
4541 $sql .=
",total_localtax2='".price2num($this->total_localtax2).
"'";
4542 $sql .=
",total_ttc='".price2num($this->total_ttc).
"'";
4543 $sql .=
" WHERE rowid = ".$this->rowid;
4545 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
4550 $this->
db->commit();
4553 $this->error = $this->
db->error();
4554 $this->
db->rollback();
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
getNbOfProductsLines()
Return number of line with type product.
Class to manage stock movements.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
const STATUS_CLOSED
Closed (Sent, billed or not)
getLinesArray()
Create an array of order lines.
LibStatut($status, $billed, $mode, $donotshowbilled=0)
Return label of status.
set_date($user, $date, $notrigger=0)
Set the order date.
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
cancel($idwarehouse=-1)
Cancel an order If stock is decremented on order validation, we must reincrement it.
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
static getIdAndTxFromCode($db, $code, $date_document= '')
Get id and rate of currency from code.
set_date_livraison($user, $delivery_date, $notrigger=0)
Set delivery date.
getNextNumRef($soc)
Returns the reference to the following non used Order depending on the active numbering module define...
update(User $user, $notrigger=0)
Update the line object into db.
getNbOfServicesLines()
Return number of line with type service.
hasDelay()
Is the customer order delayed?
demand_reason($demand_reason_id, $notrigger=0)
Update order demand_reason.
fetch($id, $ref= '', $ref_ext= '', $notused= '')
Get object from database.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
__construct($db)
Constructor.
</td >< tdcolspan="3">< spanclass="opacitymedium"></span ></td ></tr >< trclass="liste_total"> CREANCES DETTES< tdcolspan="3"class="right"></td >< tdcolspan="3"class="right"></td ></tr > CREANCES DETTES RECETTES DEPENSES trips CREANCES DETTES Y m expensereport p date_valid Y m expensereport pe datep $db idate($date_start)."' AND $column < p rowid
Class to manage products or services.
setDraft($user, $idwarehouse=-1)
Set draft status.
dol_now($mode= 'auto')
Return date for now.
line_order($renum=false, $rowidorder= 'ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
set_remise($user, $remise, $notrigger=0)
Applique une remise relative.
dol_delete_preview($object)
Delete all preview files linked to object instance.
delete_linked_contact($source= '', $code= '')
Delete all links between an object $this and all its contacts.
loadExpeditions($filtre_statut=-1)
Load array this->expeditions of lines of shipments with nb of products sent for each order line Note:...
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller= '', $localtaxes_array= '', $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code= '')
Calculate totals (net, vat, ...) of a line.
Class to manage Dolibarr users.
load_board($user)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
Class to manage Dolibarr database access.
add_contact($fk_socpeople, $type_contact, $source= 'external', $notrigger=0)
Add a link between element $this->element and a contact.
initAsSpecimen()
Initialise an instance with random values.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
classifyBilled(User $user, $notrigger=0)
Classify the order as invoiced.
valid($user, $idwarehouse=0, $notrigger=0)
Validate order.
const STATUS_SHIPMENTONPROCESS
Shipment on process.
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Fonction qui renvoie si tva doit etre tva percue recuperable.
availability($availability_id, $notrigger=0)
Update delivery delay.
$pos_source
key of pos source ('0', '1', ...)
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate, from a $thirdparty_buyer to a $thirdparty_seller Note: This function applies same rules than get_default_tva.
fetch_lines($only_product=0, $loadalsotranslation=0)
Load array lines.
$conf db
API class for accounts.
nb_expedition()
Returns a array with expeditions lines number.
info($id)
Charge les informations d'ordre info dans l'objet commande.
set_remise_absolue($user, $remise, $notrigger=0)
Applique une remise absolue.
price($amount, $form=0, $outlangs= '', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code= '')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
Class to manage order lines.
insertExtraFields($trigger= '', $userused=null)
Add/Update all extra fields values for the current object.
stock_array($filtre_statut=self::STATUS_CANCELED)
Return a array with the pending stock by product.
const STOCK_NOT_ENOUGH_FOR_ORDER
ERR Not enough stock.
create($user, $notrigger=0)
Create order Note that this->ref can be set or empty.
insert_discount($idremise)
Adding line of fixed discount in the order in DB.
Class to manage third parties objects (customers, suppliers, prospects...)
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
insert($user=null, $notrigger=0)
Insert line into database.
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
deleteEcmFiles($mode=0)
Delete related files of object in database.
Class to manage shipments.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static getIdFromCode($db, $code)
Get id of currency from code.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Class to manage customers orders.
const STATUS_DRAFT
Draft status.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
Remove a file or several files with a mask.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
deleteExtraFields()
Delete all extra fields values for the current object.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
set_reopen($user)
Tag the order as validated (opened) Function used when order is reopend after being closed...
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
deleteline($user=null, $lineid=0)
Delete an order line.
getLibStatut($mode)
Return status label of Order.
liste_array($shortlist=0, $draft=0, $excluser= '', $socid=0, $limit=0, $offset=0, $sortfield= 'c.date_commande', $sortorder= 'DESC')
Return list of orders (eventuelly filtered on a user) into an array.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
Superclass for orders classes.
deleteObjectLinked($sourceid=null, $sourcetype= '', $targetid=null, $targettype= '', $rowid= '')
Delete all links between an object $this.
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0)
Get buy price to use for margin calculation.
print $_SERVER["PHP_SELF"]
Edit parameters.
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= '')
Update a line in database.
const STATUS_VALIDATED
Validated status.
copy_linked_contact($objFrom, $source= 'internal')
Copy contact from one element to current.
add_product($idproduct, $qty, $remise_percent=0.0, $date_start= '', $date_end= '')
Add line into array $this->client must be loaded.
print
Draft customers invoices.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
call_trigger($triggerName, $user)
Call trigger based on this instance.
createFromClone(User $user, $socid=0)
Load an object from its id and create a new one in database.
$module_source
key of module source when order generated from a dedicated module ('cashdesk', 'takepos', ...)
Superclass for orders classes.
getNomUrl($withpicto=0, $option= '', $max=0, $short=0, $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0)
Return clicable link of object (with eventually picto)
cloture($user, $notrigger=0)
Close order.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that return vat rate of a product line (according to seller, buyer and product vat rate) Si ...
getLabelSource()
Return source label of order.
classifyUnBilled()
Classify the order as not invoiced.
Class to manage absolute discounts.
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
load_state_board()
Charge indicateurs this->nb de tableau de bord.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
update(User $user, $notrigger=0)
Update database.
add_object_linked($origin=null, $origin_id=null)
Add objects linked in llx_element_element.
createFromProposal($object, User $user)
Load an object from a proposal and create a new order into database.
update_price($exclspec=0, $roundingadjust= 'none', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines)...
update_total()
Update DB line fields total_xxx Used by migration.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
setDeliveryDate($user, $delivery_date, $notrigger=0)
Set the planned delivery date.
if(!empty($search_group)) natural_search(array("g.nom"g note
fetch($rowid)
Load line order.
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= '')
Add an order line into database (linked to product/service or not)
showDelay()
Show the customer delayed info.
getNbOfShipments()
Count number of shipments for this order.
__construct($db)
Constructor.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
set_ref_client($user, $ref_client, $notrigger=0)
Set customer ref.
const STATUS_CANCELED
Canceled status.