44require_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
45require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
46require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.orderline.class.php';
47require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
48require_once DOL_DOCUMENT_ROOT.
'/subtotals/class/commonsubtotal.class.php';
50 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
64 public $element =
'order_supplier';
69 public $table_element =
'commande_fournisseur';
74 public $table_element_line =
'commande_fournisseurdet';
79 public $class_element_line =
'CommandeFournisseurLigne';
84 public $fk_element =
'fk_commande';
89 public $picto =
'supplier_order';
95 public $restrictiononfksoc = 1;
115 public $ref_supplier;
163 public $date_approve;
169 public $date_approve2;
174 public $date_commande;
180 public $remise_percent;
184 public $methode_commande_id;
188 public $methode_commande;
193 public $delivery_date;
198 public $fk_warehouse;
213 public $total_localtax1;
218 public $total_localtax2;
233 public $cond_reglement_id;
238 public $cond_reglement_code;
243 public $cond_reglement_label;
248 public $cond_reglement_doc;
254 public $deposit_percent;
264 public $mode_reglement_id;
269 public $mode_reglement_code;
274 public $mode_reglement;
279 public $user_author_id;
284 public $user_approve_id;
290 public $user_approve_id2;
305 public $extraparams = array();
310 public $lines = array();
330 public $date_lim_reglement;
335 public $receptions = array();
341 public $fk_multicurrency;
346 public $multicurrency_code;
351 public $multicurrency_tx;
356 public $multicurrency_total_ht;
361 public $multicurrency_total_tva;
366 public $multicurrency_total_ttc;
396 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => 0,
'notnull' => 1,
'position' => 10),
397 'ref' => array(
'type' =>
'varchar(255)',
'label' =>
'Ref',
'enabled' => 1,
'visible' => 1,
'showoncombobox' => 1,
'position' => 25,
'searchall' => 1),
398 'ref_ext' => array(
'type' =>
'varchar(255)',
'label' =>
'RefExt',
'enabled' => 1,
'visible' => 0,
'position' => 35),
399 'ref_supplier' => array(
'type' =>
'varchar(255)',
'label' =>
'RefOrderSupplierShort',
'enabled' => 1,
'visible' => 1,
'position' => 40,
'searchall' => 1),
400 'fk_projet' => array(
'type' =>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label' =>
'Project',
'enabled' =>
"isModEnabled('project')",
'visible' => -1,
'position' => 45),
401 'date_valid' => array(
'type' =>
'datetime',
'label' =>
'DateValidation',
'enabled' => 1,
'visible' => -1,
'position' => 710),
402 'date_approve' => array(
'type' =>
'datetime',
'label' =>
'DateApprove',
'enabled' => 1,
'visible' => -1,
'position' => 720),
403 'date_approve2' => array(
'type' =>
'datetime',
'label' =>
'DateApprove2',
'enabled' => 1,
'visible' => 3,
'position' => 725),
404 'date_commande' => array(
'type' =>
'date',
'label' =>
'OrderDateShort',
'enabled' => 1,
'visible' => 1,
'position' => 70),
405 'date_livraison' => array(
'type' =>
'datetime',
'label' =>
'DeliveryDate',
'enabled' =>
'getDolGlobalInt("ORDER_DISABLE_DELIVERY_DATE") ? 0 : 1',
'visible' => 1,
'position' => 74),
406 'fk_warehouse' => array(
'type' =>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label' =>
'DefaultWarehouse',
'enabled' =>
'isModEnabled("stock")',
'visible' => -1,
'position' => 75,
'nodepth' => 1),
407 'fk_user_author' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserAuthor',
'enabled' => 1,
'visible' => 3,
'position' => 41),
408 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' => 1,
'visible' => 3,
'notnull' => -1,
'position' => 80),
409 'fk_user_valid' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserValidation',
'enabled' => 1,
'visible' => 3,
'position' => 711),
410 'fk_user_approve' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserApproval',
'enabled' => 1,
'visible' => 3,
'position' => 721),
411 'fk_user_approve2' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserApproval2',
'enabled' => 1,
'visible' => 3,
'position' => 726),
412 'source' => array(
'type' =>
'smallint(6)',
'label' =>
'Source',
'enabled' => 1,
'visible' => 3,
'notnull' => 1,
'position' => 100),
413 'billed' => array(
'type' =>
'smallint(6)',
'label' =>
'Billed',
'enabled' => 1,
'visible' => 1,
'position' => 710),
414 'total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'AmountHT',
'enabled' => 1,
'visible' => 1,
'position' => 130,
'isameasure' => 1),
415 'total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'AmountVAT',
'enabled' => 1,
'visible' => 1,
'position' => 135,
'isameasure' => 1),
416 'localtax1' => array(
'type' =>
'double(24,8)',
'label' =>
'LT1',
'enabled' => 1,
'visible' => 3,
'position' => 140,
'isameasure' => 1),
417 'localtax2' => array(
'type' =>
'double(24,8)',
'label' =>
'LT2',
'enabled' => 1,
'visible' => 3,
'position' => 145,
'isameasure' => 1),
418 'total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'AmountTTC',
'enabled' => 1,
'visible' => -1,
'position' => 150,
'isameasure' => 1),
419 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => 0,
'position' => 750,
'searchall' => 1),
420 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => 0,
'position' => 760,
'searchall' => 1),
421 'model_pdf' => array(
'type' =>
'varchar(255)',
'label' =>
'ModelPDF',
'enabled' => 1,
'visible' => 0,
'position' => 165),
422 'fk_input_method' => array(
'type' =>
'integer',
'label' =>
'OrderMode',
'enabled' => 1,
'visible' => 3,
'position' => 170),
423 'fk_cond_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentTerm',
'enabled' => 1,
'visible' => 3,
'position' => 175),
424 'deposit_percent' => array(
'type' =>
'varchar(63)',
'label' =>
'DepositPercent',
'enabled' => 1,
'visible' => -1,
'position' => 176),
425 'fk_mode_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentMode',
'enabled' => 1,
'visible' => 3,
'position' => 180),
426 'extraparams' => array(
'type' =>
'varchar(255)',
'label' =>
'Extraparams',
'enabled' => 1,
'visible' => 0,
'position' => 190),
427 'fk_account' => array(
'type' =>
'integer',
'label' =>
'BankAccount',
'enabled' =>
'isModEnabled("bank")',
'visible' => 3,
'position' => 200),
428 'fk_incoterms' => array(
'type' =>
'integer',
'label' =>
'IncotermCode',
'enabled' => 1,
'visible' => 3,
'position' => 205),
429 'location_incoterms' => array(
'type' =>
'varchar(255)',
'label' =>
'IncotermLocation',
'enabled' => 1,
'visible' => 3,
'position' => 210),
430 'fk_multicurrency' => array(
'type' =>
'integer',
'label' =>
'Currency',
'enabled' => 1,
'visible' => 0,
'position' => 215),
431 'multicurrency_code' => array(
'type' =>
'varchar(255)',
'label' =>
'Currency',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 220),
432 'multicurrency_tx' => array(
'type' =>
'double(24,8)',
'label' =>
'CurrencyRate',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 225),
433 'multicurrency_total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountHT',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 230),
434 'multicurrency_total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountVAT',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 235),
435 'multicurrency_total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'MulticurrencyAmountTTC',
'enabled' =>
'isModEnabled("multicurrency")',
'visible' => -1,
'position' => 240),
436 'date_creation' => array(
'type' =>
'datetime',
'label' =>
'Date creation',
'enabled' => 1,
'visible' => -1,
'position' => 500),
437 'fk_soc' => array(
'type' =>
'integer:Societe:societe/class/societe.class.php',
'label' =>
'ThirdParty',
'enabled' =>
'isModEnabled("societe")',
'visible' => 1,
'notnull' => 1,
'position' => 50),
438 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'default' =>
'1',
'enabled' => 1,
'visible' => 0,
'notnull' => 1,
'position' => 1000,
'index' => 1),
439 'tms' => array(
'type' =>
'datetime',
'label' =>
"DateModificationShort",
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 501),
440 'last_main_doc' => array(
'type' =>
'varchar(255)',
'label' =>
'LastMainDoc',
'enabled' => 1,
'visible' => 0,
'position' => 700),
441 'fk_statut' => array(
'type' =>
'smallint(6)',
'label' =>
'Status',
'enabled' => 1,
'visible' => 1,
'position' => 701),
442 'import_key' => array(
'type' =>
'varchar(14)',
'label' =>
'ImportId',
'enabled' => 1,
'visible' => 0,
'position' => 900),
506 $this->ismultientitymanaged = 1;
517 public function fetch($id, $ref =
'')
520 if (empty($id) && empty($ref)) {
524 $sql =
"SELECT c.rowid, c.entity, c.ref, ref_supplier, c.fk_soc, c.fk_statut as status, c.amount_ht, c.total_ht, c.total_ttc, c.total_tva,";
525 $sql .=
" c.localtax1, c.localtax2, ";
526 $sql .=
" c.date_creation, c.date_valid, c.date_approve, c.date_approve2,";
527 $sql .=
" c.fk_user_author as user_author_id, c.fk_user_valid as user_validation_id, c.fk_user_approve as user_approve_id, c.fk_user_approve2 as user_approve_id2,";
528 $sql .=
" c.date_commande as date_commande, c.date_livraison as delivery_date, c.fk_warehouse, c.fk_cond_reglement, c.fk_mode_reglement, c.fk_projet as fk_project, c.remise_percent, c.source, c.fk_input_method,";
529 $sql .=
" c.fk_account,";
530 $sql .=
" c.note_private, c.note_public, c.model_pdf, c.last_main_doc, c.extraparams, c.billed,";
531 $sql .=
" c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc,";
532 $sql .=
" cm.libelle as methode_commande,";
533 $sql .=
" cr.code as cond_reglement_code, cr.libelle as cond_reglement_label, cr.libelle_facture as cond_reglement_doc, c.deposit_percent,";
534 $sql .=
" p.code as mode_reglement_code, p.libelle as mode_reglement_libelle";
535 $sql .=
', c.fk_incoterms, c.location_incoterms';
536 $sql .=
', c.last_main_doc';
537 $sql .=
', i.libelle as label_incoterms';
538 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseur as c";
539 $sql .=
" LEFT JOIN ".$this->db->prefix().
"c_payment_term as cr ON c.fk_cond_reglement = cr.rowid";
540 $sql .=
" LEFT JOIN ".$this->db->prefix().
"c_paiement as p ON c.fk_mode_reglement = p.id";
541 $sql .=
" LEFT JOIN ".$this->db->prefix().
"c_input_method as cm ON cm.rowid = c.fk_input_method";
542 $sql .=
' LEFT JOIN '.$this->db->prefix().
'c_incoterms as i ON c.fk_incoterms = i.rowid';
545 $sql .=
" WHERE c.entity IN (".getEntity(
'supplier_order').
")";
547 $sql .=
" WHERE c.rowid=".((int) $id);
551 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
554 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
555 $resql = $this->db->query($sql);
557 $obj = $this->db->fetch_object($resql);
559 $this->error =
'Bill with id '.$id.
' not found';
560 dol_syslog(get_class($this).
'::fetch '.$this->error);
564 $this->
id = $obj->rowid;
565 $this->entity = $obj->entity;
567 $this->
ref = $obj->ref;
568 $this->ref_supplier = $obj->ref_supplier;
570 $this->socid = $obj->fk_soc;
571 $this->thirdparty =
null;
573 $this->fourn_id = $obj->fk_soc;
574 $this->statut = $obj->status;
575 $this->
status = $obj->status;
576 $this->billed = $obj->billed;
577 $this->last_main_doc = $obj->last_main_doc;
578 $this->user_author_id = $obj->user_author_id;
579 $this->user_validation_id = $obj->user_validation_id;
580 $this->user_approve_id = $obj->user_approve_id;
581 $this->user_approve_id2 = $obj->user_approve_id2;
582 $this->total_ht = $obj->total_ht;
583 $this->total_tva = $obj->total_tva;
584 $this->total_localtax1 = $obj->localtax1;
585 $this->total_localtax2 = $obj->localtax2;
586 $this->total_ttc = $obj->total_ttc;
587 $this->date_creation = $this->db->jdate($obj->date_creation);
588 $this->date_valid = $this->db->jdate($obj->date_valid);
589 $this->date_approve = $this->db->jdate($obj->date_approve);
590 $this->date_approve2 = $this->db->jdate($obj->date_approve2);
591 $this->date_commande = $this->db->jdate($obj->date_commande);
592 if (isset($obj->date_commande)) {
593 $this->
date = $this->date_commande;
595 $this->
date = $this->date_creation;
597 $this->delivery_date = $this->db->jdate($obj->delivery_date);
598 $this->fk_warehouse = $obj->fk_warehouse;
599 $this->remise_percent = $obj->remise_percent;
600 $this->methode_commande_id = $obj->fk_input_method;
601 $this->methode_commande = $obj->methode_commande;
603 $this->source = $obj->source;
604 $this->fk_project = $obj->fk_project;
605 $this->cond_reglement_id = $obj->fk_cond_reglement;
606 $this->cond_reglement_code = $obj->cond_reglement_code;
607 $this->cond_reglement_label = $obj->cond_reglement_label;
608 $this->cond_reglement_doc = $obj->cond_reglement_doc;
609 $this->deposit_percent = $obj->deposit_percent;
610 $this->fk_account = $obj->fk_account;
611 $this->mode_reglement_id = $obj->fk_mode_reglement;
612 $this->mode_reglement_code = $obj->mode_reglement_code;
613 $this->mode_reglement = $obj->mode_reglement_libelle;
614 $this->note = $obj->note_private;
615 $this->note_private = $obj->note_private;
616 $this->note_public = $obj->note_public;
617 $this->model_pdf = $obj->model_pdf;
618 $this->last_main_doc = $obj->last_main_doc;
621 $this->fk_incoterms = $obj->fk_incoterms;
622 $this->location_incoterms = $obj->location_incoterms;
623 $this->label_incoterms = $obj->label_incoterms;
626 $this->fk_multicurrency = $obj->fk_multicurrency;
627 $this->multicurrency_code = $obj->multicurrency_code;
628 $this->multicurrency_tx = $obj->multicurrency_tx;
629 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
630 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
631 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
633 $this->extraparams = isset($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
635 $this->db->free($resql);
650 $this->error = $this->db->error().
" sql=".$sql;
666 $this->lines = array();
668 $sql =
"SELECT l.rowid, l.fk_commande, l.ref as ref_supplier, l.fk_product, l.product_type, l.label, l.description, l.qty,";
669 $sql .=
" l.vat_src_code, l.tva_tx, l.remise_percent, l.subprice, l.subprice_ttc,";
670 $sql .=
" l.localtax1_tx, l. localtax2_tx, l.localtax1_type, l. localtax2_type, l.total_localtax1, l.total_localtax2,";
671 $sql .=
" l.total_ht, l.total_tva, l.total_ttc, l.info_bits, l.special_code, l.fk_parent_line, l.rang,";
672 $sql .=
" p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch, p.barcode as product_barcode, p.stockable_product,";
673 $sql .=
" l.fk_unit, l.extraparams,";
674 $sql .=
" l.date_start, l.date_end,";
675 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_subprice_ttc, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc';
676 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseurdet as l";
677 $sql .=
' LEFT JOIN '.$this->db->prefix().
'product as p ON l.fk_product = p.rowid';
678 $sql .=
" WHERE l.fk_commande = ".((int) $this->
id);
680 $sql .=
' AND p.fk_product_type = 0';
682 $sql .=
" ORDER BY l.rang, l.rowid";
685 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
687 $result = $this->db->query($sql);
689 $num = $this->db->num_rows($result);
693 $objp = $this->db->fetch_object($result);
697 $line->id = $objp->rowid;
698 $line->fk_commande = $objp->fk_commande;
699 $line->desc = $objp->description;
700 $line->description = $objp->description;
701 $line->qty = $objp->qty;
702 $line->tva_tx = $objp->tva_tx;
703 $line->localtax1_tx = $objp->localtax1_tx;
704 $line->localtax2_tx = $objp->localtax2_tx;
705 $line->localtax1_type = $objp->localtax1_type;
706 $line->localtax2_type = $objp->localtax2_type;
707 $line->subprice = $objp->subprice;
708 $line->pu_ht = $objp->subprice;
709 $line->subprice_ttc = $objp->subprice_ttc;
710 $line->pu_ttc = $objp->subprice_ttc;
711 $line->remise_percent = $objp->remise_percent;
713 $line->vat_src_code = $objp->vat_src_code;
714 $line->total_ht = $objp->total_ht;
715 $line->total_tva = $objp->total_tva;
716 $line->total_localtax1 = $objp->total_localtax1;
717 $line->total_localtax2 = $objp->total_localtax2;
718 $line->total_ttc = $objp->total_ttc;
719 $line->product_type = $objp->product_type;
721 $line->fk_product = $objp->fk_product;
723 $line->libelle = $objp->product_label;
724 $line->product_label = $objp->product_label;
725 $line->product_desc = $objp->product_desc;
726 $line->product_tobatch = $objp->product_tobatch;
727 $line->product_barcode = $objp->product_barcode;
728 $line->stockable_product = $objp->stockable_product;
730 $line->ref = $objp->product_ref;
731 $line->product_ref = $objp->product_ref;
732 $line->ref_fourn = $objp->ref_supplier;
733 $line->ref_supplier = $objp->ref_supplier;
740 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.$this->db->prefix().
"product_fournisseur_price";
741 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'productsupplierprice').
")";
742 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
743 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
744 $sqlsearchpackage .=
" AND quantity <= ".abs((
float) $objp->qty);
745 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".abs((
float) $objp->qty).
")";
746 $sqlsearchpackage .=
" AND fk_soc = ".((int) $this->socid);
747 $sqlsearchpackage .=
" ORDER BY packaging ASC";
748 $sqlsearchpackage .=
" LIMIT 1";
750 $resqlsearchpackage = $this->db->query($sqlsearchpackage);
751 if ($resqlsearchpackage) {
752 $objsearchpackage = $this->db->fetch_object($resqlsearchpackage);
753 if ($objsearchpackage) {
754 $line->fk_fournprice = $objsearchpackage->rowid;
755 $line->packaging = (float) $objsearchpackage->packaging;
758 $this->error = $this->db->lasterror();
763 $line->date_start = $this->db->jdate($objp->date_start);
764 $line->date_end = $this->db->jdate($objp->date_end);
765 $line->fk_unit = $objp->fk_unit;
767 $line->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
770 $line->fk_multicurrency = $objp->fk_multicurrency;
771 $line->multicurrency_code = $objp->multicurrency_code;
772 $line->multicurrency_subprice = $objp->multicurrency_subprice;
773 $line->multicurrency_subprice_ttc = $objp->multicurrency_subprice_ttc;
774 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
775 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
776 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
778 $line->info_bits = $objp->info_bits;
779 $line->special_code = $objp->special_code;
780 $line->fk_parent_line = $objp->fk_parent_line;
782 $line->rang = $objp->rang;
786 $line->fetch_optionals();
788 $this->lines[$i] = $line;
792 $this->db->free($result);
796 $this->error = $this->db->error().
" sql=".$sql;
809 public function valid($user, $idwarehouse = 0, $notrigger = 0)
812 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
818 if ((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && ($user->hasRight(
"fournisseur",
"commande",
"creer") || $user->hasRight(
"supplier_order",
"creer")))
819 || (
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && $user->hasRight(
"fournisseur",
"supplier_order_advance",
"validate"))) {
823 dol_syslog(get_class($this).
"::valid checkActiveProductInLines ".$this->error, LOG_INFO);
828 $soc->fetch((
int) $this->fourn_id);
831 if (preg_match(
'/^[\(]?PROV/i', (
string) $this->
ref) || empty($this->ref)) {
832 $num = $this->getNextNumRef($soc);
834 $num = (string) $this->ref;
838 $sql =
'UPDATE '.$this->db->prefix().
"commande_fournisseur";
839 $sql .=
" SET ref='".$this->db->escape($num).
"',";
840 $sql .=
" fk_statut = ".((int) self::STATUS_VALIDATED).
",";
841 $sql .=
" date_valid='".$this->db->idate(
dol_now()).
"',";
842 $sql .=
" fk_user_valid = ".((int) $user->id);
843 $sql .=
" WHERE rowid = ".((int) $this->
id);
844 $sql .=
" AND fk_statut = ".((int) self::STATUS_DRAFT);
846 $resql = $this->db->query($sql);
848 dol_print_error($this->db);
852 if (!$error && !$notrigger) {
854 $result = $this->call_trigger(
'ORDER_SUPPLIER_VALIDATE', $user);
862 $this->oldref = $this->ref;
865 if (preg_match(
'/^[\(]?PROV/i', $this->ref)) {
867 $sql =
'UPDATE '.$this->db->prefix().
"ecm_files set filename = CONCAT('".$this->db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->ref) + 1).
")), filepath = 'fournisseur/commande/".$this->db->escape($this->newref).
"'";
868 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->ref).
"%' AND filepath = 'fournisseur/commande/".$this->db->escape($this->ref).
"' and entity = ".((int)
$conf->entity);
869 $resql = $this->db->query($sql);
872 $this->error = $this->db->lasterror();
874 $sql =
'UPDATE '.$this->db->prefix().
"ecm_files set filepath = 'fournisseur/commande/".$this->db->escape($this->newref).
"'";
875 $sql .=
" WHERE filepath = 'fournisseur/commande/".$this->db->escape($this->ref).
"' and entity = ".((int)
$conf->entity);
876 $resql = $this->db->query($sql);
879 $this->error = $this->db->lasterror();
885 $dirsource =
$conf->fournisseur->commande->dir_output.
'/'.$oldref;
886 $dirdest =
$conf->fournisseur->commande->dir_output.
'/'.$newref;
887 if (!$error && file_exists($dirsource)) {
888 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
890 if (@rename($dirsource, $dirdest)) {
893 $listoffiles =
dol_dir_list(
$conf->fournisseur->commande->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
894 foreach ($listoffiles as $fileentry) {
895 $dirsource = $fileentry[
'name'];
896 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
897 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
898 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
899 @rename($dirsource, $dirdest);
917 $this->db->rollback();
921 $this->error =
'NotAuthorized';
922 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
947 public function LibStatut($status, $mode = 0, $billed = 0)
950 global $langs, $hookmanager;
952 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
953 $langs->load(
'orders');
955 $this->labelStatus[0] =
'StatusSupplierOrderDraft';
956 $this->labelStatus[1] =
'StatusSupplierOrderValidated';
957 $this->labelStatus[2] =
'StatusSupplierOrderApproved';
959 $this->labelStatus[3] =
'StatusSupplierOrderOnProcess';
961 $this->labelStatus[3] =
'StatusSupplierOrderOnProcessWithValidation';
963 $this->labelStatus[4] =
'StatusSupplierOrderReceivedPartially';
964 $this->labelStatus[5] =
'StatusSupplierOrderReceivedAll';
965 $this->labelStatus[6] =
'StatusSupplierOrderCanceled';
966 $this->labelStatus[7] =
'StatusSupplierOrderCanceled';
967 $this->labelStatus[9] =
'StatusSupplierOrderRefused';
970 $this->labelStatusShort[0] =
'StatusSupplierOrderDraftShort';
971 $this->labelStatusShort[1] =
'StatusSupplierOrderValidatedShort';
972 $this->labelStatusShort[2] =
'StatusSupplierOrderApprovedShort';
973 $this->labelStatusShort[3] =
'StatusSupplierOrderOnProcessShort';
974 $this->labelStatusShort[4] =
'StatusSupplierOrderReceivedPartiallyShort';
975 $this->labelStatusShort[5] =
'StatusSupplierOrderReceivedAllShort';
976 $this->labelStatusShort[6] =
'StatusSupplierOrderCanceledShort';
977 $this->labelStatusShort[7] =
'StatusSupplierOrderCanceledShort';
978 $this->labelStatusShort[9] =
'StatusSupplierOrderRefusedShort';
981 $statustrans = array(
993 $statusClass =
'status0';
994 if (!empty($statustrans[$status])) {
995 $statusClass = $statustrans[$status];
1000 $billedtext =
' - '.$langs->trans(
"Billed");
1002 if ($status == 5 && $billed) {
1003 $statusClass =
'status6';
1006 $statusLong = $langs->transnoentitiesnoconv($this->labelStatus[$status]).$billedtext;
1007 $statusShort = $langs->transnoentitiesnoconv($this->labelStatusShort[$status]);
1009 $parameters = array(
'status' => $status,
'mode' => $mode,
'billed' => $billed);
1010 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
1012 return $hookmanager->resPrint;
1015 return dolGetStatus($statusLong, $statusShort,
'', $statusClass, $mode);
1026 global
$conf, $langs, $user;
1028 $langs->loadLangs([
'bills',
'orders']);
1031 $nofetch = !empty($params[
'nofetch']);
1033 if ($user->hasRight(
"fournisseur",
"commande",
"read")) {
1034 $datas[
'picto'] =
'<u class="paddingrightonly">'.$langs->trans(
"SupplierOrder").
'</u>';
1036 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
1038 if (!empty($this->
ref)) {
1039 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1041 if (!empty($this->ref_supplier)) {
1042 $datas[
'refsupplier'] =
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
1045 $langs->load(
'companies');
1046 if (empty($this->thirdparty)) {
1049 $datas[
'supplier'] =
'<br><b>'.$langs->trans(
'Supplier').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
1051 if (!empty($this->total_ht)) {
1052 $datas[
'totalht'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1,
$conf->currency);
1054 if (!empty($this->total_tva)) {
1055 $datas[
'totaltva'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1,
$conf->currency);
1057 if (!empty($this->total_ttc)) {
1058 $datas[
'totalttc'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1,
$conf->currency);
1060 if (!empty($this->
date)) {
1061 $datas[
'date'] =
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->
date,
'day');
1063 if (!empty($this->delivery_date)) {
1064 $langs->load(
"sendings");
1065 $datas[
'deliverydate'] =
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
1081 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
1083 global $langs, $user, $hookmanager;
1088 'objecttype' => $this->element,
1089 'option' => $option,
1092 $classfortooltip =
'classfortooltip';
1095 $classfortooltip =
'classforajaxtooltip';
1096 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
1102 $url = DOL_URL_ROOT.
'/fourn/commande/card.php?id='.$this->id;
1104 if ($option !==
'nolink') {
1106 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1107 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1108 $add_save_lastsearch_values = 1;
1110 if ($add_save_lastsearch_values) {
1111 $url .=
'&save_lastsearch_values=1';
1116 if (empty($notooltip)) {
1118 $label = $langs->trans(
"ShowOrder");
1119 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
1121 $linkclose .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
1122 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
1125 $linkstart =
'<a href="'.$url.
'"';
1126 $linkstart .= $linkclose.
'>';
1129 $result .= $linkstart;
1131 $result .= img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
1133 if ($withpicto != 2) {
1134 $result .= $this->ref;
1136 $result .= $linkend;
1138 if ($addlinktonotes) {
1139 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
1142 $result .=
' <span class="note inline-block">';
1143 $result .=
'<a href="'.DOL_URL_ROOT.
'/fourn/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
1144 $result .= img_picto(
'',
'note');
1148 $result .=
'</span>';
1153 $hookmanager->initHooks(array($this->element .
'dao'));
1154 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1155 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1157 $result = $hookmanager->resPrint;
1159 $result .= $hookmanager->resPrint;
1172 public function getNextNumRef($soc)
1174 global $langs,
$conf;
1175 $langs->load(
"orders");
1184 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
1186 foreach ($dirmodels as $reldir) {
1187 $dir =
dol_buildpath($reldir.
"core/modules/supplier_order/");
1190 $mybool = ((bool) @include_once $dir.$file) || $mybool;
1194 dol_print_error(
null,
"Failed to include file ".$file);
1198 $obj =
new $classname();
1199 '@phan-var-force ModeleNumRefSuppliersOrders $obj';
1201 $numref = $obj->getNextValue($soc, $this);
1203 if ($numref !=
"") {
1206 $this->error = $obj->error;
1210 $this->error =
"Error_COMMANDE_SUPPLIER_ADDON_NotDefined";
1225 if ($this->billed) {
1231 $sql =
'UPDATE '.$this->db->prefix().
'commande_fournisseur SET billed = 1';
1234 if ($this->db->query($sql)) {
1236 $result = $this->call_trigger(
'ORDER_SUPPLIER_CLASSIFY_BILLED', $user);
1245 $this->db->commit();
1248 $this->db->rollback();
1252 dol_print_error($this->db);
1254 $this->db->rollback();
1268 if (empty($this->billed)) {
1274 $sql =
'UPDATE '.$this->db->prefix().
'commande_fournisseur SET billed = 0';
1278 if (!$this->db->query($sql)) {
1279 dol_print_error($this->db);
1280 $this->db->rollback();
1285 $result = $this->call_trigger(
'ORDER_SUPPLIER_CLASSIFY_UNBILLED', $user);
1287 $this->db->rollback();
1293 $this->db->commit();
1306 public function approve($user, $idwarehouse = 0, $secondlevel = 0)
1310 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1316 if ($user->hasRight(
"fournisseur",
"commande",
"approuver")) {
1322 $soc =
new Societe($this->db);
1323 $soc->fetch((
int) $this->fourn_id);
1326 if (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
1327 $num = $this->getNextNumRef($soc);
1329 $num = (string) $this->
ref;
1334 $movetoapprovestatus =
true;
1337 $sql =
"UPDATE ".$this->db->prefix().
"commande_fournisseur";
1338 $sql .=
" SET ref='".$this->db->escape($num).
"',";
1339 if (empty($secondlevel)) {
1340 $sql .=
" date_approve='".$this->db->idate($now).
"',";
1341 $sql .=
" fk_user_approve = ".((int) $user->id);
1343 if (empty($this->user_approve_id2)) {
1344 $movetoapprovestatus =
false;
1345 $comment =
' (first level)';
1349 $sql .=
" date_approve2='".$this->db->idate($now).
"',";
1350 $sql .=
" fk_user_approve2 = ".((int) $user->id);
1351 if (empty($this->user_approve_id)) {
1352 $movetoapprovestatus =
false;
1354 $comment =
' (second level)';
1357 if ($movetoapprovestatus) {
1358 $sql .=
", fk_statut = ".self::STATUS_ACCEPTED;
1360 $sql .=
", fk_statut = ".self::STATUS_VALIDATED;
1362 $sql .=
" WHERE rowid = ".((int) $this->
id);
1363 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
1365 if ($this->db->query($sql)) {
1367 $result = $this->
add_contact($user->id,
'SALESREPFOLL',
'internal', 1);
1368 if ($result < 0 && $result != -2) {
1375 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
1376 $langs->load(
"agenda");
1378 $cpt = count($this->lines);
1379 for ($i = 0; $i < $cpt; $i++) {
1381 if ($this->lines[$i]->fk_product > 0) {
1382 $this->line = $this->lines[$i];
1384 $mouvP->origin = &$this;
1385 $mouvP->setOrigin($this->element, $this->
id);
1387 $up_ht_disc = $this->lines[$i]->subprice;
1388 if (!empty($this->lines[$i]->remise_percent) && !
getDolGlobalString(
'STOCK_EXCLUDE_DISCOUNT_FOR_PMP')) {
1389 $up_ht_disc =
price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100,
'MU');
1391 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans(
"OrderApprovedInDolibarr", $this->ref));
1402 $result = $this->call_trigger(
'ORDER_SUPPLIER_APPROVE', $user);
1410 $this->
ref = $this->newref;
1412 if ($movetoapprovestatus) {
1419 if (empty($secondlevel)) {
1420 $this->date_approve = $now;
1421 $this->user_approve_id = $user->id;
1423 $this->date_approve2 = $now;
1424 $this->user_approve_id2 = $user->id;
1427 $this->db->commit();
1430 $this->db->rollback();
1434 $this->db->rollback();
1435 $this->error = $this->db->lasterror();
1439 dol_syslog(get_class($this).
"::approve Not Authorized", LOG_ERR);
1456 if ($user->hasRight(
"fournisseur",
"commande",
"approuver")) {
1459 $sql =
"UPDATE ".$this->db->prefix().
"commande_fournisseur SET fk_statut = ".
self::STATUS_REFUSED;
1460 $sql .=
" WHERE rowid = ".((int) $this->
id);
1462 if ($this->db->query($sql)) {
1467 $result = $this->call_trigger(
'ORDER_SUPPLIER_REFUSE', $user);
1470 $this->db->rollback();
1472 $this->db->commit();
1477 $this->db->rollback();
1478 $this->error = $this->db->lasterror();
1479 dol_syslog(get_class($this).
"::refuse Error -1");
1483 dol_syslog(get_class($this).
"::refuse Not Authorized");
1497 public function cancel($user, $idwarehouse = -1)
1504 if ($user->hasRight(
"fournisseur",
"commande",
"commander")) {
1509 $sql =
"UPDATE ".$this->db->prefix().
"commande_fournisseur SET fk_statut = ".((int) $statut);
1510 $sql .=
" WHERE rowid = ".((int) $this->
id);
1511 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
1512 if ($this->db->query($sql)) {
1516 $result = $this->call_trigger(
'ORDER_SUPPLIER_CANCEL', $user);
1523 $this->db->commit();
1526 $this->db->rollback();
1530 $this->db->rollback();
1531 $this->error = $this->db->lasterror();
1532 dol_syslog(get_class($this).
"::cancel ".$this->error);
1536 dol_syslog(get_class($this).
"::cancel Not Authorized");
1550 public function commande($user, $date, $methode, $comment =
'')
1555 if ($user->hasRight(
"fournisseur",
"commande",
"commander")) {
1558 $newnoteprivate = $this->note_private;
1560 $newnoteprivate =
dol_concatdesc($newnoteprivate, $langs->trans(
"Comment").
': '.$comment);
1563 $sql =
"UPDATE ".$this->db->prefix().
"commande_fournisseur";
1564 $sql .=
" SET fk_statut=".self::STATUS_ORDERSENT.
", fk_input_method=".((int) $methode).
", date_commande='".$this->db->idate($date).
"', ";
1565 $sql .=
" note_private='".$this->db->escape((
string) $newnoteprivate).
"'";
1566 $sql .=
" WHERE rowid=".((int) $this->
id);
1568 dol_syslog(get_class($this).
"::commande", LOG_DEBUG);
1569 if ($this->db->query($sql)) {
1572 $this->methode_commande_id = $methode;
1573 $this->date_commande = $date;
1574 $this->context[
'comments'] = $comment;
1577 $result = $this->call_trigger(
'ORDER_SUPPLIER_SUBMIT', $user);
1584 $this->error = $this->db->lasterror();
1585 $this->errors[] = $this->db->lasterror();
1589 $this->db->commit();
1591 $this->db->rollback();
1595 $this->error = $langs->trans(
'NotAuthorized');
1596 $this->errors[] = $langs->trans(
'NotAuthorized');
1597 dol_syslog(get_class($this).
"::commande User not Authorized", LOG_WARNING);
1600 return ($error ? -1 : 1);
1610 public function create($user, $notrigger = 0)
1620 $date = ($this->date_commande ? $this->date_commande : $this->date);
1624 $delivery_date = $this->delivery_date;
1627 if (empty($this->source)) {
1632 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1637 if (empty($this->fk_multicurrency)) {
1638 $this->multicurrency_code =
$conf->currency;
1639 $this->fk_multicurrency = 0;
1640 $this->multicurrency_tx = 1;
1648 $sql =
"INSERT INTO ".$this->db->prefix().
"commande_fournisseur (";
1650 $sql .=
", ref_supplier";
1651 $sql .=
", note_private";
1652 $sql .=
", note_public";
1655 $sql .=
", fk_projet";
1656 $sql .=
", date_creation";
1657 $sql .=
", date_livraison";
1658 $sql .=
", fk_warehouse";
1659 $sql .=
", fk_user_author";
1660 $sql .=
", fk_statut";
1662 $sql .=
", model_pdf";
1663 $sql .=
", fk_mode_reglement";
1664 $sql .=
", deposit_percent";
1665 $sql .=
", fk_cond_reglement";
1666 $sql .=
", fk_account";
1667 $sql .=
", fk_incoterms, location_incoterms";
1668 $sql .=
", fk_multicurrency";
1669 $sql .=
", multicurrency_code";
1670 $sql .=
", multicurrency_tx";
1672 $sql .=
" VALUES (";
1674 $sql .=
", ".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"NULL");
1675 $sql .=
", '".$this->db->escape($this->note_private).
"'";
1676 $sql .=
", '".$this->db->escape($this->note_public).
"'";
1677 $sql .=
", ".((int) $this->entity);
1678 $sql .=
", ".((int) $this->socid);
1679 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
1680 $sql .=
", '".$this->db->idate($date).
"'";
1681 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
1682 $sql .=
", ".($this->fk_warehouse > 0 ? ((int) $this->fk_warehouse) :
"null");
1683 $sql .=
", ".((int) $user->id);
1684 $sql .=
", ".self::STATUS_DRAFT;
1685 $sql .=
", ".((int) $this->source);
1686 $sql .=
", '".$this->db->escape(
getDolGlobalString(
'COMMANDE_SUPPLIER_ADDON_PDF')).
"'";
1687 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
'null');
1688 $sql .=
", ".(!empty($this->deposit_percent) ?
"'" . $this->db->escape($this->deposit_percent) .
"'" :
"null");
1689 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
'null');
1690 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
1691 $sql .=
", ".(int) $this->fk_incoterms;
1692 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
1693 $sql .=
", ".(int) $this->fk_multicurrency;
1694 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1695 $sql .=
", ".(float) $this->multicurrency_tx;
1698 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1699 if ($this->db->query($sql)) {
1700 $this->
id = $this->db->last_insert_id($this->db->prefix().
"commande_fournisseur");
1703 $num = count($this->lines);
1706 for ($i = 0; $i < $num; $i++) {
1707 $line = $this->lines[$i];
1708 if (!is_object($line)) {
1712 foreach ($line as $key => $val) {
1713 $lineobj->$key = $val;
1716 $this->lines[$i] = $line;
1726 $line_price_base_type = $line->getPriceBaseType();
1727 $line_pu_devise = ($line_price_base_type ===
'TTC') ? 0 : (float) $line->multicurrency_subprice;
1729 (
string) $line->desc,
1730 (
float) $line->subprice,
1733 (
float) $line->localtax1_tx,
1734 (
float) $line->localtax2_tx,
1735 (
int) $line->fk_product,
1737 (
string) ($line->ref_supplier ? $line->ref_supplier : $line->ref_fourn),
1738 (
float) $line->remise_percent,
1739 $line_price_base_type,
1740 (float) $line->subprice_ttc,
1741 (
int) $line->product_type,
1742 (int) $line->info_bits,
1744 $line->date_start ??
null,
1745 $line->date_end ??
null,
1746 $line->array_options,
1747 $line->fk_unit ??
null,
1749 (
string) $line->origin,
1750 (int) $line->origin_id,
1751 (
int) ($line->rang > 0 ? $line->rang : -1),
1752 (
int) $line->special_code,
1753 isset($line->product_label) ? $line->product_label :
''
1756 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
1757 $this->db->rollback();
1762 $sql =
"UPDATE ".$this->db->prefix().
"commande_fournisseur";
1763 $sql .=
" SET ref='(PROV".((int) $this->
id).
")'";
1764 $sql .=
" WHERE rowid=".((int) $this->
id);
1766 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1767 if ($this->db->query($sql)) {
1770 $this->
ref =
"(PROV".$this->id.
")";
1772 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1773 $this->linked_objects = $this->linkedObjectsIds;
1777 if (!empty($this->linked_objects) && is_array($this->linked_objects)) {
1778 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1779 if (is_array($tmp_origin_id)) {
1780 foreach ($tmp_origin_id as $origin_id) {
1783 dol_print_error($this->db);
1788 $origin_id = $tmp_origin_id;
1791 dol_print_error($this->db);
1806 if (!$error && !$notrigger) {
1808 $result = $this->call_trigger(
'ORDER_SUPPLIER_CREATE', $user);
1810 $this->db->rollback();
1817 $this->db->commit();
1820 $this->error = $this->db->lasterror();
1821 $this->db->rollback();
1826 $this->error =
'Failed to get ID of inserted line';
1831 $this->error = $this->db->lasterror();
1832 $this->db->rollback();
1850 if (isset($this->
ref)) {
1851 $this->
ref = trim($this->
ref);
1853 if (isset($this->ref_supplier)) {
1854 $this->ref_supplier = trim($this->ref_supplier);
1856 if (isset($this->note_private)) {
1857 $this->note_private = trim($this->note_private);
1859 if (isset($this->note_public)) {
1860 $this->note_public = trim($this->note_public);
1862 if (isset($this->model_pdf)) {
1863 $this->model_pdf = trim($this->model_pdf);
1865 if (isset($this->import_key)) {
1866 $this->import_key = trim($this->import_key);
1870 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
1872 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1873 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1874 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1875 $sql .=
" fk_soc=".(isset($this->socid) ? ((int) $this->socid) :
"null").
",";
1876 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
1877 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
1878 $sql .=
" fk_warehouse=".($this->fk_warehouse > 0 ? ((int) $this->fk_warehouse) :
"null").
",";
1879 $sql .=
" total_tva=".(isset($this->total_tva) ? ((float) $this->total_tva) :
"null").
",";
1880 $sql .=
" localtax1=".(isset($this->total_localtax1) ? ((float) $this->total_localtax1) :
"null").
",";
1881 $sql .=
" localtax2=".(isset($this->total_localtax2) ? ((float) $this->total_localtax2) :
"null").
",";
1882 $sql .=
" total_ht=".(isset($this->total_ht) ? ((float) $this->total_ht) :
"null").
",";
1883 $sql .=
" total_ttc=".(isset($this->total_ttc) ? ((float) $this->total_ttc) :
"null").
",";
1884 $sql .=
" fk_statut=".(isset($this->
status) ? ((int) $this->
status) :
"null").
",";
1885 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? ((int) $this->user_author_id) :
"null").
",";
1886 $sql .=
" fk_user_valid=".(isset($this->user_validation_id) && $this->user_validation_id > 0 ? ((int) $this->user_validation_id) :
"null").
",";
1887 $sql .=
" fk_projet=".((!empty($this->fk_project) && $this->fk_project > 0) ? ((
int) $this->fk_project) :
"null").
",";
1888 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? ((int) $this->cond_reglement_id) :
"null").
",";
1889 $sql .=
" deposit_percent=".(!empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
"null").
",";
1890 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? ((int) $this->mode_reglement_id) :
"null").
",";
1891 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
1893 $sql .=
" fk_account=".($this->fk_account > 0 ? ((int) $this->fk_account) :
"null").
",";
1895 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1896 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1897 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1898 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
1900 $sql .=
" WHERE rowid=".((int) $this->
id);
1904 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1905 $resql = $this->db->query($sql);
1908 $this->errors[] =
"Error ".$this->db->lasterror();
1918 if (!$error && !$notrigger) {
1920 $result = $this->call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
1929 foreach ($this->errors as $errmsg) {
1930 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1931 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1933 $this->db->rollback();
1936 $this->db->commit();
1951 global
$conf, $user, $hookmanager;
1958 foreach ($this->lines as $line) {
1959 $line->fetch_optionals();
1963 $objFrom = clone $this;
1966 if (!empty($socid) && $socid != $this->socid) {
1967 $objsoc =
new Societe($this->db);
1969 if ($objsoc->fetch($socid) > 0) {
1970 $this->socid = $objsoc->id;
1971 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1972 $this->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent :
'0');
1973 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1974 $this->fk_project = 0;
1975 $this->fk_delivery_address = 0;
1986 $this->user_author_id = $user->id;
1987 $this->user_validation_id = 0;
1990 $this->date_creation = 0;
1991 $this->date_validation = 0;
1992 $this->date_commande =
null;
1993 $this->ref_supplier =
'';
1994 $this->user_approve_id = 0;
1995 $this->user_approve_id2 = 0;
1996 $this->date_approve = 0;
1997 $this->date_approve2 = 0;
2000 $this->context[
'createfromclone'] =
'createfromclone';
2001 $result = $this->
create($user, $notrigger);
2008 if (is_object($hookmanager)) {
2009 $parameters = array(
'objFrom' => $objFrom);
2011 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
2019 unset($this->context[
'createfromclone']);
2023 $this->db->commit();
2026 $this->db->rollback();
2061 public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $fk_product = 0, $fk_prod_fourn_price = 0, $ref_supplier =
'', $remise_percent = 0.0, $price_base_type =
'HT', $pu_ttc = 0.0, $type = 0, $info_bits = 0, $notrigger = 0,
$date_start =
null, $date_end =
null, $array_options = [], $fk_unit =
null, $pu_ht_devise = 0, $origin =
'', $origin_id = 0, $rang = -1, $special_code = 0, $label =
'')
2065 dol_syslog(get_class($this).
"::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $fk_prod_fourn_price, $ref_supplier, $remise_percent, $price_base_type, $pu_ttc, $type, $info_bits, $notrigger, $date_start, $date_end, $fk_unit, $pu_ht_devise, $origin, $origin_id");
2066 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2068 if ($this->
status == self::STATUS_DRAFT) {
2069 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2078 if (empty($txtva)) {
2084 if (empty($txlocaltax1)) {
2087 if (empty($txlocaltax2)) {
2090 if (empty($remise_percent)) {
2091 $remise_percent = 0;
2094 $remise_percent =
price2num($remise_percent);
2097 $pu_ht_devise =
price2num($pu_ht_devise);
2099 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
2104 if ($price_base_type ==
'HT') {
2110 $label = trim((
string) $label);
2111 $desc = trim((
string) $desc);
2112 if ($desc ===
'' && $label !==
'') {
2117 if ($qty < 0 && !$fk_product) {
2118 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Product"));
2125 $langs->load(
"errors");
2126 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2133 $product_type = $type;
2136 if ($fk_product > 0) {
2137 if (
getDolGlobalInt(
'SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY') == 1) {
2139 dol_syslog(get_class($this).
"::addline we check supplier prices fk_product=".$fk_product.
" fk_prod_fourn_price=".$fk_prod_fourn_price.
" qty=".$qty.
" ref_supplier=".$ref_supplier);
2141 if (
$prod->fetch($fk_product) > 0) {
2142 $product_type =
$prod->type;
2143 $label =
$prod->label;
2147 $result =
$prod->get_buyprice($fk_prod_fourn_price, (
float) $qty, $fk_product,
'none', (isset($this->fk_soc) ? $this->fk_soc : $this->socid));
2151 if ($result > 0 && ($origin ==
'commande' || $pu ===
'')) {
2152 $pu =
$prod->fourn_pu;
2153 $ref_supplier =
$prod->ref_supplier;
2155 if ($remise_percent == 0 &&
$prod->remise_percent != 0) {
2156 $remise_percent =
$prod->remise_percent;
2160 $langs->load(
"errors");
2161 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
2162 $this->db->rollback();
2163 dol_syslog(get_class($this).
"::addline we did not found supplier price, so we can't guess unit price");
2168 if ($result == -1) {
2169 $langs->load(
"errors");
2170 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
2171 $this->db->rollback();
2172 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_DEBUG);
2176 $this->error =
$prod->error;
2177 $this->db->rollback();
2178 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_ERR);
2182 $this->error =
$prod->error;
2183 $this->db->rollback();
2191 $prod->get_buyprice($fk_prod_fourn_price, (
float) $qty, $fk_product,
'none', (empty($this->fk_soc) ? $this->socid : $this->fk_soc));
2196 if ($newqty != $qty) {
2198 setEventMessages($langs->trans(
'QtyRecalculatedWithPackaging'),
null,
'warnings');
2206 if (!empty(
$prod->fourn_qty) && (
float) $qty > 0 && (
float) $qty < (
float)
$prod->fourn_qty) {
2207 if (!empty(
$prod->packaging) && (
float)
price2num(fmod((
float)
$prod->fourn_qty, (
float)
$prod->packaging),
'MS')) {
2208 $coeff = intval((
float)
$prod->fourn_qty / (
float)
$prod->packaging) + 1;
2209 $qty = (float)
price2num((
float)
$prod->packaging * $coeff,
'MS');
2211 $qty = (float)
$prod->fourn_qty;
2213 setEventMessages($langs->trans(
'QtyRecalculatedWithPackaging'),
null,
'mesgs');
2218 if (
isModEnabled(
"multicurrency") && $pu_ht_devise > 0) {
2227 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
2228 $vat_src_code = $reg[1];
2229 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
2236 $tabprice =
calcul_price_total((
float) $qty, $pu, (
float) $remise_percent, $txtva, (
float) $txlocaltax1, (
float) $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, (
float) $pu_ht_devise);
2238 $total_ht = $tabprice[0];
2239 $total_tva = $tabprice[1];
2240 $total_ttc = $tabprice[2];
2241 $total_localtax1 = $tabprice[9];
2242 $total_localtax2 = $tabprice[10];
2243 $pu = $pu_ht = $tabprice[3];
2244 $pu_tva = $tabprice[4];
2245 $pu_ttc = $tabprice[5];
2248 $multicurrency_total_ht = $tabprice[16];
2249 $multicurrency_total_tva = $tabprice[17];
2250 $multicurrency_total_ttc = $tabprice[18];
2251 $pu_ht_devise = $tabprice[19];
2252 $multicurrency_pu_ttc = $tabprice[21];
2254 $localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2255 $localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2259 $rang = $rangmax + 1;
2265 $this->line->context = $this->context;
2267 $this->line->fk_commande = $this->id;
2268 $this->line->label = $label;
2269 $this->line->ref_fourn = $ref_supplier;
2270 $this->line->ref_supplier = $ref_supplier;
2271 $this->line->desc = $desc;
2272 $this->line->qty = $qty;
2273 $this->line->tva_tx = $txtva;
2274 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
2275 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
2276 $this->line->localtax1_type = $localtax1_type;
2277 $this->line->localtax2_type = $localtax2_type;
2278 $this->line->fk_product = $fk_product;
2279 $this->line->product_type = $product_type;
2280 $this->line->remise_percent = $remise_percent;
2281 $this->line->subprice = (float) $pu_ht;
2282 $this->line->subprice_ttc = ($price_base_type ===
'TTC') ? (
float) $pu_ttc : 0;
2284 $this->line->rang = $rang;
2285 $this->line->info_bits = $info_bits;
2287 $this->line->vat_src_code = $vat_src_code;
2288 $this->line->total_ht = (float) $total_ht;
2289 $this->line->total_tva = (float) $total_tva;
2290 $this->line->total_localtax1 = (float) $total_localtax1;
2291 $this->line->total_localtax2 = (float) $total_localtax2;
2292 $this->line->total_ttc = (float) $total_ttc;
2293 $this->line->product_type = $type;
2294 $this->line->special_code = (!empty($special_code) ? $special_code : 0);
2295 $this->line->origin = $origin;
2296 $this->line->origin_type = $origin;
2297 $this->line->origin_id = $origin_id;
2298 $this->line->fk_unit = $fk_unit;
2301 $this->line->date_end = $date_end;
2304 $this->line->fk_multicurrency = $this->fk_multicurrency;
2305 $this->line->multicurrency_code = $this->multicurrency_code;
2306 $this->line->multicurrency_subprice = (float) $pu_ht_devise;
2307 $this->line->multicurrency_subprice_ttc = (float) $multicurrency_pu_ttc;
2308 $this->line->multicurrency_total_ht = (float) $multicurrency_total_ht;
2309 $this->line->multicurrency_total_tva = (float) $multicurrency_total_tva;
2310 $this->line->multicurrency_total_ttc = (float) $multicurrency_total_ttc;
2312 $this->line->subprice = (float) $pu_ht;
2313 $this->line->price = $this->line->subprice;
2315 $this->line->remise_percent = $remise_percent;
2317 if (is_array($array_options) && count($array_options) > 0) {
2318 $this->line->array_options = $array_options;
2321 $result = $this->line->insert($notrigger);
2324 $result = $this->
update_price(1,
'auto', 0, $this->thirdparty);
2327 if (!isset($this->context[
'createfromclone'])) {
2328 if (!empty($this->line->fk_parent_line)) {
2331 } elseif ($rang > 0 && $rang <= count($this->lines)) {
2333 $linecount = count($this->lines);
2334 for ($ii = $rang; $ii <= $linecount; $ii++) {
2339 $this->lines[] = $this->line;
2341 foreach ($this->lines as $line) {
2342 if ($line->id == $origin_id) {
2343 $this->line->extraparams = $line->extraparams;
2344 $this->line->setExtraParameters();
2349 $this->db->commit();
2350 return $this->line->id;
2352 $this->db->rollback();
2357 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
2358 $this->db->rollback();
2383 public function dispatchProduct($user, $product, $qty, $entrepot, $price = 0, $comment =
'', $eatby =
'', $sellby =
'', $batch =
'', $fk_commandefourndet = 0, $notrigger = 0, $fk_reception = 0)
2385 global
$conf, $langs;
2388 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2391 if ($entrepot <= 0) {
2392 $this->error =
'ErrorBadValueForParameterWarehouse';
2396 $this->error =
'ErrorBadValueForParameterQty';
2400 $dispatchstatus = 1;
2402 $dispatchstatus = 0;
2409 if (($this->
status == self::STATUS_ORDERSENT || $this->
status == self::STATUS_RECEIVED_PARTIALLY || $this->
status == self::STATUS_RECEIVED_COMPLETELY)) {
2412 $sql =
"INSERT INTO ".$this->db->prefix().
"receptiondet_batch";
2413 $sql .=
" (fk_element, fk_product, qty, fk_entrepot, fk_user, datec, fk_elementdet, status, comment, eatby, sellby, batch, fk_reception) VALUES";
2414 $sql .=
" ('".((int) $this->
id).
"','".((int) $product).
"','".((float) $qty).
"',".($entrepot > 0 ?
"'".((int) $entrepot).
"'" :
"null").
",'".((
int) $user->id).
"','".$this->db->idate($now).
"','".((int) $fk_commandefourndet).
"', ".((int) $dispatchstatus).
", '".$this->db->escape($comment).
"', ";
2415 $sql .= ($eatby ?
"'".$this->db->idate($eatby).
"'" :
"null").
", ".($sellby ?
"'".$this->db->idate($sellby).
"'" :
"null").
", ".($batch ?
"'".$this->db->escape($batch).
"'" :
"null").
", ".($fk_reception > 0 ?
"'".$this->db->escape((
string) $fk_reception).
"'" :
"null");
2418 dol_syslog(get_class($this).
"::dispatchProduct", LOG_DEBUG);
2419 $resql = $this->db->query($sql);
2422 global
$conf, $langs, $user;
2424 $result = $this->call_trigger(
'LINEORDER_SUPPLIER_DISPATCH', $user);
2431 $this->error = $this->db->lasterror();
2440 $mouv->origin = &$this;
2441 $mouv->setOrigin($this->element, $this->
id);
2444 if (
getDolGlobalString(
'SUPPLIER_ORDER_ALLOW_NEGATIVE_QTY_FOR_SUPPLIER_ORDER_RETURN') && $qty < 0) {
2445 $result = $mouv->livraison($user, $product, $entrepot, $qty * (-1), $price, $comment, $now, $eatby, $sellby, $batch, 0, $inventorycode);
2447 $result = $mouv->reception($user, $product, $entrepot, $qty, $price, $comment, $eatby, $sellby, $batch,
'', 0, $inventorycode);
2451 $this->error = $mouv->error;
2452 $this->errors = $mouv->errors;
2453 dol_syslog(get_class($this).
"::dispatchProduct ".$this->error.
" ".implode(
',', $this->errors), LOG_ERR);
2460 $this->db->commit();
2463 $this->db->rollback();
2467 $this->error =
'BadStatusForObject';
2483 if ($this->
status == 0) {
2486 if ($line->fetch($idline) <= 0) {
2489 if ($this->
id > 0 && (
int) $line->fk_commande !== (
int) $this->id) {
2490 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
2496 foreach ($dispatchedLines as $dispatchLine) {
2497 if ($dispatchLine[
'orderlineid'] == $idline) {
2498 $this->error =
"LineAlreadyDispatched";
2499 $this->errors[] = $this->error;
2504 if ($line->delete($user, $notrigger) > 0) {
2523 public function delete(
User $user, $notrigger = 0)
2525 global $langs,
$conf;
2526 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
2532 if (empty($notrigger)) {
2534 $result = $this->call_trigger(
'ORDER_SUPPLIER_DELETE', $user);
2536 $this->errors[] =
'ErrorWhenRunningTrigger';
2537 dol_syslog(get_class($this).
"::delete ".$this->error, LOG_ERR);
2538 $this->db->rollback();
2546 if (!empty($this->linkedObjects) && array_key_exists(
'reception', $this->linkedObjects)) {
2547 foreach ($this->linkedObjects[
'reception'] as $element) {
2548 if ($element->statut >= 0) {
2549 $this->errors[] = $langs->trans(
'ReceptionExist');
2558 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"categorie_supplier_order";
2559 $sql .=
" WHERE fk_supplier_order = ".((int) $this->
id);
2561 $result = $this->db->query($sql);
2564 $this->errors[] = $this->db->lasterror();
2568 $main = $this->db->prefix().
'commande_fournisseurdet';
2571 $sql1 =
"UPDATE ".$this->db->prefix().
"commandedet SET fk_commandefourndet = NULL WHERE fk_commandefourndet IN (SELECT rowid FROM ".$this->db->sanitize($main).
" WHERE fk_commande = ".((int) $this->
id).
")";
2572 dol_syslog(__METHOD__.
" linked order lines", LOG_DEBUG);
2573 if (!$this->db->query($sql1)) {
2575 $this->error = $this->db->lasterror();
2576 $this->errors[] = $this->db->lasterror();
2581 $ef = $main.
"_extrafields";
2582 $sql =
"DELETE FROM ".$this->db->sanitize($ef).
" WHERE fk_object IN (SELECT rowid FROM ".$this->db->sanitize($main).
" WHERE fk_commande = ".((int) $this->
id).
")";
2583 dol_syslog(get_class($this).
"::delete extrafields lines", LOG_DEBUG);
2584 if (!$this->db->query($sql)) {
2585 $this->error = $this->db->lasterror();
2586 $this->errors[] = $this->db->lasterror();
2592 $sql =
"DELETE FROM ".$this->db->prefix().
"commande_fournisseurdet WHERE fk_commande = ".((int) $this->
id);
2593 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
2594 if (!$this->db->query($sql)) {
2595 $this->error = $this->db->lasterror();
2596 $this->errors[] = $this->db->lasterror();
2602 $sql =
"DELETE FROM ".$this->db->prefix().
"commande_fournisseur WHERE rowid = ".((int) $this->
id);
2603 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
2604 if ($resql = $this->db->query($sql)) {
2605 if ($this->db->affected_rows($resql) < 1) {
2606 $this->error = $this->db->lasterror();
2607 $this->errors[] = $this->db->lasterror();
2611 $this->error = $this->db->lasterror();
2612 $this->errors[] = $this->db->lasterror();
2621 $this->error =
'FailToDeleteExtraFields';
2622 $this->errors[] =
'FailToDeleteExtraFields';
2624 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
2631 $this->error =
'FailToDeleteObjectLinked';
2632 $this->errors[] =
'FailToDeleteObjectLinked';
2643 if ($conf->fournisseur->commande->dir_output && !empty($ref)) {
2644 $dir =
$conf->fournisseur->commande->dir_output.
"/".$ref;
2645 $file = $dir.
"/".$ref.
".pdf";
2646 if (file_exists($file)) {
2648 $this->error =
'ErrorFailToDeleteFile';
2649 $this->errors[] =
'ErrorFailToDeleteFile';
2653 if (file_exists($dir)) {
2656 $this->error =
'ErrorFailToDeleteDir';
2657 $this->errors[] =
'ErrorFailToDeleteDir';
2665 dol_syslog(get_class($this).
"::delete $this->id by $user->id", LOG_DEBUG);
2666 $this->db->commit();
2669 dol_syslog(get_class($this).
"::delete ".$this->error, LOG_ERR);
2670 $this->db->rollback();
2689 $sql =
"SELECT p.ref, p.label,";
2690 $sql .=
" e.rowid as warehouse_id, e.ref as entrepot,";
2691 $sql .=
" cfd.rowid as dispatchedlineid, cfd.fk_product, cfd.qty, cfd.eatby, cfd.sellby, cfd.batch, cfd.comment, cfd.status, cfd.fk_elementdet";
2692 $sql .=
" FROM ".$this->db->prefix().
"product as p,";
2693 $sql .=
" ".$this->db->prefix().
"receptiondet_batch as cfd";
2694 $sql .=
" LEFT JOIN ".$this->db->prefix().
"entrepot as e ON cfd.fk_entrepot = e.rowid";
2695 $sql .=
" WHERE cfd.fk_element = ".((int) $this->
id);
2696 $sql .=
" AND cfd.fk_product = p.rowid";
2698 $sql .=
" AND cfd.status = ".((int) $status);
2700 $sql .=
" ORDER BY cfd.rowid ASC";
2702 $resql = $this->db->query($sql);
2704 $num = $this->db->num_rows($resql);
2708 $objp = $this->db->fetch_object($resql);
2711 'id' => $objp->dispatchedlineid,
2712 'productid' => $objp->fk_product,
2713 'warehouseid' => $objp->warehouse_id,
2714 'qty' => $objp->qty,
2715 'orderlineid' => $objp->fk_elementdet
2722 dol_print_error($this->db,
'Failed to execute request to get dispatched lines');
2745 $dispatchedlinearray = array();
2749 $usercanreceive = 0;
2751 $usercanreceive = $user->hasRight(
"fournisseur",
"commande",
"receptionner");
2753 $usercanreceive = $user->hasRight(
"reception",
"creer");
2756 if ($usercanreceive) {
2758 if ($type ==
'par') {
2760 } elseif ($type ==
'tot') {
2762 } elseif ($type ==
'nev') {
2764 } elseif ($type ==
'can') {
2768 dol_syslog(get_class($this).
"::Livraison Error -2", LOG_ERR);
2775 if ($type ==
'tot') {
2777 if (count($dispatchedlinearray) > 0) {
2780 $this->errors[] =
'ErrorCantSetReceptionToTotalDoneWithReceptionToApprove';
2781 dol_syslog(
'ErrorCantSetReceptionToTotalDoneWithReceptionToApprove', LOG_DEBUG);
2784 if (!$error &&
getDolGlobalString(
'SUPPLIER_ORDER_USE_DISPATCH_STATUS_NEED_APPROVE') && ($type ==
'tot')) {
2786 if (count($dispatcheddenied) > 0) {
2789 $this->errors[] =
'ErrorCantSetReceptionToTotalDoneWithReceptionDenied';
2790 dol_syslog(
'ErrorCantSetReceptionToTotalDoneWithReceptionDenied', LOG_DEBUG);
2797 if (empty($error)) {
2800 $sql =
"UPDATE ".$this->db->prefix().
"commande_fournisseur";
2801 $sql .=
" SET fk_statut = ".((int) $statut);
2802 $sql .=
" WHERE rowid = ".((int) $this->
id);
2803 $sql .=
" AND fk_statut IN (".self::STATUS_ORDERSENT.
",".self::STATUS_RECEIVED_PARTIALLY.
")";
2805 dol_syslog(get_class($this).
"::Livraison", LOG_DEBUG);
2806 $resql = $this->db->query($sql);
2809 $old_statut = $this->status;
2811 $this->context[
'actionmsg2'] = $comment;
2814 $result_trigger = $this->call_trigger(
'ORDER_SUPPLIER_RECEIVE', $user);
2815 if ($result_trigger < 0) {
2820 if (empty($error)) {
2821 $this->db->commit();
2823 $this->
status = $old_statut;
2824 $this->db->rollback();
2825 $this->error = $this->db->lasterror();
2829 $this->db->rollback();
2830 $this->error = $this->db->lasterror();
2835 $this->error = $langs->trans(
'NotAuthorized');
2836 $this->errors[] = $langs->trans(
'NotAuthorized');
2837 dol_syslog(get_class($this).
"::Livraison Not Authorized");
2869 if ($user->hasRight(
"fournisseur",
"commande",
"creer") || $user->hasRight(
"supplier_order",
"creer")) {
2874 $sql =
"UPDATE ".$this->db->prefix().
"commande_fournisseur";
2875 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2876 $sql .=
" WHERE rowid = ".((int) $this->
id);
2879 $resql = $this->db->query($sql);
2881 $this->errors[] = $this->db->error();
2886 $this->oldcopy = clone $this;
2887 $this->delivery_date = $delivery_date;
2890 if (!$notrigger && empty($error)) {
2892 $result = $this->call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
2900 $this->db->commit();
2903 foreach ($this->errors as $errmsg) {
2904 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2905 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2907 $this->db->rollback();
2923 if (in_array($this->
status, [1, 2, 3, 4, 5, 6, 7, 9])) {
2924 if ($this->
status == 1) {
2926 } elseif ($this->
status == 2) {
2928 } elseif ($this->
status == 3) {
2930 } elseif ($this->
status == 4) {
2932 } elseif ($this->
status == 5) {
2938 } elseif ($this->
status == 6) {
2940 } elseif ($this->
status == 7) {
2942 } elseif ($this->
status == 9) {
2950 $result = $this->
setStatus($user, $newStatus);
2952 if ($newStatus == 0) {
2953 $sql =
'UPDATE '.$this->db->prefix().
'commande_fournisseur';
2954 $sql .=
' SET fk_user_approve = null, fk_user_approve2 = null, date_approve = null, date_approve2 = null';
2955 $sql .=
' WHERE rowid = '.((int) $this->
id);
2957 $this->db->query($sql);
2960 $this->db->commit();
2964 $this->db->rollback();
2985 if ($user->hasRight(
"fournisseur",
"commande",
"creer") || $user->hasRight(
"supplier_order",
"creer")) {
2990 $sql =
"UPDATE ".$this->db->prefix().
"commande_fournisseur";
2991 $sql .=
" SET fk_projet = ".($id_projet > 0 ? (int) $id_projet :
'null');
2992 $sql .=
" WHERE rowid = ".((int) $this->
id);
2995 $resql = $this->db->query($sql);
2997 $this->errors[] = $this->db->error();
3002 $this->oldcopy = clone $this;
3003 $this->fk_projet = $id_projet;
3004 $this->fk_project = $id_projet;
3007 if (!$notrigger && empty($error)) {
3009 $result = $this->call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
3017 $this->db->commit();
3020 foreach ($this->errors as $errmsg) {
3021 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
3022 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3024 $this->db->rollback();
3042 $comclient =
new Commande($this->db);
3043 $comclient->fetch($comclientid);
3047 $this->lines = array();
3049 $num = count($comclient->lines);
3050 for ($i = 0; $i < $num; $i++) {
3054 if (
$prod->fetch($comclient->lines[$i]->fk_product) > 0) {
3055 $label =
$prod->label;
3056 $ref = (string)
$prod->ref;
3059 $sql =
"INSERT INTO ".$this->db->prefix().
"commande_fournisseurdet";
3060 $sql .=
" (fk_commande, label, description, fk_product, price, qty, tva_tx, localtax1_tx, localtax2_tx, remise_percent, subprice, remise, ref)";
3061 $sql .=
" VALUES (".((int) $idc).
", '".$this->db->escape($label).
"', '".$this->db->escape($comclient->lines[$i]->desc).
"'";
3062 $sql .=
",".((int) $comclient->lines[$i]->fk_product).
", ".
price2num($comclient->lines[$i]->price,
'MU');
3063 $sql .=
", ".price2num($comclient->lines[$i]->qty,
'MS').
", ".
price2num($comclient->lines[$i]->tva_tx, 5).
", ".
price2num($comclient->lines[$i]->localtax1_tx, 5).
", ".
price2num($comclient->lines[$i]->localtax2_tx, 5).
", ".
price2num($comclient->lines[$i]->remise_percent, 3);
3064 $sql .=
", '".price2num($comclient->lines[$i]->subprice,
'MT').
"','0', '".$this->db->escape($ref).
"');";
3065 if ($this->db->query($sql)) {
3086 $sql =
'UPDATE '.$this->db->prefix().
'commande_fournisseur';
3087 $sql .=
" SET fk_statut = ".((int) $status);
3088 $sql .=
" WHERE rowid = ".((int) $this->
id);
3090 dol_syslog(get_class($this).
"::setStatus", LOG_DEBUG);
3091 $resql = $this->db->query($sql);
3094 $triggerName = array();
3095 $triggerName[0] =
'DRAFT';
3096 $triggerName[1] =
'VALIDATED';
3097 $triggerName[2] =
'APPROVED';
3098 $triggerName[3] =
'ORDERED';
3099 $triggerName[4] =
'RECEIVED_PARTIALLY';
3100 $triggerName[5] =
'RECEIVED_COMPLETELY';
3101 $triggerName[6] =
'CANCELED';
3102 $triggerName[7] =
'CANCELED';
3103 $triggerName[9] =
'REFUSED';
3106 $result = $this->call_trigger(
"ORDER_SUPPLIER_STATUS_".$triggerName[$status], $user);
3113 $this->error = $this->db->lasterror();
3114 dol_syslog(get_class($this).
"::setStatus ".$this->error);
3119 $this->db->commit();
3122 $this->db->rollback();
3140 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
3141 return parent::setCategoriesCommon($categories, Categorie::TYPE_SUPPLIER_ORDER);
3167 public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $price_base_type =
'HT', $info_bits = 0, $type = 0, $notrigger = 0,
$date_start = 0, $date_end = 0, $array_options = [], $fk_unit =
null, $pu_ht_devise = 0, $ref_supplier =
'')
3172 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
3176 dol_syslog(get_class($this).
"::updateline $rowid, $desc, $pu, $qty, $remise_percent, $txtva, $price_base_type, $info_bits, $type, $fk_unit");
3177 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3181 if ($this->
status == self::STATUS_DRAFT) {
3186 if (empty($info_bits)) {
3189 if (empty($txtva)) {
3192 if (empty($txlocaltax1)) {
3195 if (empty($txlocaltax2)) {
3198 if (empty($remise_percent)) {
3199 $remise_percent = 0;
3202 $remise_percent = (float)
price2num($remise_percent);
3208 $pu_ht_devise =
price2num($pu_ht_devise);
3209 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
3212 $txlocaltax1 = (float)
price2num($txlocaltax1);
3213 $txlocaltax2 = (float)
price2num($txlocaltax2);
3220 $langs->load(
"errors");
3221 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3236 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3237 $vat_src_code = $reg[1];
3238 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3245 if ($tmpline->fetch($rowid) > 0) {
3247 if ($newqty != $qty) {
3249 setEventMessages($langs->trans(
'QtyRecalculatedWithPackaging'),
null,
'warnings');
3254 $tabprice =
calcul_price_total($qty, (
float) $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, (
float) $this->multicurrency_tx, (
float) $pu_ht_devise);
3255 $total_ht = $tabprice[0];
3256 $total_tva = $tabprice[1];
3257 $total_ttc = $tabprice[2];
3258 $total_localtax1 = $tabprice[9];
3259 $total_localtax2 = $tabprice[10];
3260 $pu_ht = $tabprice[3];
3261 $pu_tva = $tabprice[4];
3262 $pu_ttc = $tabprice[5];
3265 $multicurrency_total_ht = $tabprice[16];
3266 $multicurrency_total_tva = $tabprice[17];
3267 $multicurrency_total_ttc = $tabprice[18];
3268 $pu_ht_devise = $tabprice[19];
3269 $multicurrency_pu_ttc = $tabprice[21];
3271 $localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3272 $localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3276 $this->line->fetch($rowid);
3278 $oldline = clone $this->line;
3279 $this->line->oldline = $oldline;
3281 $this->line->context = $this->context;
3283 $this->line->fk_commande = $this->id;
3285 $this->line->desc = $desc;
3288 $this->line->qty = $qty;
3289 $this->line->ref_supplier = $ref_supplier;
3291 $this->line->vat_src_code = $vat_src_code;
3292 $this->line->tva_tx = $txtva;
3293 $this->line->localtax1_tx = $txlocaltax1;
3294 $this->line->localtax2_tx = $txlocaltax2;
3295 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3296 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3297 $this->line->remise_percent = $remise_percent;
3298 $this->line->subprice = (float) $pu_ht;
3299 $this->line->subprice_ttc = (float) $pu_ttc;
3300 $this->line->info_bits = $info_bits;
3301 $this->line->total_ht = (float) $total_ht;
3302 $this->line->total_tva = (float) $total_tva;
3303 $this->line->total_localtax1 = (float) $total_localtax1;
3304 $this->line->total_localtax2 = (float) $total_localtax2;
3305 $this->line->total_ttc = (float) $total_ttc;
3306 $this->line->product_type = $type;
3307 $this->line->special_code = $oldline->special_code;
3308 $this->line->rang = $oldline->rang;
3309 $this->line->origin = $this->origin;
3310 $this->line->fk_unit = $fk_unit;
3313 $this->line->date_end = $date_end;
3316 $this->line->fk_multicurrency = $this->fk_multicurrency;
3317 $this->line->multicurrency_code = $this->multicurrency_code;
3318 $this->line->multicurrency_subprice = (float) $pu_ht_devise;
3319 $this->line->multicurrency_subprice_ttc = (float) $multicurrency_pu_ttc;
3320 $this->line->multicurrency_total_ht = (float) $multicurrency_total_ht;
3321 $this->line->multicurrency_total_tva = (float) $multicurrency_total_tva;
3322 $this->line->multicurrency_total_ttc = (float) $multicurrency_total_ttc;
3324 $this->line->subprice = (float) $pu_ht;
3325 $this->line->subprice_ttc = ($price_base_type ===
'TTC') ? (
float) $pu_ttc : 0;
3326 $this->line->price = $this->line->subprice;
3328 $this->line->remise_percent = $remise_percent;
3330 if (is_array($array_options) && count($array_options) > 0) {
3332 foreach ($array_options as $key => $value) {
3333 $this->line->array_options[$key] = $array_options[$key];
3337 $result = $this->line->update($notrigger);
3343 $this->db->commit();
3346 $this->errors[] = $this->line->error;
3347 $this->errors = array_merge($this->errors, $this->line->errors);
3348 $this->error = $this->db->lasterror();
3349 $this->db->rollback();
3353 $this->error =
"Order status makes operation forbidden";
3354 dol_syslog(get_class($this).
"::updateline ".$this->error, LOG_ERR);
3369 global $user, $langs,
$conf;
3371 include_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
3373 dol_syslog(get_class($this).
"::initAsSpecimen");
3380 $sql =
"SELECT rowid";
3381 $sql .=
" FROM ".$this->db->prefix().
"product";
3382 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3383 $sql .= $this->db->order(
"rowid",
"ASC");
3384 $sql .= $this->db->plimit(1);
3385 $resql = $this->db->query($sql);
3386 if ($resql && $this->db->num_rows($resql)) {
3387 $obj = $this->db->fetch_object($resql);
3388 $prodid = $obj->rowid;
3393 $this->
ref =
'SPECIMEN';
3394 $this->specimen = 1;
3397 $this->date_commande = $now;
3398 $this->date_lim_reglement = $this->
date + 3600 * 24 * 30;
3399 $this->cond_reglement_code =
'RECEP';
3400 $this->mode_reglement_code =
'CHQ';
3402 $this->note_public =
'This is a comment (public)';
3403 $this->note_private =
'This is a comment (private)';
3405 $this->multicurrency_tx = 1;
3406 $this->multicurrency_code =
$conf->currency;
3414 while ($xnbp < $nbp) {
3416 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
3418 $line->subprice = 100;
3419 $line->tva_tx = 19.6;
3420 $line->localtax1_tx = 0;
3421 $line->localtax2_tx = 0;
3423 $line->total_ht = 50;
3424 $line->total_ttc = 59.8;
3425 $line->total_tva = 9.8;
3426 $line->remise_percent = 50;
3428 $line->total_ht = 100;
3429 $line->total_ttc = 119.6;
3430 $line->total_tva = 19.6;
3431 $line->remise_percent = 00;
3433 $line->fk_product = $prodid;
3435 $this->lines[$xnbp] = $line;
3437 $this->total_ht += $line->total_ht;
3438 $this->total_tva += $line->total_tva;
3439 $this->total_ttc += $line->total_ttc;
3455 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem, date_valid as date_validation, date_approve as datea, date_approve2 as datea2,';
3456 $sql .=
' fk_user_author, fk_user_modif, fk_user_valid, fk_user_approve, fk_user_approve2';
3457 $sql .=
' FROM '.$this->db->prefix().
'commande_fournisseur as c';
3458 $sql .=
' WHERE c.rowid = '.((int) $id);
3460 $result = $this->db->query($sql);
3462 if ($this->db->num_rows($result)) {
3463 $obj = $this->db->fetch_object($result);
3465 $this->
id = $obj->rowid;
3467 $this->user_creation_id = $obj->fk_user_author;
3468 $this->user_validation_id = $obj->fk_user_valid;
3469 $this->user_modification_id = $obj->fk_user_modif;
3470 $this->user_approve_id = $obj->fk_user_approve;
3471 $this->user_approve_id2 = $obj->fk_user_approve2;
3473 $this->date_creation = $this->db->jdate($obj->datec);
3474 $this->date_modification = $this->db->jdate($obj->datem);
3475 $this->date_approve = $this->db->jdate($obj->datea);
3476 $this->date_approve2 = $this->db->jdate($obj->datea2);
3477 $this->date_validation = $this->db->jdate($obj->date_validation);
3479 $this->db->free($result);
3481 dol_print_error($this->db);
3494 $this->nb = array();
3495 $sanitizedclause =
"WHERE";
3497 $sql =
"SELECT count(co.rowid) as nb";
3498 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseur as co";
3499 $sql .=
" LEFT JOIN ".$this->db->prefix().
"societe as s ON co.fk_soc = s.rowid";
3500 if (empty($user->socid) && !$user->hasRight(
"societe",
"client",
"voir") && !$user->socid) {
3501 $sql .=
" LEFT JOIN ".$this->db->prefix().
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3502 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3503 $sanitizedclause =
"AND";
3505 $sql .=
" ".$sanitizedclause.
" co.entity IN (".
getEntity(
'supplier_order').
")";
3507 $resql = $this->db->query($sql);
3509 while ($obj = $this->db->fetch_object($resql)) {
3510 $this->nb[
"supplier_orders"] = $obj->nb;
3512 $this->db->free($resql);
3515 dol_print_error($this->db);
3516 $this->error = $this->db->error();
3532 global
$conf, $langs;
3534 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.fk_statut, c.date_livraison as delivery_date, c.total_ht";
3535 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseur as c";
3536 if (empty($user->socid) && !$user->hasRight(
"societe",
"client",
"voir") && !$user->socid) {
3537 $sql .=
" JOIN ".$this->db->prefix().
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3539 $sql .=
" WHERE c.entity = ".((int)
$conf->entity);
3540 if ($mode ===
'awaiting') {
3541 $sql .=
" AND c.fk_statut IN (".self::STATUS_ORDERSENT.
", ".self::STATUS_RECEIVED_PARTIALLY.
")";
3543 $sql .=
" AND c.fk_statut IN (".self::STATUS_VALIDATED.
", ".self::STATUS_ACCEPTED.
")";
3546 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3549 $resql = $this->db->query($sql);
3554 $response->warning_delay =
$conf->commande->fournisseur->warning_delay / 60 / 60 / 24;
3555 $response->label = $langs->trans(
"SuppliersOrdersToProcess");
3556 $response->labelShort = $langs->trans(
"Opened");
3557 $response->url = DOL_URL_ROOT.
'/fourn/commande/list.php?search_status=1,2&mainmenu=commercial&leftmenu=orders_suppliers';
3558 $response->url_late = DOL_URL_ROOT.
'/fourn/commande/list.php?mainmenu=commercial&leftmenu=orders_suppliers&search_option=late';
3559 $response->img = img_object(
'',
"order");
3561 if ($mode ===
'awaiting') {
3562 $response->label = $langs->trans(
"SuppliersOrdersAwaitingReception");
3563 $response->labelShort = $langs->trans(
"AwaitingReception");
3564 $response->url = DOL_URL_ROOT.
'/fourn/commande/list.php?search_status=3,4&mainmenu=commercial&leftmenu=orders_suppliers';
3565 $response->url_late = DOL_URL_ROOT.
'/fourn/commande/list.php?mainmenu=commercial&leftmenu=orders_suppliers&search_option=recv_late';
3568 while ($obj = $this->db->fetch_object($resql)) {
3569 $commandestatic->delivery_date = $this->db->jdate($obj->delivery_date);
3570 $commandestatic->date_commande = $this->db->jdate($obj->date_commande);
3571 $commandestatic->statut = $obj->fk_statut;
3572 $commandestatic->status = $obj->fk_statut;
3574 $response->nbtodo++;
3575 $response->total += $obj->total_ht;
3577 if ($commandestatic->hasDelay()) {
3578 $response->nbtodolate++;
3584 $this->error = $this->db->error();
3599 if ($this->methode_commande_id > 0) {
3600 $sql =
"SELECT rowid, code, libelle as label";
3601 $sql .=
" FROM ".$this->db->prefix().
'c_input_method';
3602 $sql .=
" WHERE active=1 AND rowid = ".((int) $this->methode_commande_id);
3604 $resql = $this->db->query($sql);
3606 if ($this->db->num_rows($resql)) {
3607 $obj = $this->db->fetch_object($resql);
3609 $string = $langs->trans($obj->code);
3610 if ($string == $obj->code) {
3611 $string = $obj->label !=
'-' ? $obj->label :
'';
3616 dol_print_error($this->db);
3634 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
3641 if (!empty($this->model_pdf)) {
3642 $modele = $this->model_pdf;
3648 if (empty($modele)) {
3651 $langs->load(
"suppliers");
3652 $outputlangs->load(
"products");
3654 $modelpath =
"core/modules/supplier_order/doc/";
3655 $result = $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3668 if (empty($this->lines)) {
3675 foreach ($this->lines as $line) {
3676 if ($line->fk_product > 0) {
3678 $idp = $tmpproductfourn->find_min_price_product_fournisseur($line->fk_product, $line->qty, $this->thirdparty->id);
3681 if ($tmpproductfourn->delivery_time_days > $nb) {
3682 $nb = $tmpproductfourn->delivery_time_days;
3691 return $nb.
' '.$langs->trans(
'days');
3704 return $user->hasRight(
"fournisseur",
"commande");
3719 'commande_fournisseur'
3736 'commande_fournisseurdet'
3753 if ($this->
status == self::STATUS_ORDERSENT || $this->
status == self::STATUS_RECEIVED_PARTIALLY) {
3755 if (!empty($this->delivery_date)) {
3756 $date_to_test = $this->delivery_date;
3757 return $date_to_test && $date_to_test < ($now -
$conf->commande->fournisseur->warning_delay);
3765 $date_to_test = $this->date_commande;
3767 return ($this->
status > 0 && $this->
status < 5) && $date_to_test && $date_to_test < ($now -
$conf->commande->fournisseur->warning_delay);
3780 global
$conf, $langs;
3782 $langs->load(
'orders');
3786 if ($this->
status == self::STATUS_ORDERSENT || $this->
status == self::STATUS_RECEIVED_PARTIALLY) {
3787 if (!empty($this->delivery_date)) {
3788 $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->delivery_date,
'day');
3790 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
3793 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
3796 $text .=
' '.($conf->commande->fournisseur->warning_delay > 0 ?
'+' :
'-').
' '.round(abs(
$conf->commande->fournisseur->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
3814 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.dispatch.class.php';
3816 $qtydelivered = array();
3817 $qtywished = array();
3821 $filter = array(
't.fk_element' => $this->
id);
3823 $filter[
't.status'] = 1;
3826 $ret = $supplierorderdispatch->fetchAll(
'',
'', 0, 0, $filter);
3828 $this->error = $supplierorderdispatch->error;
3829 $this->errors = $supplierorderdispatch->errors;
3832 if (is_array($supplierorderdispatch->lines) && count($supplierorderdispatch->lines) > 0) {
3833 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
3837 foreach ($supplierorderdispatch->lines as $line) {
3838 if (array_key_exists($line->fk_product, $qtydelivered)) {
3839 $qtydelivered[$line->fk_product] += $line->qty;
3841 $qtydelivered[$line->fk_product] = $line->qty;
3844 foreach ($this->lines as $line) {
3846 if (empty($line->fk_product)) {
3850 if ($line->product_type > 0 && (!
getDolGlobalString(
'STOCK_SUPPORTS_SERVICES') || empty($line->stockable_product))) {
3853 if (array_key_exists($line->fk_product, $qtywished)) {
3854 $qtywished[$line->fk_product] += $line->qty;
3856 $qtywished[$line->fk_product] = $line->qty;
3861 $diff_array = array_diff_assoc($qtydelivered, $qtywished);
3862 $keysinwishednotindelivered = array_diff(array_keys($qtywished), array_keys($qtydelivered));
3863 $keysindeliverednotinwished = array_diff(array_keys($qtydelivered), array_keys($qtywished));
3871 if (count($diff_array) == 0 && count($keysinwishednotindelivered) == 0 && count($keysindeliverednotinwished) == 0) {
3872 if ($closeopenorder) {
3874 $ret = $this->
Livraison($user, $date_liv,
'tot', $comment);
3882 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3893 if (count($diff_array) > 0) {
3897 foreach ($diff_array as $key => $value) {
3899 if ($qtydelivered[$key] >= $qtywished[$key]) {
3906 if ($close == count($diff_array)) {
3908 if ($closeopenorder) {
3909 $ret = $this->
Livraison($user, $date_liv,
'tot', $comment);
3916 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3924 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3932 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3954 $this->receptions = array();
3956 dol_syslog(get_class($this).
"::loadReceptions", LOG_DEBUG);
3958 $sql =
'SELECT cd.rowid, cd.fk_product,';
3959 $sql .=
' sum(cfd.qty) as qty';
3960 $sql .=
' FROM '.$this->db->prefix().
'receptiondet_batch as cfd,';
3961 if ($filtre_statut >= 0) {
3962 $sql .=
' '.$this->db->prefix().
'reception as e,';
3964 $sql .=
' '.$this->db->prefix().
'commande_fournisseurdet as cd';
3966 if ($filtre_statut >= 0) {
3967 $sql .=
' cfd.fk_reception = e.rowid AND';
3969 $sql .=
' cfd.fk_elementdet = cd.rowid';
3970 $sql .=
' AND cd.fk_commande ='.((int) $this->
id);
3971 if (isset($this->fk_product) && !empty($this->fk_product) > 0) {
3972 $sql .=
' AND cd.fk_product = '.((int) $this->fk_product);
3974 if ($filtre_statut >= 0) {
3975 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
3977 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
3979 $resql = $this->db->query($sql);
3981 $num = $this->db->num_rows($resql);
3984 $obj = $this->db->fetch_object($resql);
3985 if (empty($this->receptions[$obj->rowid])) {
3986 $this->receptions[$obj->rowid] = (float) $obj->qty;
3988 $this->receptions[$obj->rowid] += (float) $obj->qty;
3992 $this->db->free($resql);
3996 $this->error = $this->db->lasterror();
4012 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
4014 $return =
'<div class="box-flex-item box-flex-grow-zero">';
4015 $return .=
'<div class="info-box info-box-sm">';
4016 $return .=
'<span class="info-box-icon bg-infobox-action">';
4017 $return .= img_picto(
'', $this->picto);
4018 $return .=
'</span>';
4019 $return .=
'<div class="info-box-content">';
4020 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() : $this->ref).
'</span>';
4021 if ($selected >= 0) {
4022 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
4024 if (!empty($arraydata[
'thirdparty'])) {
4025 $return .=
'<br><span class="info-box-label">'.$arraydata[
'thirdparty'].
'</span>';
4027 if (!empty($this->
date)) {
4028 $return .=
'<br><span class="info-box-label">'.dol_print_date($this->
date,
'day').
'</span>';
4030 if (!empty($this->total_ht)) {
4031 $return .=
' <span class="info-box-label amount" title="'.dol_escape_htmltag($langs->trans(
"AmountHT")).
'">'.
price($this->total_ht);
4032 $return .=
' '.$langs->trans(
"HT");
4033 $return .=
'</span>';
4035 if (method_exists($this,
'getLibStatut')) {
4036 $return .=
'<br><span class="info-box-status">'.$this->getLibStatut(3).
'</span>';
4038 if (property_exists($this,
'billed')) {
4039 $return .=
' <span class="opacitymedium">'.$langs->trans(
"Billed").
': </span><span class="info-box-label">'.yn($this->billed).
'</span>';
4041 $return .=
'</div>';
4042 $return .=
'</div>';
4043 $return .=
'</div>';
Class to manage table ReceptionLineBatch.
Class to manage predefined suppliers products.
const STATUS_CANCELED_AFTER_ORDER
Order canceled/never received.
const STATUS_RECEIVED_PARTIALLY
Received partially.
setDeliveryDate($user, $delivery_date, $notrigger=0)
Set the planned delivery date.
updateFromCommandeClient($user, $idc, $comclientid)
Update a supplier order from a sales order.
getNomUrl($withpicto=0, $option='', $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0)
Return clickable name (with picto eventually)
loadReceptions($filtre_statut=-1)
Load array this->receptions of lines of shipments with nb of products sent for each order line Note: ...
static replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a product id with another one.
$fields
'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortf...
refuse($user)
Refuse an order.
set_date_livraison($user, $delivery_date, $notrigger=0)
Set delivery date.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
info($id)
Charge les information d'ordre info dans l'objet facture.
const STATUS_CANCELED
Order canceled.
fetch_lines($only_product=0)
Load array lines.
getInputMethod()
Returns the translated input method of object (defined if $this->methode_commande_id > 0).
const STATUS_VALIDATED
Validated status.
const STATUS_RECEIVED_COMPLETELY
Received completely.
cancel($user, $idwarehouse=-1)
Cancel an approved order.
setReopen(User $user)
Reopen supplier order.
loadStateBoard()
Load the indicators this->nb for the state board.
calcAndSetStatusDispatch(User $user, $closeopenorder=1, $comment='')
Calc status regarding to dispatched stock.
set_id_projet($user, $id_projet, $notrigger=0)
Set the id projet.
const STATUS_DRAFT
Draft status.
showDelay()
Show the customer delayed info.
getTooltipContentArray($params)
getTooltipContentArray
classifyUnBilled(User $user)
Classify not billed.
approve($user, $idwarehouse=0, $secondlevel=0)
Approve a supplier order.
__construct($db)
Constructor.
addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0.0, $txlocaltax2=0.0, $fk_product=0, $fk_prod_fourn_price=0, $ref_supplier='', $remise_percent=0.0, $price_base_type='HT', $pu_ttc=0.0, $type=0, $info_bits=0, $notrigger=0, $date_start=null, $date_end=null, $array_options=[], $fk_unit=null, $pu_ht_devise=0, $origin='', $origin_id=0, $rang=-1, $special_code=0, $label='')
Add order line.
dispatchProduct($user, $product, $qty, $entrepot, $price=0, $comment='', $eatby='', $sellby='', $batch='', $fk_commandefourndet=0, $notrigger=0, $fk_reception=0)
Save a receiving into the tracking table of receiving (receptiondet_batch) and add product into stock...
valid($user, $idwarehouse=0, $notrigger=0)
Validate an order.
create($user, $notrigger=0)
Create order with draft status.
update(User $user, $notrigger=0)
Update Supplier Order.
createFromClone(User $user, $socid=0, $notrigger=0)
Load an object from its id and create a new one in database.
updateline($rowid, $desc, $pu, $qty, $remise_percent, $txtva, $txlocaltax1=0, $txlocaltax2=0, $price_base_type='HT', $info_bits=0, $type=0, $notrigger=0, $date_start=0, $date_end=0, $array_options=[], $fk_unit=null, $pu_ht_devise=0, $ref_supplier='')
Update line.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template model.
const STATUS_ACCEPTED
Accepted.
const STATUS_ORDERSENT
Order sent, shipment on process.
commande($user, $date, $methode, $comment='')
Submit a supplier order to supplier.
getRights()
Returns the rights used for this class.
load_board($user, $mode='opened')
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
deleteLine($idline, $notrigger=0)
Delete line.
getMaxDeliveryTimeDay($langs)
Return the max number delivery delay in day.
fetch($id, $ref='')
Get object and lines from database.
Livraison($user, $date, $type, $comment)
Set a delivery in database for this supplier order.
getDispachedLines($status=-1)
Return array of dispatched lines waiting to be approved for this order.
classifyBilled(User $user)
Class invoiced the supplier order.
initAsSpecimen()
Initialise an instance with random values.
const SOURCE_ID_REPLENISHMENT
The constant used into source field to track the order was generated by the replenishement feature.
setCategories($categories)
Sets object to given categories.
setStatus($user, $status)
Tag order with a particular status.
const STATUS_REFUSED
Refused.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
getLibStatut($mode=0)
Return label of the status of object.
hasDelay()
Is the supplier order delayed? We suppose a purchase ordered as late if a the purchase order has been...
LibStatut($status, $mode=0, $billed=0)
Return label of a status.
Class to manage line orders.
Class to manage customers orders.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
line_order($renum=false, $rowidorder='ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
deleteEcmFiles($mode=0)
Delete related files of object in database.
getTooltipContent($params)
getTooltipContent
update_price($exclspec=0, $roundingadjust='auto', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
roundQtyToPackaging($qty, $packaging)
Round a quantity up to the next multiple of a packaging quantity (options PRODUCT_USE_CUSTOMER_PACKAG...
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
setErrorsFromObject($object)
setErrorsFromObject
isLineOfObject($lineid)
Check that a line belongs to this object, using $this->table_element_line and $this->fk_element.
updateRangOfLine($rowid, $rang)
Update position of line (rang)
checkActiveProductInLines($status='onsale')
Check if all products have the right status (on sale, on buy) called during validation of propal,...
deleteExtraFields()
Delete all extra fields values for the current object.
fetchObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $clause='OR', $alsosametype=1, $orderby='sourcetype', $loadalsoobjects=1)
Fetch array of objects linked to current object (object of enabled modules only).
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Superclass for orders classes.
Class to manage Dolibarr database access.
Class to manage stock movements.
static getIdAndTxFromCode($dbs, $code, $date_document=0)
Get id and rate of currency from code.
static getIdFromCode($dbs, $code)
Get id of currency from code.
Class to manage predefined suppliers products.
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
$date_start
Variables from include:
dol_now($mode='gmt')
Return date for now.
getDolGlobalFloat($key, $default=0)
Return a Dolibarr global constant float value.
setEntity($currentobject)
Set entity id to use when to create an object.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalBool($key, $default=false)
Return a Dolibarr global constant boolean value.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $notused, $price_base_type, $info_bits, $type, $seller=null, $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date