dolibarr  13.0.2
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-2020 Frédéric France <frederic.france@netlogic.fr>
13  * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program. If not, see <https://www.gnu.org/licenses/>.
27  */
28 
35 include_once DOL_DOCUMENT_ROOT.'/core/class/commonorder.class.php';
36 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
37 if (!empty($conf->productbatch->enabled)) require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
38 require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
39 
44 {
48  public $element = 'order_supplier';
49 
53  public $table_element = 'commande_fournisseur';
54 
58  public $table_element_line = 'commande_fournisseurdet';
59 
63  public $fk_element = 'fk_commande';
64 
68  public $picto = 'supplier_order';
69 
74  public $ismultientitymanaged = 1;
75 
80  public $restrictiononfksoc = 1;
81 
85  protected $table_ref_field = 'ref';
86 
90  public $id;
91 
96  public $ref;
97 
98  public $ref_supplier;
99  public $brouillon;
100  public $statut; // 0=Draft -> 1=Validated -> 2=Approved -> 3=Ordered/Process runing -> 4=Received partially -> 5=Received totally -> (reopen) 4=Received partially
101  // -> 7=Canceled/Never received -> (reopen) 3=Process runing
102  // -> 6=Canceled -> (reopen) 2=Approved
103  // -> 9=Refused -> (reopen) 1=Validated
104  // Note: billed or not is on another field "billed"
105  public $statuts; // List of status
106 
107  public $billed;
108 
109  public $socid;
110  public $fourn_id;
111  public $date;
112  public $date_valid;
113  public $date_approve;
114  public $date_approve2; // Used when SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED is set
115  public $date_commande;
116 
121  public $date_livraison;
122 
126  public $delivery_date;
127 
128  public $total_ht;
129  public $total_tva;
130  public $total_localtax1; // Total Local tax 1
131  public $total_localtax2; // Total Local tax 2
132  public $total_ttc;
133  public $source;
134 
138  public $fk_project;
139 
140  public $cond_reglement_id;
141  public $cond_reglement_code;
142  public $cond_reglement_label; // Label
143  public $cond_reglement_doc; // Label on documents
144 
148  public $fk_account;
149 
150  public $mode_reglement_id;
151  public $mode_reglement_code;
152  public $user_author_id;
153  public $user_valid_id;
154  public $user_approve_id;
155  public $user_approve_id2; // Used when SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED is set
156 
157  public $extraparams = array();
158 
162  public $lines = array();
163 
164  //Add for supplier_proposal
165  public $origin;
166  public $origin_id;
167  public $linked_objects = array();
168 
169  // Multicurrency
173  public $fk_multicurrency;
174 
175  public $multicurrency_code;
176  public $multicurrency_tx;
177  public $multicurrency_total_ht;
178  public $multicurrency_total_tva;
179  public $multicurrency_total_ttc;
180 
181 
182 
183  public $fields = array(
184  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
185  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>15),
186  'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>20),
187  'ref' =>array('type'=>'varchar(255)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'showoncombobox'=>1, 'position'=>25),
188  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>30, 'index'=>1),
189  'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>0, 'position'=>35),
190  'ref_supplier' =>array('type'=>'varchar(255)', 'label'=>'RefSupplier', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
191  'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label'=>'Fk projet', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
192  'date_creation' =>array('type'=>'datetime', 'label'=>'Date creation', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
193  'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
194  'date_approve' =>array('type'=>'datetime', 'label'=>'Date approve', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
195  'date_approve2' =>array('type'=>'datetime', 'label'=>'Date approve2', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
196  'date_commande' =>array('type'=>'date', 'label'=>'Date commande', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
197  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
198  'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>80),
199  'fk_user_valid' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
200  'fk_user_approve' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserApproval', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
201  'fk_user_approve2' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserSecondApproval', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
202  'source' =>array('type'=>'smallint(6)', 'label'=>'Source', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>100),
203  'billed' =>array('type'=>'smallint(6)', 'label'=>'Billed', 'enabled'=>1, 'visible'=>-1, 'position'=>110),
204  'amount_ht' =>array('type'=>'double(24,8)', 'label'=>'Amount ht', 'enabled'=>1, 'visible'=>-1, 'position'=>115),
205  'remise_percent' =>array('type'=>'double', 'label'=>'Remise percent', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
206  'remise' =>array('type'=>'double', 'label'=>'Remise', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
207  'tva' =>array('type'=>'double(24,8)', 'label'=>'Tva', 'enabled'=>1, 'visible'=>-1, 'position'=>130, 'isameasure'=>1),
208  'localtax1' =>array('type'=>'double(24,8)', 'label'=>'Localtax1', 'enabled'=>1, 'visible'=>-1, 'position'=>135, 'isameasure'=>1),
209  'localtax2' =>array('type'=>'double(24,8)', 'label'=>'Localtax2', 'enabled'=>1, 'visible'=>-1, 'position'=>140, 'isameasure'=>1),
210  'total_ht' =>array('type'=>'double(24,8)', 'label'=>'TotalHT', 'enabled'=>1, 'visible'=>-1, 'position'=>145, 'isameasure'=>1),
211  'total_ttc' =>array('type'=>'double(24,8)', 'label'=>'TotalTTC', 'enabled'=>1, 'visible'=>-1, 'position'=>150, 'isameasure'=>1),
212  'note_private' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>155),
213  'note_public' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>160),
214  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'ModelPDF', 'enabled'=>1, 'visible'=>0, 'position'=>165),
215  'fk_input_method' =>array('type'=>'integer', 'label'=>'InputMethod', 'enabled'=>1, 'visible'=>-1, 'position'=>170),
216  'fk_cond_reglement' =>array('type'=>'integer', 'label'=>'PaymentTerm', 'enabled'=>1, 'visible'=>-1, 'position'=>175),
217  'fk_mode_reglement' =>array('type'=>'integer', 'label'=>'PaymentMode', 'enabled'=>1, 'visible'=>-1, 'position'=>180),
218  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>190),
219  'date_livraison' =>array('type'=>'datetime', 'label'=>'DeliveryDate', 'enabled'=>1, 'visible'=>-1, 'position'=>195),
220  'fk_account' =>array('type'=>'integer', 'label'=>'Fk account', 'enabled'=>1, 'visible'=>-1, 'position'=>200),
221  'fk_incoterms' =>array('type'=>'integer', 'label'=>'IncotermCode', 'enabled'=>1, 'visible'=>-1, 'position'=>205),
222  'location_incoterms' =>array('type'=>'varchar(255)', 'label'=>'IncotermLocation', 'enabled'=>1, 'visible'=>-1, 'position'=>210),
223  'fk_multicurrency' =>array('type'=>'integer', 'label'=>'Fk multicurrency', 'enabled'=>1, 'visible'=>-1, 'position'=>215),
224  'multicurrency_code' =>array('type'=>'varchar(255)', 'label'=>'MulticurrencyCode', 'enabled'=>1, 'visible'=>-1, 'position'=>220),
225  'multicurrency_tx' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyRate', 'enabled'=>1, 'visible'=>-1, 'position'=>225),
226  'multicurrency_total_ht' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyTotalHT', 'enabled'=>1, 'visible'=>-1, 'position'=>230),
227  'multicurrency_total_tva' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyTotalVAT', 'enabled'=>1, 'visible'=>-1, 'position'=>235),
228  'multicurrency_total_ttc' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyTotalTTC', 'enabled'=>1, 'visible'=>-1, 'position'=>240),
229  'last_main_doc' =>array('type'=>'varchar(255)', 'label'=>'LastMainDoc', 'enabled'=>1, 'visible'=>-1, 'position'=>245),
230  'fk_statut' =>array('type'=>'smallint(6)', 'label'=>'Status', 'enabled'=>1, 'visible'=>-1, 'position'=>500),
231  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>900),
232  );
233 
234 
238  const STATUS_DRAFT = 0;
239 
243  const STATUS_VALIDATED = 1;
244 
248  const STATUS_ACCEPTED = 2;
249 
253  const STATUS_ORDERSENT = 3;
254 
259 
264 
268  const STATUS_CANCELED = 6;
269 
274 
278  const STATUS_REFUSED = 9;
279 
280 
281 
282 
288  public function __construct($db)
289  {
290  $this->db = $db;
291  }
292 
293 
301  public function fetch($id, $ref = '')
302  {
303  global $conf;
304 
305  // Check parameters
306  if (empty($id) && empty($ref)) return -1;
307 
308  $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.tva as total_vat,";
309  $sql .= " c.localtax1, c.localtax2, ";
310  $sql .= " c.date_creation, c.date_valid, c.date_approve, c.date_approve2,";
311  $sql .= " c.fk_user_author, c.fk_user_valid, c.fk_user_approve, c.fk_user_approve2,";
312  $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,";
313  $sql .= " c.fk_account,";
314  $sql .= " c.note_private, c.note_public, c.model_pdf, c.extraparams, c.billed,";
315  $sql .= " c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc,";
316  $sql .= " cm.libelle as methode_commande,";
317  $sql .= " cr.code as cond_reglement_code, cr.libelle as cond_reglement_label, cr.libelle_facture as cond_reglement_doc,";
318  $sql .= " p.code as mode_reglement_code, p.libelle as mode_reglement_libelle";
319  $sql .= ', c.fk_incoterms, c.location_incoterms';
320  $sql .= ', i.libelle as label_incoterms';
321  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c";
322  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_payment_term as cr ON c.fk_cond_reglement = cr.rowid";
323  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as p ON c.fk_mode_reglement = p.id";
324  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_input_method as cm ON cm.rowid = c.fk_input_method";
325  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON c.fk_incoterms = i.rowid';
326 
327  if (empty($id)) $sql .= " WHERE c.entity IN (".getEntity('supplier_order').")";
328  else $sql .= " WHERE c.rowid=".$id;
329 
330  if ($ref) $sql .= " AND c.ref='".$this->db->escape($ref)."'";
331 
332  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
333  $resql = $this->db->query($sql);
334  if ($resql)
335  {
336  $obj = $this->db->fetch_object($resql);
337  if (!$obj)
338  {
339  $this->error = 'Bill with id '.$id.' not found';
340  dol_syslog(get_class($this).'::fetch '.$this->error);
341  return 0;
342  }
343 
344  $this->id = $obj->rowid;
345  $this->entity = $obj->entity;
346 
347  $this->ref = $obj->ref;
348  $this->ref_supplier = $obj->ref_supplier;
349  $this->socid = $obj->fk_soc;
350  $this->fourn_id = $obj->fk_soc;
351  $this->statut = $obj->fk_statut;
352  $this->status = $obj->fk_statut;
353  $this->billed = $obj->billed;
354  $this->user_author_id = $obj->fk_user_author;
355  $this->user_valid_id = $obj->fk_user_valid;
356  $this->user_approve_id = $obj->fk_user_approve;
357  $this->user_approve_id2 = $obj->fk_user_approve2;
358  $this->total_ht = $obj->total_ht;
359  $this->total_tva = $obj->total_vat;
360  $this->total_localtax1 = $obj->localtax1;
361  $this->total_localtax2 = $obj->localtax2;
362  $this->total_ttc = $obj->total_ttc;
363  $this->date = $this->db->jdate($obj->date_creation);
364  $this->date_valid = $this->db->jdate($obj->date_valid);
365  $this->date_approve = $this->db->jdate($obj->date_approve);
366  $this->date_approve2 = $this->db->jdate($obj->date_approve2);
367  $this->date_commande = $this->db->jdate($obj->date_commande); // date we make the order to supplier
368  $this->date_livraison = $this->db->jdate($obj->delivery_date); // deprecated
369  $this->delivery_date = $this->db->jdate($obj->delivery_date);
370  $this->remise_percent = $obj->remise_percent;
371  $this->methode_commande_id = $obj->fk_input_method;
372  $this->methode_commande = $obj->methode_commande;
373 
374  $this->source = $obj->source;
375  $this->fk_project = $obj->fk_project;
376  $this->cond_reglement_id = $obj->fk_cond_reglement;
377  $this->cond_reglement_code = $obj->cond_reglement_code;
378  $this->cond_reglement = $obj->cond_reglement_label; // deprecated
379  $this->cond_reglement_label = $obj->cond_reglement_label;
380  $this->cond_reglement_doc = $obj->cond_reglement_doc;
381  $this->fk_account = $obj->fk_account;
382  $this->mode_reglement_id = $obj->fk_mode_reglement;
383  $this->mode_reglement_code = $obj->mode_reglement_code;
384  $this->mode_reglement = $obj->mode_reglement_libelle;
385  $this->note = $obj->note_private; // deprecated
386  $this->note_private = $obj->note_private;
387  $this->note_public = $obj->note_public;
388  $this->model_pdf = $obj->model_pdf;
389  $this->modelpdf = $obj->model_pdf; // deprecated
390 
391  //Incoterms
392  $this->fk_incoterms = $obj->fk_incoterms;
393  $this->location_incoterms = $obj->location_incoterms;
394  $this->label_incoterms = $obj->label_incoterms;
395 
396  // Multicurrency
397  $this->fk_multicurrency = $obj->fk_multicurrency;
398  $this->multicurrency_code = $obj->multicurrency_code;
399  $this->multicurrency_tx = $obj->multicurrency_tx;
400  $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
401  $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
402  $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
403 
404  $this->extraparams = (array) json_decode($obj->extraparams, true);
405 
406  $this->db->free($resql);
407 
408  // Retrieve all extrafield
409  // fetch optionals attributes and labels
410  $this->fetch_optionals();
411 
412  if ($this->statut == 0) $this->brouillon = 1;
413 
414  /*
415  * Lines
416  */
417  $result = $this->fetch_lines();
418  if ($result < 0)
419  {
420  return -1;
421  } else {
422  return 1;
423  }
424  } else {
425  $this->error = $this->db->error()." sql=".$sql;
426  return -1;
427  }
428  }
429 
430  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
437  public function fetch_lines($only_product = 0)
438  {
439  global $conf;
440  // phpcs:enable
441  //$result=$this->fetch_lines();
442  $this->lines = array();
443 
444  $sql = "SELECT l.rowid, l.ref as ref_supplier, l.fk_product, l.product_type, l.label, l.description, l.qty,";
445  $sql .= " l.vat_src_code, l.tva_tx, l.remise_percent, l.subprice,";
446  $sql .= " l.localtax1_tx, l. localtax2_tx, l.localtax1_type, l. localtax2_type, l.total_localtax1, l.total_localtax2,";
447  $sql .= " l.total_ht, l.total_tva, l.total_ttc, l.special_code, l.fk_parent_line, l.rang,";
448  $sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc,";
449  $sql .= " l.fk_unit,";
450  $sql .= " l.date_start, l.date_end,";
451  $sql .= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc';
452  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING))
453  $sql .= ", pfp.rowid as fk_pfp, pfp.packaging";
454  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as l";
455  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
456  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING))
457  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON l.fk_product = pfp.fk_product and l.ref = pfp.ref_fourn";
458  $sql .= " WHERE l.fk_commande = ".$this->id;
459  if ($only_product) $sql .= ' AND p.fk_product_type = 0';
460  $sql .= " ORDER BY l.rang, l.rowid";
461  //print $sql;
462 
463  dol_syslog(get_class($this)."::fetch get lines", LOG_DEBUG);
464  $result = $this->db->query($sql);
465  if ($result)
466  {
467  $num = $this->db->num_rows($result);
468  $i = 0;
469 
470  while ($i < $num)
471  {
472  $objp = $this->db->fetch_object($result);
473 
474  $line = new CommandeFournisseurLigne($this->db);
475 
476  $line->id = $objp->rowid;
477  $line->desc = $objp->description;
478  $line->description = $objp->description;
479  $line->qty = $objp->qty;
480  $line->tva_tx = $objp->tva_tx;
481  $line->localtax1_tx = $objp->localtax1_tx;
482  $line->localtax2_tx = $objp->localtax2_tx;
483  $line->localtax1_type = $objp->localtax1_type;
484  $line->localtax2_type = $objp->localtax2_type;
485  $line->subprice = $objp->subprice;
486  $line->pu_ht = $objp->subprice;
487  $line->remise_percent = $objp->remise_percent;
488 
489  $line->vat_src_code = $objp->vat_src_code;
490  $line->total_ht = $objp->total_ht;
491  $line->total_tva = $objp->total_tva;
492  $line->total_localtax1 = $objp->total_localtax1;
493  $line->total_localtax2 = $objp->total_localtax2;
494  $line->total_ttc = $objp->total_ttc;
495  $line->product_type = $objp->product_type;
496 
497  $line->fk_product = $objp->fk_product;
498 
499  $line->libelle = $objp->product_label; // deprecated
500  $line->product_label = $objp->product_label;
501  $line->product_desc = $objp->product_desc;
502 
503  $line->ref = $objp->product_ref; // Ref of product
504  $line->product_ref = $objp->product_ref; // Ref of product
505  $line->ref_fourn = $objp->ref_supplier; // The supplier ref of price when product was added. May have change since
506  $line->ref_supplier = $objp->ref_supplier; // The supplier ref of price when product was added. May have change since
507 
508  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING))
509  {
510  $line->fk_fournprice = $objp->fk_pfp;
511  $line->packaging = $objp->packaging;
512  }
513 
514  $line->date_start = $this->db->jdate($objp->date_start);
515  $line->date_end = $this->db->jdate($objp->date_end);
516  $line->fk_unit = $objp->fk_unit;
517 
518  // Multicurrency
519  $line->fk_multicurrency = $objp->fk_multicurrency;
520  $line->multicurrency_code = $objp->multicurrency_code;
521  $line->multicurrency_subprice = $objp->multicurrency_subprice;
522  $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
523  $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
524  $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
525 
526  $line->special_code = $objp->special_code;
527  $line->fk_parent_line = $objp->fk_parent_line;
528 
529  $line->rang = $objp->rang;
530 
531  // Retrieve all extrafield
532  // fetch optionals attributes and labels
533  $line->fetch_optionals();
534 
535  $this->lines[$i] = $line;
536 
537  $i++;
538  }
539  $this->db->free($result);
540 
541  return $num;
542  } else {
543  $this->error = $this->db->error()." sql=".$sql;
544  return -1;
545  }
546  }
547 
556  public function valid($user, $idwarehouse = 0, $notrigger = 0)
557  {
558  global $langs, $conf;
559  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
560 
561  $error = 0;
562 
563  dol_syslog(get_class($this)."::valid");
564  $result = 0;
565  if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->fournisseur->commande->creer))
566  || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->fournisseur->supplier_order_advance->validate)))
567  {
568  $this->db->begin();
569 
570  // Definition of supplier order numbering model name
571  $soc = new Societe($this->db);
572  $soc->fetch($this->fourn_id);
573 
574  // Check if object has a temporary ref
575  if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) // empty should not happened, but when it occurs, the test save life
576  {
577  $num = $this->getNextNumRef($soc);
578  } else {
579  $num = $this->ref;
580  }
581  $this->newref = dol_sanitizeFileName($num);
582 
583  $sql = 'UPDATE '.MAIN_DB_PREFIX."commande_fournisseur";
584  $sql .= " SET ref='".$this->db->escape($num)."',";
585  $sql .= " fk_statut = ".self::STATUS_VALIDATED.",";
586  $sql .= " date_valid='".$this->db->idate(dol_now())."',";
587  $sql .= " fk_user_valid = ".$user->id;
588  $sql .= " WHERE rowid = ".$this->id;
589  $sql .= " AND fk_statut = ".self::STATUS_DRAFT;
590 
591  $resql = $this->db->query($sql);
592  if (!$resql)
593  {
594  dol_print_error($this->db);
595  $error++;
596  }
597 
598  if (!$error && !$notrigger)
599  {
600  // Call trigger
601  $result = $this->call_trigger('ORDER_SUPPLIER_VALIDATE', $user);
602  if ($result < 0) $error++;
603  // End call triggers
604  }
605 
606  if (!$error)
607  {
608  $this->oldref = $this->ref;
609 
610  // Rename directory if dir was a temporary ref
611  if (preg_match('/^[\(]?PROV/i', $this->ref))
612  {
613  // Now we rename also files into index
614  $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)."'";
615  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'fournisseur/commande/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
616  $resql = $this->db->query($sql);
617  if (!$resql) { $error++; $this->error = $this->db->lasterror(); }
618 
619  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
620  $oldref = dol_sanitizeFileName($this->ref);
621  $newref = dol_sanitizeFileName($num);
622  $dirsource = $conf->fournisseur->commande->dir_output.'/'.$oldref;
623  $dirdest = $conf->fournisseur->commande->dir_output.'/'.$newref;
624  if (!$error && file_exists($dirsource))
625  {
626  dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
627 
628  if (@rename($dirsource, $dirdest))
629  {
630  dol_syslog("Rename ok");
631  // Rename docs starting with $oldref with $newref
632  $listoffiles = dol_dir_list($conf->fournisseur->commande->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
633  foreach ($listoffiles as $fileentry)
634  {
635  $dirsource = $fileentry['name'];
636  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
637  $dirsource = $fileentry['path'].'/'.$dirsource;
638  $dirdest = $fileentry['path'].'/'.$dirdest;
639  @rename($dirsource, $dirdest);
640  }
641  }
642  }
643  }
644  }
645 
646  if (!$error)
647  {
648  $result = 1;
649  $this->statut = self::STATUS_VALIDATED;
650  $this->ref = $num;
651  }
652 
653  if (!$error)
654  {
655  $this->db->commit();
656  return 1;
657  } else {
658  $this->db->rollback();
659  return -1;
660  }
661  } else {
662  $this->error = 'NotAuthorized';
663  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
664  return -1;
665  }
666  }
667 
674  public function getLibStatut($mode = 0)
675  {
676  return $this->LibStatut($this->statut, $mode, $this->billed);
677  }
678 
679  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
688  public function LibStatut($status, $mode = 0, $billed = 0)
689  {
690  // phpcs:enable
691  global $conf, $langs;
692 
693  if (empty($this->statuts) || empty($this->statutshort)) {
694  $langs->load('orders');
695 
696  $this->statuts[0] = 'StatusSupplierOrderDraft';
697  $this->statuts[1] = 'StatusSupplierOrderValidated';
698  $this->statuts[2] = 'StatusSupplierOrderApproved';
699  if (empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) $this->statuts[3] = 'StatusSupplierOrderOnProcess';
700  else $this->statuts[3] = 'StatusSupplierOrderOnProcessWithValidation';
701  $this->statuts[4] = 'StatusSupplierOrderReceivedPartially';
702  $this->statuts[5] = 'StatusSupplierOrderReceivedAll';
703  $this->statuts[6] = 'StatusSupplierOrderCanceled'; // Approved->Canceled
704  $this->statuts[7] = 'StatusSupplierOrderCanceled'; // Process running->canceled
705  $this->statuts[9] = 'StatusSupplierOrderRefused';
706 
707  // List of language codes for status
708  $this->statutshort[0] = 'StatusSupplierOrderDraftShort';
709  $this->statutshort[1] = 'StatusSupplierOrderValidatedShort';
710  $this->statutshort[2] = 'StatusSupplierOrderApprovedShort';
711  $this->statutshort[3] = 'StatusSupplierOrderOnProcessShort';
712  $this->statutshort[4] = 'StatusSupplierOrderReceivedPartiallyShort';
713  $this->statutshort[5] = 'StatusSupplierOrderReceivedAllShort';
714  $this->statutshort[6] = 'StatusSupplierOrderCanceledShort';
715  $this->statutshort[7] = 'StatusSupplierOrderCanceledShort';
716  $this->statutshort[9] = 'StatusSupplierOrderRefusedShort';
717  }
718 
719  $statustrans = array(
720  0 => 'status0',
721  1 => 'status1b',
722  2 => 'status1',
723  3 => 'status4',
724  4 => 'status4b',
725  5 => 'status6',
726  6 => 'status9',
727  7 => 'status9',
728  9 => 'status9',
729  );
730 
731  $statusClass = 'status0';
732  if (!empty($statustrans[$status])) {
733  $statusClass = $statustrans[$status];
734  }
735 
736  $billedtext = '';
737  if ($billed) {
738  $billedtext = ' - '.$langs->trans("Billed");
739  }
740  if ($status == 5 && $billed) $statusClass = 'status6';
741 
742  $statusLong = $langs->trans($this->statuts[$status]).$billedtext;
743  $statusShort = $langs->trans($this->statutshort[$status]);
744 
745  return dolGetStatus($statusLong, $statusShort, '', $statusClass, $mode);
746  }
747 
748 
759  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
760  {
761  global $langs, $conf, $user;
762 
763  $result = '';
764 
765  $label = '';
766 
767  if ($user->rights->fournisseur->commande->lire) {
768  $label = '<u class="paddingrightonly">'.$langs->trans("SupplierOrder").'</u>';
769  if (isset($this->statut)) {
770  $label .= ' '.$this->getLibStatut(5);
771  }
772  if (!empty($this->ref)) {
773  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
774  }
775  if (!empty($this->ref_supplier)) {
776  $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
777  }
778  if (!empty($this->total_ht)) {
779  $label .= '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
780  }
781  if (!empty($this->total_tva)) {
782  $label .= '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
783  }
784  if (!empty($this->total_ttc)) {
785  $label .= '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
786  }
787  if (!empty($this->delivery_date)) {
788  $label .= '<br><b>'.$langs->trans('DeliveryDate').':</b> '.dol_print_date($this->delivery_date, 'dayhour');
789  }
790  }
791 
792  $picto = 'order';
793  $url = DOL_URL_ROOT.'/fourn/commande/card.php?id='.$this->id;
794 
795  if ($option !== 'nolink')
796  {
797  // Add param to save lastsearch_values or not
798  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
799  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
800  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
801  }
802 
803  $linkclose = '';
804  if (empty($notooltip))
805  {
806  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
807  {
808  $label = $langs->trans("ShowOrder");
809  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
810  }
811  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
812  $linkclose .= ' class="classfortooltip"';
813  }
814 
815  $linkstart = '<a href="'.$url.'"';
816  $linkstart .= $linkclose.'>';
817  $linkend = '</a>';
818 
819  $result .= $linkstart;
820  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
821  if ($withpicto != 2) $result .= $this->ref;
822  $result .= $linkend;
823 
824  if ($addlinktonotes)
825  {
826  $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
827  if ($txttoshow)
828  {
829  $notetoshow = $langs->trans("ViewPrivateNote").':<br>'.dol_string_nohtmltag($txttoshow, 1);
830  $result .= ' <span class="note inline-block">';
831  $result .= '<a href="'.DOL_URL_ROOT.'/fourn/commande/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($notetoshow).'">';
832  $result .= img_picto('', 'note');
833  $result .= '</a>';
834  //$result.=img_picto($langs->trans("ViewNote"),'object_generic');
835  //$result.='</a>';
836  $result .= '</span>';
837  }
838  }
839 
840  return $result;
841  }
842 
843 
851  public function getNextNumRef($soc)
852  {
853  global $db, $langs, $conf;
854  $langs->load("orders");
855 
856  if (!empty($conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER))
857  {
858  $mybool = false;
859 
860  $file = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER.'.php';
861  $classname = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER;
862 
863  // Include file with class
864  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
865 
866  foreach ($dirmodels as $reldir) {
867  $dir = dol_buildpath($reldir."core/modules/supplier_order/");
868 
869  // Load file with numbering class (if found)
870  $mybool |= @include_once $dir.$file;
871  }
872 
873  if ($mybool === false) {
874  dol_print_error('', "Failed to include file ".$file);
875  return '';
876  }
877 
878  $obj = new $classname();
879  $numref = $obj->getNextValue($soc, $this);
880 
881  if ($numref != "")
882  {
883  return $numref;
884  } else {
885  $this->error = $obj->error;
886  return -1;
887  }
888  } else {
889  $this->error = "Error_COMMANDE_SUPPLIER_ADDON_NotDefined";
890  return -2;
891  }
892  }
899  public function classifyBilled(User $user)
900  {
901  $error = 0;
902 
903  if ($this->billed)
904  {
905  return 0;
906  }
907 
908  $this->db->begin();
909 
910  $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande_fournisseur SET billed = 1';
911  $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut > '.self::STATUS_DRAFT;
912 
913  if ($this->db->query($sql))
914  {
915  if (!$error)
916  {
917  // Call trigger
918  $result = $this->call_trigger('ORDER_SUPPLIER_CLASSIFY_BILLED', $user);
919  if ($result < 0) $error++;
920  // End call triggers
921  }
922 
923  if (!$error)
924  {
925  $this->billed = 1;
926 
927  $this->db->commit();
928  return 1;
929  } else {
930  $this->db->rollback();
931  return -1;
932  }
933  } else {
934  dol_print_error($this->db);
935 
936  $this->db->rollback();
937  return -1;
938  }
939  }
940 
949  public function approve($user, $idwarehouse = 0, $secondlevel = 0)
950  {
951  global $langs, $conf;
952  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
953 
954  $error = 0;
955 
956  dol_syslog(get_class($this)."::approve");
957 
958  if ($user->rights->fournisseur->commande->approuver)
959  {
960  $now = dol_now();
961 
962  $this->db->begin();
963 
964  // Definition of order numbering model name
965  $soc = new Societe($this->db);
966  $soc->fetch($this->fourn_id);
967 
968  // Check if object has a temporary ref
969  if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) // empty should not happened, but when it occurs, the test save life
970  {
971  $num = $this->getNextNumRef($soc);
972  } else {
973  $num = $this->ref;
974  }
975  $this->newref = dol_sanitizeFileName($num);
976 
977  // Do we have to change status now ? (If double approval is required and first approval, we keep status to 1 = validated)
978  $movetoapprovestatus = true;
979  $comment = '';
980 
981  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
982  $sql .= " SET ref='".$this->db->escape($num)."',";
983  if (empty($secondlevel)) // standard or first level approval
984  {
985  $sql .= " date_approve='".$this->db->idate($now)."',";
986  $sql .= " fk_user_approve = ".$user->id;
987  if (!empty($conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED) && $conf->global->MAIN_FEATURES_LEVEL > 0 && $this->total_ht >= $conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED)
988  {
989  if (empty($this->user_approve_id2))
990  {
991  $movetoapprovestatus = false; // second level approval not done
992  $comment = ' (first level)';
993  }
994  }
995  } else // request a second level approval
996  {
997  $sql .= " date_approve2='".$this->db->idate($now)."',";
998  $sql .= " fk_user_approve2 = ".$user->id;
999  if (empty($this->user_approve_id)) $movetoapprovestatus = false; // first level approval not done
1000  $comment = ' (second level)';
1001  }
1002  // If double approval is required and first approval, we keep status to 1 = validated
1003  if ($movetoapprovestatus) $sql .= ", fk_statut = ".self::STATUS_ACCEPTED;
1004  else $sql .= ", fk_statut = ".self::STATUS_VALIDATED;
1005  $sql .= " WHERE rowid = ".$this->id;
1006  $sql .= " AND fk_statut = ".self::STATUS_VALIDATED;
1007 
1008  if ($this->db->query($sql))
1009  {
1010  if (!empty($conf->global->SUPPLIER_ORDER_AUTOADD_USER_CONTACT))
1011  {
1012  $result = $this->add_contact($user->id, 'SALESREPFOLL', 'internal', 1);
1013  if ($result < 0 && $result != -2) // -2 means already exists
1014  {
1015  $error++;
1016  }
1017  }
1018 
1019  // If stock is incremented on validate order, we must increment it
1020  if (!$error && $movetoapprovestatus && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER))
1021  {
1022  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
1023  $langs->load("agenda");
1024 
1025  $cpt = count($this->lines);
1026  for ($i = 0; $i < $cpt; $i++)
1027  {
1028  // Product with reference
1029  if ($this->lines[$i]->fk_product > 0)
1030  {
1031  $this->line = $this->lines[$i];
1032  $mouvP = new MouvementStock($this->db);
1033  $mouvP->origin = &$this;
1034  // We decrement stock of product (and sub-products)
1035  $up_ht_disc = $this->lines[$i]->subprice;
1036  if (!empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) $up_ht_disc = price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU');
1037  $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("OrderApprovedInDolibarr", $this->ref));
1038  if ($result < 0) { $error++; }
1039  unset($this->line);
1040  }
1041  }
1042  }
1043 
1044  if (!$error)
1045  {
1046  // Call trigger
1047  $result = $this->call_trigger('ORDER_SUPPLIER_APPROVE', $user);
1048  if ($result < 0) $error++;
1049  // End call triggers
1050  }
1051 
1052  if (!$error)
1053  {
1054  $this->ref = $this->newref;
1055 
1056  if ($movetoapprovestatus) $this->statut = self::STATUS_ACCEPTED;
1057  else $this->statut = self::STATUS_VALIDATED;
1058  if (empty($secondlevel)) // standard or first level approval
1059  {
1060  $this->date_approve = $now;
1061  $this->user_approve_id = $user->id;
1062  } else // request a second level approval
1063  {
1064  $this->date_approve2 = $now;
1065  $this->user_approve_id2 = $user->id;
1066  }
1067 
1068  $this->db->commit();
1069  return 1;
1070  } else {
1071  $this->db->rollback();
1072  return -1;
1073  }
1074  } else {
1075  $this->db->rollback();
1076  $this->error = $this->db->lasterror();
1077  return -1;
1078  }
1079  } else {
1080  dol_syslog(get_class($this)."::approve Not Authorized", LOG_ERR);
1081  }
1082  return -1;
1083  }
1084 
1091  public function refuse($user)
1092  {
1093  global $conf, $langs;
1094 
1095  $error = 0;
1096 
1097  dol_syslog(get_class($this)."::refuse");
1098  $result = 0;
1099  if ($user->rights->fournisseur->commande->approuver)
1100  {
1101  $this->db->begin();
1102 
1103  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET fk_statut = ".self::STATUS_REFUSED;
1104  $sql .= " WHERE rowid = ".$this->id;
1105 
1106  if ($this->db->query($sql))
1107  {
1108  $result = 0;
1109 
1110  if ($error == 0)
1111  {
1112  // Call trigger
1113  $result = $this->call_trigger('ORDER_SUPPLIER_REFUSE', $user);
1114  if ($result < 0)
1115  {
1116  $error++;
1117  $this->db->rollback();
1118  } else $this->db->commit();
1119  // End call triggers
1120  }
1121  } else {
1122  $this->db->rollback();
1123  $this->error = $this->db->lasterror();
1124  dol_syslog(get_class($this)."::refuse Error -1");
1125  $result = -1;
1126  }
1127  } else {
1128  dol_syslog(get_class($this)."::refuse Not Authorized");
1129  }
1130  return $result;
1131  }
1132 
1133  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1142  public function Cancel($user, $idwarehouse = -1)
1143  {
1144  // phpcs:enable
1145  global $langs, $conf;
1146 
1147  $error = 0;
1148 
1149  //dol_syslog("CommandeFournisseur::Cancel");
1150  $result = 0;
1151  if ($user->rights->fournisseur->commande->commander)
1152  {
1153  $statut = self::STATUS_CANCELED;
1154 
1155  $this->db->begin();
1156 
1157  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET fk_statut = ".$statut;
1158  $sql .= " WHERE rowid = ".$this->id;
1159  dol_syslog(get_class($this)."::cancel", LOG_DEBUG);
1160  if ($this->db->query($sql))
1161  {
1162  $result = 0;
1163 
1164  // Call trigger
1165  $result = $this->call_trigger('ORDER_SUPPLIER_CANCEL', $user);
1166  if ($result < 0) $error++;
1167  // End call triggers
1168 
1169  if ($error == 0)
1170  {
1171  $this->db->commit();
1172  return 1;
1173  } else {
1174  $this->db->rollback();
1175  return -1;
1176  }
1177  } else {
1178  $this->db->rollback();
1179  $this->error = $this->db->lasterror();
1180  dol_syslog(get_class($this)."::cancel ".$this->error);
1181  return -1;
1182  }
1183  } else {
1184  dol_syslog(get_class($this)."::cancel Not Authorized");
1185  return -1;
1186  }
1187  }
1188 
1198  public function commande($user, $date, $methode, $comment = '')
1199  {
1200  global $langs;
1201  dol_syslog(get_class($this)."::commande");
1202  $error = 0;
1203  if ($user->rights->fournisseur->commande->commander)
1204  {
1205  $this->db->begin();
1206 
1207  $newnoteprivate = $this->note_private;
1208  if ($comment) $newnoteprivate = dol_concatdesc($newnoteprivate, $langs->trans("Comment").': '.$comment);
1209 
1210  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
1211  $sql .= " SET fk_statut=".self::STATUS_ORDERSENT.", fk_input_method=".$methode.", date_commande='".$this->db->idate($date)."', ";
1212  $sql .= " note_private='".$this->db->escape($newnoteprivate)."'";
1213  $sql .= " WHERE rowid=".$this->id;
1214 
1215  dol_syslog(get_class($this)."::commande", LOG_DEBUG);
1216  if ($this->db->query($sql))
1217  {
1218  $this->statut = self::STATUS_ORDERSENT;
1219  $this->methode_commande_id = $methode;
1220  $this->date_commande = $date;
1221  $this->context = array('comments' => $comment);
1222 
1223  // Call trigger
1224  $result = $this->call_trigger('ORDER_SUPPLIER_SUBMIT', $user);
1225  if ($result < 0) $error++;
1226  // End call triggers
1227  } else {
1228  $error++;
1229  $this->error = $this->db->lasterror();
1230  $this->errors[] = $this->db->lasterror();
1231  }
1232 
1233  if (!$error)
1234  {
1235  $this->db->commit();
1236  } else {
1237  $this->db->rollback();
1238  }
1239  } else {
1240  $error++;
1241  $this->error = $langs->trans('NotAuthorized');
1242  $this->errors[] = $langs->trans('NotAuthorized');
1243  dol_syslog(get_class($this)."::commande User not Authorized", LOG_WARNING);
1244  }
1245 
1246  return ($error ? -1 : 1);
1247  }
1248 
1256  public function create($user, $notrigger = 0)
1257  {
1258  global $langs, $conf, $hookmanager;
1259 
1260  $this->db->begin();
1261 
1262  $error = 0;
1263  $now = dol_now();
1264 
1265  // set tmp vars
1266  $date = ($this->date_commande ? $this->date_commande : $this->date); // in case of date is set
1267  if (empty($date)) $date = $now;
1268  $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
1269 
1270  // Clean parameters
1271  if (empty($this->source)) $this->source = 0;
1272 
1273  // Multicurrency (test on $this->multicurrency_tx because we should take the default rate only if not using origin rate)
1274  if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code, $date);
1275  else $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
1276  if (empty($this->fk_multicurrency))
1277  {
1278  $this->multicurrency_code = $conf->currency;
1279  $this->fk_multicurrency = 0;
1280  $this->multicurrency_tx = 1;
1281  }
1282 
1283  // We set order into draft status
1284  $this->brouillon = 1;
1285 
1286  $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur (";
1287  $sql .= "ref";
1288  $sql .= ", ref_supplier";
1289  $sql .= ", note_private";
1290  $sql .= ", note_public";
1291  $sql .= ", entity";
1292  $sql .= ", fk_soc";
1293  $sql .= ", fk_projet";
1294  $sql .= ", date_creation";
1295  $sql .= ", date_livraison";
1296  $sql .= ", fk_user_author";
1297  $sql .= ", fk_statut";
1298  $sql .= ", source";
1299  $sql .= ", model_pdf";
1300  $sql .= ", fk_mode_reglement";
1301  $sql .= ", fk_cond_reglement";
1302  $sql .= ", fk_account";
1303  $sql .= ", fk_incoterms, location_incoterms";
1304  $sql .= ", fk_multicurrency";
1305  $sql .= ", multicurrency_code";
1306  $sql .= ", multicurrency_tx";
1307  $sql .= ") ";
1308  $sql .= " VALUES (";
1309  $sql .= "''";
1310  $sql .= ", '".$this->db->escape($this->ref_supplier)."'";
1311  $sql .= ", '".$this->db->escape($this->note_private)."'";
1312  $sql .= ", '".$this->db->escape($this->note_public)."'";
1313  $sql .= ", ".$conf->entity;
1314  $sql .= ", ".$this->socid;
1315  $sql .= ", ".($this->fk_project > 0 ? $this->fk_project : "null");
1316  $sql .= ", '".$this->db->idate($date)."'";
1317  $sql .= ", ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : "null");
1318  $sql .= ", ".$user->id;
1319  $sql .= ", ".self::STATUS_DRAFT;
1320  $sql .= ", ".$this->db->escape($this->source);
1321  $sql .= ", '".$this->db->escape($conf->global->COMMANDE_SUPPLIER_ADDON_PDF)."'";
1322  $sql .= ", ".($this->mode_reglement_id > 0 ? $this->mode_reglement_id : 'null');
1323  $sql .= ", ".($this->cond_reglement_id > 0 ? $this->cond_reglement_id : 'null');
1324  $sql .= ", ".($this->fk_account > 0 ? $this->fk_account : 'NULL');
1325  $sql .= ", ".(int) $this->fk_incoterms;
1326  $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
1327  $sql .= ", ".(int) $this->fk_multicurrency;
1328  $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
1329  $sql .= ", ".(double) $this->multicurrency_tx;
1330  $sql .= ")";
1331 
1332  dol_syslog(get_class($this)."::create", LOG_DEBUG);
1333  if ($this->db->query($sql))
1334  {
1335  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."commande_fournisseur");
1336 
1337  if ($this->id) {
1338  $num = count($this->lines);
1339 
1340  // insert products details into database
1341  for ($i = 0; $i < $num; $i++)
1342  {
1343  $this->special_code = $this->lines[$i]->special_code; // TODO : remove this in 9.0 and add special_code param to addline()
1344 
1345  // This include test on qty if option SUPPLIER_ORDER_WITH_NOPRICEDEFINED is not set
1346  $result = $this->addline(
1347  $this->lines[$i]->desc,
1348  $this->lines[$i]->subprice,
1349  $this->lines[$i]->qty,
1350  $this->lines[$i]->tva_tx,
1351  $this->lines[$i]->localtax1_tx,
1352  $this->lines[$i]->localtax2_tx,
1353  $this->lines[$i]->fk_product,
1354  0,
1355  $this->lines[$i]->ref_fourn, // $this->lines[$i]->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
1356  $this->lines[$i]->remise_percent,
1357  'HT',
1358  0,
1359  $this->lines[$i]->product_type,
1360  $this->lines[$i]->info_bits,
1361  false,
1362  $this->lines[$i]->date_start,
1363  $this->lines[$i]->date_end,
1364  $this->lines[$i]->array_options,
1365  $this->lines[$i]->fk_unit
1366  );
1367  if ($result < 0)
1368  {
1369  dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING); // do not use dol_print_error here as it may be a functionnal error
1370  $this->db->rollback();
1371  return -1;
1372  }
1373  }
1374 
1375  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
1376  $sql .= " SET ref='(PROV".$this->id.")'";
1377  $sql .= " WHERE rowid=".$this->id;
1378  dol_syslog(get_class($this)."::create", LOG_DEBUG);
1379  if ($this->db->query($sql))
1380  {
1381  // Add link with price request and supplier order
1382  if ($this->id)
1383  {
1384  $this->ref = "(PROV".$this->id.")";
1385 
1386  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) // To use new linkedObjectsIds instead of old linked_objects
1387  {
1388  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
1389  }
1390 
1391  // Add object linked
1392  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects))
1393  {
1394  foreach ($this->linked_objects as $origin => $tmp_origin_id)
1395  {
1396  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, ...))
1397  {
1398  foreach ($tmp_origin_id as $origin_id)
1399  {
1400  $ret = $this->add_object_linked($origin, $origin_id);
1401  if (!$ret)
1402  {
1403  dol_print_error($this->db);
1404  $error++;
1405  }
1406  }
1407  } else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
1408  {
1409  $origin_id = $tmp_origin_id;
1410  $ret = $this->add_object_linked($origin, $origin_id);
1411  if (!$ret)
1412  {
1413  dol_print_error($this->db);
1414  $error++;
1415  }
1416  }
1417  }
1418  }
1419  }
1420 
1421  if (!$error)
1422  {
1423  $result = $this->insertExtraFields();
1424  if ($result < 0) $error++;
1425  }
1426 
1427  if (!$error && !$notrigger)
1428  {
1429  // Call trigger
1430  $result = $this->call_trigger('ORDER_SUPPLIER_CREATE', $user);
1431  if ($result < 0)
1432  {
1433  $this->db->rollback();
1434  return -1;
1435  }
1436  // End call triggers
1437  }
1438 
1439  $this->db->commit();
1440  return $this->id;
1441  } else {
1442  $this->error = $this->db->lasterror();
1443  $this->db->rollback();
1444  return -2;
1445  }
1446  }
1447  } else {
1448  $this->error = $this->db->lasterror();
1449  $this->db->rollback();
1450  return -1;
1451  }
1452  }
1453 
1461  public function createFromClone(User $user, $socid = 0)
1462  {
1463  global $conf, $user, $hookmanager;
1464 
1465  $error = 0;
1466 
1467  $this->db->begin();
1468 
1469  // get extrafields so they will be clone
1470  foreach ($this->lines as $line)
1471  $line->fetch_optionals();
1472 
1473  // Load source object
1474  $objFrom = clone $this;
1475 
1476  // Change socid if needed
1477  if (!empty($socid) && $socid != $this->socid)
1478  {
1479  $objsoc = new Societe($this->db);
1480 
1481  if ($objsoc->fetch($socid) > 0)
1482  {
1483  $this->socid = $objsoc->id;
1484  $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1485  $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1486  $this->fk_project = 0;
1487  $this->fk_delivery_address = 0;
1488  }
1489 
1490  // TODO Change product price if multi-prices
1491  }
1492 
1493  $this->id = 0;
1494  $this->statut = self::STATUS_DRAFT;
1495 
1496  // Clear fields
1497  $this->user_author_id = $user->id;
1498  $this->user_valid = '';
1499  $this->date_creation = '';
1500  $this->date_validation = '';
1501  $this->ref_supplier = '';
1502  $this->user_approve_id = '';
1503  $this->user_approve_id2 = '';
1504  $this->date_approve = '';
1505  $this->date_approve2 = '';
1506 
1507  // Create clone
1508  $this->context['createfromclone'] = 'createfromclone';
1509  $result = $this->create($user);
1510  if ($result < 0) $error++;
1511 
1512  if (!$error)
1513  {
1514  // Hook of thirdparty module
1515  if (is_object($hookmanager))
1516  {
1517  $parameters = array('objFrom'=>$objFrom);
1518  $action = '';
1519  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1520  if ($reshook < 0) $error++;
1521  }
1522  }
1523 
1524  unset($this->context['createfromclone']);
1525 
1526  // End
1527  if (!$error)
1528  {
1529  $this->db->commit();
1530  return $this->id;
1531  } else {
1532  $this->db->rollback();
1533  return -1;
1534  }
1535  }
1536 
1564  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)
1565  {
1566  global $langs, $mysoc, $conf;
1567 
1568  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");
1569  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1570 
1571  if ($this->statut == self::STATUS_DRAFT)
1572  {
1573  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1574 
1575  // Clean parameters
1576  if (empty($qty)) $qty = 0;
1577  if (!$info_bits) $info_bits = 0;
1578  if (empty($txtva)) $txtva = 0;
1579  if (empty($txlocaltax1)) $txlocaltax1 = 0;
1580  if (empty($txlocaltax2)) $txlocaltax2 = 0;
1581  if (empty($remise_percent)) $remise_percent = 0;
1582 
1583  $remise_percent = price2num($remise_percent);
1584  $qty = price2num($qty);
1585  $pu_ht = price2num($pu_ht);
1586  $pu_ht_devise = price2num($pu_ht_devise);
1587  $pu_ttc = price2num($pu_ttc);
1588  if (!preg_match('/\((.*)\)/', $txtva)) {
1589  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
1590  }
1591  $txlocaltax1 = price2num($txlocaltax1);
1592  $txlocaltax2 = price2num($txlocaltax2);
1593  if ($price_base_type == 'HT')
1594  {
1595  $pu = $pu_ht;
1596  } else {
1597  $pu = $pu_ttc;
1598  }
1599  $desc = trim($desc);
1600 
1601  // Check parameters
1602  if ($qty < 0 && !$fk_product)
1603  {
1604  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Product"));
1605  return -1;
1606  }
1607  if ($type < 0) return -1;
1608  if ($date_start && $date_end && $date_start > $date_end) {
1609  $langs->load("errors");
1610  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
1611  return -1;
1612  }
1613 
1614 
1615  $this->db->begin();
1616 
1617  $product_type = $type;
1618  $label = ''; // deprecated
1619 
1620  if ($fk_product > 0) {
1621  if (!empty($conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY)) {
1622  // Check quantity is enough
1623  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);
1624  $prod = new Product($this->db);
1625  if ($prod->fetch($fk_product) > 0) {
1626  $product_type = $prod->type;
1627  $label = $prod->label;
1628 
1629  // We use 'none' instead of $ref_supplier, because fourn_ref may not exists anymore. So we will take the first supplier price ok.
1630  // If we want a dedicated supplier price, we must provide $fk_prod_fourn_price.
1631  $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
1632 
1633  // If supplier order created from customer order, we take best supplier price
1634  // If $pu (defined previously from pu_ht or pu_ttc) is not defined at all, we also take the best supplier price
1635  if ($result > 0 && ($origin == 'commande' || $pu === ''))
1636  {
1637  $pu = $prod->fourn_pu; // Unit price supplier price set by get_buyprice
1638  $ref_supplier = $prod->ref_supplier; // Ref supplier price set by get_buyprice
1639  // is remise percent not keyed but present for the product we add it
1640  if ($remise_percent == 0 && $prod->remise_percent != 0) $remise_percent = $prod->remise_percent;
1641  }
1642  if ($result == 0) // If result == 0, we failed to found the supplier reference price
1643  {
1644  $langs->load("errors");
1645  $this->error = "Ref ".$prod->ref." ".$langs->trans("ErrorQtyTooLowForThisSupplier");
1646  $this->db->rollback();
1647  dol_syslog(get_class($this)."::addline we did not found supplier price, so we can't guess unit price");
1648  //$pu = $prod->fourn_pu; // We do not overwrite unit price
1649  //$ref = $prod->ref_fourn; // We do not overwrite ref supplier price
1650  return -1;
1651  }
1652  if ($result == -1)
1653  {
1654  $langs->load("errors");
1655  $this->error = "Ref ".$prod->ref." ".$langs->trans("ErrorQtyTooLowForThisSupplier");
1656  $this->db->rollback();
1657  dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_DEBUG);
1658  return -1;
1659  }
1660  if ($result < -1)
1661  {
1662  $this->error = $prod->error;
1663  $this->db->rollback();
1664  dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_ERR);
1665  return -1;
1666  }
1667  } else {
1668  $this->error = $prod->error;
1669  $this->db->rollback();
1670  return -1;
1671  }
1672  }
1673 
1674  // Predefine quantity according to packaging
1675  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
1676  $prod = new Product($this->db, $fk_product);
1677  $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product, 'none', ($this->fk_soc ? $this->fk_soc : $this->socid));
1678  if ($qty < $prod->packaging) {
1679  $qty = $prod->packaging;
1680  } else {
1681  if (!empty($prod->packaging) && ($qty % $prod->packaging) > 0) {
1682  $coeff = intval($qty / $prod->packaging) + 1;
1683  $qty = $prod->packaging * $coeff;
1684  }
1685  }
1686  setEventMessage($langs->trans('QtyRecalculatedWithPackaging'), 'mesgs');
1687  }
1688  }
1689 
1690  if (!empty($conf->multicurrency->enabled) && $pu_ht_devise > 0) {
1691  $pu = 0;
1692  }
1693 
1694  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $mysoc, $this->thirdparty);
1695 
1696  // Clean vat code
1697  $vat_src_code = '';
1698  if (preg_match('/\((.*)\)/', $txtva, $reg))
1699  {
1700  $vat_src_code = $reg[1];
1701  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
1702  }
1703 
1704  // Calcul du total TTC et de la TVA pour la ligne a partir de
1705  // qty, pu, remise_percent et txtva
1706  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1707  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1708 
1709  $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);
1710 
1711  $total_ht = $tabprice[0];
1712  $total_tva = $tabprice[1];
1713  $total_ttc = $tabprice[2];
1714  $total_localtax1 = $tabprice[9];
1715  $total_localtax2 = $tabprice[10];
1716  $pu = $pu_ht = $tabprice[3];
1717 
1718  // MultiCurrency
1719  $multicurrency_total_ht = $tabprice[16];
1720  $multicurrency_total_tva = $tabprice[17];
1721  $multicurrency_total_ttc = $tabprice[18];
1722  $pu_ht_devise = $tabprice[19];
1723 
1724  $localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
1725  $localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
1726 
1727  $rangmax = $this->line_max();
1728  $rang = $rangmax + 1;
1729 
1730  // Insert line
1731  $this->line = new CommandeFournisseurLigne($this->db);
1732 
1733  $this->line->context = $this->context;
1734 
1735  $this->line->fk_commande = $this->id;
1736  $this->line->label = $label;
1737  $this->line->ref_fourn = $ref_supplier;
1738  $this->line->ref_supplier = $ref_supplier;
1739  $this->line->desc = $desc;
1740  $this->line->qty = $qty;
1741  $this->line->tva_tx = $txtva;
1742  $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1743  $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1744  $this->line->localtax1_type = $localtax1_type;
1745  $this->line->localtax2_type = $localtax2_type;
1746  $this->line->fk_product = $fk_product;
1747  $this->line->product_type = $product_type;
1748  $this->line->remise_percent = $remise_percent;
1749  $this->line->subprice = $pu_ht;
1750  $this->line->rang = $rang;
1751  $this->line->info_bits = $info_bits;
1752 
1753  $this->line->vat_src_code = $vat_src_code;
1754  $this->line->total_ht = $total_ht;
1755  $this->line->total_tva = $total_tva;
1756  $this->line->total_localtax1 = $total_localtax1;
1757  $this->line->total_localtax2 = $total_localtax2;
1758  $this->line->total_ttc = $total_ttc;
1759  $this->line->product_type = $type;
1760  $this->line->special_code = $this->special_code;
1761  $this->line->origin = $origin;
1762  $this->line->origin_id = $origin_id;
1763  $this->line->fk_unit = $fk_unit;
1764 
1765  $this->line->date_start = $date_start;
1766  $this->line->date_end = $date_end;
1767 
1768  // Multicurrency
1769  $this->line->fk_multicurrency = $this->fk_multicurrency;
1770  $this->line->multicurrency_code = $this->multicurrency_code;
1771  $this->line->multicurrency_subprice = $pu_ht_devise;
1772  $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1773  $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1774  $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1775 
1776  $this->line->subprice = $pu_ht;
1777  $this->line->price = $this->line->subprice;
1778 
1779  $this->line->remise_percent = $remise_percent;
1780 
1781  if (is_array($array_options) && count($array_options) > 0) {
1782  $this->line->array_options = $array_options;
1783  }
1784 
1785  $result = $this->line->insert($notrigger);
1786  if ($result > 0)
1787  {
1788  // Reorder if child line
1789  if (!empty($fk_parent_line)) $this->line_order(true, 'DESC');
1790 
1791  // Mise a jour informations denormalisees au niveau de la commande meme
1792  $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.
1793  if ($result > 0)
1794  {
1795  $this->db->commit();
1796  return $this->line->id;
1797  } else {
1798  $this->db->rollback();
1799  return -1;
1800  }
1801  } else {
1802  $this->error = $this->line->error;
1803  $this->errors = $this->line->errors;
1804  dol_syslog(get_class($this)."::addline error=".$this->error, LOG_ERR);
1805  $this->db->rollback();
1806  return -1;
1807  }
1808  }
1809  }
1810 
1811 
1828  public function dispatchProduct($user, $product, $qty, $entrepot, $price = 0, $comment = '', $eatby = '', $sellby = '', $batch = '', $fk_commandefourndet = 0, $notrigger = 0)
1829  {
1830  global $conf, $langs;
1831 
1832  $error = 0;
1833  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
1834 
1835  // Check parameters (if test are wrong here, there is bug into caller)
1836  if ($entrepot <= 0)
1837  {
1838  $this->error = 'ErrorBadValueForParameterWarehouse';
1839  return -1;
1840  }
1841  if ($qty == 0)
1842  {
1843  $this->error = 'ErrorBadValueForParameterQty';
1844  return -1;
1845  }
1846 
1847  $dispatchstatus = 1;
1848  if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) $dispatchstatus = 0; // Setting dispatch status (a validation step after receiving products) will be done manually to 1 or 2 if this option is on
1849 
1850  $now = dol_now();
1851 
1852  if (($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY || $this->statut == self::STATUS_RECEIVED_COMPLETELY))
1853  {
1854  $this->db->begin();
1855 
1856  $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur_dispatch";
1857  $sql .= " (fk_commande, fk_product, qty, fk_entrepot, fk_user, datec, fk_commandefourndet, status, comment, eatby, sellby, batch) VALUES";
1858  $sql .= " ('".$this->id."','".$product."','".$qty."',".($entrepot > 0 ? "'".$entrepot."'" : "null").",'".$user->id."','".$this->db->idate($now)."','".$fk_commandefourndet."', ".$dispatchstatus.", '".$this->db->escape($comment)."', ";
1859  $sql .= ($eatby ? "'".$this->db->idate($eatby)."'" : "null").", ".($sellby ? "'".$this->db->idate($sellby)."'" : "null").", ".($batch ? "'".$this->db->escape($batch)."'" : "null");
1860  $sql .= ")";
1861 
1862  dol_syslog(get_class($this)."::dispatchProduct", LOG_DEBUG);
1863  $resql = $this->db->query($sql);
1864  if ($resql)
1865  {
1866  if (!$notrigger)
1867  {
1868  global $conf, $langs, $user;
1869  // Call trigger
1870  $result = $this->call_trigger('LINEORDER_SUPPLIER_DISPATCH', $user);
1871  if ($result < 0)
1872  {
1873  $error++;
1874  }
1875  // End call triggers
1876  }
1877  } else {
1878  $this->error = $this->db->lasterror();
1879  $error++;
1880  }
1881 
1882  // If module stock is enabled and the stock increase is done on purchase order dispatching
1883  if (!$error && $entrepot > 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER))
1884  {
1885  $mouv = new MouvementStock($this->db);
1886  if ($product > 0)
1887  {
1888  // $price should take into account discount (except if option STOCK_EXCLUDE_DISCOUNT_FOR_PMP is on)
1889  $mouv->origin = &$this;
1890  $result = $mouv->reception($user, $product, $entrepot, $qty, $price, $comment, $eatby, $sellby, $batch);
1891  if ($result < 0)
1892  {
1893  $this->error = $mouv->error;
1894  $this->errors = $mouv->errors;
1895  dol_syslog(get_class($this)."::dispatchProduct ".$this->error." ".join(',', $this->errors), LOG_ERR);
1896  $error++;
1897  }
1898  }
1899  }
1900 
1901  if ($error == 0)
1902  {
1903  $this->db->commit();
1904  return 1;
1905  } else {
1906  $this->db->rollback();
1907  return -1;
1908  }
1909  } else {
1910  $this->error = 'BadStatusForObject';
1911  return -2;
1912  }
1913  }
1914 
1922  public function deleteline($idline, $notrigger = 0)
1923  {
1924  if ($this->statut == 0)
1925  {
1926  $line = new CommandeFournisseurLigne($this->db);
1927 
1928  if ($line->fetch($idline) <= 0)
1929  {
1930  return 0;
1931  }
1932 
1933  if ($line->delete($notrigger) > 0)
1934  {
1935  $this->update_price();
1936  return 1;
1937  } else {
1938  $this->error = $line->error;
1939  $this->errors = $line->errors;
1940  return -1;
1941  }
1942  } else {
1943  return -2;
1944  }
1945  }
1946 
1954  public function delete(User $user, $notrigger = 0)
1955  {
1956  global $langs, $conf;
1957  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1958 
1959  $error = 0;
1960 
1961  $this->db->begin();
1962 
1963  if (empty($notrigger))
1964  {
1965  // Call trigger
1966  $result = $this->call_trigger('ORDER_SUPPLIER_DELETE', $user);
1967  if ($result < 0)
1968  {
1969  $this->errors[] = 'ErrorWhenRunningTrigger';
1970  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
1971  $this->db->rollback();
1972  return -1;
1973  }
1974  // End call triggers
1975  }
1976 
1977  $main = MAIN_DB_PREFIX.'commande_fournisseurdet';
1978  $ef = $main."_extrafields";
1979  $sql = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_commande = ".$this->id.")";
1980  dol_syslog(get_class($this)."::delete extrafields lines", LOG_DEBUG);
1981  if (!$this->db->query($sql))
1982  {
1983  $this->error = $this->db->lasterror();
1984  $this->errors[] = $this->db->lasterror();
1985  $error++;
1986  }
1987 
1988  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet WHERE fk_commande =".$this->id;
1989  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1990  if (!$this->db->query($sql))
1991  {
1992  $this->error = $this->db->lasterror();
1993  $this->errors[] = $this->db->lasterror();
1994  $error++;
1995  }
1996 
1997  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur WHERE rowid =".$this->id;
1998  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1999  if ($resql = $this->db->query($sql))
2000  {
2001  if ($this->db->affected_rows($resql) < 1)
2002  {
2003  $this->error = $this->db->lasterror();
2004  $this->errors[] = $this->db->lasterror();
2005  $error++;
2006  }
2007  } else {
2008  $this->error = $this->db->lasterror();
2009  $this->errors[] = $this->db->lasterror();
2010  $error++;
2011  }
2012 
2013  // Remove extrafields
2014  if (!$error)
2015  {
2016  $result = $this->deleteExtraFields();
2017  if ($result < 0)
2018  {
2019  $this->error = 'FailToDeleteExtraFields';
2020  $this->errors[] = 'FailToDeleteExtraFields';
2021  $error++;
2022  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
2023  }
2024  }
2025 
2026  // Delete linked object
2027  $res = $this->deleteObjectLinked();
2028  if ($res < 0) {
2029  $this->error = 'FailToDeleteObjectLinked';
2030  $this->errors[] = 'FailToDeleteObjectLinked';
2031  $error++;
2032  }
2033 
2034  if (!$error)
2035  {
2036  // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
2037  $this->deleteEcmFiles();
2038 
2039  // We remove directory
2040  $ref = dol_sanitizeFileName($this->ref);
2041  if ($conf->fournisseur->commande->dir_output)
2042  {
2043  $dir = $conf->fournisseur->commande->dir_output."/".$ref;
2044  $file = $dir."/".$ref.".pdf";
2045  if (file_exists($file))
2046  {
2047  if (!dol_delete_file($file, 0, 0, 0, $this)) // For triggers
2048  {
2049  $this->error = 'ErrorFailToDeleteFile';
2050  $this->errors[] = 'ErrorFailToDeleteFile';
2051  $error++;
2052  }
2053  }
2054  if (file_exists($dir))
2055  {
2056  $res = @dol_delete_dir_recursive($dir);
2057  if (!$res)
2058  {
2059  $this->error = 'ErrorFailToDeleteDir';
2060  $this->errors[] = 'ErrorFailToDeleteDir';
2061  $error++;
2062  }
2063  }
2064  }
2065  }
2066 
2067  if (!$error)
2068  {
2069  dol_syslog(get_class($this)."::delete $this->id by $user->id", LOG_DEBUG);
2070  $this->db->commit();
2071  return 1;
2072  } else {
2073  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
2074  $this->db->rollback();
2075  return -$error;
2076  }
2077  }
2078 
2079  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2085  public function get_methodes_commande()
2086  {
2087  // phpcs:enable
2088  $sql = "SELECT rowid, libelle";
2089  $sql .= " FROM ".MAIN_DB_PREFIX."c_input_method";
2090  $sql .= " WHERE active = 1";
2091 
2092  $resql = $this->db->query($sql);
2093  if ($resql)
2094  {
2095  $i = 0;
2096  $num = $this->db->num_rows($resql);
2097  $this->methodes_commande = array();
2098  while ($i < $num)
2099  {
2100  $row = $this->db->fetch_row($resql);
2101 
2102  $this->methodes_commande[$row[0]] = $row[1];
2103 
2104  $i++;
2105  }
2106  return 0;
2107  } else {
2108  return -1;
2109  }
2110  }
2111 
2120  public function getDispachedLines($status = -1)
2121  {
2122  $ret = array();
2123 
2124  // List of already dispatched lines
2125  $sql = "SELECT p.ref, p.label,";
2126  $sql .= " e.rowid as warehouse_id, e.ref as entrepot,";
2127  $sql .= " cfd.rowid as dispatchedlineid, cfd.fk_product, cfd.qty, cfd.eatby, cfd.sellby, cfd.batch, cfd.comment, cfd.status";
2128  $sql .= " FROM ".MAIN_DB_PREFIX."product as p,";
2129  $sql .= " ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as cfd";
2130  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."entrepot as e ON cfd.fk_entrepot = e.rowid";
2131  $sql .= " WHERE cfd.fk_commande = ".$this->id;
2132  $sql .= " AND cfd.fk_product = p.rowid";
2133  if ($status >= 0) $sql .= " AND cfd.status = ".$status;
2134  $sql .= " ORDER BY cfd.rowid ASC";
2135 
2136  $resql = $this->db->query($sql);
2137  if ($resql)
2138  {
2139  $num = $this->db->num_rows($resql);
2140  $i = 0;
2141 
2142  while ($i < $num)
2143  {
2144  $objp = $this->db->fetch_object($resql);
2145  if ($objp)
2146  {
2147  $ret[] = array(
2148  'id' => $objp->dispatchedlineid,
2149  'productid' => $objp->fk_product,
2150  'warehouseid' => $objp->warehouse_id,
2151  'qty' => $objp->qty,
2152  );
2153  }
2154 
2155  $i++;
2156  }
2157  } else dol_print_error($this->db, 'Failed to execute request to get dispatched lines');
2158 
2159  return $ret;
2160  }
2161 
2162 
2163  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2173  public function Livraison($user, $date, $type, $comment)
2174  {
2175  // phpcs:enable
2176  global $conf, $langs;
2177 
2178  $result = 0;
2179  $error = 0;
2180 
2181  dol_syslog(get_class($this)."::Livraison");
2182 
2183  if ($user->rights->fournisseur->commande->receptionner)
2184  {
2185  // Define the new status
2186  if ($type == 'par') $statut = self::STATUS_RECEIVED_PARTIALLY;
2187  elseif ($type == 'tot') $statut = self::STATUS_RECEIVED_COMPLETELY;
2188  elseif ($type == 'nev') $statut = self::STATUS_CANCELED_AFTER_ORDER;
2189  elseif ($type == 'can') $statut = self::STATUS_CANCELED_AFTER_ORDER;
2190  else {
2191  $error++;
2192  dol_syslog(get_class($this)."::Livraison Error -2", LOG_ERR);
2193  return -2;
2194  }
2195 
2196  // Some checks to accept the record
2197  if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS))
2198  {
2199  // If option SUPPLIER_ORDER_USE_DISPATCH_STATUS is on, we check all reception are approved to allow status "total/done"
2200  if (!$error && ($type == 'tot'))
2201  {
2202  $dispatchedlinearray = $this->getDispachedLines(0);
2203  if (count($dispatchedlinearray) > 0)
2204  {
2205  $result = -1;
2206  $error++;
2207  $this->errors[] = 'ErrorCantSetReceptionToTotalDoneWithReceptionToApprove';
2208  dol_syslog('ErrorCantSetReceptionToTotalDoneWithReceptionToApprove', LOG_DEBUG);
2209  }
2210  }
2211  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)
2212  {
2213  $dispatcheddenied = $this->getDispachedLines(2);
2214  if (count($dispatchedlinearray) > 0)
2215  {
2216  $result = -1;
2217  $error++;
2218  $this->errors[] = 'ErrorCantSetReceptionToTotalDoneWithReceptionDenied';
2219  dol_syslog('ErrorCantSetReceptionToTotalDoneWithReceptionDenied', LOG_DEBUG);
2220  }
2221  }
2222  }
2223 
2224  // TODO LDR01 Add a control test to accept only if ALL predefined products are received (same qty).
2225 
2226  if (empty($error))
2227  {
2228  $this->db->begin();
2229 
2230  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
2231  $sql .= " SET fk_statut = ".$statut;
2232  $sql .= " WHERE rowid = ".$this->id;
2233  $sql .= " AND fk_statut IN (".self::STATUS_ORDERSENT.",".self::STATUS_RECEIVED_PARTIALLY.")"; // Process running or Partially received
2234 
2235  dol_syslog(get_class($this)."::Livraison", LOG_DEBUG);
2236  $resql = $this->db->query($sql);
2237  if ($resql)
2238  {
2239  $result = 1;
2240  $old_statut = $this->statut;
2241  $this->statut = $statut;
2242  $this->actionmsg2 = $comment;
2243 
2244  // Call trigger
2245  $result_trigger = $this->call_trigger('ORDER_SUPPLIER_RECEIVE', $user);
2246  if ($result_trigger < 0) $error++;
2247  // End call triggers
2248 
2249  if (empty($error))
2250  {
2251  $this->db->commit();
2252  } else {
2253  $this->statut = $old_statut;
2254  $this->db->rollback();
2255  $this->error = $this->db->lasterror();
2256  $result = -1;
2257  }
2258  } else {
2259  $this->db->rollback();
2260  $this->error = $this->db->lasterror();
2261  $result = -1;
2262  }
2263  }
2264  } else {
2265  $this->error = $langs->trans('NotAuthorized');
2266  $this->errors[] = $langs->trans('NotAuthorized');
2267  dol_syslog(get_class($this)."::Livraison Not Authorized");
2268  $result = -3;
2269  }
2270  return $result;
2271  }
2272 
2273  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2283  public function set_date_livraison($user, $delivery_date, $notrigger = 0)
2284  {
2285  // phpcs:enable
2286  return $this->setDeliveryDate($user, $delivery_date, $notrigger);
2287  }
2288 
2297  public function setDeliveryDate($user, $delivery_date, $notrigger = 0)
2298  {
2299  if ($user->rights->fournisseur->commande->creer)
2300  {
2301  $error = 0;
2302 
2303  $this->db->begin();
2304 
2305  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
2306  $sql .= " SET date_livraison = ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : 'null');
2307  $sql .= " WHERE rowid = ".$this->id;
2308 
2309  dol_syslog(__METHOD__, LOG_DEBUG);
2310  $resql = $this->db->query($sql);
2311  if (!$resql)
2312  {
2313  $this->errors[] = $this->db->error();
2314  $error++;
2315  }
2316 
2317  if (!$error)
2318  {
2319  $this->oldcopy = clone $this;
2320  $this->date_livraison = $delivery_date;
2321  $this->delivery_date = $delivery_date;
2322  }
2323 
2324  if (!$notrigger && empty($error))
2325  {
2326  // Call trigger
2327  $result = $this->call_trigger('ORDER_SUPPLIER_MODIFY', $user);
2328  if ($result < 0) $error++;
2329  // End call triggers
2330  }
2331 
2332  if (!$error)
2333  {
2334  $this->db->commit();
2335  return 1;
2336  } else {
2337  foreach ($this->errors as $errmsg)
2338  {
2339  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2340  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2341  }
2342  $this->db->rollback();
2343  return -1 * $error;
2344  }
2345  } else {
2346  return -2;
2347  }
2348  }
2349 
2350  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2359  public function set_id_projet($user, $id_projet, $notrigger = 0)
2360  {
2361  // phpcs:enable
2362  if ($user->rights->fournisseur->commande->creer)
2363  {
2364  $error = 0;
2365 
2366  $this->db->begin();
2367 
2368  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
2369  $sql .= " SET fk_projet = ".($id_projet > 0 ? (int) $id_projet : 'null');
2370  $sql .= " WHERE rowid = ".$this->id;
2371 
2372  dol_syslog(__METHOD__, LOG_DEBUG);
2373  $resql = $this->db->query($sql);
2374  if (!$resql)
2375  {
2376  $this->errors[] = $this->db->error();
2377  $error++;
2378  }
2379 
2380  if (!$error)
2381  {
2382  $this->oldcopy = clone $this;
2383  $this->fk_projet = $id_projet;
2384  $this->fk_project = $id_projet;
2385  }
2386 
2387  if (!$notrigger && empty($error))
2388  {
2389  // Call trigger
2390  $result = $this->call_trigger('ORDER_SUPPLIER_MODIFY', $user);
2391  if ($result < 0) $error++;
2392  // End call triggers
2393  }
2394 
2395  if (!$error)
2396  {
2397  $this->db->commit();
2398  return 1;
2399  } else {
2400  foreach ($this->errors as $errmsg)
2401  {
2402  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2403  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2404  }
2405  $this->db->rollback();
2406  return -1 * $error;
2407  }
2408  } else {
2409  return -2;
2410  }
2411  }
2412 
2421  public function updateFromCommandeClient($user, $idc, $comclientid)
2422  {
2423  $comclient = new Commande($this->db);
2424  $comclient->fetch($comclientid);
2425 
2426  $this->id = $idc;
2427 
2428  $this->lines = array();
2429 
2430  $num = count($comclient->lines);
2431  for ($i = 0; $i < $num; $i++)
2432  {
2433  $prod = new Product($this->db);
2434  $label = '';
2435  $ref = '';
2436  if ($prod->fetch($comclient->lines[$i]->fk_product) > 0)
2437  {
2438  $label = $prod->label;
2439  $ref = $prod->ref;
2440  }
2441 
2442  $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet";
2443  $sql .= " (fk_commande, label, description, fk_product, price, qty, tva_tx, localtax1_tx, localtax2_tx, remise_percent, subprice, remise, ref)";
2444  $sql .= " VALUES (".$idc.", '".$this->db->escape($label)."', ".$this->db->escape($comclient->lines[$i]->desc);
2445  $sql .= ",".$comclient->lines[$i]->fk_product.", ".price2num($comclient->lines[$i]->price);
2446  $sql .= ", ".$comclient->lines[$i]->qty.", ".$comclient->lines[$i]->tva_tx.", ".$comclient->lines[$i]->localtax1_tx.", ".$comclient->lines[$i]->localtax2_tx.", ".$comclient->lines[$i]->remise_percent;
2447  $sql .= ", '".price2num($comclient->lines[$i]->subprice)."','0', '".$this->db->escape($ref)."');";
2448  if ($this->db->query($sql))
2449  {
2450  $this->update_price();
2451  }
2452  }
2453 
2454  return 1;
2455  }
2456 
2464  public function setStatus($user, $status)
2465  {
2466  global $conf, $langs;
2467  $error = 0;
2468 
2469  $this->db->begin();
2470 
2471  $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande_fournisseur';
2472  $sql .= ' SET fk_statut='.$status;
2473  $sql .= ' WHERE rowid = '.$this->id;
2474 
2475  dol_syslog(get_class($this)."::setStatus", LOG_DEBUG);
2476  $resql = $this->db->query($sql);
2477  if ($resql)
2478  {
2479  // Trigger names for each status
2480  $triggerName = array();
2481  $triggerName[0] = 'DRAFT';
2482  $triggerName[1] = 'VALIDATED';
2483  $triggerName[2] = 'APPROVED';
2484  $triggerName[3] = 'ORDERED'; // Ordered
2485  $triggerName[4] = 'RECEIVED_PARTIALLY';
2486  $triggerName[5] = 'RECEIVED_COMPLETELY';
2487  $triggerName[6] = 'CANCELED';
2488  $triggerName[7] = 'CANCELED';
2489  $triggerName[9] = 'REFUSED';
2490 
2491  // Call trigger
2492  $result = $this->call_trigger("ORDER_SUPPLIER_STATUS_".$triggerName[$status], $user);
2493  if ($result < 0) { $error++; }
2494  // End call triggers
2495  } else {
2496  $error++;
2497  $this->error = $this->db->lasterror();
2498  dol_syslog(get_class($this)."::setStatus ".$this->error);
2499  }
2500 
2501  if (!$error)
2502  {
2503  $this->statut = $status;
2504  $this->db->commit();
2505  return 1;
2506  } else {
2507  $this->db->rollback();
2508  return -1;
2509  }
2510  }
2511 
2535  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 = '')
2536  {
2537  global $mysoc, $conf, $langs;
2538  dol_syslog(get_class($this)."::updateline $rowid, $desc, $pu, $qty, $remise_percent, $txtva, $price_base_type, $info_bits, $type, $fk_unit");
2539  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2540 
2541  $error = 0;
2542 
2543  if ($this->brouillon)
2544  {
2545  // Clean parameters
2546  if (empty($qty)) $qty = 0;
2547  if (empty($info_bits)) $info_bits = 0;
2548  if (empty($txtva)) $txtva = 0;
2549  if (empty($txlocaltax1)) $txlocaltax1 = 0;
2550  if (empty($txlocaltax2)) $txlocaltax2 = 0;
2551  if (empty($remise)) $remise = 0;
2552  if (empty($remise_percent)) $remise_percent = 0;
2553 
2554  $remise_percent = price2num($remise_percent);
2555  $qty = price2num($qty);
2556  if (!$qty) $qty = 1;
2557  $pu = price2num($pu);
2558  $pu_ht_devise = price2num($pu_ht_devise);
2559  if (!preg_match('/\((.*)\)/', $txtva)) {
2560  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
2561  }
2562  $txlocaltax1 = price2num($txlocaltax1);
2563  $txlocaltax2 = price2num($txlocaltax2);
2564 
2565  // Check parameters
2566  if ($type < 0) return -1;
2567  if ($date_start && $date_end && $date_start > $date_end) {
2568  $langs->load("errors");
2569  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
2570  return -1;
2571  }
2572 
2573  $this->db->begin();
2574 
2575  // Calcul du total TTC et de la TVA pour la ligne a partir de
2576  // qty, pu, remise_percent et txtva
2577  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
2578  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
2579 
2580  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $mysoc, $this->thirdparty);
2581 
2582  // Clean vat code
2583  $reg = array();
2584  $vat_src_code = '';
2585  if (preg_match('/\((.*)\)/', $txtva, $reg))
2586  {
2587  $vat_src_code = $reg[1];
2588  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
2589  }
2590 
2591  $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);
2592  $total_ht = $tabprice[0];
2593  $total_tva = $tabprice[1];
2594  $total_ttc = $tabprice[2];
2595  $total_localtax1 = $tabprice[9];
2596  $total_localtax2 = $tabprice[10];
2597  $pu_ht = $tabprice[3];
2598  $pu_tva = $tabprice[4];
2599  $pu_ttc = $tabprice[5];
2600 
2601  // MultiCurrency
2602  $multicurrency_total_ht = $tabprice[16];
2603  $multicurrency_total_tva = $tabprice[17];
2604  $multicurrency_total_ttc = $tabprice[18];
2605  $pu_ht_devise = $tabprice[19];
2606 
2607  $localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
2608  $localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
2609 
2610  //Fetch current line from the database and then clone the object and set it in $oldline property
2611  $this->line = new CommandeFournisseurLigne($this->db);
2612  $this->line->fetch($rowid);
2613 
2614  $oldline = clone $this->line;
2615  $this->line->oldline = $oldline;
2616 
2617  $this->line->context = $this->context;
2618 
2619  $this->line->fk_commande = $this->id;
2620  //$this->line->label=$label;
2621  $this->line->desc = $desc;
2622 
2623  // redefine quantity according to packaging
2624  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING))
2625  {
2626  if ($qty < $this->line->packaging)
2627  {
2628  $qty = $this->line->packaging;
2629  } else {
2630  if (($qty % $this->line->packaging) > 0)
2631  {
2632  $coeff = intval($qty / $this->line->packaging) + 1;
2633  $qty = $this->line->packaging * $coeff;
2634  setEventMessage($langs->trans('QtyRecalculatedWithPackaging'), 'mesgs');
2635  }
2636  }
2637  }
2638 
2639  $this->line->qty = $qty;
2640  $this->line->ref_supplier = $ref_supplier;
2641 
2642  $this->line->vat_src_code = $vat_src_code;
2643  $this->line->tva_tx = $txtva;
2644  $this->line->localtax1_tx = $txlocaltax1;
2645  $this->line->localtax2_tx = $txlocaltax2;
2646  $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
2647  $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
2648  $this->line->remise_percent = $remise_percent;
2649  $this->line->subprice = $pu_ht;
2650  $this->line->rang = $this->rang;
2651  $this->line->info_bits = $info_bits;
2652  $this->line->total_ht = $total_ht;
2653  $this->line->total_tva = $total_tva;
2654  $this->line->total_localtax1 = $total_localtax1;
2655  $this->line->total_localtax2 = $total_localtax2;
2656  $this->line->total_ttc = $total_ttc;
2657  $this->line->product_type = $type;
2658  $this->line->special_code = $this->special_code;
2659  $this->line->origin = $this->origin;
2660  $this->line->fk_unit = $fk_unit;
2661 
2662  $this->line->date_start = $date_start;
2663  $this->line->date_end = $date_end;
2664 
2665  // Multicurrency
2666  $this->line->fk_multicurrency = $this->fk_multicurrency;
2667  $this->line->multicurrency_code = $this->multicurrency_code;
2668  $this->line->multicurrency_subprice = $pu_ht_devise;
2669  $this->line->multicurrency_total_ht = $multicurrency_total_ht;
2670  $this->line->multicurrency_total_tva = $multicurrency_total_tva;
2671  $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
2672 
2673  $this->line->subprice = $pu_ht;
2674  $this->line->price = $this->line->subprice;
2675 
2676  $this->line->remise_percent = $remise_percent;
2677 
2678  if (is_array($array_options) && count($array_options) > 0) {
2679  // We replace values in this->line->array_options only for entries defined into $array_options
2680  foreach ($array_options as $key => $value) {
2681  $this->line->array_options[$key] = $array_options[$key];
2682  }
2683  }
2684 
2685  $result = $this->line->update($notrigger);
2686 
2687 
2688  // Mise a jour info denormalisees au niveau facture
2689  if ($result >= 0)
2690  {
2691  $this->update_price('', 'auto');
2692  $this->db->commit();
2693  return $result;
2694  } else {
2695  $this->error = $this->db->lasterror();
2696  $this->db->rollback();
2697  return -1;
2698  }
2699  } else {
2700  $this->error = "Order status makes operation forbidden";
2701  dol_syslog(get_class($this)."::updateline ".$this->error, LOG_ERR);
2702  return -2;
2703  }
2704  }
2705 
2706 
2714  public function initAsSpecimen()
2715  {
2716  global $user, $langs, $conf;
2717 
2718  include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
2719 
2720  dol_syslog(get_class($this)."::initAsSpecimen");
2721 
2722  $now = dol_now();
2723 
2724  // Find first product
2725  $prodid = 0;
2726  $product = new ProductFournisseur($this->db);
2727  $sql = "SELECT rowid";
2728  $sql .= " FROM ".MAIN_DB_PREFIX."product";
2729  $sql .= " WHERE entity IN (".getEntity('product').")";
2730  $sql .= $this->db->order("rowid", "ASC");
2731  $sql .= $this->db->plimit(1);
2732  $resql = $this->db->query($sql);
2733  if ($resql)
2734  {
2735  $obj = $this->db->fetch_object($resql);
2736  $prodid = $obj->rowid;
2737  }
2738 
2739  // Initialise parametres
2740  $this->id = 0;
2741  $this->ref = 'SPECIMEN';
2742  $this->specimen = 1;
2743  $this->socid = 1;
2744  $this->date = $now;
2745  $this->date_commande = $now;
2746  $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
2747  $this->cond_reglement_code = 'RECEP';
2748  $this->mode_reglement_code = 'CHQ';
2749 
2750  $this->note_public = 'This is a comment (public)';
2751  $this->note_private = 'This is a comment (private)';
2752 
2753  $this->multicurrency_tx = 1;
2754  $this->multicurrency_code = $conf->currency;
2755 
2756  $this->statut = 0;
2757 
2758  // Lines
2759  $nbp = 5;
2760  $xnbp = 0;
2761  while ($xnbp < $nbp)
2762  {
2763  $line = new CommandeFournisseurLigne($this->db);
2764  $line->desc = $langs->trans("Description")." ".$xnbp;
2765  $line->qty = 1;
2766  $line->subprice = 100;
2767  $line->price = 100;
2768  $line->tva_tx = 19.6;
2769  $line->localtax1_tx = 0;
2770  $line->localtax2_tx = 0;
2771  if ($xnbp == 2)
2772  {
2773  $line->total_ht = 50;
2774  $line->total_ttc = 59.8;
2775  $line->total_tva = 9.8;
2776  $line->remise_percent = 50;
2777  } else {
2778  $line->total_ht = 100;
2779  $line->total_ttc = 119.6;
2780  $line->total_tva = 19.6;
2781  $line->remise_percent = 00;
2782  }
2783  $line->fk_product = $prodid;
2784 
2785  $this->lines[$xnbp] = $line;
2786 
2787  $this->total_ht += $line->total_ht;
2788  $this->total_tva += $line->total_tva;
2789  $this->total_ttc += $line->total_ttc;
2790 
2791  $xnbp++;
2792  }
2793  }
2794 
2801  public function info($id)
2802  {
2803  $sql = 'SELECT c.rowid, date_creation as datec, tms as datem, date_valid as date_validation, date_approve as datea, date_approve2 as datea2,';
2804  $sql .= ' fk_user_author, fk_user_modif, fk_user_valid, fk_user_approve, fk_user_approve2';
2805  $sql .= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseur as c';
2806  $sql .= ' WHERE c.rowid = '.$id;
2807 
2808  $result = $this->db->query($sql);
2809  if ($result)
2810  {
2811  if ($this->db->num_rows($result))
2812  {
2813  $obj = $this->db->fetch_object($result);
2814  $this->id = $obj->rowid;
2815  if ($obj->fk_user_author) $this->user_creation_id = $obj->fk_user_author;
2816  if ($obj->fk_user_valid) $this->user_validation_id = $obj->fk_user_valid;
2817  if ($obj->fk_user_modif) $this->user_modification_id = $obj->fk_user_modif;
2818  if ($obj->fk_user_approve) $this->user_approve_id = $obj->fk_user_approve;
2819  if ($obj->fk_user_approve2) $this->user_approve_id2 = $obj->fk_user_approve2;
2820 
2821  $this->date_creation = $this->db->idate($obj->datec);
2822  $this->date_modification = $this->db->idate($obj->datem);
2823  $this->date_approve = $this->db->idate($obj->datea);
2824  $this->date_approve2 = $this->db->idate($obj->datea2);
2825  $this->date_validation = $this->db->idate($obj->date_validation);
2826  }
2827  $this->db->free($result);
2828  } else {
2829  dol_print_error($this->db);
2830  }
2831  }
2832 
2833  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2839  public function load_state_board()
2840  {
2841  // phpcs:enable
2842  global $conf, $user;
2843 
2844  $this->nb = array();
2845  $clause = "WHERE";
2846 
2847  $sql = "SELECT count(co.rowid) as nb";
2848  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as co";
2849  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON co.fk_soc = s.rowid";
2850  if (!$user->rights->societe->client->voir && !$user->socid)
2851  {
2852  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2853  $sql .= " WHERE sc.fk_user = ".$user->id;
2854  $clause = "AND";
2855  }
2856  $sql .= " ".$clause." co.entity = ".$conf->entity;
2857 
2858  $resql = $this->db->query($sql);
2859  if ($resql)
2860  {
2861  while ($obj = $this->db->fetch_object($resql))
2862  {
2863  $this->nb["supplier_orders"] = $obj->nb;
2864  }
2865  $this->db->free($resql);
2866  return 1;
2867  } else {
2868  dol_print_error($this->db);
2869  $this->error = $this->db->error();
2870  return -1;
2871  }
2872  }
2873 
2874  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2882  public function load_board($user, $mode = 'opened')
2883  {
2884  // phpcs:enable
2885  global $conf, $langs;
2886 
2887  $clause = " WHERE";
2888 
2889  $sql = "SELECT c.rowid, c.date_creation as datec, c.date_commande, c.fk_statut, c.date_livraison as delivery_date";
2890  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c";
2891  if (!$user->rights->societe->client->voir && !$user->socid)
2892  {
2893  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
2894  $sql .= " WHERE sc.fk_user = ".$user->id;
2895  $clause = " AND";
2896  }
2897  $sql .= $clause." c.entity = ".$conf->entity;
2898  if ($mode === 'awaiting') {
2899  $sql .= " AND c.fk_statut IN (".self::STATUS_ORDERSENT.", ".self::STATUS_RECEIVED_PARTIALLY.")";
2900  } else {
2901  $sql .= " AND c.fk_statut IN (".self::STATUS_VALIDATED.", ".self::STATUS_ACCEPTED.")";
2902  }
2903  if ($user->socid) $sql .= " AND c.fk_soc = ".$user->socid;
2904 
2905  $resql = $this->db->query($sql);
2906  if ($resql)
2907  {
2908  $commandestatic = new CommandeFournisseur($this->db);
2909 
2910  $response = new WorkboardResponse();
2911  $response->warning_delay = $conf->commande->fournisseur->warning_delay / 60 / 60 / 24;
2912  $response->label = $langs->trans("SuppliersOrdersToProcess");
2913  $response->labelShort = $langs->trans("Opened");
2914  $response->url = DOL_URL_ROOT.'/fourn/commande/list.php?statut=1,2&mainmenu=commercial&leftmenu=orders_suppliers';
2915  $response->img = img_object('', "order");
2916 
2917  if ($mode === 'awaiting') {
2918  $response->label = $langs->trans("SuppliersOrdersAwaitingReception");
2919  $response->labelShort = $langs->trans("AwaitingReception");
2920  $response->url = DOL_URL_ROOT.'/fourn/commande/list.php?statut=3,4&mainmenu=commercial&leftmenu=orders_suppliers';
2921  }
2922 
2923  while ($obj = $this->db->fetch_object($resql))
2924  {
2925  $response->nbtodo++;
2926 
2927  $commandestatic->delivery_date = $this->db->jdate($obj->delivery_date);
2928  $commandestatic->date_commande = $this->db->jdate($obj->date_commande);
2929  $commandestatic->statut = $obj->fk_statut;
2930 
2931  if ($commandestatic->hasDelay()) {
2932  $response->nbtodolate++;
2933  }
2934  }
2935 
2936  return $response;
2937  } else {
2938  $this->error = $this->db->error();
2939  return -1;
2940  }
2941  }
2942 
2949  public function getInputMethod()
2950  {
2951  global $db, $langs;
2952 
2953  if ($this->methode_commande_id > 0)
2954  {
2955  $sql = "SELECT rowid, code, libelle as label";
2956  $sql .= " FROM ".MAIN_DB_PREFIX.'c_input_method';
2957  $sql .= " WHERE active=1 AND rowid = ".$this->db->escape($this->methode_commande_id);
2958 
2959  $resql = $this->db->query($sql);
2960  if ($resql)
2961  {
2962  if ($this->db->num_rows($resql))
2963  {
2964  $obj = $this->db->fetch_object($resql);
2965 
2966  $string = $langs->trans($obj->code);
2967  if ($string == $obj->code)
2968  {
2969  $string = $obj->label != '-' ? $obj->label : '';
2970  }
2971  return $string;
2972  }
2973  } else dol_print_error($this->db);
2974  }
2975 
2976  return '';
2977  }
2978 
2990  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
2991  {
2992  global $conf, $langs;
2993 
2994  $langs->load("suppliers");
2995  $outputlangs->load("products");
2996 
2997  if (!dol_strlen($modele)) {
2998  $modele = 'muscadet';
2999 
3000  if ($this->model_pdf) {
3001  $modele = $this->model_pdf;
3002  } elseif (!empty($conf->global->COMMANDE_SUPPLIER_ADDON_PDF)) {
3003  $modele = $conf->global->COMMANDE_SUPPLIER_ADDON_PDF;
3004  }
3005  }
3006 
3007  $modelpath = "core/modules/supplier_order/doc/";
3008 
3009  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3010  }
3011 
3018  public function getMaxDeliveryTimeDay($langs)
3019  {
3020  if (empty($this->lines)) return '';
3021 
3022  $obj = new ProductFournisseur($this->db);
3023 
3024  $nb = 0;
3025  foreach ($this->lines as $line)
3026  {
3027  if ($line->fk_product > 0)
3028  {
3029  $idp = $obj->find_min_price_product_fournisseur($line->fk_product, $line->qty);
3030  if ($idp)
3031  {
3032  $obj->fetch($idp);
3033  if ($obj->delivery_time_days > $nb) $nb = $obj->delivery_time_days;
3034  }
3035  }
3036  }
3037 
3038  if ($nb === 0) return '';
3039  else return $nb.' '.$langs->trans('Days');
3040  }
3041 
3046  public function getRights()
3047  {
3048  global $user;
3049 
3050  return $user->rights->fournisseur->commande;
3051  }
3052 
3053 
3062  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
3063  {
3064  $tables = array(
3065  'commande_fournisseur'
3066  );
3067 
3068  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
3069  }
3070 
3078  public function hasDelay()
3079  {
3080  global $conf;
3081 
3082  if (empty($this->delivery_date) && !empty($this->date_livraison)) $this->delivery_date = $this->date_livraison; // For backward compatibility
3083 
3084  if ($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY) {
3085  $now = dol_now();
3086  if (!empty($this->delivery_date)) {
3087  $date_to_test = $this->delivery_date;
3088  return $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3089  } else {
3090  //$date_to_test = $this->date_commande;
3091  //return $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3092  return false;
3093  }
3094  } else {
3095  $now = dol_now();
3096  $date_to_test = $this->date_commande;
3097 
3098  return ($this->statut > 0 && $this->statut < 5) && $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3099  }
3100  }
3101 
3109  public function showDelay()
3110  {
3111  global $conf, $langs;
3112 
3113  if (empty($this->delivery_date) && !empty($this->date_livraison)) $this->delivery_date = $this->date_livraison; // For backward compatibility
3114 
3115  $text = '';
3116 
3117  if ($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY) {
3118  if (!empty($this->delivery_date)) {
3119  $text = $langs->trans("DeliveryDate").' '.dol_print_date($this->delivery_date, 'day');
3120  } else {
3121  $text = $langs->trans("OrderDate").' '.dol_print_date($this->date_commande, 'day');
3122  }
3123  } else {
3124  $text = $langs->trans("OrderDate").' '.dol_print_date($this->date_commande, 'day');
3125  }
3126  if ($text) {
3127  $text .= ' '.($conf->commande->fournisseur->warning_delay > 0 ? '+' : '-').' '.round(abs($conf->commande->fournisseur->warning_delay) / 3600 / 24, 1).' '.$langs->trans("days").' < '.$langs->trans("Today");
3128  }
3129 
3130  return $text;
3131  }
3132 
3133 
3142  public function calcAndSetStatusDispatch(User $user, $closeopenorder = 1, $comment = '')
3143  {
3144  global $conf, $langs;
3145 
3146  if (!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || !empty($conf->supplier_order->enabled))
3147  {
3148  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.dispatch.class.php';
3149 
3150  $qtydelivered = array();
3151  $qtywished = array();
3152 
3153  $supplierorderdispatch = new CommandeFournisseurDispatch($this->db);
3154  $filter = array('t.fk_commande'=>$this->id);
3155  if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
3156  $filter['t.status'] = 1; // Restrict to lines with status validated
3157  }
3158 
3159  $ret = $supplierorderdispatch->fetchAll('', '', 0, 0, $filter);
3160  if ($ret < 0) {
3161  $this->error = $supplierorderdispatch->error; $this->errors = $supplierorderdispatch->errors;
3162  return $ret;
3163  } else {
3164  if (is_array($supplierorderdispatch->lines) && count($supplierorderdispatch->lines) > 0) {
3165  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
3166  $date_liv = dol_now();
3167 
3168  // Build array with quantity deliverd by product
3169  foreach ($supplierorderdispatch->lines as $line) {
3170  $qtydelivered[$line->fk_product] += $line->qty;
3171  }
3172  foreach ($this->lines as $line) {
3173  // Exclude lines not qualified for shipment, similar code is found into interface_20_modWrokflow for customers
3174  if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $line->product_type > 0) continue;
3175  $qtywished[$line->fk_product] += $line->qty;
3176  }
3177 
3178  //Compare array
3179  $diff_array = array_diff_assoc($qtydelivered, $qtywished); // Warning: $diff_array is done only on common keys.
3180  $keysinwishednotindelivered = array_diff(array_keys($qtywished), array_keys($qtydelivered)); // To check we also have same number of keys
3181  $keysindeliverednotinwished = array_diff(array_keys($qtydelivered), array_keys($qtywished)); // To check we also have same number of keys
3182  /*var_dump(array_keys($qtydelivered));
3183 
3184  var_dump(array_keys($qtywished));
3185  var_dump($diff_array);
3186  var_dump($keysinwishednotindelivered);
3187  var_dump($keysindeliverednotinwished);
3188  exit;*/
3189 
3190  if (count($diff_array) == 0 && count($keysinwishednotindelivered) == 0 && count($keysindeliverednotinwished) == 0) //No diff => mean everythings is received
3191  {
3192  if ($closeopenorder)
3193  {
3194  //$ret=$this->setStatus($user,5);
3195  $ret = $this->Livraison($user, $date_liv, 'tot', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3196  if ($ret < 0) {
3197  return -1;
3198  }
3199  return 5;
3200  } else {
3201  //Diff => received partially
3202  //$ret=$this->setStatus($user,4);
3203  $ret = $this->Livraison($user, $date_liv, 'par', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3204  if ($ret < 0) {
3205  return -1;
3206  }
3207  return 4;
3208  }
3209  } elseif (!empty($conf->global->SUPPLIER_ORDER_MORE_THAN_WISHED)) {
3210  //set livraison to 'tot' if more products received than wished. (and if $closeopenorder is set to 1 of course...)
3211 
3212  $close = 0;
3213 
3214  if (count($diff_array) > 0)
3215  {
3216  //there are some difference between the two arrays
3217 
3218  //scan the array of results
3219  foreach ($diff_array as $key => $value)
3220  {
3221  //if the quantity delivered is greater or equal to wish quantity
3222  if ($qtydelivered[$key] >= $qtywished[$key])
3223  {
3224  $close++;
3225  }
3226  }
3227  }
3228 
3229 
3230  if ($close == count($diff_array)) {
3231  //all the products are received equal or more than the wished quantity
3232  if ($closeopenorder) {
3233  $ret = $this->Livraison($user, $date_liv, 'tot', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3234  if ($ret < 0) {
3235  return -1;
3236  }
3237  return 5;
3238  } else {
3239  //Diff => received partially
3240  $ret = $this->Livraison($user, $date_liv, 'par', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3241  if ($ret < 0) {
3242  return -1;
3243  }
3244  return 4;
3245  }
3246  } else {
3247  //all the products are not received
3248  $ret = $this->Livraison($user, $date_liv, 'par', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3249  if ($ret < 0) {
3250  return -1;
3251  }
3252  return 4;
3253  }
3254  } else {
3255  //Diff => received partially
3256  $ret = $this->Livraison($user, $date_liv, 'par', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
3257  if ($ret < 0) {
3258  return -1;
3259  }
3260  return 4;
3261  }
3262  }
3263  return 1;
3264  }
3265  }
3266  return 0;
3267  }
3268 
3276  public function loadReceptions($filtre_statut = -1)
3277  {
3278  $this->receptions = array();
3279 
3280  $sql = 'SELECT cd.rowid, cd.fk_product,';
3281  $sql .= ' sum(cfd.qty) as qty';
3282  $sql .= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseur_dispatch as cfd,';
3283  if ($filtre_statut >= 0) $sql .= ' '.MAIN_DB_PREFIX.'reception as e,';
3284  $sql .= ' '.MAIN_DB_PREFIX.'commande_fournisseurdet as cd';
3285  $sql .= ' WHERE';
3286  if ($filtre_statut >= 0) $sql .= ' cfd.fk_reception = e.rowid AND';
3287  $sql .= ' cfd.fk_commandefourndet = cd.rowid';
3288  $sql .= ' AND cd.fk_commande ='.$this->id;
3289  if ($this->fk_product > 0) $sql .= ' AND cd.fk_product = '.$this->fk_product;
3290  if ($filtre_statut >= 0) $sql .= ' AND e.fk_statut >= '.$filtre_statut;
3291  $sql .= ' GROUP BY cd.rowid, cd.fk_product';
3292 
3293 
3294  dol_syslog(get_class($this)."::loadReceptions", LOG_DEBUG);
3295  $result = $this->db->query($sql);
3296  if ($result)
3297  {
3298  $num = $this->db->num_rows($result);
3299  $i = 0;
3300  while ($i < $num)
3301  {
3302  $obj = $this->db->fetch_object($result);
3303  empty($this->receptions[$obj->rowid]) ? $this->receptions[$obj->rowid] = $obj->qty : $this->receptions[$obj->rowid] += $obj->qty;
3304  $i++;
3305  }
3306  $this->db->free();
3307 
3308  return $num;
3309  } else {
3310  $this->error = $this->db->lasterror();
3311  return -1;
3312  }
3313  }
3314 }
3315 
3316 
3317 
3322 {
3326  public $element = 'commande_fournisseurdet';
3327 
3331  public $table_element = 'commande_fournisseurdet';
3332 
3333  public $oldline;
3334 
3339  public $fk_commande;
3340 
3341  // From llx_commande_fournisseurdet
3345  public $fk_parent_line;
3346 
3350  public $fk_facture;
3351 
3352  public $rang = 0;
3353  public $special_code = 0;
3354 
3359  public $pu_ht;
3360 
3361  public $date_start;
3362  public $date_end;
3363 
3364  // From llx_product_fournisseur_price
3365 
3370  public $ref_supplier;
3371  public $remise;
3372 
3373 
3379  public function __construct($db)
3380  {
3381  $this->db = $db;
3382  }
3383 
3390  public function fetch($rowid)
3391  {
3392  global $conf;
3393 
3394  $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,';
3395  $sql .= ' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref,';
3396  $sql .= ' cd.remise, cd.remise_percent, cd.subprice,';
3397  $sql .= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
3398  $sql .= ' cd.total_localtax1, cd.total_localtax2,';
3399  $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
3400  $sql .= ' cd.date_start, cd.date_end, cd.fk_unit,';
3401  $sql .= ' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc';
3402  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING))
3403  $sql .= ", pfp.rowid as fk_pfp, pfp.packaging";
3404  $sql .= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseurdet as cd';
3405  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid';
3406  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING))
3407  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON cd.fk_product = pfp.fk_product and cd.ref = pfp.ref_fourn";
3408  $sql .= ' WHERE cd.rowid = '.$rowid;
3409  $result = $this->db->query($sql);
3410  if ($result)
3411  {
3412  $objp = $this->db->fetch_object($result);
3413 
3414  if (!empty($objp))
3415  {
3416  $this->rowid = $objp->rowid;
3417  $this->id = $objp->rowid;
3418  $this->fk_commande = $objp->fk_commande;
3419  $this->desc = $objp->description;
3420  $this->qty = $objp->qty;
3421  $this->ref_fourn = $objp->ref;
3422  $this->ref_supplier = $objp->ref;
3423  $this->subprice = $objp->subprice;
3424  $this->tva_tx = $objp->tva_tx;
3425  $this->localtax1_tx = $objp->localtax1_tx;
3426  $this->localtax2_tx = $objp->localtax2_tx;
3427  $this->localtax1_type = $objp->localtax1_type;
3428  $this->localtax2_type = $objp->localtax2_type;
3429  $this->remise = $objp->remise;
3430  $this->remise_percent = $objp->remise_percent;
3431  $this->fk_product = $objp->fk_product;
3432  $this->info_bits = $objp->info_bits;
3433  $this->total_ht = $objp->total_ht;
3434  $this->total_tva = $objp->total_tva;
3435  $this->total_localtax1 = $objp->total_localtax1;
3436  $this->total_localtax2 = $objp->total_localtax2;
3437  $this->total_ttc = $objp->total_ttc;
3438  $this->product_type = $objp->product_type;
3439  $this->special_code = $objp->special_code;
3440 
3441  $this->ref = $objp->product_ref;
3442 
3443  $this->product_ref = $objp->product_ref;
3444  $this->product_label = $objp->product_label;
3445  $this->product_desc = $objp->product_desc;
3446  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING))
3447  {
3448  $this->packaging = $objp->packaging;
3449  $this->fk_fournprice = $objp->fk_pfp;
3450  }
3451 
3452  $this->date_start = $this->db->jdate($objp->date_start);
3453  $this->date_end = $this->db->jdate($objp->date_end);
3454  $this->fk_unit = $objp->fk_unit;
3455 
3456  $this->multicurrency_subprice = $objp->multicurrency_subprice;
3457  $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
3458  $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
3459  $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
3460 
3461  $this->fetch_optionals();
3462 
3463  $this->db->free($result);
3464  return 1;
3465  } else {
3466  $this->error = 'Supplier order line with id='.$rowid.' not found';
3467  dol_syslog(get_class($this)."::fetch Error ".$this->error, LOG_ERR);
3468  return 0;
3469  }
3470  } else {
3471  dol_print_error($this->db);
3472  return -1;
3473  }
3474  }
3475 
3482  public function insert($notrigger = 0)
3483  {
3484  global $conf, $user;
3485 
3486  $error = 0;
3487 
3488  dol_syslog(get_class($this)."::insert rang=".$this->rang);
3489 
3490  // Clean parameters
3491  if (empty($this->tva_tx)) $this->tva_tx = 0;
3492  if (empty($this->localtax1_tx)) $this->localtax1_tx = 0;
3493  if (empty($this->localtax2_tx)) $this->localtax2_tx = 0;
3494  if (empty($this->localtax1_type)) $this->localtax1_type = '0';
3495  if (empty($this->localtax2_type)) $this->localtax2_type = '0';
3496  if (empty($this->total_localtax1)) $this->total_localtax1 = 0;
3497  if (empty($this->total_localtax2)) $this->total_localtax2 = 0;
3498  if (empty($this->rang)) $this->rang = 0;
3499  if (empty($this->remise)) $this->remise = 0;
3500  if (empty($this->remise_percent)) $this->remise_percent = 0;
3501  if (empty($this->info_bits)) $this->info_bits = 0;
3502  if (empty($this->special_code)) $this->special_code = 0;
3503  if (empty($this->fk_parent_line)) $this->fk_parent_line = 0;
3504  if (empty($this->pa_ht)) $this->pa_ht = 0;
3505 
3506  // Multicurrency
3507  if (!empty($this->multicurrency_code)) list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code);
3508  if (empty($this->fk_multicurrency))
3509  {
3510  $this->multicurrency_code = $conf->currency;
3511  $this->fk_multicurrency = 0;
3512  $this->multicurrency_tx = 1;
3513  }
3514 
3515  // Check parameters
3516  if ($this->product_type < 0) return -1;
3517 
3518  $this->db->begin();
3519 
3520  // Insertion dans base de la ligne
3521  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
3522  $sql .= " (fk_commande, label, description, date_start, date_end,";
3523  $sql .= " fk_product, product_type, special_code, rang,";
3524  $sql .= " qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
3525  $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
3526  $sql .= " fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc";
3527  $sql .= ")";
3528  $sql .= " VALUES (".$this->fk_commande.", '".$this->db->escape($this->label)."','".$this->db->escape($this->desc)."',";
3529  $sql .= " ".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : "null").",";
3530  $sql .= " ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null").",";
3531  if ($this->fk_product) { $sql .= $this->fk_product.","; } else { $sql .= "null,"; }
3532  $sql .= "'".$this->db->escape($this->product_type)."',";
3533  $sql .= "'".$this->db->escape($this->special_code)."',";
3534  $sql .= "'".$this->db->escape($this->rang)."',";
3535  $sql .= "'".$this->db->escape($this->qty)."', ";
3536  $sql .= " ".(empty($this->vat_src_code) ? "''" : "'".$this->db->escape($this->vat_src_code)."'").",";
3537  $sql .= " ".$this->tva_tx.", ";
3538  $sql .= " ".$this->localtax1_tx.",";
3539  $sql .= " ".$this->localtax2_tx.",";
3540  $sql .= " '".$this->db->escape($this->localtax1_type)."',";
3541  $sql .= " '".$this->db->escape($this->localtax2_type)."',";
3542  $sql .= " ".$this->remise_percent.", ".price2num($this->subprice, 'MU').", '".$this->db->escape($this->ref_supplier)."',";
3543  $sql .= " ".price2num($this->total_ht).",";
3544  $sql .= " ".price2num($this->total_tva).",";
3545  $sql .= " ".price2num($this->total_localtax1).",";
3546  $sql .= " ".price2num($this->total_localtax2).",";
3547  $sql .= " ".price2num($this->total_ttc).",";
3548  $sql .= ($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'" : "null");
3549  $sql .= ", ".($this->fk_multicurrency ? $this->fk_multicurrency : "null");
3550  $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
3551  $sql .= ", ".($this->multicurrency_subprice ? price2num($this->multicurrency_subprice) : '0');
3552  $sql .= ", ".($this->multicurrency_total_ht ? price2num($this->multicurrency_total_ht) : '0');
3553  $sql .= ", ".($this->multicurrency_total_tva ? price2num($this->multicurrency_total_tva) : '0');
3554  $sql .= ", ".($this->multicurrency_total_ttc ? price2num($this->multicurrency_total_ttc) : '0');
3555  $sql .= ")";
3556 
3557  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
3558  $resql = $this->db->query($sql);
3559  if ($resql)
3560  {
3561  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
3562  $this->rowid = $this->id;
3563 
3564  if (!$error)
3565  {
3566  $result = $this->insertExtraFields();
3567  if ($result < 0)
3568  {
3569  $error++;
3570  }
3571  }
3572 
3573  if (!$error && !$notrigger)
3574  {
3575  // Call trigger
3576  $result = $this->call_trigger('LINEORDER_SUPPLIER_CREATE', $user);
3577  if ($result < 0) $error++;
3578  // End call triggers
3579  }
3580 
3581  if (!$error) {
3582  $this->db->commit();
3583  return 1;
3584  }
3585 
3586  foreach ($this->errors as $errmsg)
3587  {
3588  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
3589  $this->errors[] = ($this->errors ? ', '.$errmsg : $errmsg);
3590  }
3591  $this->db->rollback();
3592  return -1 * $error;
3593  } else {
3594  $this->errors[] = $this->db->error();
3595  $this->db->rollback();
3596  return -2;
3597  }
3598  }
3605  public function update($notrigger = 0)
3606  {
3607  global $conf, $user;
3608 
3609  $error = 0;
3610 
3611  $this->db->begin();
3612 
3613  // Mise a jour ligne en base
3614  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
3615  $sql .= " description='".$this->db->escape($this->desc)."'";
3616  $sql .= ", ref='".$this->db->escape($this->ref_supplier)."'";
3617  $sql .= ", subprice='".price2num($this->subprice)."'";
3618  //$sql.= ",remise='".price2num($remise)."'";
3619  $sql .= ", remise_percent='".price2num($this->remise_percent)."'";
3620 
3621  $sql .= ", vat_src_code = '".(empty($this->vat_src_code) ? '' : $this->vat_src_code)."'";
3622  $sql .= ", tva_tx='".price2num($this->tva_tx)."'";
3623  $sql .= ", localtax1_tx='".price2num($this->total_localtax1)."'";
3624  $sql .= ", localtax2_tx='".price2num($this->total_localtax2)."'";
3625  $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
3626  $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
3627  $sql .= ", qty='".price2num($this->qty)."'";
3628  $sql .= ", date_start=".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null");
3629  $sql .= ", date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null");
3630  $sql .= ", info_bits='".$this->db->escape($this->info_bits)."'";
3631  $sql .= ", total_ht='".price2num($this->total_ht)."'";
3632  $sql .= ", total_tva='".price2num($this->total_tva)."'";
3633  $sql .= ", total_localtax1='".price2num($this->total_localtax1)."'";
3634  $sql .= ", total_localtax2='".price2num($this->total_localtax2)."'";
3635  $sql .= ", total_ttc='".price2num($this->total_ttc)."'";
3636  $sql .= ", product_type=".$this->product_type;
3637  $sql .= ", special_code=".(!empty($this->special_code) ? $this->special_code : 0);
3638  $sql .= ($this->fk_unit ? ", fk_unit='".$this->db->escape($this->fk_unit)."'" : ", fk_unit=null");
3639 
3640  // Multicurrency
3641  $sql .= ", multicurrency_subprice=".price2num($this->multicurrency_subprice)."";
3642  $sql .= ", multicurrency_total_ht=".price2num($this->multicurrency_total_ht)."";
3643  $sql .= ", multicurrency_total_tva=".price2num($this->multicurrency_total_tva)."";
3644  $sql .= ", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc)."";
3645 
3646  $sql .= " WHERE rowid = ".$this->id;
3647 
3648  dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
3649  $result = $this->db->query($sql);
3650  if ($result > 0)
3651  {
3652  if (!$error)
3653  {
3654  $result = $this->insertExtraFields();
3655  if ($result < 0)
3656  {
3657  $error++;
3658  }
3659  }
3660 
3661  if (!$error && !$notrigger)
3662  {
3663  global $user;
3664  // Call trigger
3665  $result = $this->call_trigger('LINEORDER_SUPPLIER_UPDATE', $user);
3666  if ($result < 0)
3667  {
3668  $this->db->rollback();
3669  return -1;
3670  }
3671  // End call triggers
3672  }
3673 
3674  if (!$error)
3675  {
3676  $this->db->commit();
3677  return 1;
3678  } else {
3679  $this->db->rollback();
3680  return -1;
3681  }
3682  } else {
3683  $this->error = $this->db->lasterror();
3684  $this->db->rollback();
3685  return -1;
3686  }
3687  }
3688 
3695  public function delete($notrigger = 0)
3696  {
3697  global $user;
3698 
3699  $error = 0;
3700 
3701  $this->db->begin();
3702 
3703  // extrafields
3704  $result = $this->deleteExtraFields();
3705  if ($result < 0)
3706  {
3707  $this->db->rollback();
3708  return -1;
3709  }
3710 
3711  $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commande_fournisseurdet WHERE rowid=".$this->id;
3712 
3713  dol_syslog(__METHOD__, LOG_DEBUG);
3714  $resql = $this->db->query($sql);
3715  if ($resql)
3716  {
3717  if (!$notrigger)
3718  {
3719  // Call trigger
3720  $result = $this->call_trigger('LINEORDER_SUPPLIER_DELETE', $user);
3721  if ($result < 0) $error++;
3722  // End call triggers
3723  }
3724 
3725  if (!$error)
3726  {
3727  $this->db->commit();
3728  return 1;
3729  }
3730 
3731  foreach ($this->errors as $errmsg)
3732  {
3733  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
3734  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
3735  }
3736  $this->db->rollback();
3737  return -1 * $error;
3738  } else {
3739  $this->error = $this->db->lasterror();
3740  return -1;
3741  }
3742  }
3743 }
Class to manage stock movements.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
Cancel($user, $idwarehouse=-1)
Cancel an approved order.
const STATUS_DRAFT
Draft status.
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
Definition: list.php:632
static getIdAndTxFromCode($db, $code, $date_document= '')
Get id and rate of currency from code.
deleteline($idline, $notrigger=0)
Delete line.
const STATUS_RECEIVED_COMPLETELY
Received completely.
fetch($id, $ref= '')
Get object and lines from database.
Class to manage table commandefournisseurdispatch.
</td >< tdcolspan="3">< spanclass="opacitymedium"></span ></td ></tr >< trclass="liste_total"> CREANCES DETTES< tdcolspan="3"class="right"></td >< tdcolspan="3"class="right"></td ></tr > CREANCES DETTES RECETTES DEPENSES trips CREANCES DETTES Y m expensereport p date_valid Y m expensereport pe datep $db idate($date_start)."' AND $column < p rowid
Class to manage products or services.
const STATUS_CANCELED_AFTER_ORDER
Order canceled/never received.
dol_now($mode= 'auto')
Return date for now.
line_order($renum=false, $rowidorder= 'ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
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
Class to manage Dolibarr users.
Definition: user.class.php:44
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.
showDelay()
Show the customer delayed info.
Class to manage Dolibarr database access.
add_contact($fk_socpeople, $type_contact, $source= 'external', $notrigger=0)
Add a link between element $this-&gt;element and a contact.
setEventMessage($mesgs, $style= 'mesgs')
Set event message in dol_events session object.
getMaxDeliveryTimeDay($langs)
Return the max number delivery delay in day.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
hasDelay()
Is the supplier order delayed? We suppose a purchase ordered as late if a the purchase order has been...
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)
Add order line.
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...
getNextNumRef($soc)
Returns the following order reference not used depending on the numbering model activated defined wit...
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
refuse($user)
Refuse an order.
updateFromCommandeClient($user, $idc, $comclientid)
Update a supplier order from a customer order.
$conf db
API class for accounts.
Definition: inc.php:54
create($user, $notrigger=0)
Create order with draft status.
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...
insertExtraFields($trigger= '', $userused=null)
Add/Update all extra fields values for the current object.
getInputMethod()
Returns the translated input method of object (defined if $this-&gt;methode_commande_id &gt; 0)...
set_date_livraison($user, $delivery_date, $notrigger=0)
Set delivery date.
Class to manage third parties objects (customers, suppliers, prospects...)
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
getNomUrl($withpicto=0, $option= '', $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0)
Return clicable name (with picto eventually)
LibStatut($status, $mode=0, $billed=0)
Return label of a status.
deleteEcmFiles($mode=0)
Delete related files of object in database.
info($id)
Charge les informations d&#39;ordre info dans l&#39;objet facture.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static getIdFromCode($db, $code)
Get id of currency from code.
const STATUS_RECEIVED_PARTIALLY
Received partially.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
Class to manage customers orders.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
Definition: files.lib.php:1286
const STATUS_VALIDATED
Validated status.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
Remove a file or several files with a mask.
Definition: files.lib.php:1144
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
calcAndSetStatusDispatch(User $user, $closeopenorder=1, $comment= '')
Calc status regarding to dispatched stock.
deleteExtraFields()
Delete all extra fields values for the current object.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
load_board($user, $mode= 'opened')
Load indicators for dashboard (this-&gt;nbtodo and this-&gt;nbtodolate)
classifyBilled(User $user)
Class invoiced the supplier order.
setStatus($user, $status)
Tag order with a particular status.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this-&gt;array_options This method is in most cases call...
Superclass for orders classes.
commande($user, $date, $methode, $comment= '')
Submit a supplier order to supplier.
Class to manage predefined suppliers products.
deleteObjectLinked($sourceid=null, $sourcetype= '', $targetid=null, $targettype= '', $rowid= '')
Delete all links between an object $this.
Livraison($user, $date, $type, $comment)
Set a delivery in database for this supplier order.
print $_SERVER["PHP_SELF"]
Edit parameters.
getDispachedLines($status=-1)
Return array of dispatched lines waiting to be approved for this order.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
call_trigger($triggerName, $user)
Call trigger based on this instance.
const STATUS_ORDERSENT
Order sent, shipment on process.
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...
Superclass for orders classes.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template model.
valid($user, $idwarehouse=0, $notrigger=0)
Validate an order.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
update($notrigger=0)
Update the line object into db.
initAsSpecimen()
Initialise an instance with random values.
load_state_board()
Charge indicateurs this-&gt;nb de tableau de bord.
getLibStatut($mode=0)
Return label of the status of object.
getRights()
Returns the rights used for this class.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
approve($user, $idwarehouse=0, $secondlevel=0)
Approve a supplier order.
add_object_linked($origin=null, $origin_id=null)
Add objects linked in llx_element_element.
const STATUS_CANCELED
Order canceled.
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)...
fetch_lines($only_product=0)
Load array lines.
get_methodes_commande()
Get list of order methods.
insert($notrigger=0)
Insert line into database.
set_id_projet($user, $id_projet, $notrigger=0)
Set the id projet.
loadReceptions($filtre_statut=-1)
Load array this-&gt;receptions of lines of shipments with nb of products sent for each order line Note: ...
Class to manage predefined suppliers products.
if(!empty($search_group)) natural_search(array("g.nom"g note
Definition: list.php:122
createFromClone(User $user, $socid=0)
Load an object from its id and create a new one in database.
Class to manage line orders.
setDeliveryDate($user, $delivery_date, $notrigger=0)
Set the planned delivery date.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...