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);
598 $this->statut = self::STATUS_VALIDATED;
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);
682 $this->statut = self::STATUS_DRAFT;
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();
738 $this->statut = self::STATUS_VALIDATED;
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);
782 $sql .=
" WHERE rowid = ".((int) $this->
id).
' AND fk_statut > '.self::STATUS_DRAFT;
784 if ($this->
db->query($sql)) {
795 $this->statut = self::STATUS_CLOSED;
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;
867 $this->statut = self::STATUS_CANCELED;
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)) {
921 $result = self::isExistingObject($this->element, 0, $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;
1233 $this->statut = self::STATUS_DRAFT;
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);
1288 unset($this->context[
'createfromclone']);
1292 $this->
db->commit();
1295 $this->
db->rollback();
1310 global $conf, $hookmanager;
1317 $this->date_commande =
dol_now();
1320 $num = count($object->lines);
1321 for ($i = 0; $i < $num; $i++) {
1324 $line->libelle = $object->lines[$i]->libelle;
1325 $line->label = $object->lines[$i]->label;
1326 $line->desc = $object->lines[$i]->desc;
1327 $line->price = $object->lines[$i]->price;
1328 $line->subprice = $object->lines[$i]->subprice;
1329 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1330 $line->tva_tx = $object->lines[$i]->tva_tx;
1331 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1332 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1333 $line->qty = $object->lines[$i]->qty;
1334 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1335 $line->remise_percent = $object->lines[$i]->remise_percent;
1336 $line->fk_product = $object->lines[$i]->fk_product;
1337 $line->info_bits = $object->lines[$i]->info_bits;
1338 $line->product_type = $object->lines[$i]->product_type;
1339 $line->rang = $object->lines[$i]->rang;
1340 $line->special_code = $object->lines[$i]->special_code;
1341 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1342 $line->fk_unit = $object->lines[$i]->fk_unit;
1344 $line->date_start = $object->lines[$i]->date_start;
1345 $line->date_end = $object->lines[$i]->date_end;
1347 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1348 $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);
1349 $line->pa_ht = $marginInfos[0];
1350 $line->marge_tx = $marginInfos[1];
1351 $line->marque_tx = $marginInfos[2];
1354 $object->lines[$i]->fetch_optionals();
1355 foreach ($object->lines[$i]->array_options as $options_key => $value) {
1356 $line->array_options[$options_key] = $value;
1359 $this->lines[$i] = $line;
1362 $this->entity = $object->entity;
1363 $this->socid = $object->socid;
1364 $this->fk_project = $object->fk_project;
1365 $this->cond_reglement_id = $object->cond_reglement_id;
1366 $this->deposit_percent = $object->deposit_percent;
1367 $this->mode_reglement_id = $object->mode_reglement_id;
1368 $this->fk_account = $object->fk_account;
1369 $this->availability_id = $object->availability_id;
1370 $this->demand_reason_id = $object->demand_reason_id;
1371 $this->date_livraison = $object->date_livraison;
1372 $this->delivery_date = $object->date_livraison;
1373 $this->shipping_method_id = $object->shipping_method_id;
1374 $this->warehouse_id = $object->warehouse_id;
1375 $this->fk_delivery_address = $object->fk_delivery_address;
1376 $this->contact_id = $object->contact_id;
1377 $this->ref_client = $object->ref_client;
1379 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1380 $this->note_private = $object->note_private;
1381 $this->note_public = $object->note_public;
1384 $this->origin = $object->element;
1385 $this->origin_id = $object->id;
1388 $object->fetch_optionals();
1391 $element_extrafields = $e->fetch_name_optionals_label($this->table_element);
1393 foreach ($object->array_options as $options_key => $value) {
1394 if (array_key_exists(str_replace(
'options_',
'', $options_key), $element_extrafields)) {
1395 $this->array_options[$options_key] = $value;
1399 $this->linked_objects[$this->origin] = $this->origin_id;
1400 if (isset($object->other_linked_objects) && is_array($object->other_linked_objects) && !empty($object->other_linked_objects)) {
1401 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1404 $ret = $this->
create($user);
1408 $hookmanager->initHooks(array(
'orderdao'));
1410 $parameters = array(
'objFrom'=>$object);
1412 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1419 if (!empty($conf->global->ORDER_VALID_AFTER_CLOSE_PROPAL)) {
1421 $this->
valid($user);
1473 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)
1475 global $mysoc, $conf, $langs, $user;
1477 $logtext =
"::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent";
1478 $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";
1479 $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";
1480 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
1482 if ($this->statut == self::STATUS_DRAFT) {
1483 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1487 if (empty($remise_percent)) {
1488 $remise_percent = 0;
1493 if (empty($info_bits)) {
1499 if (empty($txtva)) {
1502 if (empty($txlocaltax1)) {
1505 if (empty($txlocaltax2)) {
1508 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1509 $fk_parent_line = 0;
1511 if (empty($this->fk_multicurrency)) {
1512 $this->fk_multicurrency = 0;
1514 if (empty($ref_ext)) {
1518 $remise_percent =
price2num($remise_percent);
1521 $pu_ht_devise =
price2num($pu_ht_devise);
1524 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1529 if ($price_base_type ==
'HT') {
1534 $label = trim($label);
1535 $desc = trim($desc);
1542 if ($date_start && $date_end && $date_start > $date_end) {
1543 $langs->load(
"errors");
1544 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1550 $product_type = $type;
1551 if (!empty($fk_product) && $fk_product > 0) {
1553 $result = $product->fetch($fk_product);
1554 $product_type = $product->type;
1556 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) {
1557 $langs->load(
"errors");
1558 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
1559 $this->errors[] = $this->error;
1560 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
1561 $this->
db->rollback();
1562 return self::STOCK_NOT_ENOUGH_FOR_ORDER;
1575 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1576 $vat_src_code = $reg[1];
1577 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1580 $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);
1590 $total_ht = $tabprice[0];
1591 $total_tva = $tabprice[1];
1592 $total_ttc = $tabprice[2];
1593 $total_localtax1 = $tabprice[9];
1594 $total_localtax2 = $tabprice[10];
1595 $pu_ht = $tabprice[3];
1598 $multicurrency_total_ht = $tabprice[16];
1599 $multicurrency_total_tva = $tabprice[17];
1600 $multicurrency_total_ttc = $tabprice[18];
1601 $pu_ht_devise = $tabprice[19];
1605 if ($ranktouse == -1) {
1606 $rangmax = $this->
line_max($fk_parent_line);
1607 $ranktouse = $rangmax + 1;
1614 if ($remise_percent > 0) {
1615 $remise = round(($pu * $remise_percent / 100), 2);
1616 $price = $pu - $remise;
1622 $this->line->context = $this->context;
1624 $this->line->fk_commande = $this->id;
1625 $this->line->label = $label;
1626 $this->line->desc = $desc;
1627 $this->line->qty = $qty;
1628 $this->line->ref_ext = $ref_ext;
1630 $this->line->vat_src_code = $vat_src_code;
1631 $this->line->tva_tx = $txtva;
1632 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1633 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1634 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1635 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1636 $this->line->fk_product = $fk_product;
1637 $this->line->product_type = $product_type;
1638 $this->line->fk_remise_except = $fk_remise_except;
1639 $this->line->remise_percent = $remise_percent;
1640 $this->line->subprice = $pu_ht;
1641 $this->line->rang = $ranktouse;
1642 $this->line->info_bits = $info_bits;
1643 $this->line->total_ht = $total_ht;
1644 $this->line->total_tva = $total_tva;
1645 $this->line->total_localtax1 = $total_localtax1;
1646 $this->line->total_localtax2 = $total_localtax2;
1647 $this->line->total_ttc = $total_ttc;
1648 $this->line->special_code = $special_code;
1649 $this->line->origin = $origin;
1650 $this->line->origin_id = $origin_id;
1651 $this->line->fk_parent_line = $fk_parent_line;
1652 $this->line->fk_unit = $fk_unit;
1654 $this->line->date_start = $date_start;
1655 $this->line->date_end = $date_end;
1657 $this->line->fk_fournprice = $fk_fournprice;
1658 $this->line->pa_ht = $pa_ht;
1661 $this->line->fk_multicurrency = $this->fk_multicurrency;
1662 $this->line->multicurrency_code = $this->multicurrency_code;
1663 $this->line->multicurrency_subprice = $pu_ht_devise;
1664 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1665 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1666 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1669 $this->line->price = $price;
1671 if (is_array($array_options) && count($array_options) > 0) {
1672 $this->line->array_options = $array_options;
1675 $result = $this->line->insert($user);
1678 if (!empty($fk_parent_line)) {
1680 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
1681 $linecount = count($this->lines);
1682 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
1688 if (empty($noupdateafterinsertline)) {
1693 $this->
db->commit();
1694 return $this->line->id;
1696 $this->
db->rollback();
1700 $this->error = $this->line->error;
1701 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1702 $this->
db->rollback();
1706 dol_syslog(get_class($this).
"::addline status of order must be Draft to allow use of ->addline()", LOG_ERR);
1726 public function add_product($idproduct, $qty, $remise_percent = 0.0, $date_start =
'', $date_end =
'')
1729 global $conf, $mysoc;
1735 if ($idproduct > 0) {
1737 $prod->fetch($idproduct);
1741 if (empty($tva_tx)) {
1746 $localtax1_tx =
get_localtax($tva_tx, 1, $this->thirdparty, $mysoc, $tva_npr);
1747 $localtax2_tx =
get_localtax($tva_tx, 2, $this->thirdparty, $mysoc, $tva_npr);
1750 if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
1751 $price = $prod->multiprices[$this->thirdparty->price_level];
1753 $price = $prod->price;
1758 $line->context = $this->context;
1760 $line->fk_product = $idproduct;
1761 $line->desc = $prod->description;
1763 $line->subprice = $price;
1764 $line->remise_percent = $remise_percent;
1765 $line->vat_src_code = $vat_src_code;
1766 $line->tva_tx = $tva_tx;
1767 $line->localtax1_tx = $localtax1_tx;
1768 $line->localtax2_tx = $localtax2_tx;
1769 $line->ref = $prod->ref;
1770 $line->libelle = $prod->label;
1771 $line->product_desc = $prod->description;
1772 $line->fk_unit = $prod->fk_unit;
1776 $line->date_start = $date_start;
1779 $line->date_end = $date_end;
1782 $this->lines[] = $line;
1815 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
1818 if (empty($id) && empty($ref) && empty($ref_ext)) {
1822 $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';
1823 $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';
1824 $sql .=
', c.fk_account';
1825 $sql .=
', c.date_commande, c.date_valid, c.tms';
1826 $sql .=
', c.date_livraison as delivery_date';
1827 $sql .=
', c.fk_shipping_method';
1828 $sql .=
', c.fk_warehouse';
1829 $sql .=
', c.fk_projet as fk_project, c.remise_percent, c.remise, c.remise_absolue, c.source, c.facture as billed';
1830 $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';
1831 $sql .=
', c.fk_incoterms, c.location_incoterms';
1832 $sql .=
", c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc";
1833 $sql .=
", c.module_source, c.pos_source";
1834 $sql .=
", i.libelle as label_incoterms";
1835 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
1836 $sql .=
', cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle, cr.libelle_facture as cond_reglement_libelle_doc';
1837 $sql .=
', ca.code as availability_code, ca.label as availability_label';
1838 $sql .=
', dr.code as demand_reason_code';
1839 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
1840 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON c.fk_cond_reglement = cr.rowid';
1841 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON c.fk_mode_reglement = p.id';
1842 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON c.fk_availability = ca.rowid';
1843 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON c.fk_input_reason = dr.rowid';
1844 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
1847 $sql .=
" WHERE c.rowid=".((int) $id);
1849 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
1853 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
1856 $sql .=
" AND c.ref_ext='".$this->db->escape($ref_ext).
"'";
1859 $sql .=
" AND c.ref_int='".$this->db->escape($notused).
"'";
1862 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1863 $result = $this->
db->query($sql);
1865 $obj = $this->
db->fetch_object($result);
1867 $this->
id = $obj->rowid;
1868 $this->entity = $obj->entity;
1870 $this->
ref = $obj->ref;
1871 $this->ref_client = $obj->ref_client;
1872 $this->ref_customer = $obj->ref_client;
1873 $this->ref_ext = $obj->ref_ext;
1874 $this->ref_int = $obj->ref_int;
1876 $this->socid = $obj->fk_soc;
1877 $this->thirdparty = null;
1879 $this->fk_project = $obj->fk_project;
1880 $this->project = null;
1882 $this->statut = $obj->fk_statut;
1883 $this->status = $obj->fk_statut;
1885 $this->user_author_id = $obj->fk_user_author;
1886 $this->user_creation_id = $obj->fk_user_author;
1887 $this->user_validation_id = $obj->fk_user_valid;
1888 $this->user_valid = $obj->fk_user_valid;
1889 $this->user_modification_id = $obj->fk_user_modif;
1890 $this->user_modification = $obj->fk_user_modif;
1891 $this->total_ht = $obj->total_ht;
1892 $this->total_tva = $obj->total_tva;
1893 $this->total_localtax1 = $obj->total_localtax1;
1894 $this->total_localtax2 = $obj->total_localtax2;
1895 $this->total_ttc = $obj->total_ttc;
1896 $this->date = $this->
db->jdate($obj->date_commande);
1897 $this->date_commande = $this->
db->jdate($obj->date_commande);
1898 $this->date_creation = $this->
db->jdate($obj->date_creation);
1899 $this->date_validation = $this->
db->jdate($obj->date_valid);
1900 $this->date_modification = $this->
db->jdate($obj->tms);
1901 $this->remise = $obj->remise;
1902 $this->remise_percent = $obj->remise_percent;
1903 $this->remise_absolue = $obj->remise_absolue;
1904 $this->source = $obj->source;
1905 $this->billed = $obj->billed;
1906 $this->note = $obj->note_private;
1907 $this->note_private = $obj->note_private;
1908 $this->note_public = $obj->note_public;
1909 $this->model_pdf = $obj->model_pdf;
1910 $this->modelpdf = $obj->model_pdf;
1911 $this->last_main_doc = $obj->last_main_doc;
1912 $this->mode_reglement_id = $obj->fk_mode_reglement;
1913 $this->mode_reglement_code = $obj->mode_reglement_code;
1914 $this->mode_reglement = $obj->mode_reglement_libelle;
1915 $this->cond_reglement_id = $obj->fk_cond_reglement;
1916 $this->cond_reglement_code = $obj->cond_reglement_code;
1917 $this->cond_reglement = $obj->cond_reglement_libelle;
1918 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1919 $this->deposit_percent = $obj->deposit_percent;
1920 $this->fk_account = $obj->fk_account;
1921 $this->availability_id = $obj->fk_availability;
1922 $this->availability_code = $obj->availability_code;
1924 $this->demand_reason_id = $obj->fk_input_reason;
1925 $this->demand_reason_code = $obj->demand_reason_code;
1926 $this->date_livraison = $this->
db->jdate($obj->delivery_date);
1927 $this->delivery_date = $this->
db->jdate($obj->delivery_date);
1928 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method : null;
1929 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse : null;
1930 $this->fk_delivery_address = $obj->fk_delivery_address;
1931 $this->module_source = $obj->module_source;
1932 $this->pos_source = $obj->pos_source;
1935 $this->fk_incoterms = $obj->fk_incoterms;
1936 $this->location_incoterms = $obj->location_incoterms;
1937 $this->label_incoterms = $obj->label_incoterms;
1940 $this->fk_multicurrency = $obj->fk_multicurrency;
1941 $this->multicurrency_code = $obj->multicurrency_code;
1942 $this->multicurrency_tx = $obj->multicurrency_tx;
1943 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1944 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1945 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1947 $this->extraparams = (array) json_decode($obj->extraparams,
true);
1949 $this->lines = array();
1951 if ($this->statut == self::STATUS_DRAFT) {
1952 $this->brouillon = 1;
1959 $this->
db->free($result);
1968 $this->error =
'Order with id '.$id.
' not found sql='.$sql;
1972 $this->error = $this->
db->error();
1990 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1991 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1996 $result = $remise->fetch($idremise);
1999 if ($remise->fk_facture) {
2000 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2001 $this->
db->rollback();
2007 $line->fk_commande = $this->id;
2008 $line->fk_remise_except = $remise->id;
2009 $line->desc = $remise->description;
2010 $line->vat_src_code = $remise->vat_src_code;
2011 $line->tva_tx = $remise->tva_tx;
2012 $line->subprice = -$remise->amount_ht;
2013 $line->price = -$remise->amount_ht;
2014 $line->fk_product = 0;
2016 $line->remise_percent = 0;
2018 $line->info_bits = 2;
2020 $line->total_ht = -$remise->amount_ht;
2021 $line->total_tva = -$remise->amount_tva;
2022 $line->total_ttc = -$remise->amount_ttc;
2024 $result = $line->insert();
2028 $this->
db->commit();
2031 $this->
db->rollback();
2035 $this->error = $line->error;
2036 $this->errors = $line->errors;
2037 $this->
db->rollback();
2041 $this->
db->rollback();
2055 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2058 global $langs, $conf;
2060 $this->lines = array();
2062 $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,';
2063 $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,';
2064 $sql .=
' l.total_ht, l.total_ttc, l.total_tva, l.total_localtax1, l.total_localtax2, l.date_start, l.date_end,';
2065 $sql .=
' l.fk_unit,';
2066 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2067 $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,';
2068 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units';
2069 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as l';
2070 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (p.rowid = l.fk_product)';
2071 $sql .=
' WHERE l.fk_commande = '.((int) $this->
id);
2072 if ($only_product) {
2073 $sql .=
' AND p.fk_product_type = 0';
2075 $sql .=
' ORDER BY l.rang, l.rowid';
2077 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
2078 $result = $this->
db->query($sql);
2080 $num = $this->
db->num_rows($result);
2084 $objp = $this->
db->fetch_object($result);
2088 $line->rowid = $objp->rowid;
2089 $line->id = $objp->rowid;
2090 $line->fk_commande = $objp->fk_commande;
2091 $line->commande_id = $objp->fk_commande;
2092 $line->label = $objp->custom_label;
2093 $line->desc = $objp->description;
2094 $line->description = $objp->description;
2095 $line->product_type = $objp->product_type;
2096 $line->qty = $objp->qty;
2097 $line->ref_ext = $objp->ref_ext;
2099 $line->vat_src_code = $objp->vat_src_code;
2100 $line->tva_tx = $objp->tva_tx;
2101 $line->localtax1_tx = $objp->localtax1_tx;
2102 $line->localtax2_tx = $objp->localtax2_tx;
2103 $line->localtax1_type = $objp->localtax1_type;
2104 $line->localtax2_type = $objp->localtax2_type;
2105 $line->total_ht = $objp->total_ht;
2106 $line->total_ttc = $objp->total_ttc;
2107 $line->total_tva = $objp->total_tva;
2108 $line->total_localtax1 = $objp->total_localtax1;
2109 $line->total_localtax2 = $objp->total_localtax2;
2110 $line->subprice = $objp->subprice;
2111 $line->fk_remise_except = $objp->fk_remise_except;
2112 $line->remise_percent = $objp->remise_percent;
2113 $line->price = $objp->price;
2114 $line->fk_product = $objp->fk_product;
2115 $line->fk_fournprice = $objp->fk_fournprice;
2116 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2117 $line->pa_ht = $marginInfos[0];
2118 $line->marge_tx = $marginInfos[1];
2119 $line->marque_tx = $marginInfos[2];
2120 $line->rang = $objp->rang;
2121 $line->info_bits = $objp->info_bits;
2122 $line->special_code = $objp->special_code;
2123 $line->fk_parent_line = $objp->fk_parent_line;
2125 $line->ref = $objp->product_ref;
2126 $line->libelle = $objp->product_label;
2128 $line->product_ref = $objp->product_ref;
2129 $line->product_label = $objp->product_label;
2130 $line->product_tosell = $objp->product_tosell;
2131 $line->product_tobuy = $objp->product_tobuy;
2132 $line->product_desc = $objp->product_desc;
2133 $line->product_tobatch = $objp->product_tobatch;
2134 $line->product_barcode = $objp->product_barcode;
2136 $line->fk_product_type = $objp->fk_product_type;
2137 $line->fk_unit = $objp->fk_unit;
2139 $line->weight = $objp->weight;
2140 $line->weight_units = $objp->weight_units;
2141 $line->volume = $objp->volume;
2142 $line->volume_units = $objp->volume_units;
2144 $line->date_start = $this->
db->jdate($objp->date_start);
2145 $line->date_end = $this->
db->jdate($objp->date_end);
2148 $line->fk_multicurrency = $objp->fk_multicurrency;
2149 $line->multicurrency_code = $objp->multicurrency_code;
2150 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2151 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2152 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2153 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2155 $line->fetch_optionals();
2158 if (!empty($conf->global->MAIN_MULTILANGS) && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2160 $tmpproduct->fetch($objp->fk_product);
2161 $tmpproduct->getMultiLangs();
2163 $line->multilangs = $tmpproduct->multilangs;
2166 $this->lines[$i] = $line;
2171 $this->
db->free($result);
2175 $this->error = $this->
db->error();
2189 foreach ($this->lines as $line) {
2190 if ($line->product_type == 0) {
2205 foreach ($this->lines as $line) {
2206 if ($line->product_type == 1) {
2222 $sql =
'SELECT COUNT(DISTINCT ed.fk_expedition) as nb';
2223 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2224 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2226 $sql .=
' ed.fk_origin_line = cd.rowid';
2227 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2230 dol_syslog(get_class($this).
"::getNbOfShipments", LOG_DEBUG);
2233 $obj = $this->
db->fetch_object(
$resql);
2241 $this->error = $this->
db->lasterror();
2256 $this->expeditions = array();
2258 $sql =
'SELECT cd.rowid, cd.fk_product,';
2259 $sql .=
' sum(ed.qty) as qty';
2260 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expeditiondet as ed,';
2261 if ($filtre_statut >= 0) {
2262 $sql .=
' '.MAIN_DB_PREFIX.
'expedition as e,';
2264 $sql .=
' '.MAIN_DB_PREFIX.
'commandedet as cd';
2266 if ($filtre_statut >= 0) {
2267 $sql .=
' ed.fk_expedition = e.rowid AND';
2269 $sql .=
' ed.fk_origin_line = cd.rowid';
2270 $sql .=
' AND cd.fk_commande = '.((int) $this->
id);
2271 if ($fk_product > 0) {
2272 $sql .=
' AND cd.fk_product = '.((int) $fk_product);
2274 if ($filtre_statut >= 0) {
2275 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
2277 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
2280 dol_syslog(get_class($this).
"::loadExpeditions", LOG_DEBUG);
2283 $num = $this->
db->num_rows(
$resql);
2286 $obj = $this->
db->fetch_object(
$resql);
2287 $this->expeditions[$obj->rowid] = $obj->qty;
2293 $this->error = $this->
db->lasterror();
2309 $sql =
'SELECT count(*)';
2310 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expedition as e';
2311 $sql .=
', '.MAIN_DB_PREFIX.
'element_element as el';
2312 $sql .=
' WHERE el.fk_source = '.((int) $this->
id);
2313 $sql .=
" AND el.sourcetype = 'commande'";
2314 $sql .=
" AND el.fk_target = e.rowid";
2315 $sql .=
" AND el.targettype = 'shipping'";
2319 $row = $this->
db->fetch_row(
$resql);
2338 $this->stocks = array();
2341 $array_of_product = array();
2345 if (count($array_of_product)) {
2346 $sql =
"SELECT fk_product, sum(ps.reel) as total";
2347 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_stock as ps";
2348 $sql .=
" WHERE ps.fk_product IN (".$this->db->sanitize(join(
',', $array_of_product)).
")";
2349 $sql .=
' GROUP BY fk_product';
2352 $num = $this->
db->num_rows(
$resql);
2355 $obj = $this->
db->fetch_object(
$resql);
2356 $this->stocks[$obj->fk_product] = $obj->total;
2374 if ($this->statut == self::STATUS_DRAFT) {
2377 $sql =
"SELECT fk_product, qty";
2378 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet";
2379 $sql .=
" WHERE rowid = ".((int) $lineid);
2381 $result = $this->
db->query($sql);
2383 $obj = $this->
db->fetch_object($result);
2387 $product->id = $obj->fk_product;
2393 $line->fetch($lineid);
2396 $staticline = clone $line;
2397 $line->oldline = $staticline;
2399 if ($line->delete($user) > 0) {
2403 $this->
db->commit();
2406 $this->
db->rollback();
2407 $this->error = $this->
db->lasterror();
2411 $this->
db->rollback();
2412 $this->error = $line->error;
2416 $this->
db->rollback();
2420 $this->
db->rollback();
2421 $this->error = $this->
db->lasterror();
2425 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
2444 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
2445 return $this->
setDiscount($user, $remise, $notrigger);
2458 $remise = trim($remise) ?trim($remise) : 0;
2460 if ($user->rights->commande->creer) {
2467 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2468 $sql .=
' SET remise_percent = '.((float) $remise);
2469 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.((int) self::STATUS_DRAFT);
2474 $this->errors[] = $this->
db->error();
2479 $this->oldcopy = clone $this;
2480 $this->remise_percent = $remise;
2484 if (!$notrigger && empty($error)) {
2494 $this->
db->commit();
2497 foreach ($this->errors as $errmsg) {
2498 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2499 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2501 $this->
db->rollback();
2520 if (empty($remise)) {
2526 if ($user->rights->commande->creer) {
2531 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2532 $sql .=
' SET remise_absolue = '.((float) $remise);
2533 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.self::STATUS_DRAFT;
2538 $this->errors[] = $this->
db->error();
2543 $this->oldcopy = clone $this;
2544 $this->remise_absolue = $remise;
2548 if (!$notrigger && empty($error)) {
2558 $this->
db->commit();
2561 foreach ($this->errors as $errmsg) {
2562 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2563 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2565 $this->
db->rollback();
2584 if ($user->rights->commande->creer) {
2589 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2590 $sql .=
" SET date_commande = ".($date ?
"'".$this->db->idate($date).
"'" :
'null');
2591 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut = ".((int) self::STATUS_DRAFT);
2596 $this->errors[] = $this->
db->error();
2601 $this->oldcopy = clone $this;
2602 $this->date = $date;
2605 if (!$notrigger && empty($error)) {
2615 $this->
db->commit();
2618 foreach ($this->errors as $errmsg) {
2619 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2620 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2622 $this->
db->rollback();
2656 if ($user->rights->commande->creer) {
2661 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande";
2662 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2663 $sql .=
" WHERE rowid = ".((int) $this->
id);
2668 $this->errors[] = $this->
db->error();
2673 $this->oldcopy = clone $this;
2674 $this->date_livraison = $delivery_date;
2675 $this->delivery_date = $delivery_date;
2678 if (!$notrigger && empty($error)) {
2688 $this->
db->commit();
2691 foreach ($this->errors as $errmsg) {
2692 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2693 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2695 $this->
db->rollback();
2717 public function liste_array($shortlist = 0, $draft = 0, $excluser =
'', $socid = 0, $limit = 0, $offset = 0, $sortfield =
'c.date_commande', $sortorder =
'DESC')
2724 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2725 $sql .=
" c.rowid as cid, c.ref";
2726 if (empty($user->rights->societe->client->voir) && !$socid) {
2727 $sql .=
", sc.fk_soc, sc.fk_user";
2729 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"commande as c";
2730 if (empty($user->rights->societe->client->voir) && !$socid) {
2731 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2733 $sql .=
" WHERE c.entity IN (".getEntity(
'commande').
")";
2734 $sql .=
" AND c.fk_soc = s.rowid";
2735 if (empty($user->rights->societe->client->voir) && !$socid) {
2736 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2739 $sql .=
" AND s.rowid = ".((int) $socid);
2742 $sql .=
" AND c.fk_statut = ".self::STATUS_DRAFT;
2744 if (is_object($excluser)) {
2745 $sql .=
" AND c.fk_user_author <> ".((int) $excluser->id);
2747 $sql .= $this->
db->order($sortfield, $sortorder);
2748 $sql .= $this->
db->plimit($limit, $offset);
2750 $result = $this->
db->query($sql);
2752 $numc = $this->
db->num_rows($result);
2755 while ($i < $numc) {
2756 $obj = $this->
db->fetch_object($result);
2758 if ($shortlist == 1) {
2759 $ga[$obj->cid] = $obj->ref;
2760 } elseif ($shortlist == 2) {
2761 $ga[$obj->cid] = $obj->ref.
' ('.$obj->name.
')';
2763 $ga[$i][
'id'] = $obj->cid;
2764 $ga[$i][
'ref'] = $obj->ref;
2765 $ga[$i][
'name'] = $obj->name;
2788 dol_syslog(
'Commande::availability('.$availability_id.
')');
2789 if ($this->statut >= self::STATUS_DRAFT) {
2794 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2795 $sql .=
' SET fk_availability = '.((int) $availability_id);
2796 $sql .=
' WHERE rowid='.((int) $this->
id);
2801 $this->errors[] = $this->
db->error();
2806 $this->oldcopy = clone $this;
2807 $this->availability_id = $availability_id;
2810 if (!$notrigger && empty($error)) {
2820 $this->
db->commit();
2823 foreach ($this->errors as $errmsg) {
2824 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2825 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2827 $this->
db->rollback();
2831 $error_str =
'Command status do not meet requirement '.$this->statut;
2833 $this->error = $error_str;
2834 $this->errors[] = $this->error;
2852 dol_syslog(
'Commande::demand_reason('.$demand_reason_id.
')');
2853 if ($this->statut >= self::STATUS_DRAFT) {
2858 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande';
2859 $sql .=
' SET fk_input_reason = '.((int) $demand_reason_id);
2860 $sql .=
' WHERE rowid='.((int) $this->
id);
2865 $this->errors[] = $this->
db->error();
2870 $this->oldcopy = clone $this;
2871 $this->demand_reason_id = $demand_reason_id;
2874 if (!$notrigger && empty($error)) {
2884 $this->
db->commit();
2887 foreach ($this->errors as $errmsg) {
2888 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2889 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2891 $this->
db->rollback();
2895 $error_str =
'order status do not meet requirement '.$this->statut;
2897 $this->error = $error_str;
2898 $this->errors[] = $this->error;
2915 if ($user->rights->commande->creer) {
2920 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET';
2921 $sql .=
' ref_client = '.(empty($ref_client) ?
'NULL' :
"'".$this->db->escape($ref_client).
"'");
2922 $sql .=
' WHERE rowid = '.((int) $this->
id);
2924 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2927 $this->errors[] = $this->
db->error();
2932 $this->oldcopy = clone $this;
2933 $this->ref_client = $ref_client;
2936 if (!$notrigger && empty($error)) {
2945 $this->
db->commit();
2948 foreach ($this->errors as $errmsg) {
2949 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2950 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2952 $this->
db->rollback();
2971 if ($this->billed) {
2977 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 1';
2978 $sql .=
" WHERE rowid = ".((int) $this->
id).
' AND fk_statut > '.self::STATUS_DRAFT;
2980 dol_syslog(get_class($this).
"::classifyBilled", LOG_DEBUG);
2981 if ($this->
db->query($sql)) {
2983 $this->oldcopy = clone $this;
2987 if (!$notrigger && empty($error)) {
2989 $result = $this->
call_trigger(
'ORDER_CLASSIFY_BILLED', $user);
2997 $this->
db->commit();
3000 foreach ($this->errors as $errmsg) {
3001 dol_syslog(get_class($this).
"::classifyBilled ".$errmsg, LOG_ERR);
3002 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3004 $this->
db->rollback();
3008 $this->error = $this->
db->error();
3009 $this->
db->rollback();
3027 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande SET facture = 0';
3028 $sql .=
" WHERE rowid = ".((int) $this->
id).
' AND fk_statut > '.self::STATUS_DRAFT;
3030 dol_syslog(get_class($this).
"::classifyUnBilled", LOG_DEBUG);
3031 if ($this->
db->query($sql)) {
3033 $this->oldcopy = clone $this;
3037 if (!$notrigger && empty($error)) {
3039 $result = $this->
call_trigger(
'ORDER_CLASSIFY_UNBILLED', $user);
3049 $this->
db->commit();
3052 foreach ($this->errors as $errmsg) {
3053 dol_syslog(get_class($this).
"::classifyUnBilled ".$errmsg, LOG_ERR);
3054 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3056 $this->
db->rollback();
3060 $this->error = $this->
db->error();
3061 $this->
db->rollback();
3097 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)
3099 global $conf, $mysoc, $langs, $user;
3101 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");
3102 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3109 if (empty($info_bits)) {
3112 if (empty($txtva)) {
3115 if (empty($txlocaltax1)) {
3118 if (empty($txlocaltax2)) {
3121 if (empty($remise_percent)) {
3122 $remise_percent = 0;
3124 if (empty($special_code) || $special_code == 3) {
3127 if (empty($ref_ext)) {
3131 if ($date_start && $date_end && $date_start > $date_end) {
3132 $langs->load(
"errors");
3133 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3137 $remise_percent =
price2num($remise_percent);
3141 $pu_ht_devise =
price2num($pu_ht_devise);
3142 if (!preg_match(
'/\((.*)\)/', $txtva)) {
3160 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3161 $vat_src_code = $reg[1];
3162 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3165 $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);
3167 $total_ht = $tabprice[0];
3168 $total_tva = $tabprice[1];
3169 $total_ttc = $tabprice[2];
3170 $total_localtax1 = $tabprice[9];
3171 $total_localtax2 = $tabprice[10];
3172 $pu_ht = $tabprice[3];
3173 $pu_tva = $tabprice[4];
3174 $pu_ttc = $tabprice[5];
3177 $multicurrency_total_ht = $tabprice[16];
3178 $multicurrency_total_tva = $tabprice[17];
3179 $multicurrency_total_ttc = $tabprice[18];
3180 $pu_ht_devise = $tabprice[19];
3184 if ($price_base_type ==
'TTC') {
3185 $subprice = $pu_ttc;
3190 if ($remise_percent > 0) {
3191 $remise = round(($pu * $remise_percent / 100), 2);
3192 $price = ($pu - $remise);
3197 $line->fetch($rowid);
3198 $line->fetch_optionals();
3200 if (!empty($line->fk_product)) {
3202 $result = $product->fetch($line->fk_product);
3203 $product_type = $product->type;
3205 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) {
3206 $langs->load(
"errors");
3207 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnOrder', $product->ref);
3208 $this->errors[] = $this->error;
3210 dol_syslog(get_class($this).
"::addline error=Product ".$product->ref.
": ".$this->error, LOG_ERR);
3212 $this->
db->rollback();
3213 return self::STOCK_NOT_ENOUGH_FOR_ORDER;
3217 $staticline = clone $line;
3219 $line->oldline = $staticline;
3220 $this->line = $line;
3221 $this->line->context = $this->context;
3222 $this->line->rang = $rang;
3225 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
3226 $rangmax = $this->
line_max($fk_parent_line);
3227 $this->line->rang = $rangmax + 1;
3230 $this->line->id = $rowid;
3231 $this->line->label = $label;
3232 $this->line->desc = $desc;
3233 $this->line->qty = $qty;
3234 $this->line->ref_ext = $ref_ext;
3236 $this->line->vat_src_code = $vat_src_code;
3237 $this->line->tva_tx = $txtva;
3238 $this->line->localtax1_tx = $txlocaltax1;
3239 $this->line->localtax2_tx = $txlocaltax2;
3240 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3241 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3242 $this->line->remise_percent = $remise_percent;
3243 $this->line->subprice = $subprice;
3244 $this->line->info_bits = $info_bits;
3245 $this->line->special_code = $special_code;
3246 $this->line->total_ht = $total_ht;
3247 $this->line->total_tva = $total_tva;
3248 $this->line->total_localtax1 = $total_localtax1;
3249 $this->line->total_localtax2 = $total_localtax2;
3250 $this->line->total_ttc = $total_ttc;
3251 $this->line->date_start = $date_start;
3252 $this->line->date_end = $date_end;
3253 $this->line->product_type = $type;
3254 $this->line->fk_parent_line = $fk_parent_line;
3255 $this->line->skip_update_total = $skip_update_total;
3256 $this->line->fk_unit = $fk_unit;
3258 $this->line->fk_fournprice = $fk_fournprice;
3259 $this->line->pa_ht = $pa_ht;
3262 $this->line->multicurrency_subprice = $pu_ht_devise;
3263 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
3264 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
3265 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
3268 $this->line->price = $price;
3270 if (is_array($array_options) && count($array_options) > 0) {
3272 foreach ($array_options as $key => $value) {
3273 $this->line->array_options[$key] = $array_options[$key];
3277 $result = $this->line->update($user, $notrigger);
3280 if (!empty($fk_parent_line)) {
3287 $this->
db->commit();
3290 $this->error = $this->line->error;
3292 $this->
db->rollback();
3296 $this->error = get_class($this).
"::updateline Order status makes operation forbidden";
3297 $this->errors = array(
'OrderStatusMakeOperationForbidden');
3316 if (isset($this->
ref)) {
3317 $this->
ref = trim($this->
ref);
3319 if (isset($this->ref_client)) {
3320 $this->ref_client = trim($this->ref_client);
3322 if (isset($this->note) || isset($this->note_private)) {
3323 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
3325 if (isset($this->note_public)) {
3326 $this->note_public = trim($this->note_public);
3328 if (isset($this->model_pdf)) {
3329 $this->model_pdf = trim($this->model_pdf);
3331 if (isset($this->import_key)) {
3332 $this->import_key = trim($this->import_key);
3334 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
3340 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande SET";
3342 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
3343 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
3344 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
3345 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
3346 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
3347 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
3348 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
3349 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
3350 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
3351 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
3352 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
3353 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
3354 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id :
"null").
",";
3355 $sql .=
" fk_user_valid=".((isset($this->user_valid) && $this->user_valid > 0) ? $this->user_valid :
"null").
",";
3356 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
3357 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
3358 $sql .=
" deposit_percent=".(! empty($this->deposit_percent) ? strval($this->deposit_percent) :
"null").
",";
3359 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
3360 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
3361 $sql .=
" fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id :
"null").
",";
3362 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
3363 $sql .=
" fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id :
"null").
",";
3364 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
3365 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
3366 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
3367 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
"";
3369 $sql .=
" WHERE rowid=".((int) $this->
id);
3373 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3376 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
3386 if (!$error && !$notrigger) {
3397 foreach ($this->errors as $errmsg) {
3398 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3399 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3401 $this->
db->rollback();
3404 $this->
db->commit();
3416 public function delete($user, $notrigger = 0)
3418 global $conf, $langs;
3419 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3423 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
3438 $this->errors[] = $langs->trans(
'SomeShipmentExists');
3443 if (!$error && !empty($this->table_element_line)) {
3444 $tabletodelete = $this->table_element_line;
3445 $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).
")";
3446 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3447 if (!$this->
db->query($sqlef) || !$this->
db->query($sql)) {
3449 $this->error = $this->
db->lasterror();
3450 $this->errors[] = $this->error;
3451 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3476 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3482 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
3483 $res = $this->
db->query($sql);
3486 $this->error = $this->
db->lasterror();
3487 $this->errors[] = $this->error;
3488 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3503 if ($conf->commande->multidir_output[$this->entity] && !empty($this->
ref)) {
3504 $dir = $conf->commande->multidir_output[$this->entity].
"/".$ref;
3505 $file = $dir.
"/".$ref.
".pdf";
3506 if (file_exists($file)) {
3510 $this->error =
'ErrorFailToDeleteFile';
3511 $this->errors[] = $this->error;
3512 $this->
db->rollback();
3516 if (file_exists($dir)) {
3519 $this->error =
'ErrorFailToDeleteDir';
3520 $this->errors[] = $this->error;
3521 $this->
db->rollback();
3529 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3530 $this->
db->commit();
3533 $this->
db->rollback();
3549 global $conf, $langs;
3553 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.date_livraison as delivery_date, c.fk_statut, c.total_ht";
3554 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as c";
3555 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3556 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
3557 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3560 $sql .= $clause.
" c.entity IN (".
getEntity(
'commande').
")";
3562 $sql .=
" AND ((c.fk_statut IN (".self::STATUS_VALIDATED.
",".self::STATUS_SHIPMENTONPROCESS.
")) OR (c.fk_statut = ".self::STATUS_CLOSED.
" AND c.facture = 0))";
3564 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3570 $response->warning_delay = $conf->commande->client->warning_delay / 60 / 60 / 24;
3571 $response->label = $langs->trans(
"OrdersToProcess");
3572 $response->labelShort = $langs->trans(
"Opened");
3573 $response->url = DOL_URL_ROOT.
'/commande/list.php?search_status=-3&mainmenu=commercial&leftmenu=orders';
3578 while ($obj = $this->
db->fetch_object(
$resql)) {
3579 $response->nbtodo++;
3580 $response->total += $obj->total_ht;
3582 $generic_commande->statut = $obj->fk_statut;
3583 $generic_commande->date_commande = $this->
db->jdate($obj->date_commande);
3584 $generic_commande->date = $this->
db->jdate($obj->date_commande);
3585 $generic_commande->date_livraison = $this->
db->jdate($obj->delivery_date);
3586 $generic_commande->delivery_date = $this->
db->jdate($obj->delivery_date);
3588 if ($generic_commande->hasDelay()) {
3589 $response->nbtodolate++;
3595 $this->error = $this->
db->error();
3609 $label = $langs->trans(
'OrderSource'.$this->source);
3611 if ($label ==
'OrderSource') {
3625 return $this->
LibStatut($this->statut, $this->billed, $mode);
3638 public function LibStatut($status, $billed, $mode, $donotshowbilled = 0)
3641 global $langs, $conf;
3644 if (empty($donotshowbilled)) {
3645 $billedtext .= ($billed ?
' - '.$langs->transnoentitiesnoconv(
"Billed") :
'');
3650 if ($status == self::STATUS_CANCELED) {
3651 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderCanceled');
3652 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderCanceledShort');
3653 $statusType =
'status9';
3654 } elseif ($status == self::STATUS_DRAFT) {
3655 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDraft');
3656 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDraftShort');
3657 $statusType =
'status0';
3658 } elseif ($status == self::STATUS_VALIDATED) {
3659 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderValidated').$billedtext;
3660 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderValidatedShort').$billedtext;
3661 $statusType =
'status1';
3662 } elseif ($status == self::STATUS_SHIPMENTONPROCESS) {
3663 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderSent').$billedtext;
3664 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderSentShort').$billedtext;
3665 $labelTooltip = $langs->transnoentitiesnoconv(
"StatusOrderSent").
' - '.$langs->transnoentitiesnoconv(
"DateDeliveryPlanned").dol_print_date($this->date_livraison).$billedtext;
3666 $statusType =
'status4';
3667 } elseif ($status == self::STATUS_CLOSED && (!$billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3668 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderToBill');
3669 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderToBillShort');
3670 $statusType =
'status4';
3671 } elseif ($status == self::STATUS_CLOSED && ($billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3672 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderProcessed').$billedtext;
3673 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderProcessedShort').$billedtext;
3674 $statusType =
'status6';
3675 } elseif ($status == self::STATUS_CLOSED && (!empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) {
3676 $labelStatus = $langs->transnoentitiesnoconv(
'StatusOrderDelivered');
3677 $labelStatusShort = $langs->transnoentitiesnoconv(
'StatusOrderDeliveredShort');
3678 $statusType =
'status6';
3680 $labelStatus = $langs->transnoentitiesnoconv(
'Unknown');
3681 $labelStatusShort =
'';
3686 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', array(
'tooltip' => $labelTooltip));
3703 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0, $target =
'')
3705 global $conf, $langs, $user, $hookmanager;
3707 if (!empty($conf->dol_no_mouse_hover)) {
3713 if (!empty($conf->expedition->enabled) && ($option ==
'1' || $option ==
'2')) {
3714 $url = DOL_URL_ROOT.
'/expedition/shipment.php?id='.$this->id;
3716 $url = DOL_URL_ROOT.
'/commande/card.php?id='.$this->id;
3719 if (!$user->rights->commande->lire) {
3723 if ($option !==
'nolink') {
3725 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3726 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
3727 $add_save_lastsearch_values = 1;
3729 if ($add_save_lastsearch_values) {
3730 $url .=
'&save_lastsearch_values=1';
3740 if ($user->rights->commande->lire) {
3741 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Order").
'</u>';
3742 if (isset($this->statut)) {
3743 $label .=
' '.$this->getLibStatut(5);
3745 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
3746 $label .=
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.(empty($this->ref_customer) ? (empty($this->ref_client) ?
'' : $this->ref_client) : $this->ref_customer);
3747 if (!empty($this->total_ht)) {
3748 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
3750 if (!empty($this->total_tva)) {
3751 $label .=
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
3753 if (!empty($this->total_ttc)) {
3754 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
3756 if (!empty($this->date)) {
3757 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
3759 if (!empty($this->delivery_date)) {
3760 $label .=
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
3765 if (empty($notooltip) && $user->rights->commande->lire) {
3766 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
3767 $label = $langs->trans(
"Order");
3768 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
3770 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
3771 $linkclose .=
' class="classfortooltip"';
3773 $target_value=array(
'_self',
'_blank',
'_parent',
'_top');
3774 if (in_array($target, $target_value)) {
3775 $linkclose .=
' target="'.dol_escape_htmltag($target).
'"';
3779 $linkstart =
'<a href="'.$url.
'"';
3780 $linkstart .= $linkclose.
'>';
3783 if ($option ===
'nolink') {
3788 $result .= $linkstart;
3790 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
3792 if ($withpicto != 2) {
3793 $result .= $this->ref;
3795 $result .= $linkend;
3797 if ($addlinktonotes) {
3798 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
3801 $result .=
' <span class="note inline-block">';
3802 $result .=
'<a href="'.DOL_URL_ROOT.
'/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
3807 $result .=
'</span>';
3812 $hookmanager->initHooks(array($this->element .
'dao'));
3813 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
3814 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3816 $result = $hookmanager->resPrint;
3818 $result .= $hookmanager->resPrint;
3832 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem,';
3833 $sql .=
' date_valid as datev,';
3834 $sql .=
' date_cloture as datecloture,';
3835 $sql .=
' fk_user_author, fk_user_valid, fk_user_cloture';
3836 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande as c';
3837 $sql .=
' WHERE c.rowid = '.((int) $id);
3838 $result = $this->
db->query($sql);
3840 if ($this->
db->num_rows($result)) {
3841 $obj = $this->
db->fetch_object($result);
3842 $this->
id = $obj->rowid;
3843 if ($obj->fk_user_author) {
3844 $this->user_creation_id = $obj->fk_user_author;
3846 if ($obj->fk_user_valid) {
3847 $this->user_validation_id = $obj->fk_user_valid;
3849 if ($obj->fk_user_cloture) {
3850 $this->user_closing_id = $obj->fk_user_cloture;
3853 $this->date_creation = $this->
db->jdate($obj->datec);
3854 $this->date_modification = $this->
db->jdate($obj->datem);
3855 $this->date_validation = $this->
db->jdate($obj->datev);
3856 $this->date_cloture = $this->
db->jdate($obj->datecloture);
3859 $this->
db->free($result);
3875 global $conf, $langs;
3877 dol_syslog(get_class($this).
"::initAsSpecimen");
3882 $sql =
"SELECT rowid";
3883 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
3884 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3885 $sql .= $this->
db->plimit(100);
3889 $num_prods = $this->
db->num_rows(
$resql);
3891 while ($i < $num_prods) {
3893 $row = $this->
db->fetch_row(
$resql);
3894 $prodids[$i] = $row[0];
3900 $this->
ref =
'SPECIMEN';
3901 $this->specimen = 1;
3903 $this->date = time();
3904 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
3905 $this->cond_reglement_code =
'RECEP';
3906 $this->mode_reglement_code =
'CHQ';
3907 $this->availability_code =
'DSP';
3908 $this->demand_reason_code =
'SRC_00';
3910 $this->note_public =
'This is a comment (public)';
3911 $this->note_private =
'This is a comment (private)';
3913 $this->multicurrency_tx = 1;
3914 $this->multicurrency_code = $conf->currency;
3919 while ($xnbp < $nbp) {
3922 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
3924 $line->subprice = 100;
3928 $line->total_ht = 50;
3929 $line->total_ttc = 60;
3930 $line->total_tva = 10;
3931 $line->remise_percent = 50;
3933 $line->total_ht = 100;
3934 $line->total_ttc = 120;
3935 $line->total_tva = 20;
3936 $line->remise_percent = 0;
3938 if ($num_prods > 0) {
3939 $prodid = mt_rand(1, $num_prods);
3940 $line->fk_product = $prodids[$prodid];
3941 $line->product_ref =
'SPECIMEN';
3944 $this->lines[$xnbp] = $line;
3946 $this->total_ht += $line->total_ht;
3947 $this->total_tva += $line->total_tva;
3948 $this->total_ttc += $line->total_ttc;
3966 $this->nb = array();
3969 $sql =
"SELECT count(co.rowid) as nb";
3970 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as co";
3971 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
3972 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3973 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3974 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3977 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'commande').
")";
3981 while ($obj = $this->
db->fetch_object(
$resql)) {
3982 $this->nb[
"orders"] = $obj->nb;
3988 $this->error = $this->
db->error();
4014 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
4016 global $conf, $langs;
4018 $langs->load(
"orders");
4019 $outputlangs->load(
"products");
4022 $modele =
'einstein';
4024 if (!empty($this->model_pdf)) {
4025 $modele = $this->model_pdf;
4026 } elseif (!empty($this->modelpdf)) {
4027 $modele = $this->modelpdf;
4028 } elseif (!empty($conf->global->COMMANDE_ADDON_PDF)) {
4029 $modele = $conf->global->COMMANDE_ADDON_PDF;
4033 $modelpath =
"core/modules/commande/doc/";
4035 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
4088 return max($this->date, $this->date_livraison) < ($now - $conf->commande->client->warning_delay);
4098 global $conf, $langs;
4100 if (empty($this->date_livraison)) {
4101 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
4103 $text = $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->date_livraison,
'day');
4105 $text .=
' '.($conf->commande->client->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->client->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
4120 public $element =
'commandedet';
4122 public $table_element =
'commandedet';
4130 public $fk_commande;
4138 public $commande_id;
4140 public $fk_parent_line;
4152 public $fk_remise_except;
4158 public $fk_fournprice;
4178 public $skip_update_total;
4199 $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,';
4200 $sql .=
' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
4201 $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,';
4202 $sql .=
' cd.fk_unit,';
4203 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
4204 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
4205 $sql .=
' cd.date_start, cd.date_end, cd.vat_src_code';
4206 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as cd';
4207 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
4208 $sql .=
' WHERE cd.rowid = '.((int) $rowid);
4209 $result = $this->
db->query($sql);
4211 $objp = $this->
db->fetch_object($result);
4212 $this->
rowid = $objp->rowid;
4213 $this->
id = $objp->rowid;
4214 $this->fk_commande = $objp->fk_commande;
4215 $this->fk_parent_line = $objp->fk_parent_line;
4216 $this->label = $objp->custom_label;
4217 $this->desc = $objp->description;
4218 $this->qty = $objp->qty;
4219 $this->
price = $objp->price;
4220 $this->subprice = $objp->subprice;
4221 $this->ref_ext = $objp->ref_ext;
4222 $this->vat_src_code = $objp->vat_src_code;
4223 $this->tva_tx = $objp->tva_tx;
4224 $this->localtax1_tx = $objp->localtax1_tx;
4225 $this->localtax2_tx = $objp->localtax2_tx;
4226 $this->remise = $objp->remise;
4227 $this->remise_percent = $objp->remise_percent;
4228 $this->fk_remise_except = $objp->fk_remise_except;
4229 $this->fk_product = $objp->fk_product;
4230 $this->product_type = $objp->product_type;
4231 $this->info_bits = $objp->info_bits;
4232 $this->special_code = $objp->special_code;
4233 $this->total_ht = $objp->total_ht;
4234 $this->total_tva = $objp->total_tva;
4235 $this->total_localtax1 = $objp->total_localtax1;
4236 $this->total_localtax2 = $objp->total_localtax2;
4237 $this->total_ttc = $objp->total_ttc;
4238 $this->fk_fournprice = $objp->fk_fournprice;
4239 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
4240 $this->pa_ht = $marginInfos[0];
4241 $this->marge_tx = $marginInfos[1];
4242 $this->marque_tx = $marginInfos[2];
4243 $this->special_code = $objp->special_code;
4244 $this->rang = $objp->rang;
4246 $this->
ref = $objp->product_ref;
4248 $this->product_ref = $objp->product_ref;
4249 $this->product_label = $objp->product_label;
4250 $this->product_desc = $objp->product_desc;
4251 $this->product_tobatch = $objp->product_tobatch;
4252 $this->fk_unit = $objp->fk_unit;
4254 $this->date_start = $this->
db->jdate($objp->date_start);
4255 $this->date_end = $this->
db->jdate($objp->date_end);
4257 $this->fk_multicurrency = $objp->fk_multicurrency;
4258 $this->multicurrency_code = $objp->multicurrency_code;
4259 $this->multicurrency_subprice = $objp->multicurrency_subprice;
4260 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
4261 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
4262 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
4264 $this->
db->free($result);
4268 $this->error = $this->
db->lasterror();
4280 public function delete(
User $user, $notrigger = 0)
4282 global $conf, $langs;
4286 if (empty($this->
id) && !empty($this->
rowid)) {
4287 $this->
id = $this->rowid;
4291 $sqlCheckShipmentLine =
"SELECT";
4292 $sqlCheckShipmentLine .=
" ed.rowid";
4293 $sqlCheckShipmentLine .=
" FROM ".MAIN_DB_PREFIX.
"expeditiondet ed";
4294 $sqlCheckShipmentLine .=
" WHERE ed.fk_origin_line = ".((int) $this->
id);
4296 $resqlCheckShipmentLine = $this->
db->query($sqlCheckShipmentLine);
4297 if (!$resqlCheckShipmentLine) {
4299 $this->error = $this->
db->lasterror();
4300 $this->errors[] = $this->error;
4302 $langs->load(
'errors');
4303 $num = $this->
db->num_rows($resqlCheckShipmentLine);
4306 $objCheckShipmentLine = $this->
db->fetch_object($resqlCheckShipmentLine);
4307 $this->error = $langs->trans(
'ErrorRecordAlreadyExists').
' : '.$langs->trans(
'ShipmentLine').
' '.$objCheckShipmentLine->rowid;
4308 $this->errors[] = $this->error;
4310 $this->
db->free($resqlCheckShipmentLine);
4313 dol_syslog(__METHOD__.
'Error ; '.$this->error, LOG_ERR);
4319 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"commandedet WHERE rowid = ".((int) $this->
id);
4326 $this->
id = $this->rowid;
4330 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
4334 if (!$error && !$notrigger) {
4336 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
4344 $this->
db->commit();
4348 foreach ($this->errors as $errmsg) {
4349 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
4350 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4352 $this->
db->rollback();
4355 $this->error = $this->
db->lasterror();
4367 public function insert($user = null, $notrigger = 0)
4369 global $langs, $conf;
4373 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4375 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
4378 if (empty($this->tva_tx)) {
4381 if (empty($this->localtax1_tx)) {
4382 $this->localtax1_tx = 0;
4384 if (empty($this->localtax2_tx)) {
4385 $this->localtax2_tx = 0;
4387 if (empty($this->localtax1_type)) {
4388 $this->localtax1_type = 0;
4390 if (empty($this->localtax2_type)) {
4391 $this->localtax2_type = 0;
4393 if (empty($this->total_localtax1)) {
4394 $this->total_localtax1 = 0;
4396 if (empty($this->total_localtax2)) {
4397 $this->total_localtax2 = 0;
4399 if (empty($this->rang)) {
4402 if (empty($this->remise_percent)) {
4403 $this->remise_percent = 0;
4405 if (empty($this->info_bits)) {
4406 $this->info_bits = 0;
4408 if (empty($this->special_code)) {
4409 $this->special_code = 0;
4411 if (empty($this->fk_parent_line)) {
4412 $this->fk_parent_line = 0;
4414 if (empty($this->pa_ht)) {
4417 if (empty($this->ref_ext)) {
4418 $this->ref_ext =
'';
4422 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4423 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4427 $this->pa_ht = $result;
4432 if ($this->product_type < 0) {
4439 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'commandedet';
4440 $sql .=
' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
4441 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
4442 $sql .=
' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
4443 $sql .=
' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
4444 $sql .=
' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
4446 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
4448 $sql .=
" VALUES (".$this->fk_commande.
",";
4449 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
4450 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
4451 $sql .=
" '".$this->db->escape($this->desc).
"',";
4452 $sql .=
" '".price2num($this->qty).
"',";
4453 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
4454 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
4455 $sql .=
" '".price2num($this->tva_tx).
"',";
4456 $sql .=
" '".price2num($this->localtax1_tx).
"',";
4457 $sql .=
" '".price2num($this->localtax2_tx).
"',";
4458 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
4459 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
4460 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
4461 $sql .=
" '".$this->db->escape($this->product_type).
"',";
4462 $sql .=
" '".price2num($this->remise_percent).
"',";
4463 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice) :
"null").
",";
4464 $sql .=
" ".($this->price !=
'' ?
"'".price2num($this->
price).
"'" :
"null").
",";
4465 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
4466 $sql .=
' '.((int) $this->special_code).
',';
4467 $sql .=
' '.((int) $this->rang).
',';
4468 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
4469 $sql .=
' '.price2num($this->pa_ht).
',';
4470 $sql .=
" ".((int) $this->info_bits).
",";
4471 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
4472 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
4473 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
4474 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
4475 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
4476 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
4477 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
',';
4478 $sql .=
' '.(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
4479 $sql .=
", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) :
'NULL');
4480 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
4481 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
4482 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
4483 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
4484 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
4487 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
4490 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
4491 $this->
rowid = $this->id;
4500 if (!$error && !$notrigger) {
4502 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
4510 $this->
db->commit();
4514 foreach ($this->errors as $errmsg) {
4515 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
4516 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4518 $this->
db->rollback();
4521 $this->error = $this->
db->error();
4522 $this->
db->rollback();
4536 global $conf, $langs;
4540 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4543 if (empty($this->tva_tx)) {
4546 if (empty($this->localtax1_tx)) {
4547 $this->localtax1_tx = 0;
4549 if (empty($this->localtax2_tx)) {
4550 $this->localtax2_tx = 0;
4552 if (empty($this->localtax1_type)) {
4553 $this->localtax1_type = 0;
4555 if (empty($this->localtax2_type)) {
4556 $this->localtax2_type = 0;
4558 if (empty($this->qty)) {
4561 if (empty($this->total_localtax1)) {
4562 $this->total_localtax1 = 0;
4564 if (empty($this->total_localtax2)) {
4565 $this->total_localtax2 = 0;
4567 if (empty($this->marque_tx)) {
4568 $this->marque_tx = 0;
4570 if (empty($this->marge_tx)) {
4571 $this->marge_tx = 0;
4573 if (empty($this->remise_percent)) {
4574 $this->remise_percent = 0;
4576 if (empty($this->info_bits)) {
4577 $this->info_bits = 0;
4579 if (empty($this->special_code)) {
4580 $this->special_code = 0;
4582 if (empty($this->product_type)) {
4583 $this->product_type = 0;
4585 if (empty($this->fk_parent_line)) {
4586 $this->fk_parent_line = 0;
4588 if (empty($this->pa_ht)) {
4591 if (empty($this->ref_ext)) {
4592 $this->ref_ext =
'';
4596 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4597 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
4601 $this->pa_ht = $result;
4608 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4609 $sql .=
" description='".$this->db->escape($this->desc).
"'";
4610 $sql .=
" , label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
4611 $sql .=
" , vat_src_code=".(!empty($this->vat_src_code) ?
"'".$this->db->escape($this->vat_src_code).
"'" :
"''");
4612 $sql .=
" , tva_tx=".price2num($this->tva_tx);
4613 $sql .=
" , localtax1_tx=".price2num($this->localtax1_tx);
4614 $sql .=
" , localtax2_tx=".price2num($this->localtax2_tx);
4615 $sql .=
" , localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
4616 $sql .=
" , localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
4617 $sql .=
" , qty=".price2num($this->qty);
4618 $sql .=
" , ref_ext='".$this->db->escape($this->ref_ext).
"'";
4619 $sql .=
" , subprice=".price2num($this->subprice).
"";
4620 $sql .=
" , remise_percent=".price2num($this->remise_percent).
"";
4621 $sql .=
" , price=".price2num($this->
price).
"";
4622 $sql .=
" , remise=".price2num($this->remise).
"";
4623 if (empty($this->skip_update_total)) {
4624 $sql .=
" , total_ht=".price2num($this->total_ht).
"";
4625 $sql .=
" , total_tva=".price2num($this->total_tva).
"";
4626 $sql .=
" , total_ttc=".price2num($this->total_ttc).
"";
4627 $sql .=
" , total_localtax1=".price2num($this->total_localtax1);
4628 $sql .=
" , total_localtax2=".price2num($this->total_localtax2);
4630 $sql .=
" , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null");
4631 $sql .=
" , buy_price_ht='".price2num($this->pa_ht).
"'";
4632 $sql .=
" , info_bits=".((int) $this->info_bits);
4633 $sql .=
" , special_code=".((int) $this->special_code);
4634 $sql .=
" , date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
4635 $sql .=
" , date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
4636 $sql .=
" , product_type=".$this->product_type;
4637 $sql .=
" , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line :
"null");
4638 if (!empty($this->rang)) {
4639 $sql .=
", rang=".((int) $this->rang);
4641 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
4644 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice).
"";
4645 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht).
"";
4646 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva).
"";
4647 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).
"";
4649 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4651 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
4655 $this->
id = $this->rowid;
4662 if (!$error && !$notrigger) {
4664 $result = $this->
call_trigger(
'LINEORDER_MODIFY', $user);
4672 $this->
db->commit();
4676 foreach ($this->errors as $errmsg) {
4677 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
4678 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4680 $this->
db->rollback();
4683 $this->error = $this->
db->error();
4684 $this->
db->rollback();
4702 if (empty($this->total_localtax1)) {
4703 $this->total_localtax1 = 0;
4705 if (empty($this->total_localtax2)) {
4706 $this->total_localtax2 = 0;
4710 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
4711 $sql .=
" total_ht='".price2num($this->total_ht).
"'";
4712 $sql .=
",total_tva='".price2num($this->total_tva).
"'";
4713 $sql .=
",total_localtax1='".price2num($this->total_localtax1).
"'";
4714 $sql .=
",total_localtax2='".price2num($this->total_localtax2).
"'";
4715 $sql .=
",total_ttc='".price2num($this->total_ttc).
"'";
4716 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4718 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
4722 $this->
db->commit();
4725 $this->error = $this->
db->error();
4726 $this->
db->rollback();
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
getNbOfProductsLines()
Return number of line with type product.
Class to manage stock movements.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
const STATUS_CLOSED
Closed (Sent, billed or not)
getLinesArray()
Create an array of order lines.
LibStatut($status, $billed, $mode, $donotshowbilled=0)
Return label of status.
set_date($user, $date, $notrigger=0)
Set the order date.
cancel($idwarehouse=-1)
Cancel an order If stock is decremented on order validation, we must reincrement it.
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
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)
Add an order line into database (linked to product/service or not)
set_date_livraison($user, $delivery_date, $notrigger=0)
Set delivery date.
getNextNumRef($soc)
Returns the reference to the following non used Order depending on the active numbering module define...
update(User $user, $notrigger=0)
Update the line object into db.
getNbOfServicesLines()
Return number of line with type service.
$conf db
API class for accounts.
hasDelay()
Is the customer order delayed?
demand_reason($demand_reason_id, $notrigger=0)
Update order demand_reason.
fetch($id, $ref= '', $ref_ext= '', $notused= '')
Get object from database.
updateRangOfLine($rowid, $rang)
Update position of line (rang)
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
__construct($db)
Constructor.
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
Class to manage products or services.
setDraft($user, $idwarehouse=-1)
Set draft status.
dol_now($mode= 'auto')
Return date for now.
line_order($renum=false, $rowidorder= 'ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
set_remise($user, $remise, $notrigger=0)
Applique une remise relative.
deleteObjectLinked($sourceid=null, $sourcetype= '', $targetid=null, $targettype= '', $rowid= '', $f_user=null, $notrigger=0)
Delete all links between an object $this.
dol_delete_preview($object)
Delete all preview files linked to object instance.
static commonReplaceProduct(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
delete_linked_contact($source= '', $code= '')
Delete all links between an object $this and all its contacts.
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller= '', $localtaxes_array= '', $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code= '')
Calculate totals (net, vat, ...) of a line.
Class to manage Dolibarr users.
load_board($user)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
classifyUnBilled(User $user, $notrigger=0)
Classify the order as not invoiced.
Class to manage Dolibarr database access.
add_contact($fk_socpeople, $type_contact, $source= 'external', $notrigger=0)
Add a link between element $this->element and a contact.
initAsSpecimen()
Initialise an instance with random values.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
classifyBilled(User $user, $notrigger=0)
Classify the order as invoiced.
valid($user, $idwarehouse=0, $notrigger=0)
Validate order.
const STATUS_SHIPMENTONPROCESS
Shipment on process.
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Fonction qui renvoie si tva doit etre tva percue recuperable.
loadExpeditions($filtre_statut=-1, $fk_product=0)
Load array this->expeditions of lines of shipments with nb of products sent for each order line Note:...
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
availability($availability_id, $notrigger=0)
Update delivery delay.
$pos_source
key of pos source ('0', '1', ...)
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate, from a $thirdparty_buyer to a $thirdparty_seller Note: This function applies same rules than get_default_tva.
fetch_lines($only_product=0, $loadalsotranslation=0)
Load array lines.
nb_expedition()
Returns a array with expeditions lines number.
info($id)
Charge les informations d'ordre info dans l'objet commande.
set_remise_absolue($user, $remise, $notrigger=0)
Set a fixed amount discount.
price($amount, $form=0, $outlangs= '', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code= '')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
Class to manage order lines.
insertExtraFields($trigger= '', $userused=null)
Add/Update all extra fields values for the current object.
stock_array($filtre_statut=self::STATUS_CANCELED)
Return a array with the pending stock by product.
const STOCK_NOT_ENOUGH_FOR_ORDER
ERR Not enough stock.
static getIdAndTxFromCode($dbs, $code, $date_document= '')
Get id and rate of currency from code.
create($user, $notrigger=0)
Create order Note that this->ref can be set or empty.
insert_discount($idremise)
Adding line of fixed discount in the order in DB.
Class to manage third parties objects (customers, suppliers, prospects...)
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
insert($user=null, $notrigger=0)
Insert line into database.
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
deleteEcmFiles($mode=0)
Delete related files of object in database.
Class to manage shipments.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Class to manage customers orders.
const STATUS_DRAFT
Draft status.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
deleteExtraFields()
Delete all extra fields values for the current object.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
set_reopen($user)
Tag the order as validated (opened) Function used when order is reopend after being closed...
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
deleteline($user=null, $lineid=0)
Delete an order line.
getLibStatut($mode)
Return status label of Order.
static getIdFromCode($dbs, $code)
Get id of currency from code.
liste_array($shortlist=0, $draft=0, $excluser= '', $socid=0, $limit=0, $offset=0, $sortfield= 'c.date_commande', $sortorder= 'DESC')
Return list of orders (eventuelly filtered on a user) into an array.
setDiscount($user, $remise, $notrigger=0)
Set a percentage discount.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
Superclass for orders classes.
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0)
Get buy price to use for margin calculation.
if(isModEnabled('facture')&&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur')&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)&&$user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice')&&$user->rights->supplier_invoice->lire)) if(isModEnabled('don')&&!empty($user->rights->don->lire)) if(isModEnabled('tax')&&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture')&&isModEnabled('commande')&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
const STATUS_VALIDATED
Validated status.
copy_linked_contact($objFrom, $source= 'internal')
Copy contact from one element to current.
add_product($idproduct, $qty, $remise_percent=0.0, $date_start= '', $date_end= '')
Add line into array $this->client must be loaded.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
call_trigger($triggerName, $user)
Call trigger based on this instance.
createFromClone(User $user, $socid=0)
Load an object from its id and create a new one in database.
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add objects linked in llx_element_element.
$module_source
key of module source when order generated from a dedicated module ('cashdesk', 'takepos', ...)
Superclass for orders classes.
cloture($user, $notrigger=0)
Close order.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that return vat rate of a product line (according to seller, buyer and product vat rate) VAT...
getLabelSource()
Return source label of order.
Class to manage absolute discounts.
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
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)
Update a line in database.
load_state_board()
Charge indicateurs this->nb de tableau de bord.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
update(User $user, $notrigger=0)
Update database.
createFromProposal($object, User $user)
Load an object from a proposal and create a new order into database.
update_price($exclspec=0, $roundingadjust= 'none', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines)...
update_total()
Update DB line fields total_xxx Used by migration.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
setDeliveryDate($user, $delivery_date, $notrigger=0)
Set the planned delivery date.
getNomUrl($withpicto=0, $option= '', $max=0, $short=0, $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0, $target= '')
Return clicable link of object (with eventually picto)
fetch($rowid)
Load line order.
showDelay()
Show the customer delayed info.
getNbOfShipments()
Count number of shipments for this order.
__construct($db)
Constructor.
set_ref_client($user, $ref_client, $notrigger=0)
Set customer ref.
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
const STATUS_CANCELED
Canceled status.