36 include_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
37 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
39 require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
40 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
51 public $element =
'commande';
56 public $table_element =
'commande';
61 public $table_element_line =
'commandedet';
66 public $class_element_line =
'OrderLine';
71 public $fk_element =
'fk_commande';
76 public $picto =
'order';
82 public $ismultientitymanaged = 1;
88 public $restrictiononfksoc = 1;
129 public $cond_reglement_code;
134 public $deposit_percent;
144 public $mode_reglement;
149 public $mode_reglement_id;
154 public $mode_reglement_code;
160 public $availability_id;
166 public $availability_code;
172 public $availability;
177 public $demand_reason_id;
182 public $demand_reason_code;
194 public $date_commande;
201 public $date_livraison;
206 public $delivery_date;
211 public $fk_remise_except;
213 public $remise_percent;
214 public $remise_absolue;
217 public $special_code;
223 public $warehouse_id;
225 public $extraparams = array();
227 public $linked_objects = array();
232 public $user_author_id;
247 public $lines = array();
253 public $fk_multicurrency;
258 public $multicurrency_code;
259 public $multicurrency_tx;
260 public $multicurrency_total_ht;
261 public $multicurrency_total_tva;
262 public $multicurrency_total_ttc;
277 public $online_payment_url;
308 public $fields = array(
309 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
310 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>20,
'index'=>1),
311 'ref' =>array(
'type'=>
'varchar(30)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'showoncombobox'=>1,
'position'=>25),
312 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefExt',
'enabled'=>1,
'visible'=>0,
'position'=>26),
313 'ref_client' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefCustomer',
'enabled'=>1,
'visible'=>-1,
'position'=>28),
314 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'isModEnabled("societe")',
'visible'=>-1,
'notnull'=>1,
'position'=>20),
315 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label'=>
'Project',
'enabled'=>
"isModEnabled('project')",
'visible'=>-1,
'position'=>25),
316 'date_commande' =>array(
'type'=>
'date',
'label'=>
'Date',
'enabled'=>1,
'visible'=>1,
'position'=>60),
317 'date_valid' =>array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>62),
318 'date_cloture' =>array(
'type'=>
'datetime',
'label'=>
'DateClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>65),
319 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>85),
320 'fk_user_cloture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
321 'source' =>array(
'type'=>
'smallint(6)',
'label'=>
'Source',
'enabled'=>1,
'visible'=>-1,
'position'=>95),
323 'remise_percent' =>array(
'type'=>
'double',
'label'=>
'RelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>110),
324 'remise_absolue' =>array(
'type'=>
'double',
'label'=>
'CustomerRelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>115),
326 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'VAT',
'enabled'=>1,
'visible'=>-1,
'position'=>125,
'isameasure'=>1),
327 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax1',
'enabled'=>1,
'visible'=>-1,
'position'=>130,
'isameasure'=>1),
328 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax2',
'enabled'=>1,
'visible'=>-1,
'position'=>135,
'isameasure'=>1),
329 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalHT',
'enabled'=>1,
'visible'=>-1,
'position'=>140,
'isameasure'=>1),
330 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>145,
'isameasure'=>1),
331 'note_private' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>150),
332 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>155),
333 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'PDFTemplate',
'enabled'=>1,
'visible'=>0,
'position'=>160),
335 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'BankAccount',
'enabled'=>
'$conf->banque->enabled',
'visible'=>-1,
'position'=>170),
336 'fk_currency' =>array(
'type'=>
'varchar(3)',
'label'=>
'MulticurrencyID',
'enabled'=>1,
'visible'=>-1,
'position'=>175),
337 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
338 'deposit_percent' =>array(
'type'=>
'varchar(63)',
'label'=>
'DepositPercent',
'enabled'=>1,
'visible'=>-1,
'position'=>181),
339 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
340 'date_livraison' =>array(
'type'=>
'date',
'label'=>
'DateDeliveryPlanned',
'enabled'=>1,
'visible'=>-1,
'position'=>190),
341 'fk_shipping_method' =>array(
'type'=>
'integer',
'label'=>
'ShippingMethod',
'enabled'=>1,
'visible'=>-1,
'position'=>195),
342 'fk_warehouse' =>array(
'type'=>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label'=>
'Fk warehouse',
'enabled'=>
'$conf->stock->enabled',
'visible'=>-1,
'position'=>200),
343 'fk_availability' =>array(
'type'=>
'integer',
'label'=>
'Availability',
'enabled'=>1,
'visible'=>-1,
'position'=>205),
344 'fk_input_reason' =>array(
'type'=>
'integer',
'label'=>
'InputReason',
'enabled'=>1,
'visible'=>-1,
'position'=>210),
346 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>225),
347 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>230),
348 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLabel',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>235),
349 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>240),
350 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'MulticurrencyCurrency',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>245),
351 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyRate',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>250,
'isameasure'=>1),
352 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>255,
'isameasure'=>1),
353 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>260,
'isameasure'=>1),
354 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>265,
'isameasure'=>1),
355 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>-1,
'position'=>270),
356 'module_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSModule',
'enabled'=>1,
'visible'=>-1,
'position'=>275),
357 'pos_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSTerminal',
'enabled'=>1,
'visible'=>-1,
'position'=>280),
358 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-1,
'position'=>300),
359 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>302),
360 'date_creation' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>304),
361 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>306),
362 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>400),
363 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>-1,
'position'=>500),
388 const STATUS_ACCEPTED = 2;
415 global $langs, $conf;
416 $langs->load(
"order");
418 if (!empty($conf->global->COMMANDE_ADDON)) {
421 $file = $conf->global->COMMANDE_ADDON.
".php";
422 $classname = $conf->global->COMMANDE_ADDON;
425 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
426 foreach ($dirmodels as $reldir) {
430 $mybool |= @include_once $dir.$file;
433 if ($mybool ===
false) {
438 $obj =
new $classname();
439 $numref = $obj->getNextValue($soc, $this);
444 $this->error = $obj->error;
449 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_COMMANDE_ADDON_NotDefined");
463 public function valid($user, $idwarehouse = 0, $notrigger = 0)
465 global $conf, $langs;
467 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
472 if ($this->statut == self::STATUS_VALIDATED) {
473 dol_syslog(get_class($this).
"::valid action abandonned: already validated", LOG_WARNING);
477 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
478 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate)))) {
479 $this->error =
'NotEnoughPermissions';
480 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
490 $soc->fetch($this->socid);
493 $result = $soc->set_as_client();
496 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
504 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
505 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
506 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
507 $sql .=
" date_valid='".$this->db->idate($now).
"',";
508 $sql .=
" fk_user_valid = ".($user->id > 0 ? (int) $user->id :
"null").
",";
509 $sql .=
" fk_user_modif = ".((int) $user->id);
510 $sql .=
" WHERE rowid = ".((int) $this->
id);
512 dol_syslog(get_class($this).
"::valid", LOG_DEBUG);
513 $resql = $this->
db->query(
$sql);
516 $this->error = $this->
db->lasterror();
522 if ($result >= 0 &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
523 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
524 $langs->load(
"agenda");
527 $cpt = count($this->lines);
528 for ($i = 0; $i < $cpt; $i++) {
529 if ($this->lines[$i]->fk_product > 0) {
531 $mouvP->origin = &$this;
532 $mouvP->setOrigin($this->element, $this->
id);
534 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"OrderValidatedInDolibarr", $num));
537 $this->error = $mouvP->error;
547 if (!$error && !$notrigger) {
557 $this->oldref = $this->ref;
560 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
562 $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).
"'";
563 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'commande/".$this->
db->escape($this->
ref).
"' and entity = ".$conf->entity;
564 $resql = $this->
db->query(
$sql);
566 $error++; $this->error = $this->
db->lasterror();
572 $dirsource = $conf->commande->multidir_output[$this->entity].
'/'.$oldref;
573 $dirdest = $conf->commande->multidir_output[$this->entity].
'/'.$newref;
574 if (!$error && file_exists($dirsource)) {
575 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
577 if (@rename($dirsource, $dirdest)) {
580 $listoffiles =
dol_dir_list($conf->commande->multidir_output[$this->entity].
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
581 foreach ($listoffiles as $fileentry) {
582 $dirsource = $fileentry[
'name'];
583 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
584 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
585 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
586 @rename($dirsource, $dirdest);
597 $this->brouillon = 0;
604 $this->
db->rollback();
620 global $conf, $langs;
625 if ($this->statut <= self::STATUS_DRAFT) {
629 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
630 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate)))) {
631 $this->error =
'Permission denied';
639 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
640 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT.
",";
641 $sql .=
" fk_user_modif = ".((int) $user->id);
642 $sql .=
" WHERE rowid = ".((int) $this->
id);
644 if ($this->
db->query(
$sql)) {
646 $this->oldcopy = clone $this;
650 if (
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
653 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
654 $langs->load(
"agenda");
656 $num = count($this->lines);
657 for ($i = 0; $i < $num; $i++) {
658 if ($this->lines[$i]->fk_product > 0) {
660 $mouvP->origin = &$this;
661 $mouvP->setOrigin($this->element, $this->
id);
663 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderBackToDraftInDolibarr", $this->ref));
665 $error++; $this->error = $mouvP->error;
break;
673 $result = $this->
call_trigger(
'ORDER_UNVALIDATE', $user);
684 $this->
db->rollback();
688 $this->error = $this->
db->error();
689 $this->
db->rollback();
708 if ($this->statut != self::STATUS_CANCELED && $this->statut != self::STATUS_CLOSED) {
709 dol_syslog(get_class($this).
"::set_reopen order has not status closed", LOG_WARNING);
715 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
716 $sql .=
' SET fk_statut='.self::STATUS_VALIDATED.
', facture=0,';
717 $sql .=
" fk_user_modif = ".((int) $user->id);
718 $sql .=
" WHERE rowid = ".((int) $this->
id);
720 dol_syslog(get_class($this).
"::set_reopen", LOG_DEBUG);
721 $resql = $this->
db->query(
$sql);
731 $this->error = $this->
db->lasterror();
742 foreach ($this->errors as $errmsg) {
743 dol_syslog(get_class($this).
"::set_reopen ".$errmsg, LOG_ERR);
744 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
746 $this->
db->rollback();
758 public function cloture($user, $notrigger = 0)
764 $usercanclose = ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
765 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->close)));
768 if ($this->statut == self::STATUS_CLOSED) {
775 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
776 $sql .=
' SET fk_statut = '.self::STATUS_CLOSED.
',';
777 $sql .=
' fk_user_cloture = '.((int) $user->id).
',';
778 $sql .=
" date_cloture = '".$this->db->idate($now).
"',";
779 $sql .=
" fk_user_modif = ".((int) $user->id);
782 if ($this->
db->query(
$sql)) {
798 $this->
db->rollback();
802 $this->error = $this->
db->lasterror();
804 $this->
db->rollback();
818 public function cancel($idwarehouse = -1)
820 global $conf, $user, $langs;
826 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
827 $sql .=
" SET fk_statut = ".self::STATUS_CANCELED.
",";
828 $sql .=
" fk_user_modif = ".((int) $user->id);
829 $sql .=
" WHERE rowid = ".((int) $this->
id);
830 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
832 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
833 if ($this->
db->query(
$sql)) {
835 if (
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
836 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
837 $langs->load(
"agenda");
839 $num = count($this->lines);
840 for ($i = 0; $i < $num; $i++) {
841 if ($this->lines[$i]->fk_product > 0) {
843 $mouvP->setOrigin($this->element, $this->
id);
845 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderCanceledInDolibarr", $this->ref));
848 $this->error = $mouvP->error;
869 foreach ($this->errors as $errmsg) {
870 dol_syslog(get_class($this).
"::cancel ".$errmsg, LOG_ERR);
871 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
873 $this->
db->rollback();
877 $this->error = $this->
db->error();
878 $this->
db->rollback();
891 public function create($user, $notrigger = 0)
893 global $conf, $langs, $mysoc;
897 $this->brouillon = 1;
900 $date = ($this->date_commande ? $this->date_commande : $this->date);
901 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
904 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
909 if (empty($this->fk_multicurrency)) {
910 $this->multicurrency_code = $conf->currency;
911 $this->fk_multicurrency = 0;
912 $this->multicurrency_tx = 1;
915 dol_syslog(get_class($this).
"::create user=".$user->id);
918 if (!empty($this->
ref)) {
921 $this->error =
'ErrorRefAlreadyExists';
922 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
923 $this->
db->rollback();
929 $result = $soc->fetch($this->socid);
931 $this->error =
"Failed to fetch company";
932 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
935 if (!empty($conf->global->ORDER_REQUIRE_SOURCE) && $this->source < 0) {
936 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Source"));
937 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
945 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande (";
946 $sql .=
" ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client";
947 $sql .=
", model_pdf, fk_cond_reglement, deposit_percent, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address";
948 $sql .=
", fk_shipping_method";
949 $sql .=
", fk_warehouse";
950 $sql .=
", remise_absolue, remise_percent";
951 $sql .=
", fk_incoterms, location_incoterms";
952 $sql .=
", entity, module_source, pos_source";
953 $sql .=
", fk_multicurrency";
954 $sql .=
", multicurrency_code";
955 $sql .=
", multicurrency_tx";
957 $sql .=
" VALUES ('(PROV)', ".((int) $this->socid).
", '".$this->
db->idate($now).
"', ".((int) $user->id);
958 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
959 $sql .=
", '".$this->db->idate($date).
"'";
960 $sql .=
", ".($this->source >= 0 && $this->source !=
'' ? $this->
db->escape($this->source) :
'null');
961 $sql .=
", '".$this->db->escape($this->note_private).
"'";
962 $sql .=
", '".$this->db->escape($this->note_public).
"'";
963 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
964 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
965 $sql .=
", '".$this->db->escape($this->model_pdf).
"'";
966 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
967 $sql .=
", ".(!empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
"null");
968 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
969 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
970 $sql .=
", ".($this->availability_id > 0 ? ((int) $this->availability_id) :
"null");
971 $sql .=
", ".($this->demand_reason_id > 0 ? ((int) $this->demand_reason_id) :
"null");
972 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
973 $sql .=
", ".($this->fk_delivery_address > 0 ? ((int) $this->fk_delivery_address) :
'NULL');
974 $sql .=
", ".(!empty($this->shipping_method_id) && $this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) :
'NULL');
975 $sql .=
", ".(!empty($this->warehouse_id) && $this->warehouse_id > 0 ? ((int) $this->warehouse_id) :
'NULL');
976 $sql .=
", ".($this->remise_absolue > 0 ? $this->
db->escape($this->remise_absolue) :
'NULL');
977 $sql .=
", ".($this->remise_percent > 0 ? $this->
db->escape($this->remise_percent) : 0);
978 $sql .=
", ".(int) $this->fk_incoterms;
979 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
980 $sql .=
", ".setEntity($this);
981 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
982 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
983 $sql .=
", ".(int) $this->fk_multicurrency;
984 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
985 $sql .=
", ".(float) $this->multicurrency_tx;
988 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
989 $resql = $this->
db->query(
$sql);
991 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'commande');
995 $num = count($this->lines);
1000 for ($i = 0; $i < $num; $i++) {
1001 $line = $this->lines[$i];
1005 if (!is_object($line)) {
1006 $line = (object) $line;
1010 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1011 $fk_parent_line = 0;
1015 $vatrate = $line->tva_tx;
1016 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', $vatrate)) {
1017 $vatrate .=
' ('.$line->vat_src_code.
')';
1020 if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
1021 $originid = $line->origin_id;
1022 $origintype = $line->origin;
1024 $originid = $line->id;
1025 $origintype = $this->element;
1029 if (empty($line->ref_ext)) {
1030 $line->ref_ext =
'';
1038 $line->localtax1_tx,
1039 $line->localtax2_tx,
1041 $line->remise_percent,
1043 $line->fk_remise_except,
1048 $line->product_type,
1050 $line->special_code,
1052 $line->fk_fournprice,
1055 $line->array_options,
1064 if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) {
1065 $this->error = $this->
db->lasterror();
1066 $this->errors[] = $this->error;
1069 $this->
db->rollback();
1073 if ($result > 0 && $line->product_type == 9) {
1074 $fk_parent_line = $result;
1081 $initialref =
'(PROV'.$this->id.
')';
1082 if (!empty($this->
ref)) {
1083 $initialref = $this->ref;
1086 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"commande SET ref='".$this->
db->escape($initialref).
"' WHERE rowid=".((int) $this->
id);
1087 if ($this->
db->query(
$sql)) {
1088 $this->
ref = $initialref;
1090 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1091 $this->linked_objects = $this->linkedObjectsIds;
1095 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1096 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1097 if (is_array($tmp_origin_id)) {
1098 foreach ($tmp_origin_id as $origin_id) {
1101 $this->error = $this->
db->lasterror();
1107 $origin_id = $tmp_origin_id;
1110 $this->error = $this->
db->lasterror();
1117 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) {
1118 $originforcontact = $this->origin;
1119 $originidforcontact = $this->origin_id;
1120 if ($originforcontact ==
'shipping') {
1121 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1123 $exp->fetch($this->origin_id);
1124 $exp->fetchObjectLinked();
1125 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1126 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1127 $originforcontact =
'commande';
1128 if (is_object($value)) {
1129 $originidforcontact = $value->id;
1131 $originidforcontact = $value;
1138 $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";
1139 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->
db->escape($originforcontact).
"'";
1141 $resqlcontact = $this->
db->query($sqlcontact);
1142 if ($resqlcontact) {
1143 while ($objcontact = $this->
db->fetch_object($resqlcontact)) {
1145 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1159 if (!$error && !$notrigger) {
1169 $this->
db->commit();
1172 $this->
db->rollback();
1176 $this->error = $this->
db->lasterror();
1177 $this->
db->rollback();
1185 $this->
db->rollback();
1200 global $conf, $user, $hookmanager;
1207 foreach ($this->lines as $line) {
1208 $line->fetch_optionals();
1212 $objFrom = clone $this;
1215 if (!empty($socid) && $socid != $this->socid) {
1218 if ($objsoc->fetch($socid) > 0) {
1219 $this->socid = $objsoc->id;
1220 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1221 $this->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent :
null);
1222 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1223 $this->fk_project = 0;
1224 $this->fk_delivery_address = 0;
1235 $this->user_author_id = $user->id;
1236 $this->user_valid = 0;
1237 $this->user_validation_id = 0;
1239 $this->date_commande =
dol_now();
1240 $this->date_creation =
'';
1241 $this->date_validation =
'';
1242 if (empty($conf->global->MAIN_KEEP_REF_CUSTOMER_ON_CLONING)) {
1243 $this->ref_client =
'';
1247 $num = count($this->lines);
1248 for ($i = 0; $i < $num; $i++) {
1249 $this->lines[$i]->ref_ext =
'';
1253 $this->context[
'createfromclone'] =
'createfromclone';
1254 $result = $this->
create($user);
1268 if ($this->socid == $objFrom->socid) {
1277 if (is_object($hookmanager)) {
1278 $parameters = array(
'objFrom'=>$objFrom);
1280 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1288 unset($this->context[
'createfromclone']);
1292 $this->
db->commit();
1295 $this->
db->rollback();
1310 global $conf, $hookmanager;
1318 $this->date_commande =
dol_now();
1321 $num = count($object->lines);
1322 for ($i = 0; $i < $num; $i++) {
1325 $line->libelle = $object->lines[$i]->libelle;
1326 $line->label = $object->lines[$i]->label;
1327 $line->desc = $object->lines[$i]->desc;
1328 $line->price = $object->lines[$i]->price;
1329 $line->subprice = $object->lines[$i]->subprice;
1330 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1331 $line->tva_tx = $object->lines[$i]->tva_tx;
1332 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1333 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1334 $line->qty = $object->lines[$i]->qty;
1335 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1336 $line->remise_percent = $object->lines[$i]->remise_percent;
1337 $line->fk_product = $object->lines[$i]->fk_product;
1338 $line->info_bits = $object->lines[$i]->info_bits;
1339 $line->product_type = $object->lines[$i]->product_type;
1340 $line->rang = $object->lines[$i]->rang;
1341 $line->special_code = $object->lines[$i]->special_code;
1342 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1343 $line->fk_unit = $object->lines[$i]->fk_unit;
1345 $line->date_start = $object->lines[$i]->date_start;
1346 $line->date_end = $object->lines[$i]->date_end;
1348 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1349 $marginInfos =
getMarginInfos($object->lines[$i]->subprice, $object->lines[$i]->remise_percent, $object->lines[$i]->tva_tx, $object->lines[$i]->localtax1_tx, $object->lines[$i]->localtax2_tx, $object->lines[$i]->fk_fournprice, $object->lines[$i]->pa_ht);
1350 $line->pa_ht = $marginInfos[0];
1351 $line->marge_tx = $marginInfos[1];
1352 $line->marque_tx = $marginInfos[2];
1355 $object->lines[$i]->fetch_optionals();
1356 foreach ($object->lines[$i]->array_options as $options_key => $value) {
1357 $line->array_options[$options_key] = $value;
1360 $this->lines[$i] = $line;
1363 $this->entity = $object->entity;
1364 $this->socid = $object->socid;
1365 $this->fk_project = $object->fk_project;
1366 $this->cond_reglement_id = $object->cond_reglement_id;
1367 $this->deposit_percent = $object->deposit_percent;
1368 $this->mode_reglement_id = $object->mode_reglement_id;
1369 $this->fk_account = $object->fk_account;
1370 $this->availability_id = $object->availability_id;
1371 $this->demand_reason_id = $object->demand_reason_id;
1372 $this->date_livraison = $object->date_livraison;
1373 $this->delivery_date = $object->date_livraison;
1374 $this->shipping_method_id = $object->shipping_method_id;
1375 $this->warehouse_id = $object->warehouse_id;
1376 $this->fk_delivery_address = $object->fk_delivery_address;
1377 $this->contact_id = $object->contact_id;
1378 $this->ref_client = $object->ref_client;
1380 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1381 $this->note_private = $object->note_private;
1382 $this->note_public = $object->note_public;
1385 $this->origin = $object->element;
1386 $this->origin_id = $object->id;
1389 if (!empty($conf->multicurrency->enabled)) {
1390 if (!empty($object->multicurrency_code)) {
1391 $this->multicurrency_code = $object->multicurrency_code;
1393 if (!empty($conf->global->MULTICURRENCY_USE_ORIGIN_TX) && !empty($object->multicurrency_tx)) {
1394 $this->multicurrency_tx = $object->multicurrency_tx;
1397 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1399 $this->fk_multicurrency = $tmparray[0];
1400 $this->multicurrency_tx = $tmparray[1];
1404 if (empty($this->fk_multicurrency)) {
1405 $this->multicurrency_code = $conf->currency;
1406 $this->fk_multicurrency = 0;
1407 $this->multicurrency_tx = 1;
1412 $object->fetch_optionals();
1415 $element_extrafields = $e->fetch_name_optionals_label($this->table_element);
1417 foreach ($object->array_options as $options_key => $value) {
1418 if (array_key_exists(str_replace(
'options_',
'', $options_key), $element_extrafields)) {
1419 $this->array_options[$options_key] = $value;
1423 $this->linked_objects[$this->origin] = $this->origin_id;
1424 if (isset($object->other_linked_objects) && is_array($object->other_linked_objects) && !empty($object->other_linked_objects)) {
1425 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1428 $ret = $this->
create($user);
1432 $hookmanager->initHooks(array(
'orderdao'));
1434 $parameters = array(
'objFrom'=>$object);
1436 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1444 if (!empty($conf->global->ORDER_VALID_AFTER_CLOSE_PROPAL)) {
1446 $this->
valid($user);
1498 public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0, $remise_percent = 0, $info_bits = 0, $fk_remise_except = 0, $price_base_type =
'HT', $pu_ttc = 0, $date_start =
'', $date_end =
'', $type = 0, $rang = -1, $special_code = 0, $fk_parent_line = 0, $fk_fournprice =
null, $pa_ht = 0, $label =
'', $array_options = 0, $fk_unit =
null, $origin =
'', $origin_id = 0, $pu_ht_devise = 0, $ref_ext =
'', $noupdateafterinsertline = 0)
1500 global $mysoc, $conf, $langs, $user;
1502 $logtext =
"::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent";
1503 $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";
1504 $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";
1505 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
1507 if ($this->statut == self::STATUS_DRAFT) {
1508 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1512 if (empty($remise_percent)) {
1513 $remise_percent = 0;
1518 if (empty($info_bits)) {
1524 if (empty($txtva)) {
1527 if (empty($txlocaltax1)) {
1530 if (empty($txlocaltax2)) {
1533 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1534 $fk_parent_line = 0;
1536 if (empty($this->fk_multicurrency)) {
1537 $this->fk_multicurrency = 0;
1539 if (empty($ref_ext)) {
1543 $remise_percent =
price2num($remise_percent);
1546 $pu_ht_devise =
price2num($pu_ht_devise);
1549 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1554 if ($price_base_type ==
'HT') {
1559 $label = trim($label);
1560 $desc = trim($desc);
1567 if ($date_start && $date_end && $date_start > $date_end) {
1568 $langs->load(
"errors");
1569 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1575 $product_type = $type;
1576 if (!empty($fk_product) && $fk_product > 0) {
1578 $result = $product->fetch($fk_product);
1579 $product_type = $product->type;
1581 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) {
1582 $langs->load(
"errors");
1583 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
1584 $this->errors[] = $this->error;
1585 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
1586 $this->
db->rollback();
1600 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1601 $vat_src_code = $reg[1];
1602 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1605 $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);
1615 $total_ht = $tabprice[0];
1616 $total_tva = $tabprice[1];
1617 $total_ttc = $tabprice[2];
1618 $total_localtax1 = $tabprice[9];
1619 $total_localtax2 = $tabprice[10];
1620 $pu_ht = $tabprice[3];
1623 $multicurrency_total_ht = $tabprice[16];
1624 $multicurrency_total_tva = $tabprice[17];
1625 $multicurrency_total_ttc = $tabprice[18];
1626 $pu_ht_devise = $tabprice[19];
1630 if ($ranktouse == -1) {
1631 $rangmax = $this->
line_max($fk_parent_line);
1632 $ranktouse = $rangmax + 1;
1639 if ($remise_percent > 0) {
1640 $remise = round(($pu * $remise_percent / 100), 2);
1641 $price = $pu - $remise;
1647 $this->line->context = $this->context;
1649 $this->line->fk_commande = $this->id;
1650 $this->line->label = $label;
1651 $this->line->desc = $desc;
1652 $this->line->qty = $qty;
1653 $this->line->ref_ext = $ref_ext;
1655 $this->line->vat_src_code = $vat_src_code;
1656 $this->line->tva_tx = $txtva;
1657 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1658 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1659 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1660 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1661 $this->line->fk_product = $fk_product;
1662 $this->line->product_type = $product_type;
1663 $this->line->fk_remise_except = $fk_remise_except;
1664 $this->line->remise_percent = $remise_percent;
1665 $this->line->subprice = $pu_ht;
1666 $this->line->rang = $ranktouse;
1667 $this->line->info_bits = $info_bits;
1668 $this->line->total_ht = $total_ht;
1669 $this->line->total_tva = $total_tva;
1670 $this->line->total_localtax1 = $total_localtax1;
1671 $this->line->total_localtax2 = $total_localtax2;
1672 $this->line->total_ttc = $total_ttc;
1673 $this->line->special_code = $special_code;
1674 $this->line->origin = $origin;
1675 $this->line->origin_id = $origin_id;
1676 $this->line->fk_parent_line = $fk_parent_line;
1677 $this->line->fk_unit = $fk_unit;
1679 $this->line->date_start = $date_start;
1680 $this->line->date_end = $date_end;
1682 $this->line->fk_fournprice = $fk_fournprice;
1683 $this->line->pa_ht = $pa_ht;
1686 $this->line->fk_multicurrency = $this->fk_multicurrency;
1687 $this->line->multicurrency_code = $this->multicurrency_code;
1688 $this->line->multicurrency_subprice = $pu_ht_devise;
1689 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1690 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1691 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1694 $this->line->price = $price;
1696 if (is_array($array_options) && count($array_options) > 0) {
1697 $this->line->array_options = $array_options;
1700 $result = $this->line->insert($user);
1703 if (!empty($fk_parent_line)) {
1705 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
1706 $linecount = count($this->lines);
1707 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
1713 if (empty($noupdateafterinsertline)) {
1718 $this->
db->commit();
1719 return $this->line->id;
1721 $this->
db->rollback();
1725 $this->error = $this->line->error;
1726 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1727 $this->
db->rollback();
1731 dol_syslog(get_class($this).
"::addline status of order must be Draft to allow use of ->addline()", LOG_ERR);
1751 public function add_product($idproduct, $qty, $remise_percent = 0.0, $date_start =
'', $date_end =
'')
1754 global $conf, $mysoc;
1760 if ($idproduct > 0) {
1762 $prod->fetch($idproduct);
1766 if (empty($tva_tx)) {
1771 $localtax1_tx =
get_localtax($tva_tx, 1, $this->thirdparty, $mysoc, $tva_npr);
1772 $localtax2_tx =
get_localtax($tva_tx, 2, $this->thirdparty, $mysoc, $tva_npr);
1775 if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
1776 $price = $prod->multiprices[$this->thirdparty->price_level];
1778 $price = $prod->price;
1783 $line->context = $this->context;
1785 $line->fk_product = $idproduct;
1786 $line->desc = $prod->description;
1788 $line->subprice = $price;
1789 $line->remise_percent = $remise_percent;
1790 $line->vat_src_code = $vat_src_code;
1791 $line->tva_tx = $tva_tx;
1792 $line->localtax1_tx = $localtax1_tx;
1793 $line->localtax2_tx = $localtax2_tx;
1794 $line->ref = $prod->ref;
1795 $line->libelle = $prod->label;
1796 $line->product_desc = $prod->description;
1797 $line->fk_unit = $prod->fk_unit;
1801 $line->date_start = $date_start;
1804 $line->date_end = $date_end;
1807 $this->lines[] = $line;
1840 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
1843 if (empty($id) && empty($ref) && empty($ref_ext)) {
1847 $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';
1848 $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';
1849 $sql .=
', c.fk_account';
1850 $sql .=
', c.date_commande, c.date_valid, c.tms';
1851 $sql .=
', c.date_livraison as delivery_date';
1852 $sql .=
', c.fk_shipping_method';
1853 $sql .=
', c.fk_warehouse';
1854 $sql .=
', c.fk_projet as fk_project, c.remise_percent, c.remise, c.remise_absolue, c.source, c.facture as billed';
1855 $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';
1856 $sql .=
', c.fk_incoterms, c.location_incoterms';
1857 $sql .=
", c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc";
1858 $sql .=
", c.module_source, c.pos_source";
1859 $sql .=
", i.libelle as label_incoterms";
1860 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
1861 $sql .=
', cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle, cr.libelle_facture as cond_reglement_libelle_doc';
1862 $sql .=
', ca.code as availability_code, ca.label as availability_label';
1863 $sql .=
', dr.code as demand_reason_code';
1864 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
1865 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON c.fk_cond_reglement = cr.rowid';
1866 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON c.fk_mode_reglement = p.id';
1867 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON c.fk_availability = ca.rowid';
1868 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON c.fk_input_reason = dr.rowid';
1869 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
1872 $sql .=
" WHERE c.rowid=".((int) $id);
1874 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
1878 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
1881 $sql .=
" AND c.ref_ext='".$this->db->escape($ref_ext).
"'";
1884 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1885 $result = $this->
db->query(
$sql);
1887 $obj = $this->
db->fetch_object($result);
1889 $this->
id = $obj->rowid;
1890 $this->entity = $obj->entity;
1892 $this->
ref = $obj->ref;
1893 $this->ref_client = $obj->ref_client;
1894 $this->ref_customer = $obj->ref_client;
1895 $this->ref_ext = $obj->ref_ext;
1897 $this->socid = $obj->fk_soc;
1898 $this->thirdparty =
null;
1900 $this->fk_project = $obj->fk_project;
1901 $this->project =
null;
1903 $this->statut = $obj->fk_statut;
1904 $this->status = $obj->fk_statut;
1906 $this->user_author_id = $obj->fk_user_author;
1907 $this->user_creation_id = $obj->fk_user_author;
1908 $this->user_validation_id = $obj->fk_user_valid;
1909 $this->user_valid = $obj->fk_user_valid;
1910 $this->user_modification_id = $obj->fk_user_modif;
1911 $this->user_modification = $obj->fk_user_modif;
1912 $this->total_ht = $obj->total_ht;
1913 $this->total_tva = $obj->total_tva;
1914 $this->total_localtax1 = $obj->total_localtax1;
1915 $this->total_localtax2 = $obj->total_localtax2;
1916 $this->total_ttc = $obj->total_ttc;
1917 $this->date = $this->
db->jdate($obj->date_commande);
1918 $this->date_commande = $this->
db->jdate($obj->date_commande);
1919 $this->date_creation = $this->
db->jdate($obj->date_creation);
1920 $this->date_validation = $this->
db->jdate($obj->date_valid);
1921 $this->date_modification = $this->
db->jdate($obj->tms);
1922 $this->remise = $obj->remise;
1923 $this->remise_percent = $obj->remise_percent;
1924 $this->remise_absolue = $obj->remise_absolue;
1925 $this->source = $obj->source;
1926 $this->billed = $obj->billed;
1927 $this->note = $obj->note_private;
1928 $this->note_private = $obj->note_private;
1929 $this->note_public = $obj->note_public;
1930 $this->model_pdf = $obj->model_pdf;
1931 $this->modelpdf = $obj->model_pdf;
1932 $this->last_main_doc = $obj->last_main_doc;
1933 $this->mode_reglement_id = $obj->fk_mode_reglement;
1934 $this->mode_reglement_code = $obj->mode_reglement_code;
1935 $this->mode_reglement = $obj->mode_reglement_libelle;
1936 $this->cond_reglement_id = $obj->fk_cond_reglement;
1937 $this->cond_reglement_code = $obj->cond_reglement_code;
1938 $this->cond_reglement = $obj->cond_reglement_libelle;
1939 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1940 $this->deposit_percent = $obj->deposit_percent;
1941 $this->fk_account = $obj->fk_account;
1942 $this->availability_id = $obj->fk_availability;
1943 $this->availability_code = $obj->availability_code;
1945 $this->demand_reason_id = $obj->fk_input_reason;
1946 $this->demand_reason_code = $obj->demand_reason_code;
1947 $this->date_livraison = $this->
db->jdate($obj->delivery_date);
1948 $this->delivery_date = $this->
db->jdate($obj->delivery_date);
1949 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method :
null;
1950 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse :
null;
1951 $this->fk_delivery_address = $obj->fk_delivery_address;
1952 $this->module_source = $obj->module_source;
1953 $this->pos_source = $obj->pos_source;
1956 $this->fk_incoterms = $obj->fk_incoterms;
1957 $this->location_incoterms = $obj->location_incoterms;
1958 $this->label_incoterms = $obj->label_incoterms;
1961 $this->fk_multicurrency = $obj->fk_multicurrency;
1962 $this->multicurrency_code = $obj->multicurrency_code;
1963 $this->multicurrency_tx = $obj->multicurrency_tx;
1964 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1965 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1966 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1968 $this->extraparams = (array) json_decode($obj->extraparams,
true);
1970 $this->lines = array();
1972 if ($this->statut == self::STATUS_DRAFT) {
1973 $this->brouillon = 1;
1980 $this->
db->free($result);
1989 $this->error =
'Order with id '.$id.
' not found sql='.
$sql;
1993 $this->error = $this->
db->error();
2011 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2012 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
2017 $result = $remise->fetch($idremise);
2020 if ($remise->fk_facture) {
2021 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2022 $this->
db->rollback();
2028 $line->fk_commande = $this->id;
2029 $line->fk_remise_except = $remise->id;
2030 $line->desc = $remise->description;
2031 $line->vat_src_code = $remise->vat_src_code;
2032 $line->tva_tx = $remise->tva_tx;
2033 $line->subprice = -$remise->amount_ht;
2034 $line->price = -$remise->amount_ht;
2035 $line->fk_product = 0;
2037 $line->remise_percent = 0;
2039 $line->info_bits = 2;
2041 $line->total_ht = -$remise->amount_ht;
2042 $line->total_tva = -$remise->amount_tva;
2043 $line->total_ttc = -$remise->amount_ttc;
2045 $result = $line->insert();
2049 $this->
db->commit();
2052 $this->
db->rollback();
2056 $this->error = $line->error;
2057 $this->errors = $line->errors;
2058 $this->
db->rollback();
2062 $this->
db->rollback();
2076 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2079 global $langs, $conf;
2081 $this->lines = array();
2083 $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,';
2084 $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,';
2085 $sql .=
' l.total_ht, l.total_ttc, l.total_tva, l.total_localtax1, l.total_localtax2, l.date_start, l.date_end,';
2086 $sql .=
' l.fk_unit,';
2087 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2088 $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,';
2089 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units';
2090 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as l';
2091 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (p.rowid = l.fk_product)';
2092 $sql .=
' WHERE l.fk_commande = '.((int) $this->
id);
2093 if ($only_product) {
2094 $sql .=
' AND p.fk_product_type = 0';
2096 $sql .=
' ORDER BY l.rang, l.rowid';
2098 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
2099 $result = $this->
db->query(
$sql);
2101 $num = $this->
db->num_rows($result);
2105 $objp = $this->
db->fetch_object($result);
2109 $line->rowid = $objp->rowid;
2110 $line->id = $objp->rowid;
2111 $line->fk_commande = $objp->fk_commande;
2112 $line->commande_id = $objp->fk_commande;
2113 $line->label = $objp->custom_label;
2114 $line->desc = $objp->description;
2115 $line->description = $objp->description;
2116 $line->product_type = $objp->product_type;
2117 $line->qty = $objp->qty;
2118 $line->ref_ext = $objp->ref_ext;
2120 $line->vat_src_code = $objp->vat_src_code;
2121 $line->tva_tx = $objp->tva_tx;
2122 $line->localtax1_tx = $objp->localtax1_tx;
2123 $line->localtax2_tx = $objp->localtax2_tx;
2124 $line->localtax1_type = $objp->localtax1_type;
2125 $line->localtax2_type = $objp->localtax2_type;
2126 $line->total_ht = $objp->total_ht;
2127 $line->total_ttc = $objp->total_ttc;
2128 $line->total_tva = $objp->total_tva;
2129 $line->total_localtax1 = $objp->total_localtax1;
2130 $line->total_localtax2 = $objp->total_localtax2;
2131 $line->subprice = $objp->subprice;
2132 $line->fk_remise_except = $objp->fk_remise_except;
2133 $line->remise_percent = $objp->remise_percent;
2134 $line->price = $objp->price;
2135 $line->fk_product = $objp->fk_product;
2136 $line->fk_fournprice = $objp->fk_fournprice;
2137 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2138 $line->pa_ht = $marginInfos[0];
2139 $line->marge_tx = $marginInfos[1];
2140 $line->marque_tx = $marginInfos[2];
2141 $line->rang = $objp->rang;
2142 $line->info_bits = $objp->info_bits;
2143 $line->special_code = $objp->special_code;
2144 $line->fk_parent_line = $objp->fk_parent_line;
2146 $line->ref = $objp->product_ref;
2147 $line->libelle = $objp->product_label;
2149 $line->product_ref = $objp->product_ref;
2150 $line->product_label = $objp->product_label;
2151 $line->product_tosell = $objp->product_tosell;
2152 $line->product_tobuy = $objp->product_tobuy;
2153 $line->product_desc = $objp->product_desc;
2154 $line->product_tobatch = $objp->product_tobatch;
2155 $line->product_barcode = $objp->product_barcode;
2157 $line->fk_product_type = $objp->fk_product_type;
2158 $line->fk_unit = $objp->fk_unit;
2160 $line->weight = $objp->weight;
2161 $line->weight_units = $objp->weight_units;
2162 $line->volume = $objp->volume;
2163 $line->volume_units = $objp->volume_units;
2165 $line->date_start = $this->
db->jdate($objp->date_start);
2166 $line->date_end = $this->
db->jdate($objp->date_end);
2169 $line->fk_multicurrency = $objp->fk_multicurrency;
2170 $line->multicurrency_code = $objp->multicurrency_code;
2171 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2172 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2173 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2174 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2176 $line->fetch_optionals();
2179 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2181 $tmpproduct->fetch($objp->fk_product);
2182 $tmpproduct->getMultiLangs();
2184 $line->multilangs = $tmpproduct->multilangs;
2187 $this->lines[$i] = $line;
2192 $this->
db->free($result);
2196 $this->error = $this->
db->error();
2210 foreach ($this->lines as $line) {
2211 if ($line->product_type == 0) {
2226 foreach ($this->lines as $line) {
2227 if ($line->product_type == 1) {
2243 $sql =
'SELECT COUNT(DISTINCT ed.fk_expedition) as nb';
2244 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2245 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2247 $sql .=
' ed.fk_origin_line = cd.rowid';
2248 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2251 dol_syslog(get_class($this).
"::getNbOfShipments", LOG_DEBUG);
2252 $resql = $this->
db->query(
$sql);
2254 $obj = $this->
db->fetch_object($resql);
2259 $this->
db->free($resql);
2262 $this->error = $this->
db->lasterror();
2277 $this->expeditions = array();
2279 $sql =
'SELECT cd.rowid, cd.fk_product,';
2280 $sql .=
' sum(ed.qty) as qty';
2281 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2282 if ($filtre_statut >= 0) {
2283 $sql .=
' '.MAIN_DB_PREFIX.
'expedition as e,';
2285 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2287 if ($filtre_statut >= 0) {
2288 $sql .=
' ed.fk_expedition = e.rowid AND';
2290 $sql .=
' ed.fk_origin_line = cd.rowid';
2291 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2292 if ($fk_product > 0) {
2293 $sql .=
' AND cd.fk_product = '.((int) $fk_product);
2295 if ($filtre_statut >= 0) {
2296 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
2298 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
2301 dol_syslog(get_class($this).
"::loadExpeditions", LOG_DEBUG);
2302 $resql = $this->
db->query(
$sql);
2304 $num = $this->
db->num_rows($resql);
2307 $obj = $this->
db->fetch_object($resql);
2308 $this->expeditions[$obj->rowid] = $obj->qty;
2311 $this->
db->free($resql);
2314 $this->error = $this->
db->lasterror();
2326 $sql =
'SELECT count(*)';
2327 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expedition as e';
2328 $sql .=
', '.MAIN_DB_PREFIX.
'element_element as el';
2329 $sql .=
' WHERE el.fk_source = '.((int) $this->
id);
2330 $sql .=
" AND el.sourcetype = 'commande'";
2331 $sql .=
" AND el.fk_target = e.rowid";
2332 $sql .=
" AND el.targettype = 'shipping'";
2334 $resql = $this->
db->query(
$sql);
2336 $row = $this->
db->fetch_row($resql);
2357 $this->stocks = array();
2360 $array_of_product = array();
2364 if (count($array_of_product)) {
2365 $sql =
"SELECT fk_product, sum(ps.reel) as total";
2366 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_stock as ps";
2367 $sql .=
" WHERE ps.fk_product IN (".$this->db->sanitize(join(
',', $array_of_product)).
")";
2368 $sql .=
' GROUP BY fk_product';
2369 $resql = $this->
db->query(
$sql);
2371 $num = $this->
db->num_rows($resql);
2374 $obj = $this->
db->fetch_object($resql);
2375 $this->stocks[$obj->fk_product] = $obj->total;
2378 $this->
db->free($resql);
2394 if ($this->statut == self::STATUS_DRAFT) {
2400 $line->context = $this->context;
2403 $line->fetch($lineid);
2405 if ($id > 0 && $line->fk_commande != $id) {
2406 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2411 $staticline = clone $line;
2412 $line->oldline = $staticline;
2414 if ($line->delete($user) > 0) {
2418 $this->
db->commit();
2421 $this->
db->rollback();
2422 $this->error = $this->
db->lasterror();
2426 $this->
db->rollback();
2427 $this->error = $line->error;
2431 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
2450 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
2451 return $this->
setDiscount($user, $remise, $notrigger);
2464 $remise = trim($remise) ?trim($remise) : 0;
2466 if ($user->rights->commande->creer) {
2473 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2474 $sql .=
' SET remise_percent = '.((float) $remise);
2475 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.((int) self::STATUS_DRAFT);
2478 $resql = $this->
db->query(
$sql);
2480 $this->errors[] = $this->
db->error();
2485 $this->oldcopy = clone $this;
2486 $this->remise_percent = $remise;
2490 if (!$notrigger && empty($error)) {
2500 $this->
db->commit();
2503 foreach ($this->errors as $errmsg) {
2504 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2505 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2507 $this->
db->rollback();
2528 if (empty($remise)) {
2534 if ($user->rights->commande->creer) {
2539 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2540 $sql .=
' SET remise_absolue = '.((float) $remise);
2544 $resql = $this->
db->query(
$sql);
2546 $this->errors[] = $this->
db->error();
2551 $this->oldcopy = clone $this;
2552 $this->remise_absolue = $remise;
2556 if (!$notrigger && empty($error)) {
2566 $this->
db->commit();
2569 foreach ($this->errors as $errmsg) {
2570 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2571 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2573 $this->
db->rollback();
2594 if ($user->rights->commande->creer) {
2599 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2600 $sql .=
" SET date_commande = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
2601 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut = ".((int) self::STATUS_DRAFT);
2604 $resql = $this->
db->query(
$sql);
2606 $this->errors[] = $this->
db->error();
2611 $this->oldcopy = clone $this;
2612 $this->date = $date;
2615 if (!$notrigger && empty($error)) {
2625 $this->
db->commit();
2628 foreach ($this->errors as $errmsg) {
2629 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2630 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2632 $this->
db->rollback();
2666 if ($user->rights->commande->creer) {
2671 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2672 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2673 $sql .=
" WHERE rowid = ".((int) $this->
id);
2676 $resql = $this->
db->query(
$sql);
2678 $this->errors[] = $this->
db->error();
2683 $this->oldcopy = clone $this;
2684 $this->date_livraison = $delivery_date;
2685 $this->delivery_date = $delivery_date;
2688 if (!$notrigger && empty($error)) {
2698 $this->
db->commit();
2701 foreach ($this->errors as $errmsg) {
2702 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2703 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2705 $this->
db->rollback();
2727 public function liste_array($shortlist = 0, $draft = 0, $excluser =
'', $socid = 0, $limit = 0, $offset = 0, $sortfield =
'c.date_commande', $sortorder =
'DESC')
2734 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2735 $sql .=
" c.rowid as cid, c.ref";
2736 if (empty($user->rights->societe->client->voir) && !$socid) {
2737 $sql .=
", sc.fk_soc, sc.fk_user";
2739 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"commande as c";
2740 if (empty($user->rights->societe->client->voir) && !$socid) {
2741 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2743 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2744 $sql .=
" AND c.fk_soc = s.rowid";
2745 if (empty($user->rights->societe->client->voir) && !$socid) {
2746 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2749 $sql .=
" AND s.rowid = ".((int) $socid);
2752 $sql .=
" AND c.fk_statut = ".self::STATUS_DRAFT;
2754 if (is_object($excluser)) {
2755 $sql .=
" AND c.fk_user_author <> ".((int) $excluser->id);
2757 $sql .= $this->
db->order($sortfield, $sortorder);
2758 $sql .= $this->
db->plimit($limit, $offset);
2760 $result = $this->
db->query(
$sql);
2762 $numc = $this->
db->num_rows($result);
2765 while ($i < $numc) {
2766 $obj = $this->
db->fetch_object($result);
2768 if ($shortlist == 1) {
2769 $ga[$obj->cid] = $obj->ref;
2770 } elseif ($shortlist == 2) {
2771 $ga[$obj->cid] = $obj->ref.
' ('.$obj->name.
')';
2773 $ga[$i][
'id'] = $obj->cid;
2774 $ga[$i][
'ref'] = $obj->ref;
2775 $ga[$i][
'name'] = $obj->name;
2798 dol_syslog(
'Commande::availability('.$availability_id.
')');
2799 if ($this->statut >= self::STATUS_DRAFT) {
2804 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2805 $sql .=
' SET fk_availability = '.((int) $availability_id);
2806 $sql .=
' WHERE rowid='.((int) $this->
id);
2809 $resql = $this->
db->query(
$sql);
2811 $this->errors[] = $this->
db->error();
2816 $this->oldcopy = clone $this;
2817 $this->availability_id = $availability_id;
2820 if (!$notrigger && empty($error)) {
2830 $this->
db->commit();
2833 foreach ($this->errors as $errmsg) {
2834 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2835 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2837 $this->
db->rollback();
2841 $error_str =
'Command status do not meet requirement '.$this->statut;
2843 $this->error = $error_str;
2844 $this->errors[] = $this->error;
2862 dol_syslog(
'Commande::demand_reason('.$demand_reason_id.
')');
2863 if ($this->statut >= self::STATUS_DRAFT) {
2868 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2869 $sql .=
' SET fk_input_reason = '.((int) $demand_reason_id);
2870 $sql .=
' WHERE rowid='.((int) $this->
id);
2873 $resql = $this->
db->query(
$sql);
2875 $this->errors[] = $this->
db->error();
2880 $this->oldcopy = clone $this;
2881 $this->demand_reason_id = $demand_reason_id;
2884 if (!$notrigger && empty($error)) {
2894 $this->
db->commit();
2897 foreach ($this->errors as $errmsg) {
2898 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2899 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2901 $this->
db->rollback();
2905 $error_str =
'order status do not meet requirement '.$this->statut;
2907 $this->error = $error_str;
2908 $this->errors[] = $this->error;
2925 if ($user->rights->commande->creer) {
2930 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET';
2931 $sql .=
' ref_client = '.(empty($ref_client) ?
'NULL' :
"'".$this->db->escape($ref_client).
"'");
2932 $sql .=
' WHERE rowid = '.((int) $this->
id);
2934 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2935 $resql = $this->
db->query(
$sql);
2937 $this->errors[] = $this->
db->error();
2942 $this->oldcopy = clone $this;
2943 $this->ref_client = $ref_client;
2946 if (!$notrigger && empty($error)) {
2955 $this->
db->commit();
2958 foreach ($this->errors as $errmsg) {
2959 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2960 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2962 $this->
db->rollback();
2981 if ($this->billed) {
2987 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 1';
2990 dol_syslog(get_class($this).
"::classifyBilled", LOG_DEBUG);
2991 if ($this->
db->query(
$sql)) {
2993 $this->oldcopy = clone $this;
2997 if (!$notrigger && empty($error)) {
2999 $result = $this->
call_trigger(
'ORDER_CLASSIFY_BILLED', $user);
3007 $this->
db->commit();
3010 foreach ($this->errors as $errmsg) {
3011 dol_syslog(get_class($this).
"::classifyBilled ".$errmsg, LOG_ERR);
3012 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3014 $this->
db->rollback();
3018 $this->error = $this->
db->error();
3019 $this->
db->rollback();
3037 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 0';
3040 dol_syslog(get_class($this).
"::classifyUnBilled", LOG_DEBUG);
3041 if ($this->
db->query(
$sql)) {
3043 $this->oldcopy = clone $this;
3047 if (!$notrigger && empty($error)) {
3049 $result = $this->
call_trigger(
'ORDER_CLASSIFY_UNBILLED', $user);
3059 $this->
db->commit();
3062 foreach ($this->errors as $errmsg) {
3063 dol_syslog(get_class($this).
"::classifyUnBilled ".$errmsg, LOG_ERR);
3064 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3066 $this->
db->rollback();
3070 $this->error = $this->
db->error();
3071 $this->
db->rollback();
3107 public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $price_base_type =
'HT', $info_bits = 0, $date_start =
'', $date_end =
'', $type = 0, $fk_parent_line = 0, $skip_update_total = 0, $fk_fournprice =
null, $pa_ht = 0, $label =
'', $special_code = 0, $array_options = 0, $fk_unit =
null, $pu_ht_devise = 0, $notrigger = 0, $ref_ext =
'', $rang = 0)
3109 global $conf, $mysoc, $langs, $user;
3111 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");
3112 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3119 if (empty($info_bits)) {
3122 if (empty($txtva)) {
3125 if (empty($txlocaltax1)) {
3128 if (empty($txlocaltax2)) {
3131 if (empty($remise_percent)) {
3132 $remise_percent = 0;
3134 if (empty($special_code) || $special_code == 3) {
3137 if (empty($ref_ext)) {
3141 if ($date_start && $date_end && $date_start > $date_end) {
3142 $langs->load(
"errors");
3143 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3147 $remise_percent =
price2num($remise_percent);
3151 $pu_ht_devise =
price2num($pu_ht_devise);
3152 if (!preg_match(
'/\((.*)\)/', $txtva)) {
3170 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3171 $vat_src_code = $reg[1];
3172 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3175 $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);
3177 $total_ht = $tabprice[0];
3178 $total_tva = $tabprice[1];
3179 $total_ttc = $tabprice[2];
3180 $total_localtax1 = $tabprice[9];
3181 $total_localtax2 = $tabprice[10];
3182 $pu_ht = $tabprice[3];
3183 $pu_tva = $tabprice[4];
3184 $pu_ttc = $tabprice[5];
3187 $multicurrency_total_ht = $tabprice[16];
3188 $multicurrency_total_tva = $tabprice[17];
3189 $multicurrency_total_ttc = $tabprice[18];
3190 $pu_ht_devise = $tabprice[19];
3194 if ($price_base_type ==
'TTC') {
3195 $subprice = $pu_ttc;
3200 if ($remise_percent > 0) {
3201 $remise = round(($pu * $remise_percent / 100), 2);
3202 $price = ($pu - $remise);
3207 $line->fetch($rowid);
3208 $line->fetch_optionals();
3210 if (!empty($line->fk_product)) {
3212 $result = $product->fetch($line->fk_product);
3213 $product_type = $product->type;
3215 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) {
3216 $langs->load(
"errors");
3217 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
3218 $this->errors[] = $this->error;
3220 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
3222 $this->
db->rollback();
3227 $staticline = clone $line;
3229 $line->oldline = $staticline;
3230 $this->line = $line;
3231 $this->line->context = $this->context;
3232 $this->line->rang = $rang;
3235 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
3236 $rangmax = $this->
line_max($fk_parent_line);
3237 $this->line->rang = $rangmax + 1;
3240 $this->line->id = $rowid;
3241 $this->line->label = $label;
3242 $this->line->desc = $desc;
3243 $this->line->qty = $qty;
3244 $this->line->ref_ext = $ref_ext;
3246 $this->line->vat_src_code = $vat_src_code;
3247 $this->line->tva_tx = $txtva;
3248 $this->line->localtax1_tx = $txlocaltax1;
3249 $this->line->localtax2_tx = $txlocaltax2;
3250 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3251 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3252 $this->line->remise_percent = $remise_percent;
3253 $this->line->subprice = $subprice;
3254 $this->line->info_bits = $info_bits;
3255 $this->line->special_code = $special_code;
3256 $this->line->total_ht = $total_ht;
3257 $this->line->total_tva = $total_tva;
3258 $this->line->total_localtax1 = $total_localtax1;
3259 $this->line->total_localtax2 = $total_localtax2;
3260 $this->line->total_ttc = $total_ttc;
3261 $this->line->date_start = $date_start;
3262 $this->line->date_end = $date_end;
3263 $this->line->product_type = $type;
3264 $this->line->fk_parent_line = $fk_parent_line;
3265 $this->line->skip_update_total = $skip_update_total;
3266 $this->line->fk_unit = $fk_unit;
3268 $this->line->fk_fournprice = $fk_fournprice;
3269 $this->line->pa_ht = $pa_ht;
3272 $this->line->multicurrency_subprice = $pu_ht_devise;
3273 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
3274 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
3275 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
3278 $this->line->price = $price;
3280 if (is_array($array_options) && count($array_options) > 0) {
3282 foreach ($array_options as $key => $value) {
3283 $this->line->array_options[$key] = $array_options[$key];
3287 $result = $this->line->update($user, $notrigger);
3290 if (!empty($fk_parent_line)) {
3297 $this->
db->commit();
3300 $this->error = $this->line->error;
3302 $this->
db->rollback();
3306 $this->error = get_class($this).
"::updateline Order status makes operation forbidden";
3307 $this->errors = array(
'OrderStatusMakeOperationForbidden');
3326 if (isset($this->
ref)) {
3327 $this->
ref = trim($this->
ref);
3329 if (isset($this->ref_client)) {
3330 $this->ref_client = trim($this->ref_client);
3332 if (isset($this->note) || isset($this->note_private)) {
3333 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
3335 if (isset($this->note_public)) {
3336 $this->note_public = trim($this->note_public);
3338 if (isset($this->model_pdf)) {
3339 $this->model_pdf = trim($this->model_pdf);
3341 if (isset($this->import_key)) {
3342 $this->import_key = trim($this->import_key);
3344 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
3350 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande SET";
3352 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
3353 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
3354 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
3355 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
3356 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
3357 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
3358 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
3359 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
3360 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
3361 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
3362 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
3363 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
3364 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id :
"null").
",";
3365 $sql .=
" fk_user_valid=".((isset($this->user_valid) && $this->user_valid > 0) ? $this->user_valid :
"null").
",";
3366 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
3367 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
3368 $sql .=
" deposit_percent=".(!empty($this->deposit_percent) ? strval($this->deposit_percent) :
"null").
",";
3369 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
3370 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
3371 $sql .=
" fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id :
"null").
",";
3372 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
3373 $sql .=
" fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id :
"null").
",";
3374 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
3375 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
3376 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
3377 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
3379 $sql .=
" WHERE rowid=".((int) $this->
id);
3383 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3384 $resql = $this->
db->query(
$sql);
3386 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
3396 if (!$error && !$notrigger) {
3407 foreach ($this->errors as $errmsg) {
3408 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3409 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3411 $this->
db->rollback();
3414 $this->
db->commit();
3426 public function delete($user, $notrigger = 0)
3428 global $conf, $langs;
3429 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3433 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
3448 $this->errors[] = $langs->trans(
'SomeShipmentExists');
3453 if (!$error && !empty($this->table_element_line)) {
3454 $tabletodelete = $this->table_element_line;
3455 $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).
")";
3456 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3457 if (!$this->
db->query($sqlef) || !$this->
db->query(
$sql)) {
3459 $this->error = $this->
db->lasterror();
3460 $this->errors[] = $this->error;
3461 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3486 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3492 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
3493 $res = $this->
db->query(
$sql);
3496 $this->error = $this->
db->lasterror();
3497 $this->errors[] = $this->error;
3498 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3513 if ($conf->commande->multidir_output[$this->entity] && !empty($this->
ref)) {
3514 $dir = $conf->commande->multidir_output[$this->entity].
"/".$ref;
3515 $file = $dir.
"/".$ref.
".pdf";
3516 if (file_exists($file)) {
3520 $this->error =
'ErrorFailToDeleteFile';
3521 $this->errors[] = $this->error;
3522 $this->
db->rollback();
3526 if (file_exists($dir)) {
3529 $this->error =
'ErrorFailToDeleteDir';
3530 $this->errors[] = $this->error;
3531 $this->
db->rollback();
3539 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3540 $this->
db->commit();
3543 $this->
db->rollback();
3559 global $conf, $langs;
3563 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.date_livraison as delivery_date, c.fk_statut, c.total_ht";
3564 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as c";
3565 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3566 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
3567 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3572 $sql .=
" AND ((c.fk_statut IN (".self::STATUS_VALIDATED.
",".self::STATUS_SHIPMENTONPROCESS.
")) OR (c.fk_statut = ".self::STATUS_CLOSED.
" AND c.facture = 0))";
3574 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3577 $resql = $this->
db->query(
$sql);
3580 $response->warning_delay = $conf->commande->client->warning_delay / 60 / 60 / 24;
3581 $response->label = $langs->trans(
"OrdersToProcess");
3582 $response->labelShort = $langs->trans(
"Opened");
3583 $response->url = DOL_URL_ROOT.
'/commande/list.php?search_status=-3&mainmenu=commercial&leftmenu=orders';
3588 while ($obj = $this->
db->fetch_object($resql)) {
3589 $response->nbtodo++;
3590 $response->total += $obj->total_ht;
3592 $generic_commande->statut = $obj->fk_statut;
3593 $generic_commande->date_commande = $this->
db->jdate($obj->date_commande);
3594 $generic_commande->date = $this->
db->jdate($obj->date_commande);
3595 $generic_commande->date_livraison = $this->
db->jdate($obj->delivery_date);
3596 $generic_commande->delivery_date = $this->
db->jdate($obj->delivery_date);
3598 if ($generic_commande->hasDelay()) {
3599 $response->nbtodolate++;
3605 $this->error = $this->
db->error();
3619 $label = $langs->trans(
'OrderSource'.$this->source);
3621 if ($label ==
'OrderSource') {
3635 return $this->
LibStatut($this->statut, $this->billed, $mode);
3648 public function LibStatut($status, $billed, $mode, $donotshowbilled = 0)
3651 global $langs, $conf, $hookmanager;
3654 if (empty($donotshowbilled)) {
3655 $billedtext .= ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3660 if ($status == self::STATUS_CANCELED) {
3661 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderCanceled');
3662 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderCanceledShort');
3663 $statusType =
'status9';
3664 } elseif ($status == self::STATUS_DRAFT) {
3665 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDraft');
3666 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDraftShort');
3667 $statusType =
'status0';
3668 } elseif ($status == self::STATUS_VALIDATED) {
3669 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderValidated').$billedtext;
3670 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderValidatedShort').$billedtext;
3671 $statusType =
'status1';
3672 } elseif ($status == self::STATUS_SHIPMENTONPROCESS) {
3673 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderSent').$billedtext;
3674 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderSentShort').$billedtext;
3675 $labelTooltip = $langs->transnoentitiesnoconv(
"StatusOrderSent");
3676 if (!empty($this->delivery_date)) {
3677 $labelTooltip .=
' - '.$langs->transnoentitiesnoconv(
"DateDeliveryPlanned").dol_print_date($this->delivery_date,
'day').$billedtext;
3679 $statusType =
'status4';
3680 } elseif ($status == self::STATUS_CLOSED && (!$billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3681 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderToBill');
3682 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderToBillShort');
3683 $statusType =
'status4';
3684 } elseif ($status == self::STATUS_CLOSED && ($billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3685 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderProcessed').$billedtext;
3686 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderProcessedShort').$billedtext;
3687 $statusType =
'status6';
3688 } elseif ($status == self::STATUS_CLOSED && (!empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3689 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDelivered');
3690 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDeliveredShort');
3691 $statusType =
'status6';
3693 $labelStatus = $langs->transnoentitiesnoconv(
'Unknown');
3694 $labelStatusShort =
'';
3699 $parameters = array(
3700 'status' => $status,
3702 'billed' => $billed,
3703 'donotshowbilled' => $donotshowbilled
3706 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
3709 return $hookmanager->resPrint;
3712 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', array(
'tooltip' => $labelTooltip));
3723 global $conf, $langs, $user;
3727 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
3728 return [
'optimize' => $langs->trans(
"Order")];
3731 if ($user->hasRight(
'commande',
'lire')) {
3732 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Order").
'</u>';
3733 if (isset($this->statut)) {
3734 $datas[] =
' '.$this->getLibStatut(5);
3736 $datas[
'Ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
3737 $datas[
'RefCustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.(empty($this->ref_customer) ? (empty($this->ref_client) ?
'' : $this->ref_client) : $this->ref_customer);
3738 if (!empty($this->total_ht)) {
3739 $datas[
'AmountHT'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
3741 if (!empty($this->total_tva)) {
3742 $datas[
'VAT'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
3744 if (!empty($this->total_ttc)) {
3745 $datas[
'AmountTTC'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
3747 if (!empty($this->date)) {
3748 $datas[
'Date'] =
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
3750 if (!empty($this->delivery_date)) {
3751 $datas[
'DeliveryDate'] =
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
3771 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0, $target =
'')
3773 global $conf, $langs, $user, $hookmanager;
3775 if (!empty($conf->dol_no_mouse_hover)) {
3781 if (
isModEnabled(
"expedition") && ($option ==
'1' || $option ==
'2')) {
3782 $url = DOL_URL_ROOT.
'/expedition/shipment.php?id='.$this->id;
3784 $url = DOL_URL_ROOT.
'/commande/card.php?id='.$this->id;
3787 if (!$user->rights->commande->lire) {
3791 if ($option !==
'nolink') {
3793 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3794 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
3795 $add_save_lastsearch_values = 1;
3797 if ($add_save_lastsearch_values) {
3798 $url .=
'&save_lastsearch_values=1';
3807 'objecttype' => $this->element,
3808 'option' => $option,
3810 $classfortooltip =
'classfortooltip';
3813 $classfortooltip =
'classforajaxtooltip';
3814 $dataparams =
' data-params='.json_encode($params);
3821 if (empty($notooltip) && $user->hasRight(
'commande',
'lire')) {
3822 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
3823 $label = $langs->trans(
"Order");
3824 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
3826 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
3827 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
3829 $target_value = array(
'_self',
'_blank',
'_parent',
'_top');
3830 if (in_array($target, $target_value)) {
3831 $linkclose .=
' target="'.dol_escape_htmltag($target).
'"';
3835 $linkstart =
'<a href="'.$url.
'"';
3836 $linkstart .= $linkclose.
'>';
3839 if ($option ===
'nolink') {
3844 $result .= $linkstart;
3846 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') : $dataparams.
' class="'.(($withpicto != 2) ?
'paddingright ' :
'').$classfortooltip.
'"'), 0, 0, $notooltip ? 0 : 1);
3848 if ($withpicto != 2) {
3849 $result .= $this->ref;
3851 $result .= $linkend;
3853 if ($addlinktonotes) {
3854 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3857 $result .=
' <span class="note inline-block">';
3858 $result .=
'<a href="'.DOL_URL_ROOT.
'/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3863 $result .=
'</span>';
3868 $hookmanager->initHooks(array($this->element .
'dao'));
3869 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
3870 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3872 $result = $hookmanager->resPrint;
3874 $result .= $hookmanager->resPrint;
3888 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem,';
3889 $sql .=
' date_valid as datev,';
3890 $sql .=
' date_cloture as datecloture,';
3891 $sql .=
' fk_user_author, fk_user_valid, fk_user_cloture';
3892 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
3893 $sql .=
' WHERE c.rowid = '.((int) $id);
3894 $result = $this->
db->query(
$sql);
3896 if ($this->
db->num_rows($result)) {
3897 $obj = $this->
db->fetch_object($result);
3898 $this->
id = $obj->rowid;
3899 if ($obj->fk_user_author) {
3900 $this->user_creation_id = $obj->fk_user_author;
3902 if ($obj->fk_user_valid) {
3903 $this->user_validation_id = $obj->fk_user_valid;
3905 if ($obj->fk_user_cloture) {
3906 $this->user_closing_id = $obj->fk_user_cloture;
3909 $this->date_creation = $this->
db->jdate($obj->datec);
3910 $this->date_modification = $this->
db->jdate($obj->datem);
3911 $this->date_validation = $this->
db->jdate($obj->datev);
3912 $this->date_cloture = $this->
db->jdate($obj->datecloture);
3915 $this->
db->free($result);
3931 global $conf, $langs;
3933 dol_syslog(get_class($this).
"::initAsSpecimen");
3938 $sql =
"SELECT rowid";
3939 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
3940 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3941 $sql .= $this->
db->plimit(100);
3943 $resql = $this->
db->query(
$sql);
3945 $num_prods = $this->
db->num_rows($resql);
3947 while ($i < $num_prods) {
3949 $row = $this->
db->fetch_row($resql);
3950 $prodids[$i] = $row[0];
3956 $this->
ref =
'SPECIMEN';
3957 $this->specimen = 1;
3959 $this->date = time();
3960 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
3961 $this->cond_reglement_code =
'RECEP';
3962 $this->mode_reglement_code =
'CHQ';
3963 $this->availability_code =
'DSP';
3964 $this->demand_reason_code =
'SRC_00';
3966 $this->note_public =
'This is a comment (public)';
3967 $this->note_private =
'This is a comment (private)';
3969 $this->multicurrency_tx = 1;
3970 $this->multicurrency_code = $conf->currency;
3975 while ($xnbp < $nbp) {
3978 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
3980 $line->subprice = 100;
3984 $line->total_ht = 50;
3985 $line->total_ttc = 60;
3986 $line->total_tva = 10;
3987 $line->remise_percent = 50;
3989 $line->total_ht = 100;
3990 $line->total_ttc = 120;
3991 $line->total_tva = 20;
3992 $line->remise_percent = 0;
3994 if ($num_prods > 0) {
3995 $prodid = mt_rand(1, $num_prods);
3996 $line->fk_product = $prodids[$prodid];
3997 $line->product_ref =
'SPECIMEN';
4000 $this->lines[$xnbp] = $line;
4002 $this->total_ht += $line->total_ht;
4003 $this->total_tva += $line->total_tva;
4004 $this->total_ttc += $line->total_ttc;
4022 $this->nb = array();
4025 $sql =
"SELECT count(co.rowid) as nb";
4026 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as co";
4027 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
4028 if (empty($user->rights->societe->client->voir) && !$user->socid) {
4029 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
4030 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
4033 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'commande').
")";
4035 $resql = $this->
db->query(
$sql);
4037 while ($obj = $this->
db->fetch_object($resql)) {
4038 $this->nb[
"orders"] = $obj->nb;
4040 $this->
db->free($resql);
4044 $this->error = $this->
db->error();
4070 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
4072 global $conf, $langs;
4074 $langs->load(
"orders");
4075 $outputlangs->load(
"products");
4078 $modele =
'einstein';
4080 if (!empty($this->model_pdf)) {
4081 $modele = $this->model_pdf;
4082 } elseif (!empty($this->modelpdf)) {
4083 $modele = $this->modelpdf;
4084 } elseif (!empty($conf->global->COMMANDE_ADDON_PDF)) {
4085 $modele = $conf->global->COMMANDE_ADDON_PDF;
4089 $modelpath =
"core/modules/commande/doc/";
4091 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
4144 return max($this->date, $this->date_livraison) < ($now - $conf->commande->client->warning_delay);
4154 global $conf, $langs;
4156 if (empty($this->date_livraison)) {
4157 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
4159 $text = $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->date_livraison,
'day');
4161 $text .=
' '.($conf->commande->client->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->client->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
4176 public $element =
'commandedet';
4178 public $table_element =
'commandedet';
4186 public $fk_commande;
4194 public $commande_id;
4196 public $fk_parent_line;
4208 public $fk_remise_except;
4214 public $fk_fournprice;
4234 public $skip_update_total;
4255 $sql =
'SELECT cd.rowid, cd.fk_commande, cd.fk_parent_line, cd.fk_product, cd.product_type, cd.label as custom_label, cd.description, cd.price, cd.qty, cd.tva_tx, cd.localtax1_tx, cd.localtax2_tx,';
4256 $sql .=
' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
4257 $sql .=
' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.fk_product_fournisseur_price as fk_fournprice, cd.buy_price_ht as pa_ht, cd.rang, cd.special_code,';
4258 $sql .=
' cd.fk_unit,';
4259 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
4260 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
4261 $sql .=
' cd.date_start, cd.date_end, cd.vat_src_code';
4262 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as cd';
4263 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
4264 $sql .=
' WHERE cd.rowid = '.((int) $rowid);
4265 $result = $this->
db->query(
$sql);
4267 $objp = $this->
db->fetch_object($result);
4270 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.
$sql;
4274 $this->
rowid = $objp->rowid;
4275 $this->
id = $objp->rowid;
4276 $this->fk_commande = $objp->fk_commande;
4277 $this->fk_parent_line = $objp->fk_parent_line;
4278 $this->label = $objp->custom_label;
4279 $this->desc = $objp->description;
4280 $this->qty = $objp->qty;
4281 $this->
price = $objp->price;
4282 $this->subprice = $objp->subprice;
4283 $this->ref_ext = $objp->ref_ext;
4284 $this->vat_src_code = $objp->vat_src_code;
4285 $this->tva_tx = $objp->tva_tx;
4286 $this->localtax1_tx = $objp->localtax1_tx;
4287 $this->localtax2_tx = $objp->localtax2_tx;
4288 $this->remise = $objp->remise;
4289 $this->remise_percent = $objp->remise_percent;
4290 $this->fk_remise_except = $objp->fk_remise_except;
4291 $this->fk_product = $objp->fk_product;
4292 $this->product_type = $objp->product_type;
4293 $this->info_bits = $objp->info_bits;
4294 $this->special_code = $objp->special_code;
4295 $this->total_ht = $objp->total_ht;
4296 $this->total_tva = $objp->total_tva;
4297 $this->total_localtax1 = $objp->total_localtax1;
4298 $this->total_localtax2 = $objp->total_localtax2;
4299 $this->total_ttc = $objp->total_ttc;
4300 $this->fk_fournprice = $objp->fk_fournprice;
4301 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
4302 $this->pa_ht = $marginInfos[0];
4303 $this->marge_tx = $marginInfos[1];
4304 $this->marque_tx = $marginInfos[2];
4305 $this->special_code = $objp->special_code;
4306 $this->rang = $objp->rang;
4308 $this->
ref = $objp->product_ref;
4310 $this->product_ref = $objp->product_ref;
4311 $this->product_label = $objp->product_label;
4312 $this->product_desc = $objp->product_desc;
4313 $this->product_tobatch = $objp->product_tobatch;
4314 $this->fk_unit = $objp->fk_unit;
4316 $this->date_start = $this->
db->jdate($objp->date_start);
4317 $this->date_end = $this->
db->jdate($objp->date_end);
4319 $this->fk_multicurrency = $objp->fk_multicurrency;
4320 $this->multicurrency_code = $objp->multicurrency_code;
4321 $this->multicurrency_subprice = $objp->multicurrency_subprice;
4322 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
4323 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
4324 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
4326 $this->
db->free($result);
4330 $this->error = $this->
db->lasterror();
4342 public function delete(
User $user, $notrigger = 0)
4344 global $conf, $langs;
4348 if (empty($this->
id) && !empty($this->
rowid)) {
4349 $this->
id = $this->rowid;
4353 $sqlCheckShipmentLine =
"SELECT";
4354 $sqlCheckShipmentLine .=
" ed.rowid";
4355 $sqlCheckShipmentLine .=
" FROM ".MAIN_DB_PREFIX.
"expeditiondet ed";
4356 $sqlCheckShipmentLine .=
" WHERE ed.fk_origin_line = ".((int) $this->
id);
4358 $resqlCheckShipmentLine = $this->
db->query($sqlCheckShipmentLine);
4359 if (!$resqlCheckShipmentLine) {
4361 $this->error = $this->
db->lasterror();
4362 $this->errors[] = $this->error;
4364 $langs->load(
'errors');
4365 $num = $this->
db->num_rows($resqlCheckShipmentLine);
4368 $objCheckShipmentLine = $this->
db->fetch_object($resqlCheckShipmentLine);
4369 $this->error = $langs->trans(
'ErrorRecordAlreadyExists').
' : '.$langs->trans(
'ShipmentLine').
' '.$objCheckShipmentLine->rowid;
4370 $this->errors[] = $this->error;
4372 $this->
db->free($resqlCheckShipmentLine);
4375 dol_syslog(__METHOD__.
'Error ; '.$this->error, LOG_ERR);
4381 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"commandedet WHERE rowid = ".((int) $this->
id);
4384 $resql = $this->
db->query(
$sql);
4386 if (!$error && !$notrigger) {
4388 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
4400 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
4405 $this->
db->commit();
4409 foreach ($this->errors as $errmsg) {
4410 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
4411 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4413 $this->
db->rollback();
4416 $this->error = $this->
db->lasterror();
4428 public function insert($user =
null, $notrigger = 0)
4430 global $langs, $conf;
4434 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4436 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
4439 if (empty($this->tva_tx)) {
4442 if (empty($this->localtax1_tx)) {
4443 $this->localtax1_tx = 0;
4445 if (empty($this->localtax2_tx)) {
4446 $this->localtax2_tx = 0;
4448 if (empty($this->localtax1_type)) {
4449 $this->localtax1_type = 0;
4451 if (empty($this->localtax2_type)) {
4452 $this->localtax2_type = 0;
4454 if (empty($this->total_localtax1)) {
4455 $this->total_localtax1 = 0;
4457 if (empty($this->total_localtax2)) {
4458 $this->total_localtax2 = 0;
4460 if (empty($this->rang)) {
4463 if (empty($this->remise_percent)) {
4464 $this->remise_percent = 0;
4466 if (empty($this->info_bits)) {
4467 $this->info_bits = 0;
4469 if (empty($this->special_code)) {
4470 $this->special_code = 0;
4472 if (empty($this->fk_parent_line)) {
4473 $this->fk_parent_line = 0;
4475 if (empty($this->pa_ht)) {
4478 if (empty($this->ref_ext)) {
4479 $this->ref_ext =
'';
4483 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4484 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4488 $this->pa_ht = $result;
4493 if ($this->product_type < 0) {
4500 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'commandedet';
4501 $sql .=
' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
4502 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
4503 $sql .=
' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
4504 $sql .=
' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
4505 $sql .=
' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
4507 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
4509 $sql .=
" VALUES (".$this->fk_commande.
",";
4510 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
4511 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
4512 $sql .=
" '".$this->db->escape($this->desc).
"',";
4513 $sql .=
" '".price2num($this->qty).
"',";
4514 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
4515 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
4516 $sql .=
" '".price2num($this->tva_tx).
"',";
4517 $sql .=
" '".price2num($this->localtax1_tx).
"',";
4518 $sql .=
" '".price2num($this->localtax2_tx).
"',";
4519 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
4520 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
4521 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
4522 $sql .=
" '".$this->db->escape($this->product_type).
"',";
4523 $sql .=
" '".price2num($this->remise_percent).
"',";
4524 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice) :
"null").
",";
4525 $sql .=
" ".($this->price !=
'' ?
"'".price2num($this->
price).
"'" :
"null").
",";
4526 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
4527 $sql .=
' '.((int) $this->special_code).
',';
4528 $sql .=
' '.((int) $this->rang).
',';
4529 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
4530 $sql .=
' '.price2num($this->pa_ht).
',';
4531 $sql .=
" ".((int) $this->info_bits).
",";
4532 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
4533 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
4534 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
4535 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
4536 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
4537 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
4538 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
',';
4539 $sql .=
' '.(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
4540 $sql .=
", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) :
'NULL');
4541 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
4542 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
4543 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
4544 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
4545 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
4548 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
4549 $resql = $this->
db->query(
$sql);
4551 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
4552 $this->
rowid = $this->id;
4561 if (!$error && !$notrigger) {
4563 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
4571 $this->
db->commit();
4575 foreach ($this->errors as $errmsg) {
4576 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
4577 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4579 $this->
db->rollback();
4582 $this->error = $this->
db->error();
4583 $this->
db->rollback();
4597 global $conf, $langs;
4601 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4604 if (empty($this->tva_tx)) {
4607 if (empty($this->localtax1_tx)) {
4608 $this->localtax1_tx = 0;
4610 if (empty($this->localtax2_tx)) {
4611 $this->localtax2_tx = 0;
4613 if (empty($this->localtax1_type)) {
4614 $this->localtax1_type = 0;
4616 if (empty($this->localtax2_type)) {
4617 $this->localtax2_type = 0;
4619 if (empty($this->qty)) {
4622 if (empty($this->total_localtax1)) {
4623 $this->total_localtax1 = 0;
4625 if (empty($this->total_localtax2)) {
4626 $this->total_localtax2 = 0;
4628 if (empty($this->marque_tx)) {
4629 $this->marque_tx = 0;
4631 if (empty($this->marge_tx)) {
4632 $this->marge_tx = 0;
4634 if (empty($this->remise_percent)) {
4635 $this->remise_percent = 0;
4637 if (empty($this->remise)) {
4640 if (empty($this->info_bits)) {
4641 $this->info_bits = 0;
4643 if (empty($this->special_code)) {
4644 $this->special_code = 0;
4646 if (empty($this->product_type)) {
4647 $this->product_type = 0;
4649 if (empty($this->fk_parent_line)) {
4650 $this->fk_parent_line = 0;
4652 if (empty($this->pa_ht)) {
4655 if (empty($this->ref_ext)) {
4656 $this->ref_ext =
'';
4660 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4661 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4665 $this->pa_ht = $result;
4672 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4673 $sql .=
" description='".$this->db->escape($this->desc).
"'";
4674 $sql .=
" , label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
4675 $sql .=
" , vat_src_code=".(!empty($this->vat_src_code) ?
"'".$this->db->escape($this->vat_src_code).
"'" :
"''");
4676 $sql .=
" , tva_tx=".price2num($this->tva_tx);
4677 $sql .=
" , localtax1_tx=".price2num($this->localtax1_tx);
4678 $sql .=
" , localtax2_tx=".price2num($this->localtax2_tx);
4679 $sql .=
" , localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
4680 $sql .=
" , localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
4681 $sql .=
" , qty=".price2num($this->qty);
4682 $sql .=
" , ref_ext='".$this->db->escape($this->ref_ext).
"'";
4683 $sql .=
" , subprice=".price2num($this->subprice);
4684 $sql .=
" , remise_percent=".price2num($this->remise_percent);
4685 $sql .=
" , price=".price2num($this->
price);
4686 $sql .=
" , remise=".price2num($this->remise);
4687 if (empty($this->skip_update_total)) {
4688 $sql .=
" , total_ht=".price2num($this->total_ht);
4689 $sql .=
" , total_tva=".price2num($this->total_tva);
4690 $sql .=
" , total_ttc=".price2num($this->total_ttc);
4691 $sql .=
" , total_localtax1=".price2num($this->total_localtax1);
4692 $sql .=
" , total_localtax2=".price2num($this->total_localtax2);
4694 $sql .=
" , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null");
4695 $sql .=
" , buy_price_ht='".price2num($this->pa_ht).
"'";
4696 $sql .=
" , info_bits=".((int) $this->info_bits);
4697 $sql .=
" , special_code=".((int) $this->special_code);
4698 $sql .=
" , date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
4699 $sql .=
" , date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
4700 $sql .=
" , product_type=".$this->product_type;
4701 $sql .=
" , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line :
"null");
4702 if (!empty($this->rang)) {
4703 $sql .=
", rang=".((int) $this->rang);
4705 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
4708 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice);
4709 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
4710 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
4711 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
4713 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4715 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
4716 $resql = $this->
db->query(
$sql);
4719 $this->
id = $this->rowid;
4726 if (!$error && !$notrigger) {
4728 $result = $this->
call_trigger(
'LINEORDER_MODIFY', $user);
4736 $this->
db->commit();
4740 foreach ($this->errors as $errmsg) {
4741 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
4742 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4744 $this->
db->rollback();
4747 $this->error = $this->
db->error();
4748 $this->
db->rollback();
4766 if (empty($this->total_localtax1)) {
4767 $this->total_localtax1 = 0;
4769 if (empty($this->total_localtax2)) {
4770 $this->total_localtax2 = 0;
4774 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4775 $sql .=
" total_ht='".price2num($this->total_ht).
"'";
4776 $sql .=
",total_tva='".price2num($this->total_tva).
"'";
4777 $sql .=
",total_localtax1='".price2num($this->total_localtax1).
"'";
4778 $sql .=
",total_localtax2='".price2num($this->total_localtax2).
"'";
4779 $sql .=
",total_ttc='".price2num($this->total_ttc).
"'";
4780 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4782 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
4784 $resql = $this->
db->query(
$sql);
4786 $this->
db->commit();
4789 $this->error = $this->
db->error();
4790 $this->
db->rollback();