39include_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
40require_once DOL_DOCUMENT_ROOT.
'/commande/class/orderline.class.php';
41require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
42require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
43require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
44require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
55 public $element =
'commande';
60 public $table_element =
'commande';
65 public $table_element_line =
'commandedet';
70 public $class_element_line =
'OrderLine';
75 public $fk_element =
'fk_commande';
80 public $picto =
'order';
86 public $restrictiononfksoc = 1;
106 public $ref_customer;
134 public $date_lim_reglement;
138 public $cond_reglement_code;
143 public $cond_reglement_doc;
150 public $deposit_percent;
160 public $mode_reglement;
165 public $mode_reglement_id;
170 public $mode_reglement_code;
176 public $availability_id;
182 public $availability_code;
188 public $availability;
193 public $demand_reason_id;
198 public $demand_reason_code;
210 public $date_commande;
215 public $delivery_date;
220 public $fk_remise_except;
226 public $remise_percent;
237 public $signed_status = 0;
242 public $warehouse_id;
247 public $extraparams = array();
249 public $linked_objects = array();
254 public $user_author_id;
264 public $lines = array();
270 public $module_source;
284 public $online_payment_url;
316 public $fields = array(
317 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 10),
318 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'default' =>
'1',
'enabled' => 1,
'visible' => -2,
'notnull' => 1,
'position' => 20,
'index' => 1),
319 'ref' => array(
'type' =>
'varchar(30)',
'label' =>
'Ref',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'showoncombobox' => 1,
'position' => 25),
320 'ref_ext' => array(
'type' =>
'varchar(255)',
'label' =>
'RefExt',
'enabled' => 1,
'visible' => 0,
'position' => 26),
321 'ref_client' => array(
'type' =>
'varchar(255)',
'label' =>
'RefCustomer',
'enabled' => 1,
'visible' => -1,
'position' => 28),
322 'fk_soc' => array(
'type' =>
'integer:Societe:societe/class/societe.class.php',
'label' =>
'ThirdParty',
'enabled' =>
'isModEnabled("societe")',
'visible' => -1,
'notnull' => 1,
'position' => 20),
323 'fk_projet' => array(
'type' =>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label' =>
'Project',
'enabled' =>
"isModEnabled('project')",
'visible' => -1,
'position' => 25),
324 'date_commande' => array(
'type' =>
'date',
'label' =>
'Date',
'enabled' => 1,
'visible' => 1,
'position' => 60,
'csslist' =>
'nowraponall'),
325 'date_valid' => array(
'type' =>
'datetime',
'label' =>
'DateValidation',
'enabled' => 1,
'visible' => -1,
'position' => 62,
'csslist' =>
'nowraponall'),
326 'date_cloture' => array(
'type' =>
'datetime',
'label' =>
'DateClosing',
'enabled' => 1,
'visible' => -1,
'position' => 65,
'csslist' =>
'nowraponall'),
327 'fk_user_valid' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserValidation',
'enabled' => 1,
'visible' => -1,
'position' => 85),
328 'fk_user_cloture' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserClosing',
'enabled' => 1,
'visible' => -1,
'position' => 90),
329 'source' => array(
'type' =>
'smallint(6)',
'label' =>
'Source',
'enabled' => 1,
'visible' => -1,
'position' => 95),
330 'total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'VAT',
'enabled' => 1,
'visible' => -1,
'position' => 125,
'isameasure' => 1),
331 'localtax1' => array(
'type' =>
'double(24,8)',
'label' =>
'LocalTax1',
'enabled' => 1,
'visible' => -1,
'position' => 130,
'isameasure' => 1),
332 'localtax2' => array(
'type' =>
'double(24,8)',
'label' =>
'LocalTax2',
'enabled' => 1,
'visible' => -1,
'position' => 135,
'isameasure' => 1),
333 'total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalHT',
'enabled' => 1,
'visible' => -1,
'position' => 140,
'isameasure' => 1),
334 'total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'TotalTTC',
'enabled' => 1,
'visible' => -1,
'position' => 145,
'isameasure' => 1),
335 'signed_status' => array(
'type' =>
'smallint(6)',
'label' =>
'SignedStatus',
'enabled' => 1,
'visible' => -1,
'position' => 146,
'arrayofkeyval' => array(0 =>
'NoSignature', 1 =>
'SignedSender', 2 =>
'SignedReceiver', 9 =>
'SignedAll')),
336 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => 0,
'position' => 150),
337 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => 0,
'position' => 155),
338 'model_pdf' => array(
'type' =>
'varchar(255)',
'label' =>
'PDFTemplate',
'enabled' => 1,
'visible' => 0,
'position' => 160),
339 'fk_account' => array(
'type' =>
'integer',
'label' =>
'BankAccount',
'enabled' =>
'isModEnabled("bank")',
'visible' => -1,
'position' => 170),
340 'fk_currency' => array(
'type' =>
'varchar(3)',
'label' =>
'MulticurrencyID',
'enabled' => 1,
'visible' => -1,
'position' => 175),
341 'fk_cond_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentTerm',
'enabled' => 1,
'visible' => -1,
'position' => 180),
342 'deposit_percent' => array(
'type' =>
'varchar(63)',
'label' =>
'DepositPercent',
'enabled' => 1,
'visible' => -1,
'position' => 181),
343 'fk_mode_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentMode',
'enabled' => 1,
'visible' => -1,
'position' => 185),
344 'date_livraison' => array(
'type' =>
'date',
'label' =>
'DateDeliveryPlanned',
'enabled' => 1,
'visible' => -1,
'position' => 190,
'csslist' =>
'nowraponall'),
345 'fk_shipping_method' => array(
'type' =>
'integer',
'label' =>
'ShippingMethod',
'enabled' => 1,
'visible' => -1,
'position' => 195),
346 'fk_warehouse' => array(
'type' =>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label' =>
'DefaultWarehouse',
'enabled' =>
'isModEnabled("stock")',
'visible' => -1,
'position' => 200,
'nodepth' => 1),
347 'fk_availability' => array(
'type' =>
'integer',
'label' =>
'Availability',
'enabled' => 1,
'visible' => -1,
'position' => 205),
348 'fk_input_reason' => array(
'type' =>
'integer',
'label' =>
'InputReason',
'enabled' => 1,
'visible' => -1,
'position' => 210),
350 'extraparams' => array(
'type' =>
'varchar(255)',
'label' =>
'Extraparams',
'enabled' => 1,
'visible' => -1,
'position' => 225),
351 'fk_incoterms' => array(
'type' =>
'integer',
'label' =>
'IncotermCode',
'enabled' =>
'$conf->incoterm->enabled',
'visible' => -1,
'position' => 230),
352 'location_incoterms' => array(
'type' =>
'varchar(255)',
'label' =>
'IncotermLabel',
'enabled' =>
'$conf->incoterm->enabled',
'visible' => -1,
'position' => 235),
353 'fk_multicurrency' => array(
'type' =>
'integer',
'label' =>
'Fk multicurrency',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 240),
354 'multicurrency_code' => array(
'type' =>
'varchar(255)',
'label' =>
'MulticurrencyCurrency',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 245),
355 'multicurrency_tx' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyRate',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 250,
'isameasure' => 1),
356 'multicurrency_total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountHT',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 255,
'isameasure' => 1),
357 'multicurrency_total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountVAT',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 260,
'isameasure' => 1),
358 'multicurrency_total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountTTC',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 265,
'isameasure' => 1),
359 'last_main_doc' => array(
'type' =>
'varchar(255)',
'label' =>
'LastMainDoc',
'enabled' => 1,
'visible' => -1,
'position' => 270),
360 'module_source' => array(
'type' =>
'varchar(32)',
'label' =>
'POSModule',
'enabled' => 1,
'visible' => -1,
'position' => 275),
361 'pos_source' => array(
'type' =>
'varchar(32)',
'label' =>
'POSTerminal',
'enabled' => 1,
'visible' => -1,
'position' => 280),
362 'fk_user_author' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserAuthor',
'enabled' => 1,
'visible' => -1,
'position' => 300),
363 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' => 1,
'visible' => -2,
'notnull' => -1,
'position' => 302),
364 'date_creation' => array(
'type' =>
'datetime',
'label' =>
'DateCreation',
'enabled' => 1,
'visible' => -2,
'position' => 304,
'csslist' =>
'nowraponall'),
365 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 306),
366 'import_key' => array(
'type' =>
'varchar(14)',
'label' =>
'ImportId',
'enabled' => 1,
'visible' => -2,
'position' => 400),
367 'fk_statut' => array(
'type' =>
'smallint(6)',
'label' =>
'Status',
'enabled' => 1,
'visible' => -1,
'position' => 500),
407 const STATUS_NO_SIGNATURE = 0;
412 const STATUS_SIGNED_SENDER = 1;
417 const STATUS_SIGNED_RECEIVER = 2;
422 const STATUS_SIGNED_ALL = 9;
434 $this->ismultientitymanaged = 1;
435 $this->isextrafieldmanaged = 1;
437 $this->fields[
'ref_ext'][
'visible'] =
getDolGlobalInt(
'MAIN_LIST_SHOW_REF_EXT');
449 global $langs,
$conf;
450 $langs->load(
"order");
459 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
460 foreach ($dirmodels as $reldir) {
464 $mybool = ((bool) @include_once $dir.$file) || $mybool;
472 $obj =
new $classname();
473 '@phan-var-force ModeleNumRefCommandes $obj';
475 $numref = $obj->getNextValue($soc, $this);
480 $this->error = $obj->error;
485 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_COMMANDE_ADDON_NotDefined");
499 public function valid($user, $idwarehouse = 0, $notrigger = 0)
501 global
$conf, $langs;
503 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
508 if ($this->statut == self::STATUS_VALIDATED) {
509 dol_syslog(get_class($this).
"::valid action abandoned: already validated", LOG_WARNING);
513 if (!((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
514 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'validate')))) {
515 $this->error =
'NotEnoughPermissions';
516 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
526 $soc->fetch($this->socid);
529 $result = $soc->setAsCustomer();
532 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
540 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
541 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
542 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
543 $sql .=
" date_valid='".$this->db->idate($now).
"',";
544 $sql .=
" fk_user_valid = ".($user->id > 0 ? (int) $user->id :
"null").
",";
545 $sql .=
" fk_user_modif = ".((int) $user->id);
546 $sql .=
" WHERE rowid = ".((int) $this->
id);
548 dol_syslog(get_class($this).
"::valid", LOG_DEBUG);
549 $resql = $this->db->query($sql);
552 $this->error = $this->db->lasterror();
558 if ($result >= 0 && isModEnabled(
'stock') &&
getDolGlobalInt(
'STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) {
559 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
560 $langs->load(
"agenda");
563 $cpt = count($this->lines);
564 for ($i = 0; $i < $cpt; $i++) {
565 if ($this->lines[$i]->fk_product > 0) {
567 $mouvP->origin = &$this;
568 $mouvP->setOrigin($this->element, $this->
id);
570 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"OrderValidatedInDolibarr", $num));
573 $this->error = $mouvP->error;
583 if (!$error && !$notrigger) {
593 $this->oldref = $this->ref;
596 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
598 $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).
"'";
599 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'commande/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
600 $resql = $this->db->query($sql);
603 $this->error = $this->db->lasterror();
605 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filepath = 'commande/".$this->db->escape($this->newref).
"'";
606 $sql .=
" WHERE filepath = 'commande/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
607 $resql = $this->db->query($sql);
610 $this->error = $this->db->lasterror();
616 $dirsource =
$conf->commande->multidir_output[$this->entity].
'/'.$oldref;
617 $dirdest =
$conf->commande->multidir_output[$this->entity].
'/'.$newref;
618 if (!$error && file_exists($dirsource)) {
619 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
621 if (@rename($dirsource, $dirdest)) {
624 $listoffiles =
dol_dir_list(
$conf->commande->multidir_output[$this->entity].
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
625 foreach ($listoffiles as $fileentry) {
626 $dirsource = $fileentry[
'name'];
627 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
628 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
629 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
630 @rename($dirsource, $dirdest);
648 $this->db->rollback();
664 global
$conf, $langs;
669 if ($this->statut <= self::STATUS_DRAFT && !
getDolGlobalInt(
'ORDER_REOPEN_TO_DRAFT')) {
673 if (!((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
674 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'validate')))) {
675 $this->error =
'Permission denied';
683 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
684 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT.
",";
685 $sql .=
" fk_user_modif = ".((int) $user->id);
686 $sql .=
" WHERE rowid = ".((int) $this->
id);
688 if ($this->db->query($sql)) {
690 $this->oldcopy = clone $this;
694 if (isModEnabled(
'stock') &&
getDolGlobalInt(
'STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) {
697 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
698 $langs->load(
"agenda");
700 $num = count($this->lines);
701 for ($i = 0; $i < $num; $i++) {
702 if ($this->lines[$i]->fk_product > 0) {
704 $mouvP->origin = &$this;
705 $mouvP->setOrigin($this->element, $this->
id);
707 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderBackToDraftInDolibarr", $this->ref));
710 $this->error = $mouvP->error;
719 $result = $this->
call_trigger(
'ORDER_UNVALIDATE', $user);
730 $this->db->rollback();
734 $this->error = $this->db->error();
735 $this->db->rollback();
754 if ($this->statut != self::STATUS_CANCELED && $this->statut != self::STATUS_CLOSED) {
755 dol_syslog(get_class($this).
"::set_reopen order has not status closed", LOG_WARNING);
761 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
762 $sql .=
' SET fk_statut='.self::STATUS_VALIDATED.
', facture=0,';
763 $sql .=
" fk_user_modif = ".((int) $user->id);
764 $sql .=
" WHERE rowid = ".((int) $this->
id);
766 dol_syslog(get_class($this).
"::set_reopen", LOG_DEBUG);
767 $resql = $this->db->query($sql);
777 $this->error = $this->db->lasterror();
788 foreach ($this->errors as $errmsg) {
789 dol_syslog(get_class($this).
"::set_reopen ".$errmsg, LOG_ERR);
790 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
792 $this->db->rollback();
804 public function cloture($user, $notrigger = 0)
808 $usercanclose = ((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'creer'))
809 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
'commande',
'order_advance',
'close')));
812 if ($this->statut == self::STATUS_CLOSED) {
819 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
820 $sql .=
' SET fk_statut = '.self::STATUS_CLOSED.
',';
821 $sql .=
' fk_user_cloture = '.((int) $user->id).
',';
822 $sql .=
" date_cloture = '".$this->db->idate($now).
"',";
823 $sql .=
" fk_user_modif = ".((int) $user->id);
826 if ($this->db->query($sql)) {
842 $this->db->rollback();
846 $this->error = $this->db->lasterror();
848 $this->db->rollback();
862 public function cancel($idwarehouse = -1)
864 global $user, $langs;
870 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
871 $sql .=
" SET fk_statut = ".self::STATUS_CANCELED.
",";
872 $sql .=
" fk_user_modif = ".((int) $user->id);
873 $sql .=
" WHERE rowid = ".((int) $this->
id);
874 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
876 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
877 if ($this->db->query($sql)) {
879 if (isModEnabled(
'stock') &&
getDolGlobalInt(
'STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) {
880 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
881 $langs->load(
"agenda");
883 $num = count($this->lines);
884 for ($i = 0; $i < $num; $i++) {
885 if ($this->lines[$i]->fk_product > 0) {
887 $mouvP->setOrigin($this->element, $this->
id);
889 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderCanceledInDolibarr", $this->ref));
892 $this->error = $mouvP->error;
913 foreach ($this->errors as $errmsg) {
914 dol_syslog(get_class($this).
"::cancel ".$errmsg, LOG_ERR);
915 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
917 $this->db->rollback();
921 $this->error = $this->db->error();
922 $this->db->rollback();
935 public function create($user, $notrigger = 0)
937 global
$conf, $langs, $mysoc;
943 $date = ($this->date_commande ? $this->date_commande : $this->date);
944 $delivery_date = $this->delivery_date;
947 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
952 if (empty($this->fk_multicurrency)) {
953 $this->multicurrency_code =
$conf->currency;
954 $this->fk_multicurrency = 0;
955 $this->multicurrency_tx = 1;
960 dol_syslog(get_class($this).
"::create user=".$user->id);
963 if (!empty($this->
ref)) {
966 $this->error =
'ErrorRefAlreadyExists';
967 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
968 $this->db->rollback();
974 $result = $soc->fetch($this->socid);
976 $this->error =
"Failed to fetch company";
977 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
981 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Source"));
982 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
990 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (";
991 $sql .=
" ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client";
992 $sql .=
", model_pdf, fk_cond_reglement, deposit_percent, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address";
993 $sql .=
", fk_shipping_method";
994 $sql .=
", fk_warehouse";
995 $sql .=
", fk_incoterms, location_incoterms";
996 $sql .=
", entity, module_source, pos_source";
997 $sql .=
", fk_multicurrency";
998 $sql .=
", multicurrency_code";
999 $sql .=
", multicurrency_tx";
1001 $sql .=
" VALUES ('(PROV)', ".((int) $this->socid).
", '".$this->db->idate($now).
"', ".((int) $user->id);
1002 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
1003 $sql .=
", '".$this->db->idate($date).
"'";
1004 $sql .=
", ".($this->source >= 0 && $this->source !=
'' ? $this->db->escape($this->source) :
'null');
1005 $sql .=
", '".$this->db->escape($this->note_private).
"'";
1006 $sql .=
", '".$this->db->escape($this->note_public).
"'";
1007 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
1008 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
1009 $sql .=
", '".$this->db->escape($this->model_pdf).
"'";
1010 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
1011 $sql .=
", ".(!empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
"null");
1012 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
1013 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
1014 $sql .=
", ".($this->availability_id > 0 ? ((int) $this->availability_id) :
"null");
1015 $sql .=
", ".($this->demand_reason_id > 0 ? ((int) $this->demand_reason_id) :
"null");
1016 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
1017 $sql .=
", ".($this->fk_delivery_address > 0 ? ((int) $this->fk_delivery_address) :
'NULL');
1018 $sql .=
", ".(!empty($this->shipping_method_id) && $this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) :
'NULL');
1019 $sql .=
", ".(!empty($this->warehouse_id) && $this->warehouse_id > 0 ? ((int) $this->warehouse_id) :
'NULL');
1020 $sql .=
", ".(int) $this->fk_incoterms;
1021 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
1022 $sql .=
", ".(int) $this->entity;
1023 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
1024 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
1025 $sql .=
", ".(int) $this->fk_multicurrency;
1026 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1027 $sql .=
", ".(float) $this->multicurrency_tx;
1030 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1031 $resql = $this->db->query($sql);
1033 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commande');
1036 $fk_parent_line = 0;
1037 $num = count($this->lines);
1042 for ($i = 0; $i < $num; $i++) {
1043 $line = $this->lines[$i];
1047 if (!is_object($line)) {
1048 $line = (object) $line;
1052 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1053 $fk_parent_line = 0;
1057 $vatrate = $line->tva_tx;
1058 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', (
string) $vatrate)) {
1059 $vatrate .=
' ('.$line->vat_src_code.
')';
1063 $originid = $line->origin_id;
1064 $origintype = $line->origin;
1066 $originid = $line->id;
1067 $origintype = $this->element;
1071 if (empty($line->ref_ext)) {
1072 $line->ref_ext =
'';
1080 $line->localtax1_tx,
1081 $line->localtax2_tx,
1083 $line->remise_percent,
1085 $line->fk_remise_except,
1090 $line->product_type,
1092 $line->special_code,
1094 $line->fk_fournprice,
1097 $line->array_options,
1107 if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) {
1108 $this->error = $this->db->lasterror();
1109 $this->errors[] = $this->error;
1112 $this->db->rollback();
1116 if ($result > 0 && $line->product_type == 9) {
1117 $fk_parent_line = $result;
1124 $initialref =
'(PROV'.$this->id.
')';
1125 if (!empty($this->
ref)) {
1126 $initialref = $this->ref;
1129 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
" SET ref='".$this->db->escape($initialref).
"' WHERE rowid=".((int) $this->
id);
1130 if ($this->db->query($sql)) {
1131 $this->
ref = $initialref;
1133 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1134 $this->linked_objects = $this->linkedObjectsIds;
1138 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1139 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1140 if (is_array($tmp_origin_id)) {
1141 foreach ($tmp_origin_id as $origin_id) {
1144 $this->error = $this->db->lasterror();
1149 $origin_id = $tmp_origin_id;
1152 $this->error = $this->db->lasterror();
1159 if (!$error && $this->
id &&
getDolGlobalString(
'MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) {
1160 $originforcontact = $this->origin;
1161 $originidforcontact = $this->origin_id;
1162 if ($originforcontact ==
'shipping') {
1163 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1165 $exp->fetch($this->origin_id);
1166 $exp->fetchObjectLinked();
1167 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1168 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1169 $originforcontact =
'commande';
1170 if (is_object($value)) {
1171 $originidforcontact = $value->id;
1173 $originidforcontact = $value;
1180 $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";
1181 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact).
"'";
1183 $resqlcontact = $this->db->query($sqlcontact);
1184 if ($resqlcontact) {
1185 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1187 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1201 if (!$error && !$notrigger) {
1211 $this->db->commit();
1214 $this->db->rollback();
1218 $this->error = $this->db->lasterror();
1219 $this->db->rollback();
1226 $this->error = $this->db->lasterror();
1227 $this->db->rollback();
1242 global
$conf, $user, $hookmanager;
1249 foreach ($this->lines as $line) {
1250 $line->fetch_optionals();
1254 $objFrom = clone $this;
1257 if (!empty($socid) && $socid != $this->socid) {
1258 $objsoc =
new Societe($this->db);
1260 if ($objsoc->fetch($socid) > 0) {
1261 $this->socid = $objsoc->id;
1262 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1263 $this->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent : 0);
1264 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1265 $this->fk_project = 0;
1266 $this->fk_delivery_address = 0;
1277 $this->user_author_id = $user->id;
1278 $this->user_validation_id = 0;
1280 $this->date_commande =
dol_now();
1281 $this->date_creation =
'';
1282 $this->date_validation =
'';
1284 $this->ref_client =
'';
1285 $this->ref_customer =
'';
1289 $num = count($this->lines);
1290 for ($i = 0; $i < $num; $i++) {
1291 $this->lines[$i]->ref_ext =
'';
1295 $this->context[
'createfromclone'] =
'createfromclone';
1296 $result = $this->
create($user);
1310 if ($this->socid == $objFrom->socid) {
1319 if (is_object($hookmanager)) {
1320 $parameters = array(
'objFrom' => $objFrom);
1322 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1330 unset($this->context[
'createfromclone']);
1334 $this->db->commit();
1337 $this->db->rollback();
1352 global
$conf, $hookmanager;
1354 require_once DOL_DOCUMENT_ROOT .
'/multicurrency/class/multicurrency.class.php';
1355 require_once DOL_DOCUMENT_ROOT .
'/core/class/extrafields.class.php';
1359 $this->date_commande =
dol_now();
1364 for ($i = 0; $i < $num; $i++) {
1367 $line->libelle =
$object->lines[$i]->libelle;
1368 $line->label =
$object->lines[$i]->label;
1369 $line->desc =
$object->lines[$i]->desc;
1370 $line->price =
$object->lines[$i]->price;
1371 $line->subprice =
$object->lines[$i]->subprice;
1372 $line->vat_src_code =
$object->lines[$i]->vat_src_code;
1373 $line->tva_tx =
$object->lines[$i]->tva_tx;
1374 $line->localtax1_tx =
$object->lines[$i]->localtax1_tx;
1375 $line->localtax2_tx =
$object->lines[$i]->localtax2_tx;
1376 $line->qty =
$object->lines[$i]->qty;
1377 $line->fk_remise_except =
$object->lines[$i]->fk_remise_except;
1378 $line->remise_percent =
$object->lines[$i]->remise_percent;
1379 $line->fk_product =
$object->lines[$i]->fk_product;
1380 $line->info_bits =
$object->lines[$i]->info_bits;
1381 $line->product_type =
$object->lines[$i]->product_type;
1382 $line->rang =
$object->lines[$i]->rang;
1383 $line->special_code =
$object->lines[$i]->special_code;
1384 $line->fk_parent_line =
$object->lines[$i]->fk_parent_line;
1385 $line->fk_unit =
$object->lines[$i]->fk_unit;
1387 $line->date_start =
$object->lines[$i]->date_start;
1388 $line->date_end =
$object->lines[$i]->date_end;
1390 $line->fk_fournprice =
$object->lines[$i]->fk_fournprice;
1392 $line->pa_ht = $marginInfos[0];
1393 $line->marge_tx = $marginInfos[1];
1394 $line->marque_tx = $marginInfos[2];
1396 $line->origin =
$object->element;
1397 $line->origin_id =
$object->lines[$i]->id;
1400 $object->lines[$i]->fetch_optionals();
1401 foreach (
$object->lines[$i]->array_options as $options_key => $value) {
1402 $line->array_options[$options_key] = $value;
1405 $this->lines[$i] = $line;
1408 $this->entity =
$object->entity;
1409 $this->socid =
$object->socid;
1410 $this->fk_project =
$object->fk_project;
1411 $this->cond_reglement_id =
$object->cond_reglement_id;
1412 $this->deposit_percent =
$object->deposit_percent;
1413 $this->mode_reglement_id =
$object->mode_reglement_id;
1414 $this->fk_account =
$object->fk_account;
1415 $this->availability_id =
$object->availability_id;
1416 $this->demand_reason_id =
$object->demand_reason_id;
1417 $this->delivery_date =
$object->delivery_date;
1418 $this->shipping_method_id =
$object->shipping_method_id;
1419 $this->warehouse_id =
$object->warehouse_id;
1420 $this->fk_delivery_address =
$object->fk_delivery_address;
1421 $this->contact_id =
$object->contact_id;
1422 $this->ref_client =
$object->ref_client;
1423 $this->ref_customer =
$object->ref_client;
1426 $this->note_private =
$object->note_private;
1427 $this->note_public =
$object->note_public;
1430 $this->origin =
$object->element;
1431 $this->origin_id =
$object->id;
1434 if (isModEnabled(
'multicurrency')) {
1435 if (!empty(
$object->multicurrency_code)) {
1436 $this->multicurrency_code =
$object->multicurrency_code;
1439 $this->multicurrency_tx =
$object->multicurrency_tx;
1442 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1444 $this->fk_multicurrency = $tmparray[0];
1445 $this->multicurrency_tx = $tmparray[1];
1449 if (empty($this->fk_multicurrency)) {
1450 $this->multicurrency_code =
$conf->currency;
1451 $this->fk_multicurrency = 0;
1452 $this->multicurrency_tx = 1;
1460 $element_extrafields = $e->fetch_name_optionals_label($this->table_element);
1462 foreach (
$object->array_options as $options_key => $value) {
1463 if (array_key_exists(str_replace(
'options_',
'', $options_key), $element_extrafields)) {
1464 $this->array_options[$options_key] = $value;
1468 $this->linked_objects[$this->origin] = $this->origin_id;
1469 if (isset(
$object->other_linked_objects) && is_array(
$object->other_linked_objects) && !empty(
$object->other_linked_objects)) {
1470 $this->linked_objects = array_merge($this->linked_objects,
$object->other_linked_objects);
1473 $ret = $this->
create($user);
1477 $hookmanager->initHooks(array(
'orderdao'));
1479 $parameters = array(
'objFrom' =>
$object);
1481 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1491 $this->
valid($user);
1543 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)
1545 global $mysoc, $langs, $user;
1547 $logtext =
"::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent";
1548 $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";
1549 $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";
1550 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
1552 if ($this->statut == self::STATUS_DRAFT) {
1553 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1557 if (empty($remise_percent)) {
1558 $remise_percent = 0;
1563 if (empty($info_bits)) {
1569 if (empty($txtva)) {
1572 if (empty($txlocaltax1)) {
1575 if (empty($txlocaltax2)) {
1578 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1579 $fk_parent_line = 0;
1581 if (empty($this->fk_multicurrency)) {
1582 $this->fk_multicurrency = 0;
1584 if (empty($ref_ext)) {
1588 $remise_percent = (float)
price2num($remise_percent);
1591 $pu_ht_devise =
price2num($pu_ht_devise);
1594 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
1599 if ($price_base_type ==
'HT') {
1604 $label = trim($label);
1605 $desc = trim($desc);
1612 if ($date_start && $date_end && $date_start > $date_end) {
1613 $langs->load(
"errors");
1614 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1620 $product_type = $type;
1621 if (!empty($fk_product) && $fk_product > 0) {
1622 $product =
new Product($this->db);
1623 $result = $product->fetch($fk_product);
1624 $product_type = $product->type;
1626 if (
getDolGlobalString(
'STOCK_MUST_BE_ENOUGH_FOR_ORDER') && $product_type == 0 && $product->stock_reel < $qty) {
1627 $langs->load(
"errors");
1628 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
1629 $this->errors[] = $this->error;
1630 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
1631 $this->db->rollback();
1645 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1646 $vat_src_code = $reg[1];
1647 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1650 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
1660 $total_ht = $tabprice[0];
1661 $total_tva = $tabprice[1];
1662 $total_ttc = $tabprice[2];
1663 $total_localtax1 = $tabprice[9];
1664 $total_localtax2 = $tabprice[10];
1665 $pu_ht = $tabprice[3];
1668 $multicurrency_total_ht = $tabprice[16];
1669 $multicurrency_total_tva = $tabprice[17];
1670 $multicurrency_total_ttc = $tabprice[18];
1671 $pu_ht_devise = $tabprice[19];
1676 if ($ranktouse == -1) {
1677 $rangmax = $this->
line_max($fk_parent_line);
1678 $ranktouse = $rangmax + 1;
1685 if ($remise_percent > 0) {
1686 $remise = round(((
float) $pu * $remise_percent / 100), 2);
1687 $price = (float) $pu - $remise;
1693 $this->line->context = $this->context;
1695 $this->line->fk_commande = $this->id;
1696 $this->line->label = $label;
1697 $this->line->desc = $desc;
1698 $this->line->qty = $qty;
1699 $this->line->ref_ext = $ref_ext;
1701 $this->line->vat_src_code = $vat_src_code;
1702 $this->line->tva_tx = $txtva;
1703 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1704 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1705 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1706 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1707 $this->line->fk_product = $fk_product;
1708 $this->line->product_type = $product_type;
1709 $this->line->fk_remise_except = $fk_remise_except;
1710 $this->line->remise_percent = $remise_percent;
1711 $this->line->subprice = (float) $pu_ht;
1712 $this->line->rang = $ranktouse;
1713 $this->line->info_bits = $info_bits;
1714 $this->line->total_ht = (float) $total_ht;
1715 $this->line->total_tva = (float) $total_tva;
1716 $this->line->total_localtax1 = (float) $total_localtax1;
1717 $this->line->total_localtax2 = (float) $total_localtax2;
1718 $this->line->total_ttc = (float) $total_ttc;
1719 $this->line->special_code = $special_code;
1720 $this->line->origin = $origin;
1721 $this->line->origin_id = $origin_id;
1722 $this->line->fk_parent_line = $fk_parent_line;
1723 $this->line->fk_unit = $fk_unit;
1725 $this->line->date_start = $date_start;
1726 $this->line->date_end = $date_end;
1728 $this->line->fk_fournprice = $fk_fournprice;
1729 $this->line->pa_ht = $pa_ht;
1732 $this->line->fk_multicurrency = $this->fk_multicurrency;
1733 $this->line->multicurrency_code = $this->multicurrency_code;
1734 $this->line->multicurrency_subprice = (float) $pu_ht_devise;
1735 $this->line->multicurrency_total_ht = (float) $multicurrency_total_ht;
1736 $this->line->multicurrency_total_tva = (float) $multicurrency_total_tva;
1737 $this->line->multicurrency_total_ttc = (float) $multicurrency_total_ttc;
1740 $this->line->price = $price;
1742 if (is_array($array_options) && count($array_options) > 0) {
1743 $this->line->array_options = $array_options;
1746 $result = $this->line->insert($user);
1749 if (empty($noupdateafterinsertline)) {
1754 if (!isset($this->context[
'createfromclone'])) {
1755 if (!empty($fk_parent_line)) {
1758 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
1760 foreach ($this->lines as $tmpline) {
1761 if ($tmpline->rang >= $ranktouse) {
1762 if (!empty($tmpline->id)) {
1769 $this->lines[] = $this->line;
1772 $this->db->commit();
1773 return $this->line->id;
1775 $this->db->rollback();
1779 $this->error = $this->line->error;
1780 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1781 $this->db->rollback();
1785 dol_syslog(get_class($this).
"::addline status of order must be Draft to allow use of ->addline()", LOG_ERR);
1805 public function add_product($idproduct, $qty, $remise_percent = 0.0, $date_start =
'', $date_end =
'')
1808 global
$conf, $mysoc;
1814 if ($idproduct > 0) {
1815 $prod =
new Product($this->db);
1816 $prod->fetch($idproduct);
1820 if (empty($tva_tx)) {
1825 $localtax1_tx =
get_localtax($tva_tx, 1, $this->thirdparty, $mysoc, $tva_npr);
1826 $localtax2_tx =
get_localtax($tva_tx, 2, $this->thirdparty, $mysoc, $tva_npr);
1830 $price = $prod->multiprices[$this->thirdparty->price_level];
1832 $price = $prod->price;
1837 $line->context = $this->context;
1839 $line->fk_product = $idproduct;
1840 $line->desc = $prod->description;
1842 $line->subprice = $price;
1843 $line->remise_percent = $remise_percent;
1844 $line->vat_src_code = $vat_src_code;
1845 $line->tva_tx = $tva_tx;
1846 $line->localtax1_tx = $localtax1_tx;
1847 $line->localtax2_tx = $localtax2_tx;
1849 $line->product_ref = $prod->ref;
1850 $line->product_label = $prod->label;
1851 $line->product_desc = $prod->description;
1852 $line->fk_unit = $prod->fk_unit;
1856 $line->date_start = $date_start;
1859 $line->date_end = $date_end;
1862 $this->lines[] = $line;
1894 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
1897 if (empty($id) && empty($ref) && empty($ref_ext)) {
1901 $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';
1902 $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';
1903 $sql .=
', c.fk_account';
1904 $sql .=
', c.date_commande, c.date_valid, c.tms';
1905 $sql .=
', c.date_livraison as delivery_date';
1906 $sql .=
', c.fk_shipping_method';
1907 $sql .=
', c.fk_warehouse';
1908 $sql .=
', c.fk_projet as fk_project, c.source, c.facture as billed';
1909 $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';
1910 $sql .=
', c.fk_incoterms, c.location_incoterms';
1911 $sql .=
", c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc";
1912 $sql .=
", c.module_source, c.pos_source";
1913 $sql .=
", i.libelle as label_incoterms";
1914 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
1915 $sql .=
', cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle, cr.libelle_facture as cond_reglement_libelle_doc';
1916 $sql .=
', ca.code as availability_code, ca.label as availability_label';
1917 $sql .=
', dr.code as demand_reason_code';
1918 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as c';
1919 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON c.fk_cond_reglement = cr.rowid';
1920 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON c.fk_mode_reglement = p.id';
1921 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON c.fk_availability = ca.rowid';
1922 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON c.fk_input_reason = dr.rowid';
1923 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
1926 $sql .=
" WHERE c.rowid=".((int) $id);
1928 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
1932 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
1935 $sql .=
" AND c.ref_ext='".$this->db->escape($ref_ext).
"'";
1938 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1939 $result = $this->db->query($sql);
1941 $obj = $this->db->fetch_object($result);
1943 $this->
id = $obj->rowid;
1944 $this->entity = $obj->entity;
1946 $this->
ref = $obj->ref;
1947 $this->ref_client = $obj->ref_client;
1948 $this->ref_customer = $obj->ref_client;
1949 $this->ref_ext = $obj->ref_ext;
1951 $this->socid = $obj->fk_soc;
1952 $this->thirdparty =
null;
1954 $this->fk_project = $obj->fk_project;
1955 $this->project =
null;
1957 $this->statut = $obj->status;
1958 $this->
status = $obj->status;
1960 $this->user_author_id = $obj->fk_user_author;
1961 $this->user_creation_id = $obj->fk_user_author;
1962 $this->user_validation_id = $obj->fk_user_valid;
1963 $this->user_modification_id = $obj->fk_user_modif;
1964 $this->total_ht = $obj->total_ht;
1965 $this->total_tva = $obj->total_tva;
1966 $this->total_localtax1 = $obj->total_localtax1;
1967 $this->total_localtax2 = $obj->total_localtax2;
1968 $this->total_ttc = $obj->total_ttc;
1969 $this->date = $this->db->jdate($obj->date_commande);
1970 $this->date_commande = $this->db->jdate($obj->date_commande);
1971 $this->date_creation = $this->db->jdate($obj->date_creation);
1972 $this->date_validation = $this->db->jdate($obj->date_valid);
1973 $this->date_modification = $this->db->jdate($obj->tms);
1974 $this->source = $obj->source;
1975 $this->billed = $obj->billed;
1976 $this->note = $obj->note_private;
1977 $this->note_private = $obj->note_private;
1978 $this->note_public = $obj->note_public;
1979 $this->model_pdf = $obj->model_pdf;
1980 $this->last_main_doc = $obj->last_main_doc;
1981 $this->mode_reglement_id = $obj->fk_mode_reglement;
1982 $this->mode_reglement_code = $obj->mode_reglement_code;
1983 $this->mode_reglement = $obj->mode_reglement_libelle;
1984 $this->cond_reglement_id = $obj->fk_cond_reglement;
1985 $this->cond_reglement_code = $obj->cond_reglement_code;
1986 $this->cond_reglement = $obj->cond_reglement_libelle;
1987 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1988 $this->deposit_percent = $obj->deposit_percent;
1989 $this->fk_account = $obj->fk_account;
1990 $this->availability_id = $obj->fk_availability;
1991 $this->availability_code = $obj->availability_code;
1993 $this->demand_reason_id = $obj->fk_input_reason;
1994 $this->demand_reason_code = $obj->demand_reason_code;
1995 $this->delivery_date = $this->db->jdate($obj->delivery_date);
1996 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method :
null;
1997 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse :
null;
1998 $this->fk_delivery_address = $obj->fk_delivery_address;
1999 $this->module_source = $obj->module_source;
2000 $this->pos_source = $obj->pos_source;
2003 $this->fk_incoterms = $obj->fk_incoterms;
2004 $this->location_incoterms = $obj->location_incoterms;
2005 $this->label_incoterms = $obj->label_incoterms;
2008 $this->fk_multicurrency = $obj->fk_multicurrency;
2009 $this->multicurrency_code = $obj->multicurrency_code;
2010 $this->multicurrency_tx = $obj->multicurrency_tx;
2011 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
2012 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
2013 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
2015 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
2017 $this->lines = array();
2023 $this->db->free($result);
2032 $this->error =
'Order with id '.$id.
' not found sql='.$sql;
2036 $this->error = $this->db->error();
2054 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2055 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
2060 $result = $remise->fetch($idremise);
2063 if ($remise->fk_facture) {
2064 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2065 $this->db->rollback();
2071 $line->fk_commande = $this->id;
2072 $line->fk_remise_except = $remise->id;
2073 $line->desc = $remise->description;
2074 $line->vat_src_code = $remise->vat_src_code;
2075 $line->tva_tx = $remise->tva_tx;
2076 $line->subprice = -(float) $remise->amount_ht;
2077 $line->price = -(float) $remise->amount_ht;
2078 $line->fk_product = 0;
2080 $line->remise_percent = 0;
2082 $line->info_bits = 2;
2084 $line->total_ht = -(float) $remise->amount_ht;
2085 $line->total_tva = -(float) $remise->amount_tva;
2086 $line->total_ttc = -(float) $remise->amount_ttc;
2088 $result = $line->insert();
2092 $this->db->commit();
2095 $this->db->rollback();
2099 $this->error = $line->error;
2100 $this->errors = $line->errors;
2101 $this->db->rollback();
2105 $this->db->rollback();
2119 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2122 global $langs,
$conf;
2124 $this->lines = array();
2126 $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,';
2127 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.fk_remise_except, l.remise_percent, l.subprice, l.fk_product_fournisseur_price as fk_fournprice, l.buy_price_ht as pa_ht, l.rang, l.info_bits, l.special_code,';
2128 $sql .=
' l.total_ht, l.total_ttc, l.total_tva, l.total_localtax1, l.total_localtax2, l.date_start, l.date_end,';
2129 $sql .=
' l.fk_unit,';
2130 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2131 $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,';
2132 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units';
2133 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element_line.
' as l';
2134 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (p.rowid = l.fk_product)';
2135 $sql .=
' WHERE l.fk_commande = '.((int) $this->
id);
2136 if ($only_product) {
2137 $sql .=
' AND p.fk_product_type = 0';
2139 $sql .=
' ORDER BY l.rang, l.rowid';
2141 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
2142 $result = $this->db->query($sql);
2144 $num = $this->db->num_rows($result);
2148 $objp = $this->db->fetch_object($result);
2152 $line->rowid = $objp->rowid;
2153 $line->id = $objp->rowid;
2154 $line->fk_commande = $objp->fk_commande;
2155 $line->commande_id = $objp->fk_commande;
2156 $line->label = $objp->custom_label;
2157 $line->desc = $objp->description;
2158 $line->description = $objp->description;
2159 $line->product_type = $objp->product_type;
2160 $line->qty = $objp->qty;
2161 $line->ref_ext = $objp->ref_ext;
2163 $line->vat_src_code = $objp->vat_src_code;
2164 $line->tva_tx = $objp->tva_tx;
2165 $line->localtax1_tx = $objp->localtax1_tx;
2166 $line->localtax2_tx = $objp->localtax2_tx;
2167 $line->localtax1_type = $objp->localtax1_type;
2168 $line->localtax2_type = $objp->localtax2_type;
2169 $line->total_ht = $objp->total_ht;
2170 $line->total_ttc = $objp->total_ttc;
2171 $line->total_tva = $objp->total_tva;
2172 $line->total_localtax1 = $objp->total_localtax1;
2173 $line->total_localtax2 = $objp->total_localtax2;
2174 $line->subprice = $objp->subprice;
2175 $line->fk_remise_except = $objp->fk_remise_except;
2176 $line->remise_percent = $objp->remise_percent;
2177 $line->price = $objp->price;
2178 $line->fk_product = $objp->fk_product;
2179 $line->fk_fournprice = $objp->fk_fournprice;
2180 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2181 $line->pa_ht = $marginInfos[0];
2182 $line->marge_tx = $marginInfos[1];
2183 $line->marque_tx = $marginInfos[2];
2184 $line->rang = $objp->rang;
2185 $line->info_bits = $objp->info_bits;
2186 $line->special_code = $objp->special_code;
2187 $line->fk_parent_line = $objp->fk_parent_line;
2189 $line->ref = $objp->product_ref;
2190 $line->libelle = $objp->product_label;
2192 $line->product_ref = $objp->product_ref;
2193 $line->product_label = $objp->product_label;
2194 $line->product_tosell = $objp->product_tosell;
2195 $line->product_tobuy = $objp->product_tobuy;
2196 $line->product_desc = $objp->product_desc;
2197 $line->product_tobatch = $objp->product_tobatch;
2198 $line->product_barcode = $objp->product_barcode;
2200 $line->fk_product_type = $objp->fk_product_type;
2201 $line->fk_unit = $objp->fk_unit;
2203 $line->weight = $objp->weight;
2204 $line->weight_units = $objp->weight_units;
2205 $line->volume = $objp->volume;
2206 $line->volume_units = $objp->volume_units;
2208 $line->date_start = $this->db->jdate($objp->date_start);
2209 $line->date_end = $this->db->jdate($objp->date_end);
2212 $line->fk_multicurrency = $objp->fk_multicurrency;
2213 $line->multicurrency_code = $objp->multicurrency_code;
2214 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2215 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2216 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2217 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2219 $line->fetch_optionals();
2222 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2223 $tmpproduct =
new Product($this->db);
2224 $tmpproduct->fetch($objp->fk_product);
2225 $tmpproduct->getMultiLangs();
2227 $line->multilangs = $tmpproduct->multilangs;
2230 $this->lines[$i] = $line;
2235 $this->db->free($result);
2239 $this->error = $this->db->error();
2253 foreach ($this->lines as $line) {
2254 if ($line->product_type == 0) {
2269 foreach ($this->lines as $line) {
2270 if ($line->product_type == 1) {
2286 $sql =
'SELECT COUNT(DISTINCT ed.fk_expedition) as nb';
2287 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2288 $sql .=
' '.MAIN_DB_PREFIX.$this->table_element_line.
' as cd';
2290 $sql .=
' ed.fk_elementdet = cd.rowid';
2291 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2294 dol_syslog(get_class($this).
"::getNbOfShipments", LOG_DEBUG);
2295 $resql = $this->db->query($sql);
2297 $obj = $this->db->fetch_object($resql);
2302 $this->db->free($resql);
2305 $this->error = $this->db->lasterror();
2320 $this->expeditions = array();
2322 $sql =
'SELECT cd.rowid, cd.fk_product,';
2323 $sql .=
' sum(ed.qty) as qty';
2324 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2325 if ($filtre_statut >= 0) {
2326 $sql .=
' '.MAIN_DB_PREFIX.
'expedition as e,';
2328 $sql .=
' '.MAIN_DB_PREFIX.$this->table_element_line.
' as cd';
2330 if ($filtre_statut >= 0) {
2331 $sql .=
' ed.fk_expedition = e.rowid AND';
2333 $sql .=
' ed.fk_elementdet = cd.rowid';
2334 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2335 if ($fk_product > 0) {
2336 $sql .=
' AND cd.fk_product = '.((int) $fk_product);
2338 if ($filtre_statut >= 0) {
2339 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
2341 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
2344 dol_syslog(get_class($this).
"::loadExpeditions", LOG_DEBUG);
2345 $resql = $this->db->query($sql);
2347 $num = $this->db->num_rows($resql);
2350 $obj = $this->db->fetch_object($resql);
2351 $this->expeditions[$obj->rowid] = $obj->qty;
2354 $this->db->free($resql);
2357 $this->error = $this->db->lasterror();
2369 $sql =
'SELECT count(*)';
2370 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expedition as e';
2371 $sql .=
', '.MAIN_DB_PREFIX.
'element_element as el';
2372 $sql .=
' WHERE el.fk_source = '.((int) $this->
id);
2373 $sql .=
" AND el.sourcetype = 'commande'";
2374 $sql .=
" AND el.fk_target = e.rowid";
2375 $sql .=
" AND el.targettype = 'shipping'";
2377 $resql = $this->db->query($sql);
2379 $row = $this->db->fetch_row($resql);
2437 if ($this->statut == self::STATUS_DRAFT) {
2443 $line->context = $this->context;
2446 $line->fetch($lineid);
2448 if ($id > 0 && $line->fk_commande != $id) {
2449 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2454 $staticline = clone $line;
2455 $line->oldline = $staticline;
2457 if ($line->delete($user) > 0) {
2461 $this->db->commit();
2464 $this->db->rollback();
2465 $this->error = $this->db->lasterror();
2469 $this->db->rollback();
2470 $this->error = $line->error;
2474 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
2493 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
2495 return $this->
setDiscount($user, $remise, $notrigger);
2508 $remise = trim((
string) $remise) ? trim((
string) $remise) : 0;
2510 if ($user->hasRight(
'commande',
'creer')) {
2517 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2518 $sql .=
' SET remise_percent = '.((float) $remise);
2519 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.((int) self::STATUS_DRAFT);
2522 $resql = $this->db->query($sql);
2524 $this->errors[] = $this->db->error();
2529 $this->oldcopy = clone $this;
2530 $this->remise_percent = $remise;
2534 if (!$notrigger && empty($error)) {
2544 $this->db->commit();
2547 foreach ($this->errors as $errmsg) {
2548 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2549 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2551 $this->db->rollback();
2639 if ($user->hasRight(
'commande',
'creer')) {
2644 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
2645 $sql .=
" SET date_commande = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
2646 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut = ".((int) self::STATUS_DRAFT);
2649 $resql = $this->db->query($sql);
2651 $this->errors[] = $this->db->error();
2656 $this->oldcopy = clone $this;
2657 $this->date = $date;
2660 if (!$notrigger && empty($error)) {
2670 $this->db->commit();
2673 foreach ($this->errors as $errmsg) {
2674 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2675 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2677 $this->db->rollback();
2711 if ($user->hasRight(
'commande',
'creer')) {
2716 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
2717 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2718 $sql .=
" WHERE rowid = ".((int) $this->
id);
2721 $resql = $this->db->query($sql);
2723 $this->errors[] = $this->db->error();
2728 $this->oldcopy = clone $this;
2729 $this->delivery_date = $delivery_date;
2732 if (!$notrigger && empty($error)) {
2742 $this->db->commit();
2745 foreach ($this->errors as $errmsg) {
2746 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2747 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2749 $this->db->rollback();
2771 public function liste_array($shortlist = 0, $draft = 0, $excluser =
null, $socid = 0, $limit = 0, $offset = 0, $sortfield =
'c.date_commande', $sortorder =
'DESC')
2778 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2779 $sql .=
" c.rowid as cid, c.ref";
2780 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
2781 $sql .=
", sc.fk_soc, sc.fk_user";
2783 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.$this->table_element.
" as c";
2784 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
2785 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2787 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2788 $sql .=
" AND c.fk_soc = s.rowid";
2789 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
2790 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2793 $sql .=
" AND s.rowid = ".((int) $socid);
2796 $sql .=
" AND c.fk_statut = ".self::STATUS_DRAFT;
2798 if (is_object($excluser)) {
2799 $sql .=
" AND c.fk_user_author <> ".((int) $excluser->id);
2801 $sql .= $this->db->order($sortfield, $sortorder);
2802 $sql .= $this->db->plimit($limit, $offset);
2804 $result = $this->db->query($sql);
2806 $numc = $this->db->num_rows($result);
2809 while ($i < $numc) {
2810 $obj = $this->db->fetch_object($result);
2812 if ($shortlist == 1) {
2813 $ga[$obj->cid] = $obj->ref;
2814 } elseif ($shortlist == 2) {
2815 $ga[$obj->cid] = $obj->ref.
' ('.$obj->name.
')';
2817 $ga[$i][
'id'] = $obj->cid;
2818 $ga[$i][
'ref'] = $obj->ref;
2819 $ga[$i][
'name'] = $obj->name;
2842 dol_syslog(
'Commande::availability('.$availability_id.
')');
2843 if ($this->statut >= self::STATUS_DRAFT) {
2848 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2849 $sql .=
' SET fk_availability = '.((int) $availability_id);
2850 $sql .=
' WHERE rowid='.((int) $this->
id);
2853 $resql = $this->db->query($sql);
2855 $this->errors[] = $this->db->error();
2860 $this->oldcopy = clone $this;
2861 $this->availability_id = $availability_id;
2864 if (!$notrigger && empty($error)) {
2874 $this->db->commit();
2877 foreach ($this->errors as $errmsg) {
2878 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2879 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2881 $this->db->rollback();
2885 $error_str =
'Command status do not meet requirement '.$this->statut;
2887 $this->error = $error_str;
2888 $this->errors[] = $this->error;
2906 dol_syslog(
'Commande::demand_reason('.$demand_reason_id.
')');
2907 if ($this->statut >= self::STATUS_DRAFT) {
2912 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2913 $sql .=
' SET fk_input_reason = '.((int) $demand_reason_id);
2914 $sql .=
' WHERE rowid='.((int) $this->
id);
2917 $resql = $this->db->query($sql);
2919 $this->errors[] = $this->db->error();
2924 $this->oldcopy = clone $this;
2925 $this->demand_reason_id = $demand_reason_id;
2928 if (!$notrigger && empty($error)) {
2938 $this->db->commit();
2941 foreach ($this->errors as $errmsg) {
2942 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2943 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2945 $this->db->rollback();
2949 $error_str =
'order status do not meet requirement '.$this->statut;
2951 $this->error = $error_str;
2952 $this->errors[] = $this->error;
2969 if ($user->hasRight(
'commande',
'creer')) {
2974 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
' SET';
2975 $sql .=
' ref_client = '.(empty($ref_client) ?
'NULL' :
"'".$this->db->escape($ref_client).
"'");
2976 $sql .=
' WHERE rowid = '.((int) $this->
id);
2978 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2979 $resql = $this->db->query($sql);
2981 $this->errors[] = $this->db->error();
2986 $this->oldcopy = clone $this;
2987 $this->ref_client = $ref_client;
2988 $this->ref_customer = $ref_client;
2991 if (!$notrigger && empty($error)) {
3000 $this->db->commit();
3003 foreach ($this->errors as $errmsg) {
3004 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
3005 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3007 $this->db->rollback();
3026 if ($this->billed) {
3032 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
' SET facture = 1';
3035 dol_syslog(get_class($this).
"::classifyBilled", LOG_DEBUG);
3036 if ($this->db->query($sql)) {
3038 $this->oldcopy = clone $this;
3042 if (!$notrigger && empty($error)) {
3044 $result = $this->
call_trigger(
'ORDER_CLASSIFY_BILLED', $user);
3052 $this->db->commit();
3055 foreach ($this->errors as $errmsg) {
3056 dol_syslog(get_class($this).
"::classifyBilled ".$errmsg, LOG_ERR);
3057 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3059 $this->db->rollback();
3063 $this->error = $this->db->error();
3064 $this->db->rollback();
3082 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
' SET facture = 0';
3085 dol_syslog(get_class($this).
"::classifyUnBilled", LOG_DEBUG);
3086 if ($this->db->query($sql)) {
3088 $this->oldcopy = clone $this;
3092 if (!$notrigger && empty($error)) {
3094 $result = $this->
call_trigger(
'ORDER_CLASSIFY_UNBILLED', $user);
3104 $this->db->commit();
3107 foreach ($this->errors as $errmsg) {
3108 dol_syslog(get_class($this).
"::classifyUnBilled ".$errmsg, LOG_ERR);
3109 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3111 $this->db->rollback();
3115 $this->error = $this->db->error();
3116 $this->db->rollback();
3152 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)
3154 global
$conf, $mysoc, $langs, $user;
3156 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");
3157 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3164 if (empty($info_bits)) {
3167 if (empty($txtva)) {
3170 if (empty($txlocaltax1)) {
3173 if (empty($txlocaltax2)) {
3176 if (empty($remise_percent)) {
3177 $remise_percent = 0;
3179 if (empty($special_code) || $special_code == 3) {
3182 if (empty($ref_ext)) {
3186 if ($date_start && $date_end && $date_start > $date_end) {
3187 $langs->load(
"errors");
3188 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3192 $remise_percent = (float)
price2num($remise_percent);
3196 $pu_ht_devise =
price2num($pu_ht_devise);
3197 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
3200 $txlocaltax1 = (float)
price2num($txlocaltax1);
3201 $txlocaltax2 = (float)
price2num($txlocaltax2);
3215 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3216 $vat_src_code = $reg[1];
3217 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3220 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
3222 $total_ht = $tabprice[0];
3223 $total_tva = $tabprice[1];
3224 $total_ttc = $tabprice[2];
3225 $total_localtax1 = $tabprice[9];
3226 $total_localtax2 = $tabprice[10];
3227 $pu_ht = $tabprice[3];
3228 $pu_tva = $tabprice[4];
3229 $pu_ttc = $tabprice[5];
3232 $multicurrency_total_ht = $tabprice[16];
3233 $multicurrency_total_tva = $tabprice[17];
3234 $multicurrency_total_ttc = $tabprice[18];
3235 $pu_ht_devise = $tabprice[19];
3239 if ($price_base_type ==
'TTC') {
3240 $subprice = $pu_ttc;
3245 if ($remise_percent > 0) {
3246 $remise = round(((
float) $pu * $remise_percent / 100), 2);
3247 $price = ((float) $pu - $remise);
3252 $line->fetch($rowid);
3253 $line->fetch_optionals();
3255 if (!empty($line->fk_product)) {
3256 $product =
new Product($this->db);
3257 $result = $product->fetch($line->fk_product);
3258 $product_type = $product->type;
3260 if (
getDolGlobalString(
'STOCK_MUST_BE_ENOUGH_FOR_ORDER') && $product_type == 0 && $product->stock_reel < $qty) {
3261 $langs->load(
"errors");
3262 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
3263 $this->errors[] = $this->error;
3265 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
3267 $this->db->rollback();
3272 $staticline = clone $line;
3274 $line->oldline = $staticline;
3275 $this->line = $line;
3276 $this->line->context = $this->context;
3277 $this->line->rang = $rang;
3280 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
3281 $rangmax = $this->
line_max($fk_parent_line);
3282 $this->line->rang = $rangmax + 1;
3285 $this->line->id = $rowid;
3286 $this->line->label = $label;
3287 $this->line->desc = $desc;
3288 $this->line->qty = $qty;
3289 $this->line->ref_ext = $ref_ext;
3291 $this->line->vat_src_code = $vat_src_code;
3292 $this->line->tva_tx = $txtva;
3293 $this->line->localtax1_tx = $txlocaltax1;
3294 $this->line->localtax2_tx = $txlocaltax2;
3295 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3296 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3297 $this->line->remise_percent = $remise_percent;
3298 $this->line->subprice = (float) $pu_ht;
3299 $this->line->info_bits = $info_bits;
3300 $this->line->special_code = $special_code;
3301 $this->line->total_ht = (float) $total_ht;
3302 $this->line->total_tva = (float) $total_tva;
3303 $this->line->total_localtax1 = (float) $total_localtax1;
3304 $this->line->total_localtax2 = (float) $total_localtax2;
3305 $this->line->total_ttc = (float) $total_ttc;
3306 $this->line->date_start = $date_start;
3307 $this->line->date_end = $date_end;
3308 $this->line->product_type = $type;
3309 $this->line->fk_parent_line = $fk_parent_line;
3310 $this->line->skip_update_total = $skip_update_total;
3311 $this->line->fk_unit = $fk_unit;
3313 $this->line->fk_fournprice = $fk_fournprice;
3314 $this->line->pa_ht = $pa_ht;
3317 $this->line->multicurrency_subprice = (float) $pu_ht_devise;
3318 $this->line->multicurrency_total_ht = (float) $multicurrency_total_ht;
3319 $this->line->multicurrency_total_tva = (float) $multicurrency_total_tva;
3320 $this->line->multicurrency_total_ttc = (float) $multicurrency_total_ttc;
3323 $this->line->price = $price;
3325 if (is_array($array_options) && count($array_options) > 0) {
3327 foreach ($array_options as $key => $value) {
3328 $this->line->array_options[$key] = $array_options[$key];
3332 $result = $this->line->update($user, $notrigger);
3335 if (!empty($fk_parent_line)) {
3342 $this->db->commit();
3345 $this->error = $this->line->error;
3347 $this->db->rollback();
3351 $this->error = get_class($this).
"::updateline Order status makes operation forbidden";
3352 $this->errors = array(
'OrderStatusMakeOperationForbidden');
3371 if (isset($this->
ref)) {
3372 $this->
ref = trim($this->
ref);
3374 if (isset($this->ref_client)) {
3375 $this->ref_client = trim($this->ref_client);
3377 if (isset($this->ref_customer)) {
3378 $this->ref_customer = trim($this->ref_customer);
3380 if (isset($this->note) || isset($this->note_private)) {
3381 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
3383 if (isset($this->note_public)) {
3384 $this->note_public = trim($this->note_public);
3386 if (isset($this->model_pdf)) {
3387 $this->model_pdf = trim($this->model_pdf);
3389 if (isset($this->import_key)) {
3390 $this->import_key = trim($this->import_key);
3392 $delivery_date = $this->delivery_date;
3398 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
3400 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
3401 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
3402 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
3403 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
3404 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
3405 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
3406 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
3407 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
3408 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
3409 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
3410 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
3411 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
3412 $sql .=
" fk_user_modif=".(isset($user->id) ? $user->id :
"null").
",";
3413 $sql .=
" fk_user_valid=".((isset($this->user_validation_id) && $this->user_validation_id > 0) ? $this->user_validation_id :
"null").
",";
3414 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
3415 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
3416 $sql .=
" deposit_percent=".(!empty($this->deposit_percent) ? strval($this->deposit_percent) :
"null").
",";
3417 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
3418 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
3419 $sql .=
" fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id :
"null").
",";
3420 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
3421 $sql .=
" fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id :
"null").
",";
3422 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
3423 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
3424 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
3425 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
3427 $sql .=
" WHERE rowid=".((int) $this->
id);
3431 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3432 $resql = $this->db->query($sql);
3435 $this->errors[] =
"Error ".$this->db->lasterror();
3445 if (!$error && !$notrigger) {
3456 foreach ($this->errors as $errmsg) {
3457 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3458 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3460 $this->db->rollback();
3463 $this->db->commit();
3475 public function delete($user, $notrigger = 0)
3477 global
$conf, $langs;
3478 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3482 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
3497 $this->errors[] = $langs->trans(
'SomeShipmentExists');
3502 if (!$error && !empty($this->table_element_line)) {
3503 $tabletodelete = $this->table_element_line;
3504 $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).
")";
3505 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3506 if (!$this->db->query($sqlef) || !$this->db->query($sql)) {
3508 $this->error = $this->db->lasterror();
3509 $this->errors[] = $this->error;
3510 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3535 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3541 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
3542 $res = $this->db->query($sql);
3545 $this->error = $this->db->lasterror();
3546 $this->errors[] = $this->error;
3547 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3563 if ($conf->commande->multidir_output[$this->entity] && !empty($this->
ref)) {
3564 $dir =
$conf->commande->multidir_output[$this->entity].
"/".$ref;
3565 $file = $dir.
"/".$ref.
".pdf";
3566 if (file_exists($file)) {
3570 $this->error =
'ErrorFailToDeleteFile';
3571 $this->errors[] = $this->error;
3572 $this->db->rollback();
3576 if (file_exists($dir)) {
3579 $this->error =
'ErrorFailToDeleteDir';
3580 $this->errors[] = $this->error;
3581 $this->db->rollback();
3589 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3590 $this->db->commit();
3593 $this->db->rollback();
3610 global
$conf, $langs;
3614 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.date_livraison as delivery_date, c.fk_statut, c.total_ht";
3615 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as c";
3616 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
3617 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
3618 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3621 $sql .= $clause.
" c.entity IN (".
getEntity(
'commande').
")";
3623 if ($mode ==
'toship') {
3625 $sql .=
" AND c.fk_statut IN (" . self::STATUS_VALIDATED .
"," . self::STATUS_SHIPMENTONPROCESS .
")";
3627 if ($mode ==
'tobill') {
3629 $sql .=
" AND c.fk_statut IN (" . self::STATUS_VALIDATED .
"," . self::STATUS_SHIPMENTONPROCESS .
", " . self::STATUS_CLOSED .
") AND c.facture = 0";
3631 if ($mode ==
'shippedtobill') {
3633 $sql .=
" AND c.fk_statut IN (" . self::STATUS_CLOSED .
") AND c.facture = 0";
3636 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3639 $resql = $this->db->query($sql);
3642 $label = $labelShort = $url =
'';
3643 if ($mode ==
'toship') {
3644 $delay_warning =
$conf->commande->client->warning_delay / 60 / 60 / 24;
3645 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=-2&mainmenu=commercial&leftmenu=orders';
3646 $label = $langs->transnoentitiesnoconv(
"OrdersToProcess");
3647 $labelShort = $langs->transnoentitiesnoconv(
"Opened");
3649 if ($mode ==
'tobill') {
3650 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=-3&search_billed=0&mainmenu=commercial&leftmenu=orders';
3651 $label = $langs->trans(
"OrdersToBill");
3652 $labelShort = $langs->trans(
"ToBill");
3654 if ($mode ==
'shippedtobill') {
3655 $url = DOL_URL_ROOT.
'/commande/list.php?search_status=3&search_billed=0&mainmenu=commercial&leftmenu=orders';
3656 $label = $langs->trans(
"OrdersToBill");
3657 $labelShort = $langs->trans(
"StatusOrderDelivered").
' '.$langs->trans(
"and").
' '.$langs->trans(
"ToBill");
3662 $response->warning_delay = $delay_warning;
3663 $response->label = $label;
3664 $response->labelShort = $labelShort;
3665 $response->url = $url;
3666 $response->url_late = DOL_URL_ROOT.
'/commande/list.php?search_option=late&mainmenu=commercial&leftmenu=orders';
3669 $generic_commande =
new Commande($this->db);
3671 while ($obj = $this->db->fetch_object($resql)) {
3672 $response->nbtodo++;
3673 $response->total += $obj->total_ht;
3675 $generic_commande->statut = $obj->fk_statut;
3676 $generic_commande->date_commande = $this->db->jdate($obj->date_commande);
3677 $generic_commande->date = $this->db->jdate($obj->date_commande);
3678 $generic_commande->delivery_date = $this->db->jdate($obj->delivery_date);
3680 if ($mode ==
'toship' && $generic_commande->hasDelay()) {
3681 $response->nbtodolate++;
3687 $this->error = $this->db->error();
3701 $label = $langs->trans(
'OrderSource'.$this->source);
3703 if ($label ==
'OrderSource') {
3717 return $this->
LibStatut($this->statut, $this->billed, $mode);
3730 public function LibStatut($status, $billed, $mode, $donotshowbilled = 0)
3733 global $langs, $hookmanager;
3736 if (empty($donotshowbilled)) {
3737 $billedtext .= ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3742 if ($status == self::STATUS_CANCELED) {
3743 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderCanceled');
3744 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderCanceledShort');
3745 $statusType =
'status9';
3746 } elseif ($status == self::STATUS_DRAFT) {
3747 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDraft');
3748 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDraftShort');
3749 $statusType =
'status0';
3750 } elseif ($status == self::STATUS_VALIDATED) {
3751 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderValidated').$billedtext;
3752 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderValidatedShort').$billedtext;
3753 $statusType =
'status1';
3754 } elseif ($status == self::STATUS_SHIPMENTONPROCESS) {
3755 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderSent').$billedtext;
3756 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderSentShort').$billedtext;
3757 $labelTooltip = $langs->transnoentitiesnoconv(
"StatusOrderSent");
3758 if (!empty($this->delivery_date)) {
3759 $labelTooltip .=
' - '.$langs->transnoentitiesnoconv(
"DateDeliveryPlanned").dol_print_date($this->delivery_date,
'day').$billedtext;
3761 $statusType =
'status4';
3762 } elseif ($status == self::STATUS_CLOSED) {
3763 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDelivered').$billedtext;
3764 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDeliveredShort').$billedtext;
3765 $statusType =
'status6';
3767 $labelStatus = $langs->transnoentitiesnoconv(
'Unknown');
3768 $labelStatusShort =
'';
3773 $parameters = array(
3774 'status' => $status,
3776 'billed' => $billed,
3777 'donotshowbilled' => $donotshowbilled
3780 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
3783 return $hookmanager->resPrint;
3786 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', array(
'tooltip' => $labelTooltip));
3798 global
$conf, $langs, $user;
3800 $langs->load(
'orders');
3802 $nofetch = !empty($params[
'nofetch']);
3805 return [
'optimize' => $langs->trans(
"Order")];
3808 if ($user->hasRight(
'commande',
'lire')) {
3809 $datas[
'picto'] =
img_picto(
'', $this->picto,
'', 0, 0, 0,
'',
'paddingrightonly').
'<u>'.$langs->trans(
"Order").
'</u>';
3810 if (isset($this->statut)) {
3811 $datas[
'status'] =
' '.$this->getLibStatut(5);
3813 $datas[
'Ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
3815 $langs->load(
'companies');
3816 if (empty($this->thirdparty)) {
3819 $datas[
'customer'] =
'<br><b>'.$langs->trans(
'Customer').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
3821 $datas[
'RefCustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.(empty($this->ref_customer) ? (empty($this->ref_client) ?
'' : $this->ref_client) : $this->ref_customer);
3823 $langs->load(
'project');
3824 if (is_null($this->project) || (is_object($this->project) && $this->project->isEmpty())) {
3826 if ($res > 0 && $this->project instanceof
Project) {
3827 $datas[
'project'] =
'<br><b>'.$langs->trans(
'Project').
':</b> '.$this->project->getNomUrl(1,
'', 0, 1);
3831 if (!empty($this->total_ht)) {
3832 $datas[
'AmountHT'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1,
$conf->currency);
3834 if (!empty($this->total_tva)) {
3835 $datas[
'VAT'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1,
$conf->currency);
3837 if (!empty($this->total_ttc)) {
3838 $datas[
'AmountTTC'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1,
$conf->currency);
3840 if (!empty($this->date)) {
3841 $datas[
'Date'] =
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
3843 if (!empty($this->delivery_date)) {
3844 $datas[
'DeliveryDate'] =
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
3864 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0, $target =
'')
3866 global
$conf, $langs, $user, $hookmanager;
3868 if (!empty(
$conf->dol_no_mouse_hover)) {
3874 if (isModEnabled(
"shipping") && ($option ==
'1' || $option ==
'2')) {
3875 $url = DOL_URL_ROOT.
'/expedition/shipment.php?id='.$this->id;
3877 $url = DOL_URL_ROOT.
'/commande/card.php?id='.$this->id;
3880 if (!$user->hasRight(
'commande',
'lire')) {
3884 if ($option !==
'nolink') {
3886 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3887 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
3888 $add_save_lastsearch_values = 1;
3890 if ($add_save_lastsearch_values) {
3891 $url .=
'&save_lastsearch_values=1';
3900 'objecttype' => $this->element,
3901 'option' => $option,
3904 $classfortooltip =
'classfortooltip';
3907 $classfortooltip =
'classforajaxtooltip';
3908 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
3915 if (empty($notooltip) && $user->hasRight(
'commande',
'lire')) {
3917 $label = $langs->trans(
"Order");
3918 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
3920 $linkclose .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
3921 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
3923 $target_value = array(
'_self',
'_blank',
'_parent',
'_top');
3924 if (in_array($target, $target_value)) {
3925 $linkclose .=
' target="'.dol_escape_htmltag($target).
'"';
3929 $linkstart =
'<a href="'.$url.
'"';
3930 $linkstart .= $linkclose.
'>';
3933 if ($option ===
'nolink') {
3938 $result .= $linkstart;
3940 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
3942 if ($withpicto != 2) {
3943 $result .= $this->ref;
3945 $result .= $linkend;
3947 if ($addlinktonotes) {
3948 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3951 $result .=
' <span class="note inline-block">';
3952 $result .=
'<a href="'.DOL_URL_ROOT.
'/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3957 $result .=
'</span>';
3962 $hookmanager->initHooks(array($this->element .
'dao'));
3963 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
3964 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3966 $result = $hookmanager->resPrint;
3968 $result .= $hookmanager->resPrint;
3982 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem,';
3983 $sql .=
' date_valid as datev,';
3984 $sql .=
' date_cloture as datecloture,';
3985 $sql .=
' fk_user_author, fk_user_valid, fk_user_cloture';
3986 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as c';
3987 $sql .=
' WHERE c.rowid = '.((int) $id);
3988 $result = $this->db->query($sql);
3990 if ($this->db->num_rows($result)) {
3991 $obj = $this->db->fetch_object($result);
3992 $this->
id = $obj->rowid;
3993 if ($obj->fk_user_author) {
3994 $this->user_creation_id = $obj->fk_user_author;
3996 if ($obj->fk_user_valid) {
3997 $this->user_validation_id = $obj->fk_user_valid;
3999 if ($obj->fk_user_cloture) {
4000 $this->user_closing_id = $obj->fk_user_cloture;
4003 $this->date_creation = $this->db->jdate($obj->datec);
4004 $this->date_modification = $this->db->jdate($obj->datem);
4005 $this->date_validation = $this->db->jdate($obj->datev);
4006 $this->date_cloture = $this->db->jdate($obj->datecloture);
4009 $this->db->free($result);
4025 global
$conf, $langs;
4027 dol_syslog(get_class($this).
"::initAsSpecimen");
4032 $sql =
"SELECT rowid";
4033 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
4034 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
4035 $sql .= $this->db->plimit(100);
4037 $resql = $this->db->query($sql);
4039 $num_prods = $this->db->num_rows($resql);
4041 while ($i < $num_prods) {
4043 $row = $this->db->fetch_row($resql);
4044 $prodids[$i] = $row[0];
4050 $this->
ref =
'SPECIMEN';
4051 $this->specimen = 1;
4052 $this->entity =
$conf->entity;
4054 $this->date = time();
4055 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
4056 $this->cond_reglement_code =
'RECEP';
4057 $this->mode_reglement_code =
'CHQ';
4058 $this->availability_code =
'DSP';
4059 $this->demand_reason_code =
'SRC_00';
4061 $this->note_public =
'This is a comment (public)';
4062 $this->note_private =
'This is a comment (private)';
4064 $this->multicurrency_tx = 1;
4065 $this->multicurrency_code =
$conf->currency;
4067 $this->
status = $this::STATUS_DRAFT;
4073 while ($xnbp < $nbp) {
4076 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
4078 $line->subprice = 100;
4082 $line->total_ht = 50;
4083 $line->total_ttc = 60;
4084 $line->total_tva = 10;
4085 $line->remise_percent = 50;
4087 $line->total_ht = 100;
4088 $line->total_ttc = 120;
4089 $line->total_tva = 20;
4090 $line->remise_percent = 0;
4092 if ($num_prods > 0) {
4093 $prodid = mt_rand(1, $num_prods);
4094 $line->fk_product = $prodids[$prodid];
4095 $line->product_ref =
'SPECIMEN';
4098 $this->lines[$xnbp] = $line;
4100 $this->total_ht += $line->total_ht;
4101 $this->total_tva += $line->total_tva;
4102 $this->total_ttc += $line->total_ttc;
4120 $this->nb = array();
4123 $sql =
"SELECT count(co.rowid) as nb";
4124 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as co";
4125 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
4126 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
4127 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
4128 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
4131 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'commande').
")";
4133 $resql = $this->db->query($sql);
4135 while ($obj = $this->db->fetch_object($resql)) {
4136 $this->nb[
"orders"] = $obj->nb;
4138 $this->db->free($resql);
4142 $this->error = $this->db->error();
4168 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
4170 global
$conf, $langs;
4172 $langs->load(
"orders");
4173 $outputlangs->load(
"products");
4176 $modele =
'einstein';
4178 if (!empty($this->model_pdf)) {
4179 $modele = $this->model_pdf;
4185 $modelpath =
"core/modules/commande/doc/";
4187 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
4240 return max($this->date, $this->delivery_date) < ($now -
$conf->commande->client->warning_delay);
4250 global
$conf, $langs;
4252 if (empty($this->delivery_date)) {
4253 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date,
'day');
4255 $text = $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->delivery_date,
'day');
4257 $text .=
' '.($conf->commande->client->warning_delay > 0 ?
'+' :
'-').
' '.round(abs(
$conf->commande->client->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage customers orders.
getNbOfServicesLines()
Return number of line with type service.
getNbOfShipments()
Count number of shipments for this order.
createFromProposal($object, User $user)
Load an object from a proposal and create a new order into database.
setDraft($user, $idwarehouse=-1)
Set draft status.
setDeliveryDate($user, $delivery_date, $notrigger=0)
Set the planned delivery date.
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
getLinesArray()
Create an array of order lines.
showDelay()
Show the customer delayed info.
set_date($user, $date, $notrigger=0)
Set a fixed amount discount.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
loadStateBoard()
Load the indicators this->nb for the state board.
fetch_lines($only_product=0, $loadalsotranslation=0)
Load array lines.
createFromClone(User $user, $socid=0)
Load an object from its id and create a new one in database.
const STATUS_SHIPMENTONPROCESS
Shipment on process.
LibStatut($status, $billed, $mode, $donotshowbilled=0)
Return label of status.
getLibStatut($mode)
Return status label of Order.
hasDelay()
Is the sales order delayed?
const STATUS_CLOSED
Closed (Sent, billed or not)
valid($user, $idwarehouse=0, $notrigger=0)
Validate order.
getLabelSource()
Return source label of order.
set_remise($user, $remise, $notrigger=0)
Applique une remise relative.
const STATUS_CANCELED
Canceled status.
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0, $target='')
Return 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.
getNextNumRef($soc)
Returns the reference to the following non used Order depending on the active numbering module define...
addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $info_bits=0, $fk_remise_except=0, $price_base_type='HT', $pu_ttc=0, $date_start='', $date_end='', $type=0, $rang=-1, $special_code=0, $fk_parent_line=0, $fk_fournprice=null, $pa_ht=0, $label='', $array_options=array(), $fk_unit=null, $origin='', $origin_id=0, $pu_ht_devise=0, $ref_ext='', $noupdateafterinsertline=0)
Add an order line into database (linked to product/service or not)
getTooltipContentArray($params)
getTooltipContentArray
create($user, $notrigger=0)
Create order Note that this->ref can be set or empty.
demand_reason($demand_reason_id, $notrigger=0)
Update order demand_reason.
const STATUS_DRAFT
Draft status.
const STOCK_NOT_ENOUGH_FOR_ORDER
ERR Not enough stock.
initAsSpecimen()
Initialise an instance with random values.
insert_discount($idremise)
Add 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.
cloture($user, $notrigger=0)
Close order.
cancel($idwarehouse=-1)
Cancel an order If stock is decremented on order validation, we must reincrement it.
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)
Return a array with the pending stock by product.
countNbOfShipments()
Returns an array with expeditions lines number.
liste_array($shortlist=0, $draft=0, $excluser=null, $socid=0, $limit=0, $offset=0, $sortfield='c.date_commande', $sortorder='DESC')
Return list of orders (eventuelly filtered on a user) into an array.
fetch($id, $ref='', $ref_ext='', $notused='')
Get object from database.
set_date_livraison($user, $delivery_date, $notrigger=0)
Set delivery date.
add_product($idproduct, $qty, $remise_percent=0.0, $date_start='', $date_end='')
Add line into array $this->client must be loaded.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
setSignedStatus(User $user, int $status=0, int $notrigger=0, $triggercode='')
Set signed status.
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.
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
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)
deleteExtraFields()
Delete all extra fields values for the current object.
copy_linked_contact($objFrom, $source='internal')
Copy contact from one element to current.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
call_trigger($triggerName, $user)
Call trigger based on this instance.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Superclass for orders classes.
Class to manage absolute discounts.
Class to manage Dolibarr database access.
Class to manage stock movements.
static getIdFromCode($dbs, $code)
Get id of currency from code.
static getIdAndTxFromCode($dbs, $code, $date_document='')
Get id and rate of currency from code.
Class to manage order lines.
Class to manage products or services.
Class to manage projects.
Class to manage third parties objects (customers, suppliers, prospects...)
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.
setEntity($currentobject)
Set entity id to use when to create an object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that returns whether VAT must be recoverable collected VAT (e.g.: VAT NPR in France)
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that return vat rate of a product line (according to seller, buyer and product vat rate) VAT...
get_localtax($vatrate, $local, $thirdparty_buyer=null, $thirdparty_seller=null, $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.