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;
242 public $lines = array();
248 public $fk_multicurrency;
253 public $multicurrency_code;
254 public $multicurrency_tx;
255 public $multicurrency_total_ht;
256 public $multicurrency_total_tva;
257 public $multicurrency_total_ttc;
272 public $online_payment_url;
303 public $fields = array(
304 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
305 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>20,
'index'=>1),
306 'ref' =>array(
'type'=>
'varchar(30)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'showoncombobox'=>1,
'position'=>25),
307 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefExt',
'enabled'=>1,
'visible'=>0,
'position'=>26),
308 'ref_client' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefCustomer',
'enabled'=>1,
'visible'=>-1,
'position'=>28),
309 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'$conf->societe->enabled',
'visible'=>-1,
'notnull'=>1,
'position'=>20),
310 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label'=>
'Project',
'enabled'=>
"isModEnabled('project')",
'visible'=>-1,
'position'=>25),
311 'date_commande' =>array(
'type'=>
'date',
'label'=>
'Date',
'enabled'=>1,
'visible'=>1,
'position'=>60),
312 'date_valid' =>array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>62),
313 'date_cloture' =>array(
'type'=>
'datetime',
'label'=>
'DateClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>65),
314 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>85),
315 'fk_user_cloture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
316 'source' =>array(
'type'=>
'smallint(6)',
'label'=>
'Source',
'enabled'=>1,
'visible'=>-1,
'position'=>95),
318 'remise_percent' =>array(
'type'=>
'double',
'label'=>
'RelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>110),
319 'remise_absolue' =>array(
'type'=>
'double',
'label'=>
'CustomerRelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>115),
321 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'VAT',
'enabled'=>1,
'visible'=>-1,
'position'=>125,
'isameasure'=>1),
322 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax1',
'enabled'=>1,
'visible'=>-1,
'position'=>130,
'isameasure'=>1),
323 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax2',
'enabled'=>1,
'visible'=>-1,
'position'=>135,
'isameasure'=>1),
324 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalHT',
'enabled'=>1,
'visible'=>-1,
'position'=>140,
'isameasure'=>1),
325 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>145,
'isameasure'=>1),
326 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>150),
327 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>155),
328 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'PDFTemplate',
'enabled'=>1,
'visible'=>0,
'position'=>160),
330 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'BankAccount',
'enabled'=>
'$conf->banque->enabled',
'visible'=>-1,
'position'=>170),
331 'fk_currency' =>array(
'type'=>
'varchar(3)',
'label'=>
'MulticurrencyID',
'enabled'=>1,
'visible'=>-1,
'position'=>175),
332 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
333 'deposit_percent' =>array(
'type'=>
'varchar(63)',
'label'=>
'DepositPercent',
'enabled'=>1,
'visible'=>-1,
'position'=>181),
334 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
335 'date_livraison' =>array(
'type'=>
'date',
'label'=>
'DateDeliveryPlanned',
'enabled'=>1,
'visible'=>-1,
'position'=>190),
336 'fk_shipping_method' =>array(
'type'=>
'integer',
'label'=>
'ShippingMethod',
'enabled'=>1,
'visible'=>-1,
'position'=>195),
337 'fk_warehouse' =>array(
'type'=>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label'=>
'Fk warehouse',
'enabled'=>
'$conf->stock->enabled',
'visible'=>-1,
'position'=>200),
338 'fk_availability' =>array(
'type'=>
'integer',
'label'=>
'Availability',
'enabled'=>1,
'visible'=>-1,
'position'=>205),
339 'fk_input_reason' =>array(
'type'=>
'integer',
'label'=>
'InputReason',
'enabled'=>1,
'visible'=>-1,
'position'=>210),
341 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>225),
342 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>230),
343 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLabel',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>235),
344 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>240),
345 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'MulticurrencyCurrency',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>245),
346 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyRate',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>250,
'isameasure'=>1),
347 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>255,
'isameasure'=>1),
348 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>260,
'isameasure'=>1),
349 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>265,
'isameasure'=>1),
350 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>-1,
'position'=>270),
351 'module_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSModule',
'enabled'=>1,
'visible'=>-1,
'position'=>275),
352 'pos_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSTerminal',
'enabled'=>1,
'visible'=>-1,
'position'=>280),
353 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-1,
'position'=>300),
354 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>302),
355 'date_creation' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>304),
356 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>306),
357 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>400),
358 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>-1,
'position'=>500),
383 const STATUS_ACCEPTED = 2;
410 global $langs, $conf;
411 $langs->load(
"order");
413 if (!empty($conf->global->COMMANDE_ADDON)) {
416 $file = $conf->global->COMMANDE_ADDON.
".php";
417 $classname = $conf->global->COMMANDE_ADDON;
420 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
421 foreach ($dirmodels as $reldir) {
425 $mybool |= @include_once $dir.$file;
428 if ($mybool ===
false) {
433 $obj =
new $classname();
434 $numref = $obj->getNextValue($soc, $this);
439 $this->error = $obj->error;
444 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_COMMANDE_ADDON_NotDefined");
458 public function valid($user, $idwarehouse = 0, $notrigger = 0)
460 global $conf, $langs;
462 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
467 if ($this->statut == self::STATUS_VALIDATED) {
468 dol_syslog(get_class($this).
"::valid action abandonned: already validated", LOG_WARNING);
472 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
473 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate)))) {
474 $this->error =
'NotEnoughPermissions';
475 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
485 $soc->fetch($this->socid);
488 $result = $soc->set_as_client();
491 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
499 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
500 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
501 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
502 $sql .=
" date_valid='".$this->db->idate($now).
"',";
503 $sql .=
" fk_user_valid = ".($user->id > 0 ? (int) $user->id :
"null").
",";
504 $sql .=
" fk_user_modif = ".((int) $user->id);
505 $sql .=
" WHERE rowid = ".((int) $this->
id);
507 dol_syslog(get_class($this).
"::valid", LOG_DEBUG);
511 $this->error = $this->
db->lasterror();
517 if ($result >= 0 &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
518 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
519 $langs->load(
"agenda");
522 $cpt = count($this->lines);
523 for ($i = 0; $i < $cpt; $i++) {
524 if ($this->lines[$i]->fk_product > 0) {
526 $mouvP->origin = &$this;
527 $mouvP->setOrigin($this->element, $this->
id);
529 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"OrderValidatedInDolibarr", $num));
532 $this->error = $mouvP->error;
542 if (!$error && !$notrigger) {
552 $this->oldref = $this->ref;
555 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
557 $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).
"'";
558 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'commande/".$this->
db->escape($this->
ref).
"' and entity = ".$conf->entity;
561 $error++; $this->error = $this->
db->lasterror();
567 $dirsource = $conf->commande->multidir_output[$this->entity].
'/'.$oldref;
568 $dirdest = $conf->commande->multidir_output[$this->entity].
'/'.$newref;
569 if (!$error && file_exists($dirsource)) {
570 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
572 if (@rename($dirsource, $dirdest)) {
575 $listoffiles =
dol_dir_list($conf->commande->multidir_output[$this->entity].
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
576 foreach ($listoffiles as $fileentry) {
577 $dirsource = $fileentry[
'name'];
578 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
579 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
580 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
581 @rename($dirsource, $dirdest);
592 $this->brouillon = 0;
599 $this->
db->rollback();
615 global $conf, $langs;
620 if ($this->statut <= self::STATUS_DRAFT) {
624 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
625 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate)))) {
626 $this->error =
'Permission denied';
634 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
635 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT.
",";
636 $sql .=
" fk_user_modif = ".((int) $user->id);
637 $sql .=
" WHERE rowid = ".((int) $this->
id);
639 if ($this->
db->query($sql)) {
641 $this->oldcopy = clone $this;
645 if (
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
648 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
649 $langs->load(
"agenda");
651 $num = count($this->lines);
652 for ($i = 0; $i < $num; $i++) {
653 if ($this->lines[$i]->fk_product > 0) {
655 $mouvP->origin = &$this;
656 $mouvP->setOrigin($this->element, $this->
id);
658 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderBackToDraftInDolibarr", $this->ref));
660 $error++; $this->error = $mouvP->error;
break;
668 $result = $this->
call_trigger(
'ORDER_UNVALIDATE', $user);
679 $this->
db->rollback();
683 $this->error = $this->
db->error();
684 $this->
db->rollback();
703 if ($this->statut != self::STATUS_CANCELED && $this->statut != self::STATUS_CLOSED) {
704 dol_syslog(get_class($this).
"::set_reopen order has not status closed", LOG_WARNING);
710 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
711 $sql .=
' SET fk_statut='.self::STATUS_VALIDATED.
', facture=0,';
712 $sql .=
" fk_user_modif = ".((int) $user->id);
713 $sql .=
" WHERE rowid = ".((int) $this->
id);
715 dol_syslog(get_class($this).
"::set_reopen", LOG_DEBUG);
726 $this->error = $this->
db->lasterror();
737 foreach ($this->errors as $errmsg) {
738 dol_syslog(get_class($this).
"::set_reopen ".$errmsg, LOG_ERR);
739 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
741 $this->
db->rollback();
753 public function cloture($user, $notrigger = 0)
759 $usercanclose = ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
760 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->close)));
763 if ($this->statut == self::STATUS_CLOSED) {
770 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
771 $sql .=
' SET fk_statut = '.self::STATUS_CLOSED.
',';
772 $sql .=
' fk_user_cloture = '.((int) $user->id).
',';
773 $sql .=
" date_cloture = '".$this->db->idate($now).
"',";
774 $sql .=
" fk_user_modif = ".((int) $user->id);
777 if ($this->
db->query($sql)) {
793 $this->
db->rollback();
797 $this->error = $this->
db->lasterror();
799 $this->
db->rollback();
813 public function cancel($idwarehouse = -1)
815 global $conf, $user, $langs;
821 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
822 $sql .=
" SET fk_statut = ".self::STATUS_CANCELED.
",";
823 $sql .=
" fk_user_modif = ".((int) $user->id);
824 $sql .=
" WHERE rowid = ".((int) $this->
id);
825 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
827 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
828 if ($this->
db->query($sql)) {
830 if (
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
831 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
832 $langs->load(
"agenda");
834 $num = count($this->lines);
835 for ($i = 0; $i < $num; $i++) {
836 if ($this->lines[$i]->fk_product > 0) {
838 $mouvP->setOrigin($this->element, $this->
id);
840 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderCanceledInDolibarr", $this->ref));
843 $this->error = $mouvP->error;
864 foreach ($this->errors as $errmsg) {
865 dol_syslog(get_class($this).
"::cancel ".$errmsg, LOG_ERR);
866 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
868 $this->
db->rollback();
872 $this->error = $this->
db->error();
873 $this->
db->rollback();
886 public function create($user, $notrigger = 0)
888 global $conf, $langs, $mysoc;
892 $this->brouillon = 1;
895 $date = ($this->date_commande ? $this->date_commande : $this->date);
896 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
899 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
904 if (empty($this->fk_multicurrency)) {
905 $this->multicurrency_code = $conf->currency;
906 $this->fk_multicurrency = 0;
907 $this->multicurrency_tx = 1;
910 dol_syslog(get_class($this).
"::create user=".$user->id);
913 if (!empty($this->
ref)) {
916 $this->error =
'ErrorRefAlreadyExists';
917 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
918 $this->
db->rollback();
924 $result = $soc->fetch($this->socid);
926 $this->error =
"Failed to fetch company";
927 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
930 if (!empty($conf->global->ORDER_REQUIRE_SOURCE) && $this->source < 0) {
931 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Source"));
932 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
940 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande (";
941 $sql .=
" ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client";
942 $sql .=
", model_pdf, fk_cond_reglement, deposit_percent, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address";
943 $sql .=
", fk_shipping_method";
944 $sql .=
", fk_warehouse";
945 $sql .=
", remise_absolue, remise_percent";
946 $sql .=
", fk_incoterms, location_incoterms";
947 $sql .=
", entity, module_source, pos_source";
948 $sql .=
", fk_multicurrency";
949 $sql .=
", multicurrency_code";
950 $sql .=
", multicurrency_tx";
952 $sql .=
" VALUES ('(PROV)', ".((int) $this->socid).
", '".$this->
db->idate($now).
"', ".((int) $user->id);
953 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
954 $sql .=
", '".$this->db->idate($date).
"'";
955 $sql .=
", ".($this->source >= 0 && $this->source !=
'' ? $this->
db->escape($this->source) :
'null');
956 $sql .=
", '".$this->db->escape($this->note_private).
"'";
957 $sql .=
", '".$this->db->escape($this->note_public).
"'";
958 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
959 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
960 $sql .=
", '".$this->db->escape($this->model_pdf).
"'";
961 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
962 $sql .=
", ".(!empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
"null");
963 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
964 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
965 $sql .=
", ".($this->availability_id > 0 ? ((int) $this->availability_id) :
"null");
966 $sql .=
", ".($this->demand_reason_id > 0 ? ((int) $this->demand_reason_id) :
"null");
967 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
968 $sql .=
", ".($this->fk_delivery_address > 0 ? ((int) $this->fk_delivery_address) :
'NULL');
969 $sql .=
", ".(!empty($this->shipping_method_id) && $this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) :
'NULL');
970 $sql .=
", ".(!empty($this->warehouse_id) && $this->warehouse_id > 0 ? ((int) $this->warehouse_id) :
'NULL');
971 $sql .=
", ".($this->remise_absolue > 0 ? $this->
db->escape($this->remise_absolue) :
'NULL');
972 $sql .=
", ".($this->remise_percent > 0 ? $this->
db->escape($this->remise_percent) : 0);
973 $sql .=
", ".(int) $this->fk_incoterms;
974 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
975 $sql .=
", ".setEntity($this);
976 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
977 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
978 $sql .=
", ".(int) $this->fk_multicurrency;
979 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
980 $sql .=
", ".(float) $this->multicurrency_tx;
983 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
986 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'commande');
990 $num = count($this->lines);
995 for ($i = 0; $i < $num; $i++) {
996 $line = $this->lines[$i];
1000 if (!is_object($line)) {
1001 $line = (object) $line;
1005 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1006 $fk_parent_line = 0;
1010 $vatrate = $line->tva_tx;
1011 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', $vatrate)) {
1012 $vatrate .=
' ('.$line->vat_src_code.
')';
1015 if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
1016 $originid = $line->origin_id;
1017 $origintype = $line->origin;
1019 $originid = $line->id;
1020 $origintype = $this->element;
1024 if (empty($line->ref_ext)) {
1025 $line->ref_ext =
'';
1033 $line->localtax1_tx,
1034 $line->localtax2_tx,
1036 $line->remise_percent,
1038 $line->fk_remise_except,
1043 $line->product_type,
1045 $line->special_code,
1047 $line->fk_fournprice,
1050 $line->array_options,
1059 if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) {
1060 $this->error = $this->
db->lasterror();
1061 $this->errors[] = $this->error;
1064 $this->
db->rollback();
1068 if ($result > 0 && $line->product_type == 9) {
1069 $fk_parent_line = $result;
1076 $initialref =
'(PROV'.$this->id.
')';
1077 if (!empty($this->
ref)) {
1078 $initialref = $this->ref;
1081 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"commande SET ref='".$this->
db->escape($initialref).
"' WHERE rowid=".((int) $this->
id);
1082 if ($this->
db->query($sql)) {
1083 $this->
ref = $initialref;
1085 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1086 $this->linked_objects = $this->linkedObjectsIds;
1090 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1091 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1092 if (is_array($tmp_origin_id)) {
1093 foreach ($tmp_origin_id as $origin_id) {
1096 $this->error = $this->
db->lasterror();
1102 $origin_id = $tmp_origin_id;
1105 $this->error = $this->
db->lasterror();
1112 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) {
1113 $originforcontact = $this->origin;
1114 $originidforcontact = $this->origin_id;
1115 if ($originforcontact ==
'shipping') {
1116 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1118 $exp->fetch($this->origin_id);
1119 $exp->fetchObjectLinked();
1120 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1121 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1122 $originforcontact =
'commande';
1123 if (is_object($value)) {
1124 $originidforcontact = $value->id;
1126 $originidforcontact = $value;
1133 $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";
1134 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->
db->escape($originforcontact).
"'";
1136 $resqlcontact = $this->
db->query($sqlcontact);
1137 if ($resqlcontact) {
1138 while ($objcontact = $this->
db->fetch_object($resqlcontact)) {
1140 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1154 if (!$error && !$notrigger) {
1164 $this->
db->commit();
1167 $this->
db->rollback();
1171 $this->error = $this->
db->lasterror();
1172 $this->
db->rollback();
1178 $this->
db->rollback();
1193 global $conf, $user, $hookmanager;
1200 foreach ($this->lines as $line) {
1201 $line->fetch_optionals();
1205 $objFrom = clone $this;
1208 if (!empty($socid) && $socid != $this->socid) {
1211 if ($objsoc->fetch($socid) > 0) {
1212 $this->socid = $objsoc->id;
1213 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1214 $this->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent :
null);
1215 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1216 $this->fk_project = 0;
1217 $this->fk_delivery_address = 0;
1228 $this->user_author_id = $user->id;
1229 $this->user_valid = 0;
1230 $this->user_validation_id = 0;
1232 $this->date_commande =
dol_now();
1233 $this->date_creation =
'';
1234 $this->date_validation =
'';
1235 if (empty($conf->global->MAIN_KEEP_REF_CUSTOMER_ON_CLONING)) {
1236 $this->ref_client =
'';
1240 $num = count($this->lines);
1241 for ($i = 0; $i < $num; $i++) {
1242 $this->lines[$i]->ref_ext =
'';
1246 $this->context[
'createfromclone'] =
'createfromclone';
1247 $result = $this->
create($user);
1261 if ($this->socid == $objFrom->socid) {
1270 if (is_object($hookmanager)) {
1271 $parameters = array(
'objFrom'=>$objFrom);
1273 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1275 $this->errors += $hookmanager->errors;
1276 $this->error = $hookmanager->error;
1282 unset($this->context[
'createfromclone']);
1286 $this->
db->commit();
1289 $this->
db->rollback();
1304 global $conf, $hookmanager;
1312 $this->date_commande =
dol_now();
1315 $num = count($object->lines);
1316 for ($i = 0; $i < $num; $i++) {
1319 $line->libelle = $object->lines[$i]->libelle;
1320 $line->label = $object->lines[$i]->label;
1321 $line->desc = $object->lines[$i]->desc;
1322 $line->price = $object->lines[$i]->price;
1323 $line->subprice = $object->lines[$i]->subprice;
1324 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1325 $line->tva_tx = $object->lines[$i]->tva_tx;
1326 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1327 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1328 $line->qty = $object->lines[$i]->qty;
1329 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1330 $line->remise_percent = $object->lines[$i]->remise_percent;
1331 $line->fk_product = $object->lines[$i]->fk_product;
1332 $line->info_bits = $object->lines[$i]->info_bits;
1333 $line->product_type = $object->lines[$i]->product_type;
1334 $line->rang = $object->lines[$i]->rang;
1335 $line->special_code = $object->lines[$i]->special_code;
1336 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1337 $line->fk_unit = $object->lines[$i]->fk_unit;
1339 $line->date_start = $object->lines[$i]->date_start;
1340 $line->date_end = $object->lines[$i]->date_end;
1342 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1343 $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);
1344 $line->pa_ht = $marginInfos[0];
1345 $line->marge_tx = $marginInfos[1];
1346 $line->marque_tx = $marginInfos[2];
1348 $line->origin = $object->element;
1349 $line->origin_id = $object->lines[$i]->id;
1352 $object->lines[$i]->fetch_optionals();
1353 foreach ($object->lines[$i]->array_options as $options_key => $value) {
1354 $line->array_options[$options_key] = $value;
1357 $this->lines[$i] = $line;
1360 $this->entity = $object->entity;
1361 $this->socid = $object->socid;
1362 $this->fk_project = $object->fk_project;
1363 $this->cond_reglement_id = $object->cond_reglement_id;
1364 $this->deposit_percent = $object->deposit_percent;
1365 $this->mode_reglement_id = $object->mode_reglement_id;
1366 $this->fk_account = $object->fk_account;
1367 $this->availability_id = $object->availability_id;
1368 $this->demand_reason_id = $object->demand_reason_id;
1369 $this->date_livraison = $object->date_livraison;
1370 $this->delivery_date = $object->date_livraison;
1371 $this->shipping_method_id = $object->shipping_method_id;
1372 $this->warehouse_id = $object->warehouse_id;
1373 $this->fk_delivery_address = $object->fk_delivery_address;
1374 $this->contact_id = $object->contact_id;
1375 $this->ref_client = $object->ref_client;
1377 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1378 $this->note_private = $object->note_private;
1379 $this->note_public = $object->note_public;
1382 $this->origin = $object->element;
1383 $this->origin_id = $object->id;
1386 if (!empty($conf->multicurrency->enabled)) {
1387 if (!empty($object->multicurrency_code)) {
1388 $this->multicurrency_code = $object->multicurrency_code;
1390 if (!empty($conf->global->MULTICURRENCY_USE_ORIGIN_TX) && !empty($object->multicurrency_tx)) {
1391 $this->multicurrency_tx = $object->multicurrency_tx;
1394 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1396 $this->fk_multicurrency = $tmparray[0];
1397 $this->multicurrency_tx = $tmparray[1];
1401 if (empty($this->fk_multicurrency)) {
1402 $this->multicurrency_code = $conf->currency;
1403 $this->fk_multicurrency = 0;
1404 $this->multicurrency_tx = 1;
1409 $object->fetch_optionals();
1412 $element_extrafields = $e->fetch_name_optionals_label($this->table_element);
1414 foreach ($object->array_options as $options_key => $value) {
1415 if (array_key_exists(str_replace(
'options_',
'', $options_key), $element_extrafields)) {
1416 $this->array_options[$options_key] = $value;
1420 $this->linked_objects[$this->origin] = $this->origin_id;
1421 if (isset($object->other_linked_objects) && is_array($object->other_linked_objects) && !empty($object->other_linked_objects)) {
1422 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1425 $ret = $this->
create($user);
1429 $hookmanager->initHooks(array(
'orderdao'));
1431 $parameters = array(
'objFrom'=>$object);
1433 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1435 $this->errors += $hookmanager->errors;
1436 $this->error = $hookmanager->error;
1442 if (!empty($conf->global->ORDER_VALID_AFTER_CLOSE_PROPAL)) {
1444 $this->
valid($user);
1496 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)
1498 global $mysoc, $conf, $langs, $user;
1500 $logtext =
"::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent";
1501 $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";
1502 $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";
1503 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
1505 if ($this->statut == self::STATUS_DRAFT) {
1506 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1510 if (empty($remise_percent)) {
1511 $remise_percent = 0;
1516 if (empty($info_bits)) {
1522 if (empty($txtva)) {
1525 if (empty($txlocaltax1)) {
1528 if (empty($txlocaltax2)) {
1531 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1532 $fk_parent_line = 0;
1534 if (empty($this->fk_multicurrency)) {
1535 $this->fk_multicurrency = 0;
1537 if (empty($ref_ext)) {
1541 $remise_percent =
price2num($remise_percent);
1544 $pu_ht_devise =
price2num($pu_ht_devise);
1547 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1552 if ($price_base_type ==
'HT') {
1557 $label = trim($label);
1558 $desc = trim($desc);
1565 if ($date_start && $date_end && $date_start > $date_end) {
1566 $langs->load(
"errors");
1567 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1573 $product_type = $type;
1574 if (!empty($fk_product) && $fk_product > 0) {
1576 $result = $product->fetch($fk_product);
1577 $product_type = $product->type;
1579 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) {
1580 $langs->load(
"errors");
1581 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
1582 $this->errors[] = $this->error;
1583 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
1584 $this->
db->rollback();
1598 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1599 $vat_src_code = $reg[1];
1600 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1603 $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);
1613 $total_ht = $tabprice[0];
1614 $total_tva = $tabprice[1];
1615 $total_ttc = $tabprice[2];
1616 $total_localtax1 = $tabprice[9];
1617 $total_localtax2 = $tabprice[10];
1618 $pu_ht = $tabprice[3];
1621 $multicurrency_total_ht = $tabprice[16];
1622 $multicurrency_total_tva = $tabprice[17];
1623 $multicurrency_total_ttc = $tabprice[18];
1624 $pu_ht_devise = $tabprice[19];
1628 if ($ranktouse == -1) {
1629 $rangmax = $this->
line_max($fk_parent_line);
1630 $ranktouse = $rangmax + 1;
1637 if ($remise_percent > 0) {
1638 $remise = round(($pu * $remise_percent / 100), 2);
1639 $price = $pu - $remise;
1645 $this->line->context = $this->context;
1647 $this->line->fk_commande = $this->id;
1648 $this->line->label = $label;
1649 $this->line->desc = $desc;
1650 $this->line->qty = $qty;
1651 $this->line->ref_ext = $ref_ext;
1653 $this->line->vat_src_code = $vat_src_code;
1654 $this->line->tva_tx = $txtva;
1655 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1656 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1657 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1658 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1659 $this->line->fk_product = $fk_product;
1660 $this->line->product_type = $product_type;
1661 $this->line->fk_remise_except = $fk_remise_except;
1662 $this->line->remise_percent = $remise_percent;
1663 $this->line->subprice = $pu_ht;
1664 $this->line->rang = $ranktouse;
1665 $this->line->info_bits = $info_bits;
1666 $this->line->total_ht = $total_ht;
1667 $this->line->total_tva = $total_tva;
1668 $this->line->total_localtax1 = $total_localtax1;
1669 $this->line->total_localtax2 = $total_localtax2;
1670 $this->line->total_ttc = $total_ttc;
1671 $this->line->special_code = $special_code;
1672 $this->line->origin = $origin;
1673 $this->line->origin_id = $origin_id;
1674 $this->line->fk_parent_line = $fk_parent_line;
1675 $this->line->fk_unit = $fk_unit;
1677 $this->line->date_start = $date_start;
1678 $this->line->date_end = $date_end;
1680 $this->line->fk_fournprice = $fk_fournprice;
1681 $this->line->pa_ht = $pa_ht;
1684 $this->line->fk_multicurrency = $this->fk_multicurrency;
1685 $this->line->multicurrency_code = $this->multicurrency_code;
1686 $this->line->multicurrency_subprice = $pu_ht_devise;
1687 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1688 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1689 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1692 $this->line->price = $price;
1694 if (is_array($array_options) && count($array_options) > 0) {
1695 $this->line->array_options = $array_options;
1698 $result = $this->line->insert($user);
1701 if (!empty($fk_parent_line)) {
1703 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
1704 $linecount = count($this->lines);
1705 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
1711 if (empty($noupdateafterinsertline)) {
1716 $this->
db->commit();
1717 return $this->line->id;
1719 $this->
db->rollback();
1723 $this->error = $this->line->error;
1724 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1725 $this->
db->rollback();
1729 dol_syslog(get_class($this).
"::addline status of order must be Draft to allow use of ->addline()", LOG_ERR);
1749 public function add_product($idproduct, $qty, $remise_percent = 0.0, $date_start =
'', $date_end =
'')
1752 global $conf, $mysoc;
1758 if ($idproduct > 0) {
1760 $prod->fetch($idproduct);
1764 if (empty($tva_tx)) {
1769 $localtax1_tx =
get_localtax($tva_tx, 1, $this->thirdparty, $mysoc, $tva_npr);
1770 $localtax2_tx =
get_localtax($tva_tx, 2, $this->thirdparty, $mysoc, $tva_npr);
1773 if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
1774 $price = $prod->multiprices[$this->thirdparty->price_level];
1776 $price = $prod->price;
1781 $line->context = $this->context;
1783 $line->fk_product = $idproduct;
1784 $line->desc = $prod->description;
1786 $line->subprice = $price;
1787 $line->remise_percent = $remise_percent;
1788 $line->vat_src_code = $vat_src_code;
1789 $line->tva_tx = $tva_tx;
1790 $line->localtax1_tx = $localtax1_tx;
1791 $line->localtax2_tx = $localtax2_tx;
1792 $line->ref = $prod->ref;
1793 $line->libelle = $prod->label;
1794 $line->product_desc = $prod->description;
1795 $line->fk_unit = $prod->fk_unit;
1799 $line->date_start = $date_start;
1802 $line->date_end = $date_end;
1805 $this->lines[] = $line;
1838 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
1841 if (empty($id) && empty($ref) && empty($ref_ext)) {
1845 $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';
1846 $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';
1847 $sql .=
', c.fk_account';
1848 $sql .=
', c.date_commande, c.date_valid, c.tms';
1849 $sql .=
', c.date_livraison as delivery_date';
1850 $sql .=
', c.fk_shipping_method';
1851 $sql .=
', c.fk_warehouse';
1852 $sql .=
', c.fk_projet as fk_project, c.remise_percent, c.remise, c.remise_absolue, c.source, c.facture as billed';
1853 $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';
1854 $sql .=
', c.fk_incoterms, c.location_incoterms';
1855 $sql .=
", c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc";
1856 $sql .=
", c.module_source, c.pos_source";
1857 $sql .=
", i.libelle as label_incoterms";
1858 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
1859 $sql .=
', cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle, cr.libelle_facture as cond_reglement_libelle_doc';
1860 $sql .=
', ca.code as availability_code, ca.label as availability_label';
1861 $sql .=
', dr.code as demand_reason_code';
1862 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
1863 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON c.fk_cond_reglement = cr.rowid';
1864 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON c.fk_mode_reglement = p.id';
1865 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON c.fk_availability = ca.rowid';
1866 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON c.fk_input_reason = dr.rowid';
1867 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
1870 $sql .=
" WHERE c.rowid=".((int) $id);
1872 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
1876 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
1879 $sql .=
" AND c.ref_ext='".$this->db->escape($ref_ext).
"'";
1882 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1883 $result = $this->
db->query($sql);
1885 $obj = $this->
db->fetch_object($result);
1887 $this->
id = $obj->rowid;
1888 $this->entity = $obj->entity;
1890 $this->
ref = $obj->ref;
1891 $this->ref_client = $obj->ref_client;
1892 $this->ref_customer = $obj->ref_client;
1893 $this->ref_ext = $obj->ref_ext;
1895 $this->socid = $obj->fk_soc;
1896 $this->thirdparty =
null;
1898 $this->fk_project = $obj->fk_project;
1899 $this->project =
null;
1901 $this->statut = $obj->fk_statut;
1902 $this->status = $obj->fk_statut;
1904 $this->user_author_id = $obj->fk_user_author;
1905 $this->user_creation_id = $obj->fk_user_author;
1906 $this->user_validation_id = $obj->fk_user_valid;
1907 $this->user_valid = $obj->fk_user_valid;
1908 $this->user_modification_id = $obj->fk_user_modif;
1909 $this->user_modification = $obj->fk_user_modif;
1910 $this->total_ht = $obj->total_ht;
1911 $this->total_tva = $obj->total_tva;
1912 $this->total_localtax1 = $obj->total_localtax1;
1913 $this->total_localtax2 = $obj->total_localtax2;
1914 $this->total_ttc = $obj->total_ttc;
1915 $this->date = $this->
db->jdate($obj->date_commande);
1916 $this->date_commande = $this->
db->jdate($obj->date_commande);
1917 $this->date_creation = $this->
db->jdate($obj->date_creation);
1918 $this->date_validation = $this->
db->jdate($obj->date_valid);
1919 $this->date_modification = $this->
db->jdate($obj->tms);
1920 $this->remise = $obj->remise;
1921 $this->remise_percent = $obj->remise_percent;
1922 $this->remise_absolue = $obj->remise_absolue;
1923 $this->source = $obj->source;
1924 $this->billed = $obj->billed;
1925 $this->note = $obj->note_private;
1926 $this->note_private = $obj->note_private;
1927 $this->note_public = $obj->note_public;
1928 $this->model_pdf = $obj->model_pdf;
1929 $this->modelpdf = $obj->model_pdf;
1930 $this->last_main_doc = $obj->last_main_doc;
1931 $this->mode_reglement_id = $obj->fk_mode_reglement;
1932 $this->mode_reglement_code = $obj->mode_reglement_code;
1933 $this->mode_reglement = $obj->mode_reglement_libelle;
1934 $this->cond_reglement_id = $obj->fk_cond_reglement;
1935 $this->cond_reglement_code = $obj->cond_reglement_code;
1936 $this->cond_reglement = $obj->cond_reglement_libelle;
1937 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1938 $this->deposit_percent = $obj->deposit_percent;
1939 $this->fk_account = $obj->fk_account;
1940 $this->availability_id = $obj->fk_availability;
1941 $this->availability_code = $obj->availability_code;
1943 $this->demand_reason_id = $obj->fk_input_reason;
1944 $this->demand_reason_code = $obj->demand_reason_code;
1945 $this->date_livraison = $this->
db->jdate($obj->delivery_date);
1946 $this->delivery_date = $this->
db->jdate($obj->delivery_date);
1947 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method :
null;
1948 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse :
null;
1949 $this->fk_delivery_address = $obj->fk_delivery_address;
1950 $this->module_source = $obj->module_source;
1951 $this->pos_source = $obj->pos_source;
1954 $this->fk_incoterms = $obj->fk_incoterms;
1955 $this->location_incoterms = $obj->location_incoterms;
1956 $this->label_incoterms = $obj->label_incoterms;
1959 $this->fk_multicurrency = $obj->fk_multicurrency;
1960 $this->multicurrency_code = $obj->multicurrency_code;
1961 $this->multicurrency_tx = $obj->multicurrency_tx;
1962 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1963 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1964 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1966 $this->extraparams = (array) json_decode($obj->extraparams,
true);
1968 $this->lines = array();
1970 if ($this->statut == self::STATUS_DRAFT) {
1971 $this->brouillon = 1;
1978 $this->
db->free($result);
1987 $this->error =
'Order with id '.$id.
' not found sql='.$sql;
1991 $this->error = $this->
db->error();
2009 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2010 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
2015 $result = $remise->fetch($idremise);
2018 if ($remise->fk_facture) {
2019 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2020 $this->
db->rollback();
2026 $line->fk_commande = $this->id;
2027 $line->fk_remise_except = $remise->id;
2028 $line->desc = $remise->description;
2029 $line->vat_src_code = $remise->vat_src_code;
2030 $line->tva_tx = $remise->tva_tx;
2031 $line->subprice = -$remise->amount_ht;
2032 $line->price = -$remise->amount_ht;
2033 $line->fk_product = 0;
2035 $line->remise_percent = 0;
2037 $line->info_bits = 2;
2039 $line->total_ht = -$remise->amount_ht;
2040 $line->total_tva = -$remise->amount_tva;
2041 $line->total_ttc = -$remise->amount_ttc;
2043 $result = $line->insert();
2047 $this->
db->commit();
2050 $this->
db->rollback();
2054 $this->error = $line->error;
2055 $this->errors = $line->errors;
2056 $this->
db->rollback();
2060 $this->
db->rollback();
2074 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2077 global $langs, $conf;
2079 $this->lines = array();
2081 $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,';
2082 $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,';
2083 $sql .=
' l.total_ht, l.total_ttc, l.total_tva, l.total_localtax1, l.total_localtax2, l.date_start, l.date_end,';
2084 $sql .=
' l.fk_unit,';
2085 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2086 $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,';
2087 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units';
2088 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as l';
2089 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (p.rowid = l.fk_product)';
2090 $sql .=
' WHERE l.fk_commande = '.((int) $this->
id);
2091 if ($only_product) {
2092 $sql .=
' AND p.fk_product_type = 0';
2094 $sql .=
' ORDER BY l.rang, l.rowid';
2096 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
2097 $result = $this->
db->query($sql);
2099 $num = $this->
db->num_rows($result);
2103 $objp = $this->
db->fetch_object($result);
2107 $line->rowid = $objp->rowid;
2108 $line->id = $objp->rowid;
2109 $line->fk_commande = $objp->fk_commande;
2110 $line->commande_id = $objp->fk_commande;
2111 $line->label = $objp->custom_label;
2112 $line->desc = $objp->description;
2113 $line->description = $objp->description;
2114 $line->product_type = $objp->product_type;
2115 $line->qty = $objp->qty;
2116 $line->ref_ext = $objp->ref_ext;
2118 $line->vat_src_code = $objp->vat_src_code;
2119 $line->tva_tx = $objp->tva_tx;
2120 $line->localtax1_tx = $objp->localtax1_tx;
2121 $line->localtax2_tx = $objp->localtax2_tx;
2122 $line->localtax1_type = $objp->localtax1_type;
2123 $line->localtax2_type = $objp->localtax2_type;
2124 $line->total_ht = $objp->total_ht;
2125 $line->total_ttc = $objp->total_ttc;
2126 $line->total_tva = $objp->total_tva;
2127 $line->total_localtax1 = $objp->total_localtax1;
2128 $line->total_localtax2 = $objp->total_localtax2;
2129 $line->subprice = $objp->subprice;
2130 $line->fk_remise_except = $objp->fk_remise_except;
2131 $line->remise_percent = $objp->remise_percent;
2132 $line->price = $objp->price;
2133 $line->fk_product = $objp->fk_product;
2134 $line->fk_fournprice = $objp->fk_fournprice;
2135 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2136 $line->pa_ht = $marginInfos[0];
2137 $line->marge_tx = $marginInfos[1];
2138 $line->marque_tx = $marginInfos[2];
2139 $line->rang = $objp->rang;
2140 $line->info_bits = $objp->info_bits;
2141 $line->special_code = $objp->special_code;
2142 $line->fk_parent_line = $objp->fk_parent_line;
2144 $line->ref = $objp->product_ref;
2145 $line->libelle = $objp->product_label;
2147 $line->product_ref = $objp->product_ref;
2148 $line->product_label = $objp->product_label;
2149 $line->product_tosell = $objp->product_tosell;
2150 $line->product_tobuy = $objp->product_tobuy;
2151 $line->product_desc = $objp->product_desc;
2152 $line->product_tobatch = $objp->product_tobatch;
2153 $line->product_barcode = $objp->product_barcode;
2155 $line->fk_product_type = $objp->fk_product_type;
2156 $line->fk_unit = $objp->fk_unit;
2158 $line->weight = $objp->weight;
2159 $line->weight_units = $objp->weight_units;
2160 $line->volume = $objp->volume;
2161 $line->volume_units = $objp->volume_units;
2163 $line->date_start = $this->
db->jdate($objp->date_start);
2164 $line->date_end = $this->
db->jdate($objp->date_end);
2167 $line->fk_multicurrency = $objp->fk_multicurrency;
2168 $line->multicurrency_code = $objp->multicurrency_code;
2169 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2170 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2171 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2172 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2174 $line->fetch_optionals();
2177 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2179 $tmpproduct->fetch($objp->fk_product);
2180 $tmpproduct->getMultiLangs();
2182 $line->multilangs = $tmpproduct->multilangs;
2185 $this->lines[$i] = $line;
2190 $this->
db->free($result);
2194 $this->error = $this->
db->error();
2208 foreach ($this->lines as $line) {
2209 if ($line->product_type == 0) {
2224 foreach ($this->lines as $line) {
2225 if ($line->product_type == 1) {
2241 $sql =
'SELECT COUNT(DISTINCT ed.fk_expedition) as nb';
2242 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2243 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2245 $sql .=
' ed.fk_origin_line = cd.rowid';
2246 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2249 dol_syslog(get_class($this).
"::getNbOfShipments", LOG_DEBUG);
2252 $obj = $this->
db->fetch_object(
$resql);
2260 $this->error = $this->
db->lasterror();
2275 $this->expeditions = array();
2277 $sql =
'SELECT cd.rowid, cd.fk_product,';
2278 $sql .=
' sum(ed.qty) as qty';
2279 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2280 if ($filtre_statut >= 0) {
2281 $sql .=
' '.MAIN_DB_PREFIX.
'expedition as e,';
2283 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2285 if ($filtre_statut >= 0) {
2286 $sql .=
' ed.fk_expedition = e.rowid AND';
2288 $sql .=
' ed.fk_origin_line = cd.rowid';
2289 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2290 if ($fk_product > 0) {
2291 $sql .=
' AND cd.fk_product = '.((int) $fk_product);
2293 if ($filtre_statut >= 0) {
2294 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
2296 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
2299 dol_syslog(get_class($this).
"::loadExpeditions", LOG_DEBUG);
2302 $num = $this->
db->num_rows(
$resql);
2305 $obj = $this->
db->fetch_object(
$resql);
2306 $this->expeditions[$obj->rowid] = $obj->qty;
2312 $this->error = $this->
db->lasterror();
2328 $sql =
'SELECT count(*)';
2329 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expedition as e';
2330 $sql .=
', '.MAIN_DB_PREFIX.
'element_element as el';
2331 $sql .=
' WHERE el.fk_source = '.((int) $this->
id);
2332 $sql .=
" AND el.sourcetype = 'commande'";
2333 $sql .=
" AND el.fk_target = e.rowid";
2334 $sql .=
" AND el.targettype = 'shipping'";
2338 $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';
2371 $num = $this->
db->num_rows(
$resql);
2374 $obj = $this->
db->fetch_object(
$resql);
2375 $this->stocks[$obj->fk_product] = $obj->total;
2393 if ($this->statut == self::STATUS_DRAFT) {
2396 $sql =
"SELECT fk_product, qty";
2397 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet";
2398 $sql .=
" WHERE rowid = ".((int) $lineid);
2400 $result = $this->
db->query($sql);
2402 $obj = $this->
db->fetch_object($result);
2406 $product->id = $obj->fk_product;
2412 $line->fetch($lineid);
2415 $staticline = clone $line;
2416 $line->oldline = $staticline;
2418 if ($line->delete($user) > 0) {
2422 $this->
db->commit();
2425 $this->
db->rollback();
2426 $this->error = $this->
db->lasterror();
2430 $this->
db->rollback();
2431 $this->error = $line->error;
2435 $this->
db->rollback();
2439 $this->
db->rollback();
2440 $this->error = $this->
db->lasterror();
2444 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
2463 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
2464 return $this->
setDiscount($user, $remise, $notrigger);
2477 $remise = trim($remise) ?trim($remise) : 0;
2479 if ($user->rights->commande->creer) {
2486 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2487 $sql .=
' SET remise_percent = '.((float) $remise);
2488 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.((int) self::STATUS_DRAFT);
2493 $this->errors[] = $this->
db->error();
2498 $this->oldcopy = clone $this;
2499 $this->remise_percent = $remise;
2503 if (!$notrigger && empty($error)) {
2513 $this->
db->commit();
2516 foreach ($this->errors as $errmsg) {
2517 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2518 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2520 $this->
db->rollback();
2539 if (empty($remise)) {
2545 if ($user->rights->commande->creer) {
2550 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2551 $sql .=
' SET remise_absolue = '.((float) $remise);
2557 $this->errors[] = $this->
db->error();
2562 $this->oldcopy = clone $this;
2563 $this->remise_absolue = $remise;
2567 if (!$notrigger && empty($error)) {
2577 $this->
db->commit();
2580 foreach ($this->errors as $errmsg) {
2581 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2582 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2584 $this->
db->rollback();
2603 if ($user->rights->commande->creer) {
2608 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2609 $sql .=
" SET date_commande = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
2610 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut = ".((int) self::STATUS_DRAFT);
2615 $this->errors[] = $this->
db->error();
2620 $this->oldcopy = clone $this;
2621 $this->date = $date;
2624 if (!$notrigger && empty($error)) {
2634 $this->
db->commit();
2637 foreach ($this->errors as $errmsg) {
2638 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2639 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2641 $this->
db->rollback();
2675 if ($user->rights->commande->creer) {
2680 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2681 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2682 $sql .=
" WHERE rowid = ".((int) $this->
id);
2687 $this->errors[] = $this->
db->error();
2692 $this->oldcopy = clone $this;
2693 $this->date_livraison = $delivery_date;
2694 $this->delivery_date = $delivery_date;
2697 if (!$notrigger && empty($error)) {
2707 $this->
db->commit();
2710 foreach ($this->errors as $errmsg) {
2711 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2712 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2714 $this->
db->rollback();
2736 public function liste_array($shortlist = 0, $draft = 0, $excluser =
'', $socid = 0, $limit = 0, $offset = 0, $sortfield =
'c.date_commande', $sortorder =
'DESC')
2743 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2744 $sql .=
" c.rowid as cid, c.ref";
2745 if (empty($user->rights->societe->client->voir) && !$socid) {
2746 $sql .=
", sc.fk_soc, sc.fk_user";
2748 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"commande as c";
2749 if (empty($user->rights->societe->client->voir) && !$socid) {
2750 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2752 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2753 $sql .=
" AND c.fk_soc = s.rowid";
2754 if (empty($user->rights->societe->client->voir) && !$socid) {
2755 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2758 $sql .=
" AND s.rowid = ".((int) $socid);
2761 $sql .=
" AND c.fk_statut = ".self::STATUS_DRAFT;
2763 if (is_object($excluser)) {
2764 $sql .=
" AND c.fk_user_author <> ".((int) $excluser->id);
2766 $sql .= $this->
db->order($sortfield, $sortorder);
2767 $sql .= $this->
db->plimit($limit, $offset);
2769 $result = $this->
db->query($sql);
2771 $numc = $this->
db->num_rows($result);
2774 while ($i < $numc) {
2775 $obj = $this->
db->fetch_object($result);
2777 if ($shortlist == 1) {
2778 $ga[$obj->cid] = $obj->ref;
2779 } elseif ($shortlist == 2) {
2780 $ga[$obj->cid] = $obj->ref.
' ('.$obj->name.
')';
2782 $ga[$i][
'id'] = $obj->cid;
2783 $ga[$i][
'ref'] = $obj->ref;
2784 $ga[$i][
'name'] = $obj->name;
2807 dol_syslog(
'Commande::availability('.$availability_id.
')');
2808 if ($this->statut >= self::STATUS_DRAFT) {
2813 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2814 $sql .=
' SET fk_availability = '.((int) $availability_id);
2815 $sql .=
' WHERE rowid='.((int) $this->
id);
2820 $this->errors[] = $this->
db->error();
2825 $this->oldcopy = clone $this;
2826 $this->availability_id = $availability_id;
2829 if (!$notrigger && empty($error)) {
2839 $this->
db->commit();
2842 foreach ($this->errors as $errmsg) {
2843 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2844 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2846 $this->
db->rollback();
2850 $error_str =
'Command status do not meet requirement '.$this->statut;
2852 $this->error = $error_str;
2853 $this->errors[] = $this->error;
2871 dol_syslog(
'Commande::demand_reason('.$demand_reason_id.
')');
2872 if ($this->statut >= self::STATUS_DRAFT) {
2877 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2878 $sql .=
' SET fk_input_reason = '.((int) $demand_reason_id);
2879 $sql .=
' WHERE rowid='.((int) $this->
id);
2884 $this->errors[] = $this->
db->error();
2889 $this->oldcopy = clone $this;
2890 $this->demand_reason_id = $demand_reason_id;
2893 if (!$notrigger && empty($error)) {
2903 $this->
db->commit();
2906 foreach ($this->errors as $errmsg) {
2907 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2908 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2910 $this->
db->rollback();
2914 $error_str =
'order status do not meet requirement '.$this->statut;
2916 $this->error = $error_str;
2917 $this->errors[] = $this->error;
2934 if ($user->rights->commande->creer) {
2939 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET';
2940 $sql .=
' ref_client = '.(empty($ref_client) ?
'NULL' :
"'".$this->db->escape($ref_client).
"'");
2941 $sql .=
' WHERE rowid = '.((int) $this->
id);
2943 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2946 $this->errors[] = $this->
db->error();
2951 $this->oldcopy = clone $this;
2952 $this->ref_client = $ref_client;
2955 if (!$notrigger && empty($error)) {
2964 $this->
db->commit();
2967 foreach ($this->errors as $errmsg) {
2968 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2969 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2971 $this->
db->rollback();
2990 if ($this->billed) {
2996 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 1';
2999 dol_syslog(get_class($this).
"::classifyBilled", LOG_DEBUG);
3000 if ($this->
db->query($sql)) {
3002 $this->oldcopy = clone $this;
3006 if (!$notrigger && empty($error)) {
3008 $result = $this->
call_trigger(
'ORDER_CLASSIFY_BILLED', $user);
3016 $this->
db->commit();
3019 foreach ($this->errors as $errmsg) {
3020 dol_syslog(get_class($this).
"::classifyBilled ".$errmsg, LOG_ERR);
3021 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3023 $this->
db->rollback();
3027 $this->error = $this->
db->error();
3028 $this->
db->rollback();
3046 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 0';
3049 dol_syslog(get_class($this).
"::classifyUnBilled", LOG_DEBUG);
3050 if ($this->
db->query($sql)) {
3052 $this->oldcopy = clone $this;
3056 if (!$notrigger && empty($error)) {
3058 $result = $this->
call_trigger(
'ORDER_CLASSIFY_UNBILLED', $user);
3068 $this->
db->commit();
3071 foreach ($this->errors as $errmsg) {
3072 dol_syslog(get_class($this).
"::classifyUnBilled ".$errmsg, LOG_ERR);
3073 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3075 $this->
db->rollback();
3079 $this->error = $this->
db->error();
3080 $this->
db->rollback();
3116 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)
3118 global $conf, $mysoc, $langs, $user;
3120 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");
3121 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3128 if (empty($info_bits)) {
3131 if (empty($txtva)) {
3134 if (empty($txlocaltax1)) {
3137 if (empty($txlocaltax2)) {
3140 if (empty($remise_percent)) {
3141 $remise_percent = 0;
3143 if (empty($special_code) || $special_code == 3) {
3146 if (empty($ref_ext)) {
3150 if ($date_start && $date_end && $date_start > $date_end) {
3151 $langs->load(
"errors");
3152 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3156 $remise_percent =
price2num($remise_percent);
3160 $pu_ht_devise =
price2num($pu_ht_devise);
3161 if (!preg_match(
'/\((.*)\)/', $txtva)) {
3179 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3180 $vat_src_code = $reg[1];
3181 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3184 $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);
3186 $total_ht = $tabprice[0];
3187 $total_tva = $tabprice[1];
3188 $total_ttc = $tabprice[2];
3189 $total_localtax1 = $tabprice[9];
3190 $total_localtax2 = $tabprice[10];
3191 $pu_ht = $tabprice[3];
3192 $pu_tva = $tabprice[4];
3193 $pu_ttc = $tabprice[5];
3196 $multicurrency_total_ht = $tabprice[16];
3197 $multicurrency_total_tva = $tabprice[17];
3198 $multicurrency_total_ttc = $tabprice[18];
3199 $pu_ht_devise = $tabprice[19];
3203 if ($price_base_type ==
'TTC') {
3204 $subprice = $pu_ttc;
3209 if ($remise_percent > 0) {
3210 $remise = round(($pu * $remise_percent / 100), 2);
3211 $price = ($pu - $remise);
3216 $line->fetch($rowid);
3217 $line->fetch_optionals();
3219 if (!empty($line->fk_product)) {
3221 $result = $product->fetch($line->fk_product);
3222 $product_type = $product->type;
3224 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) {
3225 $langs->load(
"errors");
3226 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
3227 $this->errors[] = $this->error;
3229 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
3231 $this->
db->rollback();
3236 $staticline = clone $line;
3238 $line->oldline = $staticline;
3239 $this->line = $line;
3240 $this->line->context = $this->context;
3241 $this->line->rang = $rang;
3244 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
3245 $rangmax = $this->
line_max($fk_parent_line);
3246 $this->line->rang = $rangmax + 1;
3249 $this->line->id = $rowid;
3250 $this->line->label = $label;
3251 $this->line->desc = $desc;
3252 $this->line->qty = $qty;
3253 $this->line->ref_ext = $ref_ext;
3255 $this->line->vat_src_code = $vat_src_code;
3256 $this->line->tva_tx = $txtva;
3257 $this->line->localtax1_tx = $txlocaltax1;
3258 $this->line->localtax2_tx = $txlocaltax2;
3259 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3260 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3261 $this->line->remise_percent = $remise_percent;
3262 $this->line->subprice = $subprice;
3263 $this->line->info_bits = $info_bits;
3264 $this->line->special_code = $special_code;
3265 $this->line->total_ht = $total_ht;
3266 $this->line->total_tva = $total_tva;
3267 $this->line->total_localtax1 = $total_localtax1;
3268 $this->line->total_localtax2 = $total_localtax2;
3269 $this->line->total_ttc = $total_ttc;
3270 $this->line->date_start = $date_start;
3271 $this->line->date_end = $date_end;
3272 $this->line->product_type = $type;
3273 $this->line->fk_parent_line = $fk_parent_line;
3274 $this->line->skip_update_total = $skip_update_total;
3275 $this->line->fk_unit = $fk_unit;
3277 $this->line->fk_fournprice = $fk_fournprice;
3278 $this->line->pa_ht = $pa_ht;
3281 $this->line->multicurrency_subprice = $pu_ht_devise;
3282 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
3283 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
3284 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
3287 $this->line->price = $price;
3289 if (is_array($array_options) && count($array_options) > 0) {
3291 foreach ($array_options as $key => $value) {
3292 $this->line->array_options[$key] = $array_options[$key];
3296 $result = $this->line->update($user, $notrigger);
3299 if (!empty($fk_parent_line)) {
3306 $this->
db->commit();
3309 $this->error = $this->line->error;
3311 $this->
db->rollback();
3315 $this->error = get_class($this).
"::updateline Order status makes operation forbidden";
3316 $this->errors = array(
'OrderStatusMakeOperationForbidden');
3335 if (isset($this->
ref)) {
3336 $this->
ref = trim($this->
ref);
3338 if (isset($this->ref_client)) {
3339 $this->ref_client = trim($this->ref_client);
3341 if (isset($this->note) || isset($this->note_private)) {
3342 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
3344 if (isset($this->note_public)) {
3345 $this->note_public = trim($this->note_public);
3347 if (isset($this->model_pdf)) {
3348 $this->model_pdf = trim($this->model_pdf);
3350 if (isset($this->import_key)) {
3351 $this->import_key = trim($this->import_key);
3353 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
3359 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande SET";
3361 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
3362 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
3363 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
3364 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
3365 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
3366 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
3367 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
3368 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
3369 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
3370 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
3371 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
3372 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
3373 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id :
"null").
",";
3374 $sql .=
" fk_user_valid=".((isset($this->user_valid) && $this->user_valid > 0) ? $this->user_valid :
"null").
",";
3375 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
3376 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
3377 $sql .=
" deposit_percent=".(!empty($this->deposit_percent) ? strval($this->deposit_percent) :
"null").
",";
3378 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
3379 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
3380 $sql .=
" fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id :
"null").
",";
3381 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
3382 $sql .=
" fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id :
"null").
",";
3383 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
3384 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
3385 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
3386 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
"";
3388 $sql .=
" WHERE rowid=".((int) $this->
id);
3392 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3395 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
3405 if (!$error && !$notrigger) {
3416 foreach ($this->errors as $errmsg) {
3417 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3418 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3420 $this->
db->rollback();
3423 $this->
db->commit();
3435 public function delete($user, $notrigger = 0)
3437 global $conf, $langs;
3438 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3442 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
3457 $this->errors[] = $langs->trans(
'SomeShipmentExists');
3462 if (!$error && !empty($this->table_element_line)) {
3463 $tabletodelete = $this->table_element_line;
3464 $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).
")";
3465 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3466 if (!$this->
db->query($sqlef) || !$this->
db->query($sql)) {
3468 $this->error = $this->
db->lasterror();
3469 $this->errors[] = $this->error;
3470 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3495 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3501 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
3502 $res = $this->
db->query($sql);
3505 $this->error = $this->
db->lasterror();
3506 $this->errors[] = $this->error;
3507 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3522 if ($conf->commande->multidir_output[$this->entity] && !empty($this->
ref)) {
3523 $dir = $conf->commande->multidir_output[$this->entity].
"/".$ref;
3524 $file = $dir.
"/".$ref.
".pdf";
3525 if (file_exists($file)) {
3529 $this->error =
'ErrorFailToDeleteFile';
3530 $this->errors[] = $this->error;
3531 $this->
db->rollback();
3535 if (file_exists($dir)) {
3538 $this->error =
'ErrorFailToDeleteDir';
3539 $this->errors[] = $this->error;
3540 $this->
db->rollback();
3548 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3549 $this->
db->commit();
3552 $this->
db->rollback();
3568 global $conf, $langs;
3572 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.date_livraison as delivery_date, c.fk_statut, c.total_ht";
3573 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as c";
3574 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3575 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
3576 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3579 $sql .= $clause.
" c.entity IN (".
getEntity(
'commande').
")";
3581 $sql .=
" AND ((c.fk_statut IN (".self::STATUS_VALIDATED.
",".self::STATUS_SHIPMENTONPROCESS.
")) OR (c.fk_statut = ".self::STATUS_CLOSED.
" AND c.facture = 0))";
3583 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3589 $response->warning_delay = $conf->commande->client->warning_delay / 60 / 60 / 24;
3590 $response->label = $langs->trans(
"OrdersToProcess");
3591 $response->labelShort = $langs->trans(
"Opened");
3592 $response->url = DOL_URL_ROOT.
'/commande/list.php?search_status=-3&mainmenu=commercial&leftmenu=orders';
3597 while ($obj = $this->
db->fetch_object(
$resql)) {
3598 $response->nbtodo++;
3599 $response->total += $obj->total_ht;
3601 $generic_commande->statut = $obj->fk_statut;
3602 $generic_commande->date_commande = $this->
db->jdate($obj->date_commande);
3603 $generic_commande->date = $this->
db->jdate($obj->date_commande);
3604 $generic_commande->date_livraison = $this->
db->jdate($obj->delivery_date);
3605 $generic_commande->delivery_date = $this->
db->jdate($obj->delivery_date);
3607 if ($generic_commande->hasDelay()) {
3608 $response->nbtodolate++;
3614 $this->error = $this->
db->error();
3628 $label = $langs->trans(
'OrderSource'.$this->source);
3630 if ($label ==
'OrderSource') {
3644 return $this->
LibStatut($this->statut, $this->billed, $mode);
3657 public function LibStatut($status, $billed, $mode, $donotshowbilled = 0)
3660 global $langs, $conf, $hookmanager;
3663 if (empty($donotshowbilled)) {
3664 $billedtext .= ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3669 if ($status == self::STATUS_CANCELED) {
3670 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderCanceled');
3671 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderCanceledShort');
3672 $statusType =
'status9';
3673 } elseif ($status == self::STATUS_DRAFT) {
3674 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDraft');
3675 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDraftShort');
3676 $statusType =
'status0';
3677 } elseif ($status == self::STATUS_VALIDATED) {
3678 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderValidated').$billedtext;
3679 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderValidatedShort').$billedtext;
3680 $statusType =
'status1';
3681 } elseif ($status == self::STATUS_SHIPMENTONPROCESS) {
3682 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderSent').$billedtext;
3683 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderSentShort').$billedtext;
3684 $labelTooltip = $langs->transnoentitiesnoconv(
"StatusOrderSent");
3685 if (!empty($this->delivery_date)) {
3686 $labelTooltip .=
' - '.$langs->transnoentitiesnoconv(
"DateDeliveryPlanned").dol_print_date($this->delivery_date,
'day').$billedtext;
3688 $statusType =
'status4';
3689 } elseif ($status == self::STATUS_CLOSED && (!$billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3690 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderToBill');
3691 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderToBillShort');
3692 $statusType =
'status4';
3693 } elseif ($status == self::STATUS_CLOSED && ($billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3694 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderProcessed').$billedtext;
3695 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderProcessedShort').$billedtext;
3696 $statusType =
'status6';
3697 } elseif ($status == self::STATUS_CLOSED && (!empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3698 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDelivered');
3699 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDeliveredShort');
3700 $statusType =
'status6';
3702 $labelStatus = $langs->transnoentitiesnoconv(
'Unknown');
3703 $labelStatusShort =
'';
3708 $parameters = array(
3709 'status' => $status,
3711 'billed' => $billed,
3712 'donotshowbilled' => $donotshowbilled
3715 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
3718 return $hookmanager->resPrint;
3721 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', array(
'tooltip' => $labelTooltip));
3738 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0, $target =
'')
3740 global $conf, $langs, $user, $hookmanager;
3742 if (!empty($conf->dol_no_mouse_hover)) {
3748 if (
isModEnabled(
"expedition") && ($option ==
'1' || $option ==
'2')) {
3749 $url = DOL_URL_ROOT.
'/expedition/shipment.php?id='.$this->id;
3751 $url = DOL_URL_ROOT.
'/commande/card.php?id='.$this->id;
3754 if (!$user->rights->commande->lire) {
3758 if ($option !==
'nolink') {
3760 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3761 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
3762 $add_save_lastsearch_values = 1;
3764 if ($add_save_lastsearch_values) {
3765 $url .=
'&save_lastsearch_values=1';
3775 if ($user->rights->commande->lire) {
3776 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Order").
'</u>';
3777 if (isset($this->statut)) {
3778 $label .=
' '.$this->getLibStatut(5);
3780 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
3781 $label .=
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.(empty($this->ref_customer) ? (empty($this->ref_client) ?
'' : $this->ref_client) : $this->ref_customer);
3782 if (!empty($this->total_ht)) {
3783 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
3785 if (!empty($this->total_tva)) {
3786 $label .=
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
3788 if (!empty($this->total_ttc)) {
3789 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
3791 if (!empty($this->date)) {
3792 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
3794 if (!empty($this->delivery_date)) {
3795 $label .=
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
3800 if (empty($notooltip) && $user->rights->commande->lire) {
3801 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
3802 $label = $langs->trans(
"Order");
3803 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
3805 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
3806 $linkclose .=
' class="classfortooltip"';
3808 $target_value=array(
'_self',
'_blank',
'_parent',
'_top');
3809 if (in_array($target, $target_value)) {
3810 $linkclose .=
' target="'.dol_escape_htmltag($target).
'"';
3814 $linkstart =
'<a href="'.$url.
'"';
3815 $linkstart .= $linkclose.
'>';
3818 if ($option ===
'nolink') {
3823 $result .= $linkstart;
3825 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
3827 if ($withpicto != 2) {
3828 $result .= $this->ref;
3830 $result .= $linkend;
3832 if ($addlinktonotes) {
3833 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3836 $result .=
' <span class="note inline-block">';
3837 $result .=
'<a href="'.DOL_URL_ROOT.
'/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3842 $result .=
'</span>';
3847 $hookmanager->initHooks(array($this->element .
'dao'));
3848 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
3849 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3851 $result = $hookmanager->resPrint;
3853 $result .= $hookmanager->resPrint;
3867 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem,';
3868 $sql .=
' date_valid as datev,';
3869 $sql .=
' date_cloture as datecloture,';
3870 $sql .=
' fk_user_author, fk_user_valid, fk_user_cloture';
3871 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
3872 $sql .=
' WHERE c.rowid = '.((int) $id);
3873 $result = $this->
db->query($sql);
3875 if ($this->
db->num_rows($result)) {
3876 $obj = $this->
db->fetch_object($result);
3877 $this->
id = $obj->rowid;
3878 if ($obj->fk_user_author) {
3879 $this->user_creation_id = $obj->fk_user_author;
3881 if ($obj->fk_user_valid) {
3882 $this->user_validation_id = $obj->fk_user_valid;
3884 if ($obj->fk_user_cloture) {
3885 $this->user_closing_id = $obj->fk_user_cloture;
3888 $this->date_creation = $this->
db->jdate($obj->datec);
3889 $this->date_modification = $this->
db->jdate($obj->datem);
3890 $this->date_validation = $this->
db->jdate($obj->datev);
3891 $this->date_cloture = $this->
db->jdate($obj->datecloture);
3894 $this->
db->free($result);
3910 global $conf, $langs;
3912 dol_syslog(get_class($this).
"::initAsSpecimen");
3917 $sql =
"SELECT rowid";
3918 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
3919 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3920 $sql .= $this->
db->plimit(100);
3924 $num_prods = $this->
db->num_rows(
$resql);
3926 while ($i < $num_prods) {
3928 $row = $this->
db->fetch_row(
$resql);
3929 $prodids[$i] = $row[0];
3935 $this->
ref =
'SPECIMEN';
3936 $this->specimen = 1;
3938 $this->date = time();
3939 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
3940 $this->cond_reglement_code =
'RECEP';
3941 $this->mode_reglement_code =
'CHQ';
3942 $this->availability_code =
'DSP';
3943 $this->demand_reason_code =
'SRC_00';
3945 $this->note_public =
'This is a comment (public)';
3946 $this->note_private =
'This is a comment (private)';
3948 $this->multicurrency_tx = 1;
3949 $this->multicurrency_code = $conf->currency;
3954 while ($xnbp < $nbp) {
3957 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
3959 $line->subprice = 100;
3963 $line->total_ht = 50;
3964 $line->total_ttc = 60;
3965 $line->total_tva = 10;
3966 $line->remise_percent = 50;
3968 $line->total_ht = 100;
3969 $line->total_ttc = 120;
3970 $line->total_tva = 20;
3971 $line->remise_percent = 0;
3973 if ($num_prods > 0) {
3974 $prodid = mt_rand(1, $num_prods);
3975 $line->fk_product = $prodids[$prodid];
3976 $line->product_ref =
'SPECIMEN';
3979 $this->lines[$xnbp] = $line;
3981 $this->total_ht += $line->total_ht;
3982 $this->total_tva += $line->total_tva;
3983 $this->total_ttc += $line->total_ttc;
4001 $this->nb = array();
4004 $sql =
"SELECT count(co.rowid) as nb";
4005 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as co";
4006 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
4007 if (empty($user->rights->societe->client->voir) && !$user->socid) {
4008 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
4009 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
4012 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'commande').
")";
4016 while ($obj = $this->
db->fetch_object(
$resql)) {
4017 $this->nb[
"orders"] = $obj->nb;
4023 $this->error = $this->
db->error();
4049 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
4051 global $conf, $langs;
4053 $langs->load(
"orders");
4054 $outputlangs->load(
"products");
4057 $modele =
'einstein';
4059 if (!empty($this->model_pdf)) {
4060 $modele = $this->model_pdf;
4061 } elseif (!empty($this->modelpdf)) {
4062 $modele = $this->modelpdf;
4063 } elseif (!empty($conf->global->COMMANDE_ADDON_PDF)) {
4064 $modele = $conf->global->COMMANDE_ADDON_PDF;
4068 $modelpath =
"core/modules/commande/doc/";
4070 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
4123 return max($this->date, $this->delivery_date) < ($now - $conf->commande->client->warning_delay);
4133 global $conf, $langs;
4135 if (empty($this->delivery_date)) {
4136 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
4138 $text = $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->date_livraison,
'day');
4140 $text .=
' '.($conf->commande->client->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->client->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
4155 public $element =
'commandedet';
4157 public $table_element =
'commandedet';
4165 public $fk_commande;
4173 public $commande_id;
4175 public $fk_parent_line;
4187 public $fk_remise_except;
4193 public $fk_fournprice;
4213 public $skip_update_total;
4234 $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,';
4235 $sql .=
' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
4236 $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,';
4237 $sql .=
' cd.fk_unit,';
4238 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
4239 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
4240 $sql .=
' cd.date_start, cd.date_end, cd.vat_src_code';
4241 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as cd';
4242 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
4243 $sql .=
' WHERE cd.rowid = '.((int) $rowid);
4244 $result = $this->
db->query($sql);
4246 $objp = $this->
db->fetch_object($result);
4249 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.$sql;
4253 $this->
rowid = $objp->rowid;
4254 $this->
id = $objp->rowid;
4255 $this->fk_commande = $objp->fk_commande;
4256 $this->fk_parent_line = $objp->fk_parent_line;
4257 $this->label = $objp->custom_label;
4258 $this->desc = $objp->description;
4259 $this->qty = $objp->qty;
4260 $this->
price = $objp->price;
4261 $this->subprice = $objp->subprice;
4262 $this->ref_ext = $objp->ref_ext;
4263 $this->vat_src_code = $objp->vat_src_code;
4264 $this->tva_tx = $objp->tva_tx;
4265 $this->localtax1_tx = $objp->localtax1_tx;
4266 $this->localtax2_tx = $objp->localtax2_tx;
4267 $this->remise = $objp->remise;
4268 $this->remise_percent = $objp->remise_percent;
4269 $this->fk_remise_except = $objp->fk_remise_except;
4270 $this->fk_product = $objp->fk_product;
4271 $this->product_type = $objp->product_type;
4272 $this->info_bits = $objp->info_bits;
4273 $this->special_code = $objp->special_code;
4274 $this->total_ht = $objp->total_ht;
4275 $this->total_tva = $objp->total_tva;
4276 $this->total_localtax1 = $objp->total_localtax1;
4277 $this->total_localtax2 = $objp->total_localtax2;
4278 $this->total_ttc = $objp->total_ttc;
4279 $this->fk_fournprice = $objp->fk_fournprice;
4280 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
4281 $this->pa_ht = $marginInfos[0];
4282 $this->marge_tx = $marginInfos[1];
4283 $this->marque_tx = $marginInfos[2];
4284 $this->special_code = $objp->special_code;
4285 $this->rang = $objp->rang;
4287 $this->
ref = $objp->product_ref;
4289 $this->product_ref = $objp->product_ref;
4290 $this->product_label = $objp->product_label;
4291 $this->product_desc = $objp->product_desc;
4292 $this->product_tobatch = $objp->product_tobatch;
4293 $this->fk_unit = $objp->fk_unit;
4295 $this->date_start = $this->
db->jdate($objp->date_start);
4296 $this->date_end = $this->
db->jdate($objp->date_end);
4298 $this->fk_multicurrency = $objp->fk_multicurrency;
4299 $this->multicurrency_code = $objp->multicurrency_code;
4300 $this->multicurrency_subprice = $objp->multicurrency_subprice;
4301 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
4302 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
4303 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
4305 $this->
db->free($result);
4309 $this->error = $this->
db->lasterror();
4321 public function delete(
User $user, $notrigger = 0)
4323 global $conf, $langs;
4327 if (empty($this->
id) && !empty($this->
rowid)) {
4328 $this->
id = $this->rowid;
4332 $sqlCheckShipmentLine =
"SELECT";
4333 $sqlCheckShipmentLine .=
" ed.rowid";
4334 $sqlCheckShipmentLine .=
" FROM ".MAIN_DB_PREFIX.
"expeditiondet ed";
4335 $sqlCheckShipmentLine .=
" WHERE ed.fk_origin_line = ".((int) $this->
id);
4337 $resqlCheckShipmentLine = $this->
db->query($sqlCheckShipmentLine);
4338 if (!$resqlCheckShipmentLine) {
4340 $this->error = $this->
db->lasterror();
4341 $this->errors[] = $this->error;
4343 $langs->load(
'errors');
4344 $num = $this->
db->num_rows($resqlCheckShipmentLine);
4347 $objCheckShipmentLine = $this->
db->fetch_object($resqlCheckShipmentLine);
4348 $this->error = $langs->trans(
'ErrorRecordAlreadyExists').
' : '.$langs->trans(
'ShipmentLine').
' '.$objCheckShipmentLine->rowid;
4349 $this->errors[] = $this->error;
4351 $this->
db->free($resqlCheckShipmentLine);
4354 dol_syslog(__METHOD__.
'Error ; '.$this->error, LOG_ERR);
4360 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"commandedet WHERE rowid = ".((int) $this->
id);
4365 if (!$error && !$notrigger) {
4367 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
4379 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
4384 $this->
db->commit();
4388 foreach ($this->errors as $errmsg) {
4389 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
4390 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4392 $this->
db->rollback();
4395 $this->error = $this->
db->lasterror();
4407 public function insert($user =
null, $notrigger = 0)
4409 global $langs, $conf;
4413 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4415 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
4418 if (empty($this->tva_tx)) {
4421 if (empty($this->localtax1_tx)) {
4422 $this->localtax1_tx = 0;
4424 if (empty($this->localtax2_tx)) {
4425 $this->localtax2_tx = 0;
4427 if (empty($this->localtax1_type)) {
4428 $this->localtax1_type = 0;
4430 if (empty($this->localtax2_type)) {
4431 $this->localtax2_type = 0;
4433 if (empty($this->total_localtax1)) {
4434 $this->total_localtax1 = 0;
4436 if (empty($this->total_localtax2)) {
4437 $this->total_localtax2 = 0;
4439 if (empty($this->rang)) {
4442 if (empty($this->remise_percent)) {
4443 $this->remise_percent = 0;
4445 if (empty($this->info_bits)) {
4446 $this->info_bits = 0;
4448 if (empty($this->special_code)) {
4449 $this->special_code = 0;
4451 if (empty($this->fk_parent_line)) {
4452 $this->fk_parent_line = 0;
4454 if (empty($this->pa_ht)) {
4457 if (empty($this->ref_ext)) {
4458 $this->ref_ext =
'';
4462 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4463 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4467 $this->pa_ht = $result;
4472 if ($this->product_type < 0) {
4479 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'commandedet';
4480 $sql .=
' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
4481 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
4482 $sql .=
' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
4483 $sql .=
' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
4484 $sql .=
' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
4486 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
4488 $sql .=
" VALUES (".$this->fk_commande.
",";
4489 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
4490 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
4491 $sql .=
" '".$this->db->escape($this->desc).
"',";
4492 $sql .=
" '".price2num($this->qty).
"',";
4493 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
4494 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
4495 $sql .=
" '".price2num($this->tva_tx).
"',";
4496 $sql .=
" '".price2num($this->localtax1_tx).
"',";
4497 $sql .=
" '".price2num($this->localtax2_tx).
"',";
4498 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
4499 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
4500 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
4501 $sql .=
" '".$this->db->escape($this->product_type).
"',";
4502 $sql .=
" '".price2num($this->remise_percent).
"',";
4503 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice) :
"null").
",";
4504 $sql .=
" ".($this->price !=
'' ?
"'".price2num($this->
price).
"'" :
"null").
",";
4505 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
4506 $sql .=
' '.((int) $this->special_code).
',';
4507 $sql .=
' '.((int) $this->rang).
',';
4508 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
4509 $sql .=
' '.price2num($this->pa_ht).
',';
4510 $sql .=
" ".((int) $this->info_bits).
",";
4511 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
4512 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
4513 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
4514 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
4515 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
4516 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
4517 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
',';
4518 $sql .=
' '.(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
4519 $sql .=
", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) :
'NULL');
4520 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
4521 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
4522 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
4523 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
4524 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
4527 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
4530 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
4531 $this->
rowid = $this->id;
4540 if (!$error && !$notrigger) {
4542 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
4550 $this->
db->commit();
4554 foreach ($this->errors as $errmsg) {
4555 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
4556 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4558 $this->
db->rollback();
4561 $this->error = $this->
db->error();
4562 $this->
db->rollback();
4576 global $conf, $langs;
4580 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4583 if (empty($this->tva_tx)) {
4586 if (empty($this->localtax1_tx)) {
4587 $this->localtax1_tx = 0;
4589 if (empty($this->localtax2_tx)) {
4590 $this->localtax2_tx = 0;
4592 if (empty($this->localtax1_type)) {
4593 $this->localtax1_type = 0;
4595 if (empty($this->localtax2_type)) {
4596 $this->localtax2_type = 0;
4598 if (empty($this->qty)) {
4601 if (empty($this->total_localtax1)) {
4602 $this->total_localtax1 = 0;
4604 if (empty($this->total_localtax2)) {
4605 $this->total_localtax2 = 0;
4607 if (empty($this->marque_tx)) {
4608 $this->marque_tx = 0;
4610 if (empty($this->marge_tx)) {
4611 $this->marge_tx = 0;
4613 if (empty($this->remise_percent)) {
4614 $this->remise_percent = 0;
4616 if (empty($this->remise)) {
4619 if (empty($this->info_bits)) {
4620 $this->info_bits = 0;
4622 if (empty($this->special_code)) {
4623 $this->special_code = 0;
4625 if (empty($this->product_type)) {
4626 $this->product_type = 0;
4628 if (empty($this->fk_parent_line)) {
4629 $this->fk_parent_line = 0;
4631 if (empty($this->pa_ht)) {
4634 if (empty($this->ref_ext)) {
4635 $this->ref_ext =
'';
4639 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4640 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4644 $this->pa_ht = $result;
4651 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4652 $sql .=
" description='".$this->db->escape($this->desc).
"'";
4653 $sql .=
" , label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
4654 $sql .=
" , vat_src_code=".(!empty($this->vat_src_code) ?
"'".$this->db->escape($this->vat_src_code).
"'" :
"''");
4655 $sql .=
" , tva_tx=".price2num($this->tva_tx);
4656 $sql .=
" , localtax1_tx=".price2num($this->localtax1_tx);
4657 $sql .=
" , localtax2_tx=".price2num($this->localtax2_tx);
4658 $sql .=
" , localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
4659 $sql .=
" , localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
4660 $sql .=
" , qty=".price2num($this->qty);
4661 $sql .=
" , ref_ext='".$this->db->escape($this->ref_ext).
"'";
4662 $sql .=
" , subprice=".price2num($this->subprice).
"";
4663 $sql .=
" , remise_percent=".price2num($this->remise_percent).
"";
4664 $sql .=
" , price=".price2num($this->
price).
"";
4665 $sql .=
" , remise=".price2num($this->remise).
"";
4666 if (empty($this->skip_update_total)) {
4667 $sql .=
" , total_ht=".price2num($this->total_ht).
"";
4668 $sql .=
" , total_tva=".price2num($this->total_tva).
"";
4669 $sql .=
" , total_ttc=".price2num($this->total_ttc).
"";
4670 $sql .=
" , total_localtax1=".price2num($this->total_localtax1);
4671 $sql .=
" , total_localtax2=".price2num($this->total_localtax2);
4673 $sql .=
" , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null");
4674 $sql .=
" , buy_price_ht='".price2num($this->pa_ht).
"'";
4675 $sql .=
" , info_bits=".((int) $this->info_bits);
4676 $sql .=
" , special_code=".((int) $this->special_code);
4677 $sql .=
" , date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
4678 $sql .=
" , date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
4679 $sql .=
" , product_type=".$this->product_type;
4680 $sql .=
" , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line :
"null");
4681 if (!empty($this->rang)) {
4682 $sql .=
", rang=".((int) $this->rang);
4684 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
4687 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice).
"";
4688 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht).
"";
4689 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva).
"";
4690 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).
"";
4692 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4694 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
4698 $this->
id = $this->rowid;
4705 if (!$error && !$notrigger) {
4707 $result = $this->
call_trigger(
'LINEORDER_MODIFY', $user);
4715 $this->
db->commit();
4719 foreach ($this->errors as $errmsg) {
4720 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
4721 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4723 $this->
db->rollback();
4726 $this->error = $this->
db->error();
4727 $this->
db->rollback();
4745 if (empty($this->total_localtax1)) {
4746 $this->total_localtax1 = 0;
4748 if (empty($this->total_localtax2)) {
4749 $this->total_localtax2 = 0;
4753 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4754 $sql .=
" total_ht='".price2num($this->total_ht).
"'";
4755 $sql .=
",total_tva='".price2num($this->total_tva).
"'";
4756 $sql .=
",total_localtax1='".price2num($this->total_localtax1).
"'";
4757 $sql .=
",total_localtax2='".price2num($this->total_localtax2).
"'";
4758 $sql .=
",total_ttc='".price2num($this->total_ttc).
"'";
4759 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4761 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
4765 $this->
db->commit();
4768 $this->error = $this->
db->error();
4769 $this->
db->rollback();