dolibarr  17.0.3
fournisseur.commande.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
6  * Copyright (C) 2010-2020 Juanjo Menent <jmenent@2byte.es>
7  * Copyright (C) 2010-2018 Philippe Grand <philippe.grand@atoo-net.com>
8  * Copyright (C) 2012-2015 Marcos García <marcosgdf@gmail.com>
9  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
10  * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
11  * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
12  * Copyright (C) 2018-2022 Frédéric France <frederic.france@netlogic.fr>
13  * Copyright (C) 2018-2022 Ferran Marcet <fmarcet@2byte.es>
14  * Copyright (C) 2021 Josep Lluís Amador <joseplluis@lliuretic.cat>
15  * Copyright (C) 2022 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <https://www.gnu.org/licenses/>.
29  */
30 
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';
40 if (isModEnabled('productbatch')) {
41  require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
42 }
43 
44 
49 {
53  public $element = 'order_supplier';
54 
58  public $table_element = 'commande_fournisseur';
59 
63  public $table_element_line = 'commande_fournisseurdet';
64 
68  public $fk_element = 'fk_commande';
69 
73  public $picto = 'supplier_order';
74 
79  public $ismultientitymanaged = 1;
80 
85  public $restrictiononfksoc = 1;
86 
90  protected $table_ref_field = 'ref';
91 
95  public $id;
96 
101  public $ref;
102 
103  public $ref_supplier;
104  public $brouillon;
105  public $statut; // 0=Draft -> 1=Validated -> 2=Approved -> 3=Ordered/Process runing -> 4=Received partially -> 5=Received totally -> (reopen) 4=Received partially
106  // -> 7=Canceled/Never received -> (reopen) 3=Process runing
107  // -> 6=Canceled -> (reopen) 2=Approved
108  // -> 9=Refused -> (reopen) 1=Validated
109  // Note: billed or not is on another field "billed"
110  public $statuts; // List of status
111 
112  public $billed;
113 
114  public $socid;
115  public $fourn_id;
116  public $date;
117  public $date_creation;
118  public $date_valid;
119  public $date_approve;
120  public $date_approve2; // Used when SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED is set
121  public $date_commande;
122 
127  public $date_livraison;
128 
132  public $delivery_date;
133 
134  public $total_ht;
135  public $total_tva;
136  public $total_localtax1; // Total Local tax 1
137  public $total_localtax2; // Total Local tax 2
138  public $total_ttc;
139  public $source;
140 
144  public $fk_project;
145 
146  public $cond_reglement_id;
147  public $cond_reglement_code;
148  public $cond_reglement_label; // Label
149  public $cond_reglement_doc; // Label on documents
150 
154  public $fk_account;
155 
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; // Used when SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED is set
162 
163  public $refuse_note;
164 
165  public $extraparams = array();
166 
170  public $lines = array();
171 
172  //Add for supplier_proposal
173  public $origin;
174  public $origin_id;
175  public $linked_objects = array();
176 
177  // Multicurrency
181  public $fk_multicurrency;
182 
183  public $multicurrency_code;
184  public $multicurrency_tx;
185  public $multicurrency_total_ht;
186  public $multicurrency_total_tva;
187  public $multicurrency_total_ttc;
188 
189 
217  public $fields = array(
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),
263  );
264 
265 
269  const STATUS_DRAFT = 0;
270 
274  const STATUS_VALIDATED = 1;
275 
279  const STATUS_ACCEPTED = 2;
280 
284  const STATUS_ORDERSENT = 3;
285 
290 
295 
299  const STATUS_CANCELED = 6;
300 
305 
309  const STATUS_REFUSED = 9;
310 
311 
316 
317 
318 
324  public function __construct($db)
325  {
326  $this->db = $db;
327  }
328 
329 
337  public function fetch($id, $ref = '')
338  {
339  global $conf;
340 
341  // Check parameters
342  if (empty($id) && empty($ref)) {
343  return -1;
344  }
345 
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';
364 
365  if (empty($id)) {
366  $sql .= " WHERE c.entity IN (".getEntity('supplier_order').")";
367  } else {
368  $sql .= " WHERE c.rowid=".((int) $id);
369  }
370 
371  if ($ref) {
372  $sql .= " AND c.ref='".$this->db->escape($ref)."'";
373  }
374 
375  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
376  $resql = $this->db->query($sql);
377  if ($resql) {
378  $obj = $this->db->fetch_object($resql);
379  if (!$obj) {
380  $this->error = 'Bill with id '.$id.' not found';
381  dol_syslog(get_class($this).'::fetch '.$this->error);
382  return 0;
383  }
384 
385  $this->id = $obj->rowid;
386  $this->entity = $obj->entity;
387 
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); // date we make the order to supplier
409  if (isset($obj->date_commande)) {
410  $this->date = $this->date_commande;
411  } else {
412  $this->date = $this->date_creation;
413  }
414  $this->date_livraison = $this->db->jdate($obj->delivery_date); // deprecated
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;
419 
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; // deprecated
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; // deprecated
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; // deprecated
436 
437  //Incoterms
438  $this->fk_incoterms = $obj->fk_incoterms;
439  $this->location_incoterms = $obj->location_incoterms;
440  $this->label_incoterms = $obj->label_incoterms;
441 
442  // Multicurrency
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;
449 
450  $this->extraparams = (array) json_decode($obj->extraparams, true);
451 
452  $this->db->free($resql);
453 
454  // Retrieve all extrafield
455  // fetch optionals attributes and labels
456  $this->fetch_optionals();
457 
458  if ($this->statut == 0) {
459  $this->brouillon = 1;
460  }
461 
462  /*
463  * Lines
464  */
465  $result = $this->fetch_lines();
466 
467  if ($result < 0) {
468  return -1;
469  } else {
470  return 1;
471  }
472  } else {
473  $this->error = $this->db->error()." sql=".$sql;
474  return -1;
475  }
476  }
477 
478  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
485  public function fetch_lines($only_product = 0)
486  {
487  global $conf;
488  // phpcs:enable
489 
490  $this->lines = array();
491 
492  $sql = "SELECT l.rowid, l.fk_commande, 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);
503  if ($only_product) {
504  $sql .= ' AND p.fk_product_type = 0';
505  }
506  $sql .= " ORDER BY l.rang, l.rowid";
507  //print $sql;
508 
509  dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
510 
511  $result = $this->db->query($sql);
512  if ($result) {
513  $num = $this->db->num_rows($result);
514  $i = 0;
515 
516  while ($i < $num) {
517  $objp = $this->db->fetch_object($result);
518 
519  $line = new CommandeFournisseurLigne($this->db);
520 
521  $line->id = $objp->rowid;
522  $line->fk_commande = $objp->fk_commande;
523  $line->desc = $objp->description;
524  $line->description = $objp->description;
525  $line->qty = $objp->qty;
526  $line->tva_tx = $objp->tva_tx;
527  $line->localtax1_tx = $objp->localtax1_tx;
528  $line->localtax2_tx = $objp->localtax2_tx;
529  $line->localtax1_type = $objp->localtax1_type;
530  $line->localtax2_type = $objp->localtax2_type;
531  $line->subprice = $objp->subprice;
532  $line->pu_ht = $objp->subprice;
533  $line->remise_percent = $objp->remise_percent;
534 
535  $line->vat_src_code = $objp->vat_src_code;
536  $line->total_ht = $objp->total_ht;
537  $line->total_tva = $objp->total_tva;
538  $line->total_localtax1 = $objp->total_localtax1;
539  $line->total_localtax2 = $objp->total_localtax2;
540  $line->total_ttc = $objp->total_ttc;
541  $line->product_type = $objp->product_type;
542 
543  $line->fk_product = $objp->fk_product;
544 
545  $line->libelle = $objp->product_label; // deprecated
546  $line->product_label = $objp->product_label;
547  $line->product_desc = $objp->product_desc;
548  $line->product_tobatch = $objp->product_tobatch;
549  $line->product_barcode = $objp->product_barcode;
550 
551  $line->ref = $objp->product_ref; // Ref of product
552  $line->product_ref = $objp->product_ref; // Ref of product
553  $line->ref_fourn = $objp->ref_supplier; // The supplier ref of price when product was added. May have change since
554  $line->ref_supplier = $objp->ref_supplier; // The supplier ref of price when product was added. May have change since
555 
556  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
557  // TODO We should not fetch this properties into the fetch_lines. This is NOT properties of a line.
558  // Move this into another method and call it when required.
559 
560  // Take better packaging for $objp->qty (first supplier ref quantity <= $objp->qty)
561  $sqlsearchpackage = 'SELECT rowid, packaging FROM '.MAIN_DB_PREFIX."product_fournisseur_price";
562  $sqlsearchpackage .= ' WHERE entity IN ('.getEntity('product_fournisseur_price').")";
563  $sqlsearchpackage .= " AND fk_product = ".((int) $objp->fk_product);
564  $sqlsearchpackage .= " AND ref_fourn = '".$this->db->escape($objp->ref_supplier)."'";
565  $sqlsearchpackage .= " AND quantity <= ".((float) $objp->qty); // required to be qualified
566  $sqlsearchpackage .= " AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).")"; // required to be qualified
567  $sqlsearchpackage .= " AND fk_soc = ".((int) $this->socid);
568  $sqlsearchpackage .= " ORDER BY packaging ASC"; // Take the smaller package first
569  $sqlsearchpackage .= " LIMIT 1";
570 
571  $resqlsearchpackage = $this->db->query($sqlsearchpackage);
572  if ($resqlsearchpackage) {
573  $objsearchpackage = $this->db->fetch_object($resqlsearchpackage);
574  if ($objsearchpackage) {
575  $line->fk_fournprice = $objsearchpackage->rowid;
576  $line->packaging = $objsearchpackage->packaging;
577  }
578  } else {
579  $this->error = $this->db->lasterror();
580  return -1;
581  }
582  }
583 
584  $line->date_start = $this->db->jdate($objp->date_start);
585  $line->date_end = $this->db->jdate($objp->date_end);
586  $line->fk_unit = $objp->fk_unit;
587 
588  // Multicurrency
589  $line->fk_multicurrency = $objp->fk_multicurrency;
590  $line->multicurrency_code = $objp->multicurrency_code;
591  $line->multicurrency_subprice = $objp->multicurrency_subprice;
592  $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
593  $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
594  $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
595 
596  $line->special_code = $objp->special_code;
597  $line->fk_parent_line = $objp->fk_parent_line;
598 
599  $line->rang = $objp->rang;
600 
601  // Retrieve all extrafield
602  // fetch optionals attributes and labels
603  $line->fetch_optionals();
604 
605  $this->lines[$i] = $line;
606 
607  $i++;
608  }
609  $this->db->free($result);
610 
611  return $num;
612  } else {
613  $this->error = $this->db->error()." sql=".$sql;
614  return -1;
615  }
616  }
617 
626  public function valid($user, $idwarehouse = 0, $notrigger = 0)
627  {
628  global $langs, $conf;
629  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
630 
631  $error = 0;
632 
633  dol_syslog(get_class($this)."::valid");
634  $result = 0;
635  if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && (!empty($user->rights->fournisseur->commande->creer) || !empty($user->rights->supplier_order->creer)))
636  || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->fournisseur->supplier_order_advance->validate))) {
637  $this->db->begin();
638 
639  // Definition of supplier order numbering model name
640  $soc = new Societe($this->db);
641  $soc->fetch($this->fourn_id);
642 
643  // Check if object has a temporary ref
644  if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) { // empty should not happened, but when it occurs, the test save life
645  $num = $this->getNextNumRef($soc);
646  } else {
647  $num = $this->ref;
648  }
649  $this->newref = dol_sanitizeFileName($num);
650 
651  $sql = 'UPDATE '.MAIN_DB_PREFIX."commande_fournisseur";
652  $sql .= " SET ref='".$this->db->escape($num)."',";
653  $sql .= " fk_statut = ".self::STATUS_VALIDATED.",";
654  $sql .= " date_valid='".$this->db->idate(dol_now())."',";
655  $sql .= " fk_user_valid = ".((int) $user->id);
656  $sql .= " WHERE rowid = ".((int) $this->id);
657  $sql .= " AND fk_statut = ".self::STATUS_DRAFT;
658 
659  $resql = $this->db->query($sql);
660  if (!$resql) {
661  dol_print_error($this->db);
662  $error++;
663  }
664 
665  if (!$error && !$notrigger) {
666  // Call trigger
667  $result = $this->call_trigger('ORDER_SUPPLIER_VALIDATE', $user);
668  if ($result < 0) {
669  $error++;
670  }
671  // End call triggers
672  }
673 
674  if (!$error) {
675  $this->oldref = $this->ref;
676 
677  // Rename directory if dir was a temporary ref
678  if (preg_match('/^[\(]?PROV/i', $this->ref)) {
679  // Now we rename also files into index
680  $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)."'";
681  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'fournisseur/commande/".$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
682  $resql = $this->db->query($sql);
683  if (!$resql) {
684  $error++; $this->error = $this->db->lasterror();
685  }
686 
687  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
688  $oldref = dol_sanitizeFileName($this->ref);
689  $newref = dol_sanitizeFileName($num);
690  $dirsource = $conf->fournisseur->commande->dir_output.'/'.$oldref;
691  $dirdest = $conf->fournisseur->commande->dir_output.'/'.$newref;
692  if (!$error && file_exists($dirsource)) {
693  dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
694 
695  if (@rename($dirsource, $dirdest)) {
696  dol_syslog("Rename ok");
697  // Rename docs starting with $oldref with $newref
698  $listoffiles = dol_dir_list($conf->fournisseur->commande->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
699  foreach ($listoffiles as $fileentry) {
700  $dirsource = $fileentry['name'];
701  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
702  $dirsource = $fileentry['path'].'/'.$dirsource;
703  $dirdest = $fileentry['path'].'/'.$dirdest;
704  @rename($dirsource, $dirdest);
705  }
706  }
707  }
708  }
709  }
710 
711  if (!$error) {
712  $result = 1;
713  $this->statut = self::STATUS_VALIDATED;
714  $this->ref = $num;
715  }
716 
717  if (!$error) {
718  $this->db->commit();
719  return 1;
720  } else {
721  $this->db->rollback();
722  return -1;
723  }
724  } else {
725  $this->error = 'NotAuthorized';
726  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
727  return -1;
728  }
729  }
730 
737  public function getLibStatut($mode = 0)
738  {
739  return $this->LibStatut($this->statut, $mode, $this->billed);
740  }
741 
742  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
751  public function LibStatut($status, $mode = 0, $billed = 0)
752  {
753  // phpcs:enable
754  global $conf, $langs, $hookmanager;
755 
756  if (empty($this->statuts) || empty($this->statuts_short)) {
757  $langs->load('orders');
758 
759  $this->statuts[0] = 'StatusSupplierOrderDraft';
760  $this->statuts[1] = 'StatusSupplierOrderValidated';
761  $this->statuts[2] = 'StatusSupplierOrderApproved';
762  if (empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
763  $this->statuts[3] = 'StatusSupplierOrderOnProcess';
764  } else {
765  $this->statuts[3] = 'StatusSupplierOrderOnProcessWithValidation';
766  }
767  $this->statuts[4] = 'StatusSupplierOrderReceivedPartially';
768  $this->statuts[5] = 'StatusSupplierOrderReceivedAll';
769  $this->statuts[6] = 'StatusSupplierOrderCanceled'; // Approved->Canceled
770  $this->statuts[7] = 'StatusSupplierOrderCanceled'; // Process running->canceled
771  $this->statuts[9] = 'StatusSupplierOrderRefused';
772 
773  // List of language codes for status
774  $this->statuts_short[0] = 'StatusSupplierOrderDraftShort';
775  $this->statuts_short[1] = 'StatusSupplierOrderValidatedShort';
776  $this->statuts_short[2] = 'StatusSupplierOrderApprovedShort';
777  $this->statuts_short[3] = 'StatusSupplierOrderOnProcessShort';
778  $this->statuts_short[4] = 'StatusSupplierOrderReceivedPartiallyShort';
779  $this->statuts_short[5] = 'StatusSupplierOrderReceivedAllShort';
780  $this->statuts_short[6] = 'StatusSupplierOrderCanceledShort';
781  $this->statuts_short[7] = 'StatusSupplierOrderCanceledShort';
782  $this->statuts_short[9] = 'StatusSupplierOrderRefusedShort';
783  }
784 
785  $statustrans = array(
786  0 => 'status0',
787  1 => 'status1b',
788  2 => 'status1',
789  3 => 'status4',
790  4 => 'status4b',
791  5 => 'status6',
792  6 => 'status9',
793  7 => 'status9',
794  9 => 'status9',
795  );
796 
797  $statusClass = 'status0';
798  if (!empty($statustrans[$status])) {
799  $statusClass = $statustrans[$status];
800  }
801 
802  $billedtext = '';
803  if ($billed) {
804  $billedtext = ' - '.$langs->trans("Billed");
805  }
806  if ($status == 5 && $billed) {
807  $statusClass = 'status6';
808  }
809 
810  $statusLong = $langs->transnoentitiesnoconv($this->statuts[$status]).$billedtext;
811  $statusShort = $langs->transnoentitiesnoconv($this->statuts_short[$status]);
812 
813  $parameters = array('status' => $status, 'mode' => $mode, 'billed' => $billed);
814  $reshook = $hookmanager->executeHooks('LibStatut', $parameters, $this); // Note that $action and $object may have been modified by hook
815  if ($reshook > 0) {
816  return $hookmanager->resPrint;
817  }
818 
819  return dolGetStatus($statusLong, $statusShort, '', $statusClass, $mode);
820  }
821 
822 
833  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
834  {
835  global $langs, $conf, $user, $hookmanager;
836 
837  $result = '';
838 
839  $label = '';
840 
841  if ($user->hasRight("fournisseur", "commande", "read")) {
842  $label = '<u class="paddingrightonly">'.$langs->trans("SupplierOrder").'</u>';
843  if (isset($this->statut)) {
844  $label .= ' '.$this->getLibStatut(5);
845  }
846  if (!empty($this->ref)) {
847  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
848  }
849  if (!empty($this->ref_supplier)) {
850  $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
851  }
852  if (!empty($this->total_ht)) {
853  $label .= '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
854  }
855  if (!empty($this->total_tva)) {
856  $label .= '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
857  }
858  if (!empty($this->total_ttc)) {
859  $label .= '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
860  }
861  if (!empty($this->date)) {
862  $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
863  }
864  if (!empty($this->delivery_date)) {
865  $label .= '<br><b>'.$langs->trans('DeliveryDate').':</b> '.dol_print_date($this->delivery_date, 'dayhour');
866  }
867  }
868 
869  $picto = 'order';
870  $url = DOL_URL_ROOT.'/fourn/commande/card.php?id='.$this->id;
871 
872  if ($option !== 'nolink') {
873  // Add param to save lastsearch_values or not
874  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
875  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
876  $add_save_lastsearch_values = 1;
877  }
878  if ($add_save_lastsearch_values) {
879  $url .= '&save_lastsearch_values=1';
880  }
881  }
882 
883  $linkclose = '';
884  if (empty($notooltip)) {
885  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
886  $label = $langs->trans("ShowOrder");
887  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
888  }
889  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
890  $linkclose .= ' class="classfortooltip"';
891  }
892 
893  $linkstart = '<a href="'.$url.'"';
894  $linkstart .= $linkclose.'>';
895  $linkend = '</a>';
896 
897  $result .= $linkstart;
898  if ($withpicto) {
899  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
900  }
901  if ($withpicto != 2) {
902  $result .= $this->ref;
903  }
904  $result .= $linkend;
905 
906  if ($addlinktonotes) {
907  $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
908  if ($txttoshow) {
909  $notetoshow = $langs->trans("ViewPrivateNote").':<br>'.dol_string_nohtmltag($txttoshow, 1);
910  $result .= ' <span class="note inline-block">';
911  $result .= '<a href="'.DOL_URL_ROOT.'/fourn/commande/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($notetoshow).'">';
912  $result .= img_picto('', 'note');
913  $result .= '</a>';
914  //$result.=img_picto($langs->trans("ViewNote"),'object_generic');
915  //$result.='</a>';
916  $result .= '</span>';
917  }
918  }
919 
920  global $action;
921  $hookmanager->initHooks(array($this->element . 'dao'));
922  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
923  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
924  if ($reshook > 0) {
925  $result = $hookmanager->resPrint;
926  } else {
927  $result .= $hookmanager->resPrint;
928  }
929  return $result;
930  }
931 
932 
940  public function getNextNumRef($soc)
941  {
942  global $db, $langs, $conf;
943  $langs->load("orders");
944 
945  if (!empty($conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER)) {
946  $mybool = false;
947 
948  $file = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER.'.php';
949  $classname = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER;
950 
951  // Include file with class
952  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
953 
954  foreach ($dirmodels as $reldir) {
955  $dir = dol_buildpath($reldir."core/modules/supplier_order/");
956 
957  // Load file with numbering class (if found)
958  $mybool |= @include_once $dir.$file;
959  }
960 
961  if ($mybool === false) {
962  dol_print_error('', "Failed to include file ".$file);
963  return '';
964  }
965 
966  $obj = new $classname();
967  $numref = $obj->getNextValue($soc, $this);
968 
969  if ($numref != "") {
970  return $numref;
971  } else {
972  $this->error = $obj->error;
973  return -1;
974  }
975  } else {
976  $this->error = "Error_COMMANDE_SUPPLIER_ADDON_NotDefined";
977  return -2;
978  }
979  }
986  public function classifyBilled(User $user)
987  {
988  $error = 0;
989 
990  if ($this->billed) {
991  return 0;
992  }
993 
994  $this->db->begin();
995 
996  $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande_fournisseur SET billed = 1';
997  $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut > '.self::STATUS_DRAFT;
998 
999  if ($this->db->query($sql)) {
1000  if (!$error) {
1001  // Call trigger
1002  $result = $this->call_trigger('ORDER_SUPPLIER_CLASSIFY_BILLED', $user);
1003  if ($result < 0) {
1004  $error++;
1005  }
1006  // End call triggers
1007  }
1008 
1009  if (!$error) {
1010  $this->billed = 1;
1011 
1012  $this->db->commit();
1013  return 1;
1014  } else {
1015  $this->db->rollback();
1016  return -1;
1017  }
1018  } else {
1019  dol_print_error($this->db);
1020 
1021  $this->db->rollback();
1022  return -1;
1023  }
1024  }
1025 
1034  public function approve($user, $idwarehouse = 0, $secondlevel = 0)
1035  {
1036  global $langs, $conf;
1037  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1038 
1039  $error = 0;
1040 
1041  dol_syslog(get_class($this)."::approve");
1042 
1043  if ($user->rights->fournisseur->commande->approuver) {
1044  $now = dol_now();
1045 
1046  $this->db->begin();
1047 
1048  // Definition of order numbering model name
1049  $soc = new Societe($this->db);
1050  $soc->fetch($this->fourn_id);
1051 
1052  // Check if object has a temporary ref
1053  if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) { // empty should not happened, but when it occurs, the test save life
1054  $num = $this->getNextNumRef($soc);
1055  } else {
1056  $num = $this->ref;
1057  }
1058  $this->newref = dol_sanitizeFileName($num);
1059 
1060  // Do we have to change status now ? (If double approval is required and first approval, we keep status to 1 = validated)
1061  $movetoapprovestatus = true;
1062  $comment = '';
1063 
1064  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
1065  $sql .= " SET ref='".$this->db->escape($num)."',";
1066  if (empty($secondlevel)) { // standard or first level approval
1067  $sql .= " date_approve='".$this->db->idate($now)."',";
1068  $sql .= " fk_user_approve = ".$user->id;
1069  if (!empty($conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED) && $this->total_ht >= $conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED) {
1070  if (empty($this->user_approve_id2)) {
1071  $movetoapprovestatus = false; // second level approval not done
1072  $comment = ' (first level)';
1073  }
1074  }
1075  } else // request a second level approval
1076  {
1077  $sql .= " date_approve2='".$this->db->idate($now)."',";
1078  $sql .= " fk_user_approve2 = ".((int) $user->id);
1079  if (empty($this->user_approve_id)) {
1080  $movetoapprovestatus = false; // first level approval not done
1081  }
1082  $comment = ' (second level)';
1083  }
1084  // If double approval is required and first approval, we keep status to 1 = validated
1085  if ($movetoapprovestatus) {
1086  $sql .= ", fk_statut = ".self::STATUS_ACCEPTED;
1087  } else {
1088  $sql .= ", fk_statut = ".self::STATUS_VALIDATED;
1089  }
1090  $sql .= " WHERE rowid = ".((int) $this->id);
1091  $sql .= " AND fk_statut = ".self::STATUS_VALIDATED;
1092 
1093  if ($this->db->query($sql)) {
1094  if (!empty($conf->global->SUPPLIER_ORDER_AUTOADD_USER_CONTACT)) {
1095  $result = $this->add_contact($user->id, 'SALESREPFOLL', 'internal', 1);
1096  if ($result < 0 && $result != -2) { // -2 means already exists
1097  $error++;
1098  }
1099  }
1100 
1101  // If stock is incremented on validate order, we must increment it
1102  if (!$error && $movetoapprovestatus && isModEnabled('stock') && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER)) {
1103  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
1104  $langs->load("agenda");
1105 
1106  $cpt = count($this->lines);
1107  for ($i = 0; $i < $cpt; $i++) {
1108  // Product with reference
1109  if ($this->lines[$i]->fk_product > 0) {
1110  $this->line = $this->lines[$i];
1111  $mouvP = new MouvementStock($this->db);
1112  $mouvP->origin = &$this;
1113  $mouvP->setOrigin($this->element, $this->id);
1114  // We decrement stock of product (and sub-products)
1115  $up_ht_disc = $this->lines[$i]->subprice;
1116  if (!empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) {
1117  $up_ht_disc = price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU');
1118  }
1119  $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("OrderApprovedInDolibarr", $this->ref));
1120  if ($result < 0) {
1121  $error++;
1122  }
1123  unset($this->line);
1124  }
1125  }
1126  }
1127 
1128  if (!$error) {
1129  // Call trigger
1130  $result = $this->call_trigger('ORDER_SUPPLIER_APPROVE', $user);
1131  if ($result < 0) {
1132  $error++;
1133  }
1134  // End call triggers
1135  }
1136 
1137  if (!$error) {
1138  $this->ref = $this->newref;
1139 
1140  if ($movetoapprovestatus) {
1141  $this->statut = self::STATUS_ACCEPTED;
1142  } else {
1143  $this->statut = self::STATUS_VALIDATED;
1144  }
1145  if (empty($secondlevel)) { // standard or first level approval
1146  $this->date_approve = $now;
1147  $this->user_approve_id = $user->id;
1148  } else // request a second level approval
1149  {
1150  $this->date_approve2 = $now;
1151  $this->user_approve_id2 = $user->id;
1152  }
1153 
1154  $this->db->commit();
1155  return 1;
1156  } else {
1157  $this->db->rollback();
1158  return -1;
1159  }
1160  } else {
1161  $this->db->rollback();
1162  $this->error = $this->db->lasterror();
1163  return -1;
1164  }
1165  } else {
1166  dol_syslog(get_class($this)."::approve Not Authorized", LOG_ERR);
1167  }
1168  return -1;
1169  }
1170 
1177  public function refuse($user)
1178  {
1179  global $conf, $langs;
1180 
1181  $error = 0;
1182 
1183  dol_syslog(get_class($this)."::refuse");
1184  $result = 0;
1185  if ($user->rights->fournisseur->commande->approuver) {
1186  $this->db->begin();
1187 
1188  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET fk_statut = ".self::STATUS_REFUSED;
1189  $sql .= " WHERE rowid = ".((int) $this->id);
1190 
1191  if ($this->db->query($sql)) {
1192  $result = 0;
1193 
1194  if ($error == 0) {
1195  // Call trigger
1196  $result = $this->call_trigger('ORDER_SUPPLIER_REFUSE', $user);
1197  if ($result < 0) {
1198  $error++;
1199  $this->db->rollback();
1200  } else {
1201  $this->db->commit();
1202  }
1203  // End call triggers
1204  }
1205  } else {
1206  $this->db->rollback();
1207  $this->error = $this->db->lasterror();
1208  dol_syslog(get_class($this)."::refuse Error -1");
1209  $result = -1;
1210  }
1211  } else {
1212  dol_syslog(get_class($this)."::refuse Not Authorized");
1213  }
1214  return $result;
1215  }
1216 
1217  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1226  public function Cancel($user, $idwarehouse = -1)
1227  {
1228  // phpcs:enable
1229  global $langs, $conf;
1230 
1231  $error = 0;
1232 
1233  //dol_syslog("CommandeFournisseur::Cancel");
1234  $result = 0;
1235  if ($user->rights->fournisseur->commande->commander) {
1236  $statut = self::STATUS_CANCELED;
1237 
1238  $this->db->begin();
1239 
1240  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET fk_statut = ".((int) $statut);
1241  $sql .= " WHERE rowid = ".((int) $this->id);
1242  dol_syslog(get_class($this)."::cancel", LOG_DEBUG);
1243  if ($this->db->query($sql)) {
1244  $result = 0;
1245 
1246  // Call trigger
1247  $result = $this->call_trigger('ORDER_SUPPLIER_CANCEL', $user);
1248  if ($result < 0) {
1249  $error++;
1250  }
1251  // End call triggers
1252 
1253  if ($error == 0) {
1254  $this->db->commit();
1255  return 1;
1256  } else {
1257  $this->db->rollback();
1258  return -1;
1259  }
1260  } else {
1261  $this->db->rollback();
1262  $this->error = $this->db->lasterror();
1263  dol_syslog(get_class($this)."::cancel ".$this->error);
1264  return -1;
1265  }
1266  } else {
1267  dol_syslog(get_class($this)."::cancel Not Authorized");
1268  return -1;
1269  }
1270  }
1271 
1281  public function commande($user, $date, $methode, $comment = '')
1282  {
1283  global $langs;
1284  dol_syslog(get_class($this)."::commande");
1285  $error = 0;
1286  if ($user->rights->fournisseur->commande->commander) {
1287  $this->db->begin();
1288 
1289  $newnoteprivate = $this->note_private;
1290  if ($comment) {
1291  $newnoteprivate = dol_concatdesc($newnoteprivate, $langs->trans("Comment").': '.$comment);
1292  }
1293 
1294  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
1295  $sql .= " SET fk_statut=".self::STATUS_ORDERSENT.", fk_input_method=".$methode.", date_commande='".$this->db->idate($date)."', ";
1296  $sql .= " note_private='".$this->db->escape($newnoteprivate)."'";
1297  $sql .= " WHERE rowid=".((int) $this->id);
1298 
1299  dol_syslog(get_class($this)."::commande", LOG_DEBUG);
1300  if ($this->db->query($sql)) {
1301  $this->statut = self::STATUS_ORDERSENT;
1302  $this->methode_commande_id = $methode;
1303  $this->date_commande = $date;
1304  $this->context = array('comments' => $comment);
1305 
1306  // Call trigger
1307  $result = $this->call_trigger('ORDER_SUPPLIER_SUBMIT', $user);
1308  if ($result < 0) {
1309  $error++;
1310  }
1311  // End call triggers
1312  } else {
1313  $error++;
1314  $this->error = $this->db->lasterror();
1315  $this->errors[] = $this->db->lasterror();
1316  }
1317 
1318  if (!$error) {
1319  $this->db->commit();
1320  } else {
1321  $this->db->rollback();
1322  }
1323  } else {
1324  $error++;
1325  $this->error = $langs->trans('NotAuthorized');
1326  $this->errors[] = $langs->trans('NotAuthorized');
1327  dol_syslog(get_class($this)."::commande User not Authorized", LOG_WARNING);
1328  }
1329 
1330  return ($error ? -1 : 1);
1331  }
1332 
1340  public function create($user, $notrigger = 0)
1341  {
1342  global $langs, $conf, $hookmanager;
1343 
1344  $this->db->begin();
1345 
1346  $error = 0;
1347  $now = dol_now();
1348 
1349  // set tmp vars
1350  $date = ($this->date_commande ? $this->date_commande : $this->date); // in case of date is set
1351  if (empty($date)) {
1352  $date = $now;
1353  }
1354  $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
1355 
1356  // Clean parameters
1357  if (empty($this->source)) {
1358  $this->source = 0;
1359  }
1360 
1361  // Multicurrency (test on $this->multicurrency_tx because we should take the default rate only if not using origin rate)
1362  if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1363  list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code, $date);
1364  } else {
1365  $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
1366  }
1367  if (empty($this->fk_multicurrency)) {
1368  $this->multicurrency_code = $conf->currency;
1369  $this->fk_multicurrency = 0;
1370  $this->multicurrency_tx = 1;
1371  }
1372 
1373  // We set order into draft status
1374  $this->brouillon = 1;
1375 
1376  $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur (";
1377  $sql .= "ref";
1378  $sql .= ", ref_supplier";
1379  $sql .= ", note_private";
1380  $sql .= ", note_public";
1381  $sql .= ", entity";
1382  $sql .= ", fk_soc";
1383  $sql .= ", fk_projet";
1384  $sql .= ", date_creation";
1385  $sql .= ", date_livraison";
1386  $sql .= ", fk_user_author";
1387  $sql .= ", fk_statut";
1388  $sql .= ", source";
1389  $sql .= ", model_pdf";
1390  $sql .= ", fk_mode_reglement";
1391  $sql .= ", fk_cond_reglement";
1392  $sql .= ", fk_account";
1393  $sql .= ", fk_incoterms, location_incoterms";
1394  $sql .= ", fk_multicurrency";
1395  $sql .= ", multicurrency_code";
1396  $sql .= ", multicurrency_tx";
1397  $sql .= ") ";
1398  $sql .= " VALUES (";
1399  $sql .= "'(PROV)'";
1400  $sql .= ", '".$this->db->escape($this->ref_supplier)."'";
1401  $sql .= ", '".$this->db->escape($this->note_private)."'";
1402  $sql .= ", '".$this->db->escape($this->note_public)."'";
1403  $sql .= ", ".setEntity($this);
1404  $sql .= ", ".((int) $this->socid);
1405  $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : "null");
1406  $sql .= ", '".$this->db->idate($date)."'";
1407  $sql .= ", ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : "null");
1408  $sql .= ", ".((int) $user->id);
1409  $sql .= ", ".self::STATUS_DRAFT;
1410  $sql .= ", ".((int) $this->source);
1411  $sql .= ", '".$this->db->escape($conf->global->COMMANDE_SUPPLIER_ADDON_PDF)."'";
1412  $sql .= ", ".($this->mode_reglement_id > 0 ? $this->mode_reglement_id : 'null');
1413  $sql .= ", ".($this->cond_reglement_id > 0 ? $this->cond_reglement_id : 'null');
1414  $sql .= ", ".($this->fk_account > 0 ? $this->fk_account : 'NULL');
1415  $sql .= ", ".(int) $this->fk_incoterms;
1416  $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
1417  $sql .= ", ".(int) $this->fk_multicurrency;
1418  $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
1419  $sql .= ", ".(double) $this->multicurrency_tx;
1420  $sql .= ")";
1421 
1422  dol_syslog(get_class($this)."::create", LOG_DEBUG);
1423  if ($this->db->query($sql)) {
1424  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."commande_fournisseur");
1425 
1426  if ($this->id) {
1427  $num = count($this->lines);
1428 
1429  // insert products details into database
1430  for ($i = 0; $i < $num; $i++) {
1431  $line = $this->lines[$i];
1432  if (!is_object($line)) {
1433  $line = (object) $line;
1434  }
1435 
1436 
1437  //$this->special_code = $line->special_code; // TODO : remove this in 9.0 and add special_code param to addline()
1438 
1439  // This include test on qty if option SUPPLIER_ORDER_WITH_NOPRICEDEFINED is not set
1440  $result = $this->addline(
1441  $line->desc,
1442  $line->subprice,
1443  $line->qty,
1444  $line->tva_tx,
1445  $line->localtax1_tx,
1446  $line->localtax2_tx,
1447  $line->fk_product,
1448  0,
1449  $line->ref_fourn, // $line->ref_fourn comes from field ref into table of lines. Value may ba a ref that does not exists anymore, so we first try with value of product
1450  $line->remise_percent,
1451  'HT',
1452  0,
1453  $line->product_type,
1454  $line->info_bits,
1455  false,
1456  $line->date_start,
1457  $line->date_end,
1458  $line->array_options,
1459  $line->fk_unit,
1460  $line->special_code
1461  );
1462  if ($result < 0) {
1463  dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING); // do not use dol_print_error here as it may be a functionnal error
1464  $this->db->rollback();
1465  return -1;
1466  }
1467  }
1468 
1469  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
1470  $sql .= " SET ref='(PROV".$this->id.")'";
1471  $sql .= " WHERE rowid=".((int) $this->id);
1472  dol_syslog(get_class($this)."::create", LOG_DEBUG);
1473  if ($this->db->query($sql)) {
1474  // Add link with price request and supplier order
1475  if ($this->id) {
1476  $this->ref = "(PROV".$this->id.")";
1477 
1478  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
1479  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
1480  }
1481 
1482  // Add object linked
1483  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1484  foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1485  if (is_array($tmp_origin_id)) { // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...))
1486  foreach ($tmp_origin_id as $origin_id) {
1487  $ret = $this->add_object_linked($origin, $origin_id);
1488  if (!$ret) {
1489  dol_print_error($this->db);
1490  $error++;
1491  }
1492  }
1493  } else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
1494  {
1495  $origin_id = $tmp_origin_id;
1496  $ret = $this->add_object_linked($origin, $origin_id);
1497  if (!$ret) {
1498  dol_print_error($this->db);
1499  $error++;
1500  }
1501  }
1502  }
1503  }
1504  }
1505 
1506  if (!$error) {
1507  $result = $this->insertExtraFields();
1508  if ($result < 0) {
1509  $error++;
1510  }
1511  }
1512 
1513  if (!$error && !$notrigger) {
1514  // Call trigger
1515  $result = $this->call_trigger('ORDER_SUPPLIER_CREATE', $user);
1516  if ($result < 0) {
1517  $this->db->rollback();
1518  return -1;
1519  }
1520  // End call triggers
1521  }
1522 
1523  $this->db->commit();
1524  return $this->id;
1525  } else {
1526  $this->error = $this->db->lasterror();
1527  $this->db->rollback();
1528  return -2;
1529  }
1530  }
1531  } else {
1532  $this->error = $this->db->lasterror();
1533  $this->db->rollback();
1534  return -1;
1535  }
1536  }
1537 
1545  public function update(User $user, $notrigger = 0)
1546  {
1547  global $conf;
1548 
1549  $error = 0;
1550 
1551  // Clean parameters
1552  if (isset($this->ref)) {
1553  $this->ref = trim($this->ref);
1554  }
1555  if (isset($this->ref_supplier)) {
1556  $this->ref_supplier = trim($this->ref_supplier);
1557  }
1558  if (isset($this->note_private)) {
1559  $this->note_private = trim($this->note_private);
1560  }
1561  if (isset($this->note_public)) {
1562  $this->note_public = trim($this->note_public);
1563  }
1564  if (isset($this->model_pdf)) {
1565  $this->model_pdf = trim($this->model_pdf);
1566  }
1567  if (isset($this->import_key)) {
1568  $this->import_key = trim($this->import_key);
1569  }
1570 
1571  // Update request
1572  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
1573 
1574  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
1575  $sql .= " ref_supplier=".(isset($this->ref_supplier) ? "'".$this->db->escape($this->ref_supplier)."'" : "null").",";
1576  $sql .= " ref_ext=".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").",";
1577  $sql .= " fk_soc=".(isset($this->socid) ? $this->socid : "null").",";
1578  $sql .= " date_commande=".(strval($this->date_commande) != '' ? "'".$this->db->idate($this->date_commande)."'" : 'null').",";
1579  $sql .= " date_valid=".(strval($this->date_validation) != '' ? "'".$this->db->idate($this->date_validation)."'" : 'null').",";
1580  $sql .= " total_tva=".(isset($this->total_tva) ? $this->total_tva : "null").",";
1581  $sql .= " localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 : "null").",";
1582  $sql .= " localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 : "null").",";
1583  $sql .= " total_ht=".(isset($this->total_ht) ? $this->total_ht : "null").",";
1584  $sql .= " total_ttc=".(isset($this->total_ttc) ? $this->total_ttc : "null").",";
1585  $sql .= " fk_statut=".(isset($this->statut) ? $this->statut : "null").",";
1586  $sql .= " fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id : "null").",";
1587  $sql .= " fk_user_valid=".(isset($this->user_valid) && $this->user_valid > 0 ? $this->user_valid : "null").",";
1588  $sql .= " fk_projet=".(isset($this->fk_project) ? $this->fk_project : "null").",";
1589  $sql .= " fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id : "null").",";
1590  $sql .= " fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id : "null").",";
1591  $sql .= " date_livraison=".(strval($this->delivery_date) != '' ? "'".$this->db->idate($this->delivery_date)."'" : 'null').",";
1592  //$sql .= " fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id : "null").",";
1593  $sql .= " fk_account=".($this->fk_account > 0 ? $this->fk_account : "null").",";
1594  //$sql .= " fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id : "null").",";
1595  $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
1596  $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
1597  $sql .= " model_pdf=".(isset($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null").",";
1598  $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null")."";
1599 
1600  $sql .= " WHERE rowid=".((int) $this->id);
1601 
1602  $this->db->begin();
1603 
1604  dol_syslog(get_class($this)."::update", LOG_DEBUG);
1605  $resql = $this->db->query($sql);
1606  if (!$resql) {
1607  $error++;
1608  $this->errors[] = "Error ".$this->db->lasterror();
1609  }
1610 
1611  if (!$error) {
1612  $result = $this->insertExtraFields();
1613  if ($result < 0) {
1614  $error++;
1615  }
1616  }
1617 
1618  if (!$error && !$notrigger) {
1619  // Call trigger
1620  $result = $this->call_trigger('ORDER_SUPPLIER_MODIFY', $user);
1621  if ($result < 0) {
1622  $error++;
1623  }
1624  // End call triggers
1625  }
1626 
1627  // Commit or rollback
1628  if ($error) {
1629  foreach ($this->errors as $errmsg) {
1630  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1631  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1632  }
1633  $this->db->rollback();
1634  return -1 * $error;
1635  } else {
1636  $this->db->commit();
1637  return 1;
1638  }
1639  }
1640 
1649  public function createFromClone(User $user, $socid = 0, $notrigger = 0)
1650  {
1651  global $conf, $user, $hookmanager;
1652 
1653  $error = 0;
1654 
1655  $this->db->begin();
1656 
1657  // get extrafields so they will be clone
1658  foreach ($this->lines as $line) {
1659  $line->fetch_optionals();
1660  }
1661 
1662  // Load source object
1663  $objFrom = clone $this;
1664 
1665  // Change socid if needed
1666  if (!empty($socid) && $socid != $this->socid) {
1667  $objsoc = new Societe($this->db);
1668 
1669  if ($objsoc->fetch($socid) > 0) {
1670  $this->socid = $objsoc->id;
1671  $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1672  $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1673  $this->fk_project = 0;
1674  $this->fk_delivery_address = 0;
1675  }
1676 
1677  // TODO Change product price if multi-prices
1678  }
1679 
1680  $this->id = 0;
1681  $this->statut = self::STATUS_DRAFT;
1682 
1683  // Clear fields
1684  $this->user_author_id = $user->id;
1685  $this->user_valid = 0;
1686  $this->date_creation = '';
1687  $this->date_validation = '';
1688  $this->ref_supplier = '';
1689  $this->user_approve_id = '';
1690  $this->user_approve_id2 = '';
1691  $this->date_approve = '';
1692  $this->date_approve2 = '';
1693 
1694  // Create clone
1695  $this->context['createfromclone'] = 'createfromclone';
1696  $result = $this->create($user, $notrigger);
1697  if ($result < 0) {
1698  $error++;
1699  }
1700 
1701  if (!$error) {
1702  // Hook of thirdparty module
1703  if (is_object($hookmanager)) {
1704  $parameters = array('objFrom'=>$objFrom);
1705  $action = '';
1706  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1707  if ($reshook < 0) {
1708  $this->errors += $hookmanager->errors;
1709  $this->error = $hookmanager->error;
1710  $error++;
1711  }
1712  }
1713  }
1714 
1715  unset($this->context['createfromclone']);
1716 
1717  // End
1718  if (!$error) {
1719  $this->db->commit();
1720  return $this->id;
1721  } else {
1722  $this->db->rollback();
1723  return -1;
1724  }
1725  }
1726 
1756  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)
1757  {
1758  global $langs, $mysoc, $conf;
1759 
1760  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");
1761  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1762 
1763  if ($this->statut == self::STATUS_DRAFT) {
1764  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1765 
1766  // Clean parameters
1767  if (empty($qty)) {
1768  $qty = 0;
1769  }
1770  if (!$info_bits) {
1771  $info_bits = 0;
1772  }
1773  if (empty($txtva)) {
1774  $txtva = 0;
1775  }
1776  if (empty($rang)) {
1777  $rang = 0;
1778  }
1779  if (empty($txlocaltax1)) {
1780  $txlocaltax1 = 0;
1781  }
1782  if (empty($txlocaltax2)) {
1783  $txlocaltax2 = 0;
1784  }
1785  if (empty($remise_percent)) {
1786  $remise_percent = 0;
1787  }
1788 
1789  $remise_percent = price2num($remise_percent);
1790  $qty = price2num($qty);
1791  $pu_ht = price2num($pu_ht);
1792  $pu_ht_devise = price2num($pu_ht_devise);
1793  $pu_ttc = price2num($pu_ttc);
1794  if (!preg_match('/\((.*)\)/', $txtva)) {
1795  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
1796  }
1797  $txlocaltax1 = price2num($txlocaltax1);
1798  $txlocaltax2 = price2num($txlocaltax2);
1799  if ($price_base_type == 'HT') {
1800  $pu = $pu_ht;
1801  } else {
1802  $pu = $pu_ttc;
1803  }
1804  $desc = trim($desc);
1805 
1806  // Check parameters
1807  if ($qty < 0 && !$fk_product) {
1808  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Product"));
1809  return -1;
1810  }
1811  if ($type < 0) {
1812  return -1;
1813  }
1814  if ($date_start && $date_end && $date_start > $date_end) {
1815  $langs->load("errors");
1816  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
1817  return -1;
1818  }
1819 
1820 
1821  $this->db->begin();
1822 
1823  $product_type = $type;
1824  $label = ''; // deprecated
1825 
1826  if ($fk_product > 0) {
1827  if (!empty($conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY)) { // Not the common case
1828  // Check quantity is enough
1829  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);
1830  $prod = new ProductFournisseur($this->db);
1831  if ($prod->fetch($fk_product) > 0) {
1832  $product_type = $prod->type;
1833  $label = $prod->label;
1834 
1835  // We use 'none' instead of $ref_supplier, because fourn_ref may not exists anymore. So we will take the first supplier price ok.
1836  // If we want a dedicated supplier price, we must provide $fk_prod_fourn_price.
1837  $result = $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product, 'none', (isset($this->fk_soc) ? $this->fk_soc : $this->socid)); // Search on couple $fk_prod_fourn_price/$qty first, then on triplet $qty/$fk_product/$ref_supplier/$this->fk_soc
1838 
1839  // If supplier order created from sales order, we take best supplier price
1840  // If $pu (defined previously from pu_ht or pu_ttc) is not defined at all, we also take the best supplier price
1841  if ($result > 0 && ($origin == 'commande' || $pu === '')) {
1842  $pu = $prod->fourn_pu; // Unit price supplier price set by get_buyprice
1843  $ref_supplier = $prod->ref_supplier; // Ref supplier price set by get_buyprice
1844  // is remise percent not keyed but present for the product we add it
1845  if ($remise_percent == 0 && $prod->remise_percent != 0) {
1846  $remise_percent = $prod->remise_percent;
1847  }
1848  }
1849  if ($result == 0) { // If result == 0, we failed to found the supplier reference price
1850  $langs->load("errors");
1851  $this->error = "Ref ".$prod->ref." ".$langs->trans("ErrorQtyTooLowForThisSupplier");
1852  $this->db->rollback();
1853  dol_syslog(get_class($this)."::addline we did not found supplier price, so we can't guess unit price");
1854  //$pu = $prod->fourn_pu; // We do not overwrite unit price
1855  //$ref = $prod->ref_fourn; // We do not overwrite ref supplier price
1856  return -1;
1857  }
1858  if ($result == -1) {
1859  $langs->load("errors");
1860  $this->error = "Ref ".$prod->ref." ".$langs->trans("ErrorQtyTooLowForThisSupplier");
1861  $this->db->rollback();
1862  dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_DEBUG);
1863  return -1;
1864  }
1865  if ($result < -1) {
1866  $this->error = $prod->error;
1867  $this->db->rollback();
1868  dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_ERR);
1869  return -1;
1870  }
1871  } else {
1872  $this->error = $prod->error;
1873  $this->db->rollback();
1874  return -1;
1875  }
1876  }
1877 
1878  // Predefine quantity according to packaging
1879  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
1880  $prod = new Product($this->db);
1881  $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product, 'none', ($this->fk_soc ? $this->fk_soc : $this->socid));
1882 
1883  if ($qty < $prod->packaging) {
1884  $qty = $prod->packaging;
1885  } else {
1886  if (!empty($prod->packaging) && ($qty % $prod->packaging) > 0) {
1887  $coeff = intval($qty / $prod->packaging) + 1;
1888  $qty = $prod->packaging * $coeff;
1889  setEventMessages($langs->trans('QtyRecalculatedWithPackaging'), null, 'mesgs');
1890  }
1891  }
1892  }
1893  }
1894 
1895  if (isModEnabled("multicurrency") && $pu_ht_devise > 0) {
1896  $pu = 0;
1897  }
1898 
1899  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $mysoc, $this->thirdparty);
1900 
1901  // Clean vat code
1902  $reg = array();
1903  $vat_src_code = '';
1904  if (preg_match('/\((.*)\)/', $txtva, $reg)) {
1905  $vat_src_code = $reg[1];
1906  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
1907  }
1908 
1909  // Calcul du total TTC et de la TVA pour la ligne a partir de
1910  // qty, pu, remise_percent et txtva
1911  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1912  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1913 
1914  $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);
1915 
1916  $total_ht = $tabprice[0];
1917  $total_tva = $tabprice[1];
1918  $total_ttc = $tabprice[2];
1919  $total_localtax1 = $tabprice[9];
1920  $total_localtax2 = $tabprice[10];
1921  $pu = $pu_ht = $tabprice[3];
1922 
1923  // MultiCurrency
1924  $multicurrency_total_ht = $tabprice[16];
1925  $multicurrency_total_tva = $tabprice[17];
1926  $multicurrency_total_ttc = $tabprice[18];
1927  $pu_ht_devise = $tabprice[19];
1928 
1929  $localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
1930  $localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
1931 
1932  if ($rang < 0) {
1933  $rangmax = $this->line_max();
1934  $rang = $rangmax + 1;
1935  }
1936 
1937  // Insert line
1938  $this->line = new CommandeFournisseurLigne($this->db);
1939 
1940  $this->line->context = $this->context;
1941 
1942  $this->line->fk_commande = $this->id;
1943  $this->line->label = $label;
1944  $this->line->ref_fourn = $ref_supplier;
1945  $this->line->ref_supplier = $ref_supplier;
1946  $this->line->desc = $desc;
1947  $this->line->qty = $qty;
1948  $this->line->tva_tx = $txtva;
1949  $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1950  $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1951  $this->line->localtax1_type = $localtax1_type;
1952  $this->line->localtax2_type = $localtax2_type;
1953  $this->line->fk_product = $fk_product;
1954  $this->line->product_type = $product_type;
1955  $this->line->remise_percent = $remise_percent;
1956  $this->line->subprice = $pu_ht;
1957  $this->line->rang = $rang;
1958  $this->line->info_bits = $info_bits;
1959 
1960  $this->line->vat_src_code = $vat_src_code;
1961  $this->line->total_ht = $total_ht;
1962  $this->line->total_tva = $total_tva;
1963  $this->line->total_localtax1 = $total_localtax1;
1964  $this->line->total_localtax2 = $total_localtax2;
1965  $this->line->total_ttc = $total_ttc;
1966  $this->line->product_type = $type;
1967  $this->line->special_code = (!empty($this->special_code) ? $this->special_code : 0);
1968  $this->line->origin = $origin;
1969  $this->line->origin_id = $origin_id;
1970  $this->line->fk_unit = $fk_unit;
1971 
1972  $this->line->date_start = $date_start;
1973  $this->line->date_end = $date_end;
1974 
1975  // Multicurrency
1976  $this->line->fk_multicurrency = $this->fk_multicurrency;
1977  $this->line->multicurrency_code = $this->multicurrency_code;
1978  $this->line->multicurrency_subprice = $pu_ht_devise;
1979  $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1980  $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1981  $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1982 
1983  $this->line->subprice = $pu_ht;
1984  $this->line->price = $this->line->subprice;
1985 
1986  $this->line->remise_percent = $remise_percent;
1987 
1988  if (is_array($array_options) && count($array_options) > 0) {
1989  $this->line->array_options = $array_options;
1990  }
1991 
1992  $result = $this->line->insert($notrigger);
1993  if ($result > 0) {
1994  // Reorder if child line
1995  if (!empty($fk_parent_line)) {
1996  $this->line_order(true, 'DESC');
1997  } elseif ($rang > 0 && $rang <= count($this->lines)) { // Update all rank of all other lines
1998  $linecount = count($this->lines);
1999  for ($ii = $rang; $ii <= $linecount; $ii++) {
2000  $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1);
2001  }
2002  }
2003 
2004  // Mise a jour informations denormalisees au niveau de la commande meme
2005  $result = $this->update_price(1, 'auto', 0, $this->thirdparty); // This method is designed to add line from user input so total calculation must be done using 'auto' mode.
2006  if ($result > 0) {
2007  $this->db->commit();
2008  return $this->line->id;
2009  } else {
2010  $this->db->rollback();
2011  return -1;
2012  }
2013  } else {
2014  $this->error = $this->line->error;
2015  $this->errors = $this->line->errors;
2016  dol_syslog(get_class($this)."::addline error=".$this->error, LOG_ERR);
2017  $this->db->rollback();
2018  return -1;
2019  }
2020  }
2021  }
2022 
2023 
2040  public function dispatchProduct($user, $product, $qty, $entrepot, $price = 0, $comment = '', $eatby = '', $sellby = '', $batch = '', $fk_commandefourndet = 0, $notrigger = 0)
2041  {
2042  global $conf, $langs;
2043 
2044  $error = 0;
2045  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
2046 
2047  // Check parameters (if test are wrong here, there is bug into caller)
2048  if ($entrepot <= 0) {
2049  $this->error = 'ErrorBadValueForParameterWarehouse';
2050  return -1;
2051  }
2052  if ($qty == 0) {
2053  $this->error = 'ErrorBadValueForParameterQty';
2054  return -1;
2055  }
2056 
2057  $dispatchstatus = 1;
2058  if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
2059  $dispatchstatus = 0; // Setting dispatch status (a validation step after receiving products) will be done manually to 1 or 2 if this option is on
2060  }
2061 
2062  $now = dol_now();
2063 
2064  $inventorycode = dol_print_date(dol_now(), 'dayhourlog');
2065 
2066  if (($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY || $this->statut == self::STATUS_RECEIVED_COMPLETELY)) {
2067  $this->db->begin();
2068 
2069  $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur_dispatch";
2070  $sql .= " (fk_commande, fk_product, qty, fk_entrepot, fk_user, datec, fk_commandefourndet, status, comment, eatby, sellby, batch) VALUES";
2071  $sql .= " ('".$this->id."','".$product."','".$qty."',".($entrepot > 0 ? "'".$entrepot."'" : "null").",'".$user->id."','".$this->db->idate($now)."','".$fk_commandefourndet."', ".$dispatchstatus.", '".$this->db->escape($comment)."', ";
2072  $sql .= ($eatby ? "'".$this->db->idate($eatby)."'" : "null").", ".($sellby ? "'".$this->db->idate($sellby)."'" : "null").", ".($batch ? "'".$this->db->escape($batch)."'" : "null");
2073  $sql .= ")";
2074 
2075  dol_syslog(get_class($this)."::dispatchProduct", LOG_DEBUG);
2076  $resql = $this->db->query($sql);
2077  if ($resql) {
2078  if (!$notrigger) {
2079  global $conf, $langs, $user;
2080  // Call trigger
2081  $result = $this->call_trigger('LINEORDER_SUPPLIER_DISPATCH', $user);
2082  if ($result < 0) {
2083  $error++;
2084  }
2085  // End call triggers
2086  }
2087  } else {
2088  $this->error = $this->db->lasterror();
2089  $error++;
2090  }
2091 
2092  // If module stock is enabled and the stock increase is done on purchase order dispatching
2093  if (!$error && $entrepot > 0 && isModEnabled('stock') && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)) {
2094  $mouv = new MouvementStock($this->db);
2095  if ($product > 0) {
2096  // $price should take into account discount (except if option STOCK_EXCLUDE_DISCOUNT_FOR_PMP is on)
2097  $mouv->origin = &$this;
2098  $mouv->setOrigin($this->element, $this->id);
2099 
2100  // Method change if qty < 0
2101  if (!empty($conf->global->SUPPLIER_ORDER_ALLOW_NEGATIVE_QTY_FOR_SUPPLIER_ORDER_RETURN) && $qty < 0) {
2102  $result = $mouv->livraison($user, $product, $entrepot, $qty*(-1), $price, $comment, $now, $eatby, $sellby, $batch, 0, $inventorycode);
2103  } else {
2104  $result = $mouv->reception($user, $product, $entrepot, $qty, $price, $comment, $eatby, $sellby, $batch, '', 0, $inventorycode);
2105  }
2106 
2107  if ($result < 0) {
2108  $this->error = $mouv->error;
2109  $this->errors = $mouv->errors;
2110  dol_syslog(get_class($this)."::dispatchProduct ".$this->error." ".join(',', $this->errors), LOG_ERR);
2111  $error++;
2112  }
2113  }
2114  }
2115 
2116  if ($error == 0) {
2117  $this->db->commit();
2118  return 1;
2119  } else {
2120  $this->db->rollback();
2121  return -1;
2122  }
2123  } else {
2124  $this->error = 'BadStatusForObject';
2125  return -2;
2126  }
2127  }
2128 
2136  public function deleteline($idline, $notrigger = 0)
2137  {
2138  if ($this->statut == 0) {
2139  $line = new CommandeFournisseurLigne($this->db);
2140 
2141  if ($line->fetch($idline) <= 0) {
2142  return 0;
2143  }
2144 
2145  if ($line->delete($notrigger) > 0) {
2146  $this->update_price(1);
2147  return 1;
2148  } else {
2149  $this->error = $line->error;
2150  $this->errors = $line->errors;
2151  return -1;
2152  }
2153  } else {
2154  return -2;
2155  }
2156  }
2157 
2165  public function delete(User $user, $notrigger = 0)
2166  {
2167  global $langs, $conf;
2168  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2169 
2170  $error = 0;
2171 
2172  $this->db->begin();
2173 
2174  if (empty($notrigger)) {
2175  // Call trigger
2176  $result = $this->call_trigger('ORDER_SUPPLIER_DELETE', $user);
2177  if ($result < 0) {
2178  $this->errors[] = 'ErrorWhenRunningTrigger';
2179  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
2180  $this->db->rollback();
2181  return -1;
2182  }
2183  // End call triggers
2184  }
2185 
2186  // Test we can delete
2187  $this->fetchObjectLinked(null, 'order_supplier');
2188  if (!empty($this->linkedObjects) && array_key_exists('reception', $this->linkedObjects)) {
2189  foreach ($this->linkedObjects['reception'] as $element) {
2190  if ($element->statut >= 0) {
2191  $this->errors[] = $langs->trans('ReceptionExist');
2192  $error++;
2193  break;
2194  }
2195  }
2196  }
2197 
2198  $main = MAIN_DB_PREFIX.'commande_fournisseurdet';
2199  $ef = $main."_extrafields";
2200  $sql = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_commande = ".((int) $this->id).")";
2201  dol_syslog(get_class($this)."::delete extrafields lines", LOG_DEBUG);
2202  if (!$this->db->query($sql)) {
2203  $this->error = $this->db->lasterror();
2204  $this->errors[] = $this->db->lasterror();
2205  $error++;
2206  }
2207 
2208  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet WHERE fk_commande =".((int) $this->id);
2209  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2210  if (!$this->db->query($sql)) {
2211  $this->error = $this->db->lasterror();
2212  $this->errors[] = $this->db->lasterror();
2213  $error++;
2214  }
2215 
2216  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur WHERE rowid =".((int) $this->id);
2217  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2218  if ($resql = $this->db->query($sql)) {
2219  if ($this->db->affected_rows($resql) < 1) {
2220  $this->error = $this->db->lasterror();
2221  $this->errors[] = $this->db->lasterror();
2222  $error++;
2223  }
2224  } else {
2225  $this->error = $this->db->lasterror();
2226  $this->errors[] = $this->db->lasterror();
2227  $error++;
2228  }
2229 
2230  // Remove extrafields
2231  if (!$error) {
2232  $result = $this->deleteExtraFields();
2233  if ($result < 0) {
2234  $this->error = 'FailToDeleteExtraFields';
2235  $this->errors[] = 'FailToDeleteExtraFields';
2236  $error++;
2237  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
2238  }
2239  }
2240 
2241  // Delete linked object
2242  $res = $this->deleteObjectLinked();
2243  if ($res < 0) {
2244  $this->error = 'FailToDeleteObjectLinked';
2245  $this->errors[] = 'FailToDeleteObjectLinked';
2246  $error++;
2247  }
2248 
2249  if (!$error) {
2250  // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
2251  $this->deleteEcmFiles();
2252 
2253  // We remove directory
2254  $ref = dol_sanitizeFileName($this->ref);
2255  if ($conf->fournisseur->commande->dir_output) {
2256  $dir = $conf->fournisseur->commande->dir_output."/".$ref;
2257  $file = $dir."/".$ref.".pdf";
2258  if (file_exists($file)) {
2259  if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
2260  $this->error = 'ErrorFailToDeleteFile';
2261  $this->errors[] = 'ErrorFailToDeleteFile';
2262  $error++;
2263  }
2264  }
2265  if (file_exists($dir)) {
2266  $res = @dol_delete_dir_recursive($dir);
2267  if (!$res) {
2268  $this->error = 'ErrorFailToDeleteDir';
2269  $this->errors[] = 'ErrorFailToDeleteDir';
2270  $error++;
2271  }
2272  }
2273  }
2274  }
2275 
2276  if (!$error) {
2277  dol_syslog(get_class($this)."::delete $this->id by $user->id", LOG_DEBUG);
2278  $this->db->commit();
2279  return 1;
2280  } else {
2281  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
2282  $this->db->rollback();
2283  return -$error;
2284  }
2285  }
2286 
2287  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2293  public function get_methodes_commande()
2294  {
2295  // phpcs:enable
2296  $sql = "SELECT rowid, libelle";
2297  $sql .= " FROM ".MAIN_DB_PREFIX."c_input_method";
2298  $sql .= " WHERE active = 1";
2299 
2300  $resql = $this->db->query($sql);
2301  if ($resql) {
2302  $i = 0;
2303  $num = $this->db->num_rows($resql);
2304  $this->methodes_commande = array();
2305  while ($i < $num) {
2306  $row = $this->db->fetch_row($resql);
2307 
2308  $this->methodes_commande[$row[0]] = $row[1];
2309 
2310  $i++;
2311  }
2312  return 0;
2313  } else {
2314  return -1;
2315  }
2316  }
2317 
2326  public function getDispachedLines($status = -1)
2327  {
2328  $ret = array();
2329 
2330  // List of already dispatched lines
2331  $sql = "SELECT p.ref, p.label,";
2332  $sql .= " e.rowid as warehouse_id, e.ref as entrepot,";
2333  $sql .= " cfd.rowid as dispatchedlineid, cfd.fk_product, cfd.qty, cfd.eatby, cfd.sellby, cfd.batch, cfd.comment, cfd.status";
2334  $sql .= " FROM ".MAIN_DB_PREFIX."product as p,";
2335  $sql .= " ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as cfd";
2336  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."entrepot as e ON cfd.fk_entrepot = e.rowid";
2337  $sql .= " WHERE cfd.fk_commande = ".((int) $this->id);
2338  $sql .= " AND cfd.fk_product = p.rowid";
2339  if ($status >= 0) {
2340  $sql .= " AND cfd.status = ".((int) $status);
2341  }
2342  $sql .= " ORDER BY cfd.rowid ASC";
2343 
2344  $resql = $this->db->query($sql);
2345  if ($resql) {
2346  $num = $this->db->num_rows($resql);
2347  $i = 0;
2348 
2349  while ($i < $num) {
2350  $objp = $this->db->fetch_object($resql);
2351  if ($objp) {
2352  $ret[] = array(
2353  'id' => $objp->dispatchedlineid,
2354  'productid' => $objp->fk_product,
2355  'warehouseid' => $objp->warehouse_id,
2356  'qty' => $objp->qty,
2357  );
2358  }
2359 
2360  $i++;
2361  }
2362  } else {
2363  dol_print_error($this->db, 'Failed to execute request to get dispatched lines');
2364  }
2365 
2366  return $ret;
2367  }
2368 
2369  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2379  public function Livraison($user, $date, $type, $comment)
2380  {
2381  // phpcs:enable
2382  global $conf, $langs;
2383 
2384  $result = 0;
2385  $error = 0;
2386 
2387  dol_syslog(get_class($this)."::Livraison");
2388 
2389  $usercanreceive = 0;
2390  if (!isModEnabled('reception')) {
2391  $usercanreceive = $user->rights->fournisseur->commande->receptionner;
2392  } else {
2393  $usercanreceive = $user->rights->reception->creer;
2394  }
2395 
2396  if ($usercanreceive) {
2397  // Define the new status
2398  if ($type == 'par') {
2400  } elseif ($type == 'tot') {
2402  } elseif ($type == 'nev') {
2404  } elseif ($type == 'can') {
2406  } else {
2407  $error++;
2408  dol_syslog(get_class($this)."::Livraison Error -2", LOG_ERR);
2409  return -2;
2410  }
2411 
2412  // Some checks to accept the record
2413  if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
2414  // If option SUPPLIER_ORDER_USE_DISPATCH_STATUS is on, we check all reception are approved to allow status "total/done"
2415  if (!$error && ($type == 'tot')) {
2416  $dispatchedlinearray = $this->getDispachedLines(0);
2417  if (count($dispatchedlinearray) > 0) {
2418  $result = -1;
2419  $error++;
2420  $this->errors[] = 'ErrorCantSetReceptionToTotalDoneWithReceptionToApprove';
2421  dol_syslog('ErrorCantSetReceptionToTotalDoneWithReceptionToApprove', LOG_DEBUG);
2422  }
2423  }
2424  if (!$error && !empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS_NEED_APPROVE) && ($type == 'tot')) { // Accept to move to reception done, only if status of all line are ok (refuse denied)
2425  $dispatcheddenied = $this->getDispachedLines(2);
2426  if (count($dispatchedlinearray) > 0) {
2427  $result = -1;
2428  $error++;
2429  $this->errors[] = 'ErrorCantSetReceptionToTotalDoneWithReceptionDenied';
2430  dol_syslog('ErrorCantSetReceptionToTotalDoneWithReceptionDenied', LOG_DEBUG);
2431  }
2432  }
2433  }
2434 
2435  // TODO LDR01 Add a control test to accept only if ALL predefined products are received (same qty).
2436 
2437  if (empty($error)) {
2438  $this->db->begin();
2439 
2440  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
2441  $sql .= " SET fk_statut = ".((int) $statut);
2442  $sql .= " WHERE rowid = ".((int) $this->id);
2443  $sql .= " AND fk_statut IN (".self::STATUS_ORDERSENT.",".self::STATUS_RECEIVED_PARTIALLY.")"; // Process running or Partially received
2444 
2445  dol_syslog(get_class($this)."::Livraison", LOG_DEBUG);
2446  $resql = $this->db->query($sql);
2447  if ($resql) {
2448  $result = 1;
2449  $old_statut = $this->statut;
2450  $this->statut = $statut;
2451  $this->actionmsg2 = $comment;
2452 
2453  // Call trigger
2454  $result_trigger = $this->call_trigger('ORDER_SUPPLIER_RECEIVE', $user);
2455  if ($result_trigger < 0) {
2456  $error++;
2457  }
2458  // End call triggers
2459 
2460  if (empty($error)) {
2461  $this->db->commit();
2462  } else {
2463  $this->statut = $old_statut;
2464  $this->db->rollback();
2465  $this->error = $this->db->lasterror();
2466  $result = -1;
2467  }
2468  } else {
2469  $this->db->rollback();
2470  $this->error = $this->db->lasterror();
2471  $result = -1;
2472  }
2473  }
2474  } else {
2475  $this->error = $langs->trans('NotAuthorized');
2476  $this->errors[] = $langs->trans('NotAuthorized');
2477  dol_syslog(get_class($this)."::Livraison Not Authorized");
2478  $result = -3;
2479  }
2480  return $result;
2481  }
2482 
2483  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2493  public function set_date_livraison($user, $delivery_date, $notrigger = 0)
2494  {
2495  // phpcs:enable
2496  return $this->setDeliveryDate($user, $delivery_date, $notrigger);
2497  }
2498 
2507  public function setDeliveryDate($user, $delivery_date, $notrigger = 0)
2508  {
2509  if ($user->rights->fournisseur->commande->creer || $user->rights->supplier_order->creer) {
2510  $error = 0;
2511 
2512  $this->db->begin();
2513 
2514  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
2515  $sql .= " SET date_livraison = ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : 'null');
2516  $sql .= " WHERE rowid = ".((int) $this->id);
2517 
2518  dol_syslog(__METHOD__, LOG_DEBUG);
2519  $resql = $this->db->query($sql);
2520  if (!$resql) {
2521  $this->errors[] = $this->db->error();
2522  $error++;
2523  }
2524 
2525  if (!$error) {
2526  $this->oldcopy = clone $this;
2527  $this->date_livraison = $delivery_date;
2528  $this->delivery_date = $delivery_date;
2529  }
2530 
2531  if (!$notrigger && empty($error)) {
2532  // Call trigger
2533  $result = $this->call_trigger('ORDER_SUPPLIER_MODIFY', $user);
2534  if ($result < 0) {
2535  $error++;
2536  }
2537  // End call triggers
2538  }
2539 
2540  if (!$error) {
2541  $this->db->commit();
2542  return 1;
2543  } else {
2544  foreach ($this->errors as $errmsg) {
2545  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2546  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2547  }
2548  $this->db->rollback();
2549  return -1 * $error;
2550  }
2551  } else {
2552  return -2;
2553  }
2554  }
2555 
2556  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2565  public function set_id_projet($user, $id_projet, $notrigger = 0)
2566  {
2567  // phpcs:enable
2568  if ($user->rights->fournisseur->commande->creer || $user->rights->supplier_order->creer) {
2569  $error = 0;
2570 
2571  $this->db->begin();
2572 
2573  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
2574  $sql .= " SET fk_projet = ".($id_projet > 0 ? (int) $id_projet : 'null');
2575  $sql .= " WHERE rowid = ".((int) $this->id);
2576 
2577  dol_syslog(__METHOD__, LOG_DEBUG);
2578  $resql = $this->db->query($sql);
2579  if (!$resql) {
2580  $this->errors[] = $this->db->error();
2581  $error++;
2582  }
2583 
2584  if (!$error) {
2585  $this->oldcopy = clone $this;
2586  $this->fk_projet = $id_projet;
2587  $this->fk_project = $id_projet;
2588  }
2589 
2590  if (!$notrigger && empty($error)) {
2591  // Call trigger
2592  $result = $this->call_trigger('ORDER_SUPPLIER_MODIFY', $user);
2593  if ($result < 0) {
2594  $error++;
2595  }
2596  // End call triggers
2597  }
2598 
2599  if (!$error) {
2600  $this->db->commit();
2601  return 1;
2602  } else {
2603  foreach ($this->errors as $errmsg) {
2604  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2605  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2606  }
2607  $this->db->rollback();
2608  return -1 * $error;
2609  }
2610  } else {
2611  return -2;
2612  }
2613  }
2614 
2623  public function updateFromCommandeClient($user, $idc, $comclientid)
2624  {
2625  $comclient = new Commande($this->db);
2626  $comclient->fetch($comclientid);
2627 
2628  $this->id = $idc;
2629 
2630  $this->lines = array();
2631 
2632  $num = count($comclient->lines);
2633  for ($i = 0; $i < $num; $i++) {
2634  $prod = new Product($this->db);
2635  $label = '';
2636  $ref = '';
2637  if ($prod->fetch($comclient->lines[$i]->fk_product) > 0) {
2638  $label = $prod->label;
2639  $ref = $prod->ref;
2640  }
2641 
2642  $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet";
2643  $sql .= " (fk_commande, label, description, fk_product, price, qty, tva_tx, localtax1_tx, localtax2_tx, remise_percent, subprice, remise, ref)";
2644  $sql .= " VALUES (".((int) $idc).", '".$this->db->escape($label)."', '".$this->db->escape($comclient->lines[$i]->desc)."'";
2645  $sql .= ",".$comclient->lines[$i]->fk_product.", ".price2num($comclient->lines[$i]->price, 'MU');
2646  $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);
2647  $sql .= ", '".price2num($comclient->lines[$i]->subprice, 'MT')."','0', '".$this->db->escape($ref)."');";
2648  if ($this->db->query($sql)) {
2649  $this->update_price(1);
2650  }
2651  }
2652 
2653  return 1;
2654  }
2655 
2663  public function setStatus($user, $status)
2664  {
2665  global $conf, $langs;
2666  $error = 0;
2667 
2668  $this->db->begin();
2669 
2670  $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande_fournisseur';
2671  $sql .= " SET fk_statut = ".$status;
2672  $sql .= " WHERE rowid = ".((int) $this->id);
2673 
2674  dol_syslog(get_class($this)."::setStatus", LOG_DEBUG);
2675  $resql = $this->db->query($sql);
2676  if ($resql) {
2677  // Trigger names for each status
2678  $triggerName = array();
2679  $triggerName[0] = 'DRAFT';
2680  $triggerName[1] = 'VALIDATED';
2681  $triggerName[2] = 'APPROVED';
2682  $triggerName[3] = 'ORDERED'; // Ordered
2683  $triggerName[4] = 'RECEIVED_PARTIALLY';
2684  $triggerName[5] = 'RECEIVED_COMPLETELY';
2685  $triggerName[6] = 'CANCELED';
2686  $triggerName[7] = 'CANCELED';
2687  $triggerName[9] = 'REFUSED';
2688 
2689  // Call trigger
2690  $result = $this->call_trigger("ORDER_SUPPLIER_STATUS_".$triggerName[$status], $user);
2691  if ($result < 0) {
2692  $error++;
2693  }
2694  // End call triggers
2695  } else {
2696  $error++;
2697  $this->error = $this->db->lasterror();
2698  dol_syslog(get_class($this)."::setStatus ".$this->error);
2699  }
2700 
2701  if (!$error) {
2702  $this->statut = $status;
2703  $this->db->commit();
2704  return 1;
2705  } else {
2706  $this->db->rollback();
2707  return -1;
2708  }
2709  }
2710 
2734  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 = '')
2735  {
2736  global $mysoc, $conf, $langs;
2737  dol_syslog(get_class($this)."::updateline $rowid, $desc, $pu, $qty, $remise_percent, $txtva, $price_base_type, $info_bits, $type, $fk_unit");
2738  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2739 
2740  $error = 0;
2741 
2742  if ($this->brouillon) {
2743  // Clean parameters
2744  if (empty($qty)) {
2745  $qty = 0;
2746  }
2747  if (empty($info_bits)) {
2748  $info_bits = 0;
2749  }
2750  if (empty($txtva)) {
2751  $txtva = 0;
2752  }
2753  if (empty($txlocaltax1)) {
2754  $txlocaltax1 = 0;
2755  }
2756  if (empty($txlocaltax2)) {
2757  $txlocaltax2 = 0;
2758  }
2759  if (empty($remise)) {
2760  $remise = 0;
2761  }
2762  if (empty($remise_percent)) {
2763  $remise_percent = 0;
2764  }
2765 
2766  $remise_percent = price2num($remise_percent);
2767  $qty = price2num($qty);
2768  if (!$qty) {
2769  $qty = 1;
2770  }
2771  $pu = price2num($pu);
2772  $pu_ht_devise = price2num($pu_ht_devise);
2773  if (!preg_match('/\((.*)\)/', $txtva)) {
2774  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
2775  }
2776  $txlocaltax1 = price2num($txlocaltax1);
2777  $txlocaltax2 = price2num($txlocaltax2);
2778 
2779  // Check parameters
2780  if ($type < 0) {
2781  return -1;
2782  }
2783  if ($date_start && $date_end && $date_start > $date_end) {
2784  $langs->load("errors");
2785  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
2786  return -1;
2787  }
2788 
2789  $this->db->begin();
2790 
2791  // Calcul du total TTC et de la TVA pour la ligne a partir de
2792  // qty, pu, remise_percent et txtva
2793  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
2794  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
2795 
2796  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $mysoc, $this->thirdparty);
2797 
2798  // Clean vat code
2799  $reg = array();
2800  $vat_src_code = '';
2801  if (preg_match('/\((.*)\)/', $txtva, $reg)) {
2802  $vat_src_code = $reg[1];
2803  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
2804  }
2805 
2806  $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);
2807  $total_ht = $tabprice[0];
2808  $total_tva = $tabprice[1];
2809  $total_ttc = $tabprice[2];
2810  $total_localtax1 = $tabprice[9];
2811  $total_localtax2 = $tabprice[10];
2812  $pu_ht = $tabprice[3];
2813  $pu_tva = $tabprice[4];
2814  $pu_ttc = $tabprice[5];
2815 
2816  // MultiCurrency
2817  $multicurrency_total_ht = $tabprice[16];
2818  $multicurrency_total_tva = $tabprice[17];
2819  $multicurrency_total_ttc = $tabprice[18];
2820  $pu_ht_devise = $tabprice[19];
2821 
2822  $localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
2823  $localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
2824 
2825  //Fetch current line from the database and then clone the object and set it in $oldline property
2826  $this->line = new CommandeFournisseurLigne($this->db);
2827  $this->line->fetch($rowid);
2828 
2829  $oldline = clone $this->line;
2830  $this->line->oldline = $oldline;
2831 
2832  $this->line->context = $this->context;
2833 
2834  $this->line->fk_commande = $this->id;
2835  //$this->line->label=$label;
2836  $this->line->desc = $desc;
2837 
2838  // redefine quantity according to packaging
2839  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
2840  if ($qty < $this->line->packaging) {
2841  $qty = $this->line->packaging;
2842  } else {
2843  if (!empty($this->line->packaging) && ($qty % $this->line->packaging) > 0) {
2844  $coeff = intval($qty / $this->line->packaging) + 1;
2845  $qty = $this->line->packaging * $coeff;
2846  setEventMessage($langs->trans('QtyRecalculatedWithPackaging'), 'mesgs');
2847  }
2848  }
2849  }
2850 
2851  $this->line->qty = $qty;
2852  $this->line->ref_supplier = $ref_supplier;
2853 
2854  $this->line->vat_src_code = $vat_src_code;
2855  $this->line->tva_tx = $txtva;
2856  $this->line->localtax1_tx = $txlocaltax1;
2857  $this->line->localtax2_tx = $txlocaltax2;
2858  $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
2859  $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
2860  $this->line->remise_percent = $remise_percent;
2861  $this->line->subprice = $pu_ht;
2862  $this->line->rang = $this->rang;
2863  $this->line->info_bits = $info_bits;
2864  $this->line->total_ht = $total_ht;
2865  $this->line->total_tva = $total_tva;
2866  $this->line->total_localtax1 = $total_localtax1;
2867  $this->line->total_localtax2 = $total_localtax2;
2868  $this->line->total_ttc = $total_ttc;
2869  $this->line->product_type = $type;
2870  $this->line->special_code = (!empty($this->special_code) ? $this->special_code : 0);
2871  $this->line->origin = $this->origin;
2872  $this->line->fk_unit = $fk_unit;
2873 
2874  $this->line->date_start = $date_start;
2875  $this->line->date_end = $date_end;
2876 
2877  // Multicurrency
2878  $this->line->fk_multicurrency = $this->fk_multicurrency;
2879  $this->line->multicurrency_code = $this->multicurrency_code;
2880  $this->line->multicurrency_subprice = $pu_ht_devise;
2881  $this->line->multicurrency_total_ht = $multicurrency_total_ht;
2882  $this->line->multicurrency_total_tva = $multicurrency_total_tva;
2883  $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
2884 
2885  $this->line->subprice = $pu_ht;
2886  $this->line->price = $this->line->subprice;
2887 
2888  $this->line->remise_percent = $remise_percent;
2889 
2890  if (is_array($array_options) && count($array_options) > 0) {
2891  // We replace values in this->line->array_options only for entries defined into $array_options
2892  foreach ($array_options as $key => $value) {
2893  $this->line->array_options[$key] = $array_options[$key];
2894  }
2895  }
2896 
2897  $result = $this->line->update($notrigger);
2898 
2899 
2900  // Mise a jour info denormalisees au niveau facture
2901  if ($result >= 0) {
2902  $this->update_price('1', 'auto');
2903  $this->db->commit();
2904  return $result;
2905  } else {
2906  $this->error = $this->db->lasterror();
2907  $this->db->rollback();
2908  return -1;
2909  }
2910  } else {
2911  $this->error = "Order status makes operation forbidden";
2912  dol_syslog(get_class($this)."::updateline ".$this->error, LOG_ERR);
2913  return -2;
2914  }
2915  }
2916 
2917 
2925  public function initAsSpecimen()
2926  {
2927  global $user, $langs, $conf;
2928 
2929  include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
2930 
2931  dol_syslog(get_class($this)."::initAsSpecimen");
2932 
2933  $now = dol_now();
2934 
2935  // Find first product
2936  $prodid = 0;
2937  $product = new ProductFournisseur($this->db);
2938  $sql = "SELECT rowid";
2939  $sql .= " FROM ".MAIN_DB_PREFIX."product";
2940  $sql .= " WHERE entity IN (".getEntity('product').")";
2941  $sql .= $this->db->order("rowid", "ASC");
2942  $sql .= $this->db->plimit(1);
2943  $resql = $this->db->query($sql);
2944  if ($resql) {
2945  $obj = $this->db->fetch_object($resql);
2946  $prodid = $obj->rowid;
2947  }
2948 
2949  // Initialise parametres
2950  $this->id = 0;
2951  $this->ref = 'SPECIMEN';
2952  $this->specimen = 1;
2953  $this->socid = 1;
2954  $this->date = $now;
2955  $this->date_commande = $now;
2956  $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
2957  $this->cond_reglement_code = 'RECEP';
2958  $this->mode_reglement_code = 'CHQ';
2959 
2960  $this->note_public = 'This is a comment (public)';
2961  $this->note_private = 'This is a comment (private)';
2962 
2963  $this->multicurrency_tx = 1;
2964  $this->multicurrency_code = $conf->currency;
2965 
2966  $this->statut = 0;
2967 
2968  // Lines
2969  $nbp = 5;
2970  $xnbp = 0;
2971  while ($xnbp < $nbp) {
2972  $line = new CommandeFournisseurLigne($this->db);
2973  $line->desc = $langs->trans("Description")." ".$xnbp;
2974  $line->qty = 1;
2975  $line->subprice = 100;
2976  $line->price = 100;
2977  $line->tva_tx = 19.6;
2978  $line->localtax1_tx = 0;
2979  $line->localtax2_tx = 0;
2980  if ($xnbp == 2) {
2981  $line->total_ht = 50;
2982  $line->total_ttc = 59.8;
2983  $line->total_tva = 9.8;
2984  $line->remise_percent = 50;
2985  } else {
2986  $line->total_ht = 100;
2987  $line->total_ttc = 119.6;
2988  $line->total_tva = 19.6;
2989  $line->remise_percent = 00;
2990  }
2991  $line->fk_product = $prodid;
2992 
2993  $this->lines[$xnbp] = $line;
2994 
2995  $this->total_ht += $line->total_ht;
2996  $this->total_tva += $line->total_tva;
2997  $this->total_ttc += $line->total_ttc;
2998 
2999  $xnbp++;
3000  }
3001  }
3002 
3009  public function info($id)
3010  {
3011  $sql = 'SELECT c.rowid, date_creation as datec, tms as datem, date_valid as date_validation, date_approve as datea, date_approve2 as datea2,';
3012  $sql .= ' fk_user_author, fk_user_modif, fk_user_valid, fk_user_approve, fk_user_approve2';
3013  $sql .= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseur as c';
3014  $sql .= ' WHERE c.rowid = '.((int) $id);
3015 
3016  $result = $this->db->query($sql);
3017  if ($result) {
3018  if ($this->db->num_rows($result)) {
3019  $obj = $this->db->fetch_object($result);
3020  $this->id = $obj->rowid;
3021  if ($obj->fk_user_author) {
3022  $this->user_creation_id = $obj->fk_user_author;
3023  }
3024  if ($obj->fk_user_valid) {
3025  $this->user_validation_id = $obj->fk_user_valid;
3026  }
3027  if ($obj->fk_user_modif) {
3028  $this->user_modification_id = $obj->fk_user_modif;
3029  }
3030  if ($obj->fk_user_approve) {
3031  $this->user_approve_id = $obj->fk_user_approve;
3032  }
3033  if ($obj->fk_user_approve2) {
3034  $this->user_approve_id2 = $obj->fk_user_approve2;
3035  }
3036 
3037  $this->date_creation = $this->db->jdate($obj->datec);
3038  $this->date_modification = $this->db->jdate($obj->datem);
3039  $this->date_approve = $this->db->jdate($obj->datea);
3040  $this->date_approve2 = $this->db->jdate($obj->datea2);
3041  $this->date_validation = $this->db->jdate($obj->date_validation);
3042  }
3043  $this->db->free($result);
3044  } else {
3045  dol_print_error($this->db);
3046  }
3047  }
3048 
3049  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3055  public function load_state_board()
3056  {
3057  // phpcs:enable
3058  global $conf, $user;
3059 
3060  $this->nb = array();
3061  $clause = "WHERE";
3062 
3063  $sql = "SELECT count(co.rowid) as nb";
3064  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as co";
3065  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON co.fk_soc = s.rowid";
3066  if (empty($user->rights->societe->client->voir) && !$user->socid) {
3067  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3068  $sql .= " WHERE sc.fk_user = ".((int) $user->id);
3069  $clause = "AND";
3070  }
3071  $sql .= " ".$clause." co.entity IN (".getEntity('supplier_order').")";
3072 
3073  $resql = $this->db->query($sql);
3074  if ($resql) {
3075  while ($obj = $this->db->fetch_object($resql)) {
3076  $this->nb["supplier_orders"] = $obj->nb;
3077  }
3078  $this->db->free($resql);
3079  return 1;
3080  } else {
3081  dol_print_error($this->db);
3082  $this->error = $this->db->error();
3083  return -1;
3084  }
3085  }
3086 
3087  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3095  public function load_board($user, $mode = 'opened')
3096  {
3097  // phpcs:enable
3098  global $conf, $langs;
3099 
3100  $sql = "SELECT c.rowid, c.date_creation as datec, c.date_commande, c.fk_statut, c.date_livraison as delivery_date, c.total_ht";
3101  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c";
3102  if (empty($user->rights->societe->client->voir) && !$user->socid) {
3103  $sql .= " JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3104  }
3105  $sql .= " WHERE c.entity = ".$conf->entity;
3106  if ($mode === 'awaiting') {
3107  $sql .= " AND c.fk_statut IN (".self::STATUS_ORDERSENT.", ".self::STATUS_RECEIVED_PARTIALLY.")";
3108  } else {
3109  $sql .= " AND c.fk_statut IN (".self::STATUS_VALIDATED.", ".self::STATUS_ACCEPTED.")";
3110  }
3111  if ($user->socid) {
3112  $sql .= " AND c.fk_soc = ".((int) $user->socid);
3113  }
3114 
3115  $resql = $this->db->query($sql);
3116  if ($resql) {
3117  $commandestatic = new CommandeFournisseur($this->db);
3118 
3119  $response = new WorkboardResponse();
3120  $response->warning_delay = $conf->commande->fournisseur->warning_delay / 60 / 60 / 24;
3121  $response->label = $langs->trans("SuppliersOrdersToProcess");
3122  $response->labelShort = $langs->trans("Opened");
3123  $response->url = DOL_URL_ROOT.'/fourn/commande/list.php?search_status=1,2&mainmenu=commercial&leftmenu=orders_suppliers';
3124  $response->img = img_object('', "order");
3125 
3126  if ($mode === 'awaiting') {
3127  $response->label = $langs->trans("SuppliersOrdersAwaitingReception");
3128  $response->labelShort = $langs->trans("AwaitingReception");
3129  $response->url = DOL_URL_ROOT.'/fourn/commande/list.php?search_status=3,4&mainmenu=commercial&leftmenu=orders_suppliers';
3130  }
3131 
3132  while ($obj = $this->db->fetch_object($resql)) {
3133  $commandestatic->delivery_date = $this->db->jdate($obj->delivery_date);
3134  $commandestatic->date_commande = $this->db->jdate($obj->date_commande);
3135  $commandestatic->statut = $obj->fk_statut;
3136 
3137  $response->nbtodo++;
3138  $response->total += $obj->total_ht;
3139 
3140  if ($commandestatic->hasDelay()) {
3141  $response->nbtodolate++;
3142  }
3143  }
3144 
3145  return $response;
3146  } else {
3147  $this->error = $this->db->error();
3148  return -1;
3149  }
3150  }
3151 
3158  public function getInputMethod()
3159  {
3160  global $db, $langs;
3161 
3162  if ($this->methode_commande_id > 0) {
3163  $sql = "SELECT rowid, code, libelle as label";
3164  $sql .= " FROM ".MAIN_DB_PREFIX.'c_input_method';
3165  $sql .= " WHERE active=1 AND rowid = ".((int) $this->methode_commande_id);
3166 
3167  $resql = $this->db->query($sql);
3168  if ($resql) {
3169  if ($this->db->num_rows($resql)) {
3170  $obj = $this->db->fetch_object($resql);
3171 
3172  $string = $langs->trans($obj->code);
3173  if ($string == $obj->code) {
3174  $string = $obj->label != '-' ? $obj->label : '';
3175  }
3176  return $string;
3177  }
3178  } else {
3179  dol_print_error($this->db);
3180  }
3181  }
3182 
3183  return '';
3184  }
3185 
3197  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
3198  {
3199  global $conf, $langs;
3200 
3201  if (!dol_strlen($modele)) {
3202  $modele = ''; // No doc template/generation by default
3203 
3204  if (!empty($this->model_pdf)) {
3205  $modele = $this->model_pdf;
3206  } elseif (!empty($conf->global->COMMANDE_SUPPLIER_ADDON_PDF)) {
3207  $modele = $conf->global->COMMANDE_SUPPLIER_ADDON_PDF;
3208  }
3209  }
3210 
3211  if (empty($modele)) {
3212  return 0;
3213  } else {
3214  $langs->load("suppliers");
3215  $outputlangs->load("products");
3216 
3217  $modelpath = "core/modules/supplier_order/doc/";
3218  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3219  }
3220  }
3221 
3228  public function getMaxDeliveryTimeDay($langs)
3229  {
3230  if (empty($this->lines)) {
3231  return '';
3232  }
3233 
3234  $obj = new ProductFournisseur($this->db);
3235 
3236  $nb = 0;
3237  foreach ($this->lines as $line) {
3238  if ($line->fk_product > 0) {
3239  $idp = $obj->find_min_price_product_fournisseur($line->fk_product, $line->qty);
3240  if ($idp) {
3241  $obj->fetch($idp);
3242  if ($obj->delivery_time_days > $nb) {
3243  $nb = $obj->delivery_time_days;
3244  }
3245  }
3246  }
3247  }
3248 
3249  if ($nb === 0) {
3250  return '';
3251  } else {
3252  return $nb.' '.$langs->trans('Days');
3253  }
3254  }
3255 
3260  public function getRights()
3261  {
3262  global $user;
3263 
3264  return $user->rights->fournisseur->commande;
3265  }
3266 
3267 
3276  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
3277  {
3278  $tables = array(
3279  'commande_fournisseur'
3280  );
3281 
3282  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
3283  }
3284 
3293  public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
3294  {
3295  $tables = array(
3296  'commande_fournisseurdet'
3297  );
3298 
3299  return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
3300  }
3301 
3309  public function hasDelay()
3310  {
3311  global $conf;
3312 
3313  if (empty($this->delivery_date) && !empty($this->date_livraison)) {
3314  $this->delivery_date = $this->date_livraison; // For backward compatibility
3315  }
3316 
3317  if ($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY) {
3318  $now = dol_now();
3319  if (!empty($this->delivery_date)) {
3320  $date_to_test = $this->delivery_date;
3321  return $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3322  } else {
3323  //$date_to_test = $this->date_commande;
3324  //return $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3325  return false;
3326  }
3327  } else {
3328  $now = dol_now();
3329  $date_to_test = $this->date_commande;
3330 
3331  return ($this->statut > 0 && $this->statut < 5) && $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3332  }
3333  }
3334 
3342  public function showDelay()
3343  {
3344  global $conf, $langs;
3345 
3346  if (empty($this->delivery_date) && !empty($this->date_livraison)) {
3347  $this->delivery_date = $this->date_livraison; // For backward compatibility
3348  }
3349 
3350  $text = '';
3351 
3352  if ($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY) {
3353  if (!empty($this->delivery_date)) {
3354  $text = $langs->trans("DeliveryDate").' '.dol_print_date($this->delivery_date, 'day');
3355  } else {
3356  $text = $langs->trans("OrderDate").' '.dol_print_date($this->date_commande, 'day');
3357  }
3358  } else {
3359  $text = $langs->trans("OrderDate").' '.dol_print_date($this->date_commande, 'day');
3360  }
3361  if ($text) {
3362  $text .= ' '.($conf->commande->fournisseur->warning_delay > 0 ? '+' : '-').' '.round(abs($conf->commande->fournisseur->warning_delay) / 3600 / 24, 1).' '.$langs->trans("days").' < '.$langs->trans("Today");
3363  }
3364 
3365  return $text;
3366  }
3367 
3368 
3377  public function calcAndSetStatusDispatch(User $user, $closeopenorder = 1, $comment = '')
3378  {
3379  global $conf, $langs;
3380 
3381  if ((isModEnabled("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || isModEnabled("supplier_order")) {
3382  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.dispatch.class.php';
3383 
3384  $qtydelivered = array();
3385  $qtywished = array();
3386 
3387  $supplierorderdispatch = new CommandeFournisseurDispatch($this->db);
3388  $filter = array('t.fk_commande'=>$this->id);
3389  if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
3390  $filter['t.status'] = 1; // Restrict to lines with status validated
3391  }
3392 
3393  $ret = $supplierorderdispatch->fetchAll('', '', 0, 0, $filter);
3394  if ($ret < 0) {
3395  $this->error = $supplierorderdispatch->error; $this->errors = $supplierorderdispatch->errors;
3396  return $ret;
3397  } else {
3398  if (is_array($supplierorderdispatch->lines) && count($supplierorderdispatch->lines) > 0) {
3399  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
3400  $date_liv = dol_now();
3401 
3402  // Build array with quantity deliverd by product
3403  foreach ($supplierorderdispatch->lines as $line) {
3404  $qtydelivered[$line->fk_product] += $line->qty;
3405  }
3406  foreach ($this->lines as $line) {
3407  // Exclude lines not qualified for shipment, similar code is found into interface_20_modWrokflow for customers
3408  if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $line->product_type > 0) {
3409  continue;
3410  }
3411  $qtywished[$line->fk_product] += $line->qty;
3412  }
3413 
3414  //Compare array
3415  $diff_array = array_diff_assoc($qtydelivered, $qtywished); // Warning: $diff_array is done only on common keys.
3416  $keysinwishednotindelivered = array_diff(array_keys($qtywished), array_keys($qtydelivered)); // To check we also have same number of keys
3417  $keysindeliverednotinwished = array_diff(array_keys($qtydelivered), array_keys($qtywished)); // To check we also have same number of keys
3418  //var_dump(array_keys($qtydelivered));
3419  //var_dump(array_keys($qtywished));
3420  //var_dump($diff_array);
3421  //var_dump($keysinwishednotindelivered);
3422  //var_dump($keysindeliverednotinwished);
3423  //exit;
3424 
3425  if (count($diff_array) == 0 && count($keysinwishednotindelivered) == 0 && count($keysindeliverednotinwished) == 0) { //No diff => mean everythings is received
3426  if ($closeopenorder) {
3427  //$ret=$this->setStatus($user,5);
3428  $ret = $this->Livraison($user, $date_liv, 'tot', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3429  if ($ret < 0) {
3430  return -1;
3431  }
3432  return 5;
3433  } else {
3434  //Diff => received partially
3435  //$ret=$this->setStatus($user,4);
3436  $ret = $this->Livraison($user, $date_liv, 'par', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3437  if ($ret < 0) {
3438  return -1;
3439  }
3440  return 4;
3441  }
3442  } elseif (!empty($conf->global->SUPPLIER_ORDER_MORE_THAN_WISHED)) {
3443  //set livraison to 'tot' if more products received than wished. (and if $closeopenorder is set to 1 of course...)
3444 
3445  $close = 0;
3446 
3447  if (count($diff_array) > 0) {
3448  //there are some difference between the two arrays
3449 
3450  //scan the array of results
3451  foreach ($diff_array as $key => $value) {
3452  //if the quantity delivered is greater or equal to wish quantity
3453  if ($qtydelivered[$key] >= $qtywished[$key]) {
3454  $close++;
3455  }
3456  }
3457  }
3458 
3459 
3460  if ($close == count($diff_array)) {
3461  //all the products are received equal or more than the wished quantity
3462  if ($closeopenorder) {
3463  $ret = $this->Livraison($user, $date_liv, 'tot', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3464  if ($ret < 0) {
3465  return -1;
3466  }
3467  return 5;
3468  } else {
3469  //Diff => received partially
3470  $ret = $this->Livraison($user, $date_liv, 'par', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3471  if ($ret < 0) {
3472  return -1;
3473  }
3474  return 4;
3475  }
3476  } else {
3477  //all the products are not received
3478  $ret = $this->Livraison($user, $date_liv, 'par', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3479  if ($ret < 0) {
3480  return -1;
3481  }
3482  return 4;
3483  }
3484  } else {
3485  //Diff => received partially
3486  $ret = $this->Livraison($user, $date_liv, 'par', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3487  if ($ret < 0) {
3488  return -1;
3489  }
3490  return 4;
3491  }
3492  }
3493  return 1;
3494  }
3495  }
3496  return 0;
3497  }
3498 
3506  public function loadReceptions($filtre_statut = -1)
3507  {
3508  $this->receptions = array();
3509 
3510  dol_syslog(get_class($this)."::loadReceptions", LOG_DEBUG);
3511 
3512  $sql = 'SELECT cd.rowid, cd.fk_product,';
3513  $sql .= ' sum(cfd.qty) as qty';
3514  $sql .= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseur_dispatch as cfd,';
3515  if ($filtre_statut >= 0) {
3516  $sql .= ' '.MAIN_DB_PREFIX.'reception as e,';
3517  }
3518  $sql .= ' '.MAIN_DB_PREFIX.'commande_fournisseurdet as cd';
3519  $sql .= ' WHERE';
3520  if ($filtre_statut >= 0) {
3521  $sql .= ' cfd.fk_reception = e.rowid AND';
3522  }
3523  $sql .= ' cfd.fk_commandefourndet = cd.rowid';
3524  $sql .= ' AND cd.fk_commande ='.((int) $this->id);
3525  if ($this->fk_product > 0) {
3526  $sql .= ' AND cd.fk_product = '.((int) $this->fk_product);
3527  }
3528  if ($filtre_statut >= 0) {
3529  $sql .= ' AND e.fk_statut >= '.((int) $filtre_statut);
3530  }
3531  $sql .= ' GROUP BY cd.rowid, cd.fk_product';
3532 
3533  $resql = $this->db->query($sql);
3534  if ($resql) {
3535  $num = $this->db->num_rows($resql);
3536  $i = 0;
3537  while ($i < $num) {
3538  $obj = $this->db->fetch_object($resql);
3539  empty($this->receptions[$obj->rowid]) ? $this->receptions[$obj->rowid] = $obj->qty : $this->receptions[$obj->rowid] += $obj->qty;
3540  $i++;
3541  }
3542  $this->db->free($resql);
3543 
3544  return $num;
3545  } else {
3546  $this->error = $this->db->lasterror();
3547  return -1;
3548  }
3549  }
3550 }
3551 
3552 
3553 
3558 {
3562  public $element = 'commande_fournisseurdet';
3563 
3567  public $table_element = 'commande_fournisseurdet';
3568 
3569  public $oldline;
3570 
3575  public $fk_commande;
3576 
3577  // From llx_commande_fournisseurdet
3581  public $fk_parent_line;
3582 
3586  public $fk_facture;
3587 
3588  public $rang = 0;
3589  public $special_code = 0;
3590 
3595  public $pu_ht;
3596 
3597  public $date_start;
3598  public $date_end;
3599 
3600  // From llx_product_fournisseur_price
3601 
3606  public $ref_supplier;
3607  public $remise;
3608 
3609 
3615  public function __construct($db)
3616  {
3617  $this->db = $db;
3618  }
3619 
3626  public function fetch($rowid)
3627  {
3628  global $conf;
3629 
3630  $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,';
3631  $sql .= ' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,';
3632  $sql .= ' cd.remise, cd.remise_percent, cd.subprice,';
3633  $sql .= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
3634  $sql .= ' cd.total_localtax1, cd.total_localtax2,';
3635  $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
3636  $sql .= ' cd.date_start, cd.date_end, cd.fk_unit,';
3637  $sql .= ' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
3638  $sql .= ' c.fk_soc as socid';
3639  $sql .= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseur as c, '.MAIN_DB_PREFIX.'commande_fournisseurdet as cd';
3640  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid';
3641  $sql .= ' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid);
3642 
3643  $result = $this->db->query($sql);
3644  if ($result) {
3645  $objp = $this->db->fetch_object($result);
3646 
3647  if (!empty($objp)) {
3648  $this->rowid = $objp->rowid;
3649  $this->id = $objp->rowid;
3650  $this->fk_commande = $objp->fk_commande;
3651  $this->desc = $objp->description;
3652  $this->qty = $objp->qty;
3653  $this->ref_fourn = $objp->ref_supplier;
3654  $this->ref_supplier = $objp->ref_supplier;
3655  $this->subprice = $objp->subprice;
3656  $this->tva_tx = $objp->tva_tx;
3657  $this->localtax1_tx = $objp->localtax1_tx;
3658  $this->localtax2_tx = $objp->localtax2_tx;
3659  $this->localtax1_type = $objp->localtax1_type;
3660  $this->localtax2_type = $objp->localtax2_type;
3661  $this->remise = $objp->remise;
3662  $this->remise_percent = $objp->remise_percent;
3663  $this->fk_product = $objp->fk_product;
3664  $this->info_bits = $objp->info_bits;
3665  $this->total_ht = $objp->total_ht;
3666  $this->total_tva = $objp->total_tva;
3667  $this->total_localtax1 = $objp->total_localtax1;
3668  $this->total_localtax2 = $objp->total_localtax2;
3669  $this->total_ttc = $objp->total_ttc;
3670  $this->product_type = $objp->product_type;
3671  $this->special_code = $objp->special_code;
3672 
3673  $this->ref = $objp->product_ref;
3674 
3675  $this->product_ref = $objp->product_ref;
3676  $this->product_label = $objp->product_label;
3677  $this->product_desc = $objp->product_desc;
3678 
3679  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
3680  // TODO We should not fetch this properties into the fetch_lines. This is NOT properties of a line.
3681  // Move this into another method and call it when required.
3682 
3683  // Take better packaging for $objp->qty (first supplier ref quantity <= $objp->qty)
3684  $sqlsearchpackage = 'SELECT rowid, packaging FROM '.MAIN_DB_PREFIX."product_fournisseur_price";
3685  $sqlsearchpackage .= ' WHERE entity IN ('.getEntity('product_fournisseur_price').")";
3686  $sqlsearchpackage .= " AND fk_product = ".((int) $objp->fk_product);
3687  $sqlsearchpackage .= " AND ref_fourn = '".$this->db->escape($objp->ref_supplier)."'";
3688  $sqlsearchpackage .= " AND quantity <= ".((float) $objp->qty); // required to be qualified
3689  $sqlsearchpackage .= " AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).")"; // required to be qualified
3690  $sqlsearchpackage .= " AND fk_soc = ".((int) $objp->socid);
3691  $sqlsearchpackage .= " ORDER BY packaging ASC"; // Take the smaller package first
3692  $sqlsearchpackage .= " LIMIT 1";
3693 
3694  $resqlsearchpackage = $this->db->query($sqlsearchpackage);
3695  if ($resqlsearchpackage) {
3696  $objsearchpackage = $this->db->fetch_object($resqlsearchpackage);
3697  if ($objsearchpackage) {
3698  $this->fk_fournprice = $objsearchpackage->rowid;
3699  $this->packaging = $objsearchpackage->packaging;
3700  }
3701  } else {
3702  $this->error = $this->db->lasterror();
3703  return -1;
3704  }
3705  }
3706 
3707  $this->date_start = $this->db->jdate($objp->date_start);
3708  $this->date_end = $this->db->jdate($objp->date_end);
3709  $this->fk_unit = $objp->fk_unit;
3710 
3711  $this->multicurrency_subprice = $objp->multicurrency_subprice;
3712  $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
3713  $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
3714  $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
3715 
3716  $this->fetch_optionals();
3717 
3718  $this->db->free($result);
3719  return 1;
3720  } else {
3721  $this->error = 'Supplier order line with id='.$rowid.' not found';
3722  dol_syslog(get_class($this)."::fetch Error ".$this->error, LOG_ERR);
3723  return 0;
3724  }
3725  } else {
3726  dol_print_error($this->db);
3727  return -1;
3728  }
3729  }
3730 
3737  public function insert($notrigger = 0)
3738  {
3739  global $conf, $user;
3740 
3741  $error = 0;
3742 
3743  dol_syslog(get_class($this)."::insert rang=".$this->rang);
3744 
3745  // Clean parameters
3746  if (empty($this->tva_tx)) {
3747  $this->tva_tx = 0;
3748  }
3749  if (empty($this->localtax1_tx)) {
3750  $this->localtax1_tx = 0;
3751  }
3752  if (empty($this->localtax2_tx)) {
3753  $this->localtax2_tx = 0;
3754  }
3755  if (empty($this->localtax1_type)) {
3756  $this->localtax1_type = '0';
3757  }
3758  if (empty($this->localtax2_type)) {
3759  $this->localtax2_type = '0';
3760  }
3761  if (empty($this->total_localtax1)) {
3762  $this->total_localtax1 = 0;
3763  }
3764  if (empty($this->total_localtax2)) {
3765  $this->total_localtax2 = 0;
3766  }
3767  if (empty($this->rang)) {
3768  $this->rang = 0;
3769  }
3770  if (empty($this->remise_percent)) {
3771  $this->remise_percent = 0;
3772  }
3773  if (empty($this->info_bits)) {
3774  $this->info_bits = 0;
3775  }
3776  if (empty($this->special_code)) {
3777  $this->special_code = 0;
3778  }
3779  if (empty($this->fk_parent_line)) {
3780  $this->fk_parent_line = 0;
3781  }
3782  if (empty($this->pa_ht)) {
3783  $this->pa_ht = 0;
3784  }
3785 
3786  // Multicurrency
3787  if (!empty($this->multicurrency_code)) {
3788  list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code);
3789  }
3790  if (empty($this->fk_multicurrency)) {
3791  $this->multicurrency_code = $conf->currency;
3792  $this->fk_multicurrency = 0;
3793  $this->multicurrency_tx = 1;
3794  }
3795 
3796  // Check parameters
3797  if ($this->product_type < 0) {
3798  return -1;
3799  }
3800 
3801  $this->db->begin();
3802 
3803  // Insertion dans base de la ligne
3804  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
3805  $sql .= " (fk_commande, label, description, date_start, date_end,";
3806  $sql .= " fk_product, product_type, special_code, rang,";
3807  $sql .= " qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
3808  $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
3809  $sql .= " fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc";
3810  $sql .= ")";
3811  $sql .= " VALUES (".$this->fk_commande.", '".$this->db->escape($this->label)."','".$this->db->escape($this->desc)."',";
3812  $sql .= " ".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : "null").",";
3813  $sql .= " ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null").",";
3814  if ($this->fk_product) {
3815  $sql .= $this->fk_product.",";
3816  } else {
3817  $sql .= "null,";
3818  }
3819  $sql .= "'".$this->db->escape($this->product_type)."',";
3820  $sql .= "'".$this->db->escape($this->special_code)."',";
3821  $sql .= "'".$this->db->escape($this->rang)."',";
3822  $sql .= "'".$this->db->escape($this->qty)."', ";
3823  $sql .= " ".(empty($this->vat_src_code) ? "''" : "'".$this->db->escape($this->vat_src_code)."'").",";
3824  $sql .= " ".price2num($this->tva_tx).", ";
3825  $sql .= " ".price2num($this->localtax1_tx).",";
3826  $sql .= " ".price2num($this->localtax2_tx).",";
3827  $sql .= " '".$this->db->escape($this->localtax1_type)."',";
3828  $sql .= " '".$this->db->escape($this->localtax2_type)."',";
3829  $sql .= " ".((float) $this->remise_percent).", ".price2num($this->subprice, 'MU').", '".$this->db->escape($this->ref_supplier)."',";
3830  $sql .= " ".price2num($this->total_ht).",";
3831  $sql .= " ".price2num($this->total_tva).",";
3832  $sql .= " ".price2num($this->total_localtax1).",";
3833  $sql .= " ".price2num($this->total_localtax2).",";
3834  $sql .= " ".price2num($this->total_ttc).",";
3835  $sql .= ($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'" : "null");
3836  $sql .= ", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) : "null");
3837  $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
3838  $sql .= ", ".($this->multicurrency_subprice ? price2num($this->multicurrency_subprice) : '0');
3839  $sql .= ", ".($this->multicurrency_total_ht ? price2num($this->multicurrency_total_ht) : '0');
3840  $sql .= ", ".($this->multicurrency_total_tva ? price2num($this->multicurrency_total_tva) : '0');
3841  $sql .= ", ".($this->multicurrency_total_ttc ? price2num($this->multicurrency_total_ttc) : '0');
3842  $sql .= ")";
3843 
3844  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
3845  $resql = $this->db->query($sql);
3846  if ($resql) {
3847  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
3848  $this->rowid = $this->id;
3849 
3850  if (!$error) {
3851  $result = $this->insertExtraFields();
3852  if ($result < 0) {
3853  $error++;
3854  }
3855  }
3856 
3857  if (!$error && !$notrigger) {
3858  // Call trigger
3859  $result = $this->call_trigger('LINEORDER_SUPPLIER_CREATE', $user);
3860  if ($result < 0) {
3861  $error++;
3862  }
3863  // End call triggers
3864  }
3865 
3866  if (!$error) {
3867  $this->db->commit();
3868  return 1;
3869  }
3870 
3871  foreach ($this->errors as $errmsg) {
3872  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
3873  $this->errors[] = ($this->errors ? ', '.$errmsg : $errmsg);
3874  }
3875  $this->db->rollback();
3876  return -1 * $error;
3877  } else {
3878  $this->errors[] = $this->db->error();
3879  $this->db->rollback();
3880  return -2;
3881  }
3882  }
3889  public function update($notrigger = 0)
3890  {
3891  global $conf, $user;
3892 
3893  $error = 0;
3894 
3895  $this->db->begin();
3896 
3897  // Mise a jour ligne en base
3898  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
3899  $sql .= " description='".$this->db->escape($this->desc)."'";
3900  $sql .= ", ref='".$this->db->escape($this->ref_supplier)."'";
3901  $sql .= ", subprice='".price2num($this->subprice)."'";
3902  //$sql.= ",remise='".price2num($remise)."'";
3903  $sql .= ", remise_percent='".price2num($this->remise_percent)."'";
3904 
3905  $sql .= ", vat_src_code = '".(empty($this->vat_src_code) ? '' : $this->vat_src_code)."'";
3906  $sql .= ", tva_tx='".price2num($this->tva_tx)."'";
3907  $sql .= ", localtax1_tx='".price2num($this->localtax1_tx)."'";
3908  $sql .= ", localtax2_tx='".price2num($this->localtax2_tx)."'";
3909  $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
3910  $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
3911  $sql .= ", qty='".price2num($this->qty)."'";
3912  $sql .= ", date_start=".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null");
3913  $sql .= ", date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null");
3914  $sql .= ", info_bits='".$this->db->escape($this->info_bits)."'";
3915  $sql .= ", total_ht='".price2num($this->total_ht)."'";
3916  $sql .= ", total_tva='".price2num($this->total_tva)."'";
3917  $sql .= ", total_localtax1='".price2num($this->total_localtax1)."'";
3918  $sql .= ", total_localtax2='".price2num($this->total_localtax2)."'";
3919  $sql .= ", total_ttc='".price2num($this->total_ttc)."'";
3920  $sql .= ", product_type=".$this->product_type;
3921  $sql .= ", special_code=".(!empty($this->special_code) ? $this->special_code : 0);
3922  $sql .= ($this->fk_unit ? ", fk_unit='".$this->db->escape($this->fk_unit)."'" : ", fk_unit=null");
3923 
3924  // Multicurrency
3925  $sql .= ", multicurrency_subprice=".price2num($this->multicurrency_subprice)."";
3926  $sql .= ", multicurrency_total_ht=".price2num($this->multicurrency_total_ht)."";
3927  $sql .= ", multicurrency_total_tva=".price2num($this->multicurrency_total_tva)."";
3928  $sql .= ", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc)."";
3929 
3930  $sql .= " WHERE rowid = ".((int) $this->id);
3931 
3932  dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
3933  $resql = $this->db->query($sql);
3934  if ($resql) {
3935  if (!$error) {
3936  $result = $this->insertExtraFields();
3937  if ($result < 0) {
3938  $error++;
3939  }
3940  }
3941 
3942  if (!$error && !$notrigger) {
3943  // Call trigger
3944  $result = $this->call_trigger('LINEORDER_SUPPLIER_MODIFY', $user);
3945  if ($result < 0) {
3946  $this->db->rollback();
3947  return -1;
3948  }
3949  // End call triggers
3950  }
3951 
3952  if (!$error) {
3953  $this->db->commit();
3954  return 1;
3955  } else {
3956  $this->db->rollback();
3957  return -1;
3958  }
3959  } else {
3960  $this->error = $this->db->lasterror();
3961  $this->db->rollback();
3962  return -1;
3963  }
3964  }
3965 
3972  public function delete($notrigger = 0)
3973  {
3974  global $user;
3975 
3976  $error = 0;
3977 
3978  $this->db->begin();
3979 
3980  // extrafields
3981  $result = $this->deleteExtraFields();
3982  if ($result < 0) {
3983  $this->db->rollback();
3984  return -1;
3985  }
3986 
3987  $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commande_fournisseurdet WHERE rowid=".((int) $this->id);
3988 
3989  dol_syslog(__METHOD__, LOG_DEBUG);
3990  $resql = $this->db->query($sql);
3991  if ($resql) {
3992  if (!$notrigger) {
3993  // Call trigger
3994  $result = $this->call_trigger('LINEORDER_SUPPLIER_DELETE', $user);
3995  if ($result < 0) {
3996  $error++;
3997  }
3998  // End call triggers
3999  }
4000 
4001  if (!$error) {
4002  $this->db->commit();
4003  return 1;
4004  }
4005 
4006  foreach ($this->errors as $errmsg) {
4007  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
4008  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
4009  }
4010  $this->db->rollback();
4011  return -1 * $error;
4012  } else {
4013  $this->error = $this->db->lasterror();
4014  return -1;
4015  }
4016  }
4017 }
CommandeFournisseur\fetch_lines
fetch_lines($only_product=0)
Load array lines.
Definition: fournisseur.commande.class.php:485
CommandeFournisseur\STATUS_ACCEPTED
const STATUS_ACCEPTED
Accepted.
Definition: fournisseur.commande.class.php:279
CommandeFournisseur\STATUS_CANCELED
const STATUS_CANCELED
Order canceled.
Definition: fournisseur.commande.class.php:299
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:49
getLocalTaxesFromRate
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
Definition: functions.lib.php:6264
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_escape_htmltag
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.
Definition: functions.lib.php:1493
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1225
CommandeFournisseur\deleteline
deleteline($idline, $notrigger=0)
Delete line.
Definition: fournisseur.commande.class.php:2136
CommandeFournisseur\updateFromCommandeClient
updateFromCommandeClient($user, $idc, $comclientid)
Update a supplier order from a sales order.
Definition: fournisseur.commande.class.php:2623
CommandeFournisseur\create
create($user, $notrigger=0)
Create order with draft status.
Definition: fournisseur.commande.class.php:1340
CommandeFournisseurLigne\__construct
__construct($db)
Constructor.
Definition: fournisseur.commande.class.php:3615
dol_delete_dir_recursive
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)
Definition: files.lib.php:1402
CommandeFournisseur\setDeliveryDate
setDeliveryDate($user, $delivery_date, $notrigger=0)
Set the planned delivery date.
Definition: fournisseur.commande.class.php:2507
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
ProductFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.product.class.php:40
CommandeFournisseur\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template model.
Definition: fournisseur.commande.class.php:3197
CommandeFournisseurDispatch
Class to manage table commandefournisseurdispatch.
Definition: fournisseur.commande.dispatch.class.php:34
CommonObject\updateRangOfLine
updateRangOfLine($rowid, $rang)
Update position of line (rang)
Definition: commonobject.class.php:3232
CommonObject\fetchObjectLinked
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).
Definition: commonobject.class.php:3920
CommandeFournisseur\$table_ref_field
$table_ref_field
{}
Definition: fournisseur.commande.class.php:90
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4993
CommonOrder
Superclass for orders classes.
Definition: commonorder.class.php:31
CommandeFournisseur\getRights
getRights()
Returns the rights used for this class.
Definition: fournisseur.commande.class.php:3260
CommandeFournisseur\hasDelay
hasDelay()
Is the supplier order delayed? We suppose a purchase ordered as late if a the purchase order has been...
Definition: fournisseur.commande.class.php:3309
ref
$object ref
Definition: info.php:78
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1061
dol_dir_list
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.
Definition: files.lib.php:61
CommandeFournisseurLigne\fetch
fetch($rowid)
Load line order.
Definition: fournisseur.commande.class.php:3626
MultiCurrency\getIdFromCode
static getIdFromCode($dbs, $code)
Get id of currency from code.
Definition: multicurrency.class.php:501
CommandeFournisseur\fetch
fetch($id, $ref='')
Get object and lines from database.
Definition: fournisseur.commande.class.php:337
CommandeFournisseur\getLibStatut
getLibStatut($mode=0)
Return label of the status of object.
Definition: fournisseur.commande.class.php:737
CommonObject\deleteObjectLinked
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='', $f_user=null, $notrigger=0)
Delete all links between an object $this.
Definition: commonobject.class.php:4232
CommonObject\commonReplaceProduct
static commonReplaceProduct(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
Definition: commonobject.class.php:8514
rowid
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
Definition: email_expire_services_to_representatives.php:79
CommandeFournisseur\get_methodes_commande
get_methodes_commande()
Get list of order methods.
Definition: fournisseur.commande.class.php:2293
CommandeFournisseur\dispatchProduct
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...
Definition: fournisseur.commande.class.php:2040
CommandeFournisseur\valid
valid($user, $idwarehouse=0, $notrigger=0)
Validate an order.
Definition: fournisseur.commande.class.php:626
CommandeFournisseur\getDispachedLines
getDispachedLines($status=-1)
Return array of dispatched lines waiting to be approved for this order.
Definition: fournisseur.commande.class.php:2326
CommandeFournisseur\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0)
Return clicable name (with picto eventually)
Definition: fournisseur.commande.class.php:833
CommandeFournisseur\load_state_board
load_state_board()
Charge indicateurs this->nb de tableau de bord.
Definition: fournisseur.commande.class.php:3055
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5823
CommandeFournisseur\replaceProduct
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
Definition: fournisseur.commande.class.php:3293
CommandeFournisseurLigne\insert
insert($notrigger=0)
Insert line into database.
Definition: fournisseur.commande.class.php:3737
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2550
CommandeFournisseur\showDelay
showDelay()
Show the customer delayed info.
Definition: fournisseur.commande.class.php:3342
WorkboardResponse
Definition: workboardresponse.class.php:24
dol_concatdesc
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...
Definition: functions.lib.php:7542
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4024
CommonObject\commonGenerateDocument
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
Definition: commonobject.class.php:5415
calcul_price_total
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.
Definition: price.lib.php:86
dol_delete_file
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.
Definition: files.lib.php:1251
CommandeFournisseur\Livraison
Livraison($user, $date, $type, $comment)
Set a delivery in database for this supplier order.
Definition: fournisseur.commande.class.php:2379
CommonObject\update_price
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).
Definition: commonobject.class.php:3538
CommandeFournisseur\STATUS_ORDERSENT
const STATUS_ORDERSENT
Order sent, shipment on process.
Definition: fournisseur.commande.class.php:284
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:180
CommandeFournisseur\STATUS_VALIDATED
const STATUS_VALIDATED
Validated status.
Definition: fournisseur.commande.class.php:274
CommandeFournisseur\LibStatut
LibStatut($status, $mode=0, $billed=0)
Return label of a status.
Definition: fournisseur.commande.class.php:751
CommandeFournisseur\STATUS_REFUSED
const STATUS_REFUSED
Refused.
Definition: fournisseur.commande.class.php:309
MouvementStock
Class to manage stock movements.
Definition: mouvementstock.class.php:31
CommandeFournisseur\Cancel
Cancel($user, $idwarehouse=-1)
Cancel an approved order.
Definition: fournisseur.commande.class.php:1226
CommandeFournisseur\addline
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.
Definition: fournisseur.commande.class.php:1756
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6173
$resql
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.
Definition: index.php:745
Commande
Class to manage customers orders.
Definition: commande.class.php:46
CommandeFournisseurLigne\update
update($notrigger=0)
Update the line object into db.
Definition: fournisseur.commande.class.php:3889
CommandeFournisseur\replaceThirdparty
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: fournisseur.commande.class.php:3276
dol_string_nohtmltag
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
Definition: functions.lib.php:6880
CommonObject\deleteEcmFiles
deleteEcmFiles($mode=0)
Delete related files of object in database.
Definition: commonobject.class.php:10115
CommonObject\add_contact
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Definition: commonobject.class.php:1128
CommandeFournisseur\classifyBilled
classifyBilled(User $user)
Class invoiced the supplier order.
Definition: fournisseur.commande.class.php:986
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8485
CommandeFournisseur\commande
commande($user, $date, $methode, $comment='')
Submit a supplier order to supplier.
Definition: fournisseur.commande.class.php:1281
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
setEventMessage
setEventMessage($mesgs, $style='mesgs')
Set event message in dol_events session object.
Definition: functions.lib.php:8436
MultiCurrency\getIdAndTxFromCode
static getIdAndTxFromCode($dbs, $code, $date_document='')
Get id and rate of currency from code.
Definition: multicurrency.class.php:526
CommonObject\line_max
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
Definition: commonobject.class.php:3386
CommandeFournisseur\refuse
refuse($user)
Refuse an order.
Definition: fournisseur.commande.class.php:1177
CommandeFournisseur\set_id_projet
set_id_projet($user, $id_projet, $notrigger=0)
Set the id projet.
Definition: fournisseur.commande.class.php:2565
CommandeFournisseur\getMaxDeliveryTimeDay
getMaxDeliveryTimeDay($langs)
Return the max number delivery delay in day.
Definition: fournisseur.commande.class.php:3228
CommandeFournisseur\STATUS_RECEIVED_COMPLETELY
const STATUS_RECEIVED_COMPLETELY
Received completely.
Definition: fournisseur.commande.class.php:294
CommandeFournisseur\createFromClone
createFromClone(User $user, $socid=0, $notrigger=0)
Load an object from its id and create a new one in database.
Definition: fournisseur.commande.class.php:1649
CommonObject\fetch_optionals
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...
Definition: commonobject.class.php:6022
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3887
CommandeFournisseur\SOURCE_ID_REPLENISHMENT
const SOURCE_ID_REPLENISHMENT
The constant used into source field to track the order was generated by the replenishement feature.
Definition: fournisseur.commande.class.php:315
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:137
CommandeFournisseur\getNextNumRef
getNextNumRef($soc)
Returns the following order reference not used depending on the numbering model activated defined wit...
Definition: fournisseur.commande.class.php:940
CommandeFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.commande.class.php:48
User
Class to manage Dolibarr users.
Definition: user.class.php:46
CommandeFournisseur\update
update(User $user, $notrigger=0)
Update Supplier Order.
Definition: fournisseur.commande.class.php:1545
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6133
CommandeFournisseur\STATUS_DRAFT
const STATUS_DRAFT
Draft status.
Definition: fournisseur.commande.class.php:269
Product
Class to manage products or services.
Definition: product.class.php:46
CommandeFournisseur\setStatus
setStatus($user, $status)
Tag order with a particular status.
Definition: fournisseur.commande.class.php:2663
CommonObject\add_object_linked
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
Definition: commonobject.class.php:3822
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10740
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4360
CommandeFournisseur\approve
approve($user, $idwarehouse=0, $secondlevel=0)
Approve a supplier order.
Definition: fournisseur.commande.class.php:1034
CommandeFournisseur\set_date_livraison
set_date_livraison($user, $delivery_date, $notrigger=0)
Set delivery date.
Definition: fournisseur.commande.class.php:2493
CommandeFournisseur\loadReceptions
loadReceptions($filtre_statut=-1)
Load array this->receptions of lines of shipments with nb of products sent for each order line Note: ...
Definition: fournisseur.commande.class.php:3506
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2951
CommandeFournisseur\$fields
$fields
'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortf...
Definition: fournisseur.commande.class.php:217
CommandeFournisseur\updateline
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.
Definition: fournisseur.commande.class.php:2734
price
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.
Definition: functions.lib.php:5697
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5806
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8465
CommandeFournisseur\info
info($id)
Charge les informations d'ordre info dans l'objet facture.
Definition: fournisseur.commande.class.php:3009
CommandeFournisseur\__construct
__construct($db)
Constructor.
Definition: fournisseur.commande.class.php:324
CommandeFournisseur\load_board
load_board($user, $mode='opened')
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
Definition: fournisseur.commande.class.php:3095
CommandeFournisseur\STATUS_RECEIVED_PARTIALLY
const STATUS_RECEIVED_PARTIALLY
Received partially.
Definition: fournisseur.commande.class.php:289
CommonOrderLine
Superclass for orders classes.
Definition: commonorder.class.php:44
CommonObject\line_order
line_order($renum=false, $rowidorder='ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
Definition: commonobject.class.php:3068
CommandeFournisseur\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: fournisseur.commande.class.php:2925
CommandeFournisseurLigne
Class to manage line orders.
Definition: fournisseur.commande.class.php:3557
CommandeFournisseur\getInputMethod
getInputMethod()
Returns the translated input method of object (defined if $this->methode_commande_id > 0).
Definition: fournisseur.commande.class.php:3158
CommandeFournisseur\STATUS_CANCELED_AFTER_ORDER
const STATUS_CANCELED_AFTER_ORDER
Order canceled/never received.
Definition: fournisseur.commande.class.php:304
CommandeFournisseur\calcAndSetStatusDispatch
calcAndSetStatusDispatch(User $user, $closeopenorder=1, $comment='')
Calc status regarding to dispatched stock.
Definition: fournisseur.commande.class.php:3377