dolibarr  7.0.0-beta
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@capnetworks.com>
5  * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
6  * Copyright (C) 2010-2014 Juanjo Menent <jmenent@2byte.es>
7  * Copyright (C) 2010-2016 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  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
32 include_once DOL_DOCUMENT_ROOT.'/core/class/commonorder.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
34 if (! empty($conf->productbatch->enabled)) require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
35 require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
36 
41 {
42  public $element='order_supplier';
43  public $table_element='commande_fournisseur';
44  public $table_element_line = 'commande_fournisseurdet';
45  public $fk_element = 'fk_commande';
46  public $picto='order';
51  public $ismultientitymanaged = 1;
56  public $restrictiononfksoc = 1;
57 
61  protected $table_ref_field = 'ref';
62 
63  public $id;
64 
69  public $ref;
70  public $ref_supplier;
71  public $brouillon;
72  public $statut; // 0=Draft -> 1=Validated -> 2=Approved -> 3=Ordered/Process runing -> 4=Received partially -> 5=Received totally -> (reopen) 4=Received partially
73  // -> 7=Canceled/Never received -> (reopen) 3=Process runing
74  // -> 6=Canceled -> (reopen) 2=Approved
75  // -> 9=Refused -> (reopen) 1=Validated
76  // Note: billed or not is on another field "billed"
77  public $statuts; // List of status
78 
79  public $socid;
80  public $fourn_id;
81  public $date;
82  public $date_valid;
83  public $date_approve;
84  public $date_approve2; // Used when SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED is set
85  public $date_commande;
86 
91  public $total_ht;
92  public $total_tva;
93  public $total_localtax1; // Total Local tax 1
94  public $total_localtax2; // Total Local tax 2
95  public $total_ttc;
96  public $source;
101  public $note;
102  public $note_private;
103  public $note_public;
104  public $model_pdf;
105  public $fk_project;
106  public $cond_reglement_id;
107  public $cond_reglement_code;
108  public $fk_account;
109  public $mode_reglement_id;
110  public $mode_reglement_code;
111  public $user_author_id;
112  public $user_valid_id;
113  public $user_approve_id;
114  public $user_approve_id2; // Used when SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED is set
115 
116  //Incoterms
117  public $fk_incoterms;
118  public $location_incoterms;
119  public $libelle_incoterms; //Used into tooltip
120 
121  public $extraparams=array();
122 
126  public $lines = array();
127  //Add for supplier_proposal
128  public $origin;
129  public $origin_id;
130  public $linked_objects=array();
131 
132  // Multicurrency
133  public $fk_multicurrency;
134  public $multicurrency_code;
135  public $multicurrency_tx;
136  public $multicurrency_total_ht;
137  public $multicurrency_total_tva;
138  public $multicurrency_total_ttc;
139 
143  const STATUS_DRAFT = 0;
147  const STATUS_VALIDATED = 1;
151  const STATUS_ACCEPTED = 2;
155  const STATUS_ORDERSENT = 3;
167  const STATUS_CANCELED = 6;
175  const STATUS_REFUSED = 9;
176 
177 
178 
179 
185  public function __construct($db)
186  {
187  global $conf;
188 
189  $this->db = $db;
190  $this->products = array();
191 
192  // List of language codes for status
193  $this->statuts[0] = 'StatusOrderDraft';
194  $this->statuts[1] = 'StatusOrderValidated';
195  $this->statuts[2] = 'StatusOrderApproved';
196  if (empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) $this->statuts[3] = 'StatusOrderOnProcess';
197  else $this->statuts[3] = 'StatusOrderOnProcessWithValidation';
198  $this->statuts[4] = 'StatusOrderReceivedPartially';
199  $this->statuts[5] = 'StatusOrderReceivedAll';
200  $this->statuts[6] = 'StatusOrderCanceled'; // Approved->Canceled
201  $this->statuts[7] = 'StatusOrderCanceled'; // Process running->canceled
202  //$this->statuts[8] = 'StatusOrderBilled'; // Everything is finished, order received totally and bill received
203  $this->statuts[9] = 'StatusOrderRefused';
204  }
205 
206 
214  public function fetch($id,$ref='')
215  {
216  global $conf;
217 
218  // Check parameters
219  if (empty($id) && empty($ref)) return -1;
220 
221  $sql = "SELECT c.rowid, c.ref, ref_supplier, c.fk_soc, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.tva as total_vat,";
222  $sql.= " c.localtax1, c.localtax2, ";
223  $sql.= " c.date_creation, c.date_valid, c.date_approve, c.date_approve2,";
224  $sql.= " c.fk_user_author, c.fk_user_valid, c.fk_user_approve, c.fk_user_approve2,";
225  $sql.= " c.date_commande as date_commande, c.date_livraison as date_livraison, c.fk_cond_reglement, c.fk_mode_reglement, c.fk_projet as fk_project, c.remise_percent, c.source, c.fk_input_method,";
226  $sql.= " c.fk_account,";
227  $sql.= " c.note_private, c.note_public, c.model_pdf, c.extraparams, c.billed,";
228  $sql.= " c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc,";
229  $sql.= " cm.libelle as methode_commande,";
230  $sql.= " cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle,";
231  $sql.= " p.code as mode_reglement_code, p.libelle as mode_reglement_libelle";
232  $sql.= ', c.fk_incoterms, c.location_incoterms';
233  $sql.= ', i.libelle as libelle_incoterms';
234  $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c";
235  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_payment_term as cr ON c.fk_cond_reglement = cr.rowid AND cr.entity IN (".getEntity('c_payment_term').")";
236  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as p ON c.fk_mode_reglement = p.id AND p.entity IN (".getEntity('c_paiement').")";
237  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_input_method as cm ON cm.rowid = c.fk_input_method";
238  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON c.fk_incoterms = i.rowid';
239  $sql.= " WHERE c.entity = ".$conf->entity;
240  if ($ref) $sql.= " AND c.ref='".$this->db->escape($ref)."'";
241  else $sql.= " AND c.rowid=".$id;
242 
243  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
244  $resql = $this->db->query($sql);
245  if ($resql)
246  {
247  $obj = $this->db->fetch_object($resql);
248  if (! $obj)
249  {
250  $this->error='Bill with id '.$id.' not found';
251  dol_syslog(get_class($this).'::fetch '.$this->error);
252  return 0;
253  }
254 
255  $this->id = $obj->rowid;
256  $this->ref = $obj->ref;
257  $this->ref_supplier = $obj->ref_supplier;
258  $this->socid = $obj->fk_soc;
259  $this->fourn_id = $obj->fk_soc;
260  $this->statut = $obj->fk_statut;
261  $this->billed = $obj->billed;
262  $this->user_author_id = $obj->fk_user_author;
263  $this->user_valid_id = $obj->fk_user_valid;
264  $this->user_approve_id = $obj->fk_user_approve;
265  $this->user_approve_id2 = $obj->fk_user_approve2;
266  $this->total_ht = $obj->total_ht;
267  $this->total_tva = $obj->total_vat;
268  $this->total_localtax1 = $obj->localtax1;
269  $this->total_localtax2 = $obj->localtax2;
270  $this->total_ttc = $obj->total_ttc;
271  $this->date = $this->db->jdate($obj->date_creation);
272  $this->date_valid = $this->db->jdate($obj->date_valid);
273  $this->date_approve = $this->db->jdate($obj->date_approve);
274  $this->date_approve2 = $this->db->jdate($obj->date_approve2);
275  $this->date_commande = $this->db->jdate($obj->date_commande); // date we make the order to supplier
276  $this->date_livraison = $this->db->jdate($obj->date_livraison);
277  $this->remise_percent = $obj->remise_percent;
278  $this->methode_commande_id = $obj->fk_input_method;
279  $this->methode_commande = $obj->methode_commande;
280 
281  $this->source = $obj->source;
282  $this->fk_project = $obj->fk_project;
283  $this->cond_reglement_id = $obj->fk_cond_reglement;
284  $this->cond_reglement_code = $obj->cond_reglement_code;
285  $this->cond_reglement = $obj->cond_reglement_libelle;
286  $this->cond_reglement_doc = $obj->cond_reglement_libelle;
287  $this->fk_account = $obj->fk_account;
288  $this->mode_reglement_id = $obj->fk_mode_reglement;
289  $this->mode_reglement_code = $obj->mode_reglement_code;
290  $this->mode_reglement = $obj->mode_reglement_libelle;
291  $this->note = $obj->note_private; // deprecated
292  $this->note_private = $obj->note_private;
293  $this->note_public = $obj->note_public;
294  $this->modelpdf = $obj->model_pdf;
295 
296  //Incoterms
297  $this->fk_incoterms = $obj->fk_incoterms;
298  $this->location_incoterms = $obj->location_incoterms;
299  $this->libelle_incoterms = $obj->libelle_incoterms;
300 
301  // Multicurrency
302  $this->fk_multicurrency = $obj->fk_multicurrency;
303  $this->multicurrency_code = $obj->multicurrency_code;
304  $this->multicurrency_tx = $obj->multicurrency_tx;
305  $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
306  $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
307  $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
308 
309  $this->extraparams = (array) json_decode($obj->extraparams, true);
310 
311  $this->db->free($resql);
312 
313  // Retrieve all extrafields
314  // fetch optionals attributes and labels
315  require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
316  $extrafields=new ExtraFields($this->db);
317  $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
318  $this->fetch_optionals($this->id,$extralabels);
319 
320  if ($this->statut == 0) $this->brouillon = 1;
321 
322  $this->fetchObjectLinked();
323 
324  //$result=$this->fetch_lines();
325  $this->lines=array();
326 
327  $sql = "SELECT l.rowid, l.ref as ref_supplier, l.fk_product, l.product_type, l.label, l.description, l.qty,";
328  $sql.= " l.vat_src_code, l.tva_tx, l.remise_percent, l.subprice,";
329  $sql.= " l.localtax1_tx, l. localtax2_tx, l.localtax1_type, l. localtax2_type, l.total_localtax1, l.total_localtax2,";
330  $sql.= " l.total_ht, l.total_tva, l.total_ttc, l.special_code, l.fk_parent_line, l.rang,";
331  $sql.= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc,";
332  $sql.= " l.fk_unit,";
333  $sql.= " l.date_start, l.date_end,";
334  $sql.= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc';
335  $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as l";
336  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
337  $sql.= " WHERE l.fk_commande = ".$this->id;
338  $sql.= " ORDER BY l.rang, l.rowid";
339  //print $sql;
340 
341  dol_syslog(get_class($this)."::fetch get lines", LOG_DEBUG);
342  $result = $this->db->query($sql);
343  if ($result)
344  {
345  $num = $this->db->num_rows($result);
346  $i = 0;
347 
348  while ($i < $num)
349  {
350  $objp = $this->db->fetch_object($result);
351 
352  $line = new CommandeFournisseurLigne($this->db);
353 
354  $line->id = $objp->rowid;
355  $line->desc = $objp->description;
356  $line->description = $objp->description;
357  $line->qty = $objp->qty;
358  $line->tva_tx = $objp->tva_tx;
359  $line->localtax1_tx = $objp->localtax1_tx;
360  $line->localtax2_tx = $objp->localtax2_tx;
361  $line->localtax1_type = $objp->localtax1_type;
362  $line->localtax2_type = $objp->localtax2_type;
363  $line->subprice = $objp->subprice;
364  $line->pu_ht = $objp->subprice;
365  $line->remise_percent = $objp->remise_percent;
366 
367  $line->vat_src_code = $objp->vat_src_code;
368  $line->total_ht = $objp->total_ht;
369  $line->total_tva = $objp->total_tva;
370  $line->total_localtax1 = $objp->total_localtax1;
371  $line->total_localtax2 = $objp->total_localtax2;
372  $line->total_ttc = $objp->total_ttc;
373  $line->product_type = $objp->product_type;
374 
375  $line->fk_product = $objp->fk_product;
376 
377  $line->libelle = $objp->product_label;
378  $line->product_label = $objp->product_label;
379  $line->product_desc = $objp->product_desc;
380 
381  $line->ref = $objp->product_ref; // Ref of product
382  $line->product_ref = $objp->product_ref; // Ref of product
383  $line->ref_fourn = $objp->ref_supplier; // The supplier ref of price when product was added. May have change since
384  $line->ref_supplier = $objp->ref_supplier; // The supplier ref of price when product was added. May have change since
385 
386  $line->date_start = $this->db->jdate($objp->date_start);
387  $line->date_end = $this->db->jdate($objp->date_end);
388  $line->fk_unit = $objp->fk_unit;
389 
390  // Multicurrency
391  $line->fk_multicurrency = $objp->fk_multicurrency;
392  $line->multicurrency_code = $objp->multicurrency_code;
393  $line->multicurrency_subprice = $objp->multicurrency_subprice;
394  $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
395  $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
396  $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
397 
398  $line->special_code = $objp->special_code;
399  $line->fk_parent_line = $objp->fk_parent_line;
400 
401  $line->rang = $objp->rang;
402 
403  $this->lines[$i] = $line;
404 
405  $i++;
406  }
407  $this->db->free($result);
408 
409  return 1;
410  }
411  else
412  {
413  $this->error=$this->db->error()." sql=".$sql;
414  return -1;
415  }
416  }
417  else
418  {
419  $this->error=$this->db->error()." sql=".$sql;
420  return -1;
421  }
422  }
423 
432  public function valid($user,$idwarehouse=0,$notrigger=0)
433  {
434  global $langs,$conf;
435  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
436 
437  $error=0;
438 
439  dol_syslog(get_class($this)."::valid");
440  $result = 0;
441  if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->fournisseur->commande->creer))
442  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->fournisseur->supplier_order_advance->validate)))
443  {
444  $this->db->begin();
445 
446  // Definition of supplier order numbering model name
447  $soc = new Societe($this->db);
448  $soc->fetch($this->fourn_id);
449 
450  // Check if object has a temporary ref
451  if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) // empty should not happened, but when it occurs, the test save life
452  {
453  $num = $this->getNextNumRef($soc);
454  }
455  else
456  {
457  $num = $this->ref;
458  }
459  $this->newref = $num;
460 
461  $sql = 'UPDATE '.MAIN_DB_PREFIX."commande_fournisseur";
462  $sql.= " SET ref='".$this->db->escape($num)."',";
463  $sql.= " fk_statut = ".self::STATUS_VALIDATED.",";
464  $sql.= " date_valid='".$this->db->idate(dol_now())."',";
465  $sql.= " fk_user_valid = ".$user->id;
466  $sql.= " WHERE rowid = ".$this->id;
467  $sql.= " AND fk_statut = ".self::STATUS_DRAFT;
468 
469  $resql=$this->db->query($sql);
470  if (! $resql)
471  {
472  dol_print_error($this->db);
473  $error++;
474  }
475 
476  if (! $error && ! $notrigger)
477  {
478  // Call trigger
479  $result=$this->call_trigger('ORDER_SUPPLIER_VALIDATE',$user);
480  if ($result < 0) $error++;
481  // End call triggers
482  }
483 
484  if (! $error)
485  {
486  $this->oldref = $this->ref;
487 
488  // Rename directory if dir was a temporary ref
489  if (preg_match('/^[\(]?PROV/i', $this->ref))
490  {
491  // We rename directory ($this->ref = ancienne ref, $num = nouvelle ref)
492  // in order not to lose the attached files
493  $oldref = dol_sanitizeFileName($this->ref);
494  $newref = dol_sanitizeFileName($num);
495  $dirsource = $conf->fournisseur->commande->dir_output.'/'.$oldref;
496  $dirdest = $conf->fournisseur->commande->dir_output.'/'.$newref;
497  if (file_exists($dirsource))
498  {
499  dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
500 
501  if (@rename($dirsource, $dirdest))
502  {
503  dol_syslog("Rename ok");
504  // Rename docs starting with $oldref with $newref
505  $listoffiles=dol_dir_list($conf->fournisseur->commande->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref,'/'));
506  foreach($listoffiles as $fileentry)
507  {
508  $dirsource=$fileentry['name'];
509  $dirdest=preg_replace('/^'.preg_quote($oldref,'/').'/',$newref, $dirsource);
510  $dirsource=$fileentry['path'].'/'.$dirsource;
511  $dirdest=$fileentry['path'].'/'.$dirdest;
512  @rename($dirsource, $dirdest);
513  }
514  }
515  }
516  }
517  }
518 
519  if (! $error)
520  {
521  $result = 1;
522  $this->statut = self::STATUS_VALIDATED;
523  $this->ref = $num;
524  }
525 
526  if (! $error)
527  {
528  $this->db->commit();
529  return 1;
530  }
531  else
532  {
533  $this->db->rollback();
534  return -1;
535  }
536  }
537  else
538  {
539  $this->error='NotAuthorized';
540  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
541  return -1;
542  }
543  }
544 
551  public function getLibStatut($mode=0)
552  {
553  return $this->LibStatut($this->statut,$mode,$this->billed);
554  }
555 
564  function LibStatut($statut,$mode=0,$billed=0)
565  {
566  global $langs;
567  $langs->load('orders');
568 
569  $billedtext='';
570  //if ($statut==5 && $this->billed == 1) $statut = 8;
571  if ($billed == 1) $billedtext=$langs->trans("Billed");
572 
573  // List of language codes for status
574  $statutshort[0] = 'StatusOrderDraftShort';
575  $statutshort[1] = 'StatusOrderValidatedShort';
576  $statutshort[2] = 'StatusOrderApprovedShort';
577  $statutshort[3] = 'StatusOrderOnProcessShort';
578  $statutshort[4] = 'StatusOrderReceivedPartiallyShort';
579  $statutshort[5] = 'StatusOrderReceivedAllShort';
580  $statutshort[6] = 'StatusOrderCanceledShort';
581  $statutshort[7] = 'StatusOrderCanceledShort';
582  $statutshort[9] = 'StatusOrderRefusedShort';
583 
584  if ($mode == 0)
585  {
586  return $langs->trans($this->statuts[$statut]);
587  }
588  if ($mode == 1)
589  {
590  return $langs->trans($statutshort[$statut]);
591  }
592  if ($mode == 2)
593  {
594  return $langs->trans($this->statuts[$statut]);
595  }
596  if ($mode == 3)
597  {
598  if ($statut==0) return img_picto($langs->trans($this->statuts[$statut]),'statut0');
599  if ($statut==1) return img_picto($langs->trans($this->statuts[$statut]),'statut1');
600  if ($statut==2) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
601  if ($statut==3) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
602  if ($statut==4) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
603  if ($statut==5) return img_picto($langs->trans($this->statuts[$statut]),'statut6');
604  if ($statut==6 || $statut==7) return img_picto($langs->trans($this->statuts[$statut]),'statut5');
605  if ($statut==9) return img_picto($langs->trans($this->statuts[$statut]),'statut5');
606  }
607  if ($mode == 4)
608  {
609  if ($statut==0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
610  if ($statut==1) return img_picto($langs->trans($this->statuts[$statut]),'statut1').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
611  if ($statut==2) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
612  if ($statut==3) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
613  if ($statut==4) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
614  if ($statut==5) return img_picto($langs->trans($this->statuts[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
615  if ($statut==6 || $statut==7) return img_picto($langs->trans($this->statuts[$statut]),'statut5').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
616  if ($statut==9) return img_picto($langs->trans($this->statuts[$statut]),'statut5').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
617  }
618  if ($mode == 5)
619  {
620  if ($statut==0) return '<span class="hideonsmartphone">'.$langs->trans($statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut0');
621  if ($statut==1) return '<span class="hideonsmartphone">'.$langs->trans($statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut1');
622  if ($statut==2) return '<span class="hideonsmartphone">'.$langs->trans($statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut3');
623  if ($statut==3) return '<span class="hideonsmartphone">'.$langs->trans($statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut3');
624  if ($statut==4) return '<span class="hideonsmartphone">'.$langs->trans($statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut3');
625  if ($statut==5) return '<span class="hideonsmartphone">'.$langs->trans($statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut6');
626  if ($statut==6 || $statut==7) return '<span class="hideonsmartphone">'.$langs->trans($statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut5');
627  if ($statut==9) return '<span class="hideonsmartphone">'.$langs->trans($statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut5');
628  }
629  }
630 
631 
641  public function getNomUrl($withpicto=0, $option='', $notooltip=0, $save_lastsearch_value=-1)
642  {
643  global $langs, $conf;
644 
645  $result='';
646  $label = '<u>' . $langs->trans("ShowOrder") . '</u>';
647  if (! empty($this->ref))
648  $label .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
649  if (! empty($this->ref_supplier))
650  $label.= '<br><b>' . $langs->trans('RefSupplier') . ':</b> ' . $this->ref_supplier;
651  if (! empty($this->total_ht))
652  $label.= '<br><b>' . $langs->trans('AmountHT') . ':</b> ' . price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
653  if (! empty($this->total_tva))
654  $label.= '<br><b>' . $langs->trans('VAT') . ':</b> ' . price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
655  if (! empty($this->total_ttc))
656  $label.= '<br><b>' . $langs->trans('AmountTTC') . ':</b> ' . price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
657 
658  $picto='order';
659  $url = DOL_URL_ROOT.'/fourn/commande/card.php?id='.$this->id;
660 
661  if ($option !== 'nolink')
662  {
663  // Add param to save lastsearch_values or not
664  $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
665  if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
666  if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
667  }
668 
669  $linkclose='';
670  if (empty($notooltip))
671  {
672  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
673  {
674  $label=$langs->trans("ShowOrder");
675  $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
676  }
677  $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
678  $linkclose.=' class="classfortooltip"';
679  }
680 
681  $linkstart = '<a href="'.$url.'"';
682  $linkstart.=$linkclose.'>';
683  $linkend='</a>';
684 
685  $result .= $linkstart;
686  if ($withpicto) $result.=img_object(($notooltip?'':$label), $this->picto, ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
687  if ($withpicto != 2) $result.= $this->ref;
688  $result .= $linkend;
689 
690  return $result;
691  }
692 
693 
701  public function getNextNumRef($soc)
702  {
703  global $db, $langs, $conf;
704  $langs->load("orders");
705 
706  if (! empty($conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER))
707  {
708  $mybool = false;
709 
710  $file = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER.'.php';
711  $classname=$conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER;
712 
713  // Include file with class
714  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
715 
716  foreach ($dirmodels as $reldir) {
717 
718  $dir = dol_buildpath($reldir."core/modules/supplier_order/");
719 
720  // Load file with numbering class (if found)
721  $mybool|=@include_once $dir.$file;
722  }
723 
724  if (! $mybool)
725  {
726  dol_print_error('',"Failed to include file ".$file);
727  return '';
728  }
729 
730  $obj = new $classname();
731  $numref = $obj->getNextValue($soc,$this);
732 
733  if ( $numref != "")
734  {
735  return $numref;
736  }
737  else
738  {
739  $this->error = $obj->error;
740  return -1;
741  }
742  }
743  else
744  {
745  $this->error = "Error_COMMANDE_SUPPLIER_ADDON_NotDefined";
746  return -2;
747  }
748  }
755  public function classifyBilled(User $user)
756  {
757  $error=0;
758  $this->db->begin();
759 
760  $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande_fournisseur SET billed = 1';
761  $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut > '.self::STATUS_DRAFT;
762  if ($this->db->query($sql))
763  {
764  if (! $error)
765  {
766  // Call trigger
767  $result=$this->call_trigger('ORDER_SUPPLIER_CLASSIFY_BILLED',$user);
768  if ($result < 0) $error++;
769  // End call triggers
770  }
771 
772  if (! $error)
773  {
774  $this->billed=1;
775 
776  $this->db->commit();
777  return 1;
778  }
779  else
780  {
781  $this->db->rollback();
782  return -1;
783  }
784  }
785  else
786  {
787  dol_print_error($this->db);
788 
789  $this->db->rollback();
790  return -1;
791  }
792  }
793 
802  public function approve($user, $idwarehouse=0, $secondlevel=0)
803  {
804  global $langs,$conf;
805  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
806 
807  $error=0;
808 
809  dol_syslog(get_class($this)."::approve");
810 
811  if ($user->rights->fournisseur->commande->approuver)
812  {
813  $now = dol_now();
814 
815  $this->db->begin();
816 
817  // Definition of order numbering model name
818  $soc = new Societe($this->db);
819  $soc->fetch($this->fourn_id);
820 
821  // Check if object has a temporary ref
822  if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) // empty should not happened, but when it occurs, the test save life
823  {
824  $num = $this->getNextNumRef($soc);
825  }
826  else
827  {
828  $num = $this->ref;
829  }
830  $this->newref = $num;
831 
832  // Do we have to change status now ? (If double approval is required and first approval, we keep status to 1 = validated)
833  $movetoapprovestatus=true;
834  $comment='';
835 
836  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
837  $sql.= " SET ref='".$this->db->escape($num)."',";
838  if (empty($secondlevel)) // standard or first level approval
839  {
840  $sql.= " date_approve='".$this->db->idate($now)."',";
841  $sql.= " fk_user_approve = ".$user->id;
842  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)
843  {
844  if (empty($this->user_approve_id2))
845  {
846  $movetoapprovestatus=false; // second level approval not done
847  $comment=' (first level)';
848  }
849  }
850  }
851  else // request a second level approval
852  {
853  $sql.= " date_approve2='".$this->db->idate($now)."',";
854  $sql.= " fk_user_approve2 = ".$user->id;
855  if (empty($this->user_approve_id)) $movetoapprovestatus=false; // first level approval not done
856  $comment=' (second level)';
857  }
858  // If double approval is required and first approval, we keep status to 1 = validated
859  if ($movetoapprovestatus) $sql.= ", fk_statut = ".self::STATUS_ACCEPTED;
860  else $sql.= ", fk_statut = ".self::STATUS_VALIDATED;
861  $sql.= " WHERE rowid = ".$this->id;
862  $sql.= " AND fk_statut = ".self::STATUS_VALIDATED;
863 
864  if ($this->db->query($sql))
865  {
866  if (! empty($conf->global->SUPPLIER_ORDER_AUTOADD_USER_CONTACT))
867  {
868  $result=$this->add_contact($user->id, 'SALESREPFOLL', 'internal', 1);
869  if ($result < 0 && $result != -2) // -2 means already exists
870  {
871  $error++;
872  }
873  }
874 
875  // If stock is incremented on validate order, we must increment it
876  if (! $error && $movetoapprovestatus && ! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER))
877  {
878  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
879  $langs->load("agenda");
880 
881  $cpt=count($this->lines);
882  for ($i = 0; $i < $cpt; $i++)
883  {
884  // Product with reference
885  if ($this->lines[$i]->fk_product > 0)
886  {
887  $this->line = $this->lines[$i];
888  $mouvP = new MouvementStock($this->db);
889  $mouvP->origin = &$this;
890  // We decrement stock of product (and sub-products)
891  $up_ht_disc=$this->lines[$i]->subprice;
892  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');
893  $result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("OrderApprovedInDolibarr",$this->ref));
894  if ($result < 0) { $error++; }
895  unset($this->line);
896  }
897  }
898  }
899 
900  if (! $error)
901  {
902  // Call trigger
903  $result=$this->call_trigger('ORDER_SUPPLIER_APPROVE',$user);
904  if ($result < 0) $error++;
905  // End call triggers
906  }
907 
908  if (! $error)
909  {
910  $this->ref = $this->newref;
911 
912  if ($movetoapprovestatus) $this->statut = self::STATUS_ACCEPTED;
913  else $this->statut = self::STATUS_VALIDATED;
914  if (empty($secondlevel)) // standard or first level approval
915  {
916  $this->date_approve = $now;
917  $this->user_approve_id = $user->id;
918  }
919  else // request a second level approval
920  {
921  $this->date_approve2 = $now;
922  $this->user_approve_id2 = $user->id;
923  }
924 
925  $this->db->commit();
926  return 1;
927  }
928  else
929  {
930  $this->db->rollback();
931  return -1;
932  }
933  }
934  else
935  {
936  $this->db->rollback();
937  $this->error=$this->db->lasterror();
938  return -1;
939  }
940  }
941  else
942  {
943  dol_syslog(get_class($this)."::approve Not Authorized", LOG_ERR);
944  }
945  return -1;
946  }
947 
954  public function refuse($user)
955  {
956  global $conf, $langs;
957 
958  $error=0;
959 
960  dol_syslog(get_class($this)."::refuse");
961  $result = 0;
962  if ($user->rights->fournisseur->commande->approuver)
963  {
964  $this->db->begin();
965 
966  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET fk_statut = ".self::STATUS_REFUSED;
967  $sql .= " WHERE rowid = ".$this->id;
968 
969  if ($this->db->query($sql))
970  {
971  $result = 0;
972 
973  if ($error == 0)
974  {
975  // Call trigger
976  $result=$this->call_trigger('ORDER_SUPPLIER_REFUSE',$user);
977  if ($result < 0)
978  {
979  $error++;
980  $this->db->rollback();
981  }
982  else
983  $this->db->commit();
984  // End call triggers
985  }
986  }
987  else
988  {
989  $this->db->rollback();
990  $this->error=$this->db->lasterror();
991  dol_syslog(get_class($this)."::refuse Error -1");
992  $result = -1;
993  }
994  }
995  else
996  {
997  dol_syslog(get_class($this)."::refuse Not Authorized");
998  }
999  return $result ;
1000  }
1001 
1010  function Cancel($user, $idwarehouse=-1)
1011  {
1012  global $langs,$conf;
1013 
1014  $error=0;
1015 
1016  //dol_syslog("CommandeFournisseur::Cancel");
1017  $result = 0;
1018  if ($user->rights->fournisseur->commande->commander)
1019  {
1020  $statut = self::STATUS_CANCELED;
1021 
1022  $this->db->begin();
1023 
1024  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET fk_statut = ".$statut;
1025  $sql .= " WHERE rowid = ".$this->id;
1026  dol_syslog(get_class($this)."::cancel", LOG_DEBUG);
1027  if ($this->db->query($sql))
1028  {
1029  $result = 0;
1030 
1031  // Call trigger
1032  $result=$this->call_trigger('ORDER_SUPPLIER_CANCEL',$user);
1033  if ($result < 0) $error++;
1034  // End call triggers
1035 
1036  if ($error == 0)
1037  {
1038  $this->db->commit();
1039  return 1;
1040  }
1041  else
1042  {
1043  $this->db->rollback();
1044  return -1;
1045  }
1046  }
1047  else
1048  {
1049  $this->db->rollback();
1050  $this->error=$this->db->lasterror();
1051  dol_syslog(get_class($this)."::cancel ".$this->error);
1052  return -1;
1053  }
1054  }
1055  else
1056  {
1057  dol_syslog(get_class($this)."::cancel Not Authorized");
1058  return -1;
1059  }
1060  }
1061 
1062 
1072  public function commande($user, $date, $methode, $comment='')
1073  {
1074  global $langs;
1075  dol_syslog(get_class($this)."::commande");
1076  $error = 0;
1077  if ($user->rights->fournisseur->commande->commander)
1078  {
1079  $this->db->begin();
1080 
1081  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET fk_statut = ".self::STATUS_ORDERSENT.", fk_input_method=".$methode.", date_commande='".$this->db->idate($date)."'";
1082  $sql .= " WHERE rowid = ".$this->id;
1083 
1084  dol_syslog(get_class($this)."::commande", LOG_DEBUG);
1085  if ($this->db->query($sql))
1086  {
1087  $this->statut = self::STATUS_ORDERSENT;
1088  $this->methode_commande_id = $methode;
1089  $this->date_commande = $date;
1090 
1091  // Call trigger
1092  $result=$this->call_trigger('ORDER_SUPPLIER_SUBMIT',$user);
1093  if ($result < 0) $error++;
1094  // End call triggers
1095  }
1096  else
1097  {
1098  $error++;
1099  $this->error = $this->db->lasterror();
1100  $this->errors[] = $this->db->lasterror();
1101  }
1102 
1103  if (! $error)
1104  {
1105  $this->db->commit();
1106  }
1107  else
1108  {
1109  $this->db->rollback();
1110  }
1111  }
1112  else
1113  {
1114  $error++;
1115  $this->error = $langs->trans('NotAuthorized');
1116  $this->errors[] = $langs->trans('NotAuthorized');
1117  dol_syslog(get_class($this)."::commande User not Authorized", LOG_WARNING);
1118  }
1119 
1120  return ($error ? -1 : 1);
1121  }
1122 
1130  public function create($user, $notrigger=0)
1131  {
1132  global $langs,$conf,$hookmanager;
1133 
1134  $this->db->begin();
1135 
1136  $error=0;
1137  $now=dol_now();
1138 
1139  // Clean parameters
1140  if (empty($this->source)) $this->source = 0;
1141 
1142  // Multicurrency (test on $this->multicurrency_tx because we should take the default rate only if not using origin rate)
1143  if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) list($this->fk_multicurrency,$this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code);
1144  else $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
1145  if (empty($this->fk_multicurrency))
1146  {
1147  $this->multicurrency_code = $conf->currency;
1148  $this->fk_multicurrency = 0;
1149  $this->multicurrency_tx = 1;
1150  }
1151 
1152  // We set order into draft status
1153  $this->brouillon = 1;
1154 
1155  $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur (";
1156  $sql.= "ref";
1157  $sql.= ", ref_supplier";
1158  $sql.= ", note_private";
1159  $sql.= ", note_public";
1160  $sql.= ", entity";
1161  $sql.= ", fk_soc";
1162  $sql.= ", fk_projet";
1163  $sql.= ", date_creation";
1164  $sql.= ", date_livraison";
1165  $sql.= ", fk_user_author";
1166  $sql.= ", fk_statut";
1167  $sql.= ", source";
1168  $sql.= ", model_pdf";
1169  $sql.= ", fk_mode_reglement";
1170  $sql.= ", fk_cond_reglement";
1171  $sql.= ", fk_account";
1172  $sql.= ", fk_incoterms, location_incoterms";
1173  $sql.= ", fk_multicurrency";
1174  $sql.= ", multicurrency_code";
1175  $sql.= ", multicurrency_tx";
1176  $sql.= ") ";
1177  $sql.= " VALUES (";
1178  $sql.= "''";
1179  $sql.= ", '".$this->db->escape($this->ref_supplier)."'";
1180  $sql.= ", '".$this->db->escape($this->note_private)."'";
1181  $sql.= ", '".$this->db->escape($this->note_public)."'";
1182  $sql.= ", ".$conf->entity;
1183  $sql.= ", ".$this->socid;
1184  $sql.= ", ".($this->fk_project > 0 ? $this->fk_project : "null");
1185  $sql.= ", '".$this->db->idate($now)."'";
1186  $sql.= ", ".($this->date_livraison?"'".$this->db->idate($this->date_livraison)."'":"null");
1187  $sql.= ", ".$user->id;
1188  $sql.= ", ".self::STATUS_DRAFT;
1189  $sql.= ", ".$this->db->escape($this->source);
1190  $sql.= ", '".$conf->global->COMMANDE_SUPPLIER_ADDON_PDF."'";
1191  $sql.= ", ".($this->mode_reglement_id > 0 ? $this->mode_reglement_id : 'null');
1192  $sql.= ", ".($this->cond_reglement_id > 0 ? $this->cond_reglement_id : 'null');
1193  $sql.= ", ".($this->fk_account>0?$this->fk_account:'NULL');
1194  $sql.= ", ".(int) $this->fk_incoterms;
1195  $sql.= ", '".$this->db->escape($this->location_incoterms)."'";
1196  $sql.= ", ".(int) $this->fk_multicurrency;
1197  $sql.= ", '".$this->db->escape($this->multicurrency_code)."'";
1198  $sql.= ", ".(double) $this->multicurrency_tx;
1199  $sql.= ")";
1200 
1201  dol_syslog(get_class($this)."::create", LOG_DEBUG);
1202  if ($this->db->query($sql))
1203  {
1204  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."commande_fournisseur");
1205 
1206  if ($this->id) {
1207  $num=count($this->lines);
1208 
1209  // insert products details into database
1210  for ($i=0;$i<$num;$i++)
1211  {
1212  $result = $this->addline( // This include test on qty if option SUPPLIER_ORDER_WITH_NOPRICEDEFINED is not set
1213  $this->lines[$i]->desc,
1214  $this->lines[$i]->subprice,
1215  $this->lines[$i]->qty,
1216  $this->lines[$i]->tva_tx,
1217  $this->lines[$i]->localtax1_tx,
1218  $this->lines[$i]->localtax2_tx,
1219  $this->lines[$i]->fk_product,
1220  0,
1221  $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
1222  $this->lines[$i]->remise_percent,
1223  'HT',
1224  0,
1225  $this->lines[$i]->product_type,
1226  $this->lines[$i]->info_bits,
1227  false,
1228  $this->lines[$i]->date_start,
1229  $this->lines[$i]->date_end,
1230  0,
1231  $this->lines[$i]->fk_unit
1232  );
1233  if ($result < 0)
1234  {
1235  dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING); // do not use dol_print_error here as it may be a functionnal error
1236  $this->db->rollback();
1237  return -1;
1238  }
1239  }
1240 
1241  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
1242  $sql.= " SET ref='(PROV".$this->id.")'";
1243  $sql.= " WHERE rowid=".$this->id;
1244  dol_syslog(get_class($this)."::create", LOG_DEBUG);
1245  if ($this->db->query($sql))
1246  {
1247  // Add link with price request and supplier order
1248  if ($this->id)
1249  {
1250  $this->ref="(PROV".$this->id.")";
1251 
1252  if (! empty($this->linkedObjectsIds) && empty($this->linked_objects)) // To use new linkedObjectsIds instead of old linked_objects
1253  {
1254  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
1255  }
1256 
1257  // Add object linked
1258  if (! $error && $this->id && is_array($this->linked_objects) && ! empty($this->linked_objects))
1259  {
1260  foreach($this->linked_objects as $origin => $tmp_origin_id)
1261  {
1262  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, ...))
1263  {
1264  foreach($tmp_origin_id as $origin_id)
1265  {
1266  $ret = $this->add_object_linked($origin, $origin_id);
1267  if (! $ret)
1268  {
1269  dol_print_error($this->db);
1270  $error++;
1271  }
1272  }
1273  }
1274  else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
1275  {
1276  $origin_id = $tmp_origin_id;
1277  $ret = $this->add_object_linked($origin, $origin_id);
1278  if (! $ret)
1279  {
1280  dol_print_error($this->db);
1281  $error++;
1282  }
1283  }
1284  }
1285  }
1286  }
1287 
1288  if (! $error)
1289  {
1290  $result=$this->insertExtraFields();
1291  if ($result < 0) $error++;
1292  }
1293 
1294  if (! $error && ! $notrigger)
1295  {
1296  // Call trigger
1297  $result=$this->call_trigger('ORDER_SUPPLIER_CREATE',$user);
1298  if ($result < 0)
1299  {
1300  $this->db->rollback();
1301  return -1;
1302  }
1303  // End call triggers
1304  }
1305 
1306  $this->db->commit();
1307  return $this->id;
1308  }
1309  else
1310  {
1311  $this->error=$this->db->lasterror();
1312  $this->db->rollback();
1313  return -2;
1314  }
1315  }
1316  }
1317  else
1318  {
1319  $this->error=$this->db->lasterror();
1320  $this->db->rollback();
1321  return -1;
1322  }
1323  }
1324 
1330  public function createFromClone()
1331  {
1332  global $conf,$user,$langs,$hookmanager;
1333 
1334  $error=0;
1335 
1336  $this->context['createfromclone'] = 'createfromclone';
1337 
1338  $this->db->begin();
1339 
1340  // Load source object
1341  $objFrom = clone $this;
1342 
1343  $this->id=0;
1344  $this->statut=self::STATUS_DRAFT;
1345 
1346  // Clear fields
1347  $this->user_author_id = $user->id;
1348  $this->user_valid = '';
1349  $this->date_creation = '';
1350  $this->date_validation = '';
1351  $this->ref_supplier = '';
1352  $this->user_approve_id = '';
1353  $this->user_approve_id2 = '';
1354  $this->date_approve = '';
1355  $this->date_approve2 = '';
1356 
1357  // Create clone
1358  $result=$this->create($user);
1359  if ($result < 0) $error++;
1360 
1361  if (! $error)
1362  {
1363  // Hook of thirdparty module
1364  if (is_object($hookmanager))
1365  {
1366  $parameters=array('objFrom'=>$objFrom);
1367  $action='';
1368  $reshook=$hookmanager->executeHooks('createFrom',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
1369  if ($reshook < 0) $error++;
1370  }
1371 
1372  // Call trigger
1373  $result=$this->call_trigger('ORDER_SUPPLIER_CLONE',$user);
1374  if ($result < 0) $error++;
1375  // End call triggers
1376  }
1377 
1378  unset($this->context['createfromclone']);
1379 
1380  // End
1381  if (! $error)
1382  {
1383  $this->db->commit();
1384  return $this->id;
1385  }
1386  else
1387  {
1388  $this->db->rollback();
1389  return -1;
1390  }
1391  }
1392 
1420  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)
1421  {
1422  global $langs,$mysoc,$conf;
1423 
1424  $error = 0;
1425 
1426  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, $fk_unit");
1427  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1428 
1429  // Clean parameters
1430  if (! $qty) $qty=1;
1431  if (! $info_bits) $info_bits=0;
1432  if (empty($txtva)) $txtva=0;
1433  if (empty($txlocaltax1)) $txlocaltax1=0;
1434  if (empty($txlocaltax2)) $txlocaltax2=0;
1435  if (empty($remise_percent)) $remise_percent=0;
1436 
1437  $remise_percent=price2num($remise_percent);
1438  $qty=price2num($qty);
1439  $pu_ht=price2num($pu_ht);
1440  $pu_ttc=price2num($pu_ttc);
1441  $txtva = price2num($txtva);
1442  $txlocaltax1 = price2num($txlocaltax1);
1443  $txlocaltax2 = price2num($txlocaltax2);
1444  if ($price_base_type=='HT')
1445  {
1446  $pu=$pu_ht;
1447  }
1448  else
1449  {
1450  $pu=$pu_ttc;
1451  }
1452  $desc=trim($desc);
1453 
1454  // Check parameters
1455  if ($qty < 1 && ! $fk_product)
1456  {
1457  $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Product"));
1458  return -1;
1459  }
1460  if ($type < 0) return -1;
1461 
1462  if ($this->statut == 0)
1463  {
1464  $this->db->begin();
1465 
1466  if ($fk_product > 0)
1467  {
1468  if (empty($conf->global->SUPPLIER_ORDER_WITH_NOPRICEDEFINED))
1469  {
1470  // Check quantity is enough
1471  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);
1472  $prod = new Product($this->db, $fk_product);
1473  if ($prod->fetch($fk_product) > 0)
1474  {
1475  $product_type = $prod->type;
1476  $label = $prod->label;
1477 
1478  // We use 'none' instead of $ref_supplier, because fourn_ref may not exists anymore. So we will take the first supplier price ok.
1479  // If we want a dedicated supplier price, we must provide $fk_prod_fourn_price.
1480  $result=$prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product, 'none', ($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
1481  if ($result > 0)
1482  {
1483  $pu = $prod->fourn_pu; // Unit price supplier price set by get_buyprice
1484  $ref_supplier = $prod->ref_supplier; // Ref supplier price set by get_buyprice
1485  // is remise percent not keyed but present for the product we add it
1486  if ($remise_percent == 0 && $prod->remise_percent !=0)
1487  $remise_percent =$prod->remise_percent;
1488 
1489 
1490  }
1491  if ($result == 0) // If result == 0, we failed to found the supplier reference price
1492  {
1493  $langs->load("errors");
1494  $this->error = "Ref " . $prod->ref . " " . $langs->trans("ErrorQtyTooLowForThisSupplier");
1495  $this->db->rollback();
1496  dol_syslog(get_class($this)."::addline we did not found supplier price, so we can't guess unit price");
1497  //$pu = $prod->fourn_pu; // We do not overwrite unit price
1498  //$ref = $prod->ref_fourn; // We do not overwrite ref supplier price
1499  return -1;
1500  }
1501  if ($result == -1)
1502  {
1503  $langs->load("errors");
1504  $this->error = "Ref " . $prod->ref . " " . $langs->trans("ErrorQtyTooLowForThisSupplier");
1505  $this->db->rollback();
1506  dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_DEBUG);
1507  return -1;
1508  }
1509  if ($result < -1)
1510  {
1511  $this->error=$prod->error;
1512  $this->db->rollback();
1513  dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_ERR);
1514  return -1;
1515  }
1516  }
1517  else
1518  {
1519  $this->error=$prod->error;
1520  return -1;
1521  }
1522  }
1523  }
1524  else
1525  {
1526  $product_type = $type;
1527  }
1528 
1529  // Calcul du total TTC et de la TVA pour la ligne a partir de
1530  // qty, pu, remise_percent et txtva
1531  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1532  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1533 
1534  $localtaxes_type=getLocalTaxesFromRate($txtva,0,$mysoc,$this->thirdparty);
1535 
1536  // Clean vat code
1537  $vat_src_code='';
1538  if (preg_match('/\((.*)\)/', $txtva, $reg))
1539  {
1540  $vat_src_code = $reg[1];
1541  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
1542  }
1543 
1544  $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);
1545  $total_ht = $tabprice[0];
1546  $total_tva = $tabprice[1];
1547  $total_ttc = $tabprice[2];
1548  $total_localtax1 = $tabprice[9];
1549  $total_localtax2 = $tabprice[10];
1550  $pu_ht = $tabprice[3];
1551 
1552  // MultiCurrency
1553  $multicurrency_total_ht = $tabprice[16];
1554  $multicurrency_total_tva = $tabprice[17];
1555  $multicurrency_total_ttc = $tabprice[18];
1556  $pu_ht_devise = $tabprice[19];
1557 
1558  $localtax1_type=$localtaxes_type[0];
1559  $localtax2_type=$localtaxes_type[2];
1560 
1561  $subprice = price2num($pu,'MU');
1562 
1563  $rangmax = $this->line_max();
1564  $rang = $rangmax + 1;
1565 
1566  // Insert line
1567  $this->line=new CommandeFournisseurLigne($this->db);
1568 
1569  $this->line->context = $this->context;
1570 
1571  $this->line->fk_commande=$this->id;
1572  $this->line->label=$label;
1573  $this->line->ref_fourn = $ref_supplier;
1574  $this->line->ref_supplier = $ref_supplier;
1575  $this->line->desc=$desc;
1576  $this->line->qty=$qty;
1577  $this->line->tva_tx=$txtva;
1578  $this->line->localtax1_tx=($total_localtax1?$localtaxes_type[1]:0);
1579  $this->line->localtax2_tx=($total_localtax2?$localtaxes_type[3]:0);
1580  $this->line->localtax1_type = $localtaxes_type[0];
1581  $this->line->localtax2_type = $localtaxes_type[2];
1582  $this->line->fk_product=$fk_product;
1583  $this->line->product_type=$product_type;
1584  $this->line->remise_percent=$remise_percent;
1585  $this->line->subprice=$pu_ht;
1586  $this->line->rang=$this->rang;
1587  $this->line->info_bits=$info_bits;
1588 
1589  $this->line->vat_src_code=$vat_src_code;
1590  $this->line->total_ht=$total_ht;
1591  $this->line->total_tva=$total_tva;
1592  $this->line->total_localtax1=$total_localtax1;
1593  $this->line->total_localtax2=$total_localtax2;
1594  $this->line->total_ttc=$total_ttc;
1595  $this->line->product_type=$type;
1596  $this->line->special_code=$this->special_code;
1597  $this->line->origin=$origin;
1598  $this->line->origin_id=$origin_id;
1599  $this->line->fk_unit=$fk_unit;
1600 
1601  $this->line->date_start=$date_start;
1602  $this->line->date_end=$date_end;
1603 
1604  // Multicurrency
1605  $this->line->fk_multicurrency = $this->fk_multicurrency;
1606  $this->line->multicurrency_code = $this->multicurrency_code;
1607  $this->line->multicurrency_subprice = $pu_ht_devise;
1608  $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1609  $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1610  $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1611 
1612  $this->line->subprice=$pu_ht;
1613  $this->line->price=$this->line->subprice;
1614 
1615  $this->line->remise_percent=$remise_percent;
1616 
1617  if (is_array($array_options) && count($array_options)>0) {
1618  $this->line->array_options=$array_options;
1619  }
1620 
1621  $result=$this->line->insert($notrigger);
1622  if ($result > 0)
1623  {
1624  // Reorder if child line
1625  if (! empty($fk_parent_line)) $this->line_order(true,'DESC');
1626 
1627  // Mise a jour informations denormalisees au niveau de la commande meme
1628  $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.
1629  if ($result > 0)
1630  {
1631  $this->db->commit();
1632  return $this->line->id;
1633  }
1634  else
1635  {
1636  $this->db->rollback();
1637  return -1;
1638  }
1639  }
1640  else
1641  {
1642  $this->error=$this->line->error;
1643  $this->errors=$this->line->errors;
1644  dol_syslog(get_class($this)."::addline error=".$this->error, LOG_ERR);
1645  $this->db->rollback();
1646  return -1;
1647  }
1648  }
1649  }
1650 
1651 
1668  public function dispatchProduct($user, $product, $qty, $entrepot, $price=0, $comment='', $eatby='', $sellby='', $batch='', $fk_commandefourndet=0, $notrigger=0)
1669  {
1670  global $conf, $langs;
1671 
1672  $error = 0;
1673  require_once DOL_DOCUMENT_ROOT .'/product/stock/class/mouvementstock.class.php';
1674 
1675  // Check parameters (if test are wrong here, there is bug into caller)
1676  if ($entrepot <= 0)
1677  {
1678  $this->error='ErrorBadValueForParameterWarehouse';
1679  return -1;
1680  }
1681  if ($qty <= 0)
1682  {
1683  $this->error='ErrorBadValueForParameterQty';
1684  return -1;
1685  }
1686 
1687  $dispatchstatus = 1;
1688  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
1689 
1690  $now=dol_now();
1691 
1692  if (($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY || $this->statut == self::STATUS_RECEIVED_COMPLETELY))
1693  {
1694  $this->db->begin();
1695 
1696  $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur_dispatch";
1697  $sql.= " (fk_commande, fk_product, qty, fk_entrepot, fk_user, datec, fk_commandefourndet, status, comment, eatby, sellby, batch) VALUES";
1698  $sql.= " ('".$this->id."','".$product."','".$qty."',".($entrepot>0?"'".$entrepot."'":"null").",'".$user->id."','".$this->db->idate($now)."','".$fk_commandefourndet."', ".$dispatchstatus.", '".$this->db->escape($comment)."', ";
1699  $sql.= ($eatby?"'".$this->db->idate($eatby)."'":"null").", ".($sellby?"'".$this->db->idate($sellby)."'":"null").", ".($batch?"'".$batch."'":"null");
1700  $sql.= ")";
1701 
1702  dol_syslog(get_class($this)."::dispatchProduct", LOG_DEBUG);
1703  $resql = $this->db->query($sql);
1704  if ($resql)
1705  {
1706  if (! $notrigger)
1707  {
1708  global $conf, $langs, $user;
1709  // Call trigger
1710  $result=$this->call_trigger('LINEORDER_SUPPLIER_DISPATCH',$user);
1711  if ($result < 0)
1712  {
1713  $error++;
1714  return -1;
1715  }
1716  // End call triggers
1717  }
1718  }
1719  else
1720  {
1721  $this->error=$this->db->lasterror();
1722  $error++;
1723  }
1724 
1725  // Si module stock gere et que incrementation faite depuis un dispatching en stock
1726  if (! $error && $entrepot > 0 && ! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER))
1727  {
1728 
1729  $mouv = new MouvementStock($this->db);
1730  if ($product > 0)
1731  {
1732  // $price should take into account discount (except if option STOCK_EXCLUDE_DISCOUNT_FOR_PMP is on)
1733  $mouv->origin = &$this;
1734  $result=$mouv->reception($user, $product, $entrepot, $qty, $price, $comment, $eatby, $sellby, $batch);
1735  if ($result < 0)
1736  {
1737  $this->error=$mouv->error;
1738  $this->errors=$mouv->errors;
1739  dol_syslog(get_class($this)."::dispatchProduct ".$this->error." ".join(',',$this->errors), LOG_ERR);
1740  $error++;
1741  }
1742  }
1743  }
1744 
1745  if ($error == 0)
1746  {
1747  $this->db->commit();
1748  return 1;
1749  }
1750  else
1751  {
1752  $this->db->rollback();
1753  return -1;
1754  }
1755  }
1756  else
1757  {
1758  $this->error='BadStatusForObject';
1759  return -2;
1760  }
1761  }
1762 
1770  public function deleteline($idline, $notrigger=0)
1771  {
1772  if ($this->statut == 0)
1773  {
1774  $line = new CommandeFournisseurLigne($this->db);
1775 
1776  if ($line->fetch($idline) <= 0)
1777  {
1778  return 0;
1779  }
1780 
1781  if ($line->delete($notrigger) > 0)
1782  {
1783  $this->update_price();
1784  return 1;
1785  }
1786  else
1787  {
1788  $this->error = $line->error;
1789  $this->errors = $line->errors;
1790  return -1;
1791  }
1792  }
1793  else
1794  {
1795  return -2;
1796  }
1797  }
1798 
1806  public function delete(User $user, $notrigger=0)
1807  {
1808  global $langs,$conf;
1809  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1810 
1811  $error = 0;
1812 
1813  $this->db->begin();
1814 
1815  if (empty($notrigger))
1816  {
1817  // Call trigger
1818  $result=$this->call_trigger('ORDER_SUPPLIER_DELETE',$user);
1819  if ($result < 0)
1820  {
1821  $this->errors[]='ErrorWhenRunningTrigger';
1822  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
1823  return -1;
1824  }
1825  // End call triggers
1826  }
1827 
1828  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet WHERE fk_commande =". $this->id ;
1829  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1830  if (! $this->db->query($sql) )
1831  {
1832  $this->error=$this->db->lasterror();
1833  $this->errors[]=$this->db->lasterror();
1834  $error++;
1835  }
1836 
1837  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur WHERE rowid =".$this->id;
1838  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1839  if ($resql = $this->db->query($sql) )
1840  {
1841  if ($this->db->affected_rows($resql) < 1)
1842  {
1843  $this->error=$this->db->lasterror();
1844  $this->errors[]=$this->db->lasterror();
1845  $error++;
1846  }
1847  }
1848  else
1849  {
1850  $this->error=$this->db->lasterror();
1851  $this->errors[]=$this->db->lasterror();
1852  $error++;
1853  }
1854 
1855  // Remove extrafields
1856  if ((! $error) && (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used
1857  {
1858  $result=$this->deleteExtraFields();
1859  if ($result < 0)
1860  {
1861  $this->error='FailToDeleteExtraFields';
1862  $this->errors[]='FailToDeleteExtraFields';
1863  $error++;
1864  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1865  }
1866  }
1867 
1868  // Delete linked object
1869  $res = $this->deleteObjectLinked();
1870  if ($res < 0) {
1871  $this->error='FailToDeleteObjectLinked';
1872  $this->errors[]='FailToDeleteObjectLinked';
1873  $error++;
1874  }
1875 
1876  if (! $error)
1877  {
1878  // We remove directory
1879  $ref = dol_sanitizeFileName($this->ref);
1880  if ($conf->fournisseur->commande->dir_output)
1881  {
1882  $dir = $conf->fournisseur->commande->dir_output . "/" . $ref ;
1883  $file = $dir . "/" . $ref . ".pdf";
1884  if (file_exists($file))
1885  {
1886  if (! dol_delete_file($file,0,0,0,$this)) // For triggers
1887  {
1888  $this->error='ErrorFailToDeleteFile';
1889  $this->errors[]='ErrorFailToDeleteFile';
1890  $error++;
1891  }
1892  }
1893  if (file_exists($dir))
1894  {
1895  $res=@dol_delete_dir_recursive($dir);
1896  if (! $res)
1897  {
1898  $this->error='ErrorFailToDeleteDir';
1899  $this->errors[]='ErrorFailToDeleteDir';
1900  $error++;
1901  }
1902  }
1903  }
1904  }
1905 
1906  if (! $error)
1907  {
1908  dol_syslog(get_class($this)."::delete $this->id by $user->id", LOG_DEBUG);
1909  $this->db->commit();
1910  return 1;
1911  }
1912  else
1913  {
1914  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
1915  $this->db->rollback();
1916  return -$error;
1917  }
1918  }
1919 
1926  {
1927  $sql = "SELECT rowid, libelle";
1928  $sql.= " FROM ".MAIN_DB_PREFIX."c_input_method";
1929  $sql.= " WHERE active = 1";
1930 
1931  $resql=$this->db->query($sql);
1932  if ($resql)
1933  {
1934  $i = 0;
1935  $num = $this->db->num_rows($resql);
1936  $this->methodes_commande = array();
1937  while ($i < $num)
1938  {
1939  $row = $this->db->fetch_row($resql);
1940 
1941  $this->methodes_commande[$row[0]] = $row[1];
1942 
1943  $i++;
1944  }
1945  return 0;
1946  }
1947  else
1948  {
1949  return -1;
1950  }
1951  }
1952 
1959  public function getDispachedLines($status=-1)
1960  {
1961  $ret = array();
1962 
1963  // List of already dispatched lines
1964  $sql = "SELECT p.ref, p.label,";
1965  $sql.= " e.rowid as warehouse_id, e.ref as entrepot,";
1966  $sql.= " cfd.rowid as dispatchlineid, cfd.fk_product, cfd.qty, cfd.eatby, cfd.sellby, cfd.batch, cfd.comment, cfd.status";
1967  $sql.= " FROM ".MAIN_DB_PREFIX."product as p,";
1968  $sql.= " ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as cfd";
1969  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."entrepot as e ON cfd.fk_entrepot = e.rowid";
1970  $sql.= " WHERE cfd.fk_commande = ".$this->id;
1971  $sql.= " AND cfd.fk_product = p.rowid";
1972  if ($status >= 0) $sql.=" AND cfd.status = ".$status;
1973  $sql.= " ORDER BY cfd.rowid ASC";
1974 
1975  $resql = $this->db->query($sql);
1976  if ($resql)
1977  {
1978  $num = $this->db->num_rows($resql);
1979  $i = 0;
1980 
1981  while ($i < $num)
1982  {
1983  $objp = $this->db->fetch_object($resql);
1984  if ($objp) $ret[]=array('id'=>$objp->dispatchedlineid, 'productid'=>$objp->fk_product, 'warehouseid'=>$objp->warehouse_id);
1985 
1986  $i++;
1987  }
1988  }
1989  else dol_print_error($this->db, 'Failed to execute request to get dispatched lines');
1990 
1991  return $ret;
1992  }
1993 
1994 
2004  function Livraison($user, $date, $type, $comment)
2005  {
2006  global $conf, $langs;
2007 
2008  $result = 0;
2009  $error = 0;
2010 
2011  dol_syslog(get_class($this)."::Livraison");
2012 
2013  if ($user->rights->fournisseur->commande->receptionner)
2014  {
2015  // Define the new status
2016  if ($type == 'par') $statut = self::STATUS_RECEIVED_PARTIALLY;
2017  elseif ($type == 'tot') $statut = self::STATUS_RECEIVED_COMPLETELY;
2018  elseif ($type == 'nev') $statut = self::STATUS_CANCELED_AFTER_ORDER;
2019  elseif ($type == 'can') $statut = self::STATUS_CANCELED_AFTER_ORDER;
2020  else {
2021  $error++;
2022  dol_syslog(get_class($this)."::Livraison Error -2", LOG_ERR);
2023  return -2;
2024  }
2025 
2026  // Some checks to accept the record
2027  if (! empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS))
2028  {
2029  // If option SUPPLIER_ORDER_USE_DISPATCH_STATUS is on, we check all reception are approved to allow status "total/done"
2030  if (! $error && ($type == 'tot'))
2031  {
2032  $dispatchedlinearray=$this->getDispachedLines(0);
2033  if (count($dispatchedlinearray) > 0)
2034  {
2035  $result=-1;
2036  $error++;
2037  $this->errors[]='ErrorCantSetReceptionToTotalDoneWithReceptionToApprove';
2038  dol_syslog('ErrorCantSetReceptionToTotalDoneWithReceptionToApprove', LOG_DEBUG);
2039  }
2040 
2041  }
2042  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)
2043  {
2044  $dispatcheddenied=$this->getDispachedLines(2);
2045  if (count($dispatchedlinearray) > 0)
2046  {
2047  $result=-1;
2048  $error++;
2049  $this->errors[]='ErrorCantSetReceptionToTotalDoneWithReceptionDenied';
2050  dol_syslog('ErrorCantSetReceptionToTotalDoneWithReceptionDenied', LOG_DEBUG);
2051  }
2052  }
2053  }
2054 
2055  // TODO LDR01 Add a control test to accept only if ALL predefined products are received (same qty).
2056 
2057 
2058  if (! $error)
2059  {
2060  $this->db->begin();
2061 
2062  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
2063  $sql.= " SET fk_statut = ".$statut;
2064  $sql.= " WHERE rowid = ".$this->id;
2065  $sql.= " AND fk_statut IN (".self::STATUS_ORDERSENT.",".self::STATUS_RECEIVED_PARTIALLY.")"; // Process running or Partially received
2066 
2067  dol_syslog(get_class($this)."::Livraison", LOG_DEBUG);
2068  $resql=$this->db->query($sql);
2069  if ($resql)
2070  {
2071  $result = 0;
2072  $old_statut = $this->statut;
2073  $this->statut = $statut;
2074  $this->actionmsg2 = $comment;
2075 
2076  // Call trigger
2077  $result=$this->call_trigger('ORDER_SUPPLIER_RECEIVE',$user);
2078  if ($result < 0) $error++;
2079  // End call triggers
2080 
2081  if (! $error)
2082  {
2083  $this->db->commit();
2084  }
2085  else
2086  {
2087  $this->statut = $old_statut;
2088  $this->db->rollback();
2089  $this->error=$this->db->lasterror();
2090  $result = -1;
2091  }
2092  }
2093  else
2094  {
2095  $this->db->rollback();
2096  $this->error=$this->db->lasterror();
2097  $result = -1;
2098  }
2099  }
2100  }
2101  else
2102  {
2103  $this->error = $langs->trans('NotAuthorized');
2104  $this->errors[] = $langs->trans('NotAuthorized');
2105  dol_syslog(get_class($this)."::Livraison Not Authorized");
2106  $result = -3;
2107  }
2108  return $result ;
2109  }
2110 
2119  function set_date_livraison($user, $date_livraison, $notrigger=0)
2120  {
2121  if ($user->rights->fournisseur->commande->creer)
2122  {
2123  $error=0;
2124 
2125  $this->db->begin();
2126 
2127  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
2128  $sql.= " SET date_livraison = ".($date_livraison ? "'".$this->db->idate($date_livraison)."'" : 'null');
2129  $sql.= " WHERE rowid = ".$this->id;
2130 
2131  dol_syslog(__METHOD__, LOG_DEBUG);
2132  $resql=$this->db->query($sql);
2133  if (!$resql)
2134  {
2135  $this->errors[]=$this->db->error();
2136  $error++;
2137  }
2138 
2139  if (! $error)
2140  {
2141  $this->oldcopy= clone $this;
2142  $this->date_livraison = $date_livraison;
2143  }
2144 
2145  if (! $notrigger && empty($error))
2146  {
2147  // Call trigger
2148  $result=$this->call_trigger('ORDER_SUPPLIER_MODIFY',$user);
2149  if ($result < 0) $error++;
2150  // End call triggers
2151  }
2152 
2153  if (! $error)
2154  {
2155  $this->db->commit();
2156  return 1;
2157  }
2158  else
2159  {
2160  foreach($this->errors as $errmsg)
2161  {
2162  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2163  $this->error.=($this->error?', '.$errmsg:$errmsg);
2164  }
2165  $this->db->rollback();
2166  return -1*$error;
2167  }
2168  }
2169  else
2170  {
2171  return -2;
2172  }
2173  }
2174 
2183  function set_id_projet($user, $id_projet, $notrigger=0)
2184  {
2185  if ($user->rights->fournisseur->commande->creer)
2186  {
2187  $error=0;
2188 
2189  $this->db->begin();
2190 
2191  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
2192  $sql.= " SET fk_projet = ".($id_projet > 0 ? (int) $id_projet : 'null');
2193  $sql.= " WHERE rowid = ".$this->id;
2194 
2195  dol_syslog(__METHOD__, LOG_DEBUG);
2196  $resql=$this->db->query($sql);
2197  if (!$resql)
2198  {
2199  $this->errors[]=$this->db->error();
2200  $error++;
2201  }
2202 
2203  if (! $error)
2204  {
2205  $this->oldcopy= clone $this;
2206  $this->fk_projet = $id_projet;
2207  }
2208 
2209  if (! $notrigger && empty($error))
2210  {
2211  // Call trigger
2212  $result=$this->call_trigger('ORDER_SUPPLIER_MODIFY',$user);
2213  if ($result < 0) $error++;
2214  // End call triggers
2215  }
2216 
2217  if (! $error)
2218  {
2219  $this->db->commit();
2220  return 1;
2221  }
2222  else
2223  {
2224  foreach($this->errors as $errmsg)
2225  {
2226  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2227  $this->error.=($this->error?', '.$errmsg:$errmsg);
2228  }
2229  $this->db->rollback();
2230  return -1*$error;
2231  }
2232  }
2233  else
2234  {
2235  return -2;
2236  }
2237  }
2238 
2247  public function updateFromCommandeClient($user, $idc, $comclientid)
2248  {
2249  $comclient = new Commande($this->db);
2250  $comclient->fetch($comclientid);
2251 
2252  $this->id = $idc;
2253 
2254  $this->lines = array();
2255 
2256  $num=count($comclient->lines);
2257  for ($i = 0; $i < $num; $i++)
2258  {
2259  $prod = new Product($this->db);
2260  if ($prod->fetch($comclient->lines[$i]->fk_product) > 0)
2261  {
2262  $libelle = $prod->libelle;
2263  $ref = $prod->ref;
2264  }
2265 
2266  $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet";
2267  $sql .= " (fk_commande,label,description,fk_product, price, qty, tva_tx, localtax1_tx, localtax2_tx, remise_percent, subprice, remise, ref)";
2268  $sql .= " VALUES (".$idc.", '" . $this->db->escape($libelle) . "','" . $this->db->escape($comclient->lines[$i]->desc) . "'";
2269  $sql .= ",".$comclient->lines[$i]->fk_product.",'".price2num($comclient->lines[$i]->price)."'";
2270  $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;
2271  $sql .= ", '".price2num($comclient->lines[$i]->subprice)."','0','".$ref."');";
2272  if ($this->db->query($sql))
2273  {
2274  $this->update_price();
2275  }
2276  }
2277 
2278  return 1;
2279  }
2280 
2288  public function setStatus($user, $status)
2289  {
2290  global $conf,$langs;
2291  $error=0;
2292 
2293  $this->db->begin();
2294 
2295  $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande_fournisseur';
2296  $sql.= ' SET fk_statut='.$status;
2297  $sql.= ' WHERE rowid = '.$this->id;
2298 
2299  dol_syslog(get_class($this)."::setStatus", LOG_DEBUG);
2300  $resql = $this->db->query($sql);
2301  if ($resql)
2302  {
2303  // Trigger names for each status
2304  $trigger_name[0] = 'DRAFT';
2305  $trigger_name[1] = 'VALIDATED';
2306  $trigger_name[2] = 'APPROVED';
2307  $trigger_name[3] = 'ORDERED'; // Ordered
2308  $trigger_name[4] = 'RECEIVED_PARTIALLY';
2309  $trigger_name[5] = 'RECEIVED_COMPLETELY';
2310  $trigger_name[6] = 'CANCELED';
2311  $trigger_name[7] = 'CANCELED';
2312  $trigger_name[9] = 'REFUSED';
2313 
2314  // Call trigger
2315  $result=$this->call_trigger("ORDER_SUPPLIER_STATUS_".$trigger_name[$status],$user);
2316  if ($result < 0) { $error++; }
2317  // End call triggers
2318  }
2319  else
2320  {
2321  $error++;
2322  $this->error=$this->db->lasterror();
2323  dol_syslog(get_class($this)."::setStatus ".$this->error);
2324  }
2325 
2326  if (! $error)
2327  {
2328  $this->statut = $status;
2329  $this->db->commit();
2330  return 1;
2331  }
2332  else
2333  {
2334  $this->db->rollback();
2335  return -1;
2336  }
2337  }
2338 
2362  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='')
2363  {
2364  global $mysoc, $conf;
2365  dol_syslog(get_class($this)."::updateline $rowid, $desc, $pu, $qty, $remise_percent, $txtva, $price_base_type, $info_bits, $type, $fk_unit");
2366  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2367 
2368  $error = 0;
2369 
2370  if ($this->brouillon)
2371  {
2372  $this->db->begin();
2373 
2374  // Clean parameters
2375  if (empty($qty)) $qty=0;
2376  if (empty($info_bits)) $info_bits=0;
2377  if (empty($txtva)) $txtva=0;
2378  if (empty($txlocaltax1)) $txlocaltax1=0;
2379  if (empty($txlocaltax2)) $txlocaltax2=0;
2380  if (empty($remise)) $remise=0;
2381  if (empty($remise_percent)) $remise_percent=0;
2382 
2383  $remise_percent=price2num($remise_percent);
2384  $qty=price2num($qty);
2385  if (! $qty) $qty=1;
2386  $pu = price2num($pu);
2387  $txtva=price2num($txtva);
2388  $txlocaltax1=price2num($txlocaltax1);
2389  $txlocaltax2=price2num($txlocaltax2);
2390 
2391  // Check parameters
2392  if ($type < 0) return -1;
2393 
2394  // Calcul du total TTC et de la TVA pour la ligne a partir de
2395  // qty, pu, remise_percent et txtva
2396  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
2397  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
2398 
2399  $localtaxes_type=getLocalTaxesFromRate($txtva,0,$mysoc, $this->thirdparty);
2400 
2401  // Clean vat code
2402  $vat_src_code='';
2403  if (preg_match('/\((.*)\)/', $txtva, $reg))
2404  {
2405  $vat_src_code = $reg[1];
2406  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
2407  }
2408 
2409  $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);
2410  $total_ht = $tabprice[0];
2411  $total_tva = $tabprice[1];
2412  $total_ttc = $tabprice[2];
2413  $total_localtax1 = $tabprice[9];
2414  $total_localtax2 = $tabprice[10];
2415  $pu_ht = $tabprice[3];
2416  $pu_tva = $tabprice[4];
2417  $pu_ttc = $tabprice[5];
2418 
2419  // MultiCurrency
2420  $multicurrency_total_ht = $tabprice[16];
2421  $multicurrency_total_tva = $tabprice[17];
2422  $multicurrency_total_ttc = $tabprice[18];
2423  $pu_ht_devise = $tabprice[19];
2424 
2425  $localtax1_type=$localtaxes_type[0];
2426  $localtax2_type=$localtaxes_type[2];
2427 
2428  $subprice = price2num($pu_ht,'MU');
2429 
2430  //Fetch current line from the database and then clone the object and set it in $oldline property
2431  $this->line=new CommandeFournisseurLigne($this->db);
2432  $this->line->fetch($rowid);
2433  $oldline = clone $this->line;
2434  $this->line->oldline = $oldline;
2435 
2436  $this->line->context = $this->context;
2437 
2438  $this->line->fk_commande=$this->id;
2439  //$this->line->label=$label;
2440  $this->line->desc=$desc;
2441  $this->line->qty=$qty;
2442  $this->line->ref_supplier=$ref_supplier;
2443 
2444  $this->line->vat_src_code = $vat_src_code;
2445  $this->line->tva_tx = $txtva;
2446  $this->line->localtax1_tx = $txlocaltax1;
2447  $this->line->localtax2_tx = $txlocaltax2;
2448  $this->line->localtax1_type = $localtaxes_type[0];
2449  $this->line->localtax2_type = $localtaxes_type[2];
2450  $this->line->remise_percent = $remise_percent;
2451  $this->line->subprice = $pu_ht;
2452  $this->line->rang = $this->rang;
2453  $this->line->info_bits = $info_bits;
2454  $this->line->total_ht = $total_ht;
2455  $this->line->total_tva = $total_tva;
2456  $this->line->total_localtax1= $total_localtax1;
2457  $this->line->total_localtax2= $total_localtax2;
2458  $this->line->total_ttc = $total_ttc;
2459  $this->line->product_type = $type;
2460  $this->line->special_code = $this->special_code;
2461  $this->line->origin = $this->origin;
2462  $this->line->fk_unit = $fk_unit;
2463 
2464  $this->line->date_start = $date_start;
2465  $this->line->date_end = $date_end;
2466 
2467  // Multicurrency
2468  $this->line->fk_multicurrency = $this->fk_multicurrency;
2469  $this->line->multicurrency_code = $this->multicurrency_code;
2470  $this->line->multicurrency_subprice = $pu_ht_devise;
2471  $this->line->multicurrency_total_ht = $multicurrency_total_ht;
2472  $this->line->multicurrency_total_tva = $multicurrency_total_tva;
2473  $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
2474 
2475  $this->line->subprice=$pu_ht;
2476  $this->line->price=$this->line->subprice;
2477 
2478  $this->line->remise_percent=$remise_percent;
2479 
2480  if (is_array($array_options) && count($array_options)>0) {
2481  $this->line->array_options=$array_options;
2482  }
2483 
2484  $result=$this->line->update($notrigger);
2485 
2486 
2487  // Mise a jour info denormalisees au niveau facture
2488  if ($result >= 0)
2489  {
2490  $this->update_price('','auto');
2491  $this->db->commit();
2492  return $result;
2493  }
2494  else
2495  {
2496  $this->error=$this->db->lasterror();
2497  $this->db->rollback();
2498  return -1;
2499  }
2500  }
2501  else
2502  {
2503  $this->error="Order status makes operation forbidden";
2504  dol_syslog(get_class($this)."::updateline ".$this->error, LOG_ERR);
2505  return -2;
2506  }
2507  }
2508 
2509 
2517  public function initAsSpecimen()
2518  {
2519  global $user,$langs,$conf;
2520 
2521  dol_syslog(get_class($this)."::initAsSpecimen");
2522 
2523  $now=dol_now();
2524 
2525  // Find first product
2526  $prodid=0;
2527  $product=new ProductFournisseur($this->db);
2528  $sql = "SELECT rowid";
2529  $sql.= " FROM ".MAIN_DB_PREFIX."product";
2530  $sql.= " WHERE entity IN (".getEntity('product').")";
2531  $sql.=$this->db->order("rowid","ASC");
2532  $sql.=$this->db->plimit(1);
2533  $resql = $this->db->query($sql);
2534  if ($resql)
2535  {
2536  $obj = $this->db->fetch_object($resql);
2537  $prodid = $obj->rowid;
2538  }
2539 
2540  // Initialise parametres
2541  $this->id=0;
2542  $this->ref = 'SPECIMEN';
2543  $this->specimen=1;
2544  $this->socid = 1;
2545  $this->date = $now;
2546  $this->date_commande = $now;
2547  $this->date_lim_reglement=$this->date+3600*24*30;
2548  $this->cond_reglement_code = 'RECEP';
2549  $this->mode_reglement_code = 'CHQ';
2550  $this->note_public='This is a comment (public)';
2551  $this->note_private='This is a comment (private)';
2552  $this->statut=0;
2553 
2554  // Lines
2555  $nbp = 5;
2556  $xnbp = 0;
2557  while ($xnbp < $nbp)
2558  {
2559  $line=new CommandeFournisseurLigne($this->db);
2560  $line->desc=$langs->trans("Description")." ".$xnbp;
2561  $line->qty=1;
2562  $line->subprice=100;
2563  $line->price=100;
2564  $line->tva_tx=19.6;
2565  $line->localtax1_tx=0;
2566  $line->localtax2_tx=0;
2567  if ($xnbp == 2)
2568  {
2569  $line->total_ht=50;
2570  $line->total_ttc=59.8;
2571  $line->total_tva=9.8;
2572  $line->remise_percent=50;
2573  }
2574  else
2575  {
2576  $line->total_ht=100;
2577  $line->total_ttc=119.6;
2578  $line->total_tva=19.6;
2579  $line->remise_percent=00;
2580  }
2581  $line->fk_product=$prodid;
2582 
2583  $this->lines[$xnbp]=$line;
2584 
2585  $this->total_ht += $line->total_ht;
2586  $this->total_tva += $line->total_tva;
2587  $this->total_ttc += $line->total_ttc;
2588 
2589  $xnbp++;
2590  }
2591  }
2592 
2599  public function info($id)
2600  {
2601  $sql = 'SELECT c.rowid, date_creation as datec, tms as datem, date_valid as date_validation, date_approve as datea, date_approve2 as datea2,';
2602  $sql.= ' fk_user_author, fk_user_modif, fk_user_valid, fk_user_approve, fk_user_approve2';
2603  $sql.= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseur as c';
2604  $sql.= ' WHERE c.rowid = '.$id;
2605 
2606  $result=$this->db->query($sql);
2607  if ($result)
2608  {
2609  if ($this->db->num_rows($result))
2610  {
2611  $obj = $this->db->fetch_object($result);
2612  $this->id = $obj->rowid;
2613  if ($obj->fk_user_author) $this->user_creation_id = $obj->fk_user_author;
2614  if ($obj->fk_user_valid) $this->user_validation_id = $obj->fk_user_valid;
2615  if ($obj->fk_user_modif) $this->user_modification_id =$obj->fk_user_modif;
2616  if ($obj->fk_user_approve) $this->user_approve_id = $obj->fk_user_approve;
2617  if ($obj->fk_user_approve2) $this->user_approve_id2 = $obj->fk_user_approve2;
2618 
2619  $this->date_creation = $this->db->idate($obj->datec);
2620  $this->date_modification = $this->db->idate($obj->datem);
2621  $this->date_approve = $this->db->idate($obj->datea);
2622  $this->date_approve2 = $this->db->idate($obj->datea2);
2623  $this->date_validation = $this->db->idate($obj->date_validation);
2624  }
2625  $this->db->free($result);
2626  }
2627  else
2628  {
2629  dol_print_error($this->db);
2630  }
2631  }
2632 
2638  function load_state_board()
2639  {
2640  global $conf, $user;
2641 
2642  $this->nb=array();
2643  $clause = "WHERE";
2644 
2645  $sql = "SELECT count(co.rowid) as nb";
2646  $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as co";
2647  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON co.fk_soc = s.rowid";
2648  if (!$user->rights->societe->client->voir && !$user->societe_id)
2649  {
2650  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2651  $sql.= " WHERE sc.fk_user = " .$user->id;
2652  $clause = "AND";
2653  }
2654  $sql.= " ".$clause." co.entity = ".$conf->entity;
2655 
2656  $resql=$this->db->query($sql);
2657  if ($resql)
2658  {
2659  while ($obj=$this->db->fetch_object($resql))
2660  {
2661  $this->nb["supplier_orders"]=$obj->nb;
2662  }
2663  $this->db->free($resql);
2664  return 1;
2665  }
2666  else
2667  {
2668  dol_print_error($this->db);
2669  $this->error=$this->db->error();
2670  return -1;
2671  }
2672  }
2673 
2680  function load_board($user)
2681  {
2682  global $conf, $langs;
2683 
2684  $clause = " WHERE";
2685 
2686  $sql = "SELECT c.rowid, c.date_creation as datec, c.date_commande, c.fk_statut, c.date_livraison as delivery_date";
2687  $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c";
2688  if (!$user->rights->societe->client->voir && !$user->societe_id)
2689  {
2690  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc";
2691  $sql.= " WHERE sc.fk_user = " .$user->id;
2692  $clause = " AND";
2693  }
2694  $sql.= $clause." c.entity = ".$conf->entity;
2695  $sql.= " AND c.fk_statut IN (".self::STATUS_VALIDATED.", ".self::STATUS_ACCEPTED.")";
2696  if ($user->societe_id) $sql.=" AND c.fk_soc = ".$user->societe_id;
2697 
2698  $resql=$this->db->query($sql);
2699  if ($resql)
2700  {
2701  $commandestatic = new CommandeFournisseur($this->db);
2702 
2703  $response = new WorkboardResponse();
2704  $response->warning_delay=$conf->commande->fournisseur->warning_delay/60/60/24;
2705  $response->label=$langs->trans("SuppliersOrdersToProcess");
2706  $response->url=DOL_URL_ROOT.'/fourn/commande/list.php?statut=1,2,3&mainmenu=commercial&leftmenu=orders_suppliers';
2707  $response->img=img_object('',"order");
2708 
2709  while ($obj=$this->db->fetch_object($resql))
2710  {
2711  $response->nbtodo++;
2712 
2713  $commandestatic->date_livraison = $this->db->jdate($obj->delivery_date);
2714  $commandestatic->date_commande = $this->db->jdate($obj->date_commande);
2715  $commandestatic->statut = $obj->fk_statut;
2716 
2717  if ($commandestatic->hasDelay()) {
2718  $response->nbtodolate++;
2719  }
2720  }
2721 
2722  return $response;
2723  }
2724  else
2725  {
2726  $this->error=$this->db->error();
2727  return -1;
2728  }
2729  }
2730 
2737  public function getInputMethod()
2738  {
2739  global $db, $langs;
2740 
2741  if ($this->methode_commande_id > 0)
2742  {
2743  $sql = "SELECT rowid, code, libelle as label";
2744  $sql.= " FROM ".MAIN_DB_PREFIX.'c_input_method';
2745  $sql.= " WHERE active=1 AND rowid = ".$db->escape($this->methode_commande_id);
2746 
2747  $resql = $db->query($sql);
2748  if ($resql)
2749  {
2750  if ($db->num_rows($query))
2751  {
2752  $obj = $db->fetch_object($query);
2753 
2754  $string = $langs->trans($obj->code);
2755  if ($string == $obj->code)
2756  {
2757  $string = $obj->label != '-' ? $obj->label : '';
2758  }
2759  return $string;
2760  }
2761  }
2762  else dol_print_error($db);
2763  }
2764 
2765  return '';
2766  }
2767 
2778  public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
2779  {
2780  global $conf, $langs;
2781 
2782  $langs->load("suppliers");
2783 
2784  if (! dol_strlen($modele)) {
2785 
2786  $modele = 'muscadet';
2787 
2788  if ($this->modelpdf) {
2789  $modele = $this->modelpdf;
2790  } elseif (! empty($conf->global->COMMANDE_SUPPLIER_ADDON_PDF)) {
2791  $modele = $conf->global->COMMANDE_SUPPLIER_ADDON_PDF;
2792  }
2793  }
2794 
2795  $modelpath = "core/modules/supplier_order/pdf/";
2796 
2797  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
2798  }
2799 
2806  public function getMaxDeliveryTimeDay($langs)
2807  {
2808  if (empty($this->lines)) return '';
2809 
2810  $obj = new ProductFournisseur($this->db);
2811 
2812  $nb = 0;
2813  foreach ($this->lines as $line)
2814  {
2815  if ($line->fk_product > 0)
2816  {
2817  $idp = $obj->find_min_price_product_fournisseur($line->fk_product, $line->qty);
2818  if ($idp)
2819  {
2820  $obj->fetch($idp);
2821  if ($obj->delivery_time_days > $nb) $nb = $obj->delivery_time_days;
2822  }
2823  }
2824  }
2825 
2826  if ($nb === 0) return '';
2827  else return $nb.' '.$langs->trans('Days');
2828  }
2829 
2834  public function getRights()
2835  {
2836  global $user;
2837 
2838  return $user->rights->fournisseur->commande;
2839  }
2840 
2841 
2850  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
2851  {
2852  $tables = array(
2853  'commande_fournisseur'
2854  );
2855 
2856  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
2857  }
2858 
2864  public function hasDelay()
2865  {
2866  global $conf;
2867 
2868  if (empty($this->date_delivery) && ! empty($this->date_livraison)) $this->date_delivery = $this->date_livraison; // For backward compatibility
2869 
2870  $now = dol_now();
2871  $date_to_test = empty($this->date_delivery) ? $this->date_commande : $this->date_delivery;
2872 
2873  return ($this->statut > 0 && $this->statut < 4) && $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
2874  }
2875 
2881  public function showDelay()
2882  {
2883  global $conf, $langs;
2884 
2885  if (empty($this->date_delivery) && ! empty($this->date_livraison)) $this->date_delivery = $this->date_livraison; // For backward compatibility
2886 
2887  if (empty($this->date_delivery)) $text=$langs->trans("OrderDate").' '.dol_print_date($this->date_commande, 'day');
2888  else $text=$text=$langs->trans("DeliveryDate").' '.dol_print_date($this->date_delivery, 'day');
2889  $text.=' '.($conf->commande->fournisseur->warning_delay>0?'+':'-').' '.round(abs($conf->commande->fournisseur->warning_delay)/3600/24,1).' '.$langs->trans("days").' < '.$langs->trans("Today");
2890 
2891  return $text;
2892  }
2893 
2894 
2903  public function calcAndSetStatusDispatch(User $user, $closeopenorder=1, $comment='')
2904  {
2905  global $conf, $langs;
2906 
2907  if (! empty($conf->fournisseur->enabled))
2908  {
2909  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.dispatch.class.php';
2910 
2911  $qtydelivered=array();
2912  $qtywished=array();
2913 
2914  $supplierorderdispatch = new CommandeFournisseurDispatch($this->db);
2915  $filter=array('t.fk_commande'=>$this->id);
2916  if (! empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
2917  $filter['t.status']=1; // Restrict to lines with status validated
2918  }
2919 
2920  $ret=$supplierorderdispatch->fetchAll('','',0,0,$filter);
2921  if ($ret<0)
2922  {
2923  $this->error=$supplierorderdispatch->error; $this->errors=$supplierorderdispatch->errors;
2924  return $ret;
2925  }
2926  else
2927  {
2928  if (is_array($supplierorderdispatch->lines) && count($supplierorderdispatch->lines)>0)
2929  {
2930  $date_liv = dol_now();
2931 
2932  // Build array with quantity deliverd by product
2933  foreach($supplierorderdispatch->lines as $line) {
2934  $qtydelivered[$line->fk_product]+=$line->qty;
2935  }
2936  foreach($this->lines as $line) {
2937  $qtywished[$line->fk_product]+=$line->qty;
2938  }
2939  //Compare array
2940  $diff_array=array_diff_assoc($qtydelivered,$qtywished); // Warning: $diff_array is done only on common keys.
2941  $keysinwishednotindelivered=array_diff(array_keys($qtywished),array_keys($qtydelivered)); // To check we also have same number of keys
2942  $keysindeliverednotinwished=array_diff(array_keys($qtydelivered),array_keys($qtywished)); // To check we also have same number of keys
2943  /*var_dump(array_keys($qtydelivered));
2944  var_dump(array_keys($qtywished));
2945  var_dump($diff_array);
2946  var_dump($keysinwishednotindelivered);
2947  var_dump($keysindeliverednotinwished);
2948  exit;*/
2949 
2950  if (count($diff_array)==0 && count($keysinwishednotindelivered)==0 && count($keysindeliverednotinwished)==0) //No diff => mean everythings is received
2951  {
2952  if ($closeopenorder)
2953  {
2954  //$ret=$this->setStatus($user,5);
2955  $ret = $this->Livraison($user, $date_liv, 'tot', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
2956  if ($ret<0) {
2957  return -1;
2958  }
2959  return 5;
2960  }
2961  else
2962  {
2963  //Diff => received partially
2964  //$ret=$this->setStatus($user,4);
2965  $ret = $this->Livraison($user, $date_liv, 'par', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
2966  if ($ret<0) {
2967  return -1;
2968  }
2969  return 4;
2970  }
2971  }
2972  else
2973  {
2974  //Diff => received partially
2975  $ret = $this->Livraison($user, $date_liv, 'par', $comment); // GETPOST("type") is 'tot', 'par', 'nev', 'can'
2976  if ($ret<0) {
2977  return -1;
2978  }
2979  return 4;
2980  }
2981  }
2982  return 1;
2983  }
2984  }
2985  return 0;
2986  }
2987 }
2988 
2989 
2990 
2995 {
2996  public $element='commande_fournisseurdet';
2997  public $table_element='commande_fournisseurdet';
2998 
2999  public $oldline;
3000 
3005  public $fk_commande;
3006 
3007  // From llx_commande_fournisseurdet
3008  public $fk_parent_line;
3009  public $fk_facture;
3010  public $label;
3011  public $rang = 0;
3012 
3017  public $pu_ht;
3018 
3019  public $date_start;
3020  public $date_end;
3021 
3022  // From llx_product_fournisseur_price
3023 
3028  public $ref_supplier;
3029  public $remise;
3030  public $product_libelle;
3031 
3032 
3038  public function __construct($db)
3039  {
3040  $this->db= $db;
3041  }
3042 
3049  public function fetch($rowid)
3050  {
3051  $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx,';
3052  $sql.= ' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref,';
3053  $sql.= ' cd.remise, cd.remise_percent, cd.subprice,';
3054  $sql.= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
3055  $sql.= ' cd.total_localtax1, cd.total_localtax2,';
3056  $sql.= ' p.ref as product_ref, p.label as product_libelle, p.description as product_desc,';
3057  $sql.= ' cd.date_start, cd.date_end, cd.fk_unit,';
3058  $sql.= ' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc';
3059  $sql.= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseurdet as cd';
3060  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid';
3061  $sql.= ' WHERE cd.rowid = '.$rowid;
3062  $result = $this->db->query($sql);
3063  if ($result)
3064  {
3065  $objp = $this->db->fetch_object($result);
3066 
3067  $this->rowid = $objp->rowid;
3068  $this->id = $objp->rowid;
3069  $this->fk_commande = $objp->fk_commande;
3070  $this->desc = $objp->description;
3071  $this->qty = $objp->qty;
3072  $this->ref_fourn = $objp->ref;
3073  $this->ref_supplier = $objp->ref;
3074  $this->subprice = $objp->subprice;
3075  $this->tva_tx = $objp->tva_tx;
3076  $this->localtax1_tx = $objp->localtax1_tx;
3077  $this->localtax2_tx = $objp->localtax2_tx;
3078  $this->localtax1_type = $objp->localtax1_type;
3079  $this->localtax2_type = $objp->localtax2_type;
3080  $this->remise = $objp->remise;
3081  $this->remise_percent = $objp->remise_percent;
3082  $this->fk_product = $objp->fk_product;
3083  $this->info_bits = $objp->info_bits;
3084  $this->total_ht = $objp->total_ht;
3085  $this->total_tva = $objp->total_tva;
3086  $this->total_localtax1 = $objp->total_localtax1;
3087  $this->total_localtax2 = $objp->total_localtax2;
3088  $this->total_ttc = $objp->total_ttc;
3089  $this->product_type = $objp->product_type;
3090 
3091  $this->ref = $objp->product_ref;
3092  $this->product_ref = $objp->product_ref;
3093  $this->product_libelle = $objp->product_libelle;
3094  $this->product_desc = $objp->product_desc;
3095 
3096  $this->date_start = $this->db->jdate($objp->date_start);
3097  $this->date_end = $this->db->jdate($objp->date_end);
3098  $this->fk_unit = $objp->fk_unit;
3099 
3100  $this->multicurrency_subprice = $objp->multicurrency_subprice;
3101  $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
3102  $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
3103  $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
3104 
3105  $this->db->free($result);
3106  return 1;
3107  }
3108  else
3109  {
3110  dol_print_error($this->db);
3111  return -1;
3112  }
3113  }
3114 
3120  public function updateTotal()
3121  {
3122  $this->db->begin();
3123 
3124  // Mise a jour ligne en base
3125  $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseurdet SET";
3126  $sql.= " total_ht='".price2num($this->total_ht)."'";
3127  $sql.= ",total_tva='".price2num($this->total_tva)."'";
3128  $sql.= ",total_localtax1='".price2num($this->total_localtax1)."'";
3129  $sql.= ",total_localtax2='".price2num($this->total_localtax2)."'";
3130  $sql.= ",total_ttc='".price2num($this->total_ttc)."'";
3131  $sql.= " WHERE rowid = ".$this->rowid;
3132 
3133  dol_syslog(get_class($this)."::updateTotal", LOG_DEBUG);
3134 
3135  $resql=$this->db->query($sql);
3136  if ($resql)
3137  {
3138  $this->db->commit();
3139  return 1;
3140  }
3141  else
3142  {
3143  $this->error=$this->db->error();
3144  $this->db->rollback();
3145  return -2;
3146  }
3147  }
3148 
3155  public function insert($notrigger=0)
3156  {
3157  global $conf, $user;
3158 
3159  $error=0;
3160 
3161  dol_syslog(get_class($this)."::insert rang=".$this->rang);
3162 
3163  // Clean parameters
3164  if (empty($this->tva_tx)) $this->tva_tx=0;
3165  if (empty($this->localtax1_tx)) $this->localtax1_tx=0;
3166  if (empty($this->localtax2_tx)) $this->localtax2_tx=0;
3167  if (empty($this->localtax1_type)) $this->localtax1_type='0';
3168  if (empty($this->localtax2_type)) $this->localtax2_type='0';
3169  if (empty($this->total_localtax1)) $this->total_localtax1=0;
3170  if (empty($this->total_localtax2)) $this->total_localtax2=0;
3171  if (empty($this->rang)) $this->rang=0;
3172  if (empty($this->remise)) $this->remise=0;
3173  if (empty($this->remise_percent)) $this->remise_percent=0;
3174  if (empty($this->info_bits)) $this->info_bits=0;
3175  if (empty($this->special_code)) $this->special_code=0;
3176  if (empty($this->fk_parent_line)) $this->fk_parent_line=0;
3177  if (empty($this->pa_ht)) $this->pa_ht=0;
3178 
3179  // Multicurrency
3180  if (!empty($this->multicurrency_code)) list($this->fk_multicurrency,$this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code);
3181  if (empty($this->fk_multicurrency))
3182  {
3183  $this->multicurrency_code = $conf->currency;
3184  $this->fk_multicurrency = 0;
3185  $this->multicurrency_tx = 1;
3186  }
3187 
3188  // Check parameters
3189  if ($this->product_type < 0) return -1;
3190 
3191  $this->db->begin();
3192 
3193  // Insertion dans base de la ligne
3194  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
3195  $sql.= " (fk_commande, label, description, date_start, date_end,";
3196  $sql.= " fk_product, product_type,";
3197  $sql.= " qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
3198  $sql.= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
3199  $sql.= " fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc";
3200  $sql.= ")";
3201  $sql.= " VALUES (".$this->fk_commande.", '" . $this->db->escape($this->label) . "','" . $this->db->escape($this->desc) . "',";
3202  $sql.= " ".($this->date_start?"'".$this->db->idate($this->date_start)."'":"null").",";
3203  $sql.= " ".($this->date_end?"'".$this->db->idate($this->date_end)."'":"null").",";
3204  if ($this->fk_product) { $sql.= $this->fk_product.","; }
3205  else { $sql.= "null,"; }
3206  $sql.= "'".$this->db->escape($this->product_type)."',";
3207  $sql.= "'".$this->db->escape($this->qty)."', ";
3208 
3209  $sql.= " ".(empty($this->vat_src_code)?"''":"'".$this->db->escape($this->vat_src_code)."'").",";
3210  $sql.= " ".$this->tva_tx.", ";
3211  $sql.= " ".$this->localtax1_tx.",";
3212  $sql.= " ".$this->localtax2_tx.",";
3213  $sql.= " '".$this->db->escape($this->localtax1_type)."',";
3214  $sql.= " '".$this->db->escape($this->localtax2_type)."',";
3215  $sql.= " ".$this->remise_percent.", ".price2num($this->subprice,'MU').", '".$this->db->escape($this->ref_supplier)."',";
3216  $sql.= " ".price2num($this->total_ht).",";
3217  $sql.= " ".price2num($this->total_tva).",";
3218  $sql.= " ".price2num($this->total_localtax1).",";
3219  $sql.= " ".price2num($this->total_localtax2).",";
3220  $sql.= " ".price2num($this->total_ttc).",";
3221  $sql.= ($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'":"null");
3222  $sql.= ", ".($this->fk_multicurrency ? $this->fk_multicurrency : "null");
3223  $sql.= ", '".$this->db->escape($this->multicurrency_code)."'";
3224  $sql.= ", ".price2num($this->multicurrency_subprice);
3225  $sql.= ", ".price2num($this->multicurrency_total_ht);
3226  $sql.= ", ".price2num($this->multicurrency_total_tva);
3227  $sql.= ", ".price2num($this->multicurrency_total_ttc);
3228  $sql.= ")";
3229 
3230  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
3231  $resql=$this->db->query($sql);
3232  if ($resql)
3233  {
3234  $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
3235  $this->rowid =$this->id;
3236 
3237  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
3238  {
3239 
3240  $result=$this->insertExtraFields();
3241  if ($result < 0)
3242  {
3243  $error++;
3244  }
3245  }
3246 
3247  if (! $error && ! $notrigger)
3248  {
3249  // Call trigger
3250  $result=$this->call_trigger('LINEORDER_SUPPLIER_CREATE',$user);
3251  if ($result < 0) $error++;
3252  // End call triggers
3253  }
3254 
3255  if (!$error) {
3256  $this->db->commit();
3257  return 1;
3258  }
3259 
3260  foreach($this->errors as $errmsg)
3261  {
3262  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
3263  $this->errors[]=($this->errors?', '.$errmsg:$errmsg);
3264  }
3265  $this->db->rollback();
3266  return -1*$error;
3267  }
3268  else
3269  {
3270  $this->errors[]=$this->db->error();
3271  $this->db->rollback();
3272  return -2;
3273  }
3274  }
3281  public function update($notrigger=0)
3282  {
3283  global $conf,$user;
3284 
3285  $error=0;
3286 
3287  // Mise a jour ligne en base
3288  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
3289  $sql.= " description='".$this->db->escape($this->desc)."'";
3290  $sql.= ", ref='".$this->db->escape($this->ref_supplier)."'";
3291  $sql.= ", subprice='".price2num($this->subprice)."'";
3292  //$sql.= ",remise='".price2num($remise)."'";
3293  $sql.= ", remise_percent='".price2num($this->remise_percent)."'";
3294 
3295  $sql.= ", vat_src_code = '".(empty($this->vat_src_code)?'':$this->vat_src_code)."'";
3296  $sql.= ", tva_tx='".price2num($this->tva_tx)."'";
3297  $sql.= ", localtax1_tx='".price2num($this->total_localtax1)."'";
3298  $sql.= ", localtax2_tx='".price2num($this->total_localtax2)."'";
3299  $sql.= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
3300  $sql.= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
3301  $sql.= ", qty='".price2num($this->qty)."'";
3302  $sql.= ", date_start=".(! empty($this->date_start)?"'".$this->db->idate($this->date_start)."'":"null");
3303  $sql.= ", date_end=".(! empty($this->date_end)?"'".$this->db->idate($this->date_end)."'":"null");
3304  $sql.= ", info_bits='".$this->db->escape($this->info_bits)."'";
3305  $sql.= ", total_ht='".price2num($this->total_ht)."'";
3306  $sql.= ", total_tva='".price2num($this->total_tva)."'";
3307  $sql.= ", total_localtax1='".price2num($this->total_localtax1)."'";
3308  $sql.= ", total_localtax2='".price2num($this->total_localtax2)."'";
3309  $sql.= ", total_ttc='".price2num($this->total_ttc)."'";
3310  $sql.= ", product_type=".$this->product_type;
3311  $sql.= ($this->fk_unit ? ", fk_unit='".$this->db->escape($this->fk_unit)."'":", fk_unit=null");
3312 
3313  // Multicurrency
3314  $sql.= ", multicurrency_subprice=".price2num($this->multicurrency_subprice)."";
3315  $sql.= ", multicurrency_total_ht=".price2num($this->multicurrency_total_ht)."";
3316  $sql.= ", multicurrency_total_tva=".price2num($this->multicurrency_total_tva)."";
3317  $sql.= ", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc)."";
3318 
3319  $sql.= " WHERE rowid = ".$this->id;
3320 
3321  dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
3322  $result = $this->db->query($sql);
3323  if ($result > 0)
3324  {
3325  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
3326  {
3327 
3328  $result=$this->insertExtraFields();
3329  if ($result < 0)
3330  {
3331  $error++;
3332  }
3333  }
3334 
3335  if (! $error && ! $notrigger)
3336  {
3337  global $user;
3338  // Call trigger
3339  $result=$this->call_trigger('LINEORDER_SUPPLIER_UPDATE',$user);
3340  if ($result < 0)
3341  {
3342  $this->db->rollback();
3343  return -1;
3344  }
3345  // End call triggers
3346  }
3347 
3348  if (! $error)
3349  {
3350  $this->db->commit();
3351  return $result;
3352  }
3353  else
3354  {
3355  $this->db->rollback();
3356  return -1;
3357  }
3358  }
3359  else
3360  {
3361  $this->error=$this->db->lasterror();
3362  $this->db->rollback();
3363  return -1;
3364  }
3365  }
3366 
3373  function delete($notrigger)
3374  {
3375  global $user;
3376 
3377  $error=0;
3378 
3379  $this->db->begin();
3380 
3381  $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commande_fournisseurdet WHERE rowid=".$this->rowid;
3382 
3383  dol_syslog(__METHOD__, LOG_DEBUG);
3384  $resql=$this->db->query($sql);
3385  if ($resql)
3386  {
3387 
3388  if (!$notrigger)
3389  {
3390  // Call trigger
3391  $result=$this->call_trigger('LINEORDER_SUPPLIER_DELETE',$user);
3392  if ($result < 0) $error++;
3393  // End call triggers
3394  }
3395 
3396  if (!$error)
3397  {
3398  $this->db->commit();
3399  return 1;
3400  }
3401 
3402  foreach($this->errors as $errmsg)
3403  {
3404  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
3405  $this->error.=($this->error?', '.$errmsg:$errmsg);
3406  }
3407  $this->db->rollback();
3408  return -1*$error;
3409  }
3410  else
3411  {
3412  $this->error=$this->db->lasterror();
3413  return -1;
3414  }
3415  }
3416 }
3417 
createFromClone()
Load an object from its id and create a new one in database.
Class to manage stock movements.
Cancel($user, $idwarehouse=-1)
Cancel an approved order.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
const STATUS_DRAFT
Draft status.
static getIdFromCode(&$db, $code)
Get id of currency from code.
fetchObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $clause='OR', $alsosametype=1)
Fetch array of objects linked to current object.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='')
Delete all links between an object $this.
deleteline($idline, $notrigger=0)
Delete line.
const STATUS_RECEIVED_COMPLETELY
Received completely.
Class to manage table commandefournisseurdispatch.
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...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
set_date_livraison($user, $date_livraison, $notrigger=0)
Set the planned delivery date.
Class to manage products or services.
const STATUS_CANCELED_AFTER_ORDER
Order canceled/never received.
getNomUrl($withpicto=0, $option='', $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Class to manage Dolibarr users.
Definition: user.class.php:39
showDelay()
Show the customer delayed info.
Class to manage Dolibarr database access.
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.
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.
hasDelay()
Is the supplier order delayed?
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
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.
create($user, $notrigger=0)
Create order with draft status.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="")
Scan a directory and return a list of files/directories.
Definition: files.lib.php:58
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.
getInputMethod()
Returns the translated input method of object (defined if $this->methode_commande_id > 0)...
Class to manage standard extra fields.
updateTotal()
Mise a jour de l'objet ligne de commande en base.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create a document onto disk according to template model.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
calcAndSetStatusDispatch(User $user, $closeopenorder=1, $comment='')
Calc status regarding to dispatched stock.
fetch($id, $ref='')
Get object and lines from database.
update_price($exclspec=0, $roundingadjust='none', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines)...
info($id)
Charge les informations d'ordre info dans l'objet facture.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
const STATUS_RECEIVED_PARTIALLY
Received partially.
Class to manage customers orders.
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:1234
const STATUS_VALIDATED
Validated status.
getEntity($element, $shared=1, $forceentity=null)
Get list of entity id to use.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
deleteExtraFields()
Delete all extra fields values for the current object.
classifyBilled(User $user)
Class invoiced the supplier order.
setStatus($user, $status)
Tag order with a particular status.
commande($user, $date, $methode, $comment='')
Submit a supplier order to supplier.
dol_now($mode='gmt')
Return date for now.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
Superclass for orders classes.
Class to manage predefined suppliers products.
Livraison($user, $date, $type, $comment)
Set a delivery in database for this supplier order.
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)
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:85
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
load_board($user)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
getDispachedLines($status=-1)
Return array of dispathed lines waiting to be approved for this order.
const STATUS_ORDERSENT
Order sent, shipment on process.
Superclass for orders classes.
valid($user, $idwarehouse=0, $notrigger=0)
Validate an order.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->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)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
line_order($renum=false, $rowidorder='ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null)
Remove a file or several files with a mask.
Definition: files.lib.php:1103
update($notrigger=0)
Update the line object into db.
initAsSpecimen()
Initialise an instance with random values.
load_state_board()
Charge indicateurs this->nb de tableau de bord.
getLibStatut($mode=0)
Return label of the status of object.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
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.
get_methodes_commande()
Get list of order methods.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
insert($notrigger=0)
Insert line into database.
set_id_projet($user, $id_projet, $notrigger=0)
Set the id projet.
Class to manage predefined suppliers products.
Class to manage line orders.
LibStatut($statut, $mode=0, $billed=0)
Return label of a status.
static getIdAndTxFromCode(&$db, $code, $date_document='')
Get id and rate of currency from code.