39include_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
40require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
41require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
42require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
43require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
44require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
55 public $element =
'commande';
60 public $table_element =
'commande';
65 public $table_element_line =
'commandedet';
70 public $class_element_line =
'OrderLine';
75 public $fk_element =
'fk_commande';
80 public $picto =
'order';
86 public $restrictiononfksoc = 1;
106 public $ref_customer;
127 public $date_lim_reglement;
131 public $cond_reglement_code;
136 public $cond_reglement_doc;
143 public $deposit_percent;
153 public $mode_reglement;
158 public $mode_reglement_id;
163 public $mode_reglement_code;
169 public $availability_id;
175 public $availability_code;
181 public $availability;
186 public $demand_reason_id;
191 public $demand_reason_code;
203 public $date_commande;
208 public $delivery_date;
213 public $fk_remise_except;
225 public $warehouse_id;
227 public $extraparams = array();
229 public $linked_objects = array();
234 public $user_author_id;
244 public $lines = array();
260 public $online_payment_url;
292 public $fields = array(
293 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 10),
294 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'default' =>
'1',
'enabled' => 1,
'visible' => -2,
'notnull' => 1,
'position' => 20,
'index' => 1),
295 'ref' => array(
'type' =>
'varchar(30)',
'label' =>
'Ref',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'showoncombobox' => 1,
'position' => 25),
296 'ref_ext' => array(
'type' =>
'varchar(255)',
'label' =>
'RefExt',
'enabled' => 1,
'visible' => 0,
'position' => 26),
297 'ref_client' => array(
'type' =>
'varchar(255)',
'label' =>
'RefCustomer',
'enabled' => 1,
'visible' => -1,
'position' => 28),
298 'fk_soc' => array(
'type' =>
'integer:Societe:societe/class/societe.class.php',
'label' =>
'ThirdParty',
'enabled' =>
'isModEnabled("societe")',
'visible' => -1,
'notnull' => 1,
'position' => 20),
299 'fk_projet' => array(
'type' =>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label' =>
'Project',
'enabled' =>
"isModEnabled('project')",
'visible' => -1,
'position' => 25),
300 'date_commande' => array(
'type' =>
'date',
'label' =>
'Date',
'enabled' => 1,
'visible' => 1,
'position' => 60,
'csslist' =>
'nowraponall'),
301 'date_valid' => array(
'type' =>
'datetime',
'label' =>
'DateValidation',
'enabled' => 1,
'visible' => -1,
'position' => 62,
'csslist' =>
'nowraponall'),
302 'date_cloture' => array(
'type' =>
'datetime',
'label' =>
'DateClosing',
'enabled' => 1,
'visible' => -1,
'position' => 65,
'csslist' =>
'nowraponall'),
303 'fk_user_valid' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserValidation',
'enabled' => 1,
'visible' => -1,
'position' => 85),
304 'fk_user_cloture' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserClosing',
'enabled' => 1,
'visible' => -1,
'position' => 90),
305 'source' => array(
'type' =>
'smallint(6)',
'label' =>
'Source',
'enabled' => 1,
'visible' => -1,
'position' => 95),
306 'total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'VAT',
'enabled' => 1,
'visible' => -1,
'position' => 125,
'isameasure' => 1),
307 'localtax1' => array(
'type' =>
'double(24,8)',
'label' =>
'LocalTax1',
'enabled' => 1,
'visible' => -1,
'position' => 130,
'isameasure' => 1),
308 'localtax2' => array(
'type' =>
'double(24,8)',
'label' =>
'LocalTax2',
'enabled' => 1,
'visible' => -1,
'position' => 135,
'isameasure' => 1),
309 'total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalHT',
'enabled' => 1,
'visible' => -1,
'position' => 140,
'isameasure' => 1),
310 'total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalTTC',
'enabled' => 1,
'visible' => -1,
'position' => 145,
'isameasure' => 1),
311 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => 0,
'position' => 150),
312 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => 0,
'position' => 155),
313 'model_pdf' => array(
'type' =>
'varchar(255)',
'label' =>
'PDFTemplate',
'enabled' => 1,
'visible' => 0,
'position' => 160),
314 'fk_account' => array(
'type' =>
'integer',
'label' =>
'BankAccount',
'enabled' =>
'isModEnabled("bank")',
'visible' => -1,
'position' => 170),
315 'fk_currency' => array(
'type' =>
'varchar(3)',
'label' =>
'MulticurrencyID',
'enabled' => 1,
'visible' => -1,
'position' => 175),
316 'fk_cond_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentTerm',
'enabled' => 1,
'visible' => -1,
'position' => 180),
317 'deposit_percent' => array(
'type' =>
'varchar(63)',
'label' =>
'DepositPercent',
'enabled' => 1,
'visible' => -1,
'position' => 181),
318 'fk_mode_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentMode',
'enabled' => 1,
'visible' => -1,
'position' => 185),
319 'date_livraison' => array(
'type' =>
'date',
'label' =>
'DateDeliveryPlanned',
'enabled' => 1,
'visible' => -1,
'position' => 190,
'csslist' =>
'nowraponall'),
320 'fk_shipping_method' => array(
'type' =>
'integer',
'label' =>
'ShippingMethod',
'enabled' => 1,
'visible' => -1,
'position' => 195),
321 'fk_warehouse' => array(
'type' =>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label' =>
'Fk warehouse',
'enabled' =>
'isModEnabled("stock")',
'visible' => -1,
'position' => 200),
322 'fk_availability' => array(
'type' =>
'integer',
'label' =>
'Availability',
'enabled' => 1,
'visible' => -1,
'position' => 205),
323 'fk_input_reason' => array(
'type' =>
'integer',
'label' =>
'InputReason',
'enabled' => 1,
'visible' => -1,
'position' => 210),
325 'extraparams' => array(
'type' =>
'varchar(255)',
'label' =>
'Extraparams',
'enabled' => 1,
'visible' => -1,
'position' => 225),
326 'fk_incoterms' => array(
'type' =>
'integer',
'label' =>
'IncotermCode',
'enabled' =>
'$conf->incoterm->enabled',
'visible' => -1,
'position' => 230),
327 'location_incoterms' => array(
'type' =>
'varchar(255)',
'label' =>
'IncotermLabel',
'enabled' =>
'$conf->incoterm->enabled',
'visible' => -1,
'position' => 235),
328 'fk_multicurrency' => array(
'type' =>
'integer',
'label' =>
'Fk multicurrency',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 240),
329 'multicurrency_code' => array(
'type' =>
'varchar(255)',
'label' =>
'MulticurrencyCurrency',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 245),
330 'multicurrency_tx' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyRate',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 250,
'isameasure' => 1),
331 'multicurrency_total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountHT',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 255,
'isameasure' => 1),
332 'multicurrency_total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountVAT',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 260,
'isameasure' => 1),
333 'multicurrency_total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountTTC',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 265,
'isameasure' => 1),
334 'last_main_doc' => array(
'type' =>
'varchar(255)',
'label' =>
'LastMainDoc',
'enabled' => 1,
'visible' => -1,
'position' => 270),
335 'module_source' => array(
'type' =>
'varchar(32)',
'label' =>
'POSModule',
'enabled' => 1,
'visible' => -1,
'position' => 275),
336 'pos_source' => array(
'type' =>
'varchar(32)',
'label' =>
'POSTerminal',
'enabled' => 1,
'visible' => -1,
'position' => 280),
337 'fk_user_author' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserAuthor',
'enabled' => 1,
'visible' => -1,
'position' => 300),
338 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' => 1,
'visible' => -2,
'notnull' => -1,
'position' => 302),
339 'date_creation' => array(
'type' =>
'datetime',
'label' =>
'DateCreation',
'enabled' => 1,
'visible' => -2,
'position' => 304,
'csslist' =>
'nowraponall'),
340 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 306),
341 'import_key' => array(
'type' =>
'varchar(14)',
'label' =>
'ImportId',
'enabled' => 1,
'visible' => -2,
'position' => 400),
342 'fk_statut' => array(
'type' =>
'smallint(6)',
'label' =>
'Status',
'enabled' => 1,
'visible' => -1,
'position' => 500),
367 const STATUS_ACCEPTED = 2;
384 $this->ismultientitymanaged = 1;
385 $this->isextrafieldmanaged = 1;
397 global $langs, $conf;
398 $langs->load(
"order");
407 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
408 foreach ($dirmodels as $reldir) {
412 $mybool = ((bool) @include_once $dir.$file) || $mybool;
415 if ($mybool ===
false) {
420 $obj =
new $classname();
421 $numref = $obj->getNextValue($soc, $this);
426 $this->error = $obj->error;
431 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_COMMANDE_ADDON_NotDefined");
445 public function valid($user, $idwarehouse = 0, $notrigger = 0)
447 global $conf, $langs;
449 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
454 if ($this->
statut == self::STATUS_VALIDATED) {
455 dol_syslog(get_class($this).
"::valid action abandoned: already validated", LOG_WARNING);
459 if (!((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
460 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'validate')))) {
461 $this->error =
'NotEnoughPermissions';
462 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
472 $soc->fetch($this->socid);
475 $result = $soc->setAsCustomer();
478 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
486 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
487 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
488 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
489 $sql .=
" date_valid='".$this->db->idate($now).
"',";
490 $sql .=
" fk_user_valid = ".($user->id > 0 ? (int) $user->id :
"null").
",";
491 $sql .=
" fk_user_modif = ".((int) $user->id);
492 $sql .=
" WHERE rowid = ".((int) $this->
id);
494 dol_syslog(get_class($this).
"::valid", LOG_DEBUG);
495 $resql = $this->db->query($sql);
498 $this->error = $this->db->lasterror();
504 if ($result >= 0 && isModEnabled(
'stock') &&
getDolGlobalInt(
'STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) {
505 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
506 $langs->load(
"agenda");
509 $cpt = count($this->lines);
510 for ($i = 0; $i < $cpt; $i++) {
511 if ($this->lines[$i]->fk_product > 0) {
513 $mouvP->origin = &$this;
514 $mouvP->setOrigin($this->element, $this->
id);
516 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"OrderValidatedInDolibarr", $num));
519 $this->error = $mouvP->error;
529 if (!$error && !$notrigger) {
539 $this->oldref = $this->ref;
542 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
544 $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).
"'";
545 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'commande/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
546 $resql = $this->db->query($sql);
549 $this->error = $this->db->lasterror();
551 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filepath = 'commande/".$this->db->escape($this->newref).
"'";
552 $sql .=
" WHERE filepath = 'commande/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
553 $resql = $this->db->query($sql);
556 $this->error = $this->db->lasterror();
562 $dirsource = $conf->commande->multidir_output[$this->entity].
'/'.$oldref;
563 $dirdest = $conf->commande->multidir_output[$this->entity].
'/'.$newref;
564 if (!$error && file_exists($dirsource)) {
565 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
567 if (@rename($dirsource, $dirdest)) {
570 $listoffiles =
dol_dir_list($conf->commande->multidir_output[$this->entity].
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
571 foreach ($listoffiles as $fileentry) {
572 $dirsource = $fileentry[
'name'];
573 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
574 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
575 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
576 @rename($dirsource, $dirdest);
594 $this->db->rollback();
610 global $conf, $langs;
615 if ($this->
statut <= self::STATUS_DRAFT) {
619 if (!((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
620 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'validate')))) {
621 $this->error =
'Permission denied';
629 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
630 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT.
",";
631 $sql .=
" fk_user_modif = ".((int) $user->id);
632 $sql .=
" WHERE rowid = ".((int) $this->
id);
634 if ($this->db->query($sql)) {
636 $this->oldcopy = clone $this;
640 if (isModEnabled(
'stock') &&
getDolGlobalInt(
'STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) {
643 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
644 $langs->load(
"agenda");
646 $num = count($this->lines);
647 for ($i = 0; $i < $num; $i++) {
648 if ($this->lines[$i]->fk_product > 0) {
650 $mouvP->origin = &$this;
651 $mouvP->setOrigin($this->element, $this->
id);
653 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderBackToDraftInDolibarr", $this->ref));
656 $this->error = $mouvP->error;
665 $result = $this->
call_trigger(
'ORDER_UNVALIDATE', $user);
676 $this->db->rollback();
680 $this->error = $this->db->error();
681 $this->db->rollback();
700 if ($this->
statut != self::STATUS_CANCELED && $this->
statut != self::STATUS_CLOSED) {
701 dol_syslog(get_class($this).
"::set_reopen order has not status closed", LOG_WARNING);
707 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
708 $sql .=
' SET fk_statut='.self::STATUS_VALIDATED.
', facture=0,';
709 $sql .=
" fk_user_modif = ".((int) $user->id);
710 $sql .=
" WHERE rowid = ".((int) $this->
id);
712 dol_syslog(get_class($this).
"::set_reopen", LOG_DEBUG);
713 $resql = $this->db->query($sql);
723 $this->error = $this->db->lasterror();
734 foreach ($this->errors as $errmsg) {
735 dol_syslog(get_class($this).
"::set_reopen ".$errmsg, LOG_ERR);
736 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
738 $this->db->rollback();
750 public function cloture($user, $notrigger = 0)
756 $usercanclose = ((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
757 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'close')));
760 if ($this->
statut == self::STATUS_CLOSED) {
767 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
768 $sql .=
' SET fk_statut = '.self::STATUS_CLOSED.
',';
769 $sql .=
' fk_user_cloture = '.((int) $user->id).
',';
770 $sql .=
" date_cloture = '".$this->db->idate($now).
"',";
771 $sql .=
" fk_user_modif = ".((int) $user->id);
774 if ($this->db->query($sql)) {
790 $this->db->rollback();
794 $this->error = $this->db->lasterror();
796 $this->db->rollback();
810 public function cancel($idwarehouse = -1)
812 global $conf, $user, $langs;
818 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
819 $sql .=
" SET fk_statut = ".self::STATUS_CANCELED.
",";
820 $sql .=
" fk_user_modif = ".((int) $user->id);
821 $sql .=
" WHERE rowid = ".((int) $this->
id);
822 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
824 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
825 if ($this->db->query($sql)) {
827 if (isModEnabled(
'stock') &&
getDolGlobalInt(
'STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) {
828 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
829 $langs->load(
"agenda");
831 $num = count($this->lines);
832 for ($i = 0; $i < $num; $i++) {
833 if ($this->lines[$i]->fk_product > 0) {
835 $mouvP->setOrigin($this->element, $this->
id);
837 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderCanceledInDolibarr", $this->ref));
840 $this->error = $mouvP->error;
861 foreach ($this->errors as $errmsg) {
862 dol_syslog(get_class($this).
"::cancel ".$errmsg, LOG_ERR);
863 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
865 $this->db->rollback();
869 $this->error = $this->db->error();
870 $this->db->rollback();
883 public function create($user, $notrigger = 0)
885 global $conf, $langs, $mysoc;
891 $date = ($this->date_commande ? $this->date_commande : $this->date);
892 $delivery_date = $this->delivery_date;
895 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
900 if (empty($this->fk_multicurrency)) {
901 $this->multicurrency_code = $conf->currency;
902 $this->fk_multicurrency = 0;
903 $this->multicurrency_tx = 1;
906 dol_syslog(get_class($this).
"::create user=".$user->id);
909 if (!empty($this->
ref)) {
912 $this->error =
'ErrorRefAlreadyExists';
913 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
914 $this->db->rollback();
920 $result = $soc->fetch($this->socid);
922 $this->error =
"Failed to fetch company";
923 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
927 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Source"));
928 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
936 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande (";
937 $sql .=
" ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client";
938 $sql .=
", model_pdf, fk_cond_reglement, deposit_percent, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address";
939 $sql .=
", fk_shipping_method";
940 $sql .=
", fk_warehouse";
941 $sql .=
", fk_incoterms, location_incoterms";
942 $sql .=
", entity, module_source, pos_source";
943 $sql .=
", fk_multicurrency";
944 $sql .=
", multicurrency_code";
945 $sql .=
", multicurrency_tx";
947 $sql .=
" VALUES ('(PROV)', ".((int) $this->socid).
", '".$this->db->idate($now).
"', ".((int) $user->id);
948 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
949 $sql .=
", '".$this->db->idate($date).
"'";
950 $sql .=
", ".($this->source >= 0 && $this->source !=
'' ? $this->db->escape($this->source) :
'null');
951 $sql .=
", '".$this->db->escape($this->note_private).
"'";
952 $sql .=
", '".$this->db->escape($this->note_public).
"'";
953 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
954 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
955 $sql .=
", '".$this->db->escape($this->model_pdf).
"'";
956 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
957 $sql .=
", ".(!empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
"null");
958 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
959 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
960 $sql .=
", ".($this->availability_id > 0 ? ((int) $this->availability_id) :
"null");
961 $sql .=
", ".($this->demand_reason_id > 0 ? ((int) $this->demand_reason_id) :
"null");
962 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
963 $sql .=
", ".($this->fk_delivery_address > 0 ? ((int) $this->fk_delivery_address) :
'NULL');
964 $sql .=
", ".(!empty($this->shipping_method_id) && $this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) :
'NULL');
965 $sql .=
", ".(!empty($this->warehouse_id) && $this->warehouse_id > 0 ? ((int) $this->warehouse_id) :
'NULL');
966 $sql .=
", ".(int) $this->fk_incoterms;
967 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
968 $sql .=
", ".setEntity($this);
969 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
970 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
971 $sql .=
", ".(int) $this->fk_multicurrency;
972 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
973 $sql .=
", ".(float) $this->multicurrency_tx;
976 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
977 $resql = $this->db->query($sql);
979 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commande');
983 $num = count($this->lines);
988 for ($i = 0; $i < $num; $i++) {
989 $line = $this->lines[$i];
993 if (!is_object($line)) {
994 $line = (object) $line;
998 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1003 $vatrate = $line->tva_tx;
1004 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', (
string) $vatrate)) {
1005 $vatrate .=
' ('.$line->vat_src_code.
')';
1009 $originid = $line->origin_id;
1010 $origintype = $line->origin;
1012 $originid = $line->id;
1013 $origintype = $this->element;
1017 if (empty($line->ref_ext)) {
1018 $line->ref_ext =
'';
1026 $line->localtax1_tx,
1027 $line->localtax2_tx,
1029 $line->remise_percent,
1031 $line->fk_remise_except,
1036 $line->product_type,
1038 $line->special_code,
1040 $line->fk_fournprice,
1043 $line->array_options,
1052 if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) {
1053 $this->error = $this->db->lasterror();
1054 $this->errors[] = $this->error;
1057 $this->db->rollback();
1061 if ($result > 0 && $line->product_type == 9) {
1062 $fk_parent_line = $result;
1069 $initialref =
'(PROV'.$this->id.
')';
1070 if (!empty($this->
ref)) {
1071 $initialref = $this->ref;
1074 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"commande SET ref='".$this->db->escape($initialref).
"' WHERE rowid=".((int) $this->
id);
1075 if ($this->db->query($sql)) {
1076 $this->
ref = $initialref;
1078 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1079 $this->linked_objects = $this->linkedObjectsIds;
1083 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1084 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1085 if (is_array($tmp_origin_id)) {
1086 foreach ($tmp_origin_id as $origin_id) {
1089 $this->error = $this->db->lasterror();
1094 $origin_id = $tmp_origin_id;
1097 $this->error = $this->db->lasterror();
1104 if (!$error && $this->
id &&
getDolGlobalString(
'MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) {
1105 $originforcontact = $this->origin;
1106 $originidforcontact = $this->origin_id;
1107 if ($originforcontact ==
'shipping') {
1108 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1110 $exp->fetch($this->origin_id);
1111 $exp->fetchObjectLinked();
1112 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1113 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1114 $originforcontact =
'commande';
1115 if (is_object($value)) {
1116 $originidforcontact = $value->id;
1118 $originidforcontact = $value;
1125 $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";
1126 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact).
"'";
1128 $resqlcontact = $this->db->query($sqlcontact);
1129 if ($resqlcontact) {
1130 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1132 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1146 if (!$error && !$notrigger) {
1156 $this->db->commit();
1159 $this->db->rollback();
1163 $this->error = $this->db->lasterror();
1164 $this->db->rollback();
1171 $this->error = $this->db->lasterror();
1172 $this->db->rollback();
1187 global $conf, $user, $hookmanager;
1194 foreach ($this->lines as $line) {
1195 $line->fetch_optionals();
1199 $objFrom = clone $this;
1202 if (!empty($socid) && $socid != $this->socid) {
1203 $objsoc =
new Societe($this->db);
1205 if ($objsoc->fetch($socid) > 0) {
1206 $this->socid = $objsoc->id;
1207 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1208 $this->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent : 0);
1209 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1210 $this->fk_project = 0;
1211 $this->fk_delivery_address = 0;
1222 $this->user_author_id = $user->id;
1223 $this->user_validation_id = 0;
1225 $this->date_commande =
dol_now();
1226 $this->date_creation =
'';
1227 $this->date_validation =
'';
1229 $this->ref_client =
'';
1230 $this->ref_customer =
'';
1234 $num = count($this->lines);
1235 for ($i = 0; $i < $num; $i++) {
1236 $this->lines[$i]->ref_ext =
'';
1240 $this->context[
'createfromclone'] =
'createfromclone';
1241 $result = $this->
create($user);
1255 if ($this->socid == $objFrom->socid) {
1264 if (is_object($hookmanager)) {
1265 $parameters = array(
'objFrom' => $objFrom);
1267 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1275 unset($this->context[
'createfromclone']);
1279 $this->db->commit();
1282 $this->db->rollback();
1297 global $conf, $hookmanager;
1299 require_once DOL_DOCUMENT_ROOT .
'/multicurrency/class/multicurrency.class.php';
1300 require_once DOL_DOCUMENT_ROOT .
'/core/class/extrafields.class.php';
1304 $this->date_commande =
dol_now();
1309 for ($i = 0; $i < $num; $i++) {
1312 $line->libelle =
$object->lines[$i]->libelle;
1313 $line->label =
$object->lines[$i]->label;
1314 $line->desc =
$object->lines[$i]->desc;
1315 $line->price =
$object->lines[$i]->price;
1316 $line->subprice =
$object->lines[$i]->subprice;
1317 $line->vat_src_code =
$object->lines[$i]->vat_src_code;
1318 $line->tva_tx =
$object->lines[$i]->tva_tx;
1319 $line->localtax1_tx =
$object->lines[$i]->localtax1_tx;
1320 $line->localtax2_tx =
$object->lines[$i]->localtax2_tx;
1321 $line->qty =
$object->lines[$i]->qty;
1322 $line->fk_remise_except =
$object->lines[$i]->fk_remise_except;
1323 $line->remise_percent =
$object->lines[$i]->remise_percent;
1324 $line->fk_product =
$object->lines[$i]->fk_product;
1325 $line->info_bits =
$object->lines[$i]->info_bits;
1326 $line->product_type =
$object->lines[$i]->product_type;
1327 $line->rang =
$object->lines[$i]->rang;
1328 $line->special_code =
$object->lines[$i]->special_code;
1329 $line->fk_parent_line =
$object->lines[$i]->fk_parent_line;
1330 $line->fk_unit =
$object->lines[$i]->fk_unit;
1332 $line->date_start =
$object->lines[$i]->date_start;
1333 $line->date_end =
$object->lines[$i]->date_end;
1335 $line->fk_fournprice =
$object->lines[$i]->fk_fournprice;
1337 $line->pa_ht = $marginInfos[0];
1338 $line->marge_tx = $marginInfos[1];
1339 $line->marque_tx = $marginInfos[2];
1341 $line->origin =
$object->element;
1342 $line->origin_id =
$object->lines[$i]->id;
1345 $object->lines[$i]->fetch_optionals();
1346 foreach (
$object->lines[$i]->array_options as $options_key => $value) {
1347 $line->array_options[$options_key] = $value;
1350 $this->lines[$i] = $line;
1353 $this->entity =
$object->entity;
1354 $this->socid =
$object->socid;
1355 $this->fk_project =
$object->fk_project;
1356 $this->cond_reglement_id =
$object->cond_reglement_id;
1357 $this->deposit_percent =
$object->deposit_percent;
1358 $this->mode_reglement_id =
$object->mode_reglement_id;
1359 $this->fk_account =
$object->fk_account;
1360 $this->availability_id =
$object->availability_id;
1361 $this->demand_reason_id =
$object->demand_reason_id;
1362 $this->delivery_date =
$object->delivery_date;
1363 $this->shipping_method_id =
$object->shipping_method_id;
1364 $this->warehouse_id =
$object->warehouse_id;
1365 $this->fk_delivery_address =
$object->fk_delivery_address;
1366 $this->contact_id =
$object->contact_id;
1367 $this->ref_client =
$object->ref_client;
1368 $this->ref_customer =
$object->ref_client;
1371 $this->note_private =
$object->note_private;
1372 $this->note_public =
$object->note_public;
1375 $this->origin =
$object->element;
1376 $this->origin_id =
$object->id;
1379 if (!empty($conf->multicurrency->enabled)) {
1380 if (!empty(
$object->multicurrency_code)) {
1381 $this->multicurrency_code =
$object->multicurrency_code;
1384 $this->multicurrency_tx =
$object->multicurrency_tx;
1387 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1389 $this->fk_multicurrency = $tmparray[0];
1390 $this->multicurrency_tx = $tmparray[1];
1394 if (empty($this->fk_multicurrency)) {
1395 $this->multicurrency_code = $conf->currency;
1396 $this->fk_multicurrency = 0;
1397 $this->multicurrency_tx = 1;
1405 $element_extrafields = $e->fetch_name_optionals_label($this->table_element);
1407 foreach (
$object->array_options as $options_key => $value) {
1408 if (array_key_exists(str_replace(
'options_',
'', $options_key), $element_extrafields)) {
1409 $this->array_options[$options_key] = $value;
1413 $this->linked_objects[$this->origin] = $this->origin_id;
1414 if (isset(
$object->other_linked_objects) && is_array(
$object->other_linked_objects) && !empty(
$object->other_linked_objects)) {
1415 $this->linked_objects = array_merge($this->linked_objects,
$object->other_linked_objects);
1418 $ret = $this->
create($user);
1422 $hookmanager->initHooks(array(
'orderdao'));
1424 $parameters = array(
'objFrom' =>
$object);
1426 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1436 $this->
valid($user);
1488 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)
1490 global $mysoc, $conf, $langs, $user;
1492 $logtext =
"::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent";
1493 $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";
1494 $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";
1495 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
1497 if ($this->
statut == self::STATUS_DRAFT) {
1498 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1508 if (empty($info_bits)) {
1514 if (empty($txtva)) {
1517 if (empty($txlocaltax1)) {
1520 if (empty($txlocaltax2)) {
1523 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1524 $fk_parent_line = 0;
1526 if (empty($this->fk_multicurrency)) {
1527 $this->fk_multicurrency = 0;
1529 if (empty($ref_ext)) {
1536 $pu_ht_devise =
price2num($pu_ht_devise);
1539 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
1544 if ($price_base_type ==
'HT') {
1549 $label = trim($label);
1550 $desc = trim($desc);
1557 if ($date_start && $date_end && $date_start > $date_end) {
1558 $langs->load(
"errors");
1559 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1565 $product_type = $type;
1566 if (!empty($fk_product) && $fk_product > 0) {
1567 $product =
new Product($this->db);
1568 $result = $product->fetch($fk_product);
1569 $product_type = $product->type;
1571 if (
getDolGlobalString(
'STOCK_MUST_BE_ENOUGH_FOR_ORDER') && $product_type == 0 && $product->stock_reel < $qty) {
1572 $langs->load(
"errors");
1573 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
1574 $this->errors[] = $this->error;
1575 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
1576 $this->db->rollback();
1590 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1591 $vat_src_code = $reg[1];
1592 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1595 $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);
1605 $total_ht = $tabprice[0];
1606 $total_tva = $tabprice[1];
1607 $total_ttc = $tabprice[2];
1608 $total_localtax1 = $tabprice[9];
1609 $total_localtax2 = $tabprice[10];
1610 $pu_ht = $tabprice[3];
1613 $multicurrency_total_ht = $tabprice[16];
1614 $multicurrency_total_tva = $tabprice[17];
1615 $multicurrency_total_ttc = $tabprice[18];
1616 $pu_ht_devise = $tabprice[19];
1620 if ($ranktouse == -1) {
1621 $rangmax = $this->
line_max($fk_parent_line);
1622 $ranktouse = $rangmax + 1;
1631 $price = (float) $pu - $remise;
1637 $this->line->context = $this->context;
1639 $this->line->fk_commande = $this->id;
1640 $this->line->label = $label;
1641 $this->line->desc = $desc;
1642 $this->line->qty = $qty;
1643 $this->line->ref_ext = $ref_ext;
1645 $this->line->vat_src_code = $vat_src_code;
1646 $this->line->tva_tx = $txtva;
1647 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1648 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1649 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1650 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1651 $this->line->fk_product = $fk_product;
1652 $this->line->product_type = $product_type;
1653 $this->line->fk_remise_except = $fk_remise_except;
1655 $this->line->subprice = $pu_ht;
1656 $this->line->rang = $ranktouse;
1657 $this->line->info_bits = $info_bits;
1658 $this->line->total_ht = $total_ht;
1659 $this->line->total_tva = $total_tva;
1660 $this->line->total_localtax1 = $total_localtax1;
1661 $this->line->total_localtax2 = $total_localtax2;
1662 $this->line->total_ttc = $total_ttc;
1663 $this->line->special_code = $special_code;
1664 $this->line->origin = $origin;
1665 $this->line->origin_id = $origin_id;
1666 $this->line->fk_parent_line = $fk_parent_line;
1667 $this->line->fk_unit = $fk_unit;
1669 $this->line->date_start = $date_start;
1670 $this->line->date_end = $date_end;
1672 $this->line->fk_fournprice = $fk_fournprice;
1673 $this->line->pa_ht = $pa_ht;
1676 $this->line->fk_multicurrency = $this->fk_multicurrency;
1677 $this->line->multicurrency_code = $this->multicurrency_code;
1678 $this->line->multicurrency_subprice = $pu_ht_devise;
1679 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1680 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1681 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1684 $this->line->price = $price;
1686 if (is_array($array_options) && count($array_options) > 0) {
1687 $this->line->array_options = $array_options;
1690 $result = $this->line->insert($user);
1693 if (!empty($fk_parent_line)) {
1695 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
1696 $linecount = count($this->lines);
1697 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
1703 if (empty($noupdateafterinsertline)) {
1708 $this->db->commit();
1709 $this->lines[] = $this->line;
1710 return $this->line->id;
1712 $this->db->rollback();
1716 $this->error = $this->line->error;
1717 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1718 $this->db->rollback();
1722 dol_syslog(get_class($this).
"::addline status of order must be Draft to allow use of ->addline()", LOG_ERR);
1745 global $conf, $mysoc;
1751 if ($idproduct > 0) {
1752 $prod =
new Product($this->db);
1753 $prod->fetch($idproduct);
1757 if (empty($tva_tx)) {
1762 $localtax1_tx =
get_localtax($tva_tx, 1, $this->thirdparty, $mysoc, $tva_npr);
1763 $localtax2_tx =
get_localtax($tva_tx, 2, $this->thirdparty, $mysoc, $tva_npr);
1766 if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
1767 $price = $prod->multiprices[$this->thirdparty->price_level];
1769 $price = $prod->price;
1774 $line->context = $this->context;
1776 $line->fk_product = $idproduct;
1777 $line->desc = $prod->description;
1779 $line->subprice = $price;
1781 $line->vat_src_code = $vat_src_code;
1782 $line->tva_tx = $tva_tx;
1783 $line->localtax1_tx = $localtax1_tx;
1784 $line->localtax2_tx = $localtax2_tx;
1786 $line->product_ref = $prod->ref;
1787 $line->product_label = $prod->label;
1788 $line->product_desc = $prod->description;
1789 $line->fk_unit = $prod->fk_unit;
1793 $line->date_start = $date_start;
1796 $line->date_end = $date_end;
1799 $this->lines[] = $line;
1832 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
1835 if (empty($id) && empty($ref) && empty($ref_ext)) {
1839 $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';
1840 $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';
1841 $sql .=
', c.fk_account';
1842 $sql .=
', c.date_commande, c.date_valid, c.tms';
1843 $sql .=
', c.date_livraison as delivery_date';
1844 $sql .=
', c.fk_shipping_method';
1845 $sql .=
', c.fk_warehouse';
1846 $sql .=
', c.fk_projet as fk_project, c.source, c.facture as billed';
1847 $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';
1848 $sql .=
', c.fk_incoterms, c.location_incoterms';
1849 $sql .=
", c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc";
1850 $sql .=
", c.module_source, c.pos_source";
1851 $sql .=
", i.libelle as label_incoterms";
1852 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
1853 $sql .=
', cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle, cr.libelle_facture as cond_reglement_libelle_doc';
1854 $sql .=
', ca.code as availability_code, ca.label as availability_label';
1855 $sql .=
', dr.code as demand_reason_code';
1856 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
1857 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON c.fk_cond_reglement = cr.rowid';
1858 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON c.fk_mode_reglement = p.id';
1859 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON c.fk_availability = ca.rowid';
1860 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON c.fk_input_reason = dr.rowid';
1861 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
1864 $sql .=
" WHERE c.rowid=".((int) $id);
1866 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
1870 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
1873 $sql .=
" AND c.ref_ext='".$this->db->escape($ref_ext).
"'";
1876 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1877 $result = $this->db->query($sql);
1879 $obj = $this->db->fetch_object($result);
1881 $this->
id = $obj->rowid;
1882 $this->entity = $obj->entity;
1884 $this->
ref = $obj->ref;
1885 $this->ref_client = $obj->ref_client;
1886 $this->ref_customer = $obj->ref_client;
1887 $this->ref_ext = $obj->ref_ext;
1889 $this->socid = $obj->fk_soc;
1890 $this->thirdparty =
null;
1892 $this->fk_project = $obj->fk_project;
1893 $this->project =
null;
1895 $this->
statut = $obj->fk_statut;
1896 $this->
status = $obj->fk_statut;
1898 $this->user_author_id = $obj->fk_user_author;
1899 $this->user_creation_id = $obj->fk_user_author;
1900 $this->user_validation_id = $obj->fk_user_valid;
1901 $this->user_modification_id = $obj->fk_user_modif;
1902 $this->total_ht = $obj->total_ht;
1903 $this->total_tva = $obj->total_tva;
1904 $this->total_localtax1 = $obj->total_localtax1;
1905 $this->total_localtax2 = $obj->total_localtax2;
1906 $this->total_ttc = $obj->total_ttc;
1907 $this->date = $this->db->jdate($obj->date_commande);
1908 $this->date_commande = $this->db->jdate($obj->date_commande);
1909 $this->date_creation = $this->db->jdate($obj->date_creation);
1910 $this->date_validation = $this->db->jdate($obj->date_valid);
1911 $this->date_modification = $this->db->jdate($obj->tms);
1912 $this->source = $obj->source;
1913 $this->billed = $obj->billed;
1914 $this->note = $obj->note_private;
1915 $this->note_private = $obj->note_private;
1916 $this->note_public = $obj->note_public;
1917 $this->model_pdf = $obj->model_pdf;
1918 $this->last_main_doc = $obj->last_main_doc;
1919 $this->mode_reglement_id = $obj->fk_mode_reglement;
1920 $this->mode_reglement_code = $obj->mode_reglement_code;
1921 $this->mode_reglement = $obj->mode_reglement_libelle;
1922 $this->cond_reglement_id = $obj->fk_cond_reglement;
1923 $this->cond_reglement_code = $obj->cond_reglement_code;
1924 $this->cond_reglement = $obj->cond_reglement_libelle;
1925 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1926 $this->deposit_percent = $obj->deposit_percent;
1927 $this->fk_account = $obj->fk_account;
1928 $this->availability_id = $obj->fk_availability;
1929 $this->availability_code = $obj->availability_code;
1931 $this->demand_reason_id = $obj->fk_input_reason;
1932 $this->demand_reason_code = $obj->demand_reason_code;
1933 $this->delivery_date = $this->db->jdate($obj->delivery_date);
1934 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method :
null;
1935 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse :
null;
1936 $this->fk_delivery_address = $obj->fk_delivery_address;
1937 $this->module_source = $obj->module_source;
1938 $this->pos_source = $obj->pos_source;
1941 $this->fk_incoterms = $obj->fk_incoterms;
1942 $this->location_incoterms = $obj->location_incoterms;
1943 $this->label_incoterms = $obj->label_incoterms;
1946 $this->fk_multicurrency = $obj->fk_multicurrency;
1947 $this->multicurrency_code = $obj->multicurrency_code;
1948 $this->multicurrency_tx = $obj->multicurrency_tx;
1949 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1950 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1951 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1953 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
1955 $this->lines = array();
1961 $this->db->free($result);
1970 $this->error =
'Order with id '.$id.
' not found sql='.$sql;
1974 $this->error = $this->db->error();
1992 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1993 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1998 $result = $remise->fetch($idremise);
2001 if ($remise->fk_facture) {
2002 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2003 $this->db->rollback();
2009 $line->fk_commande = $this->id;
2010 $line->fk_remise_except = $remise->id;
2011 $line->desc = $remise->description;
2012 $line->vat_src_code = $remise->vat_src_code;
2013 $line->tva_tx = $remise->tva_tx;
2014 $line->subprice = -$remise->amount_ht;
2015 $line->price = -$remise->amount_ht;
2016 $line->fk_product = 0;
2018 $line->remise_percent = 0;
2020 $line->info_bits = 2;
2022 $line->total_ht = -$remise->amount_ht;
2023 $line->total_tva = -$remise->amount_tva;
2024 $line->total_ttc = -$remise->amount_ttc;
2026 $result = $line->insert();
2030 $this->db->commit();
2033 $this->db->rollback();
2037 $this->error = $line->error;
2038 $this->errors = $line->errors;
2039 $this->db->rollback();
2043 $this->db->rollback();
2057 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2060 global $langs, $conf;
2062 $this->lines = array();
2064 $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,';
2065 $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,';
2066 $sql .=
' l.total_ht, l.total_ttc, l.total_tva, l.total_localtax1, l.total_localtax2, l.date_start, l.date_end,';
2067 $sql .=
' l.fk_unit,';
2068 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2069 $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,';
2070 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units';
2071 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as l';
2072 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (p.rowid = l.fk_product)';
2073 $sql .=
' WHERE l.fk_commande = '.((int) $this->
id);
2074 if ($only_product) {
2075 $sql .=
' AND p.fk_product_type = 0';
2077 $sql .=
' ORDER BY l.rang, l.rowid';
2079 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
2080 $result = $this->db->query($sql);
2082 $num = $this->db->num_rows($result);
2086 $objp = $this->db->fetch_object($result);
2090 $line->rowid = $objp->rowid;
2091 $line->id = $objp->rowid;
2092 $line->fk_commande = $objp->fk_commande;
2093 $line->commande_id = $objp->fk_commande;
2094 $line->label = $objp->custom_label;
2095 $line->desc = $objp->description;
2096 $line->description = $objp->description;
2097 $line->product_type = $objp->product_type;
2098 $line->qty = $objp->qty;
2099 $line->ref_ext = $objp->ref_ext;
2101 $line->vat_src_code = $objp->vat_src_code;
2102 $line->tva_tx = $objp->tva_tx;
2103 $line->localtax1_tx = $objp->localtax1_tx;
2104 $line->localtax2_tx = $objp->localtax2_tx;
2105 $line->localtax1_type = $objp->localtax1_type;
2106 $line->localtax2_type = $objp->localtax2_type;
2107 $line->total_ht = $objp->total_ht;
2108 $line->total_ttc = $objp->total_ttc;
2109 $line->total_tva = $objp->total_tva;
2110 $line->total_localtax1 = $objp->total_localtax1;
2111 $line->total_localtax2 = $objp->total_localtax2;
2112 $line->subprice = $objp->subprice;
2113 $line->fk_remise_except = $objp->fk_remise_except;
2114 $line->remise_percent = $objp->remise_percent;
2115 $line->price = $objp->price;
2116 $line->fk_product = $objp->fk_product;
2117 $line->fk_fournprice = $objp->fk_fournprice;
2118 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2119 $line->pa_ht = $marginInfos[0];
2120 $line->marge_tx = $marginInfos[1];
2121 $line->marque_tx = $marginInfos[2];
2122 $line->rang = $objp->rang;
2123 $line->info_bits = $objp->info_bits;
2124 $line->special_code = $objp->special_code;
2125 $line->fk_parent_line = $objp->fk_parent_line;
2127 $line->ref = $objp->product_ref;
2128 $line->libelle = $objp->product_label;
2130 $line->product_ref = $objp->product_ref;
2131 $line->product_label = $objp->product_label;
2132 $line->product_tosell = $objp->product_tosell;
2133 $line->product_tobuy = $objp->product_tobuy;
2134 $line->product_desc = $objp->product_desc;
2135 $line->product_tobatch = $objp->product_tobatch;
2136 $line->product_barcode = $objp->product_barcode;
2138 $line->fk_product_type = $objp->fk_product_type;
2139 $line->fk_unit = $objp->fk_unit;
2141 $line->weight = $objp->weight;
2142 $line->weight_units = $objp->weight_units;
2143 $line->volume = $objp->volume;
2144 $line->volume_units = $objp->volume_units;
2146 $line->date_start = $this->db->jdate($objp->date_start);
2147 $line->date_end = $this->db->jdate($objp->date_end);
2150 $line->fk_multicurrency = $objp->fk_multicurrency;
2151 $line->multicurrency_code = $objp->multicurrency_code;
2152 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2153 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2154 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2155 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2157 $line->fetch_optionals();
2160 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2161 $tmpproduct =
new Product($this->db);
2162 $tmpproduct->fetch($objp->fk_product);
2163 $tmpproduct->getMultiLangs();
2165 $line->multilangs = $tmpproduct->multilangs;
2168 $this->lines[$i] = $line;
2173 $this->db->free($result);
2177 $this->error = $this->db->error();
2191 foreach ($this->lines as $line) {
2192 if ($line->product_type == 0) {
2207 foreach ($this->lines as $line) {
2208 if ($line->product_type == 1) {
2224 $sql =
'SELECT COUNT(DISTINCT ed.fk_expedition) as nb';
2225 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2226 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2228 $sql .=
' ed.fk_elementdet = cd.rowid';
2229 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2232 dol_syslog(get_class($this).
"::getNbOfShipments", LOG_DEBUG);
2233 $resql = $this->db->query($sql);
2235 $obj = $this->db->fetch_object($resql);
2240 $this->db->free($resql);
2243 $this->error = $this->db->lasterror();
2258 $this->expeditions = array();
2260 $sql =
'SELECT cd.rowid, cd.fk_product,';
2261 $sql .=
' sum(ed.qty) as qty';
2262 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2263 if ($filtre_statut >= 0) {
2264 $sql .=
' '.MAIN_DB_PREFIX.
'expedition as e,';
2266 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2268 if ($filtre_statut >= 0) {
2269 $sql .=
' ed.fk_expedition = e.rowid AND';
2271 $sql .=
' ed.fk_elementdet = cd.rowid';
2272 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2273 if ($fk_product > 0) {
2274 $sql .=
' AND cd.fk_product = '.((int) $fk_product);
2276 if ($filtre_statut >= 0) {
2277 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
2279 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
2282 dol_syslog(get_class($this).
"::loadExpeditions", LOG_DEBUG);
2283 $resql = $this->db->query($sql);
2285 $num = $this->db->num_rows($resql);
2288 $obj = $this->db->fetch_object($resql);
2289 $this->expeditions[$obj->rowid] = $obj->qty;
2292 $this->db->free($resql);
2295 $this->error = $this->db->lasterror();
2307 $sql =
'SELECT count(*)';
2308 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expedition as e';
2309 $sql .=
', '.MAIN_DB_PREFIX.
'element_element as el';
2310 $sql .=
' WHERE el.fk_source = '.((int) $this->
id);
2311 $sql .=
" AND el.sourcetype = 'commande'";
2312 $sql .=
" AND el.fk_target = e.rowid";
2313 $sql .=
" AND el.targettype = 'shipping'";
2315 $resql = $this->db->query($sql);
2317 $row = $this->db->fetch_row($resql);
2375 if ($this->
statut == self::STATUS_DRAFT) {
2381 $line->context = $this->context;
2384 $line->fetch($lineid);
2386 if ($id > 0 && $line->fk_commande != $id) {
2387 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2392 $staticline = clone $line;
2393 $line->oldline = $staticline;
2395 if ($line->delete($user) > 0) {
2399 $this->db->commit();
2402 $this->db->rollback();
2403 $this->error = $this->db->lasterror();
2407 $this->db->rollback();
2408 $this->error = $line->error;
2412 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
2431 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
2433 return $this->
setDiscount($user, $remise, $notrigger);
2446 $remise = trim((
string) $remise) ? trim((
string) $remise) : 0;
2448 if ($user->hasRight(
'commande',
'creer')) {
2455 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2456 $sql .=
' SET remise_percent = '.((float) $remise);
2457 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.((int) self::STATUS_DRAFT);
2460 $resql = $this->db->query($sql);
2462 $this->errors[] = $this->db->error();
2467 $this->oldcopy = clone $this;
2468 $this->remise_percent = $remise;
2472 if (!$notrigger && empty($error)) {
2482 $this->db->commit();
2485 foreach ($this->errors as $errmsg) {
2486 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2487 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2489 $this->db->rollback();
2577 if ($user->hasRight(
'commande',
'creer')) {
2582 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2583 $sql .=
" SET date_commande = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
2584 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut = ".((int) self::STATUS_DRAFT);
2587 $resql = $this->db->query($sql);
2589 $this->errors[] = $this->db->error();
2594 $this->oldcopy = clone $this;
2595 $this->date = $date;
2598 if (!$notrigger && empty($error)) {
2608 $this->db->commit();
2611 foreach ($this->errors as $errmsg) {
2612 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2613 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2615 $this->db->rollback();
2649 if ($user->hasRight(
'commande',
'creer')) {
2654 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2655 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2656 $sql .=
" WHERE rowid = ".((int) $this->
id);
2659 $resql = $this->db->query($sql);
2661 $this->errors[] = $this->db->error();
2666 $this->oldcopy = clone $this;
2667 $this->delivery_date = $delivery_date;
2670 if (!$notrigger && empty($error)) {
2680 $this->db->commit();
2683 foreach ($this->errors as $errmsg) {
2684 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2685 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2687 $this->db->rollback();
2709 public function liste_array($shortlist = 0, $draft = 0, $excluser =
null, $socid = 0, $limit = 0, $offset = 0, $sortfield =
'c.date_commande', $sortorder =
'DESC')
2716 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2717 $sql .=
" c.rowid as cid, c.ref";
2718 if (!$user->hasRight(
'societe',
'client',
'voir')) {
2719 $sql .=
", sc.fk_soc, sc.fk_user";
2721 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"commande as c";
2722 if (!$user->hasRight(
'societe',
'client',
'voir')) {
2723 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2725 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2726 $sql .=
" AND c.fk_soc = s.rowid";
2727 if (!$user->hasRight(
'societe',
'client',
'voir')) {
2728 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2731 $sql .=
" AND s.rowid = ".((int) $socid);
2734 $sql .=
" AND c.fk_statut = ".self::STATUS_DRAFT;
2736 if (is_object($excluser)) {
2737 $sql .=
" AND c.fk_user_author <> ".((int) $excluser->id);
2739 $sql .= $this->db->order($sortfield, $sortorder);
2740 $sql .= $this->db->plimit($limit, $offset);
2742 $result = $this->db->query($sql);
2744 $numc = $this->db->num_rows($result);
2747 while ($i < $numc) {
2748 $obj = $this->db->fetch_object($result);
2750 if ($shortlist == 1) {
2751 $ga[$obj->cid] = $obj->ref;
2752 } elseif ($shortlist == 2) {
2753 $ga[$obj->cid] = $obj->ref.
' ('.$obj->name.
')';
2755 $ga[$i][
'id'] = $obj->cid;
2756 $ga[$i][
'ref'] = $obj->ref;
2757 $ga[$i][
'name'] = $obj->name;
2780 dol_syslog(
'Commande::availability('.$availability_id.
')');
2781 if ($this->
statut >= self::STATUS_DRAFT) {
2786 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2787 $sql .=
' SET fk_availability = '.((int) $availability_id);
2788 $sql .=
' WHERE rowid='.((int) $this->
id);
2791 $resql = $this->db->query($sql);
2793 $this->errors[] = $this->db->error();
2798 $this->oldcopy = clone $this;
2799 $this->availability_id = $availability_id;
2802 if (!$notrigger && empty($error)) {
2812 $this->db->commit();
2815 foreach ($this->errors as $errmsg) {
2816 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2817 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2819 $this->db->rollback();
2823 $error_str =
'Command status do not meet requirement '.$this->statut;
2825 $this->error = $error_str;
2826 $this->errors[] = $this->error;
2844 dol_syslog(
'Commande::demand_reason('.$demand_reason_id.
')');
2845 if ($this->
statut >= self::STATUS_DRAFT) {
2850 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2851 $sql .=
' SET fk_input_reason = '.((int) $demand_reason_id);
2852 $sql .=
' WHERE rowid='.((int) $this->
id);
2855 $resql = $this->db->query($sql);
2857 $this->errors[] = $this->db->error();
2862 $this->oldcopy = clone $this;
2863 $this->demand_reason_id = $demand_reason_id;
2866 if (!$notrigger && empty($error)) {
2876 $this->db->commit();
2879 foreach ($this->errors as $errmsg) {
2880 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2881 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2883 $this->db->rollback();
2887 $error_str =
'order status do not meet requirement '.$this->statut;
2889 $this->error = $error_str;
2890 $this->errors[] = $this->error;
2907 if ($user->hasRight(
'commande',
'creer')) {
2912 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET';
2913 $sql .=
' ref_client = '.(empty($ref_client) ?
'NULL' :
"'".$this->db->escape($ref_client).
"'");
2914 $sql .=
' WHERE rowid = '.((int) $this->
id);
2916 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2917 $resql = $this->db->query($sql);
2919 $this->errors[] = $this->db->error();
2924 $this->oldcopy = clone $this;
2925 $this->ref_client = $ref_client;
2926 $this->ref_customer = $ref_client;
2929 if (!$notrigger && empty($error)) {
2938 $this->db->commit();
2941 foreach ($this->errors as $errmsg) {
2942 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2943 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2945 $this->db->rollback();
2964 if ($this->billed) {
2970 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 1';
2973 dol_syslog(get_class($this).
"::classifyBilled", LOG_DEBUG);
2974 if ($this->db->query($sql)) {
2976 $this->oldcopy = clone $this;
2980 if (!$notrigger && empty($error)) {
2982 $result = $this->
call_trigger(
'ORDER_CLASSIFY_BILLED', $user);
2990 $this->db->commit();
2993 foreach ($this->errors as $errmsg) {
2994 dol_syslog(get_class($this).
"::classifyBilled ".$errmsg, LOG_ERR);
2995 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2997 $this->db->rollback();
3001 $this->error = $this->db->error();
3002 $this->db->rollback();
3020 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 0';
3023 dol_syslog(get_class($this).
"::classifyUnBilled", LOG_DEBUG);
3024 if ($this->db->query($sql)) {
3026 $this->oldcopy = clone $this;
3030 if (!$notrigger && empty($error)) {
3032 $result = $this->
call_trigger(
'ORDER_CLASSIFY_UNBILLED', $user);
3042 $this->db->commit();
3045 foreach ($this->errors as $errmsg) {
3046 dol_syslog(get_class($this).
"::classifyUnBilled ".$errmsg, LOG_ERR);
3047 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3049 $this->db->rollback();
3053 $this->error = $this->db->error();
3054 $this->db->rollback();
3090 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)
3092 global $conf, $mysoc, $langs, $user;
3094 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");
3095 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3102 if (empty($info_bits)) {
3105 if (empty($txtva)) {
3108 if (empty($txlocaltax1)) {
3111 if (empty($txlocaltax2)) {
3117 if (empty($special_code) || $special_code == 3) {
3120 if (empty($ref_ext)) {
3124 if ($date_start && $date_end && $date_start > $date_end) {
3125 $langs->load(
"errors");
3126 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3134 $pu_ht_devise =
price2num($pu_ht_devise);
3135 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
3138 $txlocaltax1 = (float)
price2num($txlocaltax1);
3139 $txlocaltax2 = (float)
price2num($txlocaltax2);
3153 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3154 $vat_src_code = $reg[1];
3155 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3158 $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);
3160 $total_ht = $tabprice[0];
3161 $total_tva = $tabprice[1];
3162 $total_ttc = $tabprice[2];
3163 $total_localtax1 = $tabprice[9];
3164 $total_localtax2 = $tabprice[10];
3165 $pu_ht = $tabprice[3];
3166 $pu_tva = $tabprice[4];
3167 $pu_ttc = $tabprice[5];
3170 $multicurrency_total_ht = $tabprice[16];
3171 $multicurrency_total_tva = $tabprice[17];
3172 $multicurrency_total_ttc = $tabprice[18];
3173 $pu_ht_devise = $tabprice[19];
3177 if ($price_base_type ==
'TTC') {
3178 $subprice = $pu_ttc;
3185 $price = ((float) $pu - $remise);
3190 $line->fetch($rowid);
3191 $line->fetch_optionals();
3193 if (!empty($line->fk_product)) {
3194 $product =
new Product($this->db);
3195 $result = $product->fetch($line->fk_product);
3196 $product_type = $product->type;
3198 if (
getDolGlobalString(
'STOCK_MUST_BE_ENOUGH_FOR_ORDER') && $product_type == 0 && $product->stock_reel < $qty) {
3199 $langs->load(
"errors");
3200 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
3201 $this->errors[] = $this->error;
3203 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
3205 $this->db->rollback();
3210 $staticline = clone $line;
3212 $line->oldline = $staticline;
3213 $this->line = $line;
3214 $this->line->context = $this->context;
3215 $this->line->rang = $rang;
3218 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
3219 $rangmax = $this->
line_max($fk_parent_line);
3220 $this->line->rang = $rangmax + 1;
3223 $this->line->id = $rowid;
3224 $this->line->label = $label;
3225 $this->line->desc = $desc;
3226 $this->line->qty = $qty;
3227 $this->line->ref_ext = $ref_ext;
3229 $this->line->vat_src_code = $vat_src_code;
3230 $this->line->tva_tx = $txtva;
3231 $this->line->localtax1_tx = $txlocaltax1;
3232 $this->line->localtax2_tx = $txlocaltax2;
3233 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3234 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3236 $this->line->subprice = $pu_ht;
3237 $this->line->info_bits = $info_bits;
3238 $this->line->special_code = $special_code;
3239 $this->line->total_ht = $total_ht;
3240 $this->line->total_tva = $total_tva;
3241 $this->line->total_localtax1 = $total_localtax1;
3242 $this->line->total_localtax2 = $total_localtax2;
3243 $this->line->total_ttc = $total_ttc;
3244 $this->line->date_start = $date_start;
3245 $this->line->date_end = $date_end;
3246 $this->line->product_type = $type;
3247 $this->line->fk_parent_line = $fk_parent_line;
3248 $this->line->skip_update_total = $skip_update_total;
3249 $this->line->fk_unit = $fk_unit;
3251 $this->line->fk_fournprice = $fk_fournprice;
3252 $this->line->pa_ht = $pa_ht;
3255 $this->line->multicurrency_subprice = $pu_ht_devise;
3256 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
3257 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
3258 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
3261 $this->line->price = $price;
3263 if (is_array($array_options) && count($array_options) > 0) {
3265 foreach ($array_options as $key => $value) {
3266 $this->line->array_options[$key] = $array_options[$key];
3270 $result = $this->line->update($user, $notrigger);
3273 if (!empty($fk_parent_line)) {
3280 $this->db->commit();
3283 $this->error = $this->line->error;
3285 $this->db->rollback();
3289 $this->error = get_class($this).
"::updateline Order status makes operation forbidden";
3290 $this->errors = array(
'OrderStatusMakeOperationForbidden');
3309 if (isset($this->
ref)) {
3310 $this->
ref = trim($this->
ref);
3312 if (isset($this->ref_client)) {
3313 $this->ref_client = trim($this->ref_client);
3315 if (isset($this->ref_customer)) {
3316 $this->ref_customer = trim($this->ref_customer);
3318 if (isset($this->note) || isset($this->note_private)) {
3319 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
3321 if (isset($this->note_public)) {
3322 $this->note_public = trim($this->note_public);
3324 if (isset($this->model_pdf)) {
3325 $this->model_pdf = trim($this->model_pdf);
3327 if (isset($this->import_key)) {
3328 $this->import_key = trim($this->import_key);
3330 $delivery_date = $this->delivery_date;
3336 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande SET";
3338 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
3339 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
3340 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
3341 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
3342 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
3343 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
3344 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
3345 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
3346 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
3347 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
3348 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
3349 $sql .=
" fk_statut=".(isset($this->
statut) ? $this->
statut :
"null").
",";
3350 $sql .=
" fk_user_modif=".(isset($user->id) ? $user->id :
"null").
",";
3351 $sql .=
" fk_user_valid=".((isset($this->user_validation_id) && $this->user_validation_id > 0) ? $this->user_validation_id :
"null").
",";
3352 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
3353 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
3354 $sql .=
" deposit_percent=".(!empty($this->deposit_percent) ? strval($this->deposit_percent) :
"null").
",";
3355 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
3356 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
3357 $sql .=
" fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id :
"null").
",";
3358 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
3359 $sql .=
" fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id :
"null").
",";
3360 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
3361 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
3362 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
3363 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
3365 $sql .=
" WHERE rowid=".((int) $this->
id);
3369 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3370 $resql = $this->db->query($sql);
3373 $this->errors[] =
"Error ".$this->db->lasterror();
3383 if (!$error && !$notrigger) {
3394 foreach ($this->errors as $errmsg) {
3395 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3396 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3398 $this->db->rollback();
3401 $this->db->commit();
3413 public function delete($user, $notrigger = 0)
3415 global $conf, $langs;
3416 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3420 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
3435 $this->errors[] = $langs->trans(
'SomeShipmentExists');
3440 if (!$error && !empty($this->table_element_line)) {
3441 $tabletodelete = $this->table_element_line;
3442 $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).
")";
3443 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3444 if (!$this->db->query($sqlef) || !$this->db->query($sql)) {
3446 $this->error = $this->db->lasterror();
3447 $this->errors[] = $this->error;
3448 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3473 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3479 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
3480 $res = $this->db->query($sql);
3483 $this->error = $this->db->lasterror();
3484 $this->errors[] = $this->error;
3485 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3501 if ($conf->commande->multidir_output[$this->entity] && !empty($this->
ref)) {
3502 $dir = $conf->commande->multidir_output[$this->entity].
"/".$ref;
3503 $file = $dir.
"/".$ref.
".pdf";
3504 if (file_exists($file)) {
3508 $this->error =
'ErrorFailToDeleteFile';
3509 $this->errors[] = $this->error;
3510 $this->db->rollback();
3514 if (file_exists($dir)) {
3517 $this->error =
'ErrorFailToDeleteDir';
3518 $this->errors[] = $this->error;
3519 $this->db->rollback();
3527 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3528 $this->db->commit();
3531 $this->db->rollback();
3548 global $conf, $langs;
3552 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.date_livraison as delivery_date, c.fk_statut, c.total_ht";
3553 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as c";
3554 if (!$user->hasRight(
'societe',
'client',
'voir')) {
3555 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
3556 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3559 $sql .= $clause.
" c.entity IN (".
getEntity(
'commande').
")";
3561 if ($mode ==
'toship') {
3563 $sql .=
" AND c.fk_statut IN (" . self::STATUS_VALIDATED .
"," . self::STATUS_SHIPMENTONPROCESS .
")";
3565 if ($mode ==
'tobill') {
3567 $sql .=
" AND c.fk_statut IN (" . self::STATUS_VALIDATED .
"," . self::STATUS_SHIPMENTONPROCESS .
", " . self::STATUS_CLOSED .
") AND c.facture = 0";
3569 if ($mode ==
'shippedtobill') {
3571 $sql .=
" AND c.fk_statut IN (" . self::STATUS_CLOSED .
") AND c.facture = 0";
3574 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3577 $resql = $this->db->query($sql);
3580 $label = $labelShort = $url =
'';
3581 if ($mode ==
'toship') {
3582 $delay_warning = $conf->commande->client->warning_delay / 60 / 60 / 24;
3583 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=-2&mainmenu=commercial&leftmenu=orders';
3584 $label = $langs->transnoentitiesnoconv(
"OrdersToProcess");
3585 $labelShort = $langs->transnoentitiesnoconv(
"Opened");
3587 if ($mode ==
'tobill') {
3588 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=-3&search_billed=0&mainmenu=commercial&leftmenu=orders';
3589 $label = $langs->trans(
"OrdersToBill");
3590 $labelShort = $langs->trans(
"ToBill");
3592 if ($mode ==
'shippedtobill') {
3593 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=3&search_billed=0&mainmenu=commercial&leftmenu=orders';
3594 $label = $langs->trans(
"OrdersToBill");
3595 $labelShort = $langs->trans(
"StatusOrderDelivered").
' '.$langs->trans(
"and").
' '.$langs->trans(
"ToBill");
3600 $response->warning_delay = $delay_warning;
3601 $response->label = $label;
3602 $response->labelShort = $labelShort;
3603 $response->url = $url;
3604 $response->url_late = DOL_URL_ROOT.
'/commande/list.php?search_option=late&mainmenu=commercial&leftmenu=orders';
3607 $generic_commande =
new Commande($this->db);
3609 while ($obj = $this->db->fetch_object($resql)) {
3610 $response->nbtodo++;
3611 $response->total += $obj->total_ht;
3613 $generic_commande->statut = $obj->fk_statut;
3614 $generic_commande->date_commande = $this->db->jdate($obj->date_commande);
3615 $generic_commande->date = $this->db->jdate($obj->date_commande);
3616 $generic_commande->delivery_date = $this->db->jdate($obj->delivery_date);
3618 if ($mode ==
'toship' && $generic_commande->hasDelay()) {
3619 $response->nbtodolate++;
3625 $this->error = $this->db->error();
3639 $label = $langs->trans(
'OrderSource'.$this->source);
3641 if ($label ==
'OrderSource') {
3668 public function LibStatut($status, $billed, $mode, $donotshowbilled = 0)
3671 global $langs, $hookmanager;
3674 if (empty($donotshowbilled)) {
3675 $billedtext .= ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3680 if ($status == self::STATUS_CANCELED) {
3681 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderCanceled');
3682 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderCanceledShort');
3683 $statusType =
'status9';
3684 } elseif ($status == self::STATUS_DRAFT) {
3685 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDraft');
3686 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDraftShort');
3687 $statusType =
'status0';
3688 } elseif ($status == self::STATUS_VALIDATED) {
3689 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderValidated').$billedtext;
3690 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderValidatedShort').$billedtext;
3691 $statusType =
'status1';
3692 } elseif ($status == self::STATUS_SHIPMENTONPROCESS) {
3693 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderSent').$billedtext;
3694 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderSentShort').$billedtext;
3695 $labelTooltip = $langs->transnoentitiesnoconv(
"StatusOrderSent");
3696 if (!empty($this->delivery_date)) {
3697 $labelTooltip .=
' - '.$langs->transnoentitiesnoconv(
"DateDeliveryPlanned").dol_print_date($this->delivery_date,
'day').$billedtext;
3699 $statusType =
'status4';
3700 } elseif ($status == self::STATUS_CLOSED) {
3701 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDelivered').$billedtext;
3702 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDeliveredShort').$billedtext;
3703 $statusType =
'status6';
3705 $labelStatus = $langs->transnoentitiesnoconv(
'Unknown');
3706 $labelStatusShort =
'';
3711 $parameters = array(
3712 'status' => $status,
3714 'billed' => $billed,
3715 'donotshowbilled' => $donotshowbilled
3718 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
3721 return $hookmanager->resPrint;
3724 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', array(
'tooltip' => $labelTooltip));
3735 global $conf, $langs, $user;
3737 $langs->load(
'orders');
3739 $nofetch = !empty($params[
'nofetch']);
3742 return [
'optimize' => $langs->trans(
"Order")];
3745 if ($user->hasRight(
'commande',
'lire')) {
3746 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Order").
'</u>';
3747 if (isset($this->
statut)) {
3748 $datas[
'status'] =
' '.$this->getLibStatut(5);
3750 $datas[
'Ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
3752 $langs->load(
'companies');
3753 if (empty($this->thirdparty)) {
3756 $datas[
'customer'] =
'<br><b>'.$langs->trans(
'Customer').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
3758 $datas[
'RefCustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.(empty($this->ref_customer) ? (empty($this->ref_client) ?
'' : $this->ref_client) : $this->ref_customer);
3760 $langs->load(
'project');
3761 if (is_null($this->project) || (is_object($this->project) && $this->project->isEmpty())) {
3763 if ($res > 0 && $this->project instanceof
Project) {
3764 $datas[
'project'] =
'<br><b>'.$langs->trans(
'Project').
':</b> '.$this->project->getNomUrl(1,
'', 0, 1);
3768 if (!empty($this->total_ht)) {
3769 $datas[
'AmountHT'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
3771 if (!empty($this->total_tva)) {
3772 $datas[
'VAT'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
3774 if (!empty($this->total_ttc)) {
3775 $datas[
'AmountTTC'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
3777 if (!empty($this->date)) {
3778 $datas[
'Date'] =
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
3780 if (!empty($this->delivery_date)) {
3781 $datas[
'DeliveryDate'] =
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
3801 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0, $target =
'')
3803 global $conf, $langs, $user, $hookmanager;
3805 if (!empty($conf->dol_no_mouse_hover)) {
3811 if (isModEnabled(
"shipping") && ($option ==
'1' || $option ==
'2')) {
3812 $url = DOL_URL_ROOT.
'/expedition/shipment.php?id='.$this->id;
3814 $url = DOL_URL_ROOT.
'/commande/card.php?id='.$this->id;
3817 if (!$user->hasRight(
'commande',
'lire')) {
3821 if ($option !==
'nolink') {
3823 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3824 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
3825 $add_save_lastsearch_values = 1;
3827 if ($add_save_lastsearch_values) {
3828 $url .=
'&save_lastsearch_values=1';
3837 'objecttype' => $this->element,
3838 'option' => $option,
3841 $classfortooltip =
'classfortooltip';
3844 $classfortooltip =
'classforajaxtooltip';
3845 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
3852 if (empty($notooltip) && $user->hasRight(
'commande',
'lire')) {
3854 $label = $langs->trans(
"Order");
3855 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
3857 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
3858 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
3860 $target_value = array(
'_self',
'_blank',
'_parent',
'_top');
3861 if (in_array($target, $target_value)) {
3862 $linkclose .=
' target="'.dol_escape_htmltag($target).
'"';
3866 $linkstart =
'<a href="'.$url.
'"';
3867 $linkstart .= $linkclose.
'>';
3870 if ($option ===
'nolink') {
3875 $result .= $linkstart;
3877 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
3879 if ($withpicto != 2) {
3880 $result .= $this->ref;
3882 $result .= $linkend;
3884 if ($addlinktonotes) {
3885 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3888 $result .=
' <span class="note inline-block">';
3889 $result .=
'<a href="'.DOL_URL_ROOT.
'/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3894 $result .=
'</span>';
3899 $hookmanager->initHooks(array($this->element .
'dao'));
3900 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
3901 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3903 $result = $hookmanager->resPrint;
3905 $result .= $hookmanager->resPrint;
3919 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem,';
3920 $sql .=
' date_valid as datev,';
3921 $sql .=
' date_cloture as datecloture,';
3922 $sql .=
' fk_user_author, fk_user_valid, fk_user_cloture';
3923 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
3924 $sql .=
' WHERE c.rowid = '.((int) $id);
3925 $result = $this->db->query($sql);
3927 if ($this->db->num_rows($result)) {
3928 $obj = $this->db->fetch_object($result);
3929 $this->
id = $obj->rowid;
3930 if ($obj->fk_user_author) {
3931 $this->user_creation_id = $obj->fk_user_author;
3933 if ($obj->fk_user_valid) {
3934 $this->user_validation_id = $obj->fk_user_valid;
3936 if ($obj->fk_user_cloture) {
3937 $this->user_closing_id = $obj->fk_user_cloture;
3940 $this->date_creation = $this->db->jdate($obj->datec);
3941 $this->date_modification = $this->db->jdate($obj->datem);
3942 $this->date_validation = $this->db->jdate($obj->datev);
3943 $this->date_cloture = $this->db->jdate($obj->datecloture);
3946 $this->db->free($result);
3962 global $conf, $langs;
3964 dol_syslog(get_class($this).
"::initAsSpecimen");
3969 $sql =
"SELECT rowid";
3970 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
3971 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3972 $sql .= $this->db->plimit(100);
3974 $resql = $this->db->query($sql);
3976 $num_prods = $this->db->num_rows($resql);
3978 while ($i < $num_prods) {
3980 $row = $this->db->fetch_row($resql);
3981 $prodids[$i] = $row[0];
3987 $this->
ref =
'SPECIMEN';
3988 $this->specimen = 1;
3989 $this->entity = $conf->entity;
3991 $this->date = time();
3992 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
3993 $this->cond_reglement_code =
'RECEP';
3994 $this->mode_reglement_code =
'CHQ';
3995 $this->availability_code =
'DSP';
3996 $this->demand_reason_code =
'SRC_00';
3998 $this->note_public =
'This is a comment (public)';
3999 $this->note_private =
'This is a comment (private)';
4001 $this->multicurrency_tx = 1;
4002 $this->multicurrency_code = $conf->currency;
4004 $this->
status = $this::STATUS_DRAFT;
4009 while ($xnbp < $nbp) {
4012 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
4014 $line->subprice = 100;
4018 $line->total_ht = 50;
4019 $line->total_ttc = 60;
4020 $line->total_tva = 10;
4021 $line->remise_percent = 50;
4023 $line->total_ht = 100;
4024 $line->total_ttc = 120;
4025 $line->total_tva = 20;
4026 $line->remise_percent = 0;
4028 if ($num_prods > 0) {
4029 $prodid = mt_rand(1, $num_prods);
4030 $line->fk_product = $prodids[$prodid];
4031 $line->product_ref =
'SPECIMEN';
4034 $this->lines[$xnbp] = $line;
4036 $this->total_ht += $line->total_ht;
4037 $this->total_tva += $line->total_tva;
4038 $this->total_ttc += $line->total_ttc;
4056 $this->nb = array();
4059 $sql =
"SELECT count(co.rowid) as nb";
4060 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as co";
4061 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
4062 if (!$user->hasRight(
'societe',
'client',
'voir')) {
4063 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
4064 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
4067 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'commande').
")";
4069 $resql = $this->db->query($sql);
4071 while ($obj = $this->db->fetch_object($resql)) {
4072 $this->nb[
"orders"] = $obj->nb;
4074 $this->db->free($resql);
4078 $this->error = $this->db->error();
4104 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
4106 global $conf, $langs;
4108 $langs->load(
"orders");
4109 $outputlangs->load(
"products");
4112 $modele =
'einstein';
4114 if (!empty($this->model_pdf)) {
4115 $modele = $this->model_pdf;
4121 $modelpath =
"core/modules/commande/doc/";
4123 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
4176 return max($this->date, $this->delivery_date) < ($now - $conf->commande->client->warning_delay);
4186 global $conf, $langs;
4188 if (empty($this->delivery_date)) {
4189 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date,
'day');
4191 $text = $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->delivery_date,
'day');
4193 $text .=
' '.($conf->commande->client->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->client->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
4208 public $element =
'commandedet';
4210 public $table_element =
'commandedet';
4218 public $fk_commande;
4226 public $commande_id;
4228 public $fk_parent_line;
4240 public $fk_remise_except;
4246 public $fk_fournprice;
4266 public $skip_update_total;
4287 $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,';
4288 $sql .=
' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
4289 $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,';
4290 $sql .=
' cd.fk_unit,';
4291 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
4292 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
4293 $sql .=
' cd.date_start, cd.date_end, cd.vat_src_code';
4294 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as cd';
4295 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
4296 $sql .=
' WHERE cd.rowid = '.((int) $rowid);
4297 $result = $this->db->query($sql);
4299 $objp = $this->db->fetch_object($result);
4302 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.$sql;
4306 $this->
rowid = $objp->rowid;
4307 $this->
id = $objp->rowid;
4308 $this->fk_commande = $objp->fk_commande;
4309 $this->fk_parent_line = $objp->fk_parent_line;
4310 $this->label = $objp->custom_label;
4311 $this->desc = $objp->description;
4312 $this->qty = $objp->qty;
4313 $this->
price = $objp->price;
4314 $this->subprice = $objp->subprice;
4315 $this->ref_ext = $objp->ref_ext;
4316 $this->vat_src_code = $objp->vat_src_code;
4317 $this->tva_tx = $objp->tva_tx;
4318 $this->localtax1_tx = $objp->localtax1_tx;
4319 $this->localtax2_tx = $objp->localtax2_tx;
4320 $this->remise = $objp->remise;
4321 $this->remise_percent = $objp->remise_percent;
4322 $this->fk_remise_except = $objp->fk_remise_except;
4323 $this->fk_product = $objp->fk_product;
4324 $this->product_type = $objp->product_type;
4325 $this->info_bits = $objp->info_bits;
4326 $this->special_code = $objp->special_code;
4327 $this->total_ht = $objp->total_ht;
4328 $this->total_tva = $objp->total_tva;
4329 $this->total_localtax1 = $objp->total_localtax1;
4330 $this->total_localtax2 = $objp->total_localtax2;
4331 $this->total_ttc = $objp->total_ttc;
4332 $this->fk_fournprice = $objp->fk_fournprice;
4333 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
4334 $this->pa_ht = $marginInfos[0];
4335 $this->marge_tx = $marginInfos[1];
4336 $this->marque_tx = $marginInfos[2];
4337 $this->special_code = $objp->special_code;
4338 $this->rang = $objp->rang;
4340 $this->
ref = $objp->product_ref;
4342 $this->product_ref = $objp->product_ref;
4343 $this->product_label = $objp->product_label;
4344 $this->product_desc = $objp->product_desc;
4345 $this->product_tobatch = $objp->product_tobatch;
4346 $this->fk_unit = $objp->fk_unit;
4348 $this->date_start = $this->db->jdate($objp->date_start);
4349 $this->date_end = $this->db->jdate($objp->date_end);
4351 $this->fk_multicurrency = $objp->fk_multicurrency;
4352 $this->multicurrency_code = $objp->multicurrency_code;
4353 $this->multicurrency_subprice = $objp->multicurrency_subprice;
4354 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
4355 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
4356 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
4360 $this->db->free($result);
4364 $this->error = $this->db->lasterror();
4376 public function delete(
User $user, $notrigger = 0)
4378 global $conf, $langs;
4382 if (empty($this->
id) && !empty($this->
rowid)) {
4383 $this->
id = $this->rowid;
4387 $sqlCheckShipmentLine =
"SELECT";
4388 $sqlCheckShipmentLine .=
" ed.rowid";
4389 $sqlCheckShipmentLine .=
" FROM " . MAIN_DB_PREFIX .
"expeditiondet ed";
4390 $sqlCheckShipmentLine .=
" WHERE ed.fk_elementdet = " . ((int) $this->
id);
4392 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
4393 if (!$resqlCheckShipmentLine) {
4395 $this->error = $this->db->lasterror();
4396 $this->errors[] = $this->error;
4398 $langs->load(
'errors');
4399 $num = $this->db->num_rows($resqlCheckShipmentLine);
4402 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
4403 $this->error = $langs->trans(
'ErrorRecordAlreadyExists') .
' : ' . $langs->trans(
'ShipmentLine') .
' ' . $objCheckShipmentLine->rowid;
4404 $this->errors[] = $this->error;
4406 $this->db->free($resqlCheckShipmentLine);
4409 dol_syslog(__METHOD__ .
'Error ; ' . $this->error, LOG_ERR);
4417 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
4425 $sql =
'DELETE FROM ' . MAIN_DB_PREFIX .
"commandedet WHERE rowid = " . ((int) $this->
id);
4428 $resql = $this->db->query($sql);
4430 $this->error = $this->db->lasterror();
4440 dol_syslog(get_class($this) .
"::delete error -4 " . $this->error, LOG_ERR);
4445 $this->db->commit();
4449 foreach ($this->errors as $errmsg) {
4450 dol_syslog(get_class($this) .
"::delete " . $errmsg, LOG_ERR);
4451 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
4453 $this->db->rollback();
4464 public function insert($user =
null, $notrigger = 0)
4468 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4470 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
4473 if (empty($this->tva_tx)) {
4476 if (empty($this->localtax1_tx)) {
4477 $this->localtax1_tx = 0;
4479 if (empty($this->localtax2_tx)) {
4480 $this->localtax2_tx = 0;
4482 if (empty($this->localtax1_type)) {
4483 $this->localtax1_type = 0;
4485 if (empty($this->localtax2_type)) {
4486 $this->localtax2_type = 0;
4488 if (empty($this->total_localtax1)) {
4489 $this->total_localtax1 = 0;
4491 if (empty($this->total_localtax2)) {
4492 $this->total_localtax2 = 0;
4494 if (empty($this->rang)) {
4497 if (empty($this->remise_percent)) {
4498 $this->remise_percent = 0;
4500 if (empty($this->info_bits)) {
4501 $this->info_bits = 0;
4503 if (empty($this->special_code)) {
4504 $this->special_code = 0;
4506 if (empty($this->fk_parent_line)) {
4507 $this->fk_parent_line = 0;
4509 if (empty($this->pa_ht)) {
4512 if (empty($this->ref_ext)) {
4513 $this->ref_ext =
'';
4517 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4518 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4522 $this->pa_ht = $result;
4527 if ($this->product_type < 0) {
4534 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'commandedet';
4535 $sql .=
' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
4536 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
4537 $sql .=
' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
4538 $sql .=
' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
4539 $sql .=
' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
4540 $sql .=
' fk_unit,';
4541 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
4543 $sql .=
" VALUES (".$this->fk_commande.
",";
4544 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
4545 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
4546 $sql .=
" '".$this->db->escape($this->desc).
"',";
4547 $sql .=
" '".price2num($this->qty).
"',";
4548 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
4549 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
4550 $sql .=
" '".price2num($this->tva_tx).
"',";
4551 $sql .=
" '".price2num($this->localtax1_tx).
"',";
4552 $sql .=
" '".price2num($this->localtax2_tx).
"',";
4553 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
4554 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
4555 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
4556 $sql .=
" '".$this->db->escape($this->product_type).
"',";
4557 $sql .=
" '".price2num($this->remise_percent).
"',";
4558 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice) :
"null").
",";
4559 $sql .=
" ".($this->price !=
'' ?
"'".price2num($this->
price).
"'" :
"null").
",";
4560 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
4561 $sql .=
' '.((int) $this->special_code).
',';
4562 $sql .=
' '.((int) $this->rang).
',';
4563 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
4564 $sql .=
' '.price2num($this->pa_ht).
',';
4565 $sql .=
" ".((int) $this->info_bits).
",";
4566 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
4567 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
4568 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
4569 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
4570 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
4571 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
4572 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
',';
4573 $sql .=
' '.(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
4574 $sql .=
", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) :
'NULL');
4575 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
4576 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
4577 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
4578 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
4579 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
4582 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
4583 $resql = $this->db->query($sql);
4585 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
4586 $this->
rowid = $this->id;
4595 if (!$error && !$notrigger) {
4597 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
4605 $this->db->commit();
4609 foreach ($this->errors as $errmsg) {
4610 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
4611 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4613 $this->db->rollback();
4616 $this->error = $this->db->error();
4617 $this->db->rollback();
4633 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4636 if (empty($this->tva_tx)) {
4639 if (empty($this->localtax1_tx)) {
4640 $this->localtax1_tx = 0;
4642 if (empty($this->localtax2_tx)) {
4643 $this->localtax2_tx = 0;
4645 if (empty($this->localtax1_type)) {
4646 $this->localtax1_type = 0;
4648 if (empty($this->localtax2_type)) {
4649 $this->localtax2_type = 0;
4651 if (empty($this->qty)) {
4654 if (empty($this->total_localtax1)) {
4655 $this->total_localtax1 = 0;
4657 if (empty($this->total_localtax2)) {
4658 $this->total_localtax2 = 0;
4660 if (empty($this->marque_tx)) {
4661 $this->marque_tx = 0;
4663 if (empty($this->marge_tx)) {
4664 $this->marge_tx = 0;
4666 if (empty($this->remise_percent)) {
4667 $this->remise_percent = 0;
4669 if (empty($this->remise)) {
4672 if (empty($this->info_bits)) {
4673 $this->info_bits = 0;
4675 if (empty($this->special_code)) {
4676 $this->special_code = 0;
4678 if (empty($this->product_type)) {
4679 $this->product_type = 0;
4681 if (empty($this->fk_parent_line)) {
4682 $this->fk_parent_line = 0;
4684 if (empty($this->pa_ht)) {
4687 if (empty($this->ref_ext)) {
4688 $this->ref_ext =
'';
4692 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4693 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4697 $this->pa_ht = $result;
4704 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4705 $sql .=
" description='".$this->db->escape($this->desc).
"'";
4706 $sql .=
" , label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
4707 $sql .=
" , vat_src_code=".(!empty($this->vat_src_code) ?
"'".$this->db->escape($this->vat_src_code).
"'" :
"''");
4708 $sql .=
" , tva_tx=".price2num($this->tva_tx);
4709 $sql .=
" , localtax1_tx=".price2num($this->localtax1_tx);
4710 $sql .=
" , localtax2_tx=".price2num($this->localtax2_tx);
4711 $sql .=
" , localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
4712 $sql .=
" , localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
4713 $sql .=
" , qty=".price2num($this->qty);
4714 $sql .=
" , ref_ext='".$this->db->escape($this->ref_ext).
"'";
4715 $sql .=
" , subprice=".price2num($this->subprice);
4716 $sql .=
" , remise_percent=".price2num($this->remise_percent);
4717 $sql .=
" , price=".price2num($this->
price);
4718 $sql .=
" , remise=".price2num($this->remise);
4719 if (empty($this->skip_update_total)) {
4720 $sql .=
" , total_ht=".price2num($this->total_ht);
4721 $sql .=
" , total_tva=".price2num($this->total_tva);
4722 $sql .=
" , total_ttc=".price2num($this->total_ttc);
4723 $sql .=
" , total_localtax1=".price2num($this->total_localtax1);
4724 $sql .=
" , total_localtax2=".price2num($this->total_localtax2);
4726 $sql .=
" , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null");
4727 $sql .=
" , buy_price_ht='".price2num($this->pa_ht).
"'";
4728 $sql .=
" , info_bits=".((int) $this->info_bits);
4729 $sql .=
" , special_code=".((int) $this->special_code);
4730 $sql .=
" , date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
4731 $sql .=
" , date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
4732 $sql .=
" , product_type=".$this->product_type;
4733 $sql .=
" , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line :
"null");
4734 if (!empty($this->rang)) {
4735 $sql .=
", rang=".((int) $this->rang);
4737 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
4740 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice);
4741 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
4742 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
4743 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
4745 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4747 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
4748 $resql = $this->db->query($sql);
4751 $this->
id = $this->rowid;
4758 if (!$error && !$notrigger) {
4760 $result = $this->
call_trigger(
'LINEORDER_MODIFY', $user);
4768 $this->db->commit();
4772 foreach ($this->errors as $errmsg) {
4773 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
4774 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4776 $this->db->rollback();
4779 $this->error = $this->db->error();
4780 $this->db->rollback();
4798 if (empty($this->total_localtax1)) {
4799 $this->total_localtax1 = 0;
4801 if (empty($this->total_localtax2)) {
4802 $this->total_localtax2 = 0;
4806 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4807 $sql .=
" total_ht='".price2num($this->total_ht).
"'";
4808 $sql .=
",total_tva='".price2num($this->total_tva).
"'";
4809 $sql .=
",total_localtax1='".price2num($this->total_localtax1).
"'";
4810 $sql .=
",total_localtax2='".price2num($this->total_localtax2).
"'";
4811 $sql .=
",total_ttc='".price2num($this->total_ttc).
"'";
4812 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4814 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
4816 $resql = $this->db->query($sql);
4818 $this->db->commit();
4821 $this->error = $this->db->error();
4822 $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