42require_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
43require_once DOL_DOCUMENT_ROOT.
'/commande/class/orderline.class.php';
44require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
45require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
46require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
47require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
48require_once DOL_DOCUMENT_ROOT.
'/subtotals/class/commonsubtotal.class.php';
61 public $element =
'commande';
67 public $TRIGGER_PREFIX =
'ORDER';
72 public $table_element =
'commande';
77 public $table_element_line =
'commandedet';
82 public $class_element_line =
'OrderLine';
87 public $fk_element =
'fk_commande';
92 public $picto =
'order';
98 public $restrictiononfksoc = 1;
123 public $ref_customer;
151 public $date_lim_reglement;
155 public $cond_reglement_code;
160 public $cond_reglement_doc;
165 public $cond_reglement_label;
171 public $deposit_percent;
181 public $mode_reglement;
186 public $mode_reglement_id;
191 public $mode_reglement_code;
197 public $availability_id;
203 public $availability_code;
209 public $availability;
214 public $demand_reason_id;
219 public $demand_reason_code;
231 public $date_commande;
236 public $delivery_date;
241 public $fk_remise_except;
247 public $remise_percent;
258 public $signed_status = 0;
263 public $warehouse_id;
268 public $extraparams = array();
273 public $user_author_id;
283 public $lines = array();
289 public $module_source;
303 public $online_payment_url;
336 public $fields = array(
337 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 10),
338 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'default' =>
'1',
'enabled' => 1,
'visible' => -2,
'notnull' => 1,
'position' => 20,
'index' => 1),
339 'ref' => array(
'type' =>
'varchar(30)',
'label' =>
'Ref',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'showoncombobox' => 1,
'position' => 25),
340 'ref_ext' => array(
'type' =>
'varchar(255)',
'label' =>
'RefExt',
'enabled' => 1,
'visible' => 0,
'position' => 26),
341 'ref_client' => array(
'type' =>
'varchar(255)',
'label' =>
'RefCustomer',
'enabled' => 1,
'visible' => -1,
'position' => 28,
'uieditable' => 1,
'alwayseditable' => 1),
342 'fk_soc' => array(
'type' =>
'integer:Societe:societe/class/societe.class.php',
'label' =>
'ThirdParty',
'enabled' =>
'isModEnabled("societe")',
'visible' => -1,
'notnull' => 1,
'position' => 20),
343 'fk_projet' => array(
'type' =>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label' =>
'Project',
'enabled' =>
"isModEnabled('project')",
'visible' => -1,
'position' => 25),
344 'date_commande' => array(
'type' =>
'date',
'label' =>
'Date',
'enabled' => 1,
'visible' => 1,
'position' => 60,
'csslist' =>
'nowraponall'),
345 'date_valid' => array(
'type' =>
'datetime',
'label' =>
'DateValidation',
'enabled' => 1,
'visible' => -1,
'position' => 62,
'csslist' =>
'nowraponall'),
346 'date_cloture' => array(
'type' =>
'datetime',
'label' =>
'DateClosing',
'enabled' => 1,
'visible' => -1,
'position' => 65,
'csslist' =>
'nowraponall'),
347 'fk_user_valid' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserValidation',
'enabled' => 1,
'visible' => -1,
'position' => 85),
348 'fk_user_cloture' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserClosing',
'enabled' => 1,
'visible' => -1,
'position' => 90),
349 'source' => array(
'type' =>
'smallint(6)',
'label' =>
'Source',
'enabled' => 1,
'visible' => -1,
'position' => 95),
350 'total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'VAT',
'enabled' => 1,
'visible' => -1,
'position' => 125,
'isameasure' => 1),
351 'localtax1' => array(
'type' =>
'double(24,8)',
'label' =>
'LocalTax1',
'enabled' => 1,
'visible' => -1,
'position' => 130,
'isameasure' => 1),
352 'localtax2' => array(
'type' =>
'double(24,8)',
'label' =>
'LocalTax2',
'enabled' => 1,
'visible' => -1,
'position' => 135,
'isameasure' => 1),
353 'total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalHT',
'enabled' => 1,
'visible' => -1,
'position' => 140,
'isameasure' => 1),
354 'total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalTTC',
'enabled' => 1,
'visible' => -1,
'position' => 145,
'isameasure' => 1),
355 'signed_status' => array(
'type' =>
'smallint(6)',
'label' =>
'SignedStatus',
'enabled' => 1,
'visible' => -1,
'position' => 146,
'arrayofkeyval' => array(0 =>
'NoSignature', 1 =>
'SignedSender', 2 =>
'SignedReceiver', 9 =>
'SignedAll')),
356 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => 0,
'position' => 150,
'uieditable' => 1,
'alwayseditable' => 1),
357 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => 0,
'position' => 155,
'uieditable' => 1,
'alwayseditable' => 1),
358 'model_pdf' => array(
'type' =>
'varchar(255)',
'label' =>
'PDFTemplate',
'enabled' => 1,
'visible' => 0,
'position' => 160),
359 'fk_account' => array(
'type' =>
'integer',
'label' =>
'BankAccount',
'enabled' =>
'isModEnabled("bank")',
'visible' => -1,
'position' => 170),
360 'fk_currency' => array(
'type' =>
'varchar(3)',
'label' =>
'MulticurrencyID',
'enabled' => 1,
'visible' => -1,
'position' => 175),
361 'fk_cond_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentTerm',
'enabled' => 1,
'visible' => -1,
'position' => 180),
362 'deposit_percent' => array(
'type' =>
'varchar(63)',
'label' =>
'DepositPercent',
'enabled' => 1,
'visible' => -1,
'position' => 181),
363 'fk_mode_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentMode',
'enabled' => 1,
'visible' => -1,
'position' => 185),
364 'date_livraison' => array(
'type' =>
'date',
'label' =>
'DateDeliveryPlanned',
'enabled' => 1,
'visible' => -1,
'position' => 190,
'csslist' =>
'nowraponall'),
365 'fk_shipping_method' => array(
'type' =>
'integer',
'label' =>
'ShippingMethod',
'enabled' => 1,
'visible' => -1,
'position' => 195),
366 'fk_warehouse' => array(
'type' =>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label' =>
'DefaultWarehouse',
'enabled' =>
'isModEnabled("stock")',
'visible' => -1,
'position' => 200,
'nodepth' => 1),
367 'fk_availability' => array(
'type' =>
'integer',
'label' =>
'Availability',
'enabled' => 1,
'visible' => -1,
'position' => 205),
368 'fk_input_reason' => array(
'type' =>
'integer',
'label' =>
'InputReason',
'enabled' => 1,
'visible' => -1,
'position' => 210),
370 'extraparams' => array(
'type' =>
'varchar(255)',
'label' =>
'Extraparams',
'enabled' => 1,
'visible' => -1,
'position' => 225),
371 'fk_incoterms' => array(
'type' =>
'integer',
'label' =>
'IncotermCode',
'enabled' =>
'isModEnabled("incoterm")',
'visible' => -1,
'position' => 230),
372 'location_incoterms' => array(
'type' =>
'varchar(255)',
'label' =>
'IncotermLabel',
'enabled' =>
'isModEnabled("incoterm")',
'visible' => -1,
'position' => 235),
373 'fk_multicurrency' => array(
'type' =>
'integer',
'label' =>
'Currency',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 240),
374 'multicurrency_code' => array(
'type' =>
'varchar(255)',
'label' =>
'MulticurrencyCurrency',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 245),
375 'multicurrency_tx' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyRate',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 250,
'isameasure' => 1),
376 'multicurrency_total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountHT',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 255,
'isameasure' => 1),
377 'multicurrency_total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountVAT',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 260,
'isameasure' => 1),
378 'multicurrency_total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountTTC',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 265,
'isameasure' => 1),
379 'last_main_doc' => array(
'type' =>
'varchar(255)',
'label' =>
'LastMainDoc',
'enabled' => 1,
'visible' => -1,
'position' => 270),
380 'module_source' => array(
'type' =>
'varchar(32)',
'label' =>
'POSModule',
'enabled' => 1,
'visible' => -1,
'position' => 275),
381 'pos_source' => array(
'type' =>
'varchar(32)',
'label' =>
'POSTerminal',
'enabled' => 1,
'visible' => -1,
'position' => 280),
382 'fk_user_author' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserAuthor',
'enabled' => 1,
'visible' => -1,
'position' => 300),
383 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' => 1,
'visible' => -2,
'notnull' => -1,
'position' => 302),
384 'date_creation' => array(
'type' =>
'datetime',
'label' =>
'DateCreation',
'enabled' => 1,
'visible' => -2,
'position' => 304,
'csslist' =>
'nowraponall'),
385 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 306),
386 'import_key' => array(
'type' =>
'varchar(14)',
'label' =>
'ImportId',
'enabled' => 1,
'visible' => -2,
'position' => 400),
387 'fk_statut' => array(
'type' =>
'smallint(6)',
'label' =>
'Status',
'enabled' => 1,
'visible' => -1,
'position' => 500),
427 const STATUS_NO_SIGNATURE = 0;
432 const STATUS_SIGNED_SENDER = 1;
437 const STATUS_SIGNED_RECEIVER = 2;
442 const STATUS_SIGNED_ALL = 9;
454 $this->ismultientitymanaged = 1;
455 $this->isextrafieldmanaged = 1;
457 $this->fields[
'ref_ext'][
'visible'] =
getDolGlobalInt(
'MAIN_LIST_SHOW_REF_EXT');
470 if ($field ==
'ref_client') {
474 return parent::persistFieldValue($user, $field);
484 public function getNextNumRef($soc)
486 global $langs,
$conf;
487 $langs->load(
"order");
496 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
497 foreach ($dirmodels as $reldir) {
501 $mybool = ((bool) @include_once $dir.$file) || $mybool;
505 dol_print_error(
null,
"Failed to include file ".$file);
509 $obj =
new $classname();
511 '@phan-var-force ModeleNumRefCommandes $obj';
513 $numref = $obj->getNextValue($soc, $this);
518 $this->error = $obj->error;
523 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_COMMANDE_ADDON_NotDefined");
537 public function valid($user, $idwarehouse = 0, $notrigger = 0)
539 global
$conf, $langs;
541 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
546 if ($this->
status == self::STATUS_VALIDATED) {
547 dol_syslog(get_class($this).
"::valid action abandoned: already validated", LOG_WARNING);
551 if (!((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
552 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'validate')))) {
553 $this->error =
'NotEnoughPermissions';
554 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
557 if (empty($this->socid)) {
558 $this->error =
'ErrorWrongParameters';
562 dol_syslog(get_class($this).
"::valid checkActiveProductInLines ".$this->error, LOG_INFO);
571 $soc->fetch($this->socid);
574 $result = $soc->setAsCustomer();
577 if (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
578 $num = $this->getNextNumRef($soc);
580 $num = (string) $this->
ref;
585 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
586 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
587 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
588 $sql .=
" date_valid = '".$this->db->idate($now).
"',";
589 $sql .=
" fk_user_valid = ".($user->id > 0 ? (int) $user->id :
"null").
",";
590 $sql .=
" fk_user_modif = ".((int) $user->id);
591 $sql .=
" WHERE rowid = ".((int) $this->
id);
593 dol_syslog(get_class($this).
"::valid", LOG_DEBUG);
594 $resql = $this->db->query($sql);
596 dol_print_error($this->db);
597 $this->error = $this->db->lasterror();
604 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
605 $langs->load(
"agenda");
608 $cpt = count($this->lines);
609 for ($i = 0; $i < $cpt; $i++) {
610 if ($this->lines[$i]->fk_product > 0) {
612 $mouvP->origin = &$this;
613 $mouvP->setOrigin($this->element, $this->
id);
615 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->transnoentitiesnoconv(
"OrderValidatedInDolibarr", $num));
628 if (!$error && !$notrigger) {
630 $result = $this->call_trigger(
'ORDER_VALIDATE', $user);
638 $this->oldref = $this->ref;
641 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
643 $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).
"'";
644 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'commande/".$this->db->escape($this->
ref).
"' and entity = ".((int)
$conf->entity);
645 $resql = $this->db->query($sql);
648 $this->error = $this->db->lasterror();
650 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filepath = 'commande/".$this->db->escape($this->newref).
"'";
651 $sql .=
" WHERE filepath = 'commande/".$this->db->escape($this->
ref).
"' and entity = ".((int)
$conf->entity);
652 $resql = $this->db->query($sql);
655 $this->error = $this->db->lasterror();
663 if (!$error && file_exists($dirsource)) {
664 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
666 if (@rename($dirsource, $dirdest)) {
669 $listoffiles =
dol_dir_list($dirdest,
'files', 1,
'^'.preg_quote($oldref,
'/'));
670 foreach ($listoffiles as $fileentry) {
671 $dirsource = $fileentry[
'name'];
672 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
673 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
674 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
675 @rename($dirsource, $dirdest);
693 $this->db->rollback();
718 if (!((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
719 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'validate')))) {
720 $this->error =
'Permission denied';
728 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
729 $sql .=
" SET fk_statut = ".((int) self::STATUS_DRAFT).
",";
730 $sql .=
" fk_user_modif = ".((int) $user->id);
731 $sql .=
" WHERE rowid = ".((int) $this->
id);
733 if ($this->db->query($sql)) {
734 $this->oldcopy = clone $this;
740 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
741 $langs->load(
"agenda");
743 $num = count($this->lines);
744 for ($i = 0; $i < $num; $i++) {
745 if ($this->lines[$i]->fk_product > 0) {
747 $mouvP->origin = &$this;
748 $mouvP->setOrigin($this->element, $this->
id);
750 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->transnoentitiesnoconv(
"OrderBackToDraftInDolibarr", $this->ref));
762 $result = $this->call_trigger(
'ORDER_UNVALIDATE', $user);
774 $this->db->rollback();
778 $this->error = $this->db->error();
779 $this->db->rollback();
798 if ($this->
status != self::STATUS_CANCELED && $this->
status != self::STATUS_CLOSED) {
799 dol_syslog(get_class($this).
"::set_reopen order has not status closed", LOG_WARNING);
805 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
806 $sql .=
' SET fk_statut='.self::STATUS_VALIDATED.
', facture=0,';
807 $sql .=
" fk_user_modif = ".((int) $user->id);
808 $sql .=
" WHERE rowid = ".((int) $this->
id);
810 dol_syslog(get_class($this).
"::set_reopen", LOG_DEBUG);
811 $resql = $this->db->query($sql);
814 $result = $this->call_trigger(
'ORDER_REOPEN', $user);
821 $this->error = $this->db->lasterror();
822 dol_print_error($this->db);
833 foreach ($this->errors as $errmsg) {
834 dol_syslog(get_class($this).
"::set_reopen ".$errmsg, LOG_ERR);
835 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
837 $this->db->rollback();
850 public function cloture($user, $notrigger = 0, $checkpermission = 1)
854 $usercanclose = (!$checkpermission
855 || (!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
856 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'close')));
859 if ($this->
status == self::STATUS_CLOSED) {
866 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
867 $sql .=
' SET fk_statut = '.self::STATUS_CLOSED.
',';
868 $sql .=
' fk_user_cloture = '.((int) $user->id).
',';
869 $sql .=
" date_cloture = '".$this->db->idate($now).
"',";
870 $sql .=
" fk_user_modif = ".((int) $user->id);
873 if ($this->db->query($sql)) {
876 $result = $this->call_trigger(
'ORDER_CLOSE', $user);
890 $this->db->rollback();
894 $this->error = $this->db->lasterror();
896 $this->db->rollback();
916 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
917 return parent::setCategoriesCommon($categories, Categorie::TYPE_ORDER);
928 public function cancel($user, $idwarehouse = -1)
930 global $user, $langs;
936 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
937 $sql .=
" SET fk_statut = ".self::STATUS_CANCELED.
",";
938 $sql .=
" fk_user_modif = ".((int) $user->id);
939 $sql .=
" WHERE rowid = ".((int) $this->
id);
940 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
942 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
943 if ($this->db->query($sql)) {
946 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
947 $langs->load(
"agenda");
949 $num = count($this->lines);
950 for ($i = 0; $i < $num; $i++) {
951 if ($this->lines[$i]->fk_product > 0) {
953 $mouvP->setOrigin($this->element, $this->
id);
955 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->transnoentitiesnoconv(
"OrderCanceledInDolibarr", $this->ref));
967 $result = $this->call_trigger(
'ORDER_CANCEL', $user);
980 foreach ($this->errors as $errmsg) {
981 dol_syslog(get_class($this).
"::cancel ".$errmsg, LOG_ERR);
982 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
984 $this->db->rollback();
988 $this->error = $this->db->error();
989 $this->db->rollback();
1002 public function create($user, $notrigger = 0)
1009 if (empty($this->socid)) {
1010 $this->error =
'ErrorWrongParameters';
1015 $date = ($this->date_commande ? $this->date_commande : $this->date);
1016 $this->import_key = trim((
string) $this->import_key);
1017 $delivery_date = $this->delivery_date;
1020 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1025 if (empty($this->fk_multicurrency)) {
1026 $this->multicurrency_code =
$conf->currency;
1027 $this->fk_multicurrency = 0;
1028 $this->multicurrency_tx = 1;
1033 dol_syslog(get_class($this).
"::create user=".$user->id);
1036 if (!empty($this->
ref)) {
1039 $this->error =
'ErrorRefAlreadyExists';
1040 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
1041 $this->db->rollback();
1050 $soc =
new Societe($this->db);
1051 $result = $soc->fetch($this->socid);
1053 $this->error =
"Failed to fetch company";
1054 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
1058 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Source"));
1059 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
1063 $this->date_creation =
dol_now();
1067 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (";
1068 $sql .=
" ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client";
1069 $sql .=
", model_pdf, fk_cond_reglement, deposit_percent, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address";
1070 $sql .=
", fk_shipping_method";
1071 $sql .=
", fk_warehouse";
1072 $sql .=
", fk_incoterms, location_incoterms";
1073 $sql .=
", entity, module_source, pos_source";
1074 $sql .=
", fk_multicurrency";
1075 $sql .=
", multicurrency_code";
1076 $sql .=
", multicurrency_tx";
1077 $sql .=
", import_key";
1079 $sql .=
" VALUES ('(PROV)', ".((int) $this->socid).
", '".$this->db->idate($this->date_creation).
"', ".($user->id > 0 ? ((int) $user->id) :
"null");
1080 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
1081 $sql .=
", '".$this->db->idate($date).
"'";
1082 $sql .=
", ".($this->source >= 0 && $this->source !=
'' ? $this->db->escape((
string) $this->source) :
'null');
1083 $sql .=
", '".$this->db->escape((
string) $this->note_private).
"'";
1084 $sql .=
", '".$this->db->escape((
string) $this->note_public).
"'";
1085 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
1086 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
1087 $sql .=
", '".$this->db->escape((
string) $this->model_pdf).
"'";
1088 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
1089 $sql .=
", ".(!empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
"null");
1090 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
1091 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
1092 $sql .=
", ".($this->availability_id > 0 ? ((int) $this->availability_id) :
"null");
1093 $sql .=
", ".($this->demand_reason_id > 0 ? ((int) $this->demand_reason_id) :
"null");
1094 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
1095 $sql .=
", ".($this->fk_delivery_address > 0 ? ((int) $this->fk_delivery_address) :
'NULL');
1096 $sql .=
", ".(!empty($this->shipping_method_id) && $this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) :
'NULL');
1097 $sql .=
", ".(!empty($this->warehouse_id) && $this->warehouse_id > 0 ? ((int) $this->warehouse_id) :
'NULL');
1098 $sql .=
", ".(int) $this->fk_incoterms;
1099 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
1100 $sql .=
", ".(int) $this->entity;
1101 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
1102 $sql .=
", ".((!is_null($this->pos_source) && $this->pos_source !=
'') ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
1103 $sql .=
", ".(int) $this->fk_multicurrency;
1104 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1105 $sql .=
", ".(float) $this->multicurrency_tx;
1106 $sql .=
", '".$this->db->escape($this->import_key).
"'";
1109 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1110 $resql = $this->db->query($sql);
1112 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commande');
1115 $fk_parent_line = 0;
1116 $num = count($this->lines);
1121 for ($i = 0; $i < $num; $i++) {
1122 $line = $this->lines[$i];
1126 if (!is_object($line)) {
1128 foreach ($line as $key => $val) {
1129 $lineobj->$key = $val;
1132 $this->lines[$i] = $line;
1136 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1137 $fk_parent_line = 0;
1141 $vatrate = $line->tva_tx;
1142 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', (
string) $vatrate)) {
1143 $vatrate .=
' ('.$line->vat_src_code.
')';
1147 $originid = $line->origin_id;
1148 $origintype = empty($line->origin_type) ? $line->origin : $line->origin_type;
1150 $originid = $line->id;
1151 $origintype = $this->element;
1155 if (empty($line->ref_ext)) {
1156 $line->ref_ext =
'';
1160 $line_price_base_type = $line->getPriceBaseType();
1163 (
float) $line->subprice,
1166 $line->localtax1_tx,
1167 $line->localtax2_tx,
1169 $line->remise_percent,
1171 $line->fk_remise_except,
1172 $line_price_base_type,
1173 (
float) $line->subprice_ttc,
1176 $line->product_type,
1178 $line->special_code,
1180 $line->fk_fournprice,
1183 $line->array_options,
1185 (
string) $origintype,
1193 if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) {
1194 $this->error = $this->db->lasterror();
1195 $this->errors[] = $this->error;
1196 dol_print_error($this->db);
1198 $this->db->rollback();
1204 if ($result > 0 && !empty($line->extraparams)) {
1206 $newline->id = $result;
1207 $newline->extraparams = $line->extraparams;
1208 $newline->setExtraParameters();
1212 if ($result > 0 && $line->product_type == 9) {
1213 $fk_parent_line = $result;
1220 $initialref =
'(PROV'.$this->id.
')';
1221 if (!empty($this->
ref)) {
1222 $initialref = $this->ref;
1225 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
" SET ref='".$this->db->escape($initialref).
"' WHERE rowid=".((int) $this->
id);
1226 if ($this->db->query($sql)) {
1227 $this->
ref = $initialref;
1229 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1230 $this->linked_objects = $this->linkedObjectsIds;
1234 if (!empty($this->linked_objects) && is_array($this->linked_objects)) {
1235 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1236 if (is_array($tmp_origin_id)) {
1237 foreach ($tmp_origin_id as $origin_id) {
1240 $this->error = $this->db->lasterror();
1245 $origin_id = $tmp_origin_id;
1248 $this->error = $this->db->lasterror();
1255 if (!$error &&
getDolGlobalString(
'MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) {
1256 $originforcontact = empty($this->origin_type) ? $this->origin : $this->origin_type;
1257 $originidforcontact = $this->origin_id;
1258 if ($originforcontact ==
'shipping') {
1259 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1261 $exp->fetch($this->origin_id);
1262 $exp->fetchObjectLinked();
1263 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1264 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1265 $originforcontact =
'commande';
1266 if (is_object($value)) {
1267 $originidforcontact = $value->id;
1269 $originidforcontact = $value;
1276 $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";
1277 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact).
"'";
1279 $resqlcontact = $this->db->query($sqlcontact);
1280 if ($resqlcontact) {
1281 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1283 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1286 dol_print_error($this->db);
1297 if (!$error && !$notrigger) {
1299 $result = $this->call_trigger(
'ORDER_CREATE', $user);
1307 $this->db->commit();
1310 $this->db->rollback();
1314 $this->error = $this->db->lasterror();
1315 $this->db->rollback();
1322 $this->error = $this->db->lasterror();
1323 $this->db->rollback();
1339 global $hookmanager;
1343 $object =
new self($this->db);
1353 if (!empty($socid) && $socid !=
$object->socid) {
1354 $objsoc =
new Societe($this->db);
1356 if ($objsoc->fetch($socid) > 0) {
1358 $object->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1359 $object->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent :
'0');
1360 $object->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1362 $object->fk_delivery_address = 0;
1368 $object->entity = (!empty($forceentity) ? $forceentity :
$object->entity);
1376 $object->user_author_id = $user->id;
1377 $object->user_validation_id = 0;
1381 $object->date_validation =
'';
1389 for ($i = 0; $i < $num; $i++) {
1390 $object->lines[$i]->ref_ext =
'';
1394 $object->context[
'createfromclone'] =
'createfromclone';
1395 $object->context[
'createfromclone_fromid'] = $objFrom->id;
1396 $object->context[
'createfromclone_fromref'] = $objFrom->ref;
1397 $result =
$object->create($user);
1405 if (
$object->copy_linked_contact($objFrom,
'internal') < 0) {
1412 if (
$object->socid == $objFrom->socid) {
1413 if (
$object->copy_linked_contact($objFrom,
'external') < 0) {
1421 if (is_object($hookmanager)) {
1422 $parameters = array(
'objFrom' => $objFrom,
'clonedObj' =>
$object);
1424 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters,
$object, $action);
1432 unset(
$object->context[
'createfromclone']);
1433 unset(
$object->context[
'createfromclone_fromid']);
1434 unset(
$object->context[
'createfromclone_fromref']);
1438 $this->db->commit();
1441 $this->db->rollback();
1456 global
$conf, $hookmanager, $langs;
1458 require_once DOL_DOCUMENT_ROOT .
'/multicurrency/class/multicurrency.class.php';
1459 require_once DOL_DOCUMENT_ROOT .
'/core/class/extrafields.class.php';
1463 $this->date_commande =
dol_now();
1468 for ($i = 0; $i < $num; $i++) {
1471 $line->libelle =
$object->lines[$i]->libelle;
1472 $line->label =
$object->lines[$i]->label;
1473 $line->desc =
$object->lines[$i]->desc;
1474 $line->price =
$object->lines[$i]->price;
1475 $line->subprice =
$object->lines[$i]->subprice;
1476 $line->subprice_ttc =
$object->lines[$i]->subprice_ttc;
1477 $line->vat_src_code =
$object->lines[$i]->vat_src_code;
1478 $line->tva_tx =
$object->lines[$i]->tva_tx;
1479 $line->localtax1_tx =
$object->lines[$i]->localtax1_tx;
1480 $line->localtax2_tx =
$object->lines[$i]->localtax2_tx;
1481 $line->qty =
$object->lines[$i]->qty;
1482 $line->fk_remise_except =
$object->lines[$i]->fk_remise_except;
1483 $line->remise_percent =
$object->lines[$i]->remise_percent;
1484 $line->fk_product =
$object->lines[$i]->fk_product;
1485 $line->info_bits =
$object->lines[$i]->info_bits;
1486 $line->product_type =
$object->lines[$i]->product_type;
1487 $line->rang =
$object->lines[$i]->rang;
1488 $line->special_code =
$object->lines[$i]->special_code;
1489 $line->fk_parent_line =
$object->lines[$i]->fk_parent_line;
1490 $line->fk_unit =
$object->lines[$i]->fk_unit;
1492 $line->date_start =
$object->lines[$i]->date_start;
1493 $line->date_end =
$object->lines[$i]->date_end;
1495 $line->fk_fournprice =
$object->lines[$i]->fk_fournprice;
1497 $line->pa_ht = $marginInfos[0];
1498 $line->marge_tx = $marginInfos[1];
1499 $line->marque_tx = $marginInfos[2];
1501 $line->origin =
$object->element;
1502 $line->origin_id =
$object->lines[$i]->id;
1505 $object->lines[$i]->fetch_optionals();
1506 foreach (
$object->lines[$i]->array_options as $options_key => $value) {
1507 $line->array_options[$options_key] = $value;
1510 $this->lines[$i] = $line;
1513 $this->entity =
$object->entity;
1514 $this->socid =
$object->socid;
1515 $this->fk_project =
$object->fk_project;
1516 $this->cond_reglement_id =
$object->cond_reglement_id;
1517 $this->deposit_percent =
$object->deposit_percent;
1518 $this->mode_reglement_id =
$object->mode_reglement_id;
1519 $this->fk_account =
$object->fk_account;
1520 $this->fk_incoterms =
$object->fk_incoterms;
1521 $this->location_incoterms =
$object->location_incoterms;
1522 $this->availability_id =
$object->availability_id;
1523 $this->demand_reason_id =
$object->demand_reason_id;
1524 $this->delivery_date =
$object->delivery_date;
1525 $this->shipping_method_id =
$object->shipping_method_id;
1526 $this->warehouse_id =
$object->warehouse_id;
1527 $this->fk_delivery_address =
$object->fk_delivery_address;
1528 $this->contact_id =
$object->contact_id;
1529 $this->ref_client =
$object->ref_client;
1530 $this->ref_customer =
$object->ref_client;
1533 $this->note_private =
$object->note_private;
1534 $this->note_public =
$object->note_public;
1537 $this->origin =
$object->element;
1538 $this->origin_id =
$object->id;
1542 if (!empty(
$object->multicurrency_code)) {
1543 $this->multicurrency_code =
$object->multicurrency_code;
1546 $this->multicurrency_tx =
$object->multicurrency_tx;
1549 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1551 $this->fk_multicurrency = $tmparray[0];
1552 $this->multicurrency_tx = $tmparray[1];
1556 if (empty($this->fk_multicurrency)) {
1557 $this->multicurrency_code =
$conf->currency;
1558 $this->fk_multicurrency = 0;
1559 $this->multicurrency_tx = 1;
1567 $element_extrafields = $e->fetch_name_optionals_label($this->table_element);
1569 foreach (
$object->array_options as $options_key => $value) {
1570 if (array_key_exists(str_replace(
'options_',
'', $options_key), $element_extrafields)) {
1571 $this->array_options[$options_key] = $value;
1575 $this->linked_objects[$this->origin] = $this->origin_id;
1576 if (isset(
$object->other_linked_objects) && is_array(
$object->other_linked_objects) && !empty(
$object->other_linked_objects)) {
1577 $this->linked_objects = array_merge($this->linked_objects,
$object->other_linked_objects);
1580 $ret = $this->
create($user);
1584 $hookmanager->initHooks(array(
'orderdao'));
1586 $parameters = array(
'objFrom' =>
$object);
1588 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1598 $result = $this->
valid($user);
1600 $this->
fetch($this->
id);
1603 $outputlangs = $langs;
1604 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($this->thirdparty->default_lang)) {
1606 $outputlangs->setDefaultLang($this->thirdparty->default_lang);
1607 $outputlangs->loadLangs(array(
'main',
'orders'));
1662 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)
1664 global
$mysoc, $langs, $user;
1666 $logtext =
"::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent";
1667 $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";
1668 $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 rang=$rang";
1669 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
1671 if ($this->
status == self::STATUS_DRAFT) {
1672 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1676 if (empty($remise_percent)) {
1677 $remise_percent = 0;
1682 if (empty($info_bits)) {
1688 if (empty($txtva)) {
1691 if (empty($txlocaltax1)) {
1694 if (empty($txlocaltax2)) {
1697 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1698 $fk_parent_line = 0;
1700 if (empty($this->fk_multicurrency)) {
1701 $this->fk_multicurrency = 0;
1703 if (empty($ref_ext)) {
1707 $remise_percent = (float)
price2num($remise_percent);
1710 $pu_ht_devise =
price2num($pu_ht_devise);
1713 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
1718 if ($price_base_type ==
'HT') {
1723 $label = trim((
string) $label);
1724 $desc = trim((
string) $desc);
1732 $langs->load(
"errors");
1733 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1739 $product_type = $type;
1740 if (!empty($fk_product) && $fk_product > 0) {
1741 $product =
new Product($this->db);
1742 $result = $product->fetch($fk_product);
1743 $product_type = $product->type;
1745 if (
getDolGlobalString(
'STOCK_MUST_BE_ENOUGH_FOR_ORDER') && $product->isStockManaged()) {
1747 $productChildrenNb = 0;
1749 $productChildrenNb = $product->hasFatherOrChild(1);
1751 if ($productChildrenNb > 0) {
1753 $product_stock =
null;
1754 $product->loadStockForVirtualProduct(
'warehouseopen', $qty);
1755 foreach ($product->stock_warehouse as $componentStockWarehouse) {
1756 if ($product_stock ===
null) {
1757 $product_stock = $componentStockWarehouse->real;
1759 $product_stock = min($product_stock, $componentStockWarehouse->real);
1762 if ($product_stock ===
null) {
1766 $product_stock = $product->stock_reel;
1769 if ($product_stock < $qty) {
1770 $langs->load(
"errors");
1771 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
1772 $this->errors[] = $this->error;
1773 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
1774 $this->db->rollback();
1786 $tmpproduct =
new Product($this->db);
1787 $result = $tmpproduct->fetch($fk_product);
1789 if ($newqty != $qty) {
1791 setEventMessages($langs->trans(
'QtyRecalculatedWithPackaging'),
null,
'warnings');
1798 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1799 $vat_src_code = $reg[1];
1800 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1803 $tabprice =
calcul_price_total($qty, (
float) $pu, $remise_percent, $txtva, (
float) $txlocaltax1, (
float) $txlocaltax2, 0, $price_base_type, $info_bits, $product_type,
$mysoc, $localtaxes_type, 100, $this->multicurrency_tx, (
float) $pu_ht_devise, $this->multicurrency_code);
1805 $total_ht = $tabprice[0];
1806 $total_tva = $tabprice[1];
1807 $total_ttc = $tabprice[2];
1808 $total_localtax1 = $tabprice[9];
1809 $total_localtax2 = $tabprice[10];
1810 $pu_ht = $tabprice[3];
1813 $multicurrency_total_ht = $tabprice[16];
1814 $multicurrency_total_tva = $tabprice[17];
1815 $multicurrency_total_ttc = $tabprice[18];
1816 $pu_ht_devise = $tabprice[19];
1821 if (empty($ranktouse) || $ranktouse == -1) {
1822 $rangmax = $this->
line_max($fk_parent_line);
1823 $ranktouse = $rangmax + 1;
1829 $this->line->context = $this->context;
1831 $this->line->fk_commande = $this->id;
1832 $this->line->label = $label;
1833 $this->line->desc = $desc;
1834 $this->line->qty = (float) $qty;
1835 $this->line->ref_ext = $ref_ext;
1837 $this->line->vat_src_code = $vat_src_code;
1838 $this->line->tva_tx = $txtva;
1839 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1840 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1841 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1842 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1843 $this->line->fk_product = $fk_product;
1844 $this->line->product_type = $product_type;
1845 $this->line->fk_remise_except = $fk_remise_except;
1846 $this->line->remise_percent = $remise_percent;
1847 $this->line->subprice = (float) $pu_ht;
1849 $this->line->subprice_ttc = ($price_base_type ===
'TTC') ? (
float) $pu_ttc : 0;
1850 $this->line->rang = $ranktouse;
1851 $this->line->info_bits = $info_bits;
1852 $this->line->total_ht = (float) $total_ht;
1853 $this->line->total_tva = (float) $total_tva;
1854 $this->line->total_localtax1 = (float) $total_localtax1;
1855 $this->line->total_localtax2 = (float) $total_localtax2;
1856 $this->line->total_ttc = (float) $total_ttc;
1857 $this->line->special_code = $special_code;
1858 if (empty($qty) && empty($special_code)) {
1859 $this->line->special_code = 3;
1861 $this->line->origin = $origin;
1862 $this->line->origin_type = $origin;
1863 $this->line->origin_id = $origin_id;
1865 $this->line->fk_parent_line = $fk_parent_line;
1866 $this->line->fk_unit = $fk_unit;
1869 $this->line->date_end = $date_end;
1871 $this->line->fk_fournprice = $fk_fournprice;
1872 $this->line->pa_ht = $pa_ht;
1875 $this->line->fk_multicurrency = $this->fk_multicurrency;
1876 $this->line->multicurrency_code = $this->multicurrency_code;
1877 $this->line->multicurrency_subprice = (float) $pu_ht_devise;
1878 $this->line->multicurrency_total_ht = (float) $multicurrency_total_ht;
1879 $this->line->multicurrency_total_tva = (float) $multicurrency_total_tva;
1880 $this->line->multicurrency_total_ttc = (float) $multicurrency_total_ttc;
1882 if (is_array($array_options) && count($array_options) > 0) {
1883 $this->line->array_options = $array_options;
1886 $result = $this->line->insert($user);
1889 if (empty($noupdateafterinsertline)) {
1894 if (!isset($this->context[
'createfromclone'])) {
1895 if (!empty($fk_parent_line)) {
1898 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
1900 foreach ($this->lines as $tmpline) {
1901 if ($tmpline->rang >= $ranktouse) {
1902 if (!empty($tmpline->id)) {
1909 $this->lines[] = $this->line;
1912 foreach ($this->lines as $tmpline) {
1913 if ($tmpline->id == $origin_id && $tmpline->element == $origin) {
1914 $this->line->extraparams = $tmpline->extraparams;
1915 $this->line->setExtraParameters();
1920 $this->db->commit();
1921 return $this->line->id;
1923 $this->db->rollback();
1928 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1929 $this->db->rollback();
1933 dol_syslog(get_class($this).
"::addline status of order must be Draft to allow use of ->addline()", LOG_ERR);
1962 if ($idproduct > 0) {
1963 $prod =
new Product($this->db);
1964 $prod->fetch($idproduct);
1968 if (empty($tva_tx)) {
1978 $price = $prod->multiprices[$this->thirdparty->price_level];
1979 $price_ttc = $prod->multiprices_ttc[$this->thirdparty->price_level];
1981 $price = $prod->price;
1982 $price_ttc = $prod->price_ttc;
1987 $line->context = $this->context;
1989 $line->fk_product = $idproduct;
1990 $line->desc = $prod->description;
1992 $line->subprice = $price;
1993 $line->subprice_ttc = $price_ttc;
1994 $line->remise_percent = $remise_percent;
1995 $line->vat_src_code = $vat_src_code;
1996 $line->tva_tx = $tva_tx;
1997 $line->localtax1_tx = $localtax1_tx;
1998 $line->localtax2_tx = $localtax2_tx;
2000 $line->product_ref = $prod->ref;
2001 $line->product_label = $prod->label;
2002 $line->product_desc = $prod->description;
2003 $line->fk_unit = $prod->fk_unit;
2010 $line->date_end = $date_end;
2013 $this->lines[] = $line;
2045 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
2048 if (empty($id) && empty($ref) && empty($ref_ext)) {
2052 $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 as status';
2053 $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';
2054 $sql .=
', c.fk_account';
2055 $sql .=
', c.date_commande, c.date_valid, c.tms';
2056 $sql .=
', c.date_livraison as delivery_date';
2057 $sql .=
', c.fk_shipping_method';
2058 $sql .=
', c.fk_warehouse';
2059 $sql .=
', c.fk_projet as fk_project, c.source, c.facture as billed';
2060 $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';
2061 $sql .=
', c.fk_incoterms, c.location_incoterms';
2062 $sql .=
", c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc";
2063 $sql .=
", c.module_source, c.pos_source";
2064 $sql .=
", i.libelle as label_incoterms";
2065 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
2066 $sql .=
', cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle, cr.libelle_facture as cond_reglement_libelle_doc';
2067 $sql .=
', ca.code as availability_code, ca.label as availability_label';
2068 $sql .=
', dr.code as demand_reason_code';
2069 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as c';
2070 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON c.fk_cond_reglement = cr.rowid';
2071 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON c.fk_mode_reglement = p.id';
2072 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON c.fk_availability = ca.rowid';
2073 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON c.fk_input_reason = dr.rowid';
2074 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
2077 $sql .=
" WHERE c.rowid = ".((int) $id);
2079 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2083 $sql .=
" AND c.ref = '".$this->db->escape($ref).
"'";
2086 $sql .=
" AND c.ref_ext = '".$this->db->escape($ref_ext).
"'";
2089 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
2090 $result = $this->db->query($sql);
2092 $obj = $this->db->fetch_object($result);
2094 $this->
id = $obj->rowid;
2095 $this->entity = $obj->entity;
2097 $this->
ref = $obj->ref;
2098 $this->ref_client = $obj->ref_client;
2099 $this->ref_customer = $obj->ref_client;
2100 $this->ref_ext = $obj->ref_ext;
2102 $this->socid = $obj->fk_soc;
2103 $this->thirdparty =
null;
2105 $this->fk_project = $obj->fk_project;
2106 $this->project =
null;
2108 $this->statut = $obj->status;
2109 $this->
status = $obj->status;
2111 $this->user_author_id = $obj->fk_user_author;
2112 $this->user_creation_id = $obj->fk_user_author;
2113 $this->user_validation_id = $obj->fk_user_valid;
2114 $this->user_modification_id = $obj->fk_user_modif;
2115 $this->total_ht = $obj->total_ht;
2116 $this->total_tva = $obj->total_tva;
2117 $this->total_localtax1 = $obj->total_localtax1;
2118 $this->total_localtax2 = $obj->total_localtax2;
2119 $this->total_ttc = $obj->total_ttc;
2120 $this->
date = $this->db->jdate($obj->date_commande);
2121 $this->date_commande = $this->db->jdate($obj->date_commande);
2122 $this->date_creation = $this->db->jdate($obj->date_creation);
2123 $this->date_validation = $this->db->jdate($obj->date_valid);
2124 $this->date_modification = $this->db->jdate($obj->tms);
2125 $this->source = $obj->source;
2126 $this->billed = $obj->billed;
2127 $this->note = $obj->note_private;
2128 $this->note_private = $obj->note_private;
2129 $this->note_public = $obj->note_public;
2130 $this->model_pdf = $obj->model_pdf;
2131 $this->last_main_doc = $obj->last_main_doc;
2132 $this->mode_reglement_id = $obj->fk_mode_reglement;
2133 $this->mode_reglement_code = $obj->mode_reglement_code;
2134 $this->mode_reglement = $obj->mode_reglement_libelle;
2135 $this->cond_reglement_id = $obj->fk_cond_reglement;
2136 $this->cond_reglement_code = $obj->cond_reglement_code;
2137 $this->cond_reglement = $obj->cond_reglement_libelle;
2138 $this->cond_reglement_label = $obj->cond_reglement_libelle;
2139 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
2140 $this->deposit_percent = $obj->deposit_percent;
2141 $this->fk_account = $obj->fk_account;
2142 $this->availability_id = $obj->fk_availability;
2143 $this->availability_code = $obj->availability_code;
2145 $this->demand_reason_id = $obj->fk_input_reason;
2146 $this->demand_reason_code = $obj->demand_reason_code;
2147 $this->delivery_date = $this->db->jdate($obj->delivery_date);
2148 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method :
null;
2149 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse :
null;
2150 $this->fk_delivery_address = $obj->fk_delivery_address;
2151 $this->module_source = $obj->module_source;
2152 $this->pos_source = $obj->pos_source;
2155 $this->fk_incoterms = $obj->fk_incoterms;
2156 $this->location_incoterms = $obj->location_incoterms;
2157 $this->label_incoterms = $obj->label_incoterms;
2160 $this->fk_multicurrency = $obj->fk_multicurrency;
2161 $this->multicurrency_code = $obj->multicurrency_code;
2162 $this->multicurrency_tx = $obj->multicurrency_tx;
2163 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
2164 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
2165 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
2167 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
2169 $this->lines = array();
2175 $this->db->free($result);
2184 $this->error =
'Order with id '.$id.
' not found sql='.$sql;
2188 $this->error = $this->db->error();
2206 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2207 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
2212 $result = $remise->fetch($idremise);
2215 if ($remise->fk_facture) {
2216 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2217 $this->db->rollback();
2223 $line->fk_commande = $this->id;
2224 $line->fk_remise_except = $remise->id;
2225 $line->desc = $remise->description;
2226 $line->vat_src_code = $remise->vat_src_code;
2227 $line->tva_tx = $remise->tva_tx;
2228 $line->localtax1_tx = $remise->localtax1_tx;
2229 $line->localtax1_type = $remise->localtax1_type;
2230 $line->localtax2_tx = $remise->localtax1_tx;
2231 $line->localtax2_type = $remise->localtax1_type;
2232 $line->subprice = -(float) $remise->amount_ht;
2233 $line->price = -(float) $remise->amount_ht;
2234 $line->fk_product = 0;
2236 $line->remise_percent = 0;
2238 $line->info_bits = 2;
2240 $line->total_ht = -(float) $remise->amount_ht;
2241 $line->total_tva = -(float) $remise->amount_tva;
2242 $line->total_ttc = -(float) $remise->amount_ttc;
2243 $line->total_localtax1 = -(float) $remise->total_localtax1;
2244 $line->total_localtax2 = -(float) $remise->total_localtax2;
2246 $result = $line->insert();
2250 $this->db->commit();
2253 $this->db->rollback();
2258 $this->db->rollback();
2262 $this->db->rollback();
2276 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2279 $this->lines = array();
2281 $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,';
2282 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.fk_remise_except, l.remise_percent, l.subprice, l.subprice_ttc, l.fk_product_fournisseur_price as fk_fournprice, l.buy_price_ht as pa_ht, l.rang, l.info_bits, l.special_code,';
2283 $sql .=
' l.total_ht, l.total_ttc, l.total_tva, l.total_localtax1, l.total_localtax2, l.date_start, l.date_end,';
2284 $sql .=
' l.fk_unit, l.extraparams,';
2285 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_subprice_ttc, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2286 $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,';
2287 $sql .=
' p.customcode, p.fk_country as country_id, c.code as country_code,';
2288 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units, p.packaging';
2289 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element_line.
' as l';
2290 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (p.rowid = l.fk_product)';
2291 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_country as c ON c.rowid = p.fk_country';
2292 $sql .=
' WHERE l.fk_commande = '.((int) $this->
id);
2293 if ($only_product) {
2294 $sql .=
' AND p.fk_product_type = 0';
2296 $sql .=
' ORDER BY l.rang, l.rowid';
2298 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
2299 $result = $this->db->query($sql);
2301 $num = $this->db->num_rows($result);
2305 $objp = $this->db->fetch_object($result);
2309 $line->rowid = $objp->rowid;
2310 $line->id = $objp->rowid;
2311 $line->fk_commande = $objp->fk_commande;
2312 $line->commande_id = $objp->fk_commande;
2313 $line->label = $objp->custom_label;
2314 $line->desc = $objp->description;
2315 $line->description = $objp->description;
2316 $line->product_type = $objp->product_type;
2317 $line->qty = $objp->qty;
2318 $line->ref_ext = $objp->ref_ext;
2320 $line->vat_src_code = $objp->vat_src_code;
2321 $line->tva_tx = $objp->tva_tx;
2322 $line->localtax1_tx = $objp->localtax1_tx;
2323 $line->localtax2_tx = $objp->localtax2_tx;
2324 $line->localtax1_type = $objp->localtax1_type;
2325 $line->localtax2_type = $objp->localtax2_type;
2326 $line->total_ht = $objp->total_ht;
2327 $line->total_ttc = $objp->total_ttc;
2328 $line->total_tva = $objp->total_tva;
2329 $line->total_localtax1 = $objp->total_localtax1;
2330 $line->total_localtax2 = $objp->total_localtax2;
2331 $line->subprice = (float) $objp->subprice;
2332 $line->subprice_ttc = (float) $objp->subprice_ttc;
2333 $line->fk_remise_except = $objp->fk_remise_except;
2334 $line->remise_percent = $objp->remise_percent;
2335 $line->price = $objp->price;
2336 $line->fk_product = $objp->fk_product;
2337 $line->fk_fournprice = $objp->fk_fournprice;
2338 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2339 $line->pa_ht = $marginInfos[0];
2340 $line->marge_tx = $marginInfos[1];
2341 $line->marque_tx = $marginInfos[2];
2342 $line->rang = $objp->rang;
2343 $line->info_bits = $objp->info_bits;
2344 $line->special_code = $objp->special_code;
2345 $line->fk_parent_line = $objp->fk_parent_line;
2347 $line->ref = $objp->product_ref;
2348 $line->libelle = $objp->product_label;
2350 $line->product_ref = $objp->product_ref;
2351 $line->product_label = $objp->product_label;
2352 $line->product_tosell = $objp->product_tosell;
2353 $line->product_tobuy = $objp->product_tobuy;
2354 $line->product_desc = $objp->product_desc;
2355 $line->product_tobatch = $objp->product_tobatch;
2356 $line->product_barcode = $objp->product_barcode;
2357 $line->product_custom_code = $objp->customcode;
2358 $line->product_custom_country_id = $objp->country_id;
2359 $line->product_custom_country_code = $objp->country_code;
2361 $line->fk_product_type = $objp->fk_product_type;
2362 $line->fk_unit = $objp->fk_unit;
2364 $line->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
2366 $line->weight = $objp->weight;
2367 $line->weight_units = $objp->weight_units;
2368 $line->volume = $objp->volume;
2369 $line->volume_units = $objp->volume_units;
2370 $line->packaging = $objp->packaging;
2372 $line->date_start = $this->db->jdate($objp->date_start);
2373 $line->date_end = $this->db->jdate($objp->date_end);
2376 $line->fk_multicurrency = $objp->fk_multicurrency;
2377 $line->multicurrency_code = $objp->multicurrency_code;
2378 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2379 $line->multicurrency_subprice_ttc = $objp->multicurrency_subprice_ttc;
2380 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2381 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2382 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2384 $line->fetch_optionals();
2387 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2388 $tmpproduct =
new Product($this->db);
2389 $tmpproduct->fetch($objp->fk_product);
2390 $tmpproduct->getMultiLangs();
2392 $line->multilangs = $tmpproduct->multilangs;
2395 $this->lines[$i] = $line;
2400 $this->db->free($result);
2404 $this->error = $this->db->error();
2418 foreach ($this->lines as $line) {
2419 if ($line->product_type == 0) {
2434 foreach ($this->lines as $line) {
2435 if ($line->product_type == 1) {
2451 $sql =
'SELECT COUNT(DISTINCT ed.fk_expedition) as nb';
2452 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2453 $sql .=
' '.MAIN_DB_PREFIX.$this->table_element_line.
' as cd';
2455 $sql .=
' ed.fk_elementdet = cd.rowid';
2456 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2459 dol_syslog(get_class($this).
"::getNbOfShipments", LOG_DEBUG);
2460 $resql = $this->db->query($sql);
2462 $obj = $this->db->fetch_object($resql);
2464 $nb = (int) $obj->nb;
2467 $this->db->free($resql);
2470 $this->error = $this->db->lasterror();
2485 $this->expeditions = array();
2487 $sql =
'SELECT cd.rowid, cd.fk_product,';
2488 $sql .=
' sum(ed.qty) as qty';
2489 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2490 if ($filtre_statut >= 0) {
2491 $sql .=
' '.MAIN_DB_PREFIX.
'expedition as e,';
2493 $sql .=
' '.MAIN_DB_PREFIX.$this->table_element_line.
' as cd';
2495 if ($filtre_statut >= 0) {
2496 $sql .=
' ed.fk_expedition = e.rowid AND';
2498 $sql .=
' ed.fk_elementdet = cd.rowid';
2499 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2500 if ($fk_product > 0) {
2501 $sql .=
' AND cd.fk_product = '.((int) $fk_product);
2503 if ($filtre_statut >= 0) {
2504 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
2506 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
2509 dol_syslog(get_class($this).
"::loadExpeditions", LOG_DEBUG);
2510 $resql = $this->db->query($sql);
2512 $num = $this->db->num_rows($resql);
2515 $obj = $this->db->fetch_object($resql);
2516 $this->expeditions[$obj->rowid] = $obj->qty;
2519 $this->db->free($resql);
2522 $this->error = $this->db->lasterror();
2534 $sql =
'SELECT count(*)';
2535 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expedition as e';
2536 $sql .=
', '.MAIN_DB_PREFIX.
'element_element as el';
2537 $sql .=
' WHERE el.fk_source = '.((int) $this->
id);
2538 $sql .=
" AND el.sourcetype = 'commande'";
2539 $sql .=
" AND el.fk_target = e.rowid";
2540 $sql .=
" AND el.targettype = 'shipping'";
2542 $resql = $this->db->query($sql);
2544 $row = $this->db->fetch_row($resql);
2547 dol_print_error($this->db);
2563 if ($this->
status == self::STATUS_DRAFT) {
2569 $line->context = $this->context;
2572 $line->fetch($lineid);
2577 if ($id > 0 && (
int) $line->fk_commande !== (
int) $id) {
2578 $this->db->rollback();
2579 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2584 $staticline = clone $line;
2585 $line->oldline = $staticline;
2587 if ($line->delete($user) > 0) {
2591 $this->db->commit();
2594 $this->db->rollback();
2595 $this->error = $this->db->lasterror();
2599 $this->db->rollback();
2604 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
2623 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
2625 return $this->
setDiscount($user, $remise, $notrigger);
2638 $remise = trim((
string) $remise) ? trim((
string) $remise) : 0;
2640 if ($user->hasRight(
'commande',
'creer')) {
2647 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2648 $sql .=
' SET remise_percent = '.((float) $remise);
2649 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.((int) self::STATUS_DRAFT);
2652 $resql = $this->db->query($sql);
2654 $this->errors[] = $this->db->error();
2659 $this->oldcopy = clone $this;
2660 $this->remise_percent = $remise;
2664 if (!$notrigger && empty($error)) {
2666 $result = $this->call_trigger(
'ORDER_MODIFY', $user);
2674 $this->db->commit();
2677 foreach ($this->errors as $errmsg) {
2678 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2679 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2681 $this->db->rollback();
2702 if ($user->hasRight(
'commande',
'creer')) {
2707 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
2708 $sql .=
" SET date_commande = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
2709 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut = ".((int) self::STATUS_DRAFT);
2712 $resql = $this->db->query($sql);
2714 $this->errors[] = $this->db->error();
2719 $this->oldcopy = clone $this;
2720 $this->
date = $date;
2723 if (!$notrigger && empty($error)) {
2725 $result = $this->call_trigger(
'ORDER_MODIFY', $user);
2733 $this->db->commit();
2736 foreach ($this->errors as $errmsg) {
2737 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2738 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2740 $this->db->rollback();
2774 if ($user->hasRight(
'commande',
'creer')) {
2779 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
2780 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2781 $sql .=
" WHERE rowid = ".((int) $this->
id);
2784 $resql = $this->db->query($sql);
2786 $this->errors[] = $this->db->error();
2791 $this->oldcopy = clone $this;
2792 $this->delivery_date = $delivery_date;
2795 if (!$notrigger && empty($error)) {
2797 $result = $this->call_trigger(
'ORDER_MODIFY', $user);
2805 $this->db->commit();
2808 foreach ($this->errors as $errmsg) {
2809 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2810 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2812 $this->db->rollback();
2834 public function liste_array($shortlist = 0, $draft = 0, $excluser =
null, $socid = 0, $limit = 0, $offset = 0, $sortfield =
'c.date_commande', $sortorder =
'DESC')
2841 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2842 $sql .=
" c.rowid as cid, c.ref";
2843 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
2844 $sql .=
", sc.fk_soc, sc.fk_user";
2846 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.$this->table_element.
" as c";
2847 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
2848 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2850 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2851 $sql .=
" AND c.fk_soc = s.rowid";
2852 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
2853 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2856 $sql .=
" AND s.rowid = ".((int) $socid);
2859 $sql .=
" AND c.fk_statut = ".self::STATUS_DRAFT;
2861 if (is_object($excluser)) {
2862 $sql .=
" AND c.fk_user_author <> ".((int) $excluser->id);
2864 $sql .= $this->db->order($sortfield, $sortorder);
2865 $sql .= $this->db->plimit($limit, $offset);
2867 $result = $this->db->query($sql);
2869 $numc = $this->db->num_rows($result);
2872 while ($i < $numc) {
2873 $obj = $this->db->fetch_object($result);
2875 if ($shortlist == 1) {
2876 $ga[$obj->cid] = $obj->ref;
2877 } elseif ($shortlist == 2) {
2878 $ga[$obj->cid] = $obj->ref.
' ('.$obj->name.
')';
2880 $ga[$i][
'id'] = $obj->cid;
2881 $ga[$i][
'ref'] = $obj->ref;
2882 $ga[$i][
'name'] = $obj->name;
2889 dol_print_error($this->db);
2905 dol_syslog(
'Commande::availability('.$availability_id.
')');
2906 if ($this->
status >= self::STATUS_DRAFT) {
2911 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2912 $sql .=
' SET fk_availability = '.((int) $availability_id);
2913 $sql .=
' WHERE rowid='.((int) $this->
id);
2916 $resql = $this->db->query($sql);
2918 $this->errors[] = $this->db->error();
2923 $this->oldcopy = clone $this;
2924 $this->availability_id = $availability_id;
2927 if (!$notrigger && empty($error)) {
2929 $result = $this->call_trigger(
'ORDER_MODIFY', $user);
2937 $this->db->commit();
2940 foreach ($this->errors as $errmsg) {
2941 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2942 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2944 $this->db->rollback();
2948 $error_str =
'Command status do not meet requirement '.$this->status;
2950 $this->error = $error_str;
2951 $this->errors[] = $this->error;
2969 dol_syslog(
'Commande::demand_reason('.$demand_reason_id.
')');
2970 if ($this->
status >= self::STATUS_DRAFT) {
2975 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2976 $sql .=
' SET fk_input_reason = '.((int) $demand_reason_id);
2977 $sql .=
' WHERE rowid='.((int) $this->
id);
2980 $resql = $this->db->query($sql);
2982 $this->errors[] = $this->db->error();
2987 $this->oldcopy = clone $this;
2988 $this->demand_reason_id = $demand_reason_id;
2991 if (!$notrigger && empty($error)) {
2993 $result = $this->call_trigger(
'ORDER_MODIFY', $user);
3001 $this->db->commit();
3004 foreach ($this->errors as $errmsg) {
3005 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
3006 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3008 $this->db->rollback();
3012 $error_str =
'order status do not meet requirement '.$this->status;
3014 $this->error = $error_str;
3015 $this->errors[] = $this->error;
3040 $ref_client = trim($ref_client);
3041 if ($ref_client ===
'' || $socid <= 0) {
3045 $sql =
"SELECT ref FROM ".$this->db->prefix().
"commande";
3046 $sql .=
" WHERE ref_client = '".$this->db->escape($ref_client).
"'";
3047 $sql .=
" AND fk_soc = ".((int) $socid);
3048 $sql .=
" AND entity IN (".getEntity(
'commande').
")";
3049 if ($excludeid > 0) {
3050 $sql .=
" AND rowid <> ".((int) $excludeid);
3052 $sql .=
" ORDER BY rowid ASC";
3053 $sql .= $this->db->plimit(1);
3055 dol_syslog(get_class($this).
"::isRefClientAlreadyUsed", LOG_DEBUG);
3056 $resql = $this->db->query($sql);
3058 $this->error = $this->db->lasterror();
3059 $this->errors[] = $this->error;
3060 dol_syslog(get_class($this).
"::isRefClientAlreadyUsed ".$this->error, LOG_ERR);
3064 $obj = $this->db->fetch_object($resql);
3065 $this->db->free($resql);
3067 if (!is_object($obj)) {
3071 $langs->load(
'orders');
3072 $this->error = $langs->trans(
'ErrorRefCustomerAlreadyUsedOnOrder', $ref_client, $obj->ref);
3073 $this->errors[] = $this->error;
3074 dol_syslog(get_class($this).
"::isRefClientAlreadyUsed ref_client=".$ref_client.
" already used by ".$obj->ref, LOG_WARNING);
3091 if ($user->hasRight(
'commande',
'creer')) {
3100 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
' SET';
3101 $sql .=
' ref_client = '.(empty($ref_client) ?
'NULL' :
"'".$this->db->escape($ref_client).
"'");
3102 $sql .=
' WHERE rowid = '.((int) $this->
id);
3104 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
3105 $resql = $this->db->query($sql);
3107 $this->errors[] = $this->db->error();
3112 $this->oldcopy = clone $this;
3113 $this->ref_client = $ref_client;
3114 $this->ref_customer = $ref_client;
3117 if (!$notrigger && empty($error)) {
3119 $result = $this->call_trigger(
'ORDER_MODIFY', $user);
3126 $this->db->commit();
3129 foreach ($this->errors as $errmsg) {
3130 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
3131 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3133 $this->db->rollback();
3152 if ($this->billed) {
3158 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
' SET facture = 1';
3161 dol_syslog(get_class($this).
"::classifyBilled", LOG_DEBUG);
3162 if ($this->db->query($sql)) {
3163 $this->oldcopy = clone $this;
3166 if (!$notrigger && empty($error)) {
3168 $result = $this->call_trigger(
'ORDER_CLASSIFY_BILLED', $user);
3176 $this->db->commit();
3179 foreach ($this->errors as $errmsg) {
3180 dol_syslog(get_class($this).
"::classifyBilled ".$errmsg, LOG_ERR);
3181 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3183 $this->db->rollback();
3187 $this->error = $this->db->error();
3188 $this->db->rollback();
3206 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
' SET facture = 0';
3209 dol_syslog(get_class($this).
"::classifyUnBilled", LOG_DEBUG);
3210 if ($this->db->query($sql)) {
3211 $this->oldcopy = clone $this;
3214 if (!$notrigger && empty($error)) {
3216 $result = $this->call_trigger(
'ORDER_CLASSIFY_UNBILLED', $user);
3226 $this->db->commit();
3229 foreach ($this->errors as $errmsg) {
3230 dol_syslog(get_class($this).
"::classifyUnBilled ".$errmsg, LOG_ERR);
3231 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3233 $this->db->rollback();
3237 $this->error = $this->db->error();
3238 $this->db->rollback();
3274 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)
3276 global
$mysoc, $langs, $user;
3279 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
3283 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");
3284 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3291 if (empty($info_bits)) {
3294 if (empty($txtva)) {
3297 if (empty($txlocaltax1)) {
3300 if (empty($txlocaltax2)) {
3303 if (empty($remise_percent)) {
3304 $remise_percent = 0;
3306 if (empty($qty) && empty($special_code)) {
3309 if (!empty($qty) && $special_code == 3) {
3312 if (empty($ref_ext)) {
3317 $langs->load(
"errors");
3318 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3322 $remise_percent = (float)
price2num($remise_percent);
3326 $pu_ht_devise =
price2num($pu_ht_devise);
3327 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
3330 $txlocaltax1 = (float)
price2num($txlocaltax1);
3331 $txlocaltax2 = (float)
price2num($txlocaltax2);
3344 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3345 $vat_src_code = $reg[1];
3346 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3353 if ($tmpline->fetch($rowid) > 0) {
3355 if ($newqty != $qty) {
3357 setEventMessages($langs->trans(
'QtyRecalculatedWithPackaging'),
null,
'warnings');
3362 $tabprice =
calcul_price_total($qty, (
float) $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type,
$mysoc, $localtaxes_type, 100, $this->multicurrency_tx, (
float) $pu_ht_devise, $this->multicurrency_code);
3364 $total_ht = $tabprice[0];
3365 $total_tva = $tabprice[1];
3366 $total_ttc = $tabprice[2];
3367 $total_localtax1 = $tabprice[9];
3368 $total_localtax2 = $tabprice[10];
3369 $pu_ht = $tabprice[3];
3370 $pu_tva = $tabprice[4];
3371 $pu_ttc = $tabprice[5];
3374 $multicurrency_total_ht = $tabprice[16];
3375 $multicurrency_total_tva = $tabprice[17];
3376 $multicurrency_total_ttc = $tabprice[18];
3377 $pu_ht_devise = $tabprice[19];
3381 $line->fetch($rowid);
3382 $line->fetch_optionals();
3384 if (!empty($line->fk_product)) {
3385 $product =
new Product($this->db);
3386 $result = $product->fetch($line->fk_product);
3387 $product_type = $product->type;
3389 if (
getDolGlobalString(
'STOCK_MUST_BE_ENOUGH_FOR_ORDER') && $product->isStockManaged()) {
3391 $productChildrenNb = 0;
3393 $productChildrenNb = $product->hasFatherOrChild(1);
3395 if ($productChildrenNb > 0) {
3397 $product_stock =
null;
3398 $product->loadStockForVirtualProduct(
'warehouseopen', $qty);
3399 foreach ($product->stock_warehouse as $componentStockWarehouse) {
3400 if ($product_stock ===
null) {
3401 $product_stock = $componentStockWarehouse->real;
3403 $product_stock = min($product_stock, $componentStockWarehouse->real);
3406 if ($product_stock ===
null) {
3410 $product_stock = $product->stock_reel;
3413 if ($product_stock < $qty) {
3414 $langs->load(
"errors");
3415 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', (
string) $product->ref);
3416 $this->errors[] = $this->error;
3418 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
3420 $this->db->rollback();
3426 $staticline = clone $line;
3428 $line->oldline = $staticline;
3429 $this->line = $line;
3430 $this->line->context = $this->context;
3431 $this->line->rang = $rang;
3434 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
3435 $rangmax = $this->
line_max($fk_parent_line);
3436 $this->line->rang = $rangmax + 1;
3440 $this->line->id = $rowid;
3441 $this->line->label = $label;
3442 $this->line->desc = $desc;
3443 $this->line->qty = $qty;
3444 $this->line->ref_ext = $ref_ext;
3446 $this->line->vat_src_code = $vat_src_code;
3447 $this->line->tva_tx = $txtva;
3448 $this->line->localtax1_tx = $txlocaltax1;
3449 $this->line->localtax2_tx = $txlocaltax2;
3450 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3451 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3452 $this->line->remise_percent = $remise_percent;
3453 $this->line->subprice = (float) $pu_ht;
3455 $this->line->subprice_ttc = ($price_base_type ===
'TTC') ? (
float) $pu_ttc : 0;
3456 $this->line->info_bits = $info_bits;
3457 $this->line->special_code = $special_code;
3458 $this->line->total_ht = (float) $total_ht;
3459 $this->line->total_tva = (float) $total_tva;
3460 $this->line->total_localtax1 = (float) $total_localtax1;
3461 $this->line->total_localtax2 = (float) $total_localtax2;
3462 $this->line->total_ttc = (float) $total_ttc;
3464 $this->line->date_end = $date_end;
3465 $this->line->product_type = $type;
3466 $this->line->fk_parent_line = $fk_parent_line;
3467 $this->line->skip_update_total = $skip_update_total;
3468 $this->line->fk_unit = $fk_unit;
3470 $this->line->fk_fournprice = $fk_fournprice;
3471 $this->line->pa_ht = $pa_ht;
3474 $this->line->multicurrency_subprice = (float) $pu_ht_devise;
3475 $this->line->multicurrency_total_ht = (float) $multicurrency_total_ht;
3476 $this->line->multicurrency_total_tva = (float) $multicurrency_total_tva;
3477 $this->line->multicurrency_total_ttc = (float) $multicurrency_total_ttc;
3479 if (is_array($array_options) && count($array_options) > 0) {
3481 foreach ($array_options as $key => $value) {
3482 $this->line->array_options[$key] = $array_options[$key];
3486 $result = $this->line->update($user, $notrigger);
3489 if (!empty($fk_parent_line)) {
3496 $this->db->commit();
3501 $this->db->rollback();
3505 $this->error = get_class($this).
"::updateline Order status makes operation forbidden";
3506 $this->errors = array(
'OrderStatusMakeOperationForbidden');
3523 if (isset($this->
ref)) {
3524 $this->
ref = trim($this->
ref);
3526 if (isset($this->ref_client)) {
3527 $this->ref_client = trim($this->ref_client);
3529 if (isset($this->ref_customer)) {
3530 $this->ref_customer = trim($this->ref_customer);
3532 if (isset($this->note) || isset($this->note_private)) {
3533 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
3535 if (isset($this->note_public)) {
3536 $this->note_public = trim($this->note_public);
3538 if (isset($this->model_pdf)) {
3539 $this->model_pdf = trim($this->model_pdf);
3541 if (isset($this->import_key)) {
3542 $this->import_key = trim($this->import_key);
3544 $delivery_date = $this->delivery_date;
3548 if ($this->
isRefClientAlreadyUsed((
string) $this->ref_client, (
int) $this->socid, (
int) $this->
id) != 0) {
3553 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
3555 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
3556 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
3557 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
3558 $sql .=
" fk_soc=".(isset($this->socid) ? ((int) $this->socid) :
"null").
",";
3559 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
3560 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
3562 $sql .=
" total_tva=".(isset($this->total_tva) ? ((float) $this->total_tva) :
"null").
",";
3563 $sql .=
" localtax1=".(isset($this->total_localtax1) ? ((float) $this->total_localtax1) :
"null").
",";
3564 $sql .=
" localtax2=".(isset($this->total_localtax2) ? ((float) $this->total_localtax2) :
"null").
",";
3565 $sql .=
" total_ht=".(isset($this->total_ht) ? ((float) $this->total_ht) :
"null").
",";
3566 $sql .=
" total_ttc=".(isset($this->total_ttc) ? ((float) $this->total_ttc) :
"null").
",";
3567 $sql .=
" fk_statut=".(isset($this->
status) ? ((int) $this->
status) :
"null").
",";
3568 $sql .=
" fk_user_modif=".(isset($user->id) ? ((int) $user->id) :
"null").
",";
3569 $sql .=
" fk_user_valid=".((isset($this->user_validation_id) && $this->user_validation_id > 0) ? ((
int) $this->user_validation_id) :
"null").
",";
3570 $sql .=
" fk_projet=".(isset($this->fk_project) ? ((int) $this->fk_project) :
"null").
",";
3571 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? ((int) $this->cond_reglement_id) :
"null").
",";
3572 $sql .=
" deposit_percent=".(!empty($this->deposit_percent) ?
"'".$this->db->escape((
string) $this->deposit_percent).
"'" :
"null").
",";
3573 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? ((int) $this->mode_reglement_id) :
"null").
",";
3574 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
3575 $sql .=
" fk_shipping_method=".(isset($this->shipping_method_id) ? ((int) $this->shipping_method_id) :
"null").
",";
3576 $sql .=
" fk_account=".($this->fk_account > 0 ? ((int) $this->fk_account) :
"null").
",";
3577 $sql .=
" fk_input_reason=".($this->demand_reason_id > 0 ? ((int) $this->demand_reason_id) :
"null").
",";
3578 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
3579 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
3580 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
3581 $sql .=
" fk_warehouse=".($this->warehouse_id > 0 ? ((int) $this->warehouse_id) :
"null").
",";
3582 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
",";
3583 $sql .=
" module_source = ".(isset($this->module_source) ?
"'".$this->db->escape($this->module_source).
"'" :
"null").
",";
3584 $sql .=
" pos_source = ".(isset($this->pos_source) ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
3585 $sql .=
" WHERE rowid=".((int) $this->
id);
3589 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3590 $resql = $this->db->query($sql);
3593 $this->errors[] =
"Error ".$this->db->lasterror();
3603 if (!$error && !$notrigger) {
3605 $result = $this->call_trigger(
'ORDER_MODIFY', $user);
3614 foreach ($this->errors as $errmsg) {
3615 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3616 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3618 $this->db->rollback();
3621 $this->db->commit();
3634 public function delete($user, $notrigger = 0, $idwarehouse = -1)
3636 global
$conf, $langs;
3637 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3641 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
3647 $result = $this->call_trigger(
'ORDER_DELETE', $user);
3656 $this->errors[] = $langs->trans(
'SomeShipmentExists');
3664 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
3665 $langs->load(
"agenda");
3669 $num = count($this->lines);
3670 for ($i = 0; $i < $num; $i++) {
3671 if ($this->lines[$i]->fk_product > 0) {
3673 $mouvP->origin = &$this;
3674 $mouvP->setOrigin($this->element, $this->
id);
3676 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->transnoentitiesnoconv(
"OrderDeletedInDolibarr", $this->ref));
3679 $this->error = $mouvP->error;
3680 $this->errors = array_merge($this->errors, $mouvP->errors);
3689 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"categorie_order";
3690 $sql .=
" WHERE fk_order = ".((int) $this->
id);
3692 $result = $this->db->query($sql);
3695 $this->errors[] = $this->db->lasterror();
3700 if (!$error && !empty($this->table_element_line)) {
3701 $tabletodelete = $this->table_element_line;
3702 $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).
")";
3703 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3704 if (!$this->db->query($sqlef) || !$this->db->query($sql)) {
3706 $this->error = $this->db->lasterror();
3707 $this->errors[] = $this->error;
3708 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3733 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3739 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
3740 $res = $this->db->query($sql);
3743 $this->error = $this->db->lasterror();
3744 $this->errors[] = $this->error;
3745 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3761 if ($conf->commande->multidir_output[$this->entity] && !empty($this->
ref)) {
3762 $dir =
$conf->commande->multidir_output[$this->entity].
"/".$ref;
3763 $file = $dir.
"/".$ref.
".pdf";
3764 if (file_exists($file)) {
3768 $this->error =
'ErrorFailToDeleteFile';
3769 $this->errors[] = $this->error;
3770 $this->db->rollback();
3774 if (file_exists($dir)) {
3777 $this->error =
'ErrorFailToDeleteDir';
3778 $this->errors[] = $this->error;
3779 $this->db->rollback();
3787 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3788 $this->db->commit();
3791 $this->db->rollback();
3808 global
$conf, $langs, $hookmanager;
3812 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.date_livraison as delivery_date, c.fk_statut, c.total_ht";
3813 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as c";
3814 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
3815 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
3816 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3819 $sql .= $clause.
" c.entity IN (".
getEntity(
'commande').
")";
3821 if ($mode ==
'toship') {
3823 $sql .=
" AND c.fk_statut IN (" . self::STATUS_VALIDATED .
"," . self::STATUS_SHIPMENTONPROCESS .
")";
3825 if ($mode ==
'tobill') {
3827 $sql .=
" AND c.fk_statut IN (" . self::STATUS_VALIDATED .
"," . self::STATUS_SHIPMENTONPROCESS .
", " . self::STATUS_CLOSED .
") AND c.facture = 0";
3829 if ($mode ==
'shippedtobill') {
3831 $sql .=
" AND c.fk_statut IN (" . self::STATUS_CLOSED .
") AND c.facture = 0";
3834 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3837 $parameters = array(
'socid' => $user->socid);
3838 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this);
3839 $sql .= $hookmanager->resPrint;
3840 $resql = $this->db->query($sql);
3843 $label = $labelShort = $url =
'';
3844 if ($mode ==
'toship') {
3845 $delay_warning =
$conf->commande->client->warning_delay / 60 / 60 / 24;
3846 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=-2&mainmenu=commercial&leftmenu=orders';
3847 $label = $langs->transnoentitiesnoconv(
"OrdersToProcess");
3848 $labelShort = $langs->transnoentitiesnoconv(
"Opened");
3850 if ($mode ==
'tobill') {
3851 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=-3&search_billed=0&mainmenu=commercial&leftmenu=orders';
3852 $label = $langs->trans(
"OrdersToBill");
3853 $labelShort = $langs->trans(
"ToBill");
3855 if ($mode ==
'shippedtobill') {
3856 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=3&search_billed=0&mainmenu=commercial&leftmenu=orders';
3857 $label = $langs->trans(
"OrdersToBill");
3858 $labelShort = $langs->trans(
"StatusOrderDelivered").
' '.$langs->trans(
"and").
' '.$langs->trans(
"ToBill");
3863 $response->warning_delay = $delay_warning;
3864 $response->label = $label;
3865 $response->labelShort = $labelShort;
3866 $response->url = $url;
3867 $response->url_late = DOL_URL_ROOT.
'/commande/list.php?search_option=late&mainmenu=commercial&leftmenu=orders';
3868 $response->img = img_object(
'',
"order");
3870 $generic_commande =
new Commande($this->db);
3872 while ($obj = $this->db->fetch_object($resql)) {
3873 $response->nbtodo++;
3874 $response->total += $obj->total_ht;
3876 $generic_commande->status = $obj->fk_statut;
3877 $generic_commande->date = $this->db->jdate($obj->date_commande);
3878 $generic_commande->delivery_date = $this->db->jdate($obj->delivery_date);
3880 if ($mode ==
'toship' && $generic_commande->hasDelay()) {
3881 $response->nbtodolate++;
3887 $this->error = $this->db->error();
3901 $label = $langs->trans(
'OrderSource'.$this->source);
3903 if ($label ==
'OrderSource') {
3930 public function LibStatut($status, $billed, $mode, $donotshowbilled = 0)
3933 global $langs, $hookmanager;
3936 if (empty($donotshowbilled)) {
3937 $billedtext .= ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3939 $billedtextlong = ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3943 $paramsBadge = array(
'badgeParams' => array(
'attr' => array(
3944 'data-status-element' => $this->element,
3945 'data-billed' => (
int) $billed,
3946 'data-status' => (
int) $status
3949 if ($status == self::STATUS_CANCELED) {
3950 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderCanceled');
3951 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderCanceledShort');
3952 $statusType =
'status9';
3953 } elseif ($status == self::STATUS_DRAFT) {
3954 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDraft');
3955 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDraftShort');
3956 $statusType =
'status0';
3957 } elseif ($status == self::STATUS_VALIDATED) {
3958 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderValidated').$billedtextlong;
3959 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderValidatedShort').$billedtext;
3960 $statusType =
'status1';
3961 } elseif ($status == self::STATUS_SHIPMENTONPROCESS) {
3962 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderSent').$billedtextlong;
3963 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderSentShort').$billedtext;
3964 $labelTooltip = $langs->transnoentitiesnoconv(
"StatusOrderSent");
3965 if (!empty($this->delivery_date)) {
3966 $labelTooltip .=
' - '.$langs->transnoentitiesnoconv(
"DateDeliveryPlanned").dol_print_date($this->delivery_date,
'day').$billedtext;
3968 $statusType =
'status4';
3969 } elseif ($status == self::STATUS_CLOSED) {
3970 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDelivered').$billedtextlong;
3971 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDeliveredShort').$billedtext;
3972 $statusType =
'status6';
3974 $labelStatus = $langs->transnoentitiesnoconv(
'Unknown');
3975 $labelStatusShort =
'';
3980 $paramsBadge[
'tooltip'] = $labelTooltip;
3982 $parameters = array(
3983 'status' => $status,
3985 'billed' => $billed,
3986 'donotshowbilled' => $donotshowbilled,
3987 'paramsBadge' => & $paramsBadge
3990 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
3993 return $hookmanager->resPrint;
3996 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', $paramsBadge);
4008 global
$conf, $langs, $user;
4010 $langs->load(
'orders');
4012 $nofetch = !empty($params[
'nofetch']);
4015 return [
'optimize' => $langs->trans(
"Order")];
4018 if ($user->hasRight(
'commande',
'lire')) {
4019 $datas[
'picto'] = img_picto(
'', $this->picto,
'', 0, 0, 0,
'',
'paddingrightonly').
'<u>'.$langs->trans(
"Order").
'</u>';
4020 if (isset($this->
status)) {
4021 $datas[
'status'] =
' '.$this->getLibStatut(5);
4023 $datas[
'Ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
4025 $langs->load(
'companies');
4026 if (empty($this->thirdparty)) {
4029 $datas[
'customer'] =
'<br><b>'.$langs->trans(
'Customer').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
4031 $datas[
'RefCustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.(empty($this->ref_customer) ? (empty($this->ref_client) ?
'' : $this->ref_client) : $this->ref_customer);
4033 $langs->load(
'project');
4034 if (is_null($this->project) || (is_object($this->project) && empty($this->project->id))) {
4036 if ($res > 0 && $this->project instanceof
Project) {
4037 $datas[
'project'] =
'<br><b>'.$langs->trans(
'Project').
':</b> '.$this->project->getNomUrl(1,
'', 0,
'1');
4041 if (!empty($this->total_ht)) {
4042 $datas[
'AmountHT'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1,
$conf->currency);
4044 if (!empty($this->total_tva)) {
4045 $datas[
'VAT'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1,
$conf->currency);
4047 if (!empty($this->total_ttc)) {
4048 $datas[
'AmountTTC'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1,
$conf->currency);
4050 if (!empty($this->
date)) {
4051 $datas[
'Date'] =
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->
date,
'day');
4053 if (!empty($this->delivery_date)) {
4054 $datas[
'DeliveryDate'] =
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
4074 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0, $target =
'')
4076 global
$conf, $langs, $user, $hookmanager;
4078 if (!empty(
$conf->dol_no_mouse_hover)) {
4084 if (
isModEnabled(
"shipping") && ($option ==
'1' || $option ==
'2')) {
4085 $baseurl = DOL_URL_ROOT .
'/expedition/shipment.php';
4087 $baseurl = DOL_URL_ROOT .
'/commande/card.php';
4089 $query = [
'id' => $this->id];
4090 if (!$user->hasRight(
'commande',
'lire')) {
4094 if ($option !==
'nolink') {
4096 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
4097 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
4098 $add_save_lastsearch_values = 1;
4100 if ($add_save_lastsearch_values) {
4101 $query = array_merge($query, [
'save_lastsearch_values' => 1]);
4111 'objecttype' => $this->element,
4112 'option' => $option,
4115 $classfortooltip =
'classfortooltip';
4118 $classfortooltip =
'classforajaxtooltip';
4119 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
4126 if (empty($notooltip) && $user->hasRight(
'commande',
'lire')) {
4128 $label = $langs->trans(
"Order");
4129 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
4131 $linkclose .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
4132 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
4134 $target_value = array(
'_self',
'_blank',
'_parent',
'_top');
4135 if (in_array($target, $target_value)) {
4136 $linkclose .=
' target="'.dol_escape_htmltag($target).
'"';
4140 $linkstart =
'<a href="'.$url.
'"';
4141 $linkstart .= $linkclose.
'>';
4144 if ($option ===
'nolink') {
4149 $result .= $linkstart;
4151 $result .= img_object(($notooltip ?
'' : $label), $this->picto, (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
4153 if ($withpicto != 2) {
4154 $result .= $this->ref;
4156 $result .= $linkend;
4158 if ($addlinktonotes) {
4159 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
4162 $result .=
' <span class="note inline-block">';
4163 $result .=
'<a href="'.DOL_URL_ROOT.
'/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
4164 $result .= img_picto(
'',
'note');
4168 $result .=
'</span>';
4173 $hookmanager->initHooks(array($this->element .
'dao'));
4174 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
4175 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
4177 $result = $hookmanager->resPrint;
4179 $result .= $hookmanager->resPrint;
4193 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem,';
4194 $sql .=
' date_valid as datev,';
4195 $sql .=
' date_cloture as datecloture,';
4196 $sql .=
' fk_user_author, fk_user_valid, fk_user_cloture';
4197 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as c';
4198 $sql .=
' WHERE c.rowid = '.((int) $id);
4199 $result = $this->db->query($sql);
4201 if ($this->db->num_rows($result)) {
4202 $obj = $this->db->fetch_object($result);
4203 $this->
id = $obj->rowid;
4204 if ($obj->fk_user_author) {
4205 $this->user_creation_id = $obj->fk_user_author;
4207 if ($obj->fk_user_valid) {
4208 $this->user_validation_id = $obj->fk_user_valid;
4210 if ($obj->fk_user_cloture) {
4211 $this->user_closing_id = $obj->fk_user_cloture;
4214 $this->date_creation = $this->db->jdate($obj->datec);
4215 $this->date_modification = $this->db->jdate($obj->datem);
4216 $this->date_validation = $this->db->jdate($obj->datev);
4217 $this->date_cloture = $this->db->jdate($obj->datecloture);
4220 $this->db->free($result);
4222 dol_print_error($this->db);
4237 global
$conf, $langs;
4239 dol_syslog(get_class($this).
"::initAsSpecimen");
4244 $sql =
"SELECT rowid";
4245 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
4246 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
4247 if (array_key_exists(
'tosell', $param)) {
4248 $sql .=
" AND tosell = ".((int) $param[
'tosell']);
4250 $sql .= $this->db->plimit(100);
4252 $resql = $this->db->query($sql);
4254 $num_prods = $this->db->num_rows($resql);
4256 while ($i < $num_prods) {
4258 $row = $this->db->fetch_row($resql);
4259 $prodids[$i] = $row[0];
4265 $this->
ref =
'SPECIMEN';
4266 $this->specimen = 1;
4267 $this->entity =
$conf->entity;
4270 $this->date_lim_reglement = $this->
date + 3600 * 24 * 30;
4271 $this->cond_reglement_code =
'RECEP';
4272 $this->mode_reglement_code =
'CHQ';
4273 $this->availability_code =
'DSP';
4274 $this->demand_reason_code =
'SRC_00';
4276 $this->note_public =
'This is a comment (public)';
4277 $this->note_private =
'This is a comment (private)';
4279 $this->multicurrency_tx = 1;
4280 $this->multicurrency_code =
$conf->currency;
4282 $this->
status = $this::STATUS_DRAFT;
4288 while ($xnbp < $nbp) {
4291 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
4293 $line->subprice = 100;
4297 $line->total_ht = 50;
4298 $line->total_ttc = 60;
4299 $line->total_tva = 10;
4300 $line->remise_percent = 50;
4302 $line->total_ht = 100;
4303 $line->total_ttc = 120;
4304 $line->total_tva = 20;
4305 $line->remise_percent = 0;
4307 if ($num_prods > 0) {
4308 $prodid = mt_rand(1, $num_prods);
4309 $line->fk_product = $prodids[$prodid];
4310 $line->product_ref =
'SPECIMEN';
4313 $this->lines[$xnbp] = $line;
4315 $this->total_ht += $line->total_ht;
4316 $this->total_tva += $line->total_tva;
4317 $this->total_ttc += $line->total_ttc;
4333 global $user, $hookmanager;
4335 $this->nb = array();
4338 $sql =
"SELECT count(c.rowid) as nb";
4339 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as c";
4340 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON c.fk_soc = s.rowid";
4341 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
4342 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
4343 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
4346 $sql .=
" ".$clause.
" c.entity IN (".
getEntity(
'commande').
")";
4348 $parameters = array();
4349 $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this);
4350 $sql .= $hookmanager->resPrint;
4351 $resql = $this->db->query($sql);
4353 while ($obj = $this->db->fetch_object($resql)) {
4354 $this->nb[
"orders"] = $obj->nb;
4356 $this->db->free($resql);
4359 dol_print_error($this->db);
4360 $this->error = $this->db->error();
4372 $context = parent::getObjectLinesDisplayContext();
4373 $context[
'inputalsopricewithtax'] = 1;
4399 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
4403 $langs->load(
"orders");
4404 $outputlangs->load(
"products");
4407 $modele =
'eratosthene';
4409 if (!empty($this->model_pdf)) {
4410 $modele = $this->model_pdf;
4416 $modelpath =
"core/modules/commande/doc/";
4418 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
4471 return max($this->
date, $this->delivery_date) < ($now -
$conf->commande->client->warning_delay);
4481 global
$conf, $langs;
4483 if (empty($this->delivery_date)) {
4486 $text = $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->delivery_date,
'day');
4488 $text .=
' '.($conf->commande->client->warning_delay > 0 ?
'+' :
'-').
' '.round(abs(
$conf->commande->client->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
4519 global
$conf, $langs, $hookmanager;
4521 $langs->loadLangs(array(
'orders',
'sendings',
'stocks',
'products'));
4524 'has_product' =>
false,
4525 'shippable' =>
false,
4531 if (is_object($hookmanager)) {
4532 $parameters = array(
'options' => $options);
4533 $reshook = $hookmanager->executeHooks(
'getShippableInfos', $parameters, $this);
4535 dol_syslog(__METHOD__.
' hook getShippableInfos failed: '.$hookmanager->error, LOG_ERR);
4537 if (!empty($hookmanager->resArray[
'shippableinfos'])
4538 && is_array($hookmanager->resArray[
'shippableinfos'])) {
4539 return array_merge($result, $hookmanager->resArray[
'shippableinfos']);
4544 if ($this->
status == self::STATUS_DRAFT || $this->
status == self::STATUS_CLOSED) {
4549 $genericCommande = $this;
4550 $genericProduct =
new Product($this->db);
4553 $productstatcache = array();
4554 $productstatcachevirtual = array();
4557 $genericCommande->getLinesArray();
4558 $genericCommande->loadExpeditions();
4567 $genericProduct =
new Product($this->db);
4569 $numlines = count($genericCommande->lines);
4570 for ($lig = 0; $lig < $numlines; $lig++) {
4571 $orderLine = $genericCommande->lines[$lig];
4573 if (isset($genericCommande->expeditions[$orderLine->id])) {
4574 $reliquat = $orderLine->qty - $genericCommande->expeditions[$orderLine->id];
4576 $reliquat = $orderLine->qty;
4579 if ($orderLine->product_type == 0 && $orderLine->fk_product > 0) {
4582 if (empty($productstatcache[$orderLine->fk_product])) {
4583 $genericProduct->fetch($orderLine->fk_product);
4584 $genericProduct->load_stock(
'nobatch,warehouseopen');
4586 $productstatcache[$orderLine->fk_product][
'stockreel'] = $genericProduct->stock_reel;
4587 $productstatcachevirtual[$orderLine->fk_product][
'stockreel'] = $genericProduct->stock_theorique;
4590 $genericProduct->stock_reel = $productstatcache[$orderLine->fk_product][
'stockreel'];
4591 $genericProduct->stock_theorique = $productstatcachevirtual[$orderLine->fk_product][
'stockreel'];
4593 if ($reliquat > 0) {
4596 $textinfo .= $reliquat .
' x ' . $orderLine->product_ref .
' ' .
dol_trunc($orderLine->product_label, 20);
4597 $textinfo .=
' - ' . $langs->trans(
"Stock") .
': <span class="' . ($genericProduct->stock_reel >= $reliquat ?
'ok' :
'error') .
'">' . $genericProduct->stock_reel .
'</span>';
4598 $textinfo .=
' - ' . $langs->trans(
"VirtualStock") .
': <span class="' . ($genericProduct->stock_theorique >= $reliquat ?
'ok' :
'error') .
'">' . $genericProduct->stock_theorique .
'</span>';
4599 if ($reliquat != $orderLine->qty) {
4600 $textinfo .=
' <span class="opacitymedium">' . $langs->trans(
"QtyInOtherShipments") .
' ' . ($orderLine->qty - $reliquat) .
'</span>';
4602 $textinfo .=
'<br>';
4606 $stockordersupplier = 0;
4610 if (empty($productstatcache[$orderLine->fk_product][
'statsordercustomer'])) {
4611 $genericProduct->fetch($orderLine->fk_product);
4612 $genericProduct->load_stats_commande(0,
'1,2');
4613 $productstatcache[$orderLine->fk_product][
'statsordercustomer'] = $genericProduct->stats_commande[
'qty'];
4615 $genericProduct->stats_commande[
'qty'] = $productstatcache[$orderLine->fk_product][
'statsordercustomer'];
4616 $stockorder = $genericProduct->stats_commande[
'qty'];
4619 if (empty($productstatcache[$orderLine->fk_product][
'statsordersupplier'])) {
4620 $genericProduct->load_stats_commande_fournisseur(0,
'3');
4621 $productstatcache[$orderLine->fk_product][
'statsordersupplier'] = $genericProduct->stats_commande_fournisseur[
'qty'];
4623 $genericProduct->stats_commande_fournisseur[
'qty'] = $productstatcache[$orderLine->fk_product][
'statsordersupplier'];
4624 $stockordersupplier = $genericProduct->stats_commande_fournisseur[
'qty'];
4629 $textinfo .= $reliquat .
' x ' . $orderLine->ref .
' ' .
dol_trunc($orderLine->product_label, 20);
4630 $textinfo .=
' ' . $langs->trans(
"Available") .
' ' . $genericProduct->stock_reel .
'..' . $stockorder;
4632 if ($stockorder && $genericProduct->stock_reel < ($genericProduct->stock_reel - $stockorder + $reliquat)) {
4634 $textwarning .=
'<span class="warning">' . $langs->trans(
"Available") .
' ' . $genericProduct->stock_reel .
'..' . $stockorder .
'</span>';
4636 if ($reliquat > $genericProduct->stock_reel) {
4637 $textinfo .=
' <span class="warning">' . $langs->trans(
"Available") .
' ' . $genericProduct->stock_reel .
'</span>';
4639 $textinfo .=
' <span class="ok">' . $langs->trans(
"Available") .
' ' . $genericProduct->stock_reel .
'</span>';
4644 $textinfo .=
' ' . $langs->trans(
"SupplierOrder") .
' ' . $stockordersupplier;
4646 if ($reliquat != $orderLine->qty) {
4647 $textinfo .=
' <span class="opacitymedium">' . $langs->trans(
"QtyInOtherShipments") .
' ' . ($orderLine->qty - $reliquat) .
'</span>';
4649 $textinfo .=
'<br>';
4652 if ($reliquat > $genericProduct->stock_reel) {
4660 if (!$has_reliquat) {
4661 $texticon = img_picto(
'',
'statut5',
'', 0, 0, 0,
'',
'paddingleft');
4662 $textinfo = $texticon .
' ' . $langs->trans(
"Shipped");
4663 $result[
'shippable'] =
true;
4664 } elseif ($notshippable) {
4665 $texticon = img_picto(
'',
'dolly',
'', 0, 0, 0,
'',
'error paddingleft');
4666 $textinfo = $texticon .
' ' . $langs->trans(
"NonShippable") .
'<br>' . $textinfo;
4667 $result[
'shippable'] =
false;
4669 $texticon = img_picto(
'',
'dolly',
'', 0, 0, 0,
'',
'green paddingleft');
4670 $textinfo = $texticon .
' ' . $langs->trans(
"Shippable") .
'<br>' . $textinfo;
4671 $result[
'shippable'] =
true;
4673 $result[
'has_product'] =
true;
4674 $result[
'texticon'] = $texticon;
4675 $result[
'textinfo'] = $textinfo;
4676 $result[
'warning'] = !empty($warning);
if(! $sortfield) if(! $sortorder) $object
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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 the order date.
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.
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.
initAsSpecimen($param=array())
Initialise an instance with random values.
const STATUS_CANCELED
Canceled status.
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0, $target='')
Return clickable 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.
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 sale 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.
getObjectLinesDisplayContext()
Return the display context of the lines, as defined by the card before printObjectLines() and formAdd...
insert_discount($idremise)
Add a discount line into a sale order (as a sale order line) using an existing absolute discount (Con...
getNbOfProductsLines()
Return number of line with type product.
update(User $user, $notrigger=0)
Update database.
classifyUnBilled(User $user, $notrigger=0)
Classify the order as not invoiced.
setDiscount($user, $remise, $notrigger=0)
Set a percentage discount.
persistFieldValue(User $user, $field)
Save into database the current value of one field, called by updateFieldFromUi().
isRefClientAlreadyUsed(string $ref_client, int $socid, int $excludeid=0)
Check if a customer ref is already used by another customer order of the same third party.
setCategories($categories)
Sets object to given categories.
const STATUS_ACCEPTED
For backward compatibility.
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)
Delete an order line.
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.
getShippableInfos(array $options=array())
Compute shippable status and tooltip/icon for the order.
cloture($user, $notrigger=0, $checkpermission=1)
Close order.
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.
setSignedStatus(User $user, int $status=0, int $notrigger=0, $triggercode='')
Set signed status.
cancel($user, $idwarehouse=-1)
Cancel an order If stock is decremented on order validation, we must reincrement it.
createFromClone(User $user, $socid=0, $forceentity=null)
Load an object from its id and create a new one in database.
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.
roundQtyToPackaging($qty, $packaging)
Round a quantity up to the next multiple of a packaging quantity (options PRODUCT_USE_CUSTOMER_PACKAG...
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
fetchProject()
Load the project with id $this->fk_project into this->project.
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
isLineOfObject($lineid)
Check that a line belongs to this object, using $this->table_element_line and $this->fk_element.
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)
checkActiveProductInLines($status='onsale')
Check if all products have the right status (on sale, on buy) called during validation of propal,...
deleteExtraFields()
Delete all extra fields values for the current object.
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.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Superclass for orders classes.
Class to manage absolute discounts.
Class to manage Dolibarr database access.
Class to manage stock movements.
static getIdAndTxFromCode($dbs, $code, $date_document=0)
Get id and rate of currency from code.
static getIdFromCode($dbs, $code)
Get id of currency from code.
Class to manage order lines.
Class to manage products or services.
Class to manage projects.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
setSignedStatusCommon(User $user, int $status, int $notrigger=0, string $triggercode='')
Set signed status & call trigger with context message.
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_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
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.
$date_start
Variables from include:
dol_now($mode='gmt')
Return date for now.
setEntity($currentobject)
Set entity id to use when to create an object.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
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.
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.
getMultidirOutput($object, $module='', $forobject=0, $mode='output')
Return the full path of the directory where a module (or an object of a module) stores its files.
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)
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalBool($key, $default=false)
Return a Dolibarr global constant boolean value.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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 rate, 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)
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, $notused, $price_base_type, $info_bits, $type, $seller=null, $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date