37 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
39 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
41 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
53 public $element =
'order_supplier';
58 public $table_element =
'commande_fournisseur';
63 public $table_element_line =
'commande_fournisseurdet';
68 public $fk_element =
'fk_commande';
73 public $picto =
'supplier_order';
79 public $ismultientitymanaged = 1;
85 public $restrictiononfksoc = 1;
103 public $ref_supplier;
117 public $date_creation;
119 public $date_approve;
120 public $date_approve2;
121 public $date_commande;
127 public $date_livraison;
132 public $delivery_date;
136 public $total_localtax1;
137 public $total_localtax2;
146 public $cond_reglement_id;
147 public $cond_reglement_code;
148 public $cond_reglement_label;
149 public $cond_reglement_doc;
156 public $mode_reglement_id;
157 public $mode_reglement_code;
158 public $user_author_id;
159 public $user_valid_id;
160 public $user_approve_id;
161 public $user_approve_id2;
165 public $extraparams = array();
170 public $lines = array();
175 public $linked_objects = array();
181 public $fk_multicurrency;
183 public $multicurrency_code;
184 public $multicurrency_tx;
185 public $multicurrency_total_ht;
186 public $multicurrency_total_tva;
187 public $multicurrency_total_ttc;
218 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>0,
'notnull'=>1,
'position'=>10),
219 'ref' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>1,
'showoncombobox'=>1,
'position'=>25,
'searchall'=>1),
220 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref ext',
'enabled'=>1,
'visible'=>0,
'position'=>35),
221 'ref_supplier' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefOrderSupplierShort',
'enabled'=>1,
'visible'=>1,
'position'=>40,
'searchall'=>1),
222 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:fk_statut=1',
'label'=>
'Project',
'enabled'=>
"isModEnabled('project')",
'visible'=>-1,
'position'=>45),
223 'date_valid' =>array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>60),
224 'date_approve' =>array(
'type'=>
'datetime',
'label'=>
'DateApprove',
'enabled'=>1,
'visible'=>-1,
'position'=>62),
225 'date_approve2' =>array(
'type'=>
'datetime',
'label'=>
'DateApprove2',
'enabled'=>1,
'visible'=>3,
'position'=>64),
226 'date_commande' =>array(
'type'=>
'date',
'label'=>
'OrderDateShort',
'enabled'=>1,
'visible'=>1,
'position'=>70),
227 'date_livraison' =>array(
'type'=>
'datetime',
'label'=>
'DeliveryDate',
'enabled'=>
'empty($conf->global->ORDER_DISABLE_DELIVERY_DATE)',
'visible'=>1,
'position'=>74),
228 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>3,
'position'=>75),
229 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>3,
'notnull'=>-1,
'position'=>80),
230 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>3,
'position'=>85),
231 'fk_user_approve' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserApproval',
'enabled'=>1,
'visible'=>3,
'position'=>90),
232 'fk_user_approve2' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserApproval2',
'enabled'=>1,
'visible'=>3,
'position'=>95),
233 'source' =>array(
'type'=>
'smallint(6)',
'label'=>
'Source',
'enabled'=>1,
'visible'=>3,
'notnull'=>1,
'position'=>100),
234 'billed' =>array(
'type'=>
'smallint(6)',
'label'=>
'Billed',
'enabled'=>1,
'visible'=>1,
'position'=>110),
235 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'Tva',
'enabled'=>1,
'visible'=>1,
'position'=>130,
'isameasure'=>1),
236 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax1',
'enabled'=>1,
'visible'=>3,
'position'=>135,
'isameasure'=>1),
237 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax2',
'enabled'=>1,
'visible'=>3,
'position'=>140,
'isameasure'=>1),
238 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalHT',
'enabled'=>1,
'visible'=>1,
'position'=>145,
'isameasure'=>1),
239 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>150,
'isameasure'=>1),
240 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>155,
'searchall'=>1),
241 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>160,
'searchall'=>1),
242 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'ModelPDF',
'enabled'=>1,
'visible'=>0,
'position'=>165),
243 'fk_input_method' =>array(
'type'=>
'integer',
'label'=>
'OrderMode',
'enabled'=>1,
'visible'=>3,
'position'=>170),
244 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>3,
'position'=>175),
245 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>3,
'position'=>180),
246 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>0,
'position'=>190),
247 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'BankAccount',
'enabled'=>
'$conf->banque->enabled',
'visible'=>3,
'position'=>200),
248 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>1,
'visible'=>3,
'position'=>205),
249 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLocation',
'enabled'=>1,
'visible'=>3,
'position'=>210),
250 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>1,
'visible'=>0,
'position'=>215),
251 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'Currency',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>220),
252 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'CurrencyRate',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>225),
253 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>230),
254 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>235),
255 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>240),
256 'date_creation' =>array(
'type'=>
'datetime',
'label'=>
'Date creation',
'enabled'=>1,
'visible'=>-1,
'position'=>500),
257 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'$conf->societe->enabled',
'visible'=>1,
'notnull'=>1,
'position'=>46),
258 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>0,
'notnull'=>1,
'position'=>1000,
'index'=>1),
259 'tms'=>array(
'type'=>
'datetime',
'label'=>
"DateModificationShort",
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>501),
260 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>0,
'position'=>700),
261 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>1,
'position'=>701),
262 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>0,
'position'=>900),
337 public function fetch($id, $ref =
'')
342 if (empty($id) && empty($ref)) {
346 $sql =
"SELECT c.rowid, c.entity, c.ref, ref_supplier, c.fk_soc, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.total_tva,";
347 $sql .=
" c.localtax1, c.localtax2, ";
348 $sql .=
" c.date_creation, c.date_valid, c.date_approve, c.date_approve2,";
349 $sql .=
" c.fk_user_author, c.fk_user_valid, c.fk_user_approve, c.fk_user_approve2,";
350 $sql .=
" c.date_commande as date_commande, c.date_livraison as delivery_date, c.fk_cond_reglement, c.fk_mode_reglement, c.fk_projet as fk_project, c.remise_percent, c.source, c.fk_input_method,";
351 $sql .=
" c.fk_account,";
352 $sql .=
" c.note_private, c.note_public, c.model_pdf, c.extraparams, c.billed,";
353 $sql .=
" c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc,";
354 $sql .=
" cm.libelle as methode_commande,";
355 $sql .=
" cr.code as cond_reglement_code, cr.libelle as cond_reglement_label, cr.libelle_facture as cond_reglement_doc,";
356 $sql .=
" p.code as mode_reglement_code, p.libelle as mode_reglement_libelle";
357 $sql .=
', c.fk_incoterms, c.location_incoterms';
358 $sql .=
', i.libelle as label_incoterms';
359 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur as c";
360 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_payment_term as cr ON c.fk_cond_reglement = cr.rowid";
361 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as p ON c.fk_mode_reglement = p.id";
362 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_input_method as cm ON cm.rowid = c.fk_input_method";
363 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
366 $sql .=
" WHERE c.entity IN (".getEntity(
'supplier_order').
")";
368 $sql .=
" WHERE c.rowid=".((int) $id);
372 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
375 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
378 $obj = $this->
db->fetch_object(
$resql);
380 $this->error =
'Bill with id '.$id.
' not found';
381 dol_syslog(get_class($this).
'::fetch '.$this->error);
385 $this->
id = $obj->rowid;
386 $this->entity = $obj->entity;
388 $this->
ref = $obj->ref;
389 $this->ref_supplier = $obj->ref_supplier;
390 $this->socid = $obj->fk_soc;
391 $this->fourn_id = $obj->fk_soc;
392 $this->statut = $obj->fk_statut;
393 $this->status = $obj->fk_statut;
394 $this->billed = $obj->billed;
395 $this->user_author_id = $obj->fk_user_author;
396 $this->user_valid_id = $obj->fk_user_valid;
397 $this->user_approve_id = $obj->fk_user_approve;
398 $this->user_approve_id2 = $obj->fk_user_approve2;
399 $this->total_ht = $obj->total_ht;
400 $this->total_tva = $obj->total_tva;
401 $this->total_localtax1 = $obj->localtax1;
402 $this->total_localtax2 = $obj->localtax2;
403 $this->total_ttc = $obj->total_ttc;
404 $this->date_creation = $this->
db->jdate($obj->date_creation);
405 $this->date_valid = $this->
db->jdate($obj->date_valid);
406 $this->date_approve = $this->
db->jdate($obj->date_approve);
407 $this->date_approve2 = $this->
db->jdate($obj->date_approve2);
408 $this->date_commande = $this->
db->jdate($obj->date_commande);
409 if (isset($obj->date_commande)) {
410 $this->date = $this->date_commande;
412 $this->date = $this->date_creation;
414 $this->date_livraison = $this->
db->jdate($obj->delivery_date);
415 $this->delivery_date = $this->
db->jdate($obj->delivery_date);
416 $this->remise_percent = $obj->remise_percent;
417 $this->methode_commande_id = $obj->fk_input_method;
418 $this->methode_commande = $obj->methode_commande;
420 $this->source = $obj->source;
421 $this->fk_project = $obj->fk_project;
422 $this->cond_reglement_id = $obj->fk_cond_reglement;
423 $this->cond_reglement_code = $obj->cond_reglement_code;
424 $this->cond_reglement = $obj->cond_reglement_label;
425 $this->cond_reglement_label = $obj->cond_reglement_label;
426 $this->cond_reglement_doc = $obj->cond_reglement_doc;
427 $this->fk_account = $obj->fk_account;
428 $this->mode_reglement_id = $obj->fk_mode_reglement;
429 $this->mode_reglement_code = $obj->mode_reglement_code;
430 $this->mode_reglement = $obj->mode_reglement_libelle;
431 $this->note = $obj->note_private;
432 $this->note_private = $obj->note_private;
433 $this->note_public = $obj->note_public;
434 $this->model_pdf = $obj->model_pdf;
435 $this->modelpdf = $obj->model_pdf;
438 $this->fk_incoterms = $obj->fk_incoterms;
439 $this->location_incoterms = $obj->location_incoterms;
440 $this->label_incoterms = $obj->label_incoterms;
443 $this->fk_multicurrency = $obj->fk_multicurrency;
444 $this->multicurrency_code = $obj->multicurrency_code;
445 $this->multicurrency_tx = $obj->multicurrency_tx;
446 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
447 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
448 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
450 $this->extraparams = (array) json_decode($obj->extraparams,
true);
458 if ($this->statut == 0) {
459 $this->brouillon = 1;
473 $this->error = $this->
db->error().
" sql=".$sql;
490 $this->lines = array();
492 $sql =
"SELECT l.rowid, l.ref as ref_supplier, l.fk_product, l.product_type, l.label, l.description, l.qty,";
493 $sql .=
" l.vat_src_code, l.tva_tx, l.remise_percent, l.subprice,";
494 $sql .=
" l.localtax1_tx, l. localtax2_tx, l.localtax1_type, l. localtax2_type, l.total_localtax1, l.total_localtax2,";
495 $sql .=
" l.total_ht, l.total_tva, l.total_ttc, l.special_code, l.fk_parent_line, l.rang,";
496 $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,";
497 $sql .=
" l.fk_unit,";
498 $sql .=
" l.date_start, l.date_end,";
499 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc';
500 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet as l";
501 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
502 $sql .=
" WHERE l.fk_commande = ".((int) $this->
id);
504 $sql .=
' AND p.fk_product_type = 0';
506 $sql .=
" ORDER BY l.rang, l.rowid";
509 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
511 $result = $this->
db->query($sql);
513 $num = $this->
db->num_rows($result);
517 $objp = $this->
db->fetch_object($result);
521 $line->id = $objp->rowid;
522 $line->desc = $objp->description;
523 $line->description = $objp->description;
524 $line->qty = $objp->qty;
525 $line->tva_tx = $objp->tva_tx;
526 $line->localtax1_tx = $objp->localtax1_tx;
527 $line->localtax2_tx = $objp->localtax2_tx;
528 $line->localtax1_type = $objp->localtax1_type;
529 $line->localtax2_type = $objp->localtax2_type;
530 $line->subprice = $objp->subprice;
531 $line->pu_ht = $objp->subprice;
532 $line->remise_percent = $objp->remise_percent;
534 $line->vat_src_code = $objp->vat_src_code;
535 $line->total_ht = $objp->total_ht;
536 $line->total_tva = $objp->total_tva;
537 $line->total_localtax1 = $objp->total_localtax1;
538 $line->total_localtax2 = $objp->total_localtax2;
539 $line->total_ttc = $objp->total_ttc;
540 $line->product_type = $objp->product_type;
542 $line->fk_product = $objp->fk_product;
544 $line->libelle = $objp->product_label;
545 $line->product_label = $objp->product_label;
546 $line->product_desc = $objp->product_desc;
547 $line->product_tobatch = $objp->product_tobatch;
548 $line->product_barcode = $objp->product_barcode;
550 $line->ref = $objp->product_ref;
551 $line->product_ref = $objp->product_ref;
552 $line->ref_fourn = $objp->ref_supplier;
553 $line->ref_supplier = $objp->ref_supplier;
555 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
560 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.MAIN_DB_PREFIX.
"product_fournisseur_price";
561 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'product_fournisseur_price').
")";
562 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
563 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
564 $sqlsearchpackage .=
" AND quantity <= ".((float) $objp->qty);
565 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).
")";
566 $sqlsearchpackage .=
" AND fk_soc = ".((int) $this->socid);
567 $sqlsearchpackage .=
" ORDER BY packaging ASC";
568 $sqlsearchpackage .=
" LIMIT 1";
570 $resqlsearchpackage = $this->
db->query($sqlsearchpackage);
571 if ($resqlsearchpackage) {
572 $objsearchpackage = $this->
db->fetch_object($resqlsearchpackage);
573 if ($objsearchpackage) {
574 $line->fk_fournprice = $objsearchpackage->rowid;
575 $line->packaging = $objsearchpackage->packaging;
578 $this->error = $this->
db->lasterror();
583 $line->date_start = $this->
db->jdate($objp->date_start);
584 $line->date_end = $this->
db->jdate($objp->date_end);
585 $line->fk_unit = $objp->fk_unit;
588 $line->fk_multicurrency = $objp->fk_multicurrency;
589 $line->multicurrency_code = $objp->multicurrency_code;
590 $line->multicurrency_subprice = $objp->multicurrency_subprice;
591 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
592 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
593 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
595 $line->special_code = $objp->special_code;
596 $line->fk_parent_line = $objp->fk_parent_line;
598 $line->rang = $objp->rang;
602 $line->fetch_optionals();
604 $this->lines[$i] = $line;
608 $this->
db->free($result);
612 $this->error = $this->
db->error().
" sql=".$sql;
625 public function valid($user, $idwarehouse = 0, $notrigger = 0)
627 global $langs, $conf;
628 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
634 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && (!empty($user->rights->fournisseur->commande->creer) || !empty($user->rights->supplier_order->creer)))
635 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->fournisseur->supplier_order_advance->validate))) {
640 $soc->fetch($this->fourn_id);
643 if (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
650 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"commande_fournisseur";
651 $sql .=
" SET ref='".$this->db->escape($num).
"',";
652 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
653 $sql .=
" date_valid='".$this->db->idate(
dol_now()).
"',";
654 $sql .=
" fk_user_valid = ".((int) $user->id);
655 $sql .=
" WHERE rowid = ".((int) $this->
id);
656 $sql .=
" AND fk_statut = ".self::STATUS_DRAFT;
664 if (!$error && !$notrigger) {
666 $result = $this->
call_trigger(
'ORDER_SUPPLIER_VALIDATE', $user);
674 $this->oldref = $this->ref;
677 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
679 $sql =
'UPDATE '.MAIN_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).
"'";
680 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'fournisseur/commande/".$this->
db->escape($this->
ref).
"' and entity = ".((int) $conf->entity);
683 $error++; $this->error = $this->
db->lasterror();
689 $dirsource = $conf->fournisseur->commande->dir_output.
'/'.$oldref;
690 $dirdest = $conf->fournisseur->commande->dir_output.
'/'.$newref;
691 if (!$error && file_exists($dirsource)) {
692 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
694 if (@rename($dirsource, $dirdest)) {
697 $listoffiles =
dol_dir_list($conf->fournisseur->commande->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
698 foreach ($listoffiles as $fileentry) {
699 $dirsource = $fileentry[
'name'];
700 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
701 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
702 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
703 @rename($dirsource, $dirdest);
720 $this->
db->rollback();
724 $this->error =
'NotAuthorized';
725 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
738 return $this->
LibStatut($this->statut, $mode, $this->billed);
750 public function LibStatut($status, $mode = 0, $billed = 0)
753 global $conf, $langs, $hookmanager;
755 if (empty($this->statuts) || empty($this->statuts_short)) {
756 $langs->load(
'orders');
758 $this->statuts[0] =
'StatusSupplierOrderDraft';
759 $this->statuts[1] =
'StatusSupplierOrderValidated';
760 $this->statuts[2] =
'StatusSupplierOrderApproved';
761 if (empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
762 $this->statuts[3] =
'StatusSupplierOrderOnProcess';
764 $this->statuts[3] =
'StatusSupplierOrderOnProcessWithValidation';
766 $this->statuts[4] =
'StatusSupplierOrderReceivedPartially';
767 $this->statuts[5] =
'StatusSupplierOrderReceivedAll';
768 $this->statuts[6] =
'StatusSupplierOrderCanceled';
769 $this->statuts[7] =
'StatusSupplierOrderCanceled';
770 $this->statuts[9] =
'StatusSupplierOrderRefused';
773 $this->statuts_short[0] =
'StatusSupplierOrderDraftShort';
774 $this->statuts_short[1] =
'StatusSupplierOrderValidatedShort';
775 $this->statuts_short[2] =
'StatusSupplierOrderApprovedShort';
776 $this->statuts_short[3] =
'StatusSupplierOrderOnProcessShort';
777 $this->statuts_short[4] =
'StatusSupplierOrderReceivedPartiallyShort';
778 $this->statuts_short[5] =
'StatusSupplierOrderReceivedAllShort';
779 $this->statuts_short[6] =
'StatusSupplierOrderCanceledShort';
780 $this->statuts_short[7] =
'StatusSupplierOrderCanceledShort';
781 $this->statuts_short[9] =
'StatusSupplierOrderRefusedShort';
784 $statustrans = array(
796 $statusClass =
'status0';
797 if (!empty($statustrans[$status])) {
798 $statusClass = $statustrans[$status];
803 $billedtext =
' - '.$langs->trans(
"Billed");
805 if ($status == 5 && $billed) {
806 $statusClass =
'status6';
809 $statusLong = $langs->transnoentitiesnoconv($this->statuts[$status]).$billedtext;
810 $statusShort = $langs->transnoentitiesnoconv($this->statuts_short[$status]);
812 $parameters = array(
'status' => $status,
'mode' => $mode,
'billed' => $billed);
813 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
815 return $hookmanager->resPrint;
818 return dolGetStatus($statusLong, $statusShort,
'', $statusClass, $mode);
832 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
834 global $langs, $conf, $user, $hookmanager;
840 if ($user->hasRight(
"fournisseur",
"commande",
"read")) {
841 $label =
'<u class="paddingrightonly">'.$langs->trans(
"SupplierOrder").
'</u>';
842 if (isset($this->statut)) {
843 $label .=
' '.$this->getLibStatut(5);
845 if (!empty($this->
ref)) {
846 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
848 if (!empty($this->ref_supplier)) {
849 $label .=
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
851 if (!empty($this->total_ht)) {
852 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
854 if (!empty($this->total_tva)) {
855 $label .=
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
857 if (!empty($this->total_ttc)) {
858 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
860 if (!empty($this->date)) {
861 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
863 if (!empty($this->delivery_date)) {
864 $label .=
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
869 $url = DOL_URL_ROOT.
'/fourn/commande/card.php?id='.$this->id;
871 if ($option !==
'nolink') {
873 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
874 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
875 $add_save_lastsearch_values = 1;
877 if ($add_save_lastsearch_values) {
878 $url .=
'&save_lastsearch_values=1';
883 if (empty($notooltip)) {
884 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
885 $label = $langs->trans(
"ShowOrder");
886 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
888 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
889 $linkclose .=
' class="classfortooltip"';
892 $linkstart =
'<a href="'.$url.
'"';
893 $linkstart .= $linkclose.
'>';
896 $result .= $linkstart;
898 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
900 if ($withpicto != 2) {
901 $result .= $this->ref;
905 if ($addlinktonotes) {
906 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
909 $result .=
' <span class="note inline-block">';
910 $result .=
'<a href="'.DOL_URL_ROOT.
'/fourn/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
915 $result .=
'</span>';
920 $hookmanager->initHooks(array($this->element .
'dao'));
921 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
922 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
924 $result = $hookmanager->resPrint;
926 $result .= $hookmanager->resPrint;
941 global $db, $langs, $conf;
942 $langs->load(
"orders");
944 if (!empty($conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER)) {
947 $file = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER.
'.php';
948 $classname = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER;
951 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
953 foreach ($dirmodels as $reldir) {
954 $dir =
dol_buildpath($reldir.
"core/modules/supplier_order/");
957 $mybool |= @include_once $dir.$file;
960 if ($mybool ===
false) {
965 $obj =
new $classname();
966 $numref = $obj->getNextValue($soc, $this);
971 $this->error = $obj->error;
975 $this->error =
"Error_COMMANDE_SUPPLIER_ADDON_NotDefined";
995 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande_fournisseur SET billed = 1';
998 if ($this->
db->query($sql)) {
1001 $result = $this->
call_trigger(
'ORDER_SUPPLIER_CLASSIFY_BILLED', $user);
1011 $this->
db->commit();
1014 $this->
db->rollback();
1020 $this->
db->rollback();
1033 public function approve($user, $idwarehouse = 0, $secondlevel = 0)
1035 global $langs, $conf;
1036 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1042 if ($user->rights->fournisseur->commande->approuver) {
1049 $soc->fetch($this->fourn_id);
1052 if (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
1060 $movetoapprovestatus =
true;
1063 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
1064 $sql .=
" SET ref='".$this->db->escape($num).
"',";
1065 if (empty($secondlevel)) {
1066 $sql .=
" date_approve='".$this->db->idate($now).
"',";
1067 $sql .=
" fk_user_approve = ".$user->id;
1068 if (!empty($conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED) && $this->total_ht >= $conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED) {
1069 if (empty($this->user_approve_id2)) {
1070 $movetoapprovestatus =
false;
1071 $comment =
' (first level)';
1076 $sql .=
" date_approve2='".$this->db->idate($now).
"',";
1077 $sql .=
" fk_user_approve2 = ".((int) $user->id);
1078 if (empty($this->user_approve_id)) {
1079 $movetoapprovestatus =
false;
1081 $comment =
' (second level)';
1084 if ($movetoapprovestatus) {
1085 $sql .=
", fk_statut = ".self::STATUS_ACCEPTED;
1087 $sql .=
", fk_statut = ".self::STATUS_VALIDATED;
1089 $sql .=
" WHERE rowid = ".((int) $this->
id);
1090 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
1092 if ($this->
db->query($sql)) {
1093 if (!empty($conf->global->SUPPLIER_ORDER_AUTOADD_USER_CONTACT)) {
1094 $result = $this->
add_contact($user->id,
'SALESREPFOLL',
'internal', 1);
1095 if ($result < 0 && $result != -2) {
1101 if (!$error && $movetoapprovestatus &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER)) {
1102 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
1103 $langs->load(
"agenda");
1105 $cpt = count($this->lines);
1106 for ($i = 0; $i < $cpt; $i++) {
1108 if ($this->lines[$i]->fk_product > 0) {
1109 $this->line = $this->lines[$i];
1111 $mouvP->origin = &$this;
1112 $mouvP->setOrigin($this->element, $this->
id);
1114 $up_ht_disc = $this->lines[$i]->subprice;
1115 if (!empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) {
1116 $up_ht_disc =
price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100,
'MU');
1118 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans(
"OrderApprovedInDolibarr", $this->ref));
1129 $result = $this->
call_trigger(
'ORDER_SUPPLIER_APPROVE', $user);
1137 $this->
ref = $this->newref;
1139 if ($movetoapprovestatus) {
1144 if (empty($secondlevel)) {
1145 $this->date_approve = $now;
1146 $this->user_approve_id = $user->id;
1149 $this->date_approve2 = $now;
1150 $this->user_approve_id2 = $user->id;
1153 $this->
db->commit();
1156 $this->
db->rollback();
1160 $this->
db->rollback();
1161 $this->error = $this->
db->lasterror();
1165 dol_syslog(get_class($this).
"::approve Not Authorized", LOG_ERR);
1178 global $conf, $langs;
1184 if ($user->rights->fournisseur->commande->approuver) {
1188 $sql .=
" WHERE rowid = ".((int) $this->
id);
1190 if ($this->
db->query($sql)) {
1195 $result = $this->
call_trigger(
'ORDER_SUPPLIER_REFUSE', $user);
1198 $this->
db->rollback();
1200 $this->
db->commit();
1205 $this->
db->rollback();
1206 $this->error = $this->
db->lasterror();
1207 dol_syslog(get_class($this).
"::refuse Error -1");
1211 dol_syslog(get_class($this).
"::refuse Not Authorized");
1225 public function Cancel($user, $idwarehouse = -1)
1228 global $langs, $conf;
1234 if ($user->rights->fournisseur->commande->commander) {
1239 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur SET fk_statut = ".((int) $statut);
1240 $sql .=
" WHERE rowid = ".((int) $this->
id);
1241 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
1242 if ($this->
db->query($sql)) {
1246 $result = $this->
call_trigger(
'ORDER_SUPPLIER_CANCEL', $user);
1253 $this->
db->commit();
1256 $this->
db->rollback();
1260 $this->
db->rollback();
1261 $this->error = $this->
db->lasterror();
1262 dol_syslog(get_class($this).
"::cancel ".$this->error);
1266 dol_syslog(get_class($this).
"::cancel Not Authorized");
1280 public function commande($user, $date, $methode, $comment =
'')
1285 if ($user->rights->fournisseur->commande->commander) {
1288 $newnoteprivate = $this->note_private;
1290 $newnoteprivate =
dol_concatdesc($newnoteprivate, $langs->trans(
"Comment").
': '.$comment);
1293 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
1294 $sql .=
" SET fk_statut=".self::STATUS_ORDERSENT.
", fk_input_method=".$methode.
", date_commande='".$this->
db->idate($date).
"', ";
1295 $sql .=
" note_private='".$this->db->escape($newnoteprivate).
"'";
1296 $sql .=
" WHERE rowid=".((int) $this->
id);
1298 dol_syslog(get_class($this).
"::commande", LOG_DEBUG);
1299 if ($this->
db->query($sql)) {
1301 $this->methode_commande_id = $methode;
1302 $this->date_commande = $date;
1303 $this->context = array(
'comments' => $comment);
1306 $result = $this->
call_trigger(
'ORDER_SUPPLIER_SUBMIT', $user);
1313 $this->error = $this->
db->lasterror();
1314 $this->errors[] = $this->
db->lasterror();
1318 $this->
db->commit();
1320 $this->
db->rollback();
1324 $this->error = $langs->trans(
'NotAuthorized');
1325 $this->errors[] = $langs->trans(
'NotAuthorized');
1326 dol_syslog(get_class($this).
"::commande User not Authorized", LOG_WARNING);
1329 return ($error ? -1 : 1);
1339 public function create($user, $notrigger = 0)
1341 global $langs, $conf, $hookmanager;
1349 $date = ($this->date_commande ? $this->date_commande : $this->date);
1353 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
1356 if (empty($this->source)) {
1361 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1366 if (empty($this->fk_multicurrency)) {
1367 $this->multicurrency_code = $conf->currency;
1368 $this->fk_multicurrency = 0;
1369 $this->multicurrency_tx = 1;
1373 $this->brouillon = 1;
1375 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande_fournisseur (";
1377 $sql .=
", ref_supplier";
1378 $sql .=
", note_private";
1379 $sql .=
", note_public";
1382 $sql .=
", fk_projet";
1383 $sql .=
", date_creation";
1384 $sql .=
", date_livraison";
1385 $sql .=
", fk_user_author";
1386 $sql .=
", fk_statut";
1388 $sql .=
", model_pdf";
1389 $sql .=
", fk_mode_reglement";
1390 $sql .=
", fk_cond_reglement";
1391 $sql .=
", fk_account";
1392 $sql .=
", fk_incoterms, location_incoterms";
1393 $sql .=
", fk_multicurrency";
1394 $sql .=
", multicurrency_code";
1395 $sql .=
", multicurrency_tx";
1397 $sql .=
" VALUES (";
1399 $sql .=
", '".$this->db->escape($this->ref_supplier).
"'";
1400 $sql .=
", '".$this->db->escape($this->note_private).
"'";
1401 $sql .=
", '".$this->db->escape($this->note_public).
"'";
1402 $sql .=
", ".setEntity($this);
1403 $sql .=
", ".((int) $this->socid);
1404 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
1405 $sql .=
", '".$this->db->idate($date).
"'";
1406 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
1407 $sql .=
", ".((int) $user->id);
1408 $sql .=
", ".self::STATUS_DRAFT;
1409 $sql .=
", ".((int) $this->source);
1410 $sql .=
", '".$this->db->escape($conf->global->COMMANDE_SUPPLIER_ADDON_PDF).
"'";
1411 $sql .=
", ".($this->mode_reglement_id > 0 ? $this->mode_reglement_id :
'null');
1412 $sql .=
", ".($this->cond_reglement_id > 0 ? $this->cond_reglement_id :
'null');
1413 $sql .=
", ".($this->fk_account > 0 ? $this->fk_account :
'NULL');
1414 $sql .=
", ".(int) $this->fk_incoterms;
1415 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
1416 $sql .=
", ".(int) $this->fk_multicurrency;
1417 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1418 $sql .=
", ".(double) $this->multicurrency_tx;
1421 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1422 if ($this->
db->query($sql)) {
1423 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"commande_fournisseur");
1426 $num = count($this->lines);
1429 for ($i = 0; $i < $num; $i++) {
1430 $line = $this->lines[$i];
1431 if (!is_object($line)) {
1432 $line = (object) $line;
1444 $line->localtax1_tx,
1445 $line->localtax2_tx,
1449 $line->remise_percent,
1452 $line->product_type,
1457 $line->array_options,
1462 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
1463 $this->
db->rollback();
1468 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
1469 $sql .=
" SET ref='(PROV".$this->id.
")'";
1470 $sql .=
" WHERE rowid=".((int) $this->
id);
1471 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1472 if ($this->
db->query($sql)) {
1475 $this->
ref =
"(PROV".$this->id.
")";
1477 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1478 $this->linked_objects = $this->linkedObjectsIds;
1482 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1483 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1484 if (is_array($tmp_origin_id)) {
1485 foreach ($tmp_origin_id as $origin_id) {
1494 $origin_id = $tmp_origin_id;
1512 if (!$error && !$notrigger) {
1514 $result = $this->
call_trigger(
'ORDER_SUPPLIER_CREATE', $user);
1516 $this->
db->rollback();
1522 $this->
db->commit();
1525 $this->error = $this->
db->lasterror();
1526 $this->
db->rollback();
1531 $this->error = $this->
db->lasterror();
1532 $this->
db->rollback();
1551 if (isset($this->
ref)) {
1552 $this->
ref = trim($this->
ref);
1554 if (isset($this->ref_supplier)) {
1555 $this->ref_supplier = trim($this->ref_supplier);
1557 if (isset($this->note_private)) {
1558 $this->note_private = trim($this->note_private);
1560 if (isset($this->note_public)) {
1561 $this->note_public = trim($this->note_public);
1563 if (isset($this->model_pdf)) {
1564 $this->model_pdf = trim($this->model_pdf);
1566 if (isset($this->import_key)) {
1567 $this->import_key = trim($this->import_key);
1571 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
1573 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1574 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1575 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1576 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
1577 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
1578 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
1579 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
1580 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
1581 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
1582 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
1583 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
1584 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
1585 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id :
"null").
",";
1586 $sql .=
" fk_user_valid=".(isset($this->user_valid) && $this->user_valid > 0 ? $this->user_valid :
"null").
",";
1587 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
1588 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
1589 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
1590 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
1592 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
1594 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1595 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1596 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1597 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
1599 $sql .=
" WHERE rowid=".((int) $this->
id);
1603 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1607 $this->errors[] =
"Error ".$this->db->lasterror();
1617 if (!$error && !$notrigger) {
1619 $result = $this->
call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
1628 foreach ($this->errors as $errmsg) {
1629 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1630 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1632 $this->
db->rollback();
1635 $this->
db->commit();
1650 global $conf, $user, $hookmanager;
1657 foreach ($this->lines as $line) {
1658 $line->fetch_optionals();
1662 $objFrom = clone $this;
1665 if (!empty($socid) && $socid != $this->socid) {
1668 if ($objsoc->fetch($socid) > 0) {
1669 $this->socid = $objsoc->id;
1670 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1671 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1672 $this->fk_project = 0;
1673 $this->fk_delivery_address = 0;
1683 $this->user_author_id = $user->id;
1684 $this->user_valid = 0;
1685 $this->date_creation =
'';
1686 $this->date_validation =
'';
1687 $this->ref_supplier =
'';
1688 $this->user_approve_id =
'';
1689 $this->user_approve_id2 =
'';
1690 $this->date_approve =
'';
1691 $this->date_approve2 =
'';
1694 $this->context[
'createfromclone'] =
'createfromclone';
1695 $result = $this->
create($user, $notrigger);
1702 if (is_object($hookmanager)) {
1703 $parameters = array(
'objFrom'=>$objFrom);
1705 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1712 unset($this->context[
'createfromclone']);
1716 $this->
db->commit();
1719 $this->
db->rollback();
1753 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 =
false, $date_start =
null, $date_end =
null, $array_options = 0, $fk_unit =
null, $pu_ht_devise = 0, $origin =
'', $origin_id = 0, $rang = -1, $special_code = 0)
1755 global $langs, $mysoc, $conf;
1757 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");
1758 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1760 if ($this->statut == self::STATUS_DRAFT) {
1761 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1770 if (empty($txtva)) {
1776 if (empty($txlocaltax1)) {
1779 if (empty($txlocaltax2)) {
1782 if (empty($remise_percent)) {
1783 $remise_percent = 0;
1786 $remise_percent =
price2num($remise_percent);
1789 $pu_ht_devise =
price2num($pu_ht_devise);
1791 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1796 if ($price_base_type ==
'HT') {
1801 $desc = trim($desc);
1804 if ($qty < 0 && !$fk_product) {
1805 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Product"));
1811 if ($date_start && $date_end && $date_start > $date_end) {
1812 $langs->load(
"errors");
1813 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1820 $product_type = $type;
1823 if ($fk_product > 0) {
1824 if (!empty($conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY)) {
1826 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);
1828 if ($prod->fetch($fk_product) > 0) {
1829 $product_type = $prod->type;
1830 $label = $prod->label;
1834 $result = $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product,
'none', (isset($this->fk_soc) ? $this->fk_soc : $this->socid));
1838 if ($result > 0 && ($origin ==
'commande' || $pu ===
'')) {
1839 $pu = $prod->fourn_pu;
1840 $ref_supplier = $prod->ref_supplier;
1842 if ($remise_percent == 0 && $prod->remise_percent != 0) {
1843 $remise_percent = $prod->remise_percent;
1847 $langs->load(
"errors");
1848 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
1849 $this->
db->rollback();
1850 dol_syslog(get_class($this).
"::addline we did not found supplier price, so we can't guess unit price");
1855 if ($result == -1) {
1856 $langs->load(
"errors");
1857 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
1858 $this->
db->rollback();
1859 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_DEBUG);
1863 $this->error = $prod->error;
1864 $this->
db->rollback();
1865 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_ERR);
1869 $this->error = $prod->error;
1870 $this->
db->rollback();
1876 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
1878 $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product,
'none', ($this->fk_soc ? $this->fk_soc : $this->socid));
1880 if ($qty < $prod->packaging) {
1881 $qty = $prod->packaging;
1883 if (!empty($prod->packaging) && ($qty % $prod->packaging) > 0) {
1884 $coeff = intval($qty / $prod->packaging) + 1;
1885 $qty = $prod->packaging * $coeff;
1886 setEventMessages($langs->trans(
'QtyRecalculatedWithPackaging'),
null,
'mesgs');
1892 if (
isModEnabled(
"multicurrency") && $pu_ht_devise > 0) {
1901 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1902 $vat_src_code = $reg[1];
1903 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1911 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
1913 $total_ht = $tabprice[0];
1914 $total_tva = $tabprice[1];
1915 $total_ttc = $tabprice[2];
1916 $total_localtax1 = $tabprice[9];
1917 $total_localtax2 = $tabprice[10];
1918 $pu = $pu_ht = $tabprice[3];
1921 $multicurrency_total_ht = $tabprice[16];
1922 $multicurrency_total_tva = $tabprice[17];
1923 $multicurrency_total_ttc = $tabprice[18];
1924 $pu_ht_devise = $tabprice[19];
1926 $localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1927 $localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1931 $rang = $rangmax + 1;
1937 $this->line->context = $this->context;
1939 $this->line->fk_commande = $this->id;
1940 $this->line->label = $label;
1941 $this->line->ref_fourn = $ref_supplier;
1942 $this->line->ref_supplier = $ref_supplier;
1943 $this->line->desc = $desc;
1944 $this->line->qty = $qty;
1945 $this->line->tva_tx = $txtva;
1946 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1947 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1948 $this->line->localtax1_type = $localtax1_type;
1949 $this->line->localtax2_type = $localtax2_type;
1950 $this->line->fk_product = $fk_product;
1951 $this->line->product_type = $product_type;
1952 $this->line->remise_percent = $remise_percent;
1953 $this->line->subprice = $pu_ht;
1954 $this->line->rang = $rang;
1955 $this->line->info_bits = $info_bits;
1957 $this->line->vat_src_code = $vat_src_code;
1958 $this->line->total_ht = $total_ht;
1959 $this->line->total_tva = $total_tva;
1960 $this->line->total_localtax1 = $total_localtax1;
1961 $this->line->total_localtax2 = $total_localtax2;
1962 $this->line->total_ttc = $total_ttc;
1963 $this->line->product_type = $type;
1964 $this->line->special_code = (!empty($this->special_code) ? $this->special_code : 0);
1965 $this->line->origin = $origin;
1966 $this->line->origin_id = $origin_id;
1967 $this->line->fk_unit = $fk_unit;
1969 $this->line->date_start = $date_start;
1970 $this->line->date_end = $date_end;
1973 $this->line->fk_multicurrency = $this->fk_multicurrency;
1974 $this->line->multicurrency_code = $this->multicurrency_code;
1975 $this->line->multicurrency_subprice = $pu_ht_devise;
1976 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1977 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1978 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1980 $this->line->subprice = $pu_ht;
1981 $this->line->price = $this->line->subprice;
1983 $this->line->remise_percent = $remise_percent;
1985 if (is_array($array_options) && count($array_options) > 0) {
1986 $this->line->array_options = $array_options;
1989 $result = $this->line->insert($notrigger);
1992 if (!empty($fk_parent_line)) {
1994 } elseif ($rang > 0 && $rang <= count($this->lines)) {
1995 $linecount = count($this->lines);
1996 for ($ii = $rang; $ii <= $linecount; $ii++) {
2002 $result = $this->
update_price(1,
'auto', 0, $this->thirdparty);
2004 $this->
db->commit();
2005 return $this->line->id;
2007 $this->
db->rollback();
2011 $this->error = $this->line->error;
2012 $this->errors = $this->line->errors;
2013 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
2014 $this->
db->rollback();
2037 public function dispatchProduct($user, $product, $qty, $entrepot, $price = 0, $comment =
'', $eatby =
'', $sellby =
'', $batch =
'', $fk_commandefourndet = 0, $notrigger = 0)
2039 global $conf, $langs;
2042 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2045 if ($entrepot <= 0) {
2046 $this->error =
'ErrorBadValueForParameterWarehouse';
2050 $this->error =
'ErrorBadValueForParameterQty';
2054 $dispatchstatus = 1;
2055 if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
2056 $dispatchstatus = 0;
2063 if (($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY || $this->statut == self::STATUS_RECEIVED_COMPLETELY)) {
2066 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande_fournisseur_dispatch";
2067 $sql .=
" (fk_commande, fk_product, qty, fk_entrepot, fk_user, datec, fk_commandefourndet, status, comment, eatby, sellby, batch) VALUES";
2068 $sql .=
" ('".$this->id.
"','".$product.
"','".$qty.
"',".($entrepot > 0 ?
"'".$entrepot.
"'" :
"null").
",'".$user->id.
"','".$this->db->idate($now).
"','".$fk_commandefourndet.
"', ".$dispatchstatus.
", '".$this->
db->escape($comment).
"', ";
2069 $sql .= ($eatby ?
"'".$this->db->idate($eatby).
"'" :
"null").
", ".($sellby ?
"'".$this->
db->idate($sellby).
"'" :
"null").
", ".($batch ?
"'".$this->
db->escape($batch).
"'" :
"null");
2072 dol_syslog(get_class($this).
"::dispatchProduct", LOG_DEBUG);
2076 global $conf, $langs, $user;
2078 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_DISPATCH', $user);
2085 $this->error = $this->
db->lasterror();
2090 if (!$error && $entrepot > 0 &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)) {
2094 $mouv->origin = &$this;
2095 $mouv->setOrigin($this->element, $this->
id);
2098 if (!empty($conf->global->SUPPLIER_ORDER_ALLOW_NEGATIVE_QTY_FOR_SUPPLIER_ORDER_RETURN) && $qty < 0) {
2099 $result = $mouv->livraison($user, $product, $entrepot, $qty*(-1), $price, $comment, $now, $eatby, $sellby, $batch, 0, $inventorycode);
2101 $result = $mouv->reception($user, $product, $entrepot, $qty, $price, $comment, $eatby, $sellby, $batch,
'', 0, $inventorycode);
2105 $this->error = $mouv->error;
2106 $this->errors = $mouv->errors;
2107 dol_syslog(get_class($this).
"::dispatchProduct ".$this->error.
" ".join(
',', $this->errors), LOG_ERR);
2114 $this->
db->commit();
2117 $this->
db->rollback();
2121 $this->error =
'BadStatusForObject';
2135 if ($this->statut == 0) {
2138 if ($line->fetch($idline) <= 0) {
2142 if ($line->delete($notrigger) > 0) {
2146 $this->error = $line->error;
2147 $this->errors = $line->errors;
2162 public function delete(
User $user, $notrigger = 0)
2164 global $langs, $conf;
2165 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
2171 if (empty($notrigger)) {
2173 $result = $this->
call_trigger(
'ORDER_SUPPLIER_DELETE', $user);
2175 $this->errors[] =
'ErrorWhenRunningTrigger';
2176 dol_syslog(get_class($this).
"::delete ".$this->error, LOG_ERR);
2177 $this->
db->rollback();
2185 if (!empty($this->linkedObjects) && array_key_exists(
'reception', $this->linkedObjects)) {
2186 foreach ($this->linkedObjects[
'reception'] as $element) {
2187 if ($element->statut >= 0) {
2188 $this->errors[] = $langs->trans(
'ReceptionExist');
2195 $main = MAIN_DB_PREFIX.
'commande_fournisseurdet';
2196 $ef = $main.
"_extrafields";
2197 $sql =
"DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_commande = ".((int) $this->
id).
")";
2198 dol_syslog(get_class($this).
"::delete extrafields lines", LOG_DEBUG);
2199 if (!$this->
db->query($sql)) {
2200 $this->error = $this->
db->lasterror();
2201 $this->errors[] = $this->
db->lasterror();
2205 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet WHERE fk_commande =".((int) $this->
id);
2206 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
2207 if (!$this->
db->query($sql)) {
2208 $this->error = $this->
db->lasterror();
2209 $this->errors[] = $this->
db->lasterror();
2213 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"commande_fournisseur WHERE rowid =".((int) $this->
id);
2214 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
2215 if (
$resql = $this->
db->query($sql)) {
2216 if ($this->
db->affected_rows(
$resql) < 1) {
2217 $this->error = $this->
db->lasterror();
2218 $this->errors[] = $this->
db->lasterror();
2222 $this->error = $this->
db->lasterror();
2223 $this->errors[] = $this->
db->lasterror();
2231 $this->error =
'FailToDeleteExtraFields';
2232 $this->errors[] =
'FailToDeleteExtraFields';
2234 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
2241 $this->error =
'FailToDeleteObjectLinked';
2242 $this->errors[] =
'FailToDeleteObjectLinked';
2252 if ($conf->fournisseur->commande->dir_output) {
2253 $dir = $conf->fournisseur->commande->dir_output.
"/".$ref;
2254 $file = $dir.
"/".$ref.
".pdf";
2255 if (file_exists($file)) {
2257 $this->error =
'ErrorFailToDeleteFile';
2258 $this->errors[] =
'ErrorFailToDeleteFile';
2262 if (file_exists($dir)) {
2265 $this->error =
'ErrorFailToDeleteDir';
2266 $this->errors[] =
'ErrorFailToDeleteDir';
2274 dol_syslog(get_class($this).
"::delete $this->id by $user->id", LOG_DEBUG);
2275 $this->
db->commit();
2278 dol_syslog(get_class($this).
"::delete ".$this->error, LOG_ERR);
2279 $this->
db->rollback();
2293 $sql =
"SELECT rowid, libelle";
2294 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_input_method";
2295 $sql .=
" WHERE active = 1";
2300 $num = $this->
db->num_rows(
$resql);
2301 $this->methodes_commande = array();
2303 $row = $this->
db->fetch_row(
$resql);
2305 $this->methodes_commande[$row[0]] = $row[1];
2328 $sql =
"SELECT p.ref, p.label,";
2329 $sql .=
" e.rowid as warehouse_id, e.ref as entrepot,";
2330 $sql .=
" cfd.rowid as dispatchedlineid, cfd.fk_product, cfd.qty, cfd.eatby, cfd.sellby, cfd.batch, cfd.comment, cfd.status";
2331 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product as p,";
2332 $sql .=
" ".MAIN_DB_PREFIX.
"commande_fournisseur_dispatch as cfd";
2333 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"entrepot as e ON cfd.fk_entrepot = e.rowid";
2334 $sql .=
" WHERE cfd.fk_commande = ".((int) $this->
id);
2335 $sql .=
" AND cfd.fk_product = p.rowid";
2337 $sql .=
" AND cfd.status = ".((int) $status);
2339 $sql .=
" ORDER BY cfd.rowid ASC";
2343 $num = $this->
db->num_rows(
$resql);
2347 $objp = $this->
db->fetch_object(
$resql);
2350 'id' => $objp->dispatchedlineid,
2351 'productid' => $objp->fk_product,
2352 'warehouseid' => $objp->warehouse_id,
2353 'qty' => $objp->qty,
2360 dol_print_error($this->
db,
'Failed to execute request to get dispatched lines');
2379 global $conf, $langs;
2386 $usercanreceive = 0;
2388 $usercanreceive = $user->rights->fournisseur->commande->receptionner;
2390 $usercanreceive = $user->rights->reception->creer;
2393 if ($usercanreceive) {
2395 if ($type ==
'par') {
2397 } elseif ($type ==
'tot') {
2399 } elseif ($type ==
'nev') {
2401 } elseif ($type ==
'can') {
2405 dol_syslog(get_class($this).
"::Livraison Error -2", LOG_ERR);
2410 if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
2412 if (!$error && ($type ==
'tot')) {
2414 if (count($dispatchedlinearray) > 0) {
2417 $this->errors[] =
'ErrorCantSetReceptionToTotalDoneWithReceptionToApprove';
2418 dol_syslog(
'ErrorCantSetReceptionToTotalDoneWithReceptionToApprove', LOG_DEBUG);
2421 if (!$error && !empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS_NEED_APPROVE) && ($type ==
'tot')) {
2423 if (count($dispatchedlinearray) > 0) {
2426 $this->errors[] =
'ErrorCantSetReceptionToTotalDoneWithReceptionDenied';
2427 dol_syslog(
'ErrorCantSetReceptionToTotalDoneWithReceptionDenied', LOG_DEBUG);
2434 if (empty($error)) {
2437 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
2438 $sql .=
" SET fk_statut = ".((int) $statut);
2439 $sql .=
" WHERE rowid = ".((int) $this->
id);
2440 $sql .=
" AND fk_statut IN (".self::STATUS_ORDERSENT.
",".self::STATUS_RECEIVED_PARTIALLY.
")";
2442 dol_syslog(get_class($this).
"::Livraison", LOG_DEBUG);
2446 $old_statut = $this->statut;
2447 $this->statut = $statut;
2448 $this->actionmsg2 = $comment;
2451 $result_trigger = $this->
call_trigger(
'ORDER_SUPPLIER_RECEIVE', $user);
2452 if ($result_trigger < 0) {
2457 if (empty($error)) {
2458 $this->
db->commit();
2460 $this->statut = $old_statut;
2461 $this->
db->rollback();
2462 $this->error = $this->
db->lasterror();
2466 $this->
db->rollback();
2467 $this->error = $this->
db->lasterror();
2472 $this->error = $langs->trans(
'NotAuthorized');
2473 $this->errors[] = $langs->trans(
'NotAuthorized');
2474 dol_syslog(get_class($this).
"::Livraison Not Authorized");
2506 if ($user->rights->fournisseur->commande->creer || $user->rights->supplier_order->creer) {
2511 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
2512 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2513 $sql .=
" WHERE rowid = ".((int) $this->
id);
2518 $this->errors[] = $this->
db->error();
2523 $this->oldcopy = clone $this;
2524 $this->date_livraison = $delivery_date;
2525 $this->delivery_date = $delivery_date;
2528 if (!$notrigger && empty($error)) {
2530 $result = $this->
call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
2538 $this->
db->commit();
2541 foreach ($this->errors as $errmsg) {
2542 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2543 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2545 $this->
db->rollback();
2565 if ($user->rights->fournisseur->commande->creer || $user->rights->supplier_order->creer) {
2570 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
2571 $sql .=
" SET fk_projet = ".($id_projet > 0 ? (int) $id_projet :
'null');
2572 $sql .=
" WHERE rowid = ".((int) $this->
id);
2577 $this->errors[] = $this->
db->error();
2582 $this->oldcopy = clone $this;
2583 $this->fk_projet = $id_projet;
2584 $this->fk_project = $id_projet;
2587 if (!$notrigger && empty($error)) {
2589 $result = $this->
call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
2597 $this->
db->commit();
2600 foreach ($this->errors as $errmsg) {
2601 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2602 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2604 $this->
db->rollback();
2623 $comclient->fetch($comclientid);
2627 $this->lines = array();
2629 $num = count($comclient->lines);
2630 for ($i = 0; $i < $num; $i++) {
2634 if ($prod->fetch($comclient->lines[$i]->fk_product) > 0) {
2635 $label = $prod->label;
2639 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande_fournisseurdet";
2640 $sql .=
" (fk_commande, label, description, fk_product, price, qty, tva_tx, localtax1_tx, localtax2_tx, remise_percent, subprice, remise, ref)";
2641 $sql .=
" VALUES (".((int) $idc).
", '".$this->
db->escape($label).
"', '".$this->
db->escape($comclient->lines[$i]->desc).
"'";
2642 $sql .=
",".$comclient->lines[$i]->fk_product.
", ".
price2num($comclient->lines[$i]->price,
'MU');
2643 $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);
2644 $sql .=
", '".price2num($comclient->lines[$i]->subprice,
'MT').
"','0', '".$this->
db->escape($ref).
"');";
2645 if ($this->
db->query($sql)) {
2662 global $conf, $langs;
2667 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande_fournisseur';
2668 $sql .=
" SET fk_statut = ".$status;
2669 $sql .=
" WHERE rowid = ".((int) $this->
id);
2671 dol_syslog(get_class($this).
"::setStatus", LOG_DEBUG);
2675 $triggerName = array();
2676 $triggerName[0] =
'DRAFT';
2677 $triggerName[1] =
'VALIDATED';
2678 $triggerName[2] =
'APPROVED';
2679 $triggerName[3] =
'ORDERED';
2680 $triggerName[4] =
'RECEIVED_PARTIALLY';
2681 $triggerName[5] =
'RECEIVED_COMPLETELY';
2682 $triggerName[6] =
'CANCELED';
2683 $triggerName[7] =
'CANCELED';
2684 $triggerName[9] =
'REFUSED';
2687 $result = $this->
call_trigger(
"ORDER_SUPPLIER_STATUS_".$triggerName[$status], $user);
2694 $this->error = $this->
db->lasterror();
2695 dol_syslog(get_class($this).
"::setStatus ".$this->error);
2699 $this->statut = $status;
2700 $this->
db->commit();
2703 $this->
db->rollback();
2731 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 =
'', $date_end =
'', $array_options = 0, $fk_unit =
null, $pu_ht_devise = 0, $ref_supplier =
'')
2733 global $mysoc, $conf, $langs;
2734 dol_syslog(get_class($this).
"::updateline $rowid, $desc, $pu, $qty, $remise_percent, $txtva, $price_base_type, $info_bits, $type, $fk_unit");
2735 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2739 if ($this->brouillon) {
2744 if (empty($info_bits)) {
2747 if (empty($txtva)) {
2750 if (empty($txlocaltax1)) {
2753 if (empty($txlocaltax2)) {
2756 if (empty($remise)) {
2759 if (empty($remise_percent)) {
2760 $remise_percent = 0;
2763 $remise_percent =
price2num($remise_percent);
2769 $pu_ht_devise =
price2num($pu_ht_devise);
2770 if (!preg_match(
'/\((.*)\)/', $txtva)) {
2780 if ($date_start && $date_end && $date_start > $date_end) {
2781 $langs->load(
"errors");
2782 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2798 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
2799 $vat_src_code = $reg[1];
2800 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
2803 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
2804 $total_ht = $tabprice[0];
2805 $total_tva = $tabprice[1];
2806 $total_ttc = $tabprice[2];
2807 $total_localtax1 = $tabprice[9];
2808 $total_localtax2 = $tabprice[10];
2809 $pu_ht = $tabprice[3];
2810 $pu_tva = $tabprice[4];
2811 $pu_ttc = $tabprice[5];
2814 $multicurrency_total_ht = $tabprice[16];
2815 $multicurrency_total_tva = $tabprice[17];
2816 $multicurrency_total_ttc = $tabprice[18];
2817 $pu_ht_devise = $tabprice[19];
2819 $localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2820 $localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2824 $this->line->fetch($rowid);
2826 $oldline = clone $this->line;
2827 $this->line->oldline = $oldline;
2829 $this->line->context = $this->context;
2831 $this->line->fk_commande = $this->id;
2833 $this->line->desc = $desc;
2836 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
2837 if ($qty < $this->line->packaging) {
2838 $qty = $this->line->packaging;
2840 if (!empty($this->line->packaging) && ($qty % $this->line->packaging) > 0) {
2841 $coeff = intval($qty / $this->line->packaging) + 1;
2842 $qty = $this->line->packaging * $coeff;
2843 setEventMessage($langs->trans(
'QtyRecalculatedWithPackaging'),
'mesgs');
2848 $this->line->qty = $qty;
2849 $this->line->ref_supplier = $ref_supplier;
2851 $this->line->vat_src_code = $vat_src_code;
2852 $this->line->tva_tx = $txtva;
2853 $this->line->localtax1_tx = $txlocaltax1;
2854 $this->line->localtax2_tx = $txlocaltax2;
2855 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2856 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2857 $this->line->remise_percent = $remise_percent;
2858 $this->line->subprice = $pu_ht;
2859 $this->line->rang = $this->rang;
2860 $this->line->info_bits = $info_bits;
2861 $this->line->total_ht = $total_ht;
2862 $this->line->total_tva = $total_tva;
2863 $this->line->total_localtax1 = $total_localtax1;
2864 $this->line->total_localtax2 = $total_localtax2;
2865 $this->line->total_ttc = $total_ttc;
2866 $this->line->product_type = $type;
2867 $this->line->special_code = (!empty($this->special_code) ? $this->special_code : 0);
2868 $this->line->origin = $this->origin;
2869 $this->line->fk_unit = $fk_unit;
2871 $this->line->date_start = $date_start;
2872 $this->line->date_end = $date_end;
2875 $this->line->fk_multicurrency = $this->fk_multicurrency;
2876 $this->line->multicurrency_code = $this->multicurrency_code;
2877 $this->line->multicurrency_subprice = $pu_ht_devise;
2878 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
2879 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
2880 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
2882 $this->line->subprice = $pu_ht;
2883 $this->line->price = $this->line->subprice;
2885 $this->line->remise_percent = $remise_percent;
2887 if (is_array($array_options) && count($array_options) > 0) {
2889 foreach ($array_options as $key => $value) {
2890 $this->line->array_options[$key] = $array_options[$key];
2894 $result = $this->line->update($notrigger);
2900 $this->
db->commit();
2903 $this->error = $this->
db->lasterror();
2904 $this->
db->rollback();
2908 $this->error =
"Order status makes operation forbidden";
2909 dol_syslog(get_class($this).
"::updateline ".$this->error, LOG_ERR);
2924 global $user, $langs, $conf;
2926 include_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
2928 dol_syslog(get_class($this).
"::initAsSpecimen");
2935 $sql =
"SELECT rowid";
2936 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2937 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2938 $sql .= $this->
db->order(
"rowid",
"ASC");
2939 $sql .= $this->
db->plimit(1);
2942 $obj = $this->
db->fetch_object(
$resql);
2943 $prodid = $obj->rowid;
2948 $this->
ref =
'SPECIMEN';
2949 $this->specimen = 1;
2952 $this->date_commande = $now;
2953 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
2954 $this->cond_reglement_code =
'RECEP';
2955 $this->mode_reglement_code =
'CHQ';
2957 $this->note_public =
'This is a comment (public)';
2958 $this->note_private =
'This is a comment (private)';
2960 $this->multicurrency_tx = 1;
2961 $this->multicurrency_code = $conf->currency;
2968 while ($xnbp < $nbp) {
2970 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
2972 $line->subprice = 100;
2974 $line->tva_tx = 19.6;
2975 $line->localtax1_tx = 0;
2976 $line->localtax2_tx = 0;
2978 $line->total_ht = 50;
2979 $line->total_ttc = 59.8;
2980 $line->total_tva = 9.8;
2981 $line->remise_percent = 50;
2983 $line->total_ht = 100;
2984 $line->total_ttc = 119.6;
2985 $line->total_tva = 19.6;
2986 $line->remise_percent = 00;
2988 $line->fk_product = $prodid;
2990 $this->lines[$xnbp] = $line;
2992 $this->total_ht += $line->total_ht;
2993 $this->total_tva += $line->total_tva;
2994 $this->total_ttc += $line->total_ttc;
3008 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem, date_valid as date_validation, date_approve as datea, date_approve2 as datea2,';
3009 $sql .=
' fk_user_author, fk_user_modif, fk_user_valid, fk_user_approve, fk_user_approve2';
3010 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande_fournisseur as c';
3011 $sql .=
' WHERE c.rowid = '.((int) $id);
3013 $result = $this->
db->query($sql);
3015 if ($this->
db->num_rows($result)) {
3016 $obj = $this->
db->fetch_object($result);
3017 $this->
id = $obj->rowid;
3018 if ($obj->fk_user_author) {
3019 $this->user_creation_id = $obj->fk_user_author;
3021 if ($obj->fk_user_valid) {
3022 $this->user_validation_id = $obj->fk_user_valid;
3024 if ($obj->fk_user_modif) {
3025 $this->user_modification_id = $obj->fk_user_modif;
3027 if ($obj->fk_user_approve) {
3028 $this->user_approve_id = $obj->fk_user_approve;
3030 if ($obj->fk_user_approve2) {
3031 $this->user_approve_id2 = $obj->fk_user_approve2;
3034 $this->date_creation = $this->
db->jdate($obj->datec);
3035 $this->date_modification = $this->
db->jdate($obj->datem);
3036 $this->date_approve = $this->
db->jdate($obj->datea);
3037 $this->date_approve2 = $this->
db->jdate($obj->datea2);
3038 $this->date_validation = $this->
db->jdate($obj->date_validation);
3040 $this->
db->free($result);
3055 global $conf, $user;
3057 $this->nb = array();
3060 $sql =
"SELECT count(co.rowid) as nb";
3061 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur as co";
3062 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
3063 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3064 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3065 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3068 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'supplier_order').
")";
3072 while ($obj = $this->
db->fetch_object(
$resql)) {
3073 $this->nb[
"supplier_orders"] = $obj->nb;
3079 $this->error = $this->
db->error();
3095 global $conf, $langs;
3097 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.fk_statut, c.date_livraison as delivery_date, c.total_ht";
3098 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur as c";
3099 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3100 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3102 $sql .=
" WHERE c.entity = ".$conf->entity;
3103 if ($mode ===
'awaiting') {
3104 $sql .=
" AND c.fk_statut IN (".self::STATUS_ORDERSENT.
", ".self::STATUS_RECEIVED_PARTIALLY.
")";
3106 $sql .=
" AND c.fk_statut IN (".self::STATUS_VALIDATED.
", ".self::STATUS_ACCEPTED.
")";
3109 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3117 $response->warning_delay = $conf->commande->fournisseur->warning_delay / 60 / 60 / 24;
3118 $response->label = $langs->trans(
"SuppliersOrdersToProcess");
3119 $response->labelShort = $langs->trans(
"Opened");
3120 $response->url = DOL_URL_ROOT.
'/fourn/commande/list.php?search_status=1,2&mainmenu=commercial&leftmenu=orders_suppliers';
3123 if ($mode ===
'awaiting') {
3124 $response->label = $langs->trans(
"SuppliersOrdersAwaitingReception");
3125 $response->labelShort = $langs->trans(
"AwaitingReception");
3126 $response->url = DOL_URL_ROOT.
'/fourn/commande/list.php?search_status=3,4&mainmenu=commercial&leftmenu=orders_suppliers';
3129 while ($obj = $this->
db->fetch_object(
$resql)) {
3130 $commandestatic->delivery_date = $this->
db->jdate($obj->delivery_date);
3131 $commandestatic->date_commande = $this->
db->jdate($obj->date_commande);
3132 $commandestatic->statut = $obj->fk_statut;
3134 $response->nbtodo++;
3135 $response->total += $obj->total_ht;
3137 if ($commandestatic->hasDelay()) {
3138 $response->nbtodolate++;
3144 $this->error = $this->
db->error();
3159 if ($this->methode_commande_id > 0) {
3160 $sql =
"SELECT rowid, code, libelle as label";
3161 $sql .=
" FROM ".MAIN_DB_PREFIX.
'c_input_method';
3162 $sql .=
" WHERE active=1 AND rowid = ".((int) $this->methode_commande_id);
3167 $obj = $this->
db->fetch_object(
$resql);
3169 $string = $langs->trans($obj->code);
3170 if ($string == $obj->code) {
3171 $string = $obj->label !=
'-' ? $obj->label :
'';
3194 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
3196 global $conf, $langs;
3201 if (!empty($this->model_pdf)) {
3202 $modele = $this->model_pdf;
3203 } elseif (!empty($conf->global->COMMANDE_SUPPLIER_ADDON_PDF)) {
3204 $modele = $conf->global->COMMANDE_SUPPLIER_ADDON_PDF;
3208 if (empty($modele)) {
3211 $langs->load(
"suppliers");
3212 $outputlangs->load(
"products");
3214 $modelpath =
"core/modules/supplier_order/doc/";
3215 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3227 if (empty($this->lines)) {
3234 foreach ($this->lines as $line) {
3235 if ($line->fk_product > 0) {
3236 $idp = $obj->find_min_price_product_fournisseur($line->fk_product, $line->qty);
3239 if ($obj->delivery_time_days > $nb) {
3240 $nb = $obj->delivery_time_days;
3249 return $nb.
' '.$langs->trans(
'Days');
3261 return $user->rights->fournisseur->commande;
3276 'commande_fournisseur'
3293 'commande_fournisseurdet'
3310 if (empty($this->delivery_date) && !empty($this->date_livraison)) {
3311 $this->delivery_date = $this->date_livraison;
3314 if ($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY) {
3316 if (!empty($this->delivery_date)) {
3317 $date_to_test = $this->delivery_date;
3318 return $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3326 $date_to_test = $this->date_commande;
3328 return ($this->statut > 0 && $this->statut < 5) && $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3341 global $conf, $langs;
3343 if (empty($this->delivery_date) && !empty($this->date_livraison)) {
3344 $this->delivery_date = $this->date_livraison;
3349 if ($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY) {
3350 if (!empty($this->delivery_date)) {
3351 $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->delivery_date,
'day');
3353 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
3356 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
3359 $text .=
' '.($conf->commande->fournisseur->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->fournisseur->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
3376 global $conf, $langs;
3378 if ((
isModEnabled(
"fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) ||
isModEnabled(
"supplier_order")) {
3379 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.dispatch.class.php';
3381 $qtydelivered = array();
3382 $qtywished = array();
3385 $filter = array(
't.fk_commande'=>$this->
id);
3386 if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
3387 $filter[
't.status'] = 1;
3390 $ret = $supplierorderdispatch->fetchAll(
'',
'', 0, 0, $filter);
3392 $this->error = $supplierorderdispatch->error; $this->errors = $supplierorderdispatch->errors;
3395 if (is_array($supplierorderdispatch->lines) && count($supplierorderdispatch->lines) > 0) {
3396 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
3400 foreach ($supplierorderdispatch->lines as $line) {
3401 $qtydelivered[$line->fk_product] += $line->qty;
3403 foreach ($this->lines as $line) {
3405 if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $line->product_type > 0) {
3408 $qtywished[$line->fk_product] += $line->qty;
3412 $diff_array = array_diff_assoc($qtydelivered, $qtywished);
3413 $keysinwishednotindelivered = array_diff(array_keys($qtywished), array_keys($qtydelivered));
3414 $keysindeliverednotinwished = array_diff(array_keys($qtydelivered), array_keys($qtywished));
3422 if (count($diff_array) == 0 && count($keysinwishednotindelivered) == 0 && count($keysindeliverednotinwished) == 0) {
3423 if ($closeopenorder) {
3425 $ret = $this->
Livraison($user, $date_liv,
'tot', $comment);
3433 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3439 } elseif (!empty($conf->global->SUPPLIER_ORDER_MORE_THAN_WISHED)) {
3444 if (count($diff_array) > 0) {
3448 foreach ($diff_array as $key => $value) {
3450 if ($qtydelivered[$key] >= $qtywished[$key]) {
3457 if ($close == count($diff_array)) {
3459 if ($closeopenorder) {
3460 $ret = $this->
Livraison($user, $date_liv,
'tot', $comment);
3467 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3475 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3483 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3505 $this->receptions = array();
3507 dol_syslog(get_class($this).
"::loadReceptions", LOG_DEBUG);
3509 $sql =
'SELECT cd.rowid, cd.fk_product,';
3510 $sql .=
' sum(cfd.qty) as qty';
3511 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande_fournisseur_dispatch as cfd,';
3512 if ($filtre_statut >= 0) {
3513 $sql .=
' '.MAIN_DB_PREFIX.
'reception as e,';
3515 $sql .=
' '.MAIN_DB_PREFIX.
'commande_fournisseurdet as cd';
3517 if ($filtre_statut >= 0) {
3518 $sql .=
' cfd.fk_reception = e.rowid AND';
3520 $sql .=
' cfd.fk_commandefourndet = cd.rowid';
3521 $sql .=
' AND cd.fk_commande ='.((int) $this->
id);
3522 if ($this->fk_product > 0) {
3523 $sql .=
' AND cd.fk_product = '.((int) $this->fk_product);
3525 if ($filtre_statut >= 0) {
3526 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
3528 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
3532 $num = $this->
db->num_rows(
$resql);
3535 $obj = $this->
db->fetch_object(
$resql);
3536 empty($this->receptions[$obj->rowid]) ? $this->receptions[$obj->rowid] = $obj->qty : $this->receptions[$obj->rowid] += $obj->qty;
3543 $this->error = $this->
db->lasterror();
3559 public $element =
'commande_fournisseurdet';
3564 public $table_element =
'commande_fournisseurdet';
3572 public $fk_commande;
3578 public $fk_parent_line;
3586 public $special_code = 0;
3603 public $ref_supplier;
3627 $sql =
'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,';
3628 $sql .=
' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,';
3629 $sql .=
' cd.remise, cd.remise_percent, cd.subprice,';
3630 $sql .=
' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
3631 $sql .=
' cd.total_localtax1, cd.total_localtax2,';
3632 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
3633 $sql .=
' cd.date_start, cd.date_end, cd.fk_unit,';
3634 $sql .=
' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
3635 $sql .=
' c.fk_soc as socid';
3636 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande_fournisseur as c, '.MAIN_DB_PREFIX.
'commande_fournisseurdet as cd';
3637 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
3638 $sql .=
' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid);
3640 $result = $this->
db->query($sql);
3642 $objp = $this->
db->fetch_object($result);
3644 if (!empty($objp)) {
3645 $this->
rowid = $objp->rowid;
3646 $this->
id = $objp->rowid;
3647 $this->fk_commande = $objp->fk_commande;
3648 $this->desc = $objp->description;
3649 $this->qty = $objp->qty;
3650 $this->ref_fourn = $objp->ref_supplier;
3651 $this->ref_supplier = $objp->ref_supplier;
3652 $this->subprice = $objp->subprice;
3653 $this->tva_tx = $objp->tva_tx;
3654 $this->localtax1_tx = $objp->localtax1_tx;
3655 $this->localtax2_tx = $objp->localtax2_tx;
3656 $this->localtax1_type = $objp->localtax1_type;
3657 $this->localtax2_type = $objp->localtax2_type;
3658 $this->remise = $objp->remise;
3659 $this->remise_percent = $objp->remise_percent;
3660 $this->fk_product = $objp->fk_product;
3661 $this->info_bits = $objp->info_bits;
3662 $this->total_ht = $objp->total_ht;
3663 $this->total_tva = $objp->total_tva;
3664 $this->total_localtax1 = $objp->total_localtax1;
3665 $this->total_localtax2 = $objp->total_localtax2;
3666 $this->total_ttc = $objp->total_ttc;
3667 $this->product_type = $objp->product_type;
3668 $this->special_code = $objp->special_code;
3670 $this->
ref = $objp->product_ref;
3672 $this->product_ref = $objp->product_ref;
3673 $this->product_label = $objp->product_label;
3674 $this->product_desc = $objp->product_desc;
3676 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
3681 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.MAIN_DB_PREFIX.
"product_fournisseur_price";
3682 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'product_fournisseur_price').
")";
3683 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
3684 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
3685 $sqlsearchpackage .=
" AND quantity <= ".((float) $objp->qty);
3686 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).
")";
3687 $sqlsearchpackage .=
" AND fk_soc = ".((int) $objp->socid);
3688 $sqlsearchpackage .=
" ORDER BY packaging ASC";
3689 $sqlsearchpackage .=
" LIMIT 1";
3691 $resqlsearchpackage = $this->
db->query($sqlsearchpackage);
3692 if ($resqlsearchpackage) {
3693 $objsearchpackage = $this->
db->fetch_object($resqlsearchpackage);
3694 if ($objsearchpackage) {
3695 $this->fk_fournprice = $objsearchpackage->rowid;
3696 $this->packaging = $objsearchpackage->packaging;
3699 $this->error = $this->
db->lasterror();
3704 $this->date_start = $this->
db->jdate($objp->date_start);
3705 $this->date_end = $this->
db->jdate($objp->date_end);
3706 $this->fk_unit = $objp->fk_unit;
3708 $this->multicurrency_subprice = $objp->multicurrency_subprice;
3709 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
3710 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
3711 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
3715 $this->
db->free($result);
3718 $this->error =
'Supplier order line with id='.$rowid.
' not found';
3719 dol_syslog(get_class($this).
"::fetch Error ".$this->error, LOG_ERR);
3736 global $conf, $user;
3740 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
3743 if (empty($this->tva_tx)) {
3746 if (empty($this->localtax1_tx)) {
3747 $this->localtax1_tx = 0;
3749 if (empty($this->localtax2_tx)) {
3750 $this->localtax2_tx = 0;
3752 if (empty($this->localtax1_type)) {
3753 $this->localtax1_type =
'0';
3755 if (empty($this->localtax2_type)) {
3756 $this->localtax2_type =
'0';
3758 if (empty($this->total_localtax1)) {
3759 $this->total_localtax1 = 0;
3761 if (empty($this->total_localtax2)) {
3762 $this->total_localtax2 = 0;
3764 if (empty($this->rang)) {
3767 if (empty($this->remise_percent)) {
3768 $this->remise_percent = 0;
3770 if (empty($this->info_bits)) {
3771 $this->info_bits = 0;
3773 if (empty($this->special_code)) {
3774 $this->special_code = 0;
3776 if (empty($this->fk_parent_line)) {
3777 $this->fk_parent_line = 0;
3779 if (empty($this->pa_ht)) {
3784 if (!empty($this->multicurrency_code)) {
3787 if (empty($this->fk_multicurrency)) {
3788 $this->multicurrency_code = $conf->currency;
3789 $this->fk_multicurrency = 0;
3790 $this->multicurrency_tx = 1;
3794 if ($this->product_type < 0) {
3801 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
3802 $sql .=
" (fk_commande, label, description, date_start, date_end,";
3803 $sql .=
" fk_product, product_type, special_code, rang,";
3804 $sql .=
" qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
3805 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
3806 $sql .=
" fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc";
3808 $sql .=
" VALUES (".$this->fk_commande.
", '".$this->
db->escape($this->label).
"','".$this->
db->escape($this->desc).
"',";
3809 $sql .=
" ".($this->date_start ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
3810 $sql .=
" ".($this->date_end ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3811 if ($this->fk_product) {
3812 $sql .= $this->fk_product.
",";
3816 $sql .=
"'".$this->db->escape($this->product_type).
"',";
3817 $sql .=
"'".$this->db->escape($this->special_code).
"',";
3818 $sql .=
"'".$this->db->escape($this->rang).
"',";
3819 $sql .=
"'".$this->db->escape($this->qty).
"', ";
3820 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
3821 $sql .=
" ".price2num($this->tva_tx).
", ";
3822 $sql .=
" ".price2num($this->localtax1_tx).
",";
3823 $sql .=
" ".price2num($this->localtax2_tx).
",";
3824 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
3825 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
3826 $sql .=
" ".((float) $this->remise_percent).
", ".
price2num($this->subprice,
'MU').
", '".$this->
db->escape($this->ref_supplier).
"',";
3827 $sql .=
" ".price2num($this->total_ht).
",";
3828 $sql .=
" ".price2num($this->total_tva).
",";
3829 $sql .=
" ".price2num($this->total_localtax1).
",";
3830 $sql .=
" ".price2num($this->total_localtax2).
",";
3831 $sql .=
" ".price2num($this->total_ttc).
",";
3832 $sql .= ($this->fk_unit ?
"'".$this->db->escape($this->fk_unit).
"'" :
"null");
3833 $sql .=
", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) :
"null");
3834 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
3835 $sql .=
", ".($this->multicurrency_subprice ?
price2num($this->multicurrency_subprice) :
'0');
3836 $sql .=
", ".($this->multicurrency_total_ht ?
price2num($this->multicurrency_total_ht) :
'0');
3837 $sql .=
", ".($this->multicurrency_total_tva ?
price2num($this->multicurrency_total_tva) :
'0');
3838 $sql .=
", ".($this->multicurrency_total_ttc ?
price2num($this->multicurrency_total_ttc) :
'0');
3841 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
3844 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
3845 $this->
rowid = $this->id;
3854 if (!$error && !$notrigger) {
3856 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_CREATE', $user);
3864 $this->
db->commit();
3868 foreach ($this->errors as $errmsg) {
3869 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
3870 $this->errors[] = ($this->errors ?
', '.$errmsg : $errmsg);
3872 $this->
db->rollback();
3875 $this->errors[] = $this->
db->error();
3876 $this->
db->rollback();
3888 global $conf, $user;
3895 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
3896 $sql .=
" description='".$this->db->escape($this->desc).
"'";
3897 $sql .=
", ref='".$this->db->escape($this->ref_supplier).
"'";
3898 $sql .=
", subprice='".price2num($this->subprice).
"'";
3900 $sql .=
", remise_percent='".price2num($this->remise_percent).
"'";
3902 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
3903 $sql .=
", tva_tx='".price2num($this->tva_tx).
"'";
3904 $sql .=
", localtax1_tx='".price2num($this->total_localtax1).
"'";
3905 $sql .=
", localtax2_tx='".price2num($this->total_localtax2).
"'";
3906 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
3907 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
3908 $sql .=
", qty='".price2num($this->qty).
"'";
3909 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
3910 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
3911 $sql .=
", info_bits='".$this->db->escape($this->info_bits).
"'";
3912 $sql .=
", total_ht='".price2num($this->total_ht).
"'";
3913 $sql .=
", total_tva='".price2num($this->total_tva).
"'";
3914 $sql .=
", total_localtax1='".price2num($this->total_localtax1).
"'";
3915 $sql .=
", total_localtax2='".price2num($this->total_localtax2).
"'";
3916 $sql .=
", total_ttc='".price2num($this->total_ttc).
"'";
3917 $sql .=
", product_type=".$this->product_type;
3918 $sql .=
", special_code=".(!empty($this->special_code) ? $this->special_code : 0);
3919 $sql .= ($this->fk_unit ?
", fk_unit='".$this->db->escape($this->fk_unit).
"'" :
", fk_unit=null");
3922 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
3923 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
3924 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
3925 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
3927 $sql .=
" WHERE rowid = ".((int) $this->
id);
3929 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
3930 $result = $this->
db->query($sql);
3939 if (!$error && !$notrigger) {
3942 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_MODIFY', $user);
3944 $this->
db->rollback();
3951 $this->
db->commit();
3954 $this->
db->rollback();
3958 $this->error = $this->
db->lasterror();
3959 $this->
db->rollback();
3970 public function delete($notrigger = 0)
3981 $this->
db->rollback();
3985 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"commande_fournisseurdet WHERE rowid=".((int) $this->
id);
3992 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_DELETE', $user);
4000 $this->
db->commit();
4004 foreach ($this->errors as $errmsg) {
4005 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
4006 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4008 $this->
db->rollback();
4011 $this->error = $this->
db->lasterror();
Class to manage table commandefournisseurdispatch.
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.
deleteline($idline, $notrigger=0)
Delete line.
load_state_board()
Charge indicateurs this->nb de tableau de bord.
getNomUrl($withpicto=0, $option='', $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0)
Return clicable 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: ...
$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.
info($id)
Charge les informations d'ordre info dans l'objet facture.
const STATUS_CANCELED
Order canceled.
getNextNumRef($soc)
Returns the following order reference not used depending on the numbering model activated defined wit...
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.
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=false, $date_start=null, $date_end=null, $array_options=0, $fk_unit=null, $pu_ht_devise=0, $origin='', $origin_id=0, $rang=-1, $special_code=0)
Add order line.
calcAndSetStatusDispatch(User $user, $closeopenorder=1, $comment='')
Calc status regarding to dispatched stock.
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='', $date_end='', $array_options=0, $fk_unit=null, $pu_ht_devise=0, $ref_supplier='')
Update line.
set_id_projet($user, $id_projet, $notrigger=0)
Set the id projet.
const STATUS_DRAFT
Draft status.
showDelay()
Show the customer delayed info.
approve($user, $idwarehouse=0, $secondlevel=0)
Approve a supplier order.
__construct($db)
Constructor.
Cancel($user, $idwarehouse=-1)
Cancel an approved order.
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.
dispatchProduct($user, $product, $qty, $entrepot, $price=0, $comment='', $eatby='', $sellby='', $batch='', $fk_commandefourndet=0, $notrigger=0)
Save a receiving into the tracking table of receiving (commande_fournisseur_dispatch) and add product...
createFromClone(User $user, $socid=0, $notrigger=0)
Load an object from its id and create a new one in database.
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)
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.
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
const SOURCE_ID_REPLENISHMENT
The constant used into source field to track the order was generated by the replenishement feature.
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.
get_methodes_commande()
Get list of order methods.
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.
update($notrigger=0)
Update the line object into db.
insert($notrigger=0)
Insert line into database.
fetch($rowid)
Load line order.
__construct($db)
Constructor.
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.
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
updateRangOfLine($rowid, $rang)
Update position of line (rang)
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='', $f_user=null, $notrigger=0)
Delete all links between an object $this.
update_price($exclspec=0, $roundingadjust='none', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
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 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.
call_trigger($triggerName, $user)
Call trigger based on this instance.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Superclass for orders classes.
Superclass for orders classes.
Class to manage Dolibarr database access.
Class to manage stock movements.
static getIdFromCode($dbs, $code)
Get id of currency from code.
static getIdAndTxFromCode($dbs, $code, $date_document='')
Get id and rate 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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
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_dir_list($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.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
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_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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...
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
setEventMessage($mesgs, $style='mesgs')
Set event message in dol_events session object.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
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.
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array='', $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
$conf db
API class for accounts.