38include_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
39require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
40require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
41require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
42require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
43require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
54 public $element =
'commande';
59 public $table_element =
'commande';
64 public $table_element_line =
'commandedet';
69 public $class_element_line =
'OrderLine';
74 public $fk_element =
'fk_commande';
79 public $picto =
'order';
85 public $restrictiononfksoc = 1;
105 public $ref_customer;
126 public $date_lim_reglement;
130 public $cond_reglement_code;
135 public $cond_reglement_doc;
142 public $deposit_percent;
152 public $mode_reglement;
157 public $mode_reglement_id;
162 public $mode_reglement_code;
168 public $availability_id;
174 public $availability_code;
180 public $availability;
185 public $demand_reason_id;
190 public $demand_reason_code;
202 public $date_commande;
207 public $delivery_date;
212 public $fk_remise_except;
224 public $warehouse_id;
226 public $extraparams = array();
228 public $linked_objects = array();
233 public $user_author_id;
243 public $lines = array();
259 public $online_payment_url;
291 public $fields = array(
292 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 10),
293 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'default' =>
'1',
'enabled' => 1,
'visible' => -2,
'notnull' => 1,
'position' => 20,
'index' => 1),
294 'ref' => array(
'type' =>
'varchar(30)',
'label' =>
'Ref',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'showoncombobox' => 1,
'position' => 25),
295 'ref_ext' => array(
'type' =>
'varchar(255)',
'label' =>
'RefExt',
'enabled' => 1,
'visible' => 0,
'position' => 26),
296 'ref_client' => array(
'type' =>
'varchar(255)',
'label' =>
'RefCustomer',
'enabled' => 1,
'visible' => -1,
'position' => 28),
297 'fk_soc' => array(
'type' =>
'integer:Societe:societe/class/societe.class.php',
'label' =>
'ThirdParty',
'enabled' =>
'isModEnabled("societe")',
'visible' => -1,
'notnull' => 1,
'position' => 20),
298 'fk_projet' => array(
'type' =>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label' =>
'Project',
'enabled' =>
"isModEnabled('project')",
'visible' => -1,
'position' => 25),
299 'date_commande' => array(
'type' =>
'date',
'label' =>
'Date',
'enabled' => 1,
'visible' => 1,
'position' => 60,
'csslist' =>
'nowraponall'),
300 'date_valid' => array(
'type' =>
'datetime',
'label' =>
'DateValidation',
'enabled' => 1,
'visible' => -1,
'position' => 62,
'csslist' =>
'nowraponall'),
301 'date_cloture' => array(
'type' =>
'datetime',
'label' =>
'DateClosing',
'enabled' => 1,
'visible' => -1,
'position' => 65,
'csslist' =>
'nowraponall'),
302 'fk_user_valid' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserValidation',
'enabled' => 1,
'visible' => -1,
'position' => 85),
303 'fk_user_cloture' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserClosing',
'enabled' => 1,
'visible' => -1,
'position' => 90),
304 'source' => array(
'type' =>
'smallint(6)',
'label' =>
'Source',
'enabled' => 1,
'visible' => -1,
'position' => 95),
305 'total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'VAT',
'enabled' => 1,
'visible' => -1,
'position' => 125,
'isameasure' => 1),
306 'localtax1' => array(
'type' =>
'double(24,8)',
'label' =>
'LocalTax1',
'enabled' => 1,
'visible' => -1,
'position' => 130,
'isameasure' => 1),
307 'localtax2' => array(
'type' =>
'double(24,8)',
'label' =>
'LocalTax2',
'enabled' => 1,
'visible' => -1,
'position' => 135,
'isameasure' => 1),
308 'total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalHT',
'enabled' => 1,
'visible' => -1,
'position' => 140,
'isameasure' => 1),
309 'total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalTTC',
'enabled' => 1,
'visible' => -1,
'position' => 145,
'isameasure' => 1),
310 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => 0,
'position' => 150),
311 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => 0,
'position' => 155),
312 'model_pdf' => array(
'type' =>
'varchar(255)',
'label' =>
'PDFTemplate',
'enabled' => 1,
'visible' => 0,
'position' => 160),
313 'fk_account' => array(
'type' =>
'integer',
'label' =>
'BankAccount',
'enabled' =>
'isModEnabled("bank")',
'visible' => -1,
'position' => 170),
314 'fk_currency' => array(
'type' =>
'varchar(3)',
'label' =>
'MulticurrencyID',
'enabled' => 1,
'visible' => -1,
'position' => 175),
315 'fk_cond_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentTerm',
'enabled' => 1,
'visible' => -1,
'position' => 180),
316 'deposit_percent' => array(
'type' =>
'varchar(63)',
'label' =>
'DepositPercent',
'enabled' => 1,
'visible' => -1,
'position' => 181),
317 'fk_mode_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentMode',
'enabled' => 1,
'visible' => -1,
'position' => 185),
318 'date_livraison' => array(
'type' =>
'date',
'label' =>
'DateDeliveryPlanned',
'enabled' => 1,
'visible' => -1,
'position' => 190,
'csslist' =>
'nowraponall'),
319 'fk_shipping_method' => array(
'type' =>
'integer',
'label' =>
'ShippingMethod',
'enabled' => 1,
'visible' => -1,
'position' => 195),
320 'fk_warehouse' => array(
'type' =>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label' =>
'Fk warehouse',
'enabled' =>
'isModEnabled("stock")',
'visible' => -1,
'position' => 200),
321 'fk_availability' => array(
'type' =>
'integer',
'label' =>
'Availability',
'enabled' => 1,
'visible' => -1,
'position' => 205),
322 'fk_input_reason' => array(
'type' =>
'integer',
'label' =>
'InputReason',
'enabled' => 1,
'visible' => -1,
'position' => 210),
324 'extraparams' => array(
'type' =>
'varchar(255)',
'label' =>
'Extraparams',
'enabled' => 1,
'visible' => -1,
'position' => 225),
325 'fk_incoterms' => array(
'type' =>
'integer',
'label' =>
'IncotermCode',
'enabled' =>
'$conf->incoterm->enabled',
'visible' => -1,
'position' => 230),
326 'location_incoterms' => array(
'type' =>
'varchar(255)',
'label' =>
'IncotermLabel',
'enabled' =>
'$conf->incoterm->enabled',
'visible' => -1,
'position' => 235),
327 'fk_multicurrency' => array(
'type' =>
'integer',
'label' =>
'Fk multicurrency',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 240),
328 'multicurrency_code' => array(
'type' =>
'varchar(255)',
'label' =>
'MulticurrencyCurrency',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 245),
329 'multicurrency_tx' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyRate',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 250,
'isameasure' => 1),
330 'multicurrency_total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountHT',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 255,
'isameasure' => 1),
331 'multicurrency_total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountVAT',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 260,
'isameasure' => 1),
332 'multicurrency_total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountTTC',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 265,
'isameasure' => 1),
333 'last_main_doc' => array(
'type' =>
'varchar(255)',
'label' =>
'LastMainDoc',
'enabled' => 1,
'visible' => -1,
'position' => 270),
334 'module_source' => array(
'type' =>
'varchar(32)',
'label' =>
'POSModule',
'enabled' => 1,
'visible' => -1,
'position' => 275),
335 'pos_source' => array(
'type' =>
'varchar(32)',
'label' =>
'POSTerminal',
'enabled' => 1,
'visible' => -1,
'position' => 280),
336 'fk_user_author' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserAuthor',
'enabled' => 1,
'visible' => -1,
'position' => 300),
337 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' => 1,
'visible' => -2,
'notnull' => -1,
'position' => 302),
338 'date_creation' => array(
'type' =>
'datetime',
'label' =>
'DateCreation',
'enabled' => 1,
'visible' => -2,
'position' => 304,
'csslist' =>
'nowraponall'),
339 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 306),
340 'import_key' => array(
'type' =>
'varchar(14)',
'label' =>
'ImportId',
'enabled' => 1,
'visible' => -2,
'position' => 400),
341 'fk_statut' => array(
'type' =>
'smallint(6)',
'label' =>
'Status',
'enabled' => 1,
'visible' => -1,
'position' => 500),
366 const STATUS_ACCEPTED = 2;
383 $this->ismultientitymanaged = 1;
384 $this->isextrafieldmanaged = 1;
396 global $langs, $conf;
397 $langs->load(
"order");
406 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
407 foreach ($dirmodels as $reldir) {
411 $mybool = ((bool) @include_once $dir.$file) || $mybool;
414 if ($mybool ===
false) {
419 $obj =
new $classname();
420 $numref = $obj->getNextValue($soc, $this);
425 $this->error = $obj->error;
430 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_COMMANDE_ADDON_NotDefined");
444 public function valid($user, $idwarehouse = 0, $notrigger = 0)
446 global $conf, $langs;
448 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
453 if ($this->
statut == self::STATUS_VALIDATED) {
454 dol_syslog(get_class($this).
"::valid action abandoned: already validated", LOG_WARNING);
458 if (!((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
459 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'validate')))) {
460 $this->error =
'NotEnoughPermissions';
461 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
471 $soc->fetch($this->socid);
474 $result = $soc->setAsCustomer();
477 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
485 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
486 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
487 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
488 $sql .=
" date_valid='".$this->db->idate($now).
"',";
489 $sql .=
" fk_user_valid = ".($user->id > 0 ? (int) $user->id :
"null").
",";
490 $sql .=
" fk_user_modif = ".((int) $user->id);
491 $sql .=
" WHERE rowid = ".((int) $this->
id);
493 dol_syslog(get_class($this).
"::valid", LOG_DEBUG);
494 $resql = $this->db->query($sql);
497 $this->error = $this->db->lasterror();
503 if ($result >= 0 && isModEnabled(
'stock') &&
getDolGlobalInt(
'STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) {
504 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
505 $langs->load(
"agenda");
508 $cpt = count($this->lines);
509 for ($i = 0; $i < $cpt; $i++) {
510 if ($this->lines[$i]->fk_product > 0) {
512 $mouvP->origin = &$this;
513 $mouvP->setOrigin($this->element, $this->
id);
515 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"OrderValidatedInDolibarr", $num));
518 $this->error = $mouvP->error;
528 if (!$error && !$notrigger) {
538 $this->oldref = $this->ref;
541 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
543 $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).
"'";
544 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'commande/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
545 $resql = $this->db->query($sql);
548 $this->error = $this->db->lasterror();
550 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filepath = 'commande/".$this->db->escape($this->newref).
"'";
551 $sql .=
" WHERE filepath = 'commande/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
552 $resql = $this->db->query($sql);
555 $this->error = $this->db->lasterror();
561 $dirsource = $conf->commande->multidir_output[$this->entity].
'/'.$oldref;
562 $dirdest = $conf->commande->multidir_output[$this->entity].
'/'.$newref;
563 if (!$error && file_exists($dirsource)) {
564 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
566 if (@rename($dirsource, $dirdest)) {
569 $listoffiles =
dol_dir_list($conf->commande->multidir_output[$this->entity].
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
570 foreach ($listoffiles as $fileentry) {
571 $dirsource = $fileentry[
'name'];
572 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
573 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
574 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
575 @rename($dirsource, $dirdest);
593 $this->db->rollback();
609 global $conf, $langs;
614 if ($this->
statut <= self::STATUS_DRAFT) {
618 if (!((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
619 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'validate')))) {
620 $this->error =
'Permission denied';
628 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
629 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT.
",";
630 $sql .=
" fk_user_modif = ".((int) $user->id);
631 $sql .=
" WHERE rowid = ".((int) $this->
id);
633 if ($this->db->query($sql)) {
635 $this->oldcopy = clone $this;
639 if (isModEnabled(
'stock') &&
getDolGlobalInt(
'STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) {
642 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
643 $langs->load(
"agenda");
645 $num = count($this->lines);
646 for ($i = 0; $i < $num; $i++) {
647 if ($this->lines[$i]->fk_product > 0) {
649 $mouvP->origin = &$this;
650 $mouvP->setOrigin($this->element, $this->
id);
652 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderBackToDraftInDolibarr", $this->ref));
655 $this->error = $mouvP->error;
664 $result = $this->
call_trigger(
'ORDER_UNVALIDATE', $user);
675 $this->db->rollback();
679 $this->error = $this->db->error();
680 $this->db->rollback();
699 if ($this->
statut != self::STATUS_CANCELED && $this->
statut != self::STATUS_CLOSED) {
700 dol_syslog(get_class($this).
"::set_reopen order has not status closed", LOG_WARNING);
706 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
707 $sql .=
' SET fk_statut='.self::STATUS_VALIDATED.
', facture=0,';
708 $sql .=
" fk_user_modif = ".((int) $user->id);
709 $sql .=
" WHERE rowid = ".((int) $this->
id);
711 dol_syslog(get_class($this).
"::set_reopen", LOG_DEBUG);
712 $resql = $this->db->query($sql);
722 $this->error = $this->db->lasterror();
733 foreach ($this->errors as $errmsg) {
734 dol_syslog(get_class($this).
"::set_reopen ".$errmsg, LOG_ERR);
735 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
737 $this->db->rollback();
749 public function cloture($user, $notrigger = 0)
755 $usercanclose = ((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
756 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'close')));
759 if ($this->
statut == self::STATUS_CLOSED) {
766 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
767 $sql .=
' SET fk_statut = '.self::STATUS_CLOSED.
',';
768 $sql .=
' fk_user_cloture = '.((int) $user->id).
',';
769 $sql .=
" date_cloture = '".$this->db->idate($now).
"',";
770 $sql .=
" fk_user_modif = ".((int) $user->id);
773 if ($this->db->query($sql)) {
789 $this->db->rollback();
793 $this->error = $this->db->lasterror();
795 $this->db->rollback();
809 public function cancel($idwarehouse = -1)
811 global $conf, $user, $langs;
817 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
818 $sql .=
" SET fk_statut = ".self::STATUS_CANCELED.
",";
819 $sql .=
" fk_user_modif = ".((int) $user->id);
820 $sql .=
" WHERE rowid = ".((int) $this->
id);
821 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
823 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
824 if ($this->db->query($sql)) {
826 if (isModEnabled(
'stock') &&
getDolGlobalInt(
'STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) {
827 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
828 $langs->load(
"agenda");
830 $num = count($this->lines);
831 for ($i = 0; $i < $num; $i++) {
832 if ($this->lines[$i]->fk_product > 0) {
834 $mouvP->setOrigin($this->element, $this->
id);
836 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderCanceledInDolibarr", $this->ref));
839 $this->error = $mouvP->error;
860 foreach ($this->errors as $errmsg) {
861 dol_syslog(get_class($this).
"::cancel ".$errmsg, LOG_ERR);
862 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
864 $this->db->rollback();
868 $this->error = $this->db->error();
869 $this->db->rollback();
882 public function create($user, $notrigger = 0)
884 global $conf, $langs, $mysoc;
890 $date = ($this->date_commande ? $this->date_commande : $this->date);
891 $delivery_date = $this->delivery_date;
894 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
899 if (empty($this->fk_multicurrency)) {
900 $this->multicurrency_code = $conf->currency;
901 $this->fk_multicurrency = 0;
902 $this->multicurrency_tx = 1;
905 dol_syslog(get_class($this).
"::create user=".$user->id);
908 if (!empty($this->
ref)) {
911 $this->error =
'ErrorRefAlreadyExists';
912 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
913 $this->db->rollback();
919 $result = $soc->fetch($this->socid);
921 $this->error =
"Failed to fetch company";
922 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
926 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Source"));
927 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
935 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande (";
936 $sql .=
" ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client";
937 $sql .=
", model_pdf, fk_cond_reglement, deposit_percent, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address";
938 $sql .=
", fk_shipping_method";
939 $sql .=
", fk_warehouse";
940 $sql .=
", fk_incoterms, location_incoterms";
941 $sql .=
", entity, module_source, pos_source";
942 $sql .=
", fk_multicurrency";
943 $sql .=
", multicurrency_code";
944 $sql .=
", multicurrency_tx";
946 $sql .=
" VALUES ('(PROV)', ".((int) $this->socid).
", '".$this->db->idate($now).
"', ".((int) $user->id);
947 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
948 $sql .=
", '".$this->db->idate($date).
"'";
949 $sql .=
", ".($this->source >= 0 && $this->source !=
'' ? $this->db->escape($this->source) :
'null');
950 $sql .=
", '".$this->db->escape($this->note_private).
"'";
951 $sql .=
", '".$this->db->escape($this->note_public).
"'";
952 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
953 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
954 $sql .=
", '".$this->db->escape($this->model_pdf).
"'";
955 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
956 $sql .=
", ".(!empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
"null");
957 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
958 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
959 $sql .=
", ".($this->availability_id > 0 ? ((int) $this->availability_id) :
"null");
960 $sql .=
", ".($this->demand_reason_id > 0 ? ((int) $this->demand_reason_id) :
"null");
961 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
962 $sql .=
", ".($this->fk_delivery_address > 0 ? ((int) $this->fk_delivery_address) :
'NULL');
963 $sql .=
", ".(!empty($this->shipping_method_id) && $this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) :
'NULL');
964 $sql .=
", ".(!empty($this->warehouse_id) && $this->warehouse_id > 0 ? ((int) $this->warehouse_id) :
'NULL');
965 $sql .=
", ".(int) $this->fk_incoterms;
966 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
967 $sql .=
", ".setEntity($this);
968 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
969 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
970 $sql .=
", ".(int) $this->fk_multicurrency;
971 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
972 $sql .=
", ".(float) $this->multicurrency_tx;
975 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
976 $resql = $this->db->query($sql);
978 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commande');
982 $num = count($this->lines);
987 for ($i = 0; $i < $num; $i++) {
988 $line = $this->lines[$i];
992 if (!is_object($line)) {
993 $line = (object) $line;
997 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1002 $vatrate = $line->tva_tx;
1003 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', (
string) $vatrate)) {
1004 $vatrate .=
' ('.$line->vat_src_code.
')';
1008 $originid = $line->origin_id;
1009 $origintype = $line->origin;
1011 $originid = $line->id;
1012 $origintype = $this->element;
1016 if (empty($line->ref_ext)) {
1017 $line->ref_ext =
'';
1025 $line->localtax1_tx,
1026 $line->localtax2_tx,
1028 $line->remise_percent,
1030 $line->fk_remise_except,
1035 $line->product_type,
1037 $line->special_code,
1039 $line->fk_fournprice,
1042 $line->array_options,
1051 if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) {
1052 $this->error = $this->db->lasterror();
1053 $this->errors[] = $this->error;
1056 $this->db->rollback();
1060 if ($result > 0 && $line->product_type == 9) {
1061 $fk_parent_line = $result;
1068 $initialref =
'(PROV'.$this->id.
')';
1069 if (!empty($this->
ref)) {
1070 $initialref = $this->ref;
1073 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"commande SET ref='".$this->db->escape($initialref).
"' WHERE rowid=".((int) $this->
id);
1074 if ($this->db->query($sql)) {
1075 $this->
ref = $initialref;
1077 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1078 $this->linked_objects = $this->linkedObjectsIds;
1082 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1083 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1084 if (is_array($tmp_origin_id)) {
1085 foreach ($tmp_origin_id as $origin_id) {
1088 $this->error = $this->db->lasterror();
1093 $origin_id = $tmp_origin_id;
1096 $this->error = $this->db->lasterror();
1103 if (!$error && $this->
id &&
getDolGlobalString(
'MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) {
1104 $originforcontact = $this->origin;
1105 $originidforcontact = $this->origin_id;
1106 if ($originforcontact ==
'shipping') {
1107 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1109 $exp->fetch($this->origin_id);
1110 $exp->fetchObjectLinked();
1111 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1112 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1113 $originforcontact =
'commande';
1114 if (is_object($value)) {
1115 $originidforcontact = $value->id;
1117 $originidforcontact = $value;
1124 $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";
1125 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact).
"'";
1127 $resqlcontact = $this->db->query($sqlcontact);
1128 if ($resqlcontact) {
1129 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1131 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1145 if (!$error && !$notrigger) {
1155 $this->db->commit();
1158 $this->db->rollback();
1162 $this->error = $this->db->lasterror();
1163 $this->db->rollback();
1170 $this->error = $this->db->lasterror();
1171 $this->db->rollback();
1186 global $conf, $user, $hookmanager;
1193 foreach ($this->lines as $line) {
1194 $line->fetch_optionals();
1198 $objFrom = clone $this;
1201 if (!empty($socid) && $socid != $this->socid) {
1202 $objsoc =
new Societe($this->db);
1204 if ($objsoc->fetch($socid) > 0) {
1205 $this->socid = $objsoc->id;
1206 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1207 $this->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent : 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;
1221 $this->user_author_id = $user->id;
1222 $this->user_validation_id = 0;
1224 $this->date_commande =
dol_now();
1225 $this->date_creation =
'';
1226 $this->date_validation =
'';
1228 $this->ref_client =
'';
1229 $this->ref_customer =
'';
1233 $num = count($this->lines);
1234 for ($i = 0; $i < $num; $i++) {
1235 $this->lines[$i]->ref_ext =
'';
1239 $this->context[
'createfromclone'] =
'createfromclone';
1240 $result = $this->
create($user);
1254 if ($this->socid == $objFrom->socid) {
1263 if (is_object($hookmanager)) {
1264 $parameters = array(
'objFrom' => $objFrom);
1266 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1274 unset($this->context[
'createfromclone']);
1278 $this->db->commit();
1281 $this->db->rollback();
1296 global $conf, $hookmanager;
1298 require_once DOL_DOCUMENT_ROOT .
'/multicurrency/class/multicurrency.class.php';
1299 require_once DOL_DOCUMENT_ROOT .
'/core/class/extrafields.class.php';
1303 $this->date_commande =
dol_now();
1308 for ($i = 0; $i < $num; $i++) {
1311 $line->libelle =
$object->lines[$i]->libelle;
1312 $line->label =
$object->lines[$i]->label;
1313 $line->desc =
$object->lines[$i]->desc;
1314 $line->price =
$object->lines[$i]->price;
1315 $line->subprice =
$object->lines[$i]->subprice;
1316 $line->vat_src_code =
$object->lines[$i]->vat_src_code;
1317 $line->tva_tx =
$object->lines[$i]->tva_tx;
1318 $line->localtax1_tx =
$object->lines[$i]->localtax1_tx;
1319 $line->localtax2_tx =
$object->lines[$i]->localtax2_tx;
1320 $line->qty =
$object->lines[$i]->qty;
1321 $line->fk_remise_except =
$object->lines[$i]->fk_remise_except;
1322 $line->remise_percent =
$object->lines[$i]->remise_percent;
1323 $line->fk_product =
$object->lines[$i]->fk_product;
1324 $line->info_bits =
$object->lines[$i]->info_bits;
1325 $line->product_type =
$object->lines[$i]->product_type;
1326 $line->rang =
$object->lines[$i]->rang;
1327 $line->special_code =
$object->lines[$i]->special_code;
1328 $line->fk_parent_line =
$object->lines[$i]->fk_parent_line;
1329 $line->fk_unit =
$object->lines[$i]->fk_unit;
1331 $line->date_start =
$object->lines[$i]->date_start;
1332 $line->date_end =
$object->lines[$i]->date_end;
1334 $line->fk_fournprice =
$object->lines[$i]->fk_fournprice;
1336 $line->pa_ht = $marginInfos[0];
1337 $line->marge_tx = $marginInfos[1];
1338 $line->marque_tx = $marginInfos[2];
1340 $line->origin =
$object->element;
1341 $line->origin_id =
$object->lines[$i]->id;
1344 $object->lines[$i]->fetch_optionals();
1345 foreach (
$object->lines[$i]->array_options as $options_key => $value) {
1346 $line->array_options[$options_key] = $value;
1349 $this->lines[$i] = $line;
1352 $this->entity =
$object->entity;
1353 $this->socid =
$object->socid;
1354 $this->fk_project =
$object->fk_project;
1355 $this->cond_reglement_id =
$object->cond_reglement_id;
1356 $this->deposit_percent =
$object->deposit_percent;
1357 $this->mode_reglement_id =
$object->mode_reglement_id;
1358 $this->fk_account =
$object->fk_account;
1359 $this->availability_id =
$object->availability_id;
1360 $this->demand_reason_id =
$object->demand_reason_id;
1361 $this->delivery_date =
$object->delivery_date;
1362 $this->shipping_method_id =
$object->shipping_method_id;
1363 $this->warehouse_id =
$object->warehouse_id;
1364 $this->fk_delivery_address =
$object->fk_delivery_address;
1365 $this->contact_id =
$object->contact_id;
1366 $this->ref_client =
$object->ref_client;
1367 $this->ref_customer =
$object->ref_client;
1370 $this->note_private =
$object->note_private;
1371 $this->note_public =
$object->note_public;
1374 $this->origin =
$object->element;
1375 $this->origin_id =
$object->id;
1378 if (!empty($conf->multicurrency->enabled)) {
1379 if (!empty(
$object->multicurrency_code)) {
1380 $this->multicurrency_code =
$object->multicurrency_code;
1383 $this->multicurrency_tx =
$object->multicurrency_tx;
1386 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1388 $this->fk_multicurrency = $tmparray[0];
1389 $this->multicurrency_tx = $tmparray[1];
1393 if (empty($this->fk_multicurrency)) {
1394 $this->multicurrency_code = $conf->currency;
1395 $this->fk_multicurrency = 0;
1396 $this->multicurrency_tx = 1;
1404 $element_extrafields = $e->fetch_name_optionals_label($this->table_element);
1406 foreach (
$object->array_options as $options_key => $value) {
1407 if (array_key_exists(str_replace(
'options_',
'', $options_key), $element_extrafields)) {
1408 $this->array_options[$options_key] = $value;
1412 $this->linked_objects[$this->origin] = $this->origin_id;
1413 if (isset(
$object->other_linked_objects) && is_array(
$object->other_linked_objects) && !empty(
$object->other_linked_objects)) {
1414 $this->linked_objects = array_merge($this->linked_objects,
$object->other_linked_objects);
1417 $ret = $this->
create($user);
1421 $hookmanager->initHooks(array(
'orderdao'));
1423 $parameters = array(
'objFrom' =>
$object);
1425 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1435 $this->
valid($user);
1487 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 = array(), $fk_unit =
null, $origin =
'', $origin_id = 0, $pu_ht_devise = 0, $ref_ext =
'', $noupdateafterinsertline = 0)
1489 global $mysoc, $conf, $langs, $user;
1491 $logtext =
"::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent";
1492 $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";
1493 $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";
1494 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
1496 if ($this->
statut == self::STATUS_DRAFT) {
1497 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1507 if (empty($info_bits)) {
1513 if (empty($txtva)) {
1516 if (empty($txlocaltax1)) {
1519 if (empty($txlocaltax2)) {
1522 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1523 $fk_parent_line = 0;
1525 if (empty($this->fk_multicurrency)) {
1526 $this->fk_multicurrency = 0;
1528 if (empty($ref_ext)) {
1535 $pu_ht_devise =
price2num($pu_ht_devise);
1538 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
1543 if ($price_base_type ==
'HT') {
1548 $label = trim($label);
1549 $desc = trim($desc);
1556 if ($date_start && $date_end && $date_start > $date_end) {
1557 $langs->load(
"errors");
1558 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1564 $product_type = $type;
1565 if (!empty($fk_product) && $fk_product > 0) {
1566 $product =
new Product($this->db);
1567 $result = $product->fetch($fk_product);
1568 $product_type = $product->type;
1570 if (
getDolGlobalString(
'STOCK_MUST_BE_ENOUGH_FOR_ORDER') && $product_type == 0 && $product->stock_reel < $qty) {
1571 $langs->load(
"errors");
1572 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
1573 $this->errors[] = $this->error;
1574 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
1575 $this->db->rollback();
1589 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1590 $vat_src_code = $reg[1];
1591 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1594 $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);
1604 $total_ht = $tabprice[0];
1605 $total_tva = $tabprice[1];
1606 $total_ttc = $tabprice[2];
1607 $total_localtax1 = $tabprice[9];
1608 $total_localtax2 = $tabprice[10];
1609 $pu_ht = $tabprice[3];
1612 $multicurrency_total_ht = $tabprice[16];
1613 $multicurrency_total_tva = $tabprice[17];
1614 $multicurrency_total_ttc = $tabprice[18];
1615 $pu_ht_devise = $tabprice[19];
1619 if ($ranktouse == -1) {
1620 $rangmax = $this->
line_max($fk_parent_line);
1621 $ranktouse = $rangmax + 1;
1630 $price = (float) $pu - $remise;
1636 $this->line->context = $this->context;
1638 $this->line->fk_commande = $this->id;
1639 $this->line->label = $label;
1640 $this->line->desc = $desc;
1641 $this->line->qty = $qty;
1642 $this->line->ref_ext = $ref_ext;
1644 $this->line->vat_src_code = $vat_src_code;
1645 $this->line->tva_tx = $txtva;
1646 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1647 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1648 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1649 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1650 $this->line->fk_product = $fk_product;
1651 $this->line->product_type = $product_type;
1652 $this->line->fk_remise_except = $fk_remise_except;
1654 $this->line->subprice = $pu_ht;
1655 $this->line->rang = $ranktouse;
1656 $this->line->info_bits = $info_bits;
1657 $this->line->total_ht = $total_ht;
1658 $this->line->total_tva = $total_tva;
1659 $this->line->total_localtax1 = $total_localtax1;
1660 $this->line->total_localtax2 = $total_localtax2;
1661 $this->line->total_ttc = $total_ttc;
1662 $this->line->special_code = $special_code;
1663 $this->line->origin = $origin;
1664 $this->line->origin_id = $origin_id;
1665 $this->line->fk_parent_line = $fk_parent_line;
1666 $this->line->fk_unit = $fk_unit;
1668 $this->line->date_start = $date_start;
1669 $this->line->date_end = $date_end;
1671 $this->line->fk_fournprice = $fk_fournprice;
1672 $this->line->pa_ht = $pa_ht;
1675 $this->line->fk_multicurrency = $this->fk_multicurrency;
1676 $this->line->multicurrency_code = $this->multicurrency_code;
1677 $this->line->multicurrency_subprice = $pu_ht_devise;
1678 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1679 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1680 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1683 $this->line->price = $price;
1685 if (is_array($array_options) && count($array_options) > 0) {
1686 $this->line->array_options = $array_options;
1689 $result = $this->line->insert($user);
1692 if (!empty($fk_parent_line)) {
1694 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
1695 $linecount = count($this->lines);
1696 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
1702 if (empty($noupdateafterinsertline)) {
1707 $this->db->commit();
1708 $this->lines[] = $this->line;
1709 return $this->line->id;
1711 $this->db->rollback();
1715 $this->error = $this->line->error;
1716 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1717 $this->db->rollback();
1721 dol_syslog(get_class($this).
"::addline status of order must be Draft to allow use of ->addline()", LOG_ERR);
1744 global $conf, $mysoc;
1750 if ($idproduct > 0) {
1751 $prod =
new Product($this->db);
1752 $prod->fetch($idproduct);
1756 if (empty($tva_tx)) {
1761 $localtax1_tx =
get_localtax($tva_tx, 1, $this->thirdparty, $mysoc, $tva_npr);
1762 $localtax2_tx =
get_localtax($tva_tx, 2, $this->thirdparty, $mysoc, $tva_npr);
1765 if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
1766 $price = $prod->multiprices[$this->thirdparty->price_level];
1768 $price = $prod->price;
1773 $line->context = $this->context;
1775 $line->fk_product = $idproduct;
1776 $line->desc = $prod->description;
1778 $line->subprice = $price;
1780 $line->vat_src_code = $vat_src_code;
1781 $line->tva_tx = $tva_tx;
1782 $line->localtax1_tx = $localtax1_tx;
1783 $line->localtax2_tx = $localtax2_tx;
1785 $line->product_ref = $prod->ref;
1786 $line->product_label = $prod->label;
1787 $line->product_desc = $prod->description;
1788 $line->fk_unit = $prod->fk_unit;
1792 $line->date_start = $date_start;
1795 $line->date_end = $date_end;
1798 $this->lines[] = $line;
1831 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
1834 if (empty($id) && empty($ref) && empty($ref_ext)) {
1838 $sql =
'SELECT c.rowid, c.entity, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_user_valid, c.fk_user_modif, c.fk_statut';
1839 $sql .=
', c.amount_ht, c.total_ht, c.total_ttc, c.total_tva, c.localtax1 as total_localtax1, c.localtax2 as total_localtax2, c.fk_cond_reglement, c.deposit_percent, c.fk_mode_reglement, c.fk_availability, c.fk_input_reason';
1840 $sql .=
', c.fk_account';
1841 $sql .=
', c.date_commande, c.date_valid, c.tms';
1842 $sql .=
', c.date_livraison as delivery_date';
1843 $sql .=
', c.fk_shipping_method';
1844 $sql .=
', c.fk_warehouse';
1845 $sql .=
', c.fk_projet as fk_project, c.source, c.facture as billed';
1846 $sql .=
', c.note_private, c.note_public, c.ref_client, c.ref_ext, c.model_pdf, c.last_main_doc, c.fk_delivery_address, c.extraparams';
1847 $sql .=
', c.fk_incoterms, c.location_incoterms';
1848 $sql .=
", c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc";
1849 $sql .=
", c.module_source, c.pos_source";
1850 $sql .=
", i.libelle as label_incoterms";
1851 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
1852 $sql .=
', cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle, cr.libelle_facture as cond_reglement_libelle_doc';
1853 $sql .=
', ca.code as availability_code, ca.label as availability_label';
1854 $sql .=
', dr.code as demand_reason_code';
1855 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
1856 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON c.fk_cond_reglement = cr.rowid';
1857 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON c.fk_mode_reglement = p.id';
1858 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON c.fk_availability = ca.rowid';
1859 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON c.fk_input_reason = dr.rowid';
1860 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
1863 $sql .=
" WHERE c.rowid=".((int) $id);
1865 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
1869 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
1872 $sql .=
" AND c.ref_ext='".$this->db->escape($ref_ext).
"'";
1875 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1876 $result = $this->db->query($sql);
1878 $obj = $this->db->fetch_object($result);
1880 $this->
id = $obj->rowid;
1881 $this->entity = $obj->entity;
1883 $this->
ref = $obj->ref;
1884 $this->ref_client = $obj->ref_client;
1885 $this->ref_customer = $obj->ref_client;
1886 $this->ref_ext = $obj->ref_ext;
1888 $this->socid = $obj->fk_soc;
1889 $this->thirdparty =
null;
1891 $this->fk_project = $obj->fk_project;
1892 $this->project =
null;
1894 $this->
statut = $obj->fk_statut;
1895 $this->
status = $obj->fk_statut;
1897 $this->user_author_id = $obj->fk_user_author;
1898 $this->user_creation_id = $obj->fk_user_author;
1899 $this->user_validation_id = $obj->fk_user_valid;
1900 $this->user_modification_id = $obj->fk_user_modif;
1901 $this->total_ht = $obj->total_ht;
1902 $this->total_tva = $obj->total_tva;
1903 $this->total_localtax1 = $obj->total_localtax1;
1904 $this->total_localtax2 = $obj->total_localtax2;
1905 $this->total_ttc = $obj->total_ttc;
1906 $this->date = $this->db->jdate($obj->date_commande);
1907 $this->date_commande = $this->db->jdate($obj->date_commande);
1908 $this->date_creation = $this->db->jdate($obj->date_creation);
1909 $this->date_validation = $this->db->jdate($obj->date_valid);
1910 $this->date_modification = $this->db->jdate($obj->tms);
1911 $this->source = $obj->source;
1912 $this->billed = $obj->billed;
1913 $this->note = $obj->note_private;
1914 $this->note_private = $obj->note_private;
1915 $this->note_public = $obj->note_public;
1916 $this->model_pdf = $obj->model_pdf;
1917 $this->last_main_doc = $obj->last_main_doc;
1918 $this->mode_reglement_id = $obj->fk_mode_reglement;
1919 $this->mode_reglement_code = $obj->mode_reglement_code;
1920 $this->mode_reglement = $obj->mode_reglement_libelle;
1921 $this->cond_reglement_id = $obj->fk_cond_reglement;
1922 $this->cond_reglement_code = $obj->cond_reglement_code;
1923 $this->cond_reglement = $obj->cond_reglement_libelle;
1924 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1925 $this->deposit_percent = $obj->deposit_percent;
1926 $this->fk_account = $obj->fk_account;
1927 $this->availability_id = $obj->fk_availability;
1928 $this->availability_code = $obj->availability_code;
1930 $this->demand_reason_id = $obj->fk_input_reason;
1931 $this->demand_reason_code = $obj->demand_reason_code;
1932 $this->delivery_date = $this->db->jdate($obj->delivery_date);
1933 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method :
null;
1934 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse :
null;
1935 $this->fk_delivery_address = $obj->fk_delivery_address;
1936 $this->module_source = $obj->module_source;
1937 $this->pos_source = $obj->pos_source;
1940 $this->fk_incoterms = $obj->fk_incoterms;
1941 $this->location_incoterms = $obj->location_incoterms;
1942 $this->label_incoterms = $obj->label_incoterms;
1945 $this->fk_multicurrency = $obj->fk_multicurrency;
1946 $this->multicurrency_code = $obj->multicurrency_code;
1947 $this->multicurrency_tx = $obj->multicurrency_tx;
1948 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1949 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1950 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1952 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
1954 $this->lines = array();
1960 $this->db->free($result);
1969 $this->error =
'Order with id '.$id.
' not found sql='.$sql;
1973 $this->error = $this->db->error();
1991 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1992 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1997 $result = $remise->fetch($idremise);
2000 if ($remise->fk_facture) {
2001 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2002 $this->db->rollback();
2008 $line->fk_commande = $this->id;
2009 $line->fk_remise_except = $remise->id;
2010 $line->desc = $remise->description;
2011 $line->vat_src_code = $remise->vat_src_code;
2012 $line->tva_tx = $remise->tva_tx;
2013 $line->subprice = -$remise->amount_ht;
2014 $line->price = -$remise->amount_ht;
2015 $line->fk_product = 0;
2017 $line->remise_percent = 0;
2019 $line->info_bits = 2;
2021 $line->total_ht = -$remise->amount_ht;
2022 $line->total_tva = -$remise->amount_tva;
2023 $line->total_ttc = -$remise->amount_ttc;
2025 $result = $line->insert();
2029 $this->db->commit();
2032 $this->db->rollback();
2036 $this->error = $line->error;
2037 $this->errors = $line->errors;
2038 $this->db->rollback();
2042 $this->db->rollback();
2056 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2059 global $langs, $conf;
2061 $this->lines = array();
2063 $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,';
2064 $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,';
2065 $sql .=
' l.total_ht, l.total_ttc, l.total_tva, l.total_localtax1, l.total_localtax2, l.date_start, l.date_end,';
2066 $sql .=
' l.fk_unit,';
2067 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2068 $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,';
2069 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units';
2070 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as l';
2071 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (p.rowid = l.fk_product)';
2072 $sql .=
' WHERE l.fk_commande = '.((int) $this->
id);
2073 if ($only_product) {
2074 $sql .=
' AND p.fk_product_type = 0';
2076 $sql .=
' ORDER BY l.rang, l.rowid';
2078 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
2079 $result = $this->db->query($sql);
2081 $num = $this->db->num_rows($result);
2085 $objp = $this->db->fetch_object($result);
2089 $line->rowid = $objp->rowid;
2090 $line->id = $objp->rowid;
2091 $line->fk_commande = $objp->fk_commande;
2092 $line->commande_id = $objp->fk_commande;
2093 $line->label = $objp->custom_label;
2094 $line->desc = $objp->description;
2095 $line->description = $objp->description;
2096 $line->product_type = $objp->product_type;
2097 $line->qty = $objp->qty;
2098 $line->ref_ext = $objp->ref_ext;
2100 $line->vat_src_code = $objp->vat_src_code;
2101 $line->tva_tx = $objp->tva_tx;
2102 $line->localtax1_tx = $objp->localtax1_tx;
2103 $line->localtax2_tx = $objp->localtax2_tx;
2104 $line->localtax1_type = $objp->localtax1_type;
2105 $line->localtax2_type = $objp->localtax2_type;
2106 $line->total_ht = $objp->total_ht;
2107 $line->total_ttc = $objp->total_ttc;
2108 $line->total_tva = $objp->total_tva;
2109 $line->total_localtax1 = $objp->total_localtax1;
2110 $line->total_localtax2 = $objp->total_localtax2;
2111 $line->subprice = $objp->subprice;
2112 $line->fk_remise_except = $objp->fk_remise_except;
2113 $line->remise_percent = $objp->remise_percent;
2114 $line->price = $objp->price;
2115 $line->fk_product = $objp->fk_product;
2116 $line->fk_fournprice = $objp->fk_fournprice;
2117 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2118 $line->pa_ht = $marginInfos[0];
2119 $line->marge_tx = $marginInfos[1];
2120 $line->marque_tx = $marginInfos[2];
2121 $line->rang = $objp->rang;
2122 $line->info_bits = $objp->info_bits;
2123 $line->special_code = $objp->special_code;
2124 $line->fk_parent_line = $objp->fk_parent_line;
2126 $line->ref = $objp->product_ref;
2127 $line->libelle = $objp->product_label;
2129 $line->product_ref = $objp->product_ref;
2130 $line->product_label = $objp->product_label;
2131 $line->product_tosell = $objp->product_tosell;
2132 $line->product_tobuy = $objp->product_tobuy;
2133 $line->product_desc = $objp->product_desc;
2134 $line->product_tobatch = $objp->product_tobatch;
2135 $line->product_barcode = $objp->product_barcode;
2137 $line->fk_product_type = $objp->fk_product_type;
2138 $line->fk_unit = $objp->fk_unit;
2140 $line->weight = $objp->weight;
2141 $line->weight_units = $objp->weight_units;
2142 $line->volume = $objp->volume;
2143 $line->volume_units = $objp->volume_units;
2145 $line->date_start = $this->db->jdate($objp->date_start);
2146 $line->date_end = $this->db->jdate($objp->date_end);
2149 $line->fk_multicurrency = $objp->fk_multicurrency;
2150 $line->multicurrency_code = $objp->multicurrency_code;
2151 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2152 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2153 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2154 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2156 $line->fetch_optionals();
2159 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2160 $tmpproduct =
new Product($this->db);
2161 $tmpproduct->fetch($objp->fk_product);
2162 $tmpproduct->getMultiLangs();
2164 $line->multilangs = $tmpproduct->multilangs;
2167 $this->lines[$i] = $line;
2172 $this->db->free($result);
2176 $this->error = $this->db->error();
2190 foreach ($this->lines as $line) {
2191 if ($line->product_type == 0) {
2206 foreach ($this->lines as $line) {
2207 if ($line->product_type == 1) {
2223 $sql =
'SELECT COUNT(DISTINCT ed.fk_expedition) as nb';
2224 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2225 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2227 $sql .=
' ed.fk_elementdet = cd.rowid';
2228 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2231 dol_syslog(get_class($this).
"::getNbOfShipments", LOG_DEBUG);
2232 $resql = $this->db->query($sql);
2234 $obj = $this->db->fetch_object($resql);
2239 $this->db->free($resql);
2242 $this->error = $this->db->lasterror();
2257 $this->expeditions = array();
2259 $sql =
'SELECT cd.rowid, cd.fk_product,';
2260 $sql .=
' sum(ed.qty) as qty';
2261 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2262 if ($filtre_statut >= 0) {
2263 $sql .=
' '.MAIN_DB_PREFIX.
'expedition as e,';
2265 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2267 if ($filtre_statut >= 0) {
2268 $sql .=
' ed.fk_expedition = e.rowid AND';
2270 $sql .=
' ed.fk_elementdet = cd.rowid';
2271 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2272 if ($fk_product > 0) {
2273 $sql .=
' AND cd.fk_product = '.((int) $fk_product);
2275 if ($filtre_statut >= 0) {
2276 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
2278 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
2281 dol_syslog(get_class($this).
"::loadExpeditions", LOG_DEBUG);
2282 $resql = $this->db->query($sql);
2284 $num = $this->db->num_rows($resql);
2287 $obj = $this->db->fetch_object($resql);
2288 $this->expeditions[$obj->rowid] = $obj->qty;
2291 $this->db->free($resql);
2294 $this->error = $this->db->lasterror();
2306 $sql =
'SELECT count(*)';
2307 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expedition as e';
2308 $sql .=
', '.MAIN_DB_PREFIX.
'element_element as el';
2309 $sql .=
' WHERE el.fk_source = '.((int) $this->
id);
2310 $sql .=
" AND el.sourcetype = 'commande'";
2311 $sql .=
" AND el.fk_target = e.rowid";
2312 $sql .=
" AND el.targettype = 'shipping'";
2314 $resql = $this->db->query($sql);
2316 $row = $this->db->fetch_row($resql);
2374 if ($this->
statut == self::STATUS_DRAFT) {
2380 $line->context = $this->context;
2383 $line->fetch($lineid);
2385 if ($id > 0 && $line->fk_commande != $id) {
2386 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2391 $staticline = clone $line;
2392 $line->oldline = $staticline;
2394 if ($line->delete($user) > 0) {
2398 $this->db->commit();
2401 $this->db->rollback();
2402 $this->error = $this->db->lasterror();
2406 $this->db->rollback();
2407 $this->error = $line->error;
2411 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
2430 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
2432 return $this->
setDiscount($user, $remise, $notrigger);
2445 $remise = trim((
string) $remise) ? trim((
string) $remise) : 0;
2447 if ($user->hasRight(
'commande',
'creer')) {
2454 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2455 $sql .=
' SET remise_percent = '.((float) $remise);
2456 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.((int) self::STATUS_DRAFT);
2459 $resql = $this->db->query($sql);
2461 $this->errors[] = $this->db->error();
2466 $this->oldcopy = clone $this;
2467 $this->remise_percent = $remise;
2471 if (!$notrigger && empty($error)) {
2481 $this->db->commit();
2484 foreach ($this->errors as $errmsg) {
2485 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2486 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2488 $this->db->rollback();
2576 if ($user->hasRight(
'commande',
'creer')) {
2581 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2582 $sql .=
" SET date_commande = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
2583 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut = ".((int) self::STATUS_DRAFT);
2586 $resql = $this->db->query($sql);
2588 $this->errors[] = $this->db->error();
2593 $this->oldcopy = clone $this;
2594 $this->date = $date;
2597 if (!$notrigger && empty($error)) {
2607 $this->db->commit();
2610 foreach ($this->errors as $errmsg) {
2611 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2612 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2614 $this->db->rollback();
2648 if ($user->hasRight(
'commande',
'creer')) {
2653 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2654 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2655 $sql .=
" WHERE rowid = ".((int) $this->
id);
2658 $resql = $this->db->query($sql);
2660 $this->errors[] = $this->db->error();
2665 $this->oldcopy = clone $this;
2666 $this->delivery_date = $delivery_date;
2669 if (!$notrigger && empty($error)) {
2679 $this->db->commit();
2682 foreach ($this->errors as $errmsg) {
2683 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2684 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2686 $this->db->rollback();
2708 public function liste_array($shortlist = 0, $draft = 0, $excluser =
null, $socid = 0, $limit = 0, $offset = 0, $sortfield =
'c.date_commande', $sortorder =
'DESC')
2715 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2716 $sql .=
" c.rowid as cid, c.ref";
2717 if (!$user->hasRight(
'societe',
'client',
'voir')) {
2718 $sql .=
", sc.fk_soc, sc.fk_user";
2720 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"commande as c";
2721 if (!$user->hasRight(
'societe',
'client',
'voir')) {
2722 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2724 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2725 $sql .=
" AND c.fk_soc = s.rowid";
2726 if (!$user->hasRight(
'societe',
'client',
'voir')) {
2727 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2730 $sql .=
" AND s.rowid = ".((int) $socid);
2733 $sql .=
" AND c.fk_statut = ".self::STATUS_DRAFT;
2735 if (is_object($excluser)) {
2736 $sql .=
" AND c.fk_user_author <> ".((int) $excluser->id);
2738 $sql .= $this->db->order($sortfield, $sortorder);
2739 $sql .= $this->db->plimit($limit, $offset);
2741 $result = $this->db->query($sql);
2743 $numc = $this->db->num_rows($result);
2746 while ($i < $numc) {
2747 $obj = $this->db->fetch_object($result);
2749 if ($shortlist == 1) {
2750 $ga[$obj->cid] = $obj->ref;
2751 } elseif ($shortlist == 2) {
2752 $ga[$obj->cid] = $obj->ref.
' ('.$obj->name.
')';
2754 $ga[$i][
'id'] = $obj->cid;
2755 $ga[$i][
'ref'] = $obj->ref;
2756 $ga[$i][
'name'] = $obj->name;
2779 dol_syslog(
'Commande::availability('.$availability_id.
')');
2780 if ($this->
statut >= self::STATUS_DRAFT) {
2785 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2786 $sql .=
' SET fk_availability = '.((int) $availability_id);
2787 $sql .=
' WHERE rowid='.((int) $this->
id);
2790 $resql = $this->db->query($sql);
2792 $this->errors[] = $this->db->error();
2797 $this->oldcopy = clone $this;
2798 $this->availability_id = $availability_id;
2801 if (!$notrigger && empty($error)) {
2811 $this->db->commit();
2814 foreach ($this->errors as $errmsg) {
2815 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2816 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2818 $this->db->rollback();
2822 $error_str =
'Command status do not meet requirement '.$this->statut;
2824 $this->error = $error_str;
2825 $this->errors[] = $this->error;
2843 dol_syslog(
'Commande::demand_reason('.$demand_reason_id.
')');
2844 if ($this->
statut >= self::STATUS_DRAFT) {
2849 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2850 $sql .=
' SET fk_input_reason = '.((int) $demand_reason_id);
2851 $sql .=
' WHERE rowid='.((int) $this->
id);
2854 $resql = $this->db->query($sql);
2856 $this->errors[] = $this->db->error();
2861 $this->oldcopy = clone $this;
2862 $this->demand_reason_id = $demand_reason_id;
2865 if (!$notrigger && empty($error)) {
2875 $this->db->commit();
2878 foreach ($this->errors as $errmsg) {
2879 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2880 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2882 $this->db->rollback();
2886 $error_str =
'order status do not meet requirement '.$this->statut;
2888 $this->error = $error_str;
2889 $this->errors[] = $this->error;
2906 if ($user->hasRight(
'commande',
'creer')) {
2911 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET';
2912 $sql .=
' ref_client = '.(empty($ref_client) ?
'NULL' :
"'".$this->db->escape($ref_client).
"'");
2913 $sql .=
' WHERE rowid = '.((int) $this->
id);
2915 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2916 $resql = $this->db->query($sql);
2918 $this->errors[] = $this->db->error();
2923 $this->oldcopy = clone $this;
2924 $this->ref_client = $ref_client;
2925 $this->ref_customer = $ref_client;
2928 if (!$notrigger && empty($error)) {
2937 $this->db->commit();
2940 foreach ($this->errors as $errmsg) {
2941 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2942 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2944 $this->db->rollback();
2963 if ($this->billed) {
2969 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 1';
2972 dol_syslog(get_class($this).
"::classifyBilled", LOG_DEBUG);
2973 if ($this->db->query($sql)) {
2975 $this->oldcopy = clone $this;
2979 if (!$notrigger && empty($error)) {
2981 $result = $this->
call_trigger(
'ORDER_CLASSIFY_BILLED', $user);
2989 $this->db->commit();
2992 foreach ($this->errors as $errmsg) {
2993 dol_syslog(get_class($this).
"::classifyBilled ".$errmsg, LOG_ERR);
2994 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2996 $this->db->rollback();
3000 $this->error = $this->db->error();
3001 $this->db->rollback();
3019 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 0';
3022 dol_syslog(get_class($this).
"::classifyUnBilled", LOG_DEBUG);
3023 if ($this->db->query($sql)) {
3025 $this->oldcopy = clone $this;
3029 if (!$notrigger && empty($error)) {
3031 $result = $this->
call_trigger(
'ORDER_CLASSIFY_UNBILLED', $user);
3041 $this->db->commit();
3044 foreach ($this->errors as $errmsg) {
3045 dol_syslog(get_class($this).
"::classifyUnBilled ".$errmsg, LOG_ERR);
3046 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3048 $this->db->rollback();
3052 $this->error = $this->db->error();
3053 $this->db->rollback();
3089 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 = array(), $fk_unit =
null, $pu_ht_devise = 0, $notrigger = 0, $ref_ext =
'', $rang = 0)
3091 global $conf, $mysoc, $langs, $user;
3093 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");
3094 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3101 if (empty($info_bits)) {
3104 if (empty($txtva)) {
3107 if (empty($txlocaltax1)) {
3110 if (empty($txlocaltax2)) {
3116 if (empty($special_code) || $special_code == 3) {
3119 if (empty($ref_ext)) {
3123 if ($date_start && $date_end && $date_start > $date_end) {
3124 $langs->load(
"errors");
3125 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3133 $pu_ht_devise =
price2num($pu_ht_devise);
3134 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
3137 $txlocaltax1 = (float)
price2num($txlocaltax1);
3138 $txlocaltax2 = (float)
price2num($txlocaltax2);
3152 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3153 $vat_src_code = $reg[1];
3154 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3157 $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);
3159 $total_ht = $tabprice[0];
3160 $total_tva = $tabprice[1];
3161 $total_ttc = $tabprice[2];
3162 $total_localtax1 = $tabprice[9];
3163 $total_localtax2 = $tabprice[10];
3164 $pu_ht = $tabprice[3];
3165 $pu_tva = $tabprice[4];
3166 $pu_ttc = $tabprice[5];
3169 $multicurrency_total_ht = $tabprice[16];
3170 $multicurrency_total_tva = $tabprice[17];
3171 $multicurrency_total_ttc = $tabprice[18];
3172 $pu_ht_devise = $tabprice[19];
3176 if ($price_base_type ==
'TTC') {
3177 $subprice = $pu_ttc;
3184 $price = ((float) $pu - $remise);
3189 $line->fetch($rowid);
3190 $line->fetch_optionals();
3192 if (!empty($line->fk_product)) {
3193 $product =
new Product($this->db);
3194 $result = $product->fetch($line->fk_product);
3195 $product_type = $product->type;
3197 if (
getDolGlobalString(
'STOCK_MUST_BE_ENOUGH_FOR_ORDER') && $product_type == 0 && $product->stock_reel < $qty) {
3198 $langs->load(
"errors");
3199 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
3200 $this->errors[] = $this->error;
3202 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
3204 $this->db->rollback();
3209 $staticline = clone $line;
3211 $line->oldline = $staticline;
3212 $this->line = $line;
3213 $this->line->context = $this->context;
3214 $this->line->rang = $rang;
3217 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
3218 $rangmax = $this->
line_max($fk_parent_line);
3219 $this->line->rang = $rangmax + 1;
3222 $this->line->id = $rowid;
3223 $this->line->label = $label;
3224 $this->line->desc = $desc;
3225 $this->line->qty = $qty;
3226 $this->line->ref_ext = $ref_ext;
3228 $this->line->vat_src_code = $vat_src_code;
3229 $this->line->tva_tx = $txtva;
3230 $this->line->localtax1_tx = $txlocaltax1;
3231 $this->line->localtax2_tx = $txlocaltax2;
3232 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3233 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3235 $this->line->subprice = $pu_ht;
3236 $this->line->info_bits = $info_bits;
3237 $this->line->special_code = $special_code;
3238 $this->line->total_ht = $total_ht;
3239 $this->line->total_tva = $total_tva;
3240 $this->line->total_localtax1 = $total_localtax1;
3241 $this->line->total_localtax2 = $total_localtax2;
3242 $this->line->total_ttc = $total_ttc;
3243 $this->line->date_start = $date_start;
3244 $this->line->date_end = $date_end;
3245 $this->line->product_type = $type;
3246 $this->line->fk_parent_line = $fk_parent_line;
3247 $this->line->skip_update_total = $skip_update_total;
3248 $this->line->fk_unit = $fk_unit;
3250 $this->line->fk_fournprice = $fk_fournprice;
3251 $this->line->pa_ht = $pa_ht;
3254 $this->line->multicurrency_subprice = $pu_ht_devise;
3255 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
3256 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
3257 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
3260 $this->line->price = $price;
3262 if (is_array($array_options) && count($array_options) > 0) {
3264 foreach ($array_options as $key => $value) {
3265 $this->line->array_options[$key] = $array_options[$key];
3269 $result = $this->line->update($user, $notrigger);
3272 if (!empty($fk_parent_line)) {
3279 $this->db->commit();
3282 $this->error = $this->line->error;
3284 $this->db->rollback();
3288 $this->error = get_class($this).
"::updateline Order status makes operation forbidden";
3289 $this->errors = array(
'OrderStatusMakeOperationForbidden');
3308 if (isset($this->
ref)) {
3309 $this->
ref = trim($this->
ref);
3311 if (isset($this->ref_client)) {
3312 $this->ref_client = trim($this->ref_client);
3314 if (isset($this->ref_customer)) {
3315 $this->ref_customer = trim($this->ref_customer);
3317 if (isset($this->note) || isset($this->note_private)) {
3318 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
3320 if (isset($this->note_public)) {
3321 $this->note_public = trim($this->note_public);
3323 if (isset($this->model_pdf)) {
3324 $this->model_pdf = trim($this->model_pdf);
3326 if (isset($this->import_key)) {
3327 $this->import_key = trim($this->import_key);
3329 $delivery_date = $this->delivery_date;
3335 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande SET";
3337 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
3338 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
3339 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
3340 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
3341 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
3342 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
3343 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
3344 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
3345 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
3346 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
3347 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
3348 $sql .=
" fk_statut=".(isset($this->
statut) ? $this->
statut :
"null").
",";
3349 $sql .=
" fk_user_modif=".(isset($user->id) ? $user->id :
"null").
",";
3350 $sql .=
" fk_user_valid=".((isset($this->user_validation_id) && $this->user_validation_id > 0) ? $this->user_validation_id :
"null").
",";
3351 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
3352 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
3353 $sql .=
" deposit_percent=".(!empty($this->deposit_percent) ? strval($this->deposit_percent) :
"null").
",";
3354 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
3355 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
3356 $sql .=
" fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id :
"null").
",";
3357 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
3358 $sql .=
" fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id :
"null").
",";
3359 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
3360 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
3361 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
3362 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
3364 $sql .=
" WHERE rowid=".((int) $this->
id);
3368 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3369 $resql = $this->db->query($sql);
3372 $this->errors[] =
"Error ".$this->db->lasterror();
3382 if (!$error && !$notrigger) {
3393 foreach ($this->errors as $errmsg) {
3394 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3395 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3397 $this->db->rollback();
3400 $this->db->commit();
3412 public function delete($user, $notrigger = 0)
3414 global $conf, $langs;
3415 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3419 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
3434 $this->errors[] = $langs->trans(
'SomeShipmentExists');
3439 if (!$error && !empty($this->table_element_line)) {
3440 $tabletodelete = $this->table_element_line;
3441 $sqlef =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
"_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id).
")";
3442 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3443 if (!$this->db->query($sqlef) || !$this->db->query($sql)) {
3445 $this->error = $this->db->lasterror();
3446 $this->errors[] = $this->error;
3447 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3472 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3478 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
3479 $res = $this->db->query($sql);
3482 $this->error = $this->db->lasterror();
3483 $this->errors[] = $this->error;
3484 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3500 if ($conf->commande->multidir_output[$this->entity] && !empty($this->
ref)) {
3501 $dir = $conf->commande->multidir_output[$this->entity].
"/".$ref;
3502 $file = $dir.
"/".$ref.
".pdf";
3503 if (file_exists($file)) {
3507 $this->error =
'ErrorFailToDeleteFile';
3508 $this->errors[] = $this->error;
3509 $this->db->rollback();
3513 if (file_exists($dir)) {
3516 $this->error =
'ErrorFailToDeleteDir';
3517 $this->errors[] = $this->error;
3518 $this->db->rollback();
3526 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3527 $this->db->commit();
3530 $this->db->rollback();
3547 global $conf, $langs;
3551 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.date_livraison as delivery_date, c.fk_statut, c.total_ht";
3552 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as c";
3553 if (!$user->hasRight(
'societe',
'client',
'voir')) {
3554 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
3555 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3558 $sql .= $clause.
" c.entity IN (".
getEntity(
'commande').
")";
3560 if ($mode ==
'toship') {
3562 $sql .=
" AND c.fk_statut IN (" . self::STATUS_VALIDATED .
"," . self::STATUS_SHIPMENTONPROCESS .
")";
3564 if ($mode ==
'tobill') {
3566 $sql .=
" AND c.fk_statut IN (" . self::STATUS_VALIDATED .
"," . self::STATUS_SHIPMENTONPROCESS .
", " . self::STATUS_CLOSED .
") AND c.facture = 0";
3568 if ($mode ==
'shippedtobill') {
3570 $sql .=
" AND c.fk_statut IN (" . self::STATUS_CLOSED .
") AND c.facture = 0";
3573 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3576 $resql = $this->db->query($sql);
3579 $label = $labelShort = $url =
'';
3580 if ($mode ==
'toship') {
3581 $delay_warning = $conf->commande->client->warning_delay / 60 / 60 / 24;
3582 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=-2&mainmenu=commercial&leftmenu=orders';
3583 $label = $langs->transnoentitiesnoconv(
"OrdersToProcess");
3584 $labelShort = $langs->transnoentitiesnoconv(
"Opened");
3586 if ($mode ==
'tobill') {
3587 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=-3&search_billed=0&mainmenu=commercial&leftmenu=orders';
3588 $label = $langs->trans(
"OrdersToBill");
3589 $labelShort = $langs->trans(
"ToBill");
3591 if ($mode ==
'shippedtobill') {
3592 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=3&search_billed=0&mainmenu=commercial&leftmenu=orders';
3593 $label = $langs->trans(
"OrdersToBill");
3594 $labelShort = $langs->trans(
"StatusOrderDelivered").
' '.$langs->trans(
"and").
' '.$langs->trans(
"ToBill");
3598 $response->warning_delay = $delay_warning;
3599 $response->label = $label;
3600 $response->labelShort = $labelShort;
3601 $response->url = $url;
3604 $generic_commande =
new Commande($this->db);
3606 while ($obj = $this->db->fetch_object($resql)) {
3607 $response->nbtodo++;
3608 $response->total += $obj->total_ht;
3610 $generic_commande->statut = $obj->fk_statut;
3611 $generic_commande->date_commande = $this->db->jdate($obj->date_commande);
3612 $generic_commande->date = $this->db->jdate($obj->date_commande);
3613 $generic_commande->delivery_date = $this->db->jdate($obj->delivery_date);
3615 if ($mode ==
'toship' && $generic_commande->hasDelay()) {
3616 $response->nbtodolate++;
3622 $this->error = $this->db->error();
3636 $label = $langs->trans(
'OrderSource'.$this->source);
3638 if ($label ==
'OrderSource') {
3665 public function LibStatut($status, $billed, $mode, $donotshowbilled = 0)
3668 global $langs, $hookmanager;
3671 if (empty($donotshowbilled)) {
3672 $billedtext .= ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3677 if ($status == self::STATUS_CANCELED) {
3678 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderCanceled');
3679 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderCanceledShort');
3680 $statusType =
'status9';
3681 } elseif ($status == self::STATUS_DRAFT) {
3682 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDraft');
3683 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDraftShort');
3684 $statusType =
'status0';
3685 } elseif ($status == self::STATUS_VALIDATED) {
3686 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderValidated').$billedtext;
3687 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderValidatedShort').$billedtext;
3688 $statusType =
'status1';
3689 } elseif ($status == self::STATUS_SHIPMENTONPROCESS) {
3690 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderSent').$billedtext;
3691 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderSentShort').$billedtext;
3692 $labelTooltip = $langs->transnoentitiesnoconv(
"StatusOrderSent");
3693 if (!empty($this->delivery_date)) {
3694 $labelTooltip .=
' - '.$langs->transnoentitiesnoconv(
"DateDeliveryPlanned").dol_print_date($this->delivery_date,
'day').$billedtext;
3696 $statusType =
'status4';
3697 } elseif ($status == self::STATUS_CLOSED) {
3698 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDelivered').$billedtext;
3699 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDeliveredShort').$billedtext;
3700 $statusType =
'status6';
3702 $labelStatus = $langs->transnoentitiesnoconv(
'Unknown');
3703 $labelStatusShort =
'';
3708 $parameters = array(
3709 'status' => $status,
3711 'billed' => $billed,
3712 'donotshowbilled' => $donotshowbilled
3715 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
3718 return $hookmanager->resPrint;
3721 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', array(
'tooltip' => $labelTooltip));
3732 global $conf, $langs, $user;
3734 $langs->load(
'orders');
3736 $nofetch = !empty($params[
'nofetch']);
3739 return [
'optimize' => $langs->trans(
"Order")];
3742 if ($user->hasRight(
'commande',
'lire')) {
3743 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Order").
'</u>';
3744 if (isset($this->
statut)) {
3745 $datas[
'status'] =
' '.$this->getLibStatut(5);
3747 $datas[
'Ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
3749 $langs->load(
'companies');
3750 if (empty($this->thirdparty)) {
3753 $datas[
'customer'] =
'<br><b>'.$langs->trans(
'Customer').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
3755 $datas[
'RefCustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.(empty($this->ref_customer) ? (empty($this->ref_client) ?
'' : $this->ref_client) : $this->ref_customer);
3757 $langs->load(
'project');
3758 if (is_null($this->project) || (is_object($this->project) && $this->project->isEmpty())) {
3760 if ($res > 0 && $this->project instanceof
Project) {
3761 $datas[
'project'] =
'<br><b>'.$langs->trans(
'Project').
':</b> '.$this->project->getNomUrl(1,
'', 0, 1);
3765 if (!empty($this->total_ht)) {
3766 $datas[
'AmountHT'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
3768 if (!empty($this->total_tva)) {
3769 $datas[
'VAT'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
3771 if (!empty($this->total_ttc)) {
3772 $datas[
'AmountTTC'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
3774 if (!empty($this->date)) {
3775 $datas[
'Date'] =
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
3777 if (!empty($this->delivery_date)) {
3778 $datas[
'DeliveryDate'] =
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
3798 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0, $target =
'')
3800 global $conf, $langs, $user, $hookmanager;
3802 if (!empty($conf->dol_no_mouse_hover)) {
3808 if (isModEnabled(
"shipping") && ($option ==
'1' || $option ==
'2')) {
3809 $url = DOL_URL_ROOT.
'/expedition/shipment.php?id='.$this->id;
3811 $url = DOL_URL_ROOT.
'/commande/card.php?id='.$this->id;
3814 if (!$user->hasRight(
'commande',
'lire')) {
3818 if ($option !==
'nolink') {
3820 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3821 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
3822 $add_save_lastsearch_values = 1;
3824 if ($add_save_lastsearch_values) {
3825 $url .=
'&save_lastsearch_values=1';
3834 'objecttype' => $this->element,
3835 'option' => $option,
3838 $classfortooltip =
'classfortooltip';
3841 $classfortooltip =
'classforajaxtooltip';
3842 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
3849 if (empty($notooltip) && $user->hasRight(
'commande',
'lire')) {
3851 $label = $langs->trans(
"Order");
3852 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
3854 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
3855 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
3857 $target_value = array(
'_self',
'_blank',
'_parent',
'_top');
3858 if (in_array($target, $target_value)) {
3859 $linkclose .=
' target="'.dol_escape_htmltag($target).
'"';
3863 $linkstart =
'<a href="'.$url.
'"';
3864 $linkstart .= $linkclose.
'>';
3867 if ($option ===
'nolink') {
3872 $result .= $linkstart;
3874 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
3876 if ($withpicto != 2) {
3877 $result .= $this->ref;
3879 $result .= $linkend;
3881 if ($addlinktonotes) {
3882 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3885 $result .=
' <span class="note inline-block">';
3886 $result .=
'<a href="'.DOL_URL_ROOT.
'/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3891 $result .=
'</span>';
3896 $hookmanager->initHooks(array($this->element .
'dao'));
3897 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
3898 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3900 $result = $hookmanager->resPrint;
3902 $result .= $hookmanager->resPrint;
3916 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem,';
3917 $sql .=
' date_valid as datev,';
3918 $sql .=
' date_cloture as datecloture,';
3919 $sql .=
' fk_user_author, fk_user_valid, fk_user_cloture';
3920 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
3921 $sql .=
' WHERE c.rowid = '.((int) $id);
3922 $result = $this->db->query($sql);
3924 if ($this->db->num_rows($result)) {
3925 $obj = $this->db->fetch_object($result);
3926 $this->
id = $obj->rowid;
3927 if ($obj->fk_user_author) {
3928 $this->user_creation_id = $obj->fk_user_author;
3930 if ($obj->fk_user_valid) {
3931 $this->user_validation_id = $obj->fk_user_valid;
3933 if ($obj->fk_user_cloture) {
3934 $this->user_closing_id = $obj->fk_user_cloture;
3937 $this->date_creation = $this->db->jdate($obj->datec);
3938 $this->date_modification = $this->db->jdate($obj->datem);
3939 $this->date_validation = $this->db->jdate($obj->datev);
3940 $this->date_cloture = $this->db->jdate($obj->datecloture);
3943 $this->db->free($result);
3959 global $conf, $langs;
3961 dol_syslog(get_class($this).
"::initAsSpecimen");
3966 $sql =
"SELECT rowid";
3967 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
3968 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3969 $sql .= $this->db->plimit(100);
3971 $resql = $this->db->query($sql);
3973 $num_prods = $this->db->num_rows($resql);
3975 while ($i < $num_prods) {
3977 $row = $this->db->fetch_row($resql);
3978 $prodids[$i] = $row[0];
3984 $this->
ref =
'SPECIMEN';
3985 $this->specimen = 1;
3986 $this->entity = $conf->entity;
3988 $this->date = time();
3989 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
3990 $this->cond_reglement_code =
'RECEP';
3991 $this->mode_reglement_code =
'CHQ';
3992 $this->availability_code =
'DSP';
3993 $this->demand_reason_code =
'SRC_00';
3995 $this->note_public =
'This is a comment (public)';
3996 $this->note_private =
'This is a comment (private)';
3998 $this->multicurrency_tx = 1;
3999 $this->multicurrency_code = $conf->currency;
4001 $this->
status = $this::STATUS_DRAFT;
4006 while ($xnbp < $nbp) {
4009 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
4011 $line->subprice = 100;
4015 $line->total_ht = 50;
4016 $line->total_ttc = 60;
4017 $line->total_tva = 10;
4018 $line->remise_percent = 50;
4020 $line->total_ht = 100;
4021 $line->total_ttc = 120;
4022 $line->total_tva = 20;
4023 $line->remise_percent = 0;
4025 if ($num_prods > 0) {
4026 $prodid = mt_rand(1, $num_prods);
4027 $line->fk_product = $prodids[$prodid];
4028 $line->product_ref =
'SPECIMEN';
4031 $this->lines[$xnbp] = $line;
4033 $this->total_ht += $line->total_ht;
4034 $this->total_tva += $line->total_tva;
4035 $this->total_ttc += $line->total_ttc;
4053 $this->nb = array();
4056 $sql =
"SELECT count(co.rowid) as nb";
4057 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as co";
4058 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
4059 if (!$user->hasRight(
'societe',
'client',
'voir')) {
4060 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
4061 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
4064 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'commande').
")";
4066 $resql = $this->db->query($sql);
4068 while ($obj = $this->db->fetch_object($resql)) {
4069 $this->nb[
"orders"] = $obj->nb;
4071 $this->db->free($resql);
4075 $this->error = $this->db->error();
4101 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
4103 global $conf, $langs;
4105 $langs->load(
"orders");
4106 $outputlangs->load(
"products");
4109 $modele =
'einstein';
4111 if (!empty($this->model_pdf)) {
4112 $modele = $this->model_pdf;
4118 $modelpath =
"core/modules/commande/doc/";
4120 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
4173 return max($this->date, $this->delivery_date) < ($now - $conf->commande->client->warning_delay);
4183 global $conf, $langs;
4185 if (empty($this->delivery_date)) {
4186 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date,
'day');
4188 $text = $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->delivery_date,
'day');
4190 $text .=
' '.($conf->commande->client->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->client->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
4205 public $element =
'commandedet';
4207 public $table_element =
'commandedet';
4215 public $fk_commande;
4223 public $commande_id;
4225 public $fk_parent_line;
4237 public $fk_remise_except;
4243 public $fk_fournprice;
4263 public $skip_update_total;
4284 $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,';
4285 $sql .=
' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
4286 $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,';
4287 $sql .=
' cd.fk_unit,';
4288 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
4289 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
4290 $sql .=
' cd.date_start, cd.date_end, cd.vat_src_code';
4291 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as cd';
4292 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
4293 $sql .=
' WHERE cd.rowid = '.((int) $rowid);
4294 $result = $this->db->query($sql);
4296 $objp = $this->db->fetch_object($result);
4299 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.$sql;
4303 $this->
rowid = $objp->rowid;
4304 $this->
id = $objp->rowid;
4305 $this->fk_commande = $objp->fk_commande;
4306 $this->fk_parent_line = $objp->fk_parent_line;
4307 $this->label = $objp->custom_label;
4308 $this->desc = $objp->description;
4309 $this->qty = $objp->qty;
4310 $this->
price = $objp->price;
4311 $this->subprice = $objp->subprice;
4312 $this->ref_ext = $objp->ref_ext;
4313 $this->vat_src_code = $objp->vat_src_code;
4314 $this->tva_tx = $objp->tva_tx;
4315 $this->localtax1_tx = $objp->localtax1_tx;
4316 $this->localtax2_tx = $objp->localtax2_tx;
4317 $this->remise = $objp->remise;
4318 $this->remise_percent = $objp->remise_percent;
4319 $this->fk_remise_except = $objp->fk_remise_except;
4320 $this->fk_product = $objp->fk_product;
4321 $this->product_type = $objp->product_type;
4322 $this->info_bits = $objp->info_bits;
4323 $this->special_code = $objp->special_code;
4324 $this->total_ht = $objp->total_ht;
4325 $this->total_tva = $objp->total_tva;
4326 $this->total_localtax1 = $objp->total_localtax1;
4327 $this->total_localtax2 = $objp->total_localtax2;
4328 $this->total_ttc = $objp->total_ttc;
4329 $this->fk_fournprice = $objp->fk_fournprice;
4330 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
4331 $this->pa_ht = $marginInfos[0];
4332 $this->marge_tx = $marginInfos[1];
4333 $this->marque_tx = $marginInfos[2];
4334 $this->special_code = $objp->special_code;
4335 $this->rang = $objp->rang;
4337 $this->
ref = $objp->product_ref;
4339 $this->product_ref = $objp->product_ref;
4340 $this->product_label = $objp->product_label;
4341 $this->product_desc = $objp->product_desc;
4342 $this->product_tobatch = $objp->product_tobatch;
4343 $this->fk_unit = $objp->fk_unit;
4345 $this->date_start = $this->db->jdate($objp->date_start);
4346 $this->date_end = $this->db->jdate($objp->date_end);
4348 $this->fk_multicurrency = $objp->fk_multicurrency;
4349 $this->multicurrency_code = $objp->multicurrency_code;
4350 $this->multicurrency_subprice = $objp->multicurrency_subprice;
4351 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
4352 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
4353 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
4357 $this->db->free($result);
4361 $this->error = $this->db->lasterror();
4373 public function delete(
User $user, $notrigger = 0)
4375 global $conf, $langs;
4379 if (empty($this->
id) && !empty($this->
rowid)) {
4380 $this->
id = $this->rowid;
4384 $sqlCheckShipmentLine =
"SELECT";
4385 $sqlCheckShipmentLine .=
" ed.rowid";
4386 $sqlCheckShipmentLine .=
" FROM " . MAIN_DB_PREFIX .
"expeditiondet ed";
4387 $sqlCheckShipmentLine .=
" WHERE ed.fk_elementdet = " . ((int) $this->
id);
4389 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
4390 if (!$resqlCheckShipmentLine) {
4392 $this->error = $this->db->lasterror();
4393 $this->errors[] = $this->error;
4395 $langs->load(
'errors');
4396 $num = $this->db->num_rows($resqlCheckShipmentLine);
4399 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
4400 $this->error = $langs->trans(
'ErrorRecordAlreadyExists') .
' : ' . $langs->trans(
'ShipmentLine') .
' ' . $objCheckShipmentLine->rowid;
4401 $this->errors[] = $this->error;
4403 $this->db->free($resqlCheckShipmentLine);
4406 dol_syslog(__METHOD__ .
'Error ; ' . $this->error, LOG_ERR);
4414 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
4422 $sql =
'DELETE FROM ' . MAIN_DB_PREFIX .
"commandedet WHERE rowid = " . ((int) $this->
id);
4425 $resql = $this->db->query($sql);
4427 $this->error = $this->db->lasterror();
4437 dol_syslog(get_class($this) .
"::delete error -4 " . $this->error, LOG_ERR);
4442 $this->db->commit();
4446 foreach ($this->errors as $errmsg) {
4447 dol_syslog(get_class($this) .
"::delete " . $errmsg, LOG_ERR);
4448 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
4450 $this->db->rollback();
4461 public function insert($user =
null, $notrigger = 0)
4465 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4467 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
4470 if (empty($this->tva_tx)) {
4473 if (empty($this->localtax1_tx)) {
4474 $this->localtax1_tx = 0;
4476 if (empty($this->localtax2_tx)) {
4477 $this->localtax2_tx = 0;
4479 if (empty($this->localtax1_type)) {
4480 $this->localtax1_type = 0;
4482 if (empty($this->localtax2_type)) {
4483 $this->localtax2_type = 0;
4485 if (empty($this->total_localtax1)) {
4486 $this->total_localtax1 = 0;
4488 if (empty($this->total_localtax2)) {
4489 $this->total_localtax2 = 0;
4491 if (empty($this->rang)) {
4494 if (empty($this->remise_percent)) {
4495 $this->remise_percent = 0;
4497 if (empty($this->info_bits)) {
4498 $this->info_bits = 0;
4500 if (empty($this->special_code)) {
4501 $this->special_code = 0;
4503 if (empty($this->fk_parent_line)) {
4504 $this->fk_parent_line = 0;
4506 if (empty($this->pa_ht)) {
4509 if (empty($this->ref_ext)) {
4510 $this->ref_ext =
'';
4514 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4515 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4519 $this->pa_ht = $result;
4524 if ($this->product_type < 0) {
4531 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'commandedet';
4532 $sql .=
' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
4533 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
4534 $sql .=
' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
4535 $sql .=
' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
4536 $sql .=
' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
4537 $sql .=
' fk_unit,';
4538 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
4540 $sql .=
" VALUES (".$this->fk_commande.
",";
4541 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
4542 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
4543 $sql .=
" '".$this->db->escape($this->desc).
"',";
4544 $sql .=
" '".price2num($this->qty).
"',";
4545 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
4546 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
4547 $sql .=
" '".price2num($this->tva_tx).
"',";
4548 $sql .=
" '".price2num($this->localtax1_tx).
"',";
4549 $sql .=
" '".price2num($this->localtax2_tx).
"',";
4550 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
4551 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
4552 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
4553 $sql .=
" '".$this->db->escape($this->product_type).
"',";
4554 $sql .=
" '".price2num($this->remise_percent).
"',";
4555 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice) :
"null").
",";
4556 $sql .=
" ".($this->price !=
'' ?
"'".price2num($this->
price).
"'" :
"null").
",";
4557 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
4558 $sql .=
' '.((int) $this->special_code).
',';
4559 $sql .=
' '.((int) $this->rang).
',';
4560 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
4561 $sql .=
' '.price2num($this->pa_ht).
',';
4562 $sql .=
" ".((int) $this->info_bits).
",";
4563 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
4564 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
4565 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
4566 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
4567 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
4568 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
4569 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
',';
4570 $sql .=
' '.(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
4571 $sql .=
", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) :
'NULL');
4572 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
4573 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
4574 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
4575 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
4576 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
4579 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
4580 $resql = $this->db->query($sql);
4582 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
4583 $this->
rowid = $this->id;
4592 if (!$error && !$notrigger) {
4594 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
4602 $this->db->commit();
4606 foreach ($this->errors as $errmsg) {
4607 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
4608 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4610 $this->db->rollback();
4613 $this->error = $this->db->error();
4614 $this->db->rollback();
4630 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4633 if (empty($this->tva_tx)) {
4636 if (empty($this->localtax1_tx)) {
4637 $this->localtax1_tx = 0;
4639 if (empty($this->localtax2_tx)) {
4640 $this->localtax2_tx = 0;
4642 if (empty($this->localtax1_type)) {
4643 $this->localtax1_type = 0;
4645 if (empty($this->localtax2_type)) {
4646 $this->localtax2_type = 0;
4648 if (empty($this->qty)) {
4651 if (empty($this->total_localtax1)) {
4652 $this->total_localtax1 = 0;
4654 if (empty($this->total_localtax2)) {
4655 $this->total_localtax2 = 0;
4657 if (empty($this->marque_tx)) {
4658 $this->marque_tx = 0;
4660 if (empty($this->marge_tx)) {
4661 $this->marge_tx = 0;
4663 if (empty($this->remise_percent)) {
4664 $this->remise_percent = 0;
4666 if (empty($this->remise)) {
4669 if (empty($this->info_bits)) {
4670 $this->info_bits = 0;
4672 if (empty($this->special_code)) {
4673 $this->special_code = 0;
4675 if (empty($this->product_type)) {
4676 $this->product_type = 0;
4678 if (empty($this->fk_parent_line)) {
4679 $this->fk_parent_line = 0;
4681 if (empty($this->pa_ht)) {
4684 if (empty($this->ref_ext)) {
4685 $this->ref_ext =
'';
4689 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4690 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4694 $this->pa_ht = $result;
4701 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4702 $sql .=
" description='".$this->db->escape($this->desc).
"'";
4703 $sql .=
" , label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
4704 $sql .=
" , vat_src_code=".(!empty($this->vat_src_code) ?
"'".$this->db->escape($this->vat_src_code).
"'" :
"''");
4705 $sql .=
" , tva_tx=".price2num($this->tva_tx);
4706 $sql .=
" , localtax1_tx=".price2num($this->localtax1_tx);
4707 $sql .=
" , localtax2_tx=".price2num($this->localtax2_tx);
4708 $sql .=
" , localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
4709 $sql .=
" , localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
4710 $sql .=
" , qty=".price2num($this->qty);
4711 $sql .=
" , ref_ext='".$this->db->escape($this->ref_ext).
"'";
4712 $sql .=
" , subprice=".price2num($this->subprice);
4713 $sql .=
" , remise_percent=".price2num($this->remise_percent);
4714 $sql .=
" , price=".price2num($this->
price);
4715 $sql .=
" , remise=".price2num($this->remise);
4716 if (empty($this->skip_update_total)) {
4717 $sql .=
" , total_ht=".price2num($this->total_ht);
4718 $sql .=
" , total_tva=".price2num($this->total_tva);
4719 $sql .=
" , total_ttc=".price2num($this->total_ttc);
4720 $sql .=
" , total_localtax1=".price2num($this->total_localtax1);
4721 $sql .=
" , total_localtax2=".price2num($this->total_localtax2);
4723 $sql .=
" , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null");
4724 $sql .=
" , buy_price_ht='".price2num($this->pa_ht).
"'";
4725 $sql .=
" , info_bits=".((int) $this->info_bits);
4726 $sql .=
" , special_code=".((int) $this->special_code);
4727 $sql .=
" , date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
4728 $sql .=
" , date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
4729 $sql .=
" , product_type=".$this->product_type;
4730 $sql .=
" , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line :
"null");
4731 if (!empty($this->rang)) {
4732 $sql .=
", rang=".((int) $this->rang);
4734 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
4737 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice);
4738 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
4739 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
4740 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
4742 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4744 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
4745 $resql = $this->db->query($sql);
4748 $this->
id = $this->rowid;
4755 if (!$error && !$notrigger) {
4757 $result = $this->
call_trigger(
'LINEORDER_MODIFY', $user);
4765 $this->db->commit();
4769 foreach ($this->errors as $errmsg) {
4770 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
4771 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4773 $this->db->rollback();
4776 $this->error = $this->db->error();
4777 $this->db->rollback();
4795 if (empty($this->total_localtax1)) {
4796 $this->total_localtax1 = 0;
4798 if (empty($this->total_localtax2)) {
4799 $this->total_localtax2 = 0;
4803 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4804 $sql .=
" total_ht='".price2num($this->total_ht).
"'";
4805 $sql .=
",total_tva='".price2num($this->total_tva).
"'";
4806 $sql .=
",total_localtax1='".price2num($this->total_localtax1).
"'";
4807 $sql .=
",total_localtax2='".price2num($this->total_localtax2).
"'";
4808 $sql .=
",total_ttc='".price2num($this->total_ttc).
"'";
4809 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4811 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
4813 $resql = $this->db->query($sql);
4815 $this->db->commit();
4818 $this->error = $this->db->error();
4819 $this->db->rollback();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Class to manage customers orders.
getNbOfServicesLines()
Return number of line with type service.
getNbOfShipments()
Count number of shipments for this order.
createFromProposal($object, User $user)
Load an object from a proposal and create a new order into database.
setDraft($user, $idwarehouse=-1)
Set draft status.
setDeliveryDate($user, $delivery_date, $notrigger=0)
Set the planned delivery date.
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
getLinesArray()
Create an array of order lines.
showDelay()
Show the customer delayed info.
set_date($user, $date, $notrigger=0)
Set a fixed amount discount.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
loadStateBoard()
Load the indicators this->nb for the state board.
fetch_lines($only_product=0, $loadalsotranslation=0)
Load array lines.
$pos_source
key of pos source ('0', '1', ...)
createFromClone(User $user, $socid=0)
Load an object from its id and create a new one in database.
const STATUS_SHIPMENTONPROCESS
Shipment on process.
LibStatut($status, $billed, $mode, $donotshowbilled=0)
Return label of status.
getLibStatut($mode)
Return status label of Order.
hasDelay()
Is the sales order delayed?
const STATUS_CLOSED
Closed (Sent, billed or not)
valid($user, $idwarehouse=0, $notrigger=0)
Validate order.
getLabelSource()
Return source label of order.
set_remise($user, $remise, $notrigger=0)
Applique une remise relative.
const STATUS_CANCELED
Canceled status.
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0, $target='')
Return clicable link of object (with eventually picto)
loadExpeditions($filtre_statut=-1, $fk_product=0)
Load array this->expeditions of lines of shipments with nb of products sent for each order line Note:...
__construct($db)
Constructor.
availability($availability_id, $notrigger=0)
Update delivery delay.
set_reopen($user)
Tag the order as validated (opened) Function used when order is reopend after being closed.
set_ref_client($user, $ref_client, $notrigger=0)
Set customer ref.
getNextNumRef($soc)
Returns the reference to the following non used Order depending on the active numbering module define...
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=array(), $fk_unit=null, $origin='', $origin_id=0, $pu_ht_devise=0, $ref_ext='', $noupdateafterinsertline=0)
Add an order line into database (linked to product/service or not)
getTooltipContentArray($params)
getTooltipContentArray
create($user, $notrigger=0)
Create order Note that this->ref can be set or empty.
demand_reason($demand_reason_id, $notrigger=0)
Update order demand_reason.
const STATUS_DRAFT
Draft status.
const STOCK_NOT_ENOUGH_FOR_ORDER
ERR Not enough stock.
initAsSpecimen()
Initialise an instance with random values.
insert_discount($idremise)
Add line of fixed discount in the order in DB.
getNbOfProductsLines()
Return number of line with type product.
update(User $user, $notrigger=0)
Update database.
$module_source
key of module source when order generated from a dedicated module ('cashdesk', 'takepos',...
classifyUnBilled(User $user, $notrigger=0)
Classify the order as not invoiced.
setDiscount($user, $remise, $notrigger=0)
Set a percentage discount.
cloture($user, $notrigger=0)
Close order.
cancel($idwarehouse=-1)
Cancel an order If stock is decremented on order validation, we must reincrement it.
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=array(), $fk_unit=null, $pu_ht_devise=0, $notrigger=0, $ref_ext='', $rang=0)
Update a line in database.
classifyBilled(User $user, $notrigger=0)
Classify the order as invoiced.
info($id)
Charge les information d'ordre info dans l'objet commande.
load_board($user, $mode)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
const STATUS_VALIDATED
Validated status.
deleteLine($user=null, $lineid=0, $id=0)
Return a array with the pending stock by product.
countNbOfShipments()
Returns an array with expeditions lines number.
liste_array($shortlist=0, $draft=0, $excluser=null, $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($id, $ref='', $ref_ext='', $notused='')
Get object from database.
set_date_livraison($user, $delivery_date, $notrigger=0)
Set delivery date.
add_product($idproduct, $qty, $remise_percent=0.0, $date_start='', $date_end='')
Add line into array $this->client must be loaded.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
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...
line_order($renum=false, $rowidorder='ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
deleteEcmFiles($mode=0)
Delete related files of object in database.
update_price($exclspec=0, $roundingadjust='auto', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0)
Get buy price to use for margin calculation.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
setErrorsFromObject($object)
setErrorsFromObject
static isExistingObject($element, $id, $ref='', $ref_ext='')
Check if an object id or ref exists If you don't need or want to instantiate the object and just need...
updateRangOfLine($rowid, $rang)
Update position of line (rang)
fetch_project()
Load the project with id $this->fk_project into this->project.
deleteExtraFields()
Delete all extra fields values for the current object.
copy_linked_contact($objFrom, $source='internal')
Copy contact from one element to current.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
call_trigger($triggerName, $user)
Call trigger based on this instance.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Superclass for orders classes.
Superclass for orders classes.
Class to manage absolute discounts.
Class to manage Dolibarr database access.
Class to manage shipments.
Class to manage stock movements.
static getIdFromCode($dbs, $code)
Get id of currency from code.
static getIdAndTxFromCode($dbs, $code, $date_document='')
Get id and rate of currency from code.
Class to manage order lines.
insert($user=null, $notrigger=0)
Insert line into database.
update(User $user, $notrigger=0)
Update the line object into db.
update_total()
Update DB line fields total_xxx Used by migration.
__construct($db)
Constructor.
fetch($rowid)
Load line order.
Class to manage products or services.
Class to manage projects.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=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, $nolog=0)
Remove a file or several files with a mask.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
dol_delete_preview($object)
Delete all preview files linked to object instance.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
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.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that returns whether VAT must be recoverable collected VAT (e.g.: VAT NPR in France)
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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) VAT...
get_localtax($vatrate, $local, $thirdparty_buyer=null, $thirdparty_seller=null, $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.
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.
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e e e e e statut
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e rowid