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;
135 public $cond_reglement_code;
140 public $deposit_percent;
150 public $mode_reglement;
155 public $mode_reglement_id;
160 public $mode_reglement_code;
166 public $availability_id;
172 public $availability_code;
178 public $availability;
183 public $demand_reason_id;
188 public $demand_reason_code;
200 public $date_commande;
207 public $date_livraison;
212 public $delivery_date;
217 public $fk_remise_except;
219 public $remise_percent;
220 public $remise_absolue;
223 public $special_code;
229 public $warehouse_id;
231 public $extraparams = array();
233 public $linked_objects = array();
238 public $user_author_id;
248 public $lines = array();
254 public $fk_multicurrency;
259 public $multicurrency_code;
260 public $multicurrency_tx;
261 public $multicurrency_total_ht;
262 public $multicurrency_total_tva;
263 public $multicurrency_total_ttc;
278 public $online_payment_url;
309 public $fields = array(
310 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
311 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>20,
'index'=>1),
312 'ref' =>array(
'type'=>
'varchar(30)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'showoncombobox'=>1,
'position'=>25),
313 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefExt',
'enabled'=>1,
'visible'=>0,
'position'=>26),
314 'ref_int' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefInt',
'enabled'=>1,
'visible'=>0,
'position'=>27),
315 'ref_client' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefCustomer',
'enabled'=>1,
'visible'=>-1,
'position'=>28),
316 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'$conf->societe->enabled',
'visible'=>-1,
'notnull'=>1,
'position'=>20),
317 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:fk_statut=1',
'label'=>
'Project',
'enabled'=>
'$conf->project->enabled',
'visible'=>-1,
'position'=>25),
318 'date_commande' =>array(
'type'=>
'date',
'label'=>
'Date',
'enabled'=>1,
'visible'=>1,
'position'=>60),
319 'date_valid' =>array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>62),
320 'date_cloture' =>array(
'type'=>
'datetime',
'label'=>
'DateClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>65),
321 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>85),
322 'fk_user_cloture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
323 'source' =>array(
'type'=>
'smallint(6)',
'label'=>
'Source',
'enabled'=>1,
'visible'=>-1,
'position'=>95),
325 'remise_percent' =>array(
'type'=>
'double',
'label'=>
'RelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>110),
326 'remise_absolue' =>array(
'type'=>
'double',
'label'=>
'CustomerRelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>115),
328 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'VAT',
'enabled'=>1,
'visible'=>-1,
'position'=>125,
'isameasure'=>1),
329 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax1',
'enabled'=>1,
'visible'=>-1,
'position'=>130,
'isameasure'=>1),
330 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax2',
'enabled'=>1,
'visible'=>-1,
'position'=>135,
'isameasure'=>1),
331 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalHT',
'enabled'=>1,
'visible'=>-1,
'position'=>140,
'isameasure'=>1),
332 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>145,
'isameasure'=>1),
333 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>150),
334 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>155),
335 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'PDFTemplate',
'enabled'=>1,
'visible'=>0,
'position'=>160),
337 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'BankAccount',
'enabled'=>
'$conf->banque->enabled',
'visible'=>-1,
'position'=>170),
338 'fk_currency' =>array(
'type'=>
'varchar(3)',
'label'=>
'MulticurrencyID',
'enabled'=>1,
'visible'=>-1,
'position'=>175),
339 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
340 'deposit_percent' =>array(
'type'=>
'varchar(63)',
'label'=>
'DepositPercent',
'enabled'=>1,
'visible'=>-1,
'position'=>181),
341 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
342 'date_livraison' =>array(
'type'=>
'date',
'label'=>
'DateDeliveryPlanned',
'enabled'=>1,
'visible'=>-1,
'position'=>190),
343 'fk_shipping_method' =>array(
'type'=>
'integer',
'label'=>
'ShippingMethod',
'enabled'=>1,
'visible'=>-1,
'position'=>195),
344 'fk_warehouse' =>array(
'type'=>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label'=>
'Fk warehouse',
'enabled'=>
'$conf->stock->enabled',
'visible'=>-1,
'position'=>200),
345 'fk_availability' =>array(
'type'=>
'integer',
'label'=>
'Availability',
'enabled'=>1,
'visible'=>-1,
'position'=>205),
346 'fk_input_reason' =>array(
'type'=>
'integer',
'label'=>
'InputReason',
'enabled'=>1,
'visible'=>-1,
'position'=>210),
348 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>225),
349 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>230),
350 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLabel',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>235),
351 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>240),
352 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'MulticurrencyCurrency',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>245),
353 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyRate',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>250,
'isameasure'=>1),
354 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>255,
'isameasure'=>1),
355 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>260,
'isameasure'=>1),
356 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>265,
'isameasure'=>1),
357 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>-1,
'position'=>270),
358 'module_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSModule',
'enabled'=>1,
'visible'=>-1,
'position'=>275),
359 'pos_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSTerminal',
'enabled'=>1,
'visible'=>-1,
'position'=>280),
360 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-1,
'position'=>300),
361 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>302),
362 'date_creation' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>304),
363 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>306),
364 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>400),
365 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>-1,
'position'=>500),
390 const STATUS_ACCEPTED = 2;
417 global $langs, $conf;
418 $langs->load(
"order");
420 if (!empty($conf->global->COMMANDE_ADDON)) {
423 $file = $conf->global->COMMANDE_ADDON.
".php";
424 $classname = $conf->global->COMMANDE_ADDON;
427 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
428 foreach ($dirmodels as $reldir) {
432 $mybool |= @include_once $dir.$file;
435 if ($mybool ===
false) {
440 $obj =
new $classname();
441 $numref = $obj->getNextValue($soc, $this);
446 $this->error = $obj->error;
451 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_COMMANDE_ADDON_NotDefined");
465 public function valid($user, $idwarehouse = 0, $notrigger = 0)
467 global $conf, $langs;
469 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
474 if ($this->statut == self::STATUS_VALIDATED) {
475 dol_syslog(get_class($this).
"::valid action abandonned: already validated", LOG_WARNING);
479 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
480 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate)))) {
481 $this->error =
'NotEnoughPermissions';
482 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
492 $soc->fetch($this->socid);
495 $result = $soc->set_as_client();
498 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
506 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
507 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
508 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
509 $sql .=
" date_valid='".$this->db->idate($now).
"',";
510 $sql .=
" fk_user_valid = ".($user->id > 0 ? (int) $user->id :
"null").
",";
511 $sql .=
" fk_user_modif = ".((int) $user->id);
512 $sql .=
" WHERE rowid = ".((int) $this->
id);
514 dol_syslog(get_class($this).
"::valid", LOG_DEBUG);
518 $this->error = $this->
db->lasterror();
524 if ($result >= 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
525 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
526 $langs->load(
"agenda");
529 $cpt = count($this->lines);
530 for ($i = 0; $i < $cpt; $i++) {
531 if ($this->lines[$i]->fk_product > 0) {
533 $mouvP->origin = &$this;
534 $mouvP->setOrigin($this->element, $this->
id);
536 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"OrderValidatedInDolibarr", $num));
539 $this->error = $mouvP->error;
549 if (!$error && !$notrigger) {
559 $this->oldref = $this->ref;
562 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
564 $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).
"'";
565 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'commande/".$this->
db->escape($this->
ref).
"' and entity = ".$conf->entity;
568 $error++; $this->error = $this->
db->lasterror();
574 $dirsource = $conf->commande->multidir_output[$this->entity].
'/'.$oldref;
575 $dirdest = $conf->commande->multidir_output[$this->entity].
'/'.$newref;
576 if (!$error && file_exists($dirsource)) {
577 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
579 if (@rename($dirsource, $dirdest)) {
582 $listoffiles =
dol_dir_list($conf->commande->multidir_output[$this->entity].
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
583 foreach ($listoffiles as $fileentry) {
584 $dirsource = $fileentry[
'name'];
585 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
586 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
587 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
588 @rename($dirsource, $dirdest);
599 $this->brouillon = 0;
606 $this->
db->rollback();
622 global $conf, $langs;
627 if ($this->statut <= self::STATUS_DRAFT) {
631 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
632 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate)))) {
633 $this->error =
'Permission denied';
641 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
642 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT.
",";
643 $sql .=
" fk_user_modif = ".((int) $user->id);
644 $sql .=
" WHERE rowid = ".((int) $this->
id);
646 if ($this->
db->query($sql)) {
648 $this->oldcopy = clone $this;
652 if (!empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
655 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
656 $langs->load(
"agenda");
658 $num = count($this->lines);
659 for ($i = 0; $i < $num; $i++) {
660 if ($this->lines[$i]->fk_product > 0) {
662 $mouvP->origin = &$this;
663 $mouvP->setOrigin($this->element, $this->
id);
665 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderBackToDraftInDolibarr", $this->ref));
667 $error++; $this->error = $mouvP->error;
break;
675 $result = $this->
call_trigger(
'ORDER_UNVALIDATE', $user);
686 $this->
db->rollback();
690 $this->error = $this->
db->error();
691 $this->
db->rollback();
710 if ($this->statut != self::STATUS_CANCELED && $this->statut != self::STATUS_CLOSED) {
711 dol_syslog(get_class($this).
"::set_reopen order has not status closed", LOG_WARNING);
717 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
718 $sql .=
' SET fk_statut='.self::STATUS_VALIDATED.
', facture=0,';
719 $sql .=
" fk_user_modif = ".((int) $user->id);
720 $sql .=
" WHERE rowid = ".((int) $this->
id);
722 dol_syslog(get_class($this).
"::set_reopen", LOG_DEBUG);
733 $this->error = $this->
db->lasterror();
744 foreach ($this->errors as $errmsg) {
745 dol_syslog(get_class($this).
"::set_reopen ".$errmsg, LOG_ERR);
746 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
748 $this->
db->rollback();
760 public function cloture($user, $notrigger = 0)
766 $usercanclose = ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer))
767 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->close)));
770 if ($this->statut == self::STATUS_CLOSED) {
777 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
778 $sql .=
' SET fk_statut = '.self::STATUS_CLOSED.
',';
779 $sql .=
' fk_user_cloture = '.((int) $user->id).
',';
780 $sql .=
" date_cloture = '".$this->db->idate($now).
"',";
781 $sql .=
" fk_user_modif = ".((int) $user->id);
784 if ($this->
db->query($sql)) {
800 $this->
db->rollback();
804 $this->error = $this->
db->lasterror();
806 $this->
db->rollback();
820 public function cancel($idwarehouse = -1)
822 global $conf, $user, $langs;
828 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
829 $sql .=
" SET fk_statut = ".self::STATUS_CANCELED.
",";
830 $sql .=
" fk_user_modif = ".((int) $user->id);
831 $sql .=
" WHERE rowid = ".((int) $this->
id);
832 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
834 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
835 if ($this->
db->query($sql)) {
837 if (!empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
838 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
839 $langs->load(
"agenda");
841 $num = count($this->lines);
842 for ($i = 0; $i < $num; $i++) {
843 if ($this->lines[$i]->fk_product > 0) {
845 $mouvP->setOrigin($this->element, $this->
id);
847 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"OrderCanceledInDolibarr", $this->ref));
850 $this->error = $mouvP->error;
871 foreach ($this->errors as $errmsg) {
872 dol_syslog(get_class($this).
"::cancel ".$errmsg, LOG_ERR);
873 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
875 $this->
db->rollback();
879 $this->error = $this->
db->error();
880 $this->
db->rollback();
893 public function create($user, $notrigger = 0)
895 global $conf, $langs, $mysoc;
899 $this->brouillon = 1;
902 $date = ($this->date_commande ? $this->date_commande : $this->date);
903 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
906 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
911 if (empty($this->fk_multicurrency)) {
912 $this->multicurrency_code = $conf->currency;
913 $this->fk_multicurrency = 0;
914 $this->multicurrency_tx = 1;
917 dol_syslog(get_class($this).
"::create user=".$user->id);
920 if (!empty($this->
ref)) {
923 $this->error =
'ErrorRefAlreadyExists';
924 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
925 $this->
db->rollback();
931 $result = $soc->fetch($this->socid);
933 $this->error =
"Failed to fetch company";
934 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
937 if (!empty($conf->global->ORDER_REQUIRE_SOURCE) && $this->source < 0) {
938 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Source"));
939 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
947 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande (";
948 $sql .=
" ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client, ref_int";
949 $sql .=
", model_pdf, fk_cond_reglement, deposit_percent, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address";
950 $sql .=
", fk_shipping_method";
951 $sql .=
", fk_warehouse";
952 $sql .=
", remise_absolue, remise_percent";
953 $sql .=
", fk_incoterms, location_incoterms";
954 $sql .=
", entity, module_source, pos_source";
955 $sql .=
", fk_multicurrency";
956 $sql .=
", multicurrency_code";
957 $sql .=
", multicurrency_tx";
959 $sql .=
" VALUES ('(PROV)', ".((int) $this->socid).
", '".$this->
db->idate($now).
"', ".((int) $user->id);
960 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
961 $sql .=
", '".$this->db->idate($date).
"'";
962 $sql .=
", ".($this->source >= 0 && $this->source !=
'' ? $this->
db->escape($this->source) :
'null');
963 $sql .=
", '".$this->db->escape($this->note_private).
"'";
964 $sql .=
", '".$this->db->escape($this->note_public).
"'";
965 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
966 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
967 $sql .=
", ".($this->ref_int ?
"'".$this->db->escape($this->ref_int).
"'" :
"null");
968 $sql .=
", '".$this->db->escape($this->model_pdf).
"'";
969 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
970 $sql .=
", ".(! empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
"null");
971 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
972 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
973 $sql .=
", ".($this->availability_id > 0 ? ((int) $this->availability_id) :
"null");
974 $sql .=
", ".($this->demand_reason_id > 0 ? ((int) $this->demand_reason_id) :
"null");
975 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
976 $sql .=
", ".($this->fk_delivery_address > 0 ? ((int) $this->fk_delivery_address) :
'NULL');
977 $sql .=
", ".(!empty($this->shipping_method_id) && $this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) :
'NULL');
978 $sql .=
", ".(!empty($this->warehouse_id) && $this->warehouse_id > 0 ? ((int) $this->warehouse_id) :
'NULL');
979 $sql .=
", ".($this->remise_absolue > 0 ? $this->
db->escape($this->remise_absolue) :
'NULL');
980 $sql .=
", ".($this->remise_percent > 0 ? $this->
db->escape($this->remise_percent) : 0);
981 $sql .=
", ".(int) $this->fk_incoterms;
982 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
983 $sql .=
", ".setEntity($this);
984 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
985 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
986 $sql .=
", ".(int) $this->fk_multicurrency;
987 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
988 $sql .=
", ".(float) $this->multicurrency_tx;
991 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
994 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'commande');
998 $num = count($this->lines);
1003 for ($i = 0; $i < $num; $i++) {
1004 $line = $this->lines[$i];
1008 if (!is_object($line)) {
1009 $line = (object) $line;
1013 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1014 $fk_parent_line = 0;
1018 $vatrate = $line->tva_tx;
1019 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', $vatrate)) {
1020 $vatrate .=
' ('.$line->vat_src_code.
')';
1023 if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
1024 $originid = $line->origin_id;
1025 $origintype = $line->origin;
1027 $originid = $line->id;
1028 $origintype = $this->element;
1032 if (empty($line->ref_ext)) {
1033 $line->ref_ext =
'';
1041 $line->localtax1_tx,
1042 $line->localtax2_tx,
1044 $line->remise_percent,
1046 $line->fk_remise_except,
1051 $line->product_type,
1053 $line->special_code,
1055 $line->fk_fournprice,
1058 $line->array_options,
1067 if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) {
1068 $this->error = $this->
db->lasterror();
1069 $this->errors[] = $this->error;
1072 $this->
db->rollback();
1076 if ($result > 0 && $line->product_type == 9) {
1077 $fk_parent_line = $result;
1084 $initialref =
'(PROV'.$this->id.
')';
1085 if (!empty($this->
ref)) {
1086 $initialref = $this->ref;
1089 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"commande SET ref='".$this->
db->escape($initialref).
"' WHERE rowid=".((int) $this->
id);
1090 if ($this->
db->query($sql)) {
1091 $this->
ref = $initialref;
1093 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1094 $this->linked_objects = $this->linkedObjectsIds;
1098 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1099 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1100 if (is_array($tmp_origin_id)) {
1101 foreach ($tmp_origin_id as $origin_id) {
1104 $this->error = $this->
db->lasterror();
1110 $origin_id = $tmp_origin_id;
1113 $this->error = $this->
db->lasterror();
1120 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) {
1121 $originforcontact = $this->origin;
1122 $originidforcontact = $this->origin_id;
1123 if ($originforcontact ==
'shipping') {
1124 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1126 $exp->fetch($this->origin_id);
1127 $exp->fetchObjectLinked();
1128 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1129 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1130 $originforcontact =
'commande';
1131 if (is_object($value)) {
1132 $originidforcontact = $value->id;
1134 $originidforcontact = $value;
1141 $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";
1142 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->
db->escape($originforcontact).
"'";
1144 $resqlcontact = $this->
db->query($sqlcontact);
1145 if ($resqlcontact) {
1146 while ($objcontact = $this->
db->fetch_object($resqlcontact)) {
1148 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1162 if (!$error && !$notrigger) {
1172 $this->
db->commit();
1175 $this->
db->rollback();
1179 $this->error = $this->
db->lasterror();
1180 $this->
db->rollback();
1186 $this->
db->rollback();
1201 global $conf, $user, $hookmanager;
1208 foreach ($this->lines as $line) {
1209 $line->fetch_optionals();
1213 $objFrom = clone $this;
1216 if (!empty($socid) && $socid != $this->socid) {
1219 if ($objsoc->fetch($socid) > 0) {
1220 $this->socid = $objsoc->id;
1221 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1222 $this->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent :
null);
1223 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1224 $this->fk_project = 0;
1225 $this->fk_delivery_address = 0;
1236 $this->user_author_id = $user->id;
1237 $this->user_valid = 0;
1238 $this->user_validation_id = 0;
1240 $this->date_commande =
dol_now();
1241 $this->date_creation =
'';
1242 $this->date_validation =
'';
1243 if (empty($conf->global->MAIN_KEEP_REF_CUSTOMER_ON_CLONING)) {
1244 $this->ref_client =
'';
1248 $num = count($this->lines);
1249 for ($i = 0; $i < $num; $i++) {
1250 $this->lines[$i]->ref_ext =
'';
1254 $this->context[
'createfromclone'] =
'createfromclone';
1255 $result = $this->
create($user);
1269 if ($this->socid == $objFrom->socid) {
1278 if (is_object($hookmanager)) {
1279 $parameters = array(
'objFrom'=>$objFrom);
1281 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1283 $this->errors += $hookmanager->errors;
1284 $this->error = $hookmanager->error;
1290 unset($this->context[
'createfromclone']);
1294 $this->
db->commit();
1297 $this->
db->rollback();
1312 global $conf, $hookmanager;
1320 $this->date_commande =
dol_now();
1323 $num = count($object->lines);
1324 for ($i = 0; $i < $num; $i++) {
1327 $line->libelle = $object->lines[$i]->libelle;
1328 $line->label = $object->lines[$i]->label;
1329 $line->desc = $object->lines[$i]->desc;
1330 $line->price = $object->lines[$i]->price;
1331 $line->subprice = $object->lines[$i]->subprice;
1332 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1333 $line->tva_tx = $object->lines[$i]->tva_tx;
1334 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1335 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1336 $line->qty = $object->lines[$i]->qty;
1337 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1338 $line->remise_percent = $object->lines[$i]->remise_percent;
1339 $line->fk_product = $object->lines[$i]->fk_product;
1340 $line->info_bits = $object->lines[$i]->info_bits;
1341 $line->product_type = $object->lines[$i]->product_type;
1342 $line->rang = $object->lines[$i]->rang;
1343 $line->special_code = $object->lines[$i]->special_code;
1344 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1345 $line->fk_unit = $object->lines[$i]->fk_unit;
1347 $line->date_start = $object->lines[$i]->date_start;
1348 $line->date_end = $object->lines[$i]->date_end;
1350 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1351 $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);
1352 $line->pa_ht = $marginInfos[0];
1353 $line->marge_tx = $marginInfos[1];
1354 $line->marque_tx = $marginInfos[2];
1356 $line->origin = $object->element;
1357 $line->origin_id = $object->lines[$i]->id;
1360 $object->lines[$i]->fetch_optionals();
1361 foreach ($object->lines[$i]->array_options as $options_key => $value) {
1362 $line->array_options[$options_key] = $value;
1365 $this->lines[$i] = $line;
1368 $this->entity = $object->entity;
1369 $this->socid = $object->socid;
1370 $this->fk_project = $object->fk_project;
1371 $this->cond_reglement_id = $object->cond_reglement_id;
1372 $this->deposit_percent = $object->deposit_percent;
1373 $this->mode_reglement_id = $object->mode_reglement_id;
1374 $this->fk_account = $object->fk_account;
1375 $this->availability_id = $object->availability_id;
1376 $this->demand_reason_id = $object->demand_reason_id;
1377 $this->date_livraison = $object->date_livraison;
1378 $this->delivery_date = $object->date_livraison;
1379 $this->shipping_method_id = $object->shipping_method_id;
1380 $this->warehouse_id = $object->warehouse_id;
1381 $this->fk_delivery_address = $object->fk_delivery_address;
1382 $this->contact_id = $object->contact_id;
1383 $this->ref_client = $object->ref_client;
1385 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1386 $this->note_private = $object->note_private;
1387 $this->note_public = $object->note_public;
1390 $this->origin = $object->element;
1391 $this->origin_id = $object->id;
1394 if (!empty($conf->multicurrency->enabled)) {
1395 if (!empty($object->multicurrency_code)) {
1396 $this->multicurrency_code = $object->multicurrency_code;
1398 if (!empty($conf->global->MULTICURRENCY_USE_ORIGIN_TX) && !empty($object->multicurrency_tx)) {
1399 $this->multicurrency_tx = $object->multicurrency_tx;
1402 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1404 $this->fk_multicurrency = $tmparray[0];
1405 $this->multicurrency_tx = $tmparray[1];
1409 if (empty($this->fk_multicurrency)) {
1410 $this->multicurrency_code = $conf->currency;
1411 $this->fk_multicurrency = 0;
1412 $this->multicurrency_tx = 1;
1417 $object->fetch_optionals();
1420 $element_extrafields = $e->fetch_name_optionals_label($this->table_element);
1422 foreach ($object->array_options as $options_key => $value) {
1423 if (array_key_exists(str_replace(
'options_',
'', $options_key), $element_extrafields)) {
1424 $this->array_options[$options_key] = $value;
1428 $this->linked_objects[$this->origin] = $this->origin_id;
1429 if (isset($object->other_linked_objects) && is_array($object->other_linked_objects) && !empty($object->other_linked_objects)) {
1430 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1433 $ret = $this->
create($user);
1437 $hookmanager->initHooks(array(
'orderdao'));
1439 $parameters = array(
'objFrom'=>$object);
1441 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1443 $this->errors += $hookmanager->errors;
1444 $this->error = $hookmanager->error;
1450 if (!empty($conf->global->ORDER_VALID_AFTER_CLOSE_PROPAL)) {
1452 $this->
valid($user);
1504 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)
1506 global $mysoc, $conf, $langs, $user;
1508 $logtext =
"::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent";
1509 $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";
1510 $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";
1511 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
1513 if ($this->statut == self::STATUS_DRAFT) {
1514 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1518 if (empty($remise_percent)) {
1519 $remise_percent = 0;
1524 if (empty($info_bits)) {
1530 if (empty($txtva)) {
1533 if (empty($txlocaltax1)) {
1536 if (empty($txlocaltax2)) {
1539 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1540 $fk_parent_line = 0;
1542 if (empty($this->fk_multicurrency)) {
1543 $this->fk_multicurrency = 0;
1545 if (empty($ref_ext)) {
1549 $remise_percent =
price2num($remise_percent);
1552 $pu_ht_devise =
price2num($pu_ht_devise);
1555 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1560 if ($price_base_type ==
'HT') {
1565 $label = trim($label);
1566 $desc = trim($desc);
1573 if ($date_start && $date_end && $date_start > $date_end) {
1574 $langs->load(
"errors");
1575 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1581 $product_type = $type;
1582 if (!empty($fk_product) && $fk_product > 0) {
1584 $result = $product->fetch($fk_product);
1585 $product_type = $product->type;
1587 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) {
1588 $langs->load(
"errors");
1589 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
1590 $this->errors[] = $this->error;
1591 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
1592 $this->
db->rollback();
1606 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1607 $vat_src_code = $reg[1];
1608 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1611 $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);
1621 $total_ht = $tabprice[0];
1622 $total_tva = $tabprice[1];
1623 $total_ttc = $tabprice[2];
1624 $total_localtax1 = $tabprice[9];
1625 $total_localtax2 = $tabprice[10];
1626 $pu_ht = $tabprice[3];
1629 $multicurrency_total_ht = $tabprice[16];
1630 $multicurrency_total_tva = $tabprice[17];
1631 $multicurrency_total_ttc = $tabprice[18];
1632 $pu_ht_devise = $tabprice[19];
1636 if ($ranktouse == -1) {
1637 $rangmax = $this->
line_max($fk_parent_line);
1638 $ranktouse = $rangmax + 1;
1645 if ($remise_percent > 0) {
1646 $remise = round(($pu * $remise_percent / 100), 2);
1647 $price = $pu - $remise;
1653 $this->line->context = $this->context;
1655 $this->line->fk_commande = $this->id;
1656 $this->line->label = $label;
1657 $this->line->desc = $desc;
1658 $this->line->qty = $qty;
1659 $this->line->ref_ext = $ref_ext;
1661 $this->line->vat_src_code = $vat_src_code;
1662 $this->line->tva_tx = $txtva;
1663 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1664 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1665 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1666 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1667 $this->line->fk_product = $fk_product;
1668 $this->line->product_type = $product_type;
1669 $this->line->fk_remise_except = $fk_remise_except;
1670 $this->line->remise_percent = $remise_percent;
1671 $this->line->subprice = $pu_ht;
1672 $this->line->rang = $ranktouse;
1673 $this->line->info_bits = $info_bits;
1674 $this->line->total_ht = $total_ht;
1675 $this->line->total_tva = $total_tva;
1676 $this->line->total_localtax1 = $total_localtax1;
1677 $this->line->total_localtax2 = $total_localtax2;
1678 $this->line->total_ttc = $total_ttc;
1679 $this->line->special_code = $special_code;
1680 $this->line->origin = $origin;
1681 $this->line->origin_id = $origin_id;
1682 $this->line->fk_parent_line = $fk_parent_line;
1683 $this->line->fk_unit = $fk_unit;
1685 $this->line->date_start = $date_start;
1686 $this->line->date_end = $date_end;
1688 $this->line->fk_fournprice = $fk_fournprice;
1689 $this->line->pa_ht = $pa_ht;
1692 $this->line->fk_multicurrency = $this->fk_multicurrency;
1693 $this->line->multicurrency_code = $this->multicurrency_code;
1694 $this->line->multicurrency_subprice = $pu_ht_devise;
1695 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1696 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1697 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1700 $this->line->price = $price;
1702 if (is_array($array_options) && count($array_options) > 0) {
1703 $this->line->array_options = $array_options;
1706 $result = $this->line->insert($user);
1709 if (!empty($fk_parent_line)) {
1711 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
1712 $linecount = count($this->lines);
1713 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
1719 if (empty($noupdateafterinsertline)) {
1724 $this->
db->commit();
1725 return $this->line->id;
1727 $this->
db->rollback();
1731 $this->error = $this->line->error;
1732 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1733 $this->
db->rollback();
1737 dol_syslog(get_class($this).
"::addline status of order must be Draft to allow use of ->addline()", LOG_ERR);
1757 public function add_product($idproduct, $qty, $remise_percent = 0.0, $date_start =
'', $date_end =
'')
1760 global $conf, $mysoc;
1766 if ($idproduct > 0) {
1768 $prod->fetch($idproduct);
1772 if (empty($tva_tx)) {
1777 $localtax1_tx =
get_localtax($tva_tx, 1, $this->thirdparty, $mysoc, $tva_npr);
1778 $localtax2_tx =
get_localtax($tva_tx, 2, $this->thirdparty, $mysoc, $tva_npr);
1781 if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
1782 $price = $prod->multiprices[$this->thirdparty->price_level];
1784 $price = $prod->price;
1789 $line->context = $this->context;
1791 $line->fk_product = $idproduct;
1792 $line->desc = $prod->description;
1794 $line->subprice = $price;
1795 $line->remise_percent = $remise_percent;
1796 $line->vat_src_code = $vat_src_code;
1797 $line->tva_tx = $tva_tx;
1798 $line->localtax1_tx = $localtax1_tx;
1799 $line->localtax2_tx = $localtax2_tx;
1800 $line->ref = $prod->ref;
1801 $line->libelle = $prod->label;
1802 $line->product_desc = $prod->description;
1803 $line->fk_unit = $prod->fk_unit;
1807 $line->date_start = $date_start;
1810 $line->date_end = $date_end;
1813 $this->lines[] = $line;
1846 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
1849 if (empty($id) && empty($ref) && empty($ref_ext)) {
1853 $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';
1854 $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';
1855 $sql .=
', c.fk_account';
1856 $sql .=
', c.date_commande, c.date_valid, c.tms';
1857 $sql .=
', c.date_livraison as delivery_date';
1858 $sql .=
', c.fk_shipping_method';
1859 $sql .=
', c.fk_warehouse';
1860 $sql .=
', c.fk_projet as fk_project, c.remise_percent, c.remise, c.remise_absolue, c.source, c.facture as billed';
1861 $sql .=
', c.note_private, c.note_public, c.ref_client, c.ref_ext, c.ref_int, c.model_pdf, c.last_main_doc, c.fk_delivery_address, c.extraparams';
1862 $sql .=
', c.fk_incoterms, c.location_incoterms';
1863 $sql .=
", c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc";
1864 $sql .=
", c.module_source, c.pos_source";
1865 $sql .=
", i.libelle as label_incoterms";
1866 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
1867 $sql .=
', cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle, cr.libelle_facture as cond_reglement_libelle_doc';
1868 $sql .=
', ca.code as availability_code, ca.label as availability_label';
1869 $sql .=
', dr.code as demand_reason_code';
1870 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
1871 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON c.fk_cond_reglement = cr.rowid';
1872 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON c.fk_mode_reglement = p.id';
1873 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON c.fk_availability = ca.rowid';
1874 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON c.fk_input_reason = dr.rowid';
1875 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
1878 $sql .=
" WHERE c.rowid=".((int) $id);
1880 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
1884 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
1887 $sql .=
" AND c.ref_ext='".$this->db->escape($ref_ext).
"'";
1890 $sql .=
" AND c.ref_int='".$this->db->escape($notused).
"'";
1893 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1894 $result = $this->
db->query($sql);
1896 $obj = $this->
db->fetch_object($result);
1898 $this->
id = $obj->rowid;
1899 $this->entity = $obj->entity;
1901 $this->
ref = $obj->ref;
1902 $this->ref_client = $obj->ref_client;
1903 $this->ref_customer = $obj->ref_client;
1904 $this->ref_ext = $obj->ref_ext;
1905 $this->ref_int = $obj->ref_int;
1907 $this->socid = $obj->fk_soc;
1908 $this->thirdparty =
null;
1910 $this->fk_project = $obj->fk_project;
1911 $this->project =
null;
1913 $this->statut = $obj->fk_statut;
1914 $this->status = $obj->fk_statut;
1916 $this->user_author_id = $obj->fk_user_author;
1917 $this->user_creation_id = $obj->fk_user_author;
1918 $this->user_validation_id = $obj->fk_user_valid;
1919 $this->user_valid = $obj->fk_user_valid;
1920 $this->user_modification_id = $obj->fk_user_modif;
1921 $this->user_modification = $obj->fk_user_modif;
1922 $this->total_ht = $obj->total_ht;
1923 $this->total_tva = $obj->total_tva;
1924 $this->total_localtax1 = $obj->total_localtax1;
1925 $this->total_localtax2 = $obj->total_localtax2;
1926 $this->total_ttc = $obj->total_ttc;
1927 $this->date = $this->
db->jdate($obj->date_commande);
1928 $this->date_commande = $this->
db->jdate($obj->date_commande);
1929 $this->date_creation = $this->
db->jdate($obj->date_creation);
1930 $this->date_validation = $this->
db->jdate($obj->date_valid);
1931 $this->date_modification = $this->
db->jdate($obj->tms);
1932 $this->remise = $obj->remise;
1933 $this->remise_percent = $obj->remise_percent;
1934 $this->remise_absolue = $obj->remise_absolue;
1935 $this->source = $obj->source;
1936 $this->billed = $obj->billed;
1937 $this->note = $obj->note_private;
1938 $this->note_private = $obj->note_private;
1939 $this->note_public = $obj->note_public;
1940 $this->model_pdf = $obj->model_pdf;
1941 $this->modelpdf = $obj->model_pdf;
1942 $this->last_main_doc = $obj->last_main_doc;
1943 $this->mode_reglement_id = $obj->fk_mode_reglement;
1944 $this->mode_reglement_code = $obj->mode_reglement_code;
1945 $this->mode_reglement = $obj->mode_reglement_libelle;
1946 $this->cond_reglement_id = $obj->fk_cond_reglement;
1947 $this->cond_reglement_code = $obj->cond_reglement_code;
1948 $this->cond_reglement = $obj->cond_reglement_libelle;
1949 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1950 $this->deposit_percent = $obj->deposit_percent;
1951 $this->fk_account = $obj->fk_account;
1952 $this->availability_id = $obj->fk_availability;
1953 $this->availability_code = $obj->availability_code;
1955 $this->demand_reason_id = $obj->fk_input_reason;
1956 $this->demand_reason_code = $obj->demand_reason_code;
1957 $this->date_livraison = $this->
db->jdate($obj->delivery_date);
1958 $this->delivery_date = $this->
db->jdate($obj->delivery_date);
1959 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method :
null;
1960 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse :
null;
1961 $this->fk_delivery_address = $obj->fk_delivery_address;
1962 $this->module_source = $obj->module_source;
1963 $this->pos_source = $obj->pos_source;
1966 $this->fk_incoterms = $obj->fk_incoterms;
1967 $this->location_incoterms = $obj->location_incoterms;
1968 $this->label_incoterms = $obj->label_incoterms;
1971 $this->fk_multicurrency = $obj->fk_multicurrency;
1972 $this->multicurrency_code = $obj->multicurrency_code;
1973 $this->multicurrency_tx = $obj->multicurrency_tx;
1974 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1975 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1976 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1978 $this->extraparams = (array) json_decode($obj->extraparams,
true);
1980 $this->lines = array();
1982 if ($this->statut == self::STATUS_DRAFT) {
1983 $this->brouillon = 1;
1990 $this->
db->free($result);
1999 $this->error =
'Order with id '.$id.
' not found sql='.$sql;
2003 $this->error = $this->
db->error();
2021 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2022 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
2027 $result = $remise->fetch($idremise);
2030 if ($remise->fk_facture) {
2031 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2032 $this->
db->rollback();
2038 $line->fk_commande = $this->id;
2039 $line->fk_remise_except = $remise->id;
2040 $line->desc = $remise->description;
2041 $line->vat_src_code = $remise->vat_src_code;
2042 $line->tva_tx = $remise->tva_tx;
2043 $line->subprice = -$remise->amount_ht;
2044 $line->price = -$remise->amount_ht;
2045 $line->fk_product = 0;
2047 $line->remise_percent = 0;
2049 $line->info_bits = 2;
2051 $line->total_ht = -$remise->amount_ht;
2052 $line->total_tva = -$remise->amount_tva;
2053 $line->total_ttc = -$remise->amount_ttc;
2055 $result = $line->insert();
2059 $this->
db->commit();
2062 $this->
db->rollback();
2066 $this->error = $line->error;
2067 $this->errors = $line->errors;
2068 $this->
db->rollback();
2072 $this->
db->rollback();
2086 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2089 global $langs, $conf;
2091 $this->lines = array();
2093 $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,';
2094 $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,';
2095 $sql .=
' l.total_ht, l.total_ttc, l.total_tva, l.total_localtax1, l.total_localtax2, l.date_start, l.date_end,';
2096 $sql .=
' l.fk_unit,';
2097 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2098 $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,';
2099 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units';
2100 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as l';
2101 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (p.rowid = l.fk_product)';
2102 $sql .=
' WHERE l.fk_commande = '.((int) $this->
id);
2103 if ($only_product) {
2104 $sql .=
' AND p.fk_product_type = 0';
2106 $sql .=
' ORDER BY l.rang, l.rowid';
2108 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
2109 $result = $this->
db->query($sql);
2111 $num = $this->
db->num_rows($result);
2115 $objp = $this->
db->fetch_object($result);
2119 $line->rowid = $objp->rowid;
2120 $line->id = $objp->rowid;
2121 $line->fk_commande = $objp->fk_commande;
2122 $line->commande_id = $objp->fk_commande;
2123 $line->label = $objp->custom_label;
2124 $line->desc = $objp->description;
2125 $line->description = $objp->description;
2126 $line->product_type = $objp->product_type;
2127 $line->qty = $objp->qty;
2128 $line->ref_ext = $objp->ref_ext;
2130 $line->vat_src_code = $objp->vat_src_code;
2131 $line->tva_tx = $objp->tva_tx;
2132 $line->localtax1_tx = $objp->localtax1_tx;
2133 $line->localtax2_tx = $objp->localtax2_tx;
2134 $line->localtax1_type = $objp->localtax1_type;
2135 $line->localtax2_type = $objp->localtax2_type;
2136 $line->total_ht = $objp->total_ht;
2137 $line->total_ttc = $objp->total_ttc;
2138 $line->total_tva = $objp->total_tva;
2139 $line->total_localtax1 = $objp->total_localtax1;
2140 $line->total_localtax2 = $objp->total_localtax2;
2141 $line->subprice = $objp->subprice;
2142 $line->fk_remise_except = $objp->fk_remise_except;
2143 $line->remise_percent = $objp->remise_percent;
2144 $line->price = $objp->price;
2145 $line->fk_product = $objp->fk_product;
2146 $line->fk_fournprice = $objp->fk_fournprice;
2147 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2148 $line->pa_ht = $marginInfos[0];
2149 $line->marge_tx = $marginInfos[1];
2150 $line->marque_tx = $marginInfos[2];
2151 $line->rang = $objp->rang;
2152 $line->info_bits = $objp->info_bits;
2153 $line->special_code = $objp->special_code;
2154 $line->fk_parent_line = $objp->fk_parent_line;
2156 $line->ref = $objp->product_ref;
2157 $line->libelle = $objp->product_label;
2159 $line->product_ref = $objp->product_ref;
2160 $line->product_label = $objp->product_label;
2161 $line->product_tosell = $objp->product_tosell;
2162 $line->product_tobuy = $objp->product_tobuy;
2163 $line->product_desc = $objp->product_desc;
2164 $line->product_tobatch = $objp->product_tobatch;
2165 $line->product_barcode = $objp->product_barcode;
2167 $line->fk_product_type = $objp->fk_product_type;
2168 $line->fk_unit = $objp->fk_unit;
2170 $line->weight = $objp->weight;
2171 $line->weight_units = $objp->weight_units;
2172 $line->volume = $objp->volume;
2173 $line->volume_units = $objp->volume_units;
2175 $line->date_start = $this->
db->jdate($objp->date_start);
2176 $line->date_end = $this->
db->jdate($objp->date_end);
2179 $line->fk_multicurrency = $objp->fk_multicurrency;
2180 $line->multicurrency_code = $objp->multicurrency_code;
2181 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2182 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2183 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2184 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2186 $line->fetch_optionals();
2189 if (!empty($conf->global->MAIN_MULTILANGS) && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2191 $tmpproduct->fetch($objp->fk_product);
2192 $tmpproduct->getMultiLangs();
2194 $line->multilangs = $tmpproduct->multilangs;
2197 $this->lines[$i] = $line;
2202 $this->
db->free($result);
2206 $this->error = $this->
db->error();
2220 foreach ($this->lines as $line) {
2221 if ($line->product_type == 0) {
2236 foreach ($this->lines as $line) {
2237 if ($line->product_type == 1) {
2253 $sql =
'SELECT COUNT(DISTINCT ed.fk_expedition) as nb';
2254 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2255 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2257 $sql .=
' ed.fk_origin_line = cd.rowid';
2258 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2261 dol_syslog(get_class($this).
"::getNbOfShipments", LOG_DEBUG);
2264 $obj = $this->
db->fetch_object(
$resql);
2272 $this->error = $this->
db->lasterror();
2287 $this->expeditions = array();
2289 $sql =
'SELECT cd.rowid, cd.fk_product,';
2290 $sql .=
' sum(ed.qty) as qty';
2291 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2292 if ($filtre_statut >= 0) {
2293 $sql .=
' '.MAIN_DB_PREFIX.
'expedition as e,';
2295 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2297 if ($filtre_statut >= 0) {
2298 $sql .=
' ed.fk_expedition = e.rowid AND';
2300 $sql .=
' ed.fk_origin_line = cd.rowid';
2301 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2302 if ($fk_product > 0) {
2303 $sql .=
' AND cd.fk_product = '.((int) $fk_product);
2305 if ($filtre_statut >= 0) {
2306 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
2308 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
2311 dol_syslog(get_class($this).
"::loadExpeditions", LOG_DEBUG);
2314 $num = $this->
db->num_rows(
$resql);
2317 $obj = $this->
db->fetch_object(
$resql);
2318 $this->expeditions[$obj->rowid] = $obj->qty;
2324 $this->error = $this->
db->lasterror();
2340 $sql =
'SELECT count(*)';
2341 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expedition as e';
2342 $sql .=
', '.MAIN_DB_PREFIX.
'element_element as el';
2343 $sql .=
' WHERE el.fk_source = '.((int) $this->
id);
2344 $sql .=
" AND el.sourcetype = 'commande'";
2345 $sql .=
" AND el.fk_target = e.rowid";
2346 $sql .=
" AND el.targettype = 'shipping'";
2350 $row = $this->
db->fetch_row(
$resql);
2369 $this->stocks = array();
2372 $array_of_product = array();
2376 if (count($array_of_product)) {
2377 $sql =
"SELECT fk_product, sum(ps.reel) as total";
2378 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_stock as ps";
2379 $sql .=
" WHERE ps.fk_product IN (".$this->db->sanitize(join(
',', $array_of_product)).
")";
2380 $sql .=
' GROUP BY fk_product';
2383 $num = $this->
db->num_rows(
$resql);
2386 $obj = $this->
db->fetch_object(
$resql);
2387 $this->stocks[$obj->fk_product] = $obj->total;
2405 if ($this->statut == self::STATUS_DRAFT) {
2408 $sql =
"SELECT fk_product, qty";
2409 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet";
2410 $sql .=
" WHERE rowid = ".((int) $lineid);
2412 $result = $this->
db->query($sql);
2414 $obj = $this->
db->fetch_object($result);
2418 $product->id = $obj->fk_product;
2424 $line->fetch($lineid);
2427 $staticline = clone $line;
2428 $line->oldline = $staticline;
2430 if ($line->delete($user) > 0) {
2434 $this->
db->commit();
2437 $this->
db->rollback();
2438 $this->error = $this->
db->lasterror();
2442 $this->
db->rollback();
2443 $this->error = $line->error;
2447 $this->
db->rollback();
2451 $this->
db->rollback();
2452 $this->error = $this->
db->lasterror();
2456 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
2475 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
2476 return $this->
setDiscount($user, $remise, $notrigger);
2489 $remise = trim($remise) ?trim($remise) : 0;
2491 if ($user->rights->commande->creer) {
2498 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2499 $sql .=
' SET remise_percent = '.((float) $remise);
2500 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.((int) self::STATUS_DRAFT);
2505 $this->errors[] = $this->
db->error();
2510 $this->oldcopy = clone $this;
2511 $this->remise_percent = $remise;
2515 if (!$notrigger && empty($error)) {
2525 $this->
db->commit();
2528 foreach ($this->errors as $errmsg) {
2529 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2530 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2532 $this->
db->rollback();
2551 if (empty($remise)) {
2557 if ($user->rights->commande->creer) {
2562 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2563 $sql .=
' SET remise_absolue = '.((float) $remise);
2569 $this->errors[] = $this->
db->error();
2574 $this->oldcopy = clone $this;
2575 $this->remise_absolue = $remise;
2579 if (!$notrigger && empty($error)) {
2589 $this->
db->commit();
2592 foreach ($this->errors as $errmsg) {
2593 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2594 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2596 $this->
db->rollback();
2615 if ($user->rights->commande->creer) {
2620 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2621 $sql .=
" SET date_commande = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
2622 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut = ".((int) self::STATUS_DRAFT);
2627 $this->errors[] = $this->
db->error();
2632 $this->oldcopy = clone $this;
2633 $this->date = $date;
2636 if (!$notrigger && empty($error)) {
2646 $this->
db->commit();
2649 foreach ($this->errors as $errmsg) {
2650 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2651 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2653 $this->
db->rollback();
2687 if ($user->rights->commande->creer) {
2692 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2693 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2694 $sql .=
" WHERE rowid = ".((int) $this->
id);
2699 $this->errors[] = $this->
db->error();
2704 $this->oldcopy = clone $this;
2705 $this->date_livraison = $delivery_date;
2706 $this->delivery_date = $delivery_date;
2709 if (!$notrigger && empty($error)) {
2719 $this->
db->commit();
2722 foreach ($this->errors as $errmsg) {
2723 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2724 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2726 $this->
db->rollback();
2748 public function liste_array($shortlist = 0, $draft = 0, $excluser =
'', $socid = 0, $limit = 0, $offset = 0, $sortfield =
'c.date_commande', $sortorder =
'DESC')
2755 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2756 $sql .=
" c.rowid as cid, c.ref";
2757 if (empty($user->rights->societe->client->voir) && !$socid) {
2758 $sql .=
", sc.fk_soc, sc.fk_user";
2760 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"commande as c";
2761 if (empty($user->rights->societe->client->voir) && !$socid) {
2762 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2764 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2765 $sql .=
" AND c.fk_soc = s.rowid";
2766 if (empty($user->rights->societe->client->voir) && !$socid) {
2767 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2770 $sql .=
" AND s.rowid = ".((int) $socid);
2773 $sql .=
" AND c.fk_statut = ".self::STATUS_DRAFT;
2775 if (is_object($excluser)) {
2776 $sql .=
" AND c.fk_user_author <> ".((int) $excluser->id);
2778 $sql .= $this->
db->order($sortfield, $sortorder);
2779 $sql .= $this->
db->plimit($limit, $offset);
2781 $result = $this->
db->query($sql);
2783 $numc = $this->
db->num_rows($result);
2786 while ($i < $numc) {
2787 $obj = $this->
db->fetch_object($result);
2789 if ($shortlist == 1) {
2790 $ga[$obj->cid] = $obj->ref;
2791 } elseif ($shortlist == 2) {
2792 $ga[$obj->cid] = $obj->ref.
' ('.$obj->name.
')';
2794 $ga[$i][
'id'] = $obj->cid;
2795 $ga[$i][
'ref'] = $obj->ref;
2796 $ga[$i][
'name'] = $obj->name;
2819 dol_syslog(
'Commande::availability('.$availability_id.
')');
2820 if ($this->statut >= self::STATUS_DRAFT) {
2825 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2826 $sql .=
' SET fk_availability = '.((int) $availability_id);
2827 $sql .=
' WHERE rowid='.((int) $this->
id);
2832 $this->errors[] = $this->
db->error();
2837 $this->oldcopy = clone $this;
2838 $this->availability_id = $availability_id;
2841 if (!$notrigger && empty($error)) {
2851 $this->
db->commit();
2854 foreach ($this->errors as $errmsg) {
2855 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2856 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2858 $this->
db->rollback();
2862 $error_str =
'Command status do not meet requirement '.$this->statut;
2864 $this->error = $error_str;
2865 $this->errors[] = $this->error;
2883 dol_syslog(
'Commande::demand_reason('.$demand_reason_id.
')');
2884 if ($this->statut >= self::STATUS_DRAFT) {
2889 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2890 $sql .=
' SET fk_input_reason = '.((int) $demand_reason_id);
2891 $sql .=
' WHERE rowid='.((int) $this->
id);
2896 $this->errors[] = $this->
db->error();
2901 $this->oldcopy = clone $this;
2902 $this->demand_reason_id = $demand_reason_id;
2905 if (!$notrigger && empty($error)) {
2915 $this->
db->commit();
2918 foreach ($this->errors as $errmsg) {
2919 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2920 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2922 $this->
db->rollback();
2926 $error_str =
'order status do not meet requirement '.$this->statut;
2928 $this->error = $error_str;
2929 $this->errors[] = $this->error;
2946 if ($user->rights->commande->creer) {
2951 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET';
2952 $sql .=
' ref_client = '.(empty($ref_client) ?
'NULL' :
"'".$this->db->escape($ref_client).
"'");
2953 $sql .=
' WHERE rowid = '.((int) $this->
id);
2955 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2958 $this->errors[] = $this->
db->error();
2963 $this->oldcopy = clone $this;
2964 $this->ref_client = $ref_client;
2967 if (!$notrigger && empty($error)) {
2976 $this->
db->commit();
2979 foreach ($this->errors as $errmsg) {
2980 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2981 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2983 $this->
db->rollback();
3002 if ($this->billed) {
3008 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 1';
3011 dol_syslog(get_class($this).
"::classifyBilled", LOG_DEBUG);
3012 if ($this->
db->query($sql)) {
3014 $this->oldcopy = clone $this;
3018 if (!$notrigger && empty($error)) {
3020 $result = $this->
call_trigger(
'ORDER_CLASSIFY_BILLED', $user);
3028 $this->
db->commit();
3031 foreach ($this->errors as $errmsg) {
3032 dol_syslog(get_class($this).
"::classifyBilled ".$errmsg, LOG_ERR);
3033 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3035 $this->
db->rollback();
3039 $this->error = $this->
db->error();
3040 $this->
db->rollback();
3058 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 0';
3061 dol_syslog(get_class($this).
"::classifyUnBilled", LOG_DEBUG);
3062 if ($this->
db->query($sql)) {
3064 $this->oldcopy = clone $this;
3068 if (!$notrigger && empty($error)) {
3070 $result = $this->
call_trigger(
'ORDER_CLASSIFY_UNBILLED', $user);
3080 $this->
db->commit();
3083 foreach ($this->errors as $errmsg) {
3084 dol_syslog(get_class($this).
"::classifyUnBilled ".$errmsg, LOG_ERR);
3085 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3087 $this->
db->rollback();
3091 $this->error = $this->
db->error();
3092 $this->
db->rollback();
3128 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)
3130 global $conf, $mysoc, $langs, $user;
3132 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");
3133 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3140 if (empty($info_bits)) {
3143 if (empty($txtva)) {
3146 if (empty($txlocaltax1)) {
3149 if (empty($txlocaltax2)) {
3152 if (empty($remise_percent)) {
3153 $remise_percent = 0;
3155 if (empty($special_code) || $special_code == 3) {
3158 if (empty($ref_ext)) {
3162 if ($date_start && $date_end && $date_start > $date_end) {
3163 $langs->load(
"errors");
3164 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3168 $remise_percent =
price2num($remise_percent);
3172 $pu_ht_devise =
price2num($pu_ht_devise);
3173 if (!preg_match(
'/\((.*)\)/', $txtva)) {
3191 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3192 $vat_src_code = $reg[1];
3193 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3196 $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);
3198 $total_ht = $tabprice[0];
3199 $total_tva = $tabprice[1];
3200 $total_ttc = $tabprice[2];
3201 $total_localtax1 = $tabprice[9];
3202 $total_localtax2 = $tabprice[10];
3203 $pu_ht = $tabprice[3];
3204 $pu_tva = $tabprice[4];
3205 $pu_ttc = $tabprice[5];
3208 $multicurrency_total_ht = $tabprice[16];
3209 $multicurrency_total_tva = $tabprice[17];
3210 $multicurrency_total_ttc = $tabprice[18];
3211 $pu_ht_devise = $tabprice[19];
3215 if ($price_base_type ==
'TTC') {
3216 $subprice = $pu_ttc;
3221 if ($remise_percent > 0) {
3222 $remise = round(($pu * $remise_percent / 100), 2);
3223 $price = ($pu - $remise);
3228 $line->fetch($rowid);
3229 $line->fetch_optionals();
3231 if (!empty($line->fk_product)) {
3233 $result = $product->fetch($line->fk_product);
3234 $product_type = $product->type;
3236 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) {
3237 $langs->load(
"errors");
3238 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
3239 $this->errors[] = $this->error;
3241 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
3243 $this->
db->rollback();
3248 $staticline = clone $line;
3250 $line->oldline = $staticline;
3251 $this->line = $line;
3252 $this->line->context = $this->context;
3253 $this->line->rang = $rang;
3256 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
3257 $rangmax = $this->
line_max($fk_parent_line);
3258 $this->line->rang = $rangmax + 1;
3261 $this->line->id = $rowid;
3262 $this->line->label = $label;
3263 $this->line->desc = $desc;
3264 $this->line->qty = $qty;
3265 $this->line->ref_ext = $ref_ext;
3267 $this->line->vat_src_code = $vat_src_code;
3268 $this->line->tva_tx = $txtva;
3269 $this->line->localtax1_tx = $txlocaltax1;
3270 $this->line->localtax2_tx = $txlocaltax2;
3271 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3272 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3273 $this->line->remise_percent = $remise_percent;
3274 $this->line->subprice = $subprice;
3275 $this->line->info_bits = $info_bits;
3276 $this->line->special_code = $special_code;
3277 $this->line->total_ht = $total_ht;
3278 $this->line->total_tva = $total_tva;
3279 $this->line->total_localtax1 = $total_localtax1;
3280 $this->line->total_localtax2 = $total_localtax2;
3281 $this->line->total_ttc = $total_ttc;
3282 $this->line->date_start = $date_start;
3283 $this->line->date_end = $date_end;
3284 $this->line->product_type = $type;
3285 $this->line->fk_parent_line = $fk_parent_line;
3286 $this->line->skip_update_total = $skip_update_total;
3287 $this->line->fk_unit = $fk_unit;
3289 $this->line->fk_fournprice = $fk_fournprice;
3290 $this->line->pa_ht = $pa_ht;
3293 $this->line->multicurrency_subprice = $pu_ht_devise;
3294 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
3295 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
3296 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
3299 $this->line->price = $price;
3301 if (is_array($array_options) && count($array_options) > 0) {
3303 foreach ($array_options as $key => $value) {
3304 $this->line->array_options[$key] = $array_options[$key];
3308 $result = $this->line->update($user, $notrigger);
3311 if (!empty($fk_parent_line)) {
3318 $this->
db->commit();
3321 $this->error = $this->line->error;
3323 $this->
db->rollback();
3327 $this->error = get_class($this).
"::updateline Order status makes operation forbidden";
3328 $this->errors = array(
'OrderStatusMakeOperationForbidden');
3347 if (isset($this->
ref)) {
3348 $this->
ref = trim($this->
ref);
3350 if (isset($this->ref_client)) {
3351 $this->ref_client = trim($this->ref_client);
3353 if (isset($this->note) || isset($this->note_private)) {
3354 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
3356 if (isset($this->note_public)) {
3357 $this->note_public = trim($this->note_public);
3359 if (isset($this->model_pdf)) {
3360 $this->model_pdf = trim($this->model_pdf);
3362 if (isset($this->import_key)) {
3363 $this->import_key = trim($this->import_key);
3365 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
3371 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande SET";
3373 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
3374 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
3375 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
3376 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
3377 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
3378 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
3379 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
3380 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
3381 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
3382 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
3383 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
3384 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
3385 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id :
"null").
",";
3386 $sql .=
" fk_user_valid=".((isset($this->user_valid) && $this->user_valid > 0) ? $this->user_valid :
"null").
",";
3387 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
3388 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
3389 $sql .=
" deposit_percent=".(! empty($this->deposit_percent) ? strval($this->deposit_percent) :
"null").
",";
3390 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
3391 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
3392 $sql .=
" fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id :
"null").
",";
3393 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
3394 $sql .=
" fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id :
"null").
",";
3395 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
3396 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
3397 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
3398 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
"";
3400 $sql .=
" WHERE rowid=".((int) $this->
id);
3404 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3407 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
3417 if (!$error && !$notrigger) {
3428 foreach ($this->errors as $errmsg) {
3429 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3430 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3432 $this->
db->rollback();
3435 $this->
db->commit();
3447 public function delete($user, $notrigger = 0)
3449 global $conf, $langs;
3450 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3454 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
3469 $this->errors[] = $langs->trans(
'SomeShipmentExists');
3474 if (!$error && !empty($this->table_element_line)) {
3475 $tabletodelete = $this->table_element_line;
3476 $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).
")";
3477 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3478 if (!$this->
db->query($sqlef) || !$this->
db->query($sql)) {
3480 $this->error = $this->
db->lasterror();
3481 $this->errors[] = $this->error;
3482 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3507 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3513 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
3514 $res = $this->
db->query($sql);
3517 $this->error = $this->
db->lasterror();
3518 $this->errors[] = $this->error;
3519 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3534 if ($conf->commande->multidir_output[$this->entity] && !empty($this->
ref)) {
3535 $dir = $conf->commande->multidir_output[$this->entity].
"/".$ref;
3536 $file = $dir.
"/".$ref.
".pdf";
3537 if (file_exists($file)) {
3541 $this->error =
'ErrorFailToDeleteFile';
3542 $this->errors[] = $this->error;
3543 $this->
db->rollback();
3547 if (file_exists($dir)) {
3550 $this->error =
'ErrorFailToDeleteDir';
3551 $this->errors[] = $this->error;
3552 $this->
db->rollback();
3560 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3561 $this->
db->commit();
3564 $this->
db->rollback();
3580 global $conf, $langs;
3584 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.date_livraison as delivery_date, c.fk_statut, c.total_ht";
3585 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as c";
3586 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3587 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
3588 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3591 $sql .= $clause.
" c.entity IN (".
getEntity(
'commande').
")";
3593 $sql .=
" AND ((c.fk_statut IN (".self::STATUS_VALIDATED.
",".self::STATUS_SHIPMENTONPROCESS.
")) OR (c.fk_statut = ".self::STATUS_CLOSED.
" AND c.facture = 0))";
3595 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3601 $response->warning_delay = $conf->commande->client->warning_delay / 60 / 60 / 24;
3602 $response->label = $langs->trans(
"OrdersToProcess");
3603 $response->labelShort = $langs->trans(
"Opened");
3604 $response->url = DOL_URL_ROOT.
'/commande/list.php?search_status=-3&mainmenu=commercial&leftmenu=orders';
3609 while ($obj = $this->
db->fetch_object(
$resql)) {
3610 $response->nbtodo++;
3611 $response->total += $obj->total_ht;
3613 $generic_commande->statut = $obj->fk_statut;
3614 $generic_commande->date_commande = $this->
db->jdate($obj->date_commande);
3615 $generic_commande->date = $this->
db->jdate($obj->date_commande);
3616 $generic_commande->date_livraison = $this->
db->jdate($obj->delivery_date);
3617 $generic_commande->delivery_date = $this->
db->jdate($obj->delivery_date);
3619 if ($generic_commande->hasDelay()) {
3620 $response->nbtodolate++;
3626 $this->error = $this->
db->error();
3640 $label = $langs->trans(
'OrderSource'.$this->source);
3642 if ($label ==
'OrderSource') {
3656 return $this->
LibStatut($this->statut, $this->billed, $mode);
3669 public function LibStatut($status, $billed, $mode, $donotshowbilled = 0)
3672 global $langs, $conf;
3675 if (empty($donotshowbilled)) {
3676 $billedtext .= ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3681 if ($status == self::STATUS_CANCELED) {
3682 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderCanceled');
3683 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderCanceledShort');
3684 $statusType =
'status9';
3685 } elseif ($status == self::STATUS_DRAFT) {
3686 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDraft');
3687 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDraftShort');
3688 $statusType =
'status0';
3689 } elseif ($status == self::STATUS_VALIDATED) {
3690 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderValidated').$billedtext;
3691 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderValidatedShort').$billedtext;
3692 $statusType =
'status1';
3693 } elseif ($status == self::STATUS_SHIPMENTONPROCESS) {
3694 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderSent').$billedtext;
3695 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderSentShort').$billedtext;
3696 $labelTooltip = $langs->transnoentitiesnoconv(
"StatusOrderSent").
' - '.$langs->transnoentitiesnoconv(
"DateDeliveryPlanned").dol_print_date($this->date_livraison).$billedtext;
3697 $statusType =
'status4';
3698 } elseif ($status == self::STATUS_CLOSED && (!$billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3699 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderToBill');
3700 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderToBillShort');
3701 $statusType =
'status4';
3702 } elseif ($status == self::STATUS_CLOSED && ($billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3703 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderProcessed').$billedtext;
3704 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderProcessedShort').$billedtext;
3705 $statusType =
'status6';
3706 } elseif ($status == self::STATUS_CLOSED && (!empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3707 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDelivered');
3708 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDeliveredShort');
3709 $statusType =
'status6';
3711 $labelStatus = $langs->transnoentitiesnoconv(
'Unknown');
3712 $labelStatusShort =
'';
3717 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', array(
'tooltip' => $labelTooltip));
3734 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0, $target =
'')
3736 global $conf, $langs, $user, $hookmanager;
3738 if (!empty($conf->dol_no_mouse_hover)) {
3744 if (!empty($conf->expedition->enabled) && ($option ==
'1' || $option ==
'2')) {
3745 $url = DOL_URL_ROOT.
'/expedition/shipment.php?id='.$this->id;
3747 $url = DOL_URL_ROOT.
'/commande/card.php?id='.$this->id;
3750 if (!$user->rights->commande->lire) {
3754 if ($option !==
'nolink') {
3756 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3757 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
3758 $add_save_lastsearch_values = 1;
3760 if ($add_save_lastsearch_values) {
3761 $url .=
'&save_lastsearch_values=1';
3771 if ($user->rights->commande->lire) {
3772 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Order").
'</u>';
3773 if (isset($this->statut)) {
3774 $label .=
' '.$this->getLibStatut(5);
3776 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
3777 $label .=
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.(empty($this->ref_customer) ? (empty($this->ref_client) ?
'' : $this->ref_client) : $this->ref_customer);
3778 if (!empty($this->total_ht)) {
3779 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
3781 if (!empty($this->total_tva)) {
3782 $label .=
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
3784 if (!empty($this->total_ttc)) {
3785 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
3787 if (!empty($this->date)) {
3788 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
3790 if (!empty($this->delivery_date)) {
3791 $label .=
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
3796 if (empty($notooltip) && $user->rights->commande->lire) {
3797 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
3798 $label = $langs->trans(
"Order");
3799 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
3801 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
3802 $linkclose .=
' class="classfortooltip"';
3804 $target_value=array(
'_self',
'_blank',
'_parent',
'_top');
3805 if (in_array($target, $target_value)) {
3806 $linkclose .=
' target="'.dol_escape_htmltag($target).
'"';
3810 $linkstart =
'<a href="'.$url.
'"';
3811 $linkstart .= $linkclose.
'>';
3814 if ($option ===
'nolink') {
3819 $result .= $linkstart;
3821 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
3823 if ($withpicto != 2) {
3824 $result .= $this->ref;
3826 $result .= $linkend;
3828 if ($addlinktonotes) {
3829 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3832 $result .=
' <span class="note inline-block">';
3833 $result .=
'<a href="'.DOL_URL_ROOT.
'/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3838 $result .=
'</span>';
3843 $hookmanager->initHooks(array($this->element .
'dao'));
3844 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
3845 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3847 $result = $hookmanager->resPrint;
3849 $result .= $hookmanager->resPrint;
3863 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem,';
3864 $sql .=
' date_valid as datev,';
3865 $sql .=
' date_cloture as datecloture,';
3866 $sql .=
' fk_user_author, fk_user_valid, fk_user_cloture';
3867 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
3868 $sql .=
' WHERE c.rowid = '.((int) $id);
3869 $result = $this->
db->query($sql);
3871 if ($this->
db->num_rows($result)) {
3872 $obj = $this->
db->fetch_object($result);
3873 $this->
id = $obj->rowid;
3874 if ($obj->fk_user_author) {
3875 $this->user_creation_id = $obj->fk_user_author;
3877 if ($obj->fk_user_valid) {
3878 $this->user_validation_id = $obj->fk_user_valid;
3880 if ($obj->fk_user_cloture) {
3881 $this->user_closing_id = $obj->fk_user_cloture;
3884 $this->date_creation = $this->
db->jdate($obj->datec);
3885 $this->date_modification = $this->
db->jdate($obj->datem);
3886 $this->date_validation = $this->
db->jdate($obj->datev);
3887 $this->date_cloture = $this->
db->jdate($obj->datecloture);
3890 $this->
db->free($result);
3906 global $conf, $langs;
3908 dol_syslog(get_class($this).
"::initAsSpecimen");
3913 $sql =
"SELECT rowid";
3914 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
3915 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3916 $sql .= $this->
db->plimit(100);
3920 $num_prods = $this->
db->num_rows(
$resql);
3922 while ($i < $num_prods) {
3924 $row = $this->
db->fetch_row(
$resql);
3925 $prodids[$i] = $row[0];
3931 $this->
ref =
'SPECIMEN';
3932 $this->specimen = 1;
3934 $this->date = time();
3935 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
3936 $this->cond_reglement_code =
'RECEP';
3937 $this->mode_reglement_code =
'CHQ';
3938 $this->availability_code =
'DSP';
3939 $this->demand_reason_code =
'SRC_00';
3941 $this->note_public =
'This is a comment (public)';
3942 $this->note_private =
'This is a comment (private)';
3944 $this->multicurrency_tx = 1;
3945 $this->multicurrency_code = $conf->currency;
3950 while ($xnbp < $nbp) {
3953 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
3955 $line->subprice = 100;
3959 $line->total_ht = 50;
3960 $line->total_ttc = 60;
3961 $line->total_tva = 10;
3962 $line->remise_percent = 50;
3964 $line->total_ht = 100;
3965 $line->total_ttc = 120;
3966 $line->total_tva = 20;
3967 $line->remise_percent = 0;
3969 if ($num_prods > 0) {
3970 $prodid = mt_rand(1, $num_prods);
3971 $line->fk_product = $prodids[$prodid];
3972 $line->product_ref =
'SPECIMEN';
3975 $this->lines[$xnbp] = $line;
3977 $this->total_ht += $line->total_ht;
3978 $this->total_tva += $line->total_tva;
3979 $this->total_ttc += $line->total_ttc;
3997 $this->nb = array();
4000 $sql =
"SELECT count(co.rowid) as nb";
4001 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as co";
4002 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
4003 if (empty($user->rights->societe->client->voir) && !$user->socid) {
4004 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
4005 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
4008 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'commande').
")";
4012 while ($obj = $this->
db->fetch_object(
$resql)) {
4013 $this->nb[
"orders"] = $obj->nb;
4019 $this->error = $this->
db->error();
4045 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
4047 global $conf, $langs;
4049 $langs->load(
"orders");
4050 $outputlangs->load(
"products");
4053 $modele =
'einstein';
4055 if (!empty($this->model_pdf)) {
4056 $modele = $this->model_pdf;
4057 } elseif (!empty($this->modelpdf)) {
4058 $modele = $this->modelpdf;
4059 } elseif (!empty($conf->global->COMMANDE_ADDON_PDF)) {
4060 $modele = $conf->global->COMMANDE_ADDON_PDF;
4064 $modelpath =
"core/modules/commande/doc/";
4066 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
4119 return max($this->date, $this->date_livraison) < ($now - $conf->commande->client->warning_delay);
4129 global $conf, $langs;
4131 if (empty($this->date_livraison)) {
4132 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
4134 $text = $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->date_livraison,
'day');
4136 $text .=
' '.($conf->commande->client->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->client->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
4151 public $element =
'commandedet';
4153 public $table_element =
'commandedet';
4161 public $fk_commande;
4169 public $commande_id;
4171 public $fk_parent_line;
4183 public $fk_remise_except;
4189 public $fk_fournprice;
4209 public $skip_update_total;
4230 $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,';
4231 $sql .=
' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
4232 $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,';
4233 $sql .=
' cd.fk_unit,';
4234 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
4235 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
4236 $sql .=
' cd.date_start, cd.date_end, cd.vat_src_code';
4237 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as cd';
4238 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
4239 $sql .=
' WHERE cd.rowid = '.((int) $rowid);
4240 $result = $this->
db->query($sql);
4242 $objp = $this->
db->fetch_object($result);
4243 $this->
rowid = $objp->rowid;
4244 $this->
id = $objp->rowid;
4245 $this->fk_commande = $objp->fk_commande;
4246 $this->fk_parent_line = $objp->fk_parent_line;
4247 $this->label = $objp->custom_label;
4248 $this->desc = $objp->description;
4249 $this->qty = $objp->qty;
4250 $this->
price = $objp->price;
4251 $this->subprice = $objp->subprice;
4252 $this->ref_ext = $objp->ref_ext;
4253 $this->vat_src_code = $objp->vat_src_code;
4254 $this->tva_tx = $objp->tva_tx;
4255 $this->localtax1_tx = $objp->localtax1_tx;
4256 $this->localtax2_tx = $objp->localtax2_tx;
4257 $this->remise = $objp->remise;
4258 $this->remise_percent = $objp->remise_percent;
4259 $this->fk_remise_except = $objp->fk_remise_except;
4260 $this->fk_product = $objp->fk_product;
4261 $this->product_type = $objp->product_type;
4262 $this->info_bits = $objp->info_bits;
4263 $this->special_code = $objp->special_code;
4264 $this->total_ht = $objp->total_ht;
4265 $this->total_tva = $objp->total_tva;
4266 $this->total_localtax1 = $objp->total_localtax1;
4267 $this->total_localtax2 = $objp->total_localtax2;
4268 $this->total_ttc = $objp->total_ttc;
4269 $this->fk_fournprice = $objp->fk_fournprice;
4270 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
4271 $this->pa_ht = $marginInfos[0];
4272 $this->marge_tx = $marginInfos[1];
4273 $this->marque_tx = $marginInfos[2];
4274 $this->special_code = $objp->special_code;
4275 $this->rang = $objp->rang;
4277 $this->
ref = $objp->product_ref;
4279 $this->product_ref = $objp->product_ref;
4280 $this->product_label = $objp->product_label;
4281 $this->product_desc = $objp->product_desc;
4282 $this->product_tobatch = $objp->product_tobatch;
4283 $this->fk_unit = $objp->fk_unit;
4285 $this->date_start = $this->
db->jdate($objp->date_start);
4286 $this->date_end = $this->
db->jdate($objp->date_end);
4288 $this->fk_multicurrency = $objp->fk_multicurrency;
4289 $this->multicurrency_code = $objp->multicurrency_code;
4290 $this->multicurrency_subprice = $objp->multicurrency_subprice;
4291 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
4292 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
4293 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
4295 $this->
db->free($result);
4299 $this->error = $this->
db->lasterror();
4311 public function delete(
User $user, $notrigger = 0)
4313 global $conf, $langs;
4317 if (empty($this->
id) && !empty($this->
rowid)) {
4318 $this->
id = $this->rowid;
4322 $sqlCheckShipmentLine =
"SELECT";
4323 $sqlCheckShipmentLine .=
" ed.rowid";
4324 $sqlCheckShipmentLine .=
" FROM ".MAIN_DB_PREFIX.
"expeditiondet ed";
4325 $sqlCheckShipmentLine .=
" WHERE ed.fk_origin_line = ".((int) $this->
id);
4327 $resqlCheckShipmentLine = $this->
db->query($sqlCheckShipmentLine);
4328 if (!$resqlCheckShipmentLine) {
4330 $this->error = $this->
db->lasterror();
4331 $this->errors[] = $this->error;
4333 $langs->load(
'errors');
4334 $num = $this->
db->num_rows($resqlCheckShipmentLine);
4337 $objCheckShipmentLine = $this->
db->fetch_object($resqlCheckShipmentLine);
4338 $this->error = $langs->trans(
'ErrorRecordAlreadyExists').
' : '.$langs->trans(
'ShipmentLine').
' '.$objCheckShipmentLine->rowid;
4339 $this->errors[] = $this->error;
4341 $this->
db->free($resqlCheckShipmentLine);
4344 dol_syslog(__METHOD__.
'Error ; '.$this->error, LOG_ERR);
4350 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"commandedet WHERE rowid = ".((int) $this->
id);
4357 $this->
id = $this->rowid;
4361 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
4365 if (!$error && !$notrigger) {
4367 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
4375 $this->
db->commit();
4379 foreach ($this->errors as $errmsg) {
4380 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
4381 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4383 $this->
db->rollback();
4386 $this->error = $this->
db->lasterror();
4398 public function insert($user =
null, $notrigger = 0)
4400 global $langs, $conf;
4404 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4406 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
4409 if (empty($this->tva_tx)) {
4412 if (empty($this->localtax1_tx)) {
4413 $this->localtax1_tx = 0;
4415 if (empty($this->localtax2_tx)) {
4416 $this->localtax2_tx = 0;
4418 if (empty($this->localtax1_type)) {
4419 $this->localtax1_type = 0;
4421 if (empty($this->localtax2_type)) {
4422 $this->localtax2_type = 0;
4424 if (empty($this->total_localtax1)) {
4425 $this->total_localtax1 = 0;
4427 if (empty($this->total_localtax2)) {
4428 $this->total_localtax2 = 0;
4430 if (empty($this->rang)) {
4433 if (empty($this->remise_percent)) {
4434 $this->remise_percent = 0;
4436 if (empty($this->info_bits)) {
4437 $this->info_bits = 0;
4439 if (empty($this->special_code)) {
4440 $this->special_code = 0;
4442 if (empty($this->fk_parent_line)) {
4443 $this->fk_parent_line = 0;
4445 if (empty($this->pa_ht)) {
4448 if (empty($this->ref_ext)) {
4449 $this->ref_ext =
'';
4453 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4454 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4458 $this->pa_ht = $result;
4463 if ($this->product_type < 0) {
4470 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'commandedet';
4471 $sql .=
' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
4472 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
4473 $sql .=
' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
4474 $sql .=
' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
4475 $sql .=
' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
4477 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
4479 $sql .=
" VALUES (".$this->fk_commande.
",";
4480 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
4481 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
4482 $sql .=
" '".$this->db->escape($this->desc).
"',";
4483 $sql .=
" '".price2num($this->qty).
"',";
4484 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
4485 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
4486 $sql .=
" '".price2num($this->tva_tx).
"',";
4487 $sql .=
" '".price2num($this->localtax1_tx).
"',";
4488 $sql .=
" '".price2num($this->localtax2_tx).
"',";
4489 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
4490 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
4491 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
4492 $sql .=
" '".$this->db->escape($this->product_type).
"',";
4493 $sql .=
" '".price2num($this->remise_percent).
"',";
4494 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice) :
"null").
",";
4495 $sql .=
" ".($this->price !=
'' ?
"'".price2num($this->
price).
"'" :
"null").
",";
4496 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
4497 $sql .=
' '.((int) $this->special_code).
',';
4498 $sql .=
' '.((int) $this->rang).
',';
4499 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
4500 $sql .=
' '.price2num($this->pa_ht).
',';
4501 $sql .=
" ".((int) $this->info_bits).
",";
4502 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
4503 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
4504 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
4505 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
4506 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
4507 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
4508 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
',';
4509 $sql .=
' '.(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
4510 $sql .=
", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) :
'NULL');
4511 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
4512 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
4513 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
4514 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
4515 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
4518 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
4521 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
4522 $this->
rowid = $this->id;
4531 if (!$error && !$notrigger) {
4533 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
4541 $this->
db->commit();
4545 foreach ($this->errors as $errmsg) {
4546 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
4547 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4549 $this->
db->rollback();
4552 $this->error = $this->
db->error();
4553 $this->
db->rollback();
4567 global $conf, $langs;
4571 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4574 if (empty($this->tva_tx)) {
4577 if (empty($this->localtax1_tx)) {
4578 $this->localtax1_tx = 0;
4580 if (empty($this->localtax2_tx)) {
4581 $this->localtax2_tx = 0;
4583 if (empty($this->localtax1_type)) {
4584 $this->localtax1_type = 0;
4586 if (empty($this->localtax2_type)) {
4587 $this->localtax2_type = 0;
4589 if (empty($this->qty)) {
4592 if (empty($this->total_localtax1)) {
4593 $this->total_localtax1 = 0;
4595 if (empty($this->total_localtax2)) {
4596 $this->total_localtax2 = 0;
4598 if (empty($this->marque_tx)) {
4599 $this->marque_tx = 0;
4601 if (empty($this->marge_tx)) {
4602 $this->marge_tx = 0;
4604 if (empty($this->remise_percent)) {
4605 $this->remise_percent = 0;
4607 if (empty($this->remise)) {
4610 if (empty($this->info_bits)) {
4611 $this->info_bits = 0;
4613 if (empty($this->special_code)) {
4614 $this->special_code = 0;
4616 if (empty($this->product_type)) {
4617 $this->product_type = 0;
4619 if (empty($this->fk_parent_line)) {
4620 $this->fk_parent_line = 0;
4622 if (empty($this->pa_ht)) {
4625 if (empty($this->ref_ext)) {
4626 $this->ref_ext =
'';
4630 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4631 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4635 $this->pa_ht = $result;
4642 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4643 $sql .=
" description='".$this->db->escape($this->desc).
"'";
4644 $sql .=
" , label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
4645 $sql .=
" , vat_src_code=".(!empty($this->vat_src_code) ?
"'".$this->db->escape($this->vat_src_code).
"'" :
"''");
4646 $sql .=
" , tva_tx=".price2num($this->tva_tx);
4647 $sql .=
" , localtax1_tx=".price2num($this->localtax1_tx);
4648 $sql .=
" , localtax2_tx=".price2num($this->localtax2_tx);
4649 $sql .=
" , localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
4650 $sql .=
" , localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
4651 $sql .=
" , qty=".price2num($this->qty);
4652 $sql .=
" , ref_ext='".$this->db->escape($this->ref_ext).
"'";
4653 $sql .=
" , subprice=".price2num($this->subprice).
"";
4654 $sql .=
" , remise_percent=".price2num($this->remise_percent).
"";
4655 $sql .=
" , price=".price2num($this->
price).
"";
4656 $sql .=
" , remise=".price2num($this->remise).
"";
4657 if (empty($this->skip_update_total)) {
4658 $sql .=
" , total_ht=".price2num($this->total_ht).
"";
4659 $sql .=
" , total_tva=".price2num($this->total_tva).
"";
4660 $sql .=
" , total_ttc=".price2num($this->total_ttc).
"";
4661 $sql .=
" , total_localtax1=".price2num($this->total_localtax1);
4662 $sql .=
" , total_localtax2=".price2num($this->total_localtax2);
4664 $sql .=
" , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null");
4665 $sql .=
" , buy_price_ht='".price2num($this->pa_ht).
"'";
4666 $sql .=
" , info_bits=".((int) $this->info_bits);
4667 $sql .=
" , special_code=".((int) $this->special_code);
4668 $sql .=
" , date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
4669 $sql .=
" , date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
4670 $sql .=
" , product_type=".$this->product_type;
4671 $sql .=
" , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line :
"null");
4672 if (!empty($this->rang)) {
4673 $sql .=
", rang=".((int) $this->rang);
4675 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
4678 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice).
"";
4679 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht).
"";
4680 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva).
"";
4681 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).
"";
4683 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4685 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
4689 $this->
id = $this->rowid;
4696 if (!$error && !$notrigger) {
4698 $result = $this->
call_trigger(
'LINEORDER_MODIFY', $user);
4706 $this->
db->commit();
4710 foreach ($this->errors as $errmsg) {
4711 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
4712 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4714 $this->
db->rollback();
4717 $this->error = $this->
db->error();
4718 $this->
db->rollback();
4736 if (empty($this->total_localtax1)) {
4737 $this->total_localtax1 = 0;
4739 if (empty($this->total_localtax2)) {
4740 $this->total_localtax2 = 0;
4744 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4745 $sql .=
" total_ht='".price2num($this->total_ht).
"'";
4746 $sql .=
",total_tva='".price2num($this->total_tva).
"'";
4747 $sql .=
",total_localtax1='".price2num($this->total_localtax1).
"'";
4748 $sql .=
",total_localtax2='".price2num($this->total_localtax2).
"'";
4749 $sql .=
",total_ttc='".price2num($this->total_ttc).
"'";
4750 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4752 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
4756 $this->
db->commit();
4759 $this->error = $this->
db->error();
4760 $this->
db->rollback();