dolibarr  18.0.0-alpha
reception.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2008 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
4  * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
5  * Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
6  * Copyright (C) 2011-2017 Juanjo Menent <jmenent@2byte.es>
7  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8  * Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr>
9  * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
10  * Copyright (C) 2014-2020 Francis Appels <francis.appels@yahoo.com>
11  * Copyright (C) 2015 Claudio Aschieri <c.aschieri@19.coop>
12  * Copyright (C) 2016-2022 Ferran Marcet <fmarcet@2byte.es>
13  * Copyright (C) 2018 Quentin Vial-Gouteyron <quentin.vial-gouteyron@atm-consulting.fr>
14  * Copyright (C) 2022-2023 Frédéric France <frederic.france@netlogic.fr>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program. If not, see <https://www.gnu.org/licenses/>.
28  */
29 
36 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
37 require_once DOL_DOCUMENT_ROOT."/core/class/commonobjectline.class.php";
38 require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
39 if (isModEnabled("propal")) {
40  require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
41 }
42 if (isModEnabled('commande')) {
43  require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
44 }
45 
46 
50 class Reception extends CommonObject
51 {
52  use CommonIncoterm;
53 
57  public $code = "";
58 
62  public $element = "reception";
63 
67  public $fk_element = "fk_reception";
68  public $table_element = "reception";
69  public $table_element_line = "commande_fournisseur_dispatch";
70  public $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
71 
75  public $picto = 'dollyrevert';
76 
77  public $socid;
78  public $ref_supplier;
79 
80  public $brouillon;
81  public $entrepot_id;
82  public $tracking_number;
83  public $tracking_url;
84  public $billed;
85  public $model_pdf;
86 
87  public $trueWeight;
88  public $weight_units;
89  public $trueWidth;
90  public $width_units;
91  public $trueHeight;
92  public $height_units;
93  public $trueDepth;
94  public $depth_units;
95  // A denormalized value
96  public $trueSize;
97 
98  public $date_delivery; // Date delivery planed
99 
105  public $date;
106 
110  public $date_reception;
111 
115  public $date_creation;
116 
120  public $date_valid;
121 
122  public $meths;
123  public $listmeths; // List of carriers
124 
128  public $lines = array();
129 
130 
131  // detail of lot and qty = array(id in llx_commande_fournisseur_dispatch, batch, qty)
132  // We can use this to know warehouse planned to be used for each lot.
133  public $detail_batch;
134 
135  const STATUS_DRAFT = 0;
136  const STATUS_VALIDATED = 1;
137  const STATUS_CLOSED = 2;
138 
139 
140 
146  public function __construct($db)
147  {
148  $this->db = $db;
149  }
150 
157  public function getNextNumRef($soc)
158  {
159  global $langs, $conf;
160  $langs->load("receptions");
161 
162  if (!empty($conf->global->RECEPTION_ADDON_NUMBER)) {
163  $mybool = false;
164 
165  $file = $conf->global->RECEPTION_ADDON_NUMBER.".php";
166  $classname = $conf->global->RECEPTION_ADDON_NUMBER;
167 
168  // Include file with class
169  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
170 
171  foreach ($dirmodels as $reldir) {
172  $dir = dol_buildpath($reldir."core/modules/reception/");
173 
174  // Load file with numbering class (if found)
175  $mybool |= @include_once $dir.$file;
176  }
177 
178  if (!$mybool) {
179  dol_print_error('', "Failed to include file ".$file);
180  return '';
181  }
182 
183  $obj = new $classname();
184 
185  $numref = "";
186  $numref = $obj->getNextValue($soc, $this);
187 
188  if ($numref != "") {
189  return $numref;
190  } else {
191  dol_print_error($this->db, get_class($this)."::getNextNumRef ".$obj->error);
192  return "";
193  }
194  } else {
195  print $langs->trans("Error")." ".$langs->trans("Error_RECEPTION_ADDON_NUMBER_NotDefined");
196  return "";
197  }
198  }
199 
207  public function create($user, $notrigger = 0)
208  {
209  global $conf, $hookmanager;
210 
211  $now = dol_now();
212 
213  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
214  $error = 0;
215 
216  // Clean parameters
217  $this->brouillon = 1;
218  $this->tracking_number = dol_sanitizeFileName($this->tracking_number);
219  if (empty($this->fk_project)) {
220  $this->fk_project = 0;
221  }
222  if (empty($this->weight_units)) {
223  $this->weight_units = 0;
224  }
225  if (empty($this->size_units)) {
226  $this->size_units = 0;
227  }
228 
229  $this->user = $user;
230 
231  $this->db->begin();
232 
233  $sql = "INSERT INTO ".MAIN_DB_PREFIX."reception (";
234  $sql .= "ref";
235  $sql .= ", entity";
236  $sql .= ", ref_supplier";
237  $sql .= ", date_creation";
238  $sql .= ", fk_user_author";
239  $sql .= ", date_reception";
240  $sql .= ", date_delivery";
241  $sql .= ", fk_soc";
242  $sql .= ", fk_projet";
243  $sql .= ", fk_shipping_method";
244  $sql .= ", tracking_number";
245  $sql .= ", weight";
246  $sql .= ", size";
247  $sql .= ", width";
248  $sql .= ", height";
249  $sql .= ", weight_units";
250  $sql .= ", size_units";
251  $sql .= ", note_private";
252  $sql .= ", note_public";
253  $sql .= ", model_pdf";
254  $sql .= ", fk_incoterms, location_incoterms";
255  $sql .= ") VALUES (";
256  $sql .= "'(PROV)'";
257  $sql .= ", ".((int) $conf->entity);
258  $sql .= ", ".($this->ref_supplier ? "'".$this->db->escape($this->ref_supplier)."'" : "null");
259  $sql .= ", '".$this->db->idate($now)."'";
260  $sql .= ", ".((int) $user->id);
261  $sql .= ", ".($this->date_reception > 0 ? "'".$this->db->idate($this->date_reception)."'" : "null");
262  $sql .= ", ".($this->date_delivery > 0 ? "'".$this->db->idate($this->date_delivery)."'" : "null");
263  $sql .= ", ".((int) $this->socid);
264  $sql .= ", ".((int) $this->fk_project);
265  $sql .= ", ".($this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) : "null");
266  $sql .= ", '".$this->db->escape($this->tracking_number)."'";
267  $sql .= ", ".(is_null($this->weight) ? "NULL" : ((double) $this->weight));
268  $sql .= ", ".(is_null($this->trueDepth) ? "NULL" : ((double) $this->trueDepth));
269  $sql .= ", ".(is_null($this->trueWidth) ? "NULL" : ((double) $this->trueWidth));
270  $sql .= ", ".(is_null($this->trueHeight) ? "NULL" : ((double) $this->trueHeight));
271  $sql .= ", ".(is_null($this->weight_units) ? "NULL" : ((double) $this->weight_units));
272  $sql .= ", ".(is_null($this->size_units) ? "NULL" : ((double) $this->size_units));
273  $sql .= ", ".(!empty($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null");
274  $sql .= ", ".(!empty($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null");
275  $sql .= ", ".(!empty($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null");
276  $sql .= ", ".(int) $this->fk_incoterms;
277  $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
278  $sql .= ")";
279 
280  dol_syslog(get_class($this)."::create", LOG_DEBUG);
281 
282  $resql = $this->db->query($sql);
283 
284  if ($resql) {
285  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."reception");
286 
287  $sql = "UPDATE ".MAIN_DB_PREFIX."reception";
288  $sql .= " SET ref = '(PROV".$this->id.")'";
289  $sql .= " WHERE rowid = ".((int) $this->id);
290 
291  dol_syslog(get_class($this)."::create", LOG_DEBUG);
292  if ($this->db->query($sql)) {
293  // Insert of lines
294  $num = count($this->lines);
295  for ($i = 0; $i < $num; $i++) {
296  $this->lines[$i]->fk_reception = $this->id;
297 
298  if (!$this->lines[$i]->create($user) > 0) {
299  $error++;
300  }
301  }
302 
303  if (!$error && $this->id && $this->origin_id) {
304  $ret = $this->add_object_linked();
305  if (!$ret) {
306  $error++;
307  }
308  }
309 
310  // Create extrafields
311  if (!$error) {
312  $result = $this->insertExtraFields();
313  if ($result < 0) {
314  $error++;
315  }
316  }
317 
318  if (!$error && !$notrigger) {
319  // Call trigger
320  $result = $this->call_trigger('RECEPTION_CREATE', $user);
321  if ($result < 0) {
322  $error++;
323  }
324  // End call triggers
325  }
326 
327  if (!$error) {
328  $this->db->commit();
329  return $this->id;
330  } else {
331  foreach ($this->errors as $errmsg) {
332  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
333  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
334  }
335  $this->db->rollback();
336  return -1 * $error;
337  }
338  } else {
339  $error++;
340  $this->error = $this->db->lasterror()." - sql=$sql";
341  $this->db->rollback();
342  return -2;
343  }
344  } else {
345  $error++;
346  $this->error = $this->db->error()." - sql=$sql";
347  $this->db->rollback();
348  return -1;
349  }
350  }
351 
352 
353 
362  public function fetch($id, $ref = '', $ref_ext = '')
363  {
364  // Check parameters
365  if (empty($id) && empty($ref) && empty($ref_ext)) {
366  return -1;
367  }
368 
369  $sql = "SELECT e.rowid, e.ref, e.fk_soc as socid, e.date_creation, e.ref_supplier, e.ref_ext, e.fk_user_author, e.fk_statut";
370  $sql .= ", e.weight, e.weight_units, e.size, e.size_units, e.width, e.height";
371  $sql .= ", e.date_reception as date_reception, e.model_pdf, e.date_delivery";
372  $sql .= ", e.fk_shipping_method, e.tracking_number";
373  $sql .= ", el.fk_source as origin_id, el.sourcetype as origin";
374  $sql .= ", e.note_private, e.note_public";
375  $sql .= ', e.fk_incoterms, e.location_incoterms';
376  $sql .= ', i.libelle as label_incoterms';
377  $sql .= " FROM ".MAIN_DB_PREFIX."reception as e";
378  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = e.rowid AND el.targettype = '".$this->db->escape($this->element)."'";
379  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON e.fk_incoterms = i.rowid';
380  $sql .= " WHERE e.entity IN (".getEntity('reception').")";
381  if ($id) {
382  $sql .= " AND e.rowid=".((int) $id);
383  }
384  if ($ref) {
385  $sql .= " AND e.ref='".$this->db->escape($ref)."'";
386  }
387  if ($ref_ext) {
388  $sql .= " AND e.ref_ext='".$this->db->escape($ref_ext)."'";
389  }
390 
391  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
392  $result = $this->db->query($sql);
393  if ($result) {
394  if ($this->db->num_rows($result)) {
395  $obj = $this->db->fetch_object($result);
396 
397  $this->id = $obj->rowid;
398  $this->ref = $obj->ref;
399  $this->socid = $obj->socid;
400  $this->ref_supplier = $obj->ref_supplier;
401  $this->ref_ext = $obj->ref_ext;
402  $this->statut = $obj->fk_statut;
403  $this->user_author_id = $obj->fk_user_author;
404  $this->date_creation = $this->db->jdate($obj->date_creation);
405  $this->date = $this->db->jdate($obj->date_reception); // TODO deprecated
406  $this->date_reception = $this->db->jdate($obj->date_reception); // Date real
407  $this->date_delivery = $this->db->jdate($obj->date_delivery); // Date planed
408  $this->model_pdf = $obj->model_pdf;
409  $this->modelpdf = $obj->model_pdf; // deprecated
410  $this->shipping_method_id = $obj->fk_shipping_method;
411  $this->tracking_number = $obj->tracking_number;
412  $this->origin = ($obj->origin ? $obj->origin : 'commande'); // For compatibility
413  $this->origin_id = $obj->origin_id;
414  $this->billed = ($obj->fk_statut == 2 ? 1 : 0);
415 
416  $this->trueWeight = $obj->weight;
417  $this->weight_units = $obj->weight_units;
418 
419  $this->trueWidth = $obj->width;
420  $this->width_units = $obj->size_units;
421  $this->trueHeight = $obj->height;
422  $this->height_units = $obj->size_units;
423  $this->trueDepth = $obj->size;
424  $this->depth_units = $obj->size_units;
425 
426  $this->note_public = $obj->note_public;
427  $this->note_private = $obj->note_private;
428 
429  // A denormalized value
430  $this->trueSize = $obj->size."x".$obj->width."x".$obj->height;
431  $this->size_units = $obj->size_units;
432 
433  //Incoterms
434  $this->fk_incoterms = $obj->fk_incoterms;
435  $this->location_incoterms = $obj->location_incoterms;
436  $this->label_incoterms = $obj->label_incoterms;
437 
438  $this->db->free($result);
439 
440  if ($this->statut == 0) {
441  $this->brouillon = 1;
442  }
443 
444  //$file = $conf->reception->dir_output."/".get_exdir(0, 0, 0, 1, $this, 'reception')."/".$this->id.".pdf";
445  //$this->pdf_filename = $file;
446 
447  // Tracking url
448  $this->getUrlTrackingStatus($obj->tracking_number);
449 
450  /*
451  * Thirdparty
452  */
453  $result = $this->fetch_thirdparty();
454 
455 
456  // Retrieve all extrafields for reception
457  // fetch optionals attributes and labels
458  require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
459  $extrafields = new ExtraFields($this->db);
460  $extrafields->fetch_name_optionals_label($this->table_element, true);
461  $this->fetch_optionals();
462 
463  /*
464  * Lines
465  */
466  $result = $this->fetch_lines();
467  if ($result < 0) {
468  return -3;
469  }
470 
471  return 1;
472  } else {
473  dol_syslog(get_class($this).'::Fetch no reception found', LOG_ERR);
474  $this->error = 'Delivery with id '.$id.' not found';
475  return 0;
476  }
477  } else {
478  $this->error = $this->db->error();
479  return -1;
480  }
481  }
482 
490  public function valid($user, $notrigger = 0)
491  {
492  global $conf, $langs;
493 
494  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
495 
496  dol_syslog(get_class($this)."::valid");
497 
498  // Protection
499  if ($this->statut) {
500  dol_syslog(get_class($this)."::valid no draft status", LOG_WARNING);
501  return 0;
502  }
503 
504  if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->reception->creer))
505  || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->reception->reception_advance->validate)))) {
506  $this->error = 'Permission denied';
507  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
508  return -1;
509  }
510 
511  $this->db->begin();
512 
513  $error = 0;
514 
515  // Define new ref
516  $soc = new Societe($this->db);
517  $soc->fetch($this->socid);
518 
519 
520  // Define new ref
521  if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
522  $numref = $this->getNextNumRef($soc);
523  } else {
524  $numref = $this->ref;
525  }
526 
527  $this->newref = dol_sanitizeFileName($numref);
528 
529  $now = dol_now();
530 
531  // Validate
532  $sql = "UPDATE ".MAIN_DB_PREFIX."reception SET";
533  $sql .= " ref='".$this->db->escape($numref)."'";
534  $sql .= ", fk_statut = 1";
535  $sql .= ", date_valid = '".$this->db->idate($now)."'";
536  $sql .= ", fk_user_valid = ".$user->id;
537  $sql .= " WHERE rowid = ".((int) $this->id);
538  dol_syslog(get_class($this)."::valid update reception", LOG_DEBUG);
539  $resql = $this->db->query($sql);
540  if (!$resql) {
541  $this->error = $this->db->lasterror();
542  $error++;
543  }
544 
545  // If stock increment is done on reception (recommanded choice)
546  if (!$error && isModEnabled('stock') && !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION)) {
547  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
548 
549  $langs->load("agenda");
550 
551  // Loop on each product line to add a stock movement
552  // TODO in future, reception lines may not be linked to order line
553  $sql = "SELECT cd.fk_product, cd.subprice, cd.remise_percent,";
554  $sql .= " ed.rowid, ed.qty, ed.fk_entrepot,";
555  $sql .= " ed.eatby, ed.sellby, ed.batch,";
556  $sql .= " ed.cost_price";
557  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd,";
558  $sql .= " ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as ed";
559  $sql .= " WHERE ed.fk_reception = ".((int) $this->id);
560  $sql .= " AND cd.rowid = ed.fk_commandefourndet";
561 
562  dol_syslog(get_class($this)."::valid select details", LOG_DEBUG);
563  $resql = $this->db->query($sql);
564  if ($resql) {
565  $cpt = $this->db->num_rows($resql);
566  for ($i = 0; $i < $cpt; $i++) {
567  $obj = $this->db->fetch_object($resql);
568 
569  $qty = $obj->qty;
570 
571  if ($qty == 0 || ($qty < 0 && !getDolGlobalInt('RECEPTION_ALLOW_NEGATIVE_QTY'))) {
572  continue;
573  }
574  dol_syslog(get_class($this)."::valid movement index ".$i." ed.rowid=".$obj->rowid." edb.rowid=".$obj->edbrowid);
575 
576  //var_dump($this->lines[$i]);
577  $mouvS = new MouvementStock($this->db);
578  $mouvS->origin = &$this;
579  $mouvS->setOrigin($this->element, $this->id);
580 
581  if (empty($obj->batch)) {
582  // line without batch detail
583 
584  // We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record.
585  $inventorycode = '';
586  $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->cost_price, $langs->trans("ReceptionValidatedInDolibarr", $numref), '', '', '', '', 0, $inventorycode);
587 
588  if (intval($result) < 0) {
589  $error++;
590  $this->errors[] = $mouvS->error;
591  $this->errors = array_merge($this->errors, $mouvS->errors);
592  break;
593  }
594  } else {
595  // line with batch detail
596 
597  // We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record.
598  // Note: ->fk_origin_stock = id into table llx_product_batch (may be rename into llx_product_stock_batch in another version)
599  $inventorycode = '';
600  $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->cost_price, $langs->trans("ReceptionValidatedInDolibarr", $numref), $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, '', 0, $inventorycode);
601 
602  if (intval($result) < 0) {
603  $error++;
604  $this->errors[] = $mouvS->error;
605  $this->errors = array_merge($this->errors, $mouvS->errors);
606  break;
607  }
608  }
609  }
610  } else {
611  $this->db->rollback();
612  $this->error = $this->db->error();
613  return -2;
614  }
615  }
616 
617  // Change status of order to "reception in process" or "totally received"
618  $status = $this->getStatusDispatch();
619  if ($status < 0) {
620  $error++;
621  } else {
622  $trigger_key = '';
624  $ret = $this->commandeFournisseur->Livraison($user, dol_now(), 'tot', '');
625  if ($ret < 0) {
626  $error++;
627  $this->errors = array_merge($this->errors, $this->commandeFournisseur->errors);
628  }
629  } else {
630  $ret = $this->setStatut($status, $this->origin_id, 'commande_fournisseur', $trigger_key);
631  if ($ret < 0) {
632  $error++;
633  }
634  }
635  }
636 
637  if (!$error && !$notrigger) {
638  // Call trigger
639  $result = $this->call_trigger('RECEPTION_VALIDATE', $user);
640  if ($result < 0) {
641  $error++;
642  }
643  // End call triggers
644  }
645 
646  if (!$error) {
647  $this->oldref = $this->ref;
648 
649  // Rename directory if dir was a temporary ref
650  if (preg_match('/^[\(]?PROV/i', $this->ref)) {
651  // Now we rename also files into index
652  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'reception/".$this->db->escape($this->newref)."'";
653  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'reception/".$this->db->escape($this->ref)."' AND entity = ".((int) $conf->entity);
654  $resql = $this->db->query($sql);
655  if (!$resql) {
656  $error++; $this->error = $this->db->lasterror();
657  }
658 
659  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
660  $oldref = dol_sanitizeFileName($this->ref);
661  $newref = dol_sanitizeFileName($numref);
662  $dirsource = $conf->reception->dir_output.'/'.$oldref;
663  $dirdest = $conf->reception->dir_output.'/'.$newref;
664  if (!$error && file_exists($dirsource)) {
665  dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
666 
667  if (@rename($dirsource, $dirdest)) {
668  dol_syslog("Rename ok");
669  // Rename docs starting with $oldref with $newref
670  $listoffiles = dol_dir_list($conf->reception->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
671  foreach ($listoffiles as $fileentry) {
672  $dirsource = $fileentry['name'];
673  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
674  $dirsource = $fileentry['path'].'/'.$dirsource;
675  $dirdest = $fileentry['path'].'/'.$dirdest;
676  @rename($dirsource, $dirdest);
677  }
678  }
679  }
680  }
681  }
682 
683  // Set new ref and current status
684  if (!$error) {
685  $this->ref = $numref;
686  $this->statut = 1;
687  }
688 
689  if (!$error) {
690  $this->db->commit();
691  return 1;
692  } else {
693  foreach ($this->errors as $errmsg) {
694  dol_syslog(get_class($this)."::valid ".$errmsg, LOG_ERR);
695  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
696  }
697  $this->db->rollback();
698  return -1 * $error;
699  }
700  }
701 
707  public function getStatusDispatch()
708  {
709  global $conf;
710 
711  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
712  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.dispatch.class.php';
713 
715 
716  if (!empty($this->origin) && $this->origin_id > 0 && ($this->origin == 'order_supplier' || $this->origin == 'commandeFournisseur')) {
717  if (empty($this->commandeFournisseur)) {
718  $this->fetch_origin();
719  if (empty($this->commandeFournisseur->lines)) {
720  $res = $this->commandeFournisseur->fetch_lines();
721  if ($res < 0) return $res;
722  }
723  }
724 
725  $qty_received = array();
726  $qty_wished = array();
727 
728  $supplierorderdispatch = new CommandeFournisseurDispatch($this->db);
729  $filter = array('t.fk_commande'=>$this->origin_id);
730  if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
731  $filter['t.status'] = 1; // Restrict to lines with status validated
732  }
733 
734  $ret = $supplierorderdispatch->fetchAll('', '', 0, 0, $filter);
735  if ($ret < 0) {
736  $this->error = $supplierorderdispatch->error;
737  $this->errors = $supplierorderdispatch->errors;
738  return $ret;
739  } else {
740  // build array with quantity received by product in all supplier orders (origin)
741  foreach ($supplierorderdispatch->lines as $dispatch_line) {
742  $qty_received[$dispatch_line->fk_product] += $dispatch_line->qty;
743  }
744 
745  // qty wished in order supplier (origin)
746  foreach ($this->commandeFournisseur->lines as $origin_line) {
747  // exclude lines not qualified for reception
748  if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $origin_line->product_type > 0) {
749  continue;
750  }
751 
752  $qty_wished[$origin_line->fk_product] += $origin_line->qty;
753  }
754 
755  // compare array
756  $diff_array = array_diff_assoc($qty_received, $qty_wished); // Warning: $diff_array is done only on common keys.
757  $keys_in_wished_not_in_received = array_diff(array_keys($qty_wished), array_keys($qty_received));
758  $keys_in_received_not_in_wished = array_diff(array_keys($qty_received), array_keys($qty_wished));
759 
760  if (count($diff_array) == 0 && count($keys_in_wished_not_in_received) == 0 && count($keys_in_received_not_in_wished) == 0) { // no diff => mean everything is received
762  } elseif (!empty($conf->global->SUPPLIER_ORDER_MORE_THAN_WISHED)) {
763  // set totally received if more products received than ordered
764  $close = 0;
765 
766  if (count($diff_array) > 0) {
767  // there are some difference between the two arrays
768  // scan the array of results
769  foreach ($diff_array as $key => $value) {
770  // if the quantity delivered is greater or equal to ordered quantity
771  if ($qty_received[$key] >= $qty_wished[$key]) {
772  $close++;
773  }
774  }
775  }
776 
777  if ($close == count($diff_array)) {
778  // all the products are received equal or more than the ordered quantity
780  }
781  }
782  }
783  }
784 
785  return $status;
786  }
787 
804  public function addline($entrepot_id, $id, $qty, $array_options = 0, $comment = '', $eatby = '', $sellby = '', $batch = '', $cost_price = 0)
805  {
806  global $conf, $langs, $user;
807 
808  $num = count($this->lines);
809  $line = new CommandeFournisseurDispatch($this->db);
810 
811  $line->fk_entrepot = $entrepot_id;
812  $line->fk_commandefourndet = $id;
813  $line->qty = $qty;
814 
815  $supplierorderline = new CommandeFournisseurLigne($this->db);
816  $result = $supplierorderline->fetch($id);
817  if ($result <= 0) {
818  $this->error = $supplierorderline->error;
819  $this->errors = $supplierorderline->errors;
820  return -1;
821  }
822 
823  $fk_product = 0;
824  if (isModEnabled('stock') && !empty($supplierorderline->fk_product)) {
825  $fk_product = $supplierorderline->fk_product;
826 
827  if (!($entrepot_id > 0) && empty($conf->global->STOCK_WAREHOUSE_NOT_REQUIRED_FOR_RECEPTIONS)) {
828  $langs->load("errors");
829  $this->error = $langs->trans("ErrorWarehouseRequiredIntoReceptionLine");
830  return -1;
831  }
832  }
833 
834  // Check batch is set
835  $product = new Product($this->db);
836  $product->fetch($fk_product);
837  if (isModEnabled('productbatch')) {
838  $langs->load("errors");
839  if (!empty($product->status_batch) && empty($batch)) {
840  $this->error = $langs->trans('ErrorProductNeedBatchNumber', $product->ref);
841  return -1;
842  } elseif (empty($product->status_batch) && !empty($batch)) {
843  $this->error = $langs->trans('ErrorProductDoesNotNeedBatchNumber', $product->ref);
844  return -1;
845  }
846  }
847  unset($product);
848 
849  // extrafields
850  $line->array_options = $supplierorderline->array_options;
851  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options) > 0) {
852  foreach ($array_options as $key => $value) {
853  $line->array_options[$key] = $value;
854  }
855  }
856 
857  $line->fk_product = $fk_product;
858  $line->fk_commande = $supplierorderline->fk_commande;
859  $line->fk_user = $user->id;
860  $line->comment = $comment;
861  $line->batch = $batch;
862  $line->eatby = $eatby;
863  $line->sellby = $sellby;
864  $line->status = 1;
865  $line->cost_price = $cost_price;
866  $line->fk_reception = $this->id;
867 
868  $this->lines[$num] = $line;
869 
870  return $num;
871  }
872 
873 
881  public function update($user = null, $notrigger = 0)
882  {
883  global $conf;
884  $error = 0;
885 
886  // Clean parameters
887 
888  if (isset($this->ref)) {
889  $this->ref = trim($this->ref);
890  }
891  if (isset($this->entity)) {
892  $this->entity = trim($this->entity);
893  }
894  if (isset($this->ref_supplier)) {
895  $this->ref_supplier = trim($this->ref_supplier);
896  }
897  if (isset($this->socid)) {
898  $this->socid = trim($this->socid);
899  }
900  if (isset($this->fk_user_author)) {
901  $this->fk_user_author = trim($this->fk_user_author);
902  }
903  if (isset($this->fk_user_valid)) {
904  $this->fk_user_valid = trim($this->fk_user_valid);
905  }
906  if (isset($this->shipping_method_id)) {
907  $this->shipping_method_id = trim($this->shipping_method_id);
908  }
909  if (isset($this->tracking_number)) {
910  $this->tracking_number = trim($this->tracking_number);
911  }
912  if (isset($this->statut)) {
913  $this->statut = (int) $this->statut;
914  }
915  if (isset($this->trueDepth)) {
916  $this->trueDepth = trim($this->trueDepth);
917  }
918  if (isset($this->trueWidth)) {
919  $this->trueWidth = trim($this->trueWidth);
920  }
921  if (isset($this->trueHeight)) {
922  $this->trueHeight = trim($this->trueHeight);
923  }
924  if (isset($this->size_units)) {
925  $this->size_units = trim($this->size_units);
926  }
927  if (isset($this->weight_units)) {
928  $this->weight_units = trim($this->weight_units);
929  }
930  if (isset($this->trueWeight)) {
931  $this->weight = trim($this->trueWeight);
932  }
933  if (isset($this->note_private)) {
934  $this->note_private = trim($this->note_private);
935  }
936  if (isset($this->note_public)) {
937  $this->note_public = trim($this->note_public);
938  }
939  if (isset($this->model_pdf)) {
940  $this->model_pdf = trim($this->model_pdf);
941  }
942 
943 
944  // Check parameters
945  // Put here code to add control on parameters values
946 
947  // Update request
948  $sql = "UPDATE ".MAIN_DB_PREFIX."reception SET";
949 
950  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
951  $sql .= " ref_supplier=".(isset($this->ref_supplier) ? "'".$this->db->escape($this->ref_supplier)."'" : "null").",";
952  $sql .= " fk_soc=".(isset($this->socid) ? $this->socid : "null").",";
953  $sql .= " date_creation=".(dol_strlen($this->date_creation) != 0 ? "'".$this->db->idate($this->date_creation)."'" : 'null').",";
954  $sql .= " fk_user_author=".(isset($this->fk_user_author) ? $this->fk_user_author : "null").",";
955  $sql .= " date_valid=".(dol_strlen($this->date_valid) != 0 ? "'".$this->db->idate($this->date_valid)."'" : 'null').",";
956  $sql .= " fk_user_valid=".(isset($this->fk_user_valid) ? $this->fk_user_valid : "null").",";
957  $sql .= " date_reception=".(dol_strlen($this->date_reception) != 0 ? "'".$this->db->idate($this->date_reception)."'" : 'null').",";
958  $sql .= " date_delivery=".(dol_strlen($this->date_delivery) != 0 ? "'".$this->db->idate($this->date_delivery)."'" : 'null').",";
959  $sql .= " fk_shipping_method=".((isset($this->shipping_method_id) && $this->shipping_method_id > 0) ? $this->shipping_method_id : "null").",";
960  $sql .= " tracking_number=".(isset($this->tracking_number) ? "'".$this->db->escape($this->tracking_number)."'" : "null").",";
961  $sql .= " fk_statut=".(isset($this->statut) ? $this->statut : "null").",";
962  $sql .= " height=".(($this->trueHeight != '') ? $this->trueHeight : "null").",";
963  $sql .= " width=".(($this->trueWidth != '') ? $this->trueWidth : "null").",";
964  $sql .= " size_units=".(isset($this->size_units) ? $this->size_units : "null").",";
965  $sql .= " size=".(($this->trueDepth != '') ? $this->trueDepth : "null").",";
966  $sql .= " weight_units=".(isset($this->weight_units) ? $this->weight_units : "null").",";
967  $sql .= " weight=".(($this->trueWeight != '') ? $this->trueWeight : "null").",";
968  $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
969  $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
970  $sql .= " model_pdf=".(isset($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null").",";
971  $sql .= " entity = ".((int) $conf->entity);
972  $sql .= " WHERE rowid=".((int) $this->id);
973 
974  $this->db->begin();
975 
976  dol_syslog(get_class($this)."::update", LOG_DEBUG);
977  $resql = $this->db->query($sql);
978  if (!$resql) {
979  $error++; $this->errors[] = "Error ".$this->db->lasterror();
980  }
981 
982  if (!$error) {
983  if (!$notrigger) {
984  // Call trigger
985  $result = $this->call_trigger('RECEPTION_MODIFY', $user);
986  if ($result < 0) {
987  $error++;
988  }
989  // End call triggers
990  }
991  }
992 
993  // Commit or rollback
994  if ($error) {
995  foreach ($this->errors as $errmsg) {
996  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
997  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
998  }
999  $this->db->rollback();
1000  return -1 * $error;
1001  } else {
1002  $this->db->commit();
1003  return 1;
1004  }
1005  }
1006 
1013  public function delete(User $user)
1014  {
1015  global $conf, $langs, $user;
1016  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1017 
1018  $error = 0;
1019  $this->error = '';
1020 
1021 
1022  $this->db->begin();
1023 
1024  // Stock control
1025  if ($conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_RECEPTION && $this->statut > 0) {
1026  require_once DOL_DOCUMENT_ROOT."/product/stock/class/mouvementstock.class.php";
1027 
1028  $langs->load("agenda");
1029 
1030  // Loop on each product line to add a stock movement
1031  $sql = "SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot, ed.eatby, ed.sellby, ed.batch, ed.rowid as commande_fournisseur_dispatch_id";
1032  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd,";
1033  $sql .= " ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as ed";
1034  $sql .= " WHERE ed.fk_reception = ".((int) $this->id);
1035  $sql .= " AND cd.rowid = ed.fk_commandefourndet";
1036 
1037  dol_syslog(get_class($this)."::delete select details", LOG_DEBUG);
1038  $resql = $this->db->query($sql);
1039  if ($resql) {
1040  $cpt = $this->db->num_rows($resql);
1041  for ($i = 0; $i < $cpt; $i++) {
1042  dol_syslog(get_class($this)."::delete movement index ".$i);
1043  $obj = $this->db->fetch_object($resql);
1044 
1045  $mouvS = new MouvementStock($this->db);
1046  // we do not log origin because it will be deleted
1047  $mouvS->origin = null;
1048 
1049  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, 0, $langs->trans("ReceptionDeletedInDolibarr", $this->ref), '', $obj->eatby, $obj->sellby, $obj->batch); // Price is set to 0, because we don't want to see WAP changed
1050  }
1051  } else {
1052  $error++; $this->errors[] = "Error ".$this->db->lasterror();
1053  }
1054  }
1055 
1056  if (!$error) {
1057  $main = MAIN_DB_PREFIX.'commande_fournisseur_dispatch';
1058  $ef = $main."_extrafields";
1059 
1060  $sqlef = "DELETE FROM ".$ef." WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_reception = ".((int) $this->id).")";
1061 
1062  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur_dispatch";
1063  $sql .= " WHERE fk_reception = ".((int) $this->id);
1064 
1065  if ($this->db->query($sqlef) && $this->db->query($sql)) {
1066  // Delete linked object
1067  $res = $this->deleteObjectLinked();
1068  if ($res < 0) {
1069  $error++;
1070  }
1071 
1072  if (!$error) {
1073  $sql = "DELETE FROM ".MAIN_DB_PREFIX."reception";
1074  $sql .= " WHERE rowid = ".((int) $this->id);
1075 
1076  if ($this->db->query($sql)) {
1077  // Call trigger
1078  $result = $this->call_trigger('RECEPTION_DELETE', $user);
1079  if ($result < 0) {
1080  $error++;
1081  }
1082  // End call triggers
1083 
1084  if (!empty($this->origin) && $this->origin_id > 0) {
1085  $this->fetch_origin();
1086  $origin = $this->origin;
1087  if ($this->$origin->statut == 4) { // If order source of reception is "partially received"
1088  // Check if there is no more reception. If not, we can move back status of order to "validated" instead of "reception in progress"
1089  $this->$origin->loadReceptions();
1090  //var_dump($this->$origin->receptions);exit;
1091  if (count($this->$origin->receptions) <= 0) {
1092  $this->$origin->setStatut(3); // ordered
1093  }
1094  }
1095  }
1096 
1097  if (!$error) {
1098  $this->db->commit();
1099 
1100  // We delete PDFs
1101  $ref = dol_sanitizeFileName($this->ref);
1102  if (!empty($conf->reception->dir_output)) {
1103  $dir = $conf->reception->dir_output.'/'.$ref;
1104  $file = $dir.'/'.$ref.'.pdf';
1105  if (file_exists($file)) {
1106  if (!dol_delete_file($file)) {
1107  return 0;
1108  }
1109  }
1110  if (file_exists($dir)) {
1111  if (!dol_delete_dir_recursive($dir)) {
1112  $this->error = $langs->trans("ErrorCanNotDeleteDir", $dir);
1113  return 0;
1114  }
1115  }
1116  }
1117 
1118  return 1;
1119  } else {
1120  $this->db->rollback();
1121  return -1;
1122  }
1123  } else {
1124  $this->error = $this->db->lasterror()." - sql=$sql";
1125  $this->db->rollback();
1126  return -3;
1127  }
1128  } else {
1129  $this->error = $this->db->lasterror()." - sql=$sql";
1130  $this->db->rollback();
1131  return -2;
1132  }
1133  } else {
1134  $this->error = $this->db->lasterror()." - sql=$sql";
1135  $this->db->rollback();
1136  return -1;
1137  }
1138  } else {
1139  $this->db->rollback();
1140  return -1;
1141  }
1142  }
1143 
1144  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1150  public function fetch_lines()
1151  {
1152  // phpcs:enable
1153  $this->lines = array();
1154 
1155  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.dispatch.class.php';
1156 
1157  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."commande_fournisseur_dispatch WHERE fk_reception = ".((int) $this->id);
1158  $resql = $this->db->query($sql);
1159 
1160  if (!empty($resql)) {
1161  while ($obj = $this->db->fetch_object($resql)) {
1162  $line = new CommandeFournisseurDispatch($this->db);
1163 
1164  $line->fetch($obj->rowid);
1165 
1166  // TODO Remove or keep this ?
1167  $line->fetch_product();
1168 
1169  $sql_commfourndet = 'SELECT qty, ref, label, description, tva_tx, vat_src_code, subprice, multicurrency_subprice, remise_percent, total_ht, total_ttc, total_tva';
1170  $sql_commfourndet .= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseurdet';
1171  $sql_commfourndet .= ' WHERE rowid = '.((int) $line->fk_commandefourndet);
1172  $sql_commfourndet .= ' ORDER BY rang';
1173 
1174  $resql_commfourndet = $this->db->query($sql_commfourndet);
1175  if (!empty($resql_commfourndet)) {
1176  $obj = $this->db->fetch_object($resql_commfourndet);
1177  $line->qty_asked = $obj->qty;
1178  $line->description = $obj->description;
1179  $line->desc = $obj->description;
1180  $line->tva_tx = $obj->tva_tx;
1181  $line->vat_src_code = $obj->vat_src_code;
1182  $line->subprice = $obj->subprice;
1183  $line->multicurrency_subprice = $obj->multicurrency_subprice;
1184  $line->remise_percent = $obj->remise_percent;
1185  $line->label = !empty($obj->label) ? $obj->label : $line->product->label;
1186  $line->ref_supplier = $obj->ref;
1187  $line->total_ht = $obj->total_ht;
1188  $line->total_ttc = $obj->total_ttc;
1189  $line->total_tva = $obj->total_tva;
1190  } else {
1191  $line->qty_asked = 0;
1192  $line->description = '';
1193  $line->desc = '';
1194  $line->label = $obj->label;
1195  }
1196 
1197  $pu_ht = ($line->subprice * $line->qty) * (100 - $line->remise_percent) / 100;
1198  $tva = $pu_ht * $line->tva_tx / 100;
1199  $this->total_ht += $pu_ht;
1200  $this->total_tva += $pu_ht * $line->tva_tx / 100;
1201 
1202  $this->total_ttc += $pu_ht + $tva;
1203 
1204  if (isModEnabled('productbatch') && !empty($line->batch)) {
1205  $detail_batch = new stdClass();
1206  $detail_batch->eatby = $line->eatby;
1207  $detail_batch->sellby = $line->sellby;
1208  $detail_batch->batch = $line->batch;
1209  $detail_batch->qty = $line->qty;
1210  $line->detail_batch[] = $detail_batch;
1211  }
1212 
1213  $this->lines[] = $line;
1214  }
1215 
1216  return 1;
1217  } else {
1218  return -1;
1219  }
1220  }
1221 
1232  public function getNomUrl($withpicto = 0, $option = 0, $max = 0, $short = 0, $notooltip = 0)
1233  {
1234  global $conf, $langs, $hookmanager;
1235  $result = '';
1236  $label = img_picto('', $this->picto).' <u>'.$langs->trans("Reception").'</u>';
1237  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1238  $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.($this->ref_supplier ? $this->ref_supplier : '');
1239 
1240  $url = DOL_URL_ROOT.'/reception/card.php?id='.$this->id;
1241 
1242  if ($short) {
1243  return $url;
1244  }
1245 
1246  $linkclose = '';
1247  if (empty($notooltip)) {
1248  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1249  $label = $langs->trans("Reception");
1250  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1251  }
1252  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1253  $linkclose .= ' class="classfortooltip"';
1254  }
1255 
1256  $linkstart = '<a href="'.$url.'"';
1257  $linkstart .= $linkclose.'>';
1258  $linkend = '</a>';
1259 
1260  $result .= $linkstart;
1261  if ($withpicto) {
1262  $result .= img_object(($notooltip ? '' : $label), $this->picto, '', 0, 0, $notooltip ? 0 : 1);
1263  }
1264  if ($withpicto != 2) {
1265  $result .= $this->ref;
1266  }
1267 
1268  $result .= $linkend;
1269 
1270  global $action;
1271  $hookmanager->initHooks(array($this->element . 'dao'));
1272  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1273  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1274  if ($reshook > 0) {
1275  $result = $hookmanager->resPrint;
1276  } else {
1277  $result .= $hookmanager->resPrint;
1278  }
1279  return $result;
1280  }
1281 
1288  public function getLibStatut($mode = 0)
1289  {
1290  return $this->LibStatut($this->statut, $mode);
1291  }
1292 
1293  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1301  public function LibStatut($status, $mode)
1302  {
1303  // phpcs:enable
1304  global $langs;
1305 
1306  // List of long language codes for status
1307  $this->labelStatus[-1] = 'StatusReceptionCanceled';
1308  $this->labelStatus[0] = 'StatusReceptionDraft';
1309  // product to receive if stock increase is on close or already received if stock increase is on validation
1310  $this->labelStatus[1] = 'StatusReceptionValidated';
1311  if (getDolGlobalInt("STOCK_CALCULATE_ON_RECEPTION")) {
1312  $this->labelStatus[1] = 'StatusReceptionValidatedReceived';
1313  }
1314  if (getDolGlobalInt("STOCK_CALCULATE_ON_RECEPTION_CLOSE")) {
1315  $this->labelStatus[1] = 'StatusReceptionValidatedToReceive';
1316  }
1317  $this->labelStatus[2] = 'StatusReceptionProcessed';
1318 
1319  // List of short language codes for status
1320  $this->labelStatusShort[-1] = 'StatusReceptionCanceledShort';
1321  $this->labelStatusShort[0] = 'StatusReceptionDraftShort';
1322  $this->labelStatusShort[1] = 'StatusReceptionValidatedShort';
1323  $this->labelStatusShort[2] = 'StatusReceptionProcessedShort';
1324 
1325  $labelStatus = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
1326  $labelStatusShort = $langs->transnoentitiesnoconv($this->labelStatusShort[$status]);
1327 
1328  $statusType = 'status'.$status;
1329  if ($status == self::STATUS_VALIDATED) {
1330  $statusType = 'status4';
1331  }
1332  if ($status == self::STATUS_CLOSED) {
1333  $statusType = 'status6';
1334  }
1335 
1336  return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
1337  }
1338 
1346  public function initAsSpecimen()
1347  {
1348  global $langs;
1349 
1350  include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
1351  include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.dispatch.class.php';
1352  $now = dol_now();
1353 
1354  dol_syslog(get_class($this)."::initAsSpecimen");
1355 
1356  $order = new CommandeFournisseur($this->db);
1357  $order->initAsSpecimen();
1358 
1359  // Initialise parametres
1360  $this->id = 0;
1361  $this->ref = 'SPECIMEN';
1362  $this->specimen = 1;
1363  $this->statut = 1;
1364  $this->livraison_id = 0;
1365  $this->date = $now;
1366  $this->date_creation = $now;
1367  $this->date_valid = $now;
1368  $this->date_delivery = $now;
1369  $this->date_reception = $now + 24 * 3600;
1370 
1371  $this->entrepot_id = 0;
1372  $this->socid = 1;
1373 
1374  $this->commande_id = 0;
1375  $this->commande = $order;
1376 
1377  $this->origin_id = 1;
1378  $this->origin = 'commande';
1379 
1380  $this->note_private = 'Private note';
1381  $this->note_public = 'Public note';
1382 
1383  $nbp = 5;
1384  $xnbp = 0;
1385  while ($xnbp < $nbp) {
1386  $line = new CommandeFournisseurDispatch($this->db);
1387  $line->desc = $langs->trans("Description")." ".$xnbp;
1388  $line->libelle = $langs->trans("Description")." ".$xnbp; // deprecated
1389  $line->label = $langs->trans("Description")." ".$xnbp;
1390  $line->qty = 10;
1391 
1392  $line->fk_product = $this->commande->lines[$xnbp]->fk_product;
1393 
1394  $this->lines[] = $line;
1395  $xnbp++;
1396  }
1397  }
1398 
1406  public function setDeliveryDate($user, $delivery_date)
1407  {
1408  // phpcs:enable
1409  if ($user->rights->reception->creer) {
1410  $sql = "UPDATE ".MAIN_DB_PREFIX."reception";
1411  $sql .= " SET date_delivery = ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : 'null');
1412  $sql .= " WHERE rowid = ".((int) $this->id);
1413 
1414  dol_syslog(get_class($this)."::setDeliveryDate", LOG_DEBUG);
1415  $resql = $this->db->query($sql);
1416  if ($resql) {
1417  $this->date_delivery = $delivery_date;
1418  return 1;
1419  } else {
1420  $this->error = $this->db->error();
1421  return -1;
1422  }
1423  } else {
1424  return -2;
1425  }
1426  }
1427 
1428  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1434  public function fetch_delivery_methods()
1435  {
1436  // phpcs:enable
1437  global $langs;
1438  $this->meths = array();
1439 
1440  $sql = "SELECT em.rowid, em.code, em.libelle";
1441  $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as em";
1442  $sql .= " WHERE em.active = 1";
1443  $sql .= " ORDER BY em.libelle ASC";
1444 
1445  $resql = $this->db->query($sql);
1446  if ($resql) {
1447  while ($obj = $this->db->fetch_object($resql)) {
1448  $label = $langs->trans('ReceptionMethod'.$obj->code);
1449  $this->meths[$obj->rowid] = ($label != 'ReceptionMethod'.$obj->code ? $label : $obj->libelle);
1450  }
1451  }
1452  }
1453 
1454  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1461  public function list_delivery_methods($id = '')
1462  {
1463  // phpcs:enable
1464  global $langs;
1465 
1466  $this->listmeths = array();
1467  $i = 0;
1468 
1469  $sql = "SELECT em.rowid, em.code, em.libelle, em.description, em.tracking, em.active";
1470  $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as em";
1471  if ($id != '') {
1472  $sql .= " WHERE em.rowid = ".((int) $id);
1473  }
1474 
1475  $resql = $this->db->query($sql);
1476  if ($resql) {
1477  while ($obj = $this->db->fetch_object($resql)) {
1478  $this->listmeths[$i]['rowid'] = $obj->rowid;
1479  $this->listmeths[$i]['code'] = $obj->code;
1480  $label = $langs->trans('ReceptionMethod'.$obj->code);
1481  $this->listmeths[$i]['libelle'] = ($label != 'ReceptionMethod'.$obj->code ? $label : $obj->libelle);
1482  $this->listmeths[$i]['description'] = $obj->description;
1483  $this->listmeths[$i]['tracking'] = $obj->tracking;
1484  $this->listmeths[$i]['active'] = $obj->active;
1485  $i++;
1486  }
1487  }
1488  }
1489 
1496  public function getUrlTrackingStatus($value = '')
1497  {
1498  if (!empty($this->shipping_method_id)) {
1499  $sql = "SELECT em.code, em.tracking";
1500  $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as em";
1501  $sql .= " WHERE em.rowid = ".((int) $this->shipping_method_id);
1502 
1503  $resql = $this->db->query($sql);
1504  if ($resql) {
1505  if ($obj = $this->db->fetch_object($resql)) {
1506  $tracking = $obj->tracking;
1507  }
1508  }
1509  }
1510 
1511  if (!empty($tracking) && !empty($value)) {
1512  $url = str_replace('{TRACKID}', $value, $tracking);
1513  $this->tracking_url = sprintf('<a target="_blank" rel="noopener noreferrer" href="%s">'.($value ? $value : 'url').'</a>', $url, $url);
1514  } else {
1515  $this->tracking_url = $value;
1516  }
1517  }
1518 
1524  public function setClosed()
1525  {
1526  global $conf, $langs, $user;
1527 
1528  $error = 0;
1529 
1530  $this->db->begin();
1531 
1532  $sql = 'UPDATE '.MAIN_DB_PREFIX.'reception SET fk_statut='.self::STATUS_CLOSED;
1533  $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut > 0';
1534 
1535  $resql = $this->db->query($sql);
1536  if ($resql) {
1537  // Set order billed if 100% of order is received (qty in reception lines match qty in order lines)
1538  if ($this->origin == 'order_supplier' && $this->origin_id > 0) {
1539  $order = new CommandeFournisseur($this->db);
1540  $order->fetch($this->origin_id);
1541 
1542  $order->loadReceptions(self::STATUS_CLOSED); // Fill $order->receptions = array(orderlineid => qty)
1543 
1544  $receptions_match_order = 1;
1545  foreach ($order->lines as $line) {
1546  $lineid = $line->id;
1547  $qty = $line->qty;
1548  if (($line->product_type == 0 || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) && $order->receptions[$lineid] < $qty) {
1549  $receptions_match_order = 0;
1550  $text = 'Qty for order line id '.$lineid.' is '.$qty.'. However in the receptions with status Reception::STATUS_CLOSED='.self::STATUS_CLOSED.' we have qty = '.$order->receptions[$lineid].', so we can t close order';
1551  dol_syslog($text);
1552  break;
1553  }
1554  }
1555  if ($receptions_match_order) {
1556  dol_syslog("Qty for the ".count($order->lines)." lines of order have same value for receptions with status Reception::STATUS_CLOSED=".self::STATUS_CLOSED.', so we close order');
1557  $order->Livraison($user, dol_now(), 'tot', 'Reception '.$this->ref);
1558  }
1559  }
1560 
1561  $this->statut = self::STATUS_CLOSED;
1562 
1563 
1564  // If stock increment is done on closing
1565  if (!$error && isModEnabled('stock') && !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION_CLOSE)) {
1566  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
1567 
1568  $langs->load("agenda");
1569 
1570  // Loop on each product line to add a stock movement
1571  // TODO possibilite de receptionner a partir d'une propale ou autre origine ?
1572  $sql = "SELECT cd.fk_product, cd.subprice,";
1573  $sql .= " ed.rowid, ed.qty, ed.fk_entrepot,";
1574  $sql .= " ed.eatby, ed.sellby, ed.batch,";
1575  $sql .= " ed.cost_price";
1576  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd,";
1577  $sql .= " ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as ed";
1578  $sql .= " WHERE ed.fk_reception = ".((int) $this->id);
1579  $sql .= " AND cd.rowid = ed.fk_commandefourndet";
1580 
1581  dol_syslog(get_class($this)."::valid select details", LOG_DEBUG);
1582  $resql = $this->db->query($sql);
1583 
1584  if ($resql) {
1585  $cpt = $this->db->num_rows($resql);
1586  for ($i = 0; $i < $cpt; $i++) {
1587  $obj = $this->db->fetch_object($resql);
1588 
1589  $qty = $obj->qty;
1590 
1591  if ($qty <= 0) {
1592  continue;
1593  }
1594  dol_syslog(get_class($this)."::valid movement index ".$i." ed.rowid=".$obj->rowid." edb.rowid=".$obj->edbrowid);
1595 
1596  $mouvS = new MouvementStock($this->db);
1597  $mouvS->origin = &$this;
1598  $mouvS->setOrigin($this->element, $this->id);
1599 
1600  if (empty($obj->batch)) {
1601  // line without batch detail
1602 
1603  // We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
1604  $inventorycode = '';
1605  $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->cost_price, $langs->trans("ReceptionClassifyClosedInDolibarr", $this->ref), '', '', '', '', 0, $inventorycode);
1606  if ($result < 0) {
1607  $this->error = $mouvS->error;
1608  $this->errors = $mouvS->errors;
1609  $error++; break;
1610  }
1611  } else {
1612  // line with batch detail
1613 
1614  // We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
1615  $inventorycode = '';
1616  $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->cost_price, $langs->trans("ReceptionClassifyClosedInDolibarr", $this->ref), $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, '', 0, $inventorycode);
1617 
1618  if ($result < 0) {
1619  $this->error = $mouvS->error;
1620  $this->errors = $mouvS->errors;
1621  $error++; break;
1622  }
1623  }
1624  }
1625  } else {
1626  $this->error = $this->db->lasterror();
1627  $error++;
1628  }
1629  }
1630 
1631  // Call trigger
1632  if (!$error) {
1633  $result = $this->call_trigger('RECEPTION_CLOSED', $user);
1634  if ($result < 0) {
1635  $error++;
1636  }
1637  }
1638  } else {
1639  dol_print_error($this->db);
1640  $error++;
1641  }
1642 
1643  if (!$error) {
1644  $this->db->commit();
1645  return 1;
1646  } else {
1647  $this->db->rollback();
1648  return -1;
1649  }
1650  }
1651 
1657  public function setBilled()
1658  {
1659  global $user;
1660  $error = 0;
1661 
1662  $this->db->begin();
1663 
1664  $this->setClosed();
1665 
1666  $sql = 'UPDATE '.MAIN_DB_PREFIX.'reception SET billed=1';
1667  $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut > 0';
1668 
1669  $resql = $this->db->query($sql);
1670  if ($resql) {
1671  $this->statut = 2;
1672  $this->billed = 1;
1673 
1674  // Call trigger
1675  $result = $this->call_trigger('RECEPTION_BILLED', $user);
1676  if ($result < 0) {
1677  $error++;
1678  }
1679  } else {
1680  $error++;
1681  $this->errors[] = $this->db->lasterror;
1682  }
1683 
1684  if (empty($error)) {
1685  $this->db->commit();
1686  return 1;
1687  } else {
1688  $this->db->rollback();
1689  return -1;
1690  }
1691  }
1692 
1698  public function reOpen()
1699  {
1700  global $conf, $langs, $user;
1701 
1702  $error = 0;
1703 
1704  $this->db->begin();
1705 
1706  $sql = 'UPDATE '.MAIN_DB_PREFIX.'reception SET fk_statut=1, billed=0';
1707  $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut > 0';
1708 
1709  $resql = $this->db->query($sql);
1710  if ($resql) {
1711  $this->statut = 1;
1712  $this->billed = 0;
1713 
1714  // If stock increment is done on closing
1715  if (!$error && isModEnabled('stock') && !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION_CLOSE)) {
1716  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
1717  $numref = $this->ref;
1718  $langs->load("agenda");
1719 
1720  // Loop on each product line to add a stock movement
1721  // TODO possibilite de receptionner a partir d'une propale ou autre origine
1722  $sql = "SELECT ed.fk_product, cd.subprice,";
1723  $sql .= " ed.rowid, ed.qty, ed.fk_entrepot,";
1724  $sql .= " ed.eatby, ed.sellby, ed.batch,";
1725  $sql .= " ed.cost_price";
1726  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd,";
1727  $sql .= " ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as ed";
1728  $sql .= " WHERE ed.fk_reception = ".((int) $this->id);
1729  $sql .= " AND cd.rowid = ed.fk_commandefourndet";
1730 
1731  dol_syslog(get_class($this)."::valid select details", LOG_DEBUG);
1732  $resql = $this->db->query($sql);
1733  if ($resql) {
1734  $cpt = $this->db->num_rows($resql);
1735  for ($i = 0; $i < $cpt; $i++) {
1736  $obj = $this->db->fetch_object($resql);
1737 
1738  $qty = $obj->qty;
1739 
1740  if ($qty <= 0) {
1741  continue;
1742  }
1743 
1744  dol_syslog(get_class($this)."::reopen reception movement index ".$i." ed.rowid=".$obj->rowid);
1745 
1746  //var_dump($this->lines[$i]);
1747  $mouvS = new MouvementStock($this->db);
1748  $mouvS->origin = &$this;
1749  $mouvS->setOrigin($this->element, $this->id);
1750 
1751  if (empty($obj->batch)) {
1752  // line without batch detail
1753 
1754  // We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
1755  $inventorycode = '';
1756  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->cost_price, $langs->trans("ReceptionUnClassifyCloseddInDolibarr", $numref), '', '', '', '', 0, $inventorycode);
1757 
1758  if ($result < 0) {
1759  $this->error = $mouvS->error;
1760  $this->errors = $mouvS->errors;
1761  $error++; break;
1762  }
1763  } else {
1764  // line with batch detail
1765 
1766  // We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
1767  $inventorycode = '';
1768  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->cost_price, $langs->trans("ReceptionUnClassifyCloseddInDolibarr", $numref), '', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, $obj->fk_origin_stock, $inventorycode);
1769  if ($result < 0) {
1770  $this->error = $mouvS->error;
1771  $this->errors = $mouvS->errors;
1772  $error++; break;
1773  }
1774  }
1775  }
1776  } else {
1777  $this->error = $this->db->lasterror();
1778  $error++;
1779  }
1780  }
1781 
1782  if (!$error) {
1783  // Call trigger
1784  $result = $this->call_trigger('RECEPTION_REOPEN', $user);
1785  if ($result < 0) {
1786  $error++;
1787  }
1788  }
1789 
1790  if (!$error && $this->origin == 'order_supplier') {
1791  $commande = new CommandeFournisseur($this->db);
1792  $commande->fetch($this->origin_id);
1793  $result = $commande->setStatus($user, 4);
1794  if ($result < 0) {
1795  $error++;
1796  $this->error = $commande->error;
1797  $this->errors = $commande->errors;
1798  }
1799  }
1800  } else {
1801  $error++;
1802  $this->errors[] = $this->db->lasterror();
1803  }
1804 
1805  if (!$error) {
1806  $this->db->commit();
1807  return 1;
1808  } else {
1809  $this->db->rollback();
1810  return -1;
1811  }
1812  }
1813 
1820  public function setDraft($user)
1821  {
1822  // phpcs:enable
1823  global $conf, $langs;
1824 
1825  $error = 0;
1826 
1827  // Protection
1828  if ($this->statut <= self::STATUS_DRAFT) {
1829  return 0;
1830  }
1831 
1832  if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->reception->creer))
1833  || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->reception->reception_advance->validate)))) {
1834  $this->error = 'Permission denied';
1835  return -1;
1836  }
1837 
1838  $this->db->begin();
1839 
1840  $sql = "UPDATE ".MAIN_DB_PREFIX."reception";
1841  $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
1842  $sql .= " WHERE rowid = ".((int) $this->id);
1843 
1844  dol_syslog(__METHOD__, LOG_DEBUG);
1845  if ($this->db->query($sql)) {
1846  // If stock increment is done on closing
1847  if (!$error && isModEnabled('stock') && !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION)) {
1848  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
1849 
1850  $langs->load("agenda");
1851 
1852  // Loop on each product line to add a stock movement
1853  // TODO possibilite de receptionner a partir d'une propale ou autre origine
1854  $sql = "SELECT cd.fk_product, cd.subprice,";
1855  $sql .= " ed.rowid, ed.qty, ed.fk_entrepot,";
1856  $sql .= " ed.eatby, ed.sellby, ed.batch,";
1857  $sql .= " ed.cost_price";
1858  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd,";
1859  $sql .= " ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as ed";
1860  $sql .= " WHERE ed.fk_reception = ".((int) $this->id);
1861  $sql .= " AND cd.rowid = ed.fk_commandefourndet";
1862 
1863  dol_syslog(get_class($this)."::valid select details", LOG_DEBUG);
1864  $resql = $this->db->query($sql);
1865  if ($resql) {
1866  $cpt = $this->db->num_rows($resql);
1867  for ($i = 0; $i < $cpt; $i++) {
1868  $obj = $this->db->fetch_object($resql);
1869 
1870  $qty = $obj->qty;
1871 
1872 
1873  if ($qty <= 0) {
1874  continue;
1875  }
1876  dol_syslog(get_class($this)."::reopen reception movement index ".$i." ed.rowid=".$obj->rowid." edb.rowid=".$obj->edbrowid);
1877 
1878  //var_dump($this->lines[$i]);
1879  $mouvS = new MouvementStock($this->db);
1880  $mouvS->origin = &$this;
1881  $mouvS->setOrigin($this->element, $this->id);
1882 
1883  if (empty($obj->batch)) {
1884  // line without batch detail
1885 
1886  // We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
1887  $inventorycode = '';
1888  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->cost_price, $langs->trans("ReceptionBackToDraftInDolibarr", $this->ref), '', '', '', '', 0, $inventorycode);
1889  if ($result < 0) {
1890  $this->error = $mouvS->error;
1891  $this->errors = $mouvS->errors;
1892  $error++;
1893  break;
1894  }
1895  } else {
1896  // line with batch detail
1897 
1898  // We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
1899  $inventorycode = '';
1900  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->cost_price, $langs->trans("ReceptionBackToDraftInDolibarr", $this->ref), '', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, 0, $inventorycode);
1901  if ($result < 0) {
1902  $this->error = $mouvS->error;
1903  $this->errors = $mouvS->errors;
1904  $error++; break;
1905  }
1906  }
1907  }
1908  } else {
1909  $this->error = $this->db->lasterror();
1910  $error++;
1911  }
1912  }
1913 
1914  if (!$error) {
1915  // Call trigger
1916  $result = $this->call_trigger('RECEPTION_UNVALIDATE', $user);
1917  if ($result < 0) {
1918  $error++;
1919  }
1920  }
1921  if ($this->origin == 'order_supplier') {
1922  if (!empty($this->origin) && $this->origin_id > 0) {
1923  $this->fetch_origin();
1924  $origin = $this->origin;
1925  if ($this->$origin->statut == 4) { // If order source of reception is "partially received"
1926  // Check if there is no more reception validated.
1927  $this->$origin->fetchObjectLinked();
1928  $setStatut = 1;
1929  if (!empty($this->$origin->linkedObjects['reception'])) {
1930  foreach ($this->$origin->linkedObjects['reception'] as $rcption) {
1931  if ($rcption->statut > 0) {
1932  $setStatut = 0;
1933  break;
1934  }
1935  }
1936  //var_dump($this->$origin->receptions);exit;
1937  if ($setStatut) {
1938  $this->$origin->setStatut(3); // ordered
1939  }
1940  }
1941  }
1942  }
1943  }
1944 
1945  if (!$error) {
1946  $this->statut = self::STATUS_DRAFT;
1947  $this->db->commit();
1948  return 1;
1949  } else {
1950  $this->db->rollback();
1951  return -1;
1952  }
1953  } else {
1954  $this->error = $this->db->error();
1955  $this->db->rollback();
1956  return -1;
1957  }
1958  }
1959 
1970  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1971  {
1972  global $conf, $langs;
1973 
1974  $langs->load("receptions");
1975 
1976  if (!dol_strlen($modele)) {
1977  $modele = 'squille';
1978 
1979  if ($this->model_pdf) {
1980  $modele = $this->model_pdf;
1981  } elseif (!empty($conf->global->RECEPTION_ADDON_PDF)) {
1982  $modele = $conf->global->RECEPTION_ADDON_PDF;
1983  }
1984  }
1985 
1986  $modelpath = "core/modules/reception/doc/";
1987 
1988  $this->fetch_origin();
1989 
1990  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1991  }
1992 
2001  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2002  {
2003  $tables = array('reception');
2004 
2005  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
2006  }
2007 
2016  public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
2017  {
2018  $tables = array(
2019  'commande_fournisseur_dispatch'
2020  );
2021 
2022  return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
2023  }
2024 }
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:50
Reception\getNomUrl
getNomUrl($withpicto=0, $option=0, $max=0, $short=0, $notooltip=0)
Return clicable link of object (with eventually picto)
Definition: reception.class.php:1232
Reception
Class to manage receptions.
Definition: reception.class.php:50
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1282
Reception\fetch_lines
fetch_lines()
Load lines.
Definition: reception.class.php:1150
dol_delete_dir_recursive
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
Definition: files.lib.php:1483
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Reception\setBilled
setBilled()
Classify the reception as invoiced (used when WORKFLOW_EXPEDITION_CLASSIFY_CLOSED_INVOICE is on)
Definition: reception.class.php:1657
Reception\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create a document onto disk according to template module.
Definition: reception.class.php:1970
$sql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:745
CommandeFournisseurDispatch
Class to manage table commandefournisseurdispatch.
Definition: fournisseur.commande.dispatch.class.php:34
Reception\create
create($user, $notrigger=0)
Create reception en base.
Definition: reception.class.php:207
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:5052
Reception\getStatusDispatch
getStatusDispatch()
Get status from all dispatched lines.
Definition: reception.class.php:707
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1117
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:62
CommonIncoterm
trait CommonIncoterm
Superclass for incoterm classes.
Definition: commonincoterm.class.php:29
CommonObject\deleteObjectLinked
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='', $f_user=null, $notrigger=0)
Delete all links between an object $this.
Definition: commonobject.class.php:4381
Reception\getNextNumRef
getNextNumRef($soc)
Return next contract ref.
Definition: reception.class.php:157
Reception\addline
addline($entrepot_id, $id, $qty, $array_options=0, $comment='', $eatby='', $sellby='', $batch='', $cost_price=0)
Add an reception line.
Definition: reception.class.php:804
CommonObject\setStatut
setStatut($status, $elementId=null, $elementType='', $trigkey='', $fieldstatus='fk_statut')
Set status of an object.
Definition: commonobject.class.php:4517
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:45
Reception\fetch_delivery_methods
fetch_delivery_methods()
Fetch deliveries method and return an array.
Definition: reception.class.php:1434
CommonObject\fetch_origin
fetch_origin()
Read linked origin object.
Definition: commonobject.class.php:2044
CommonObject\fetch_thirdparty
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
Definition: commonobject.class.php:1851
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4082
Reception\__construct
__construct($db)
Constructor.
Definition: reception.class.php:146
CommonObject\commonGenerateDocument
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
Definition: commonobject.class.php:5572
Reception\valid
valid($user, $notrigger=0)
Validate object and update stock if option enabled.
Definition: reception.class.php:490
dol_delete_file
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
Definition: files.lib.php:1332
Reception\fetch
fetch($id, $ref='', $ref_ext='')
Get object and lines from database.
Definition: reception.class.php:362
Reception\getLibStatut
getLibStatut($mode=0)
Return status label.
Definition: reception.class.php:1288
MouvementStock
Class to manage stock movements.
Definition: mouvementstock.class.php:31
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6348
Reception\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: reception.class.php:1346
Reception\setDeliveryDate
setDeliveryDate($user, $delivery_date)
Set the planned delivery date.
Definition: reception.class.php:1406
CommonObject\commonReplaceProduct
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
Definition: commonobject.class.php:8734
Reception\replaceThirdparty
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: reception.class.php:2001
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1689
Reception\LibStatut
LibStatut($status, $mode)
Return label of a status.
Definition: reception.class.php:1301
CommandeFournisseur\STATUS_RECEIVED_COMPLETELY
const STATUS_RECEIVED_COMPLETELY
Received completely.
Definition: fournisseur.commande.class.php:334
CommonObject\fetch_optionals
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
Definition: commonobject.class.php:6197
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3944
Reception\getUrlTrackingStatus
getUrlTrackingStatus($value='')
Forge an set tracking url.
Definition: reception.class.php:1496
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:167
ref
$object ref
Definition: info.php:78
CommandeFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.commande.class.php:48
User
Class to manage Dolibarr users.
Definition: user.class.php:46
ExtraFields
Class to manage standard extra fields.
Definition: extrafields.class.php:39
Product
Class to manage products or services.
Definition: product.class.php:46
CommonObject\add_object_linked
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
Definition: commonobject.class.php:3971
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10863
Reception\replaceProduct
static replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a product id with another one.
Definition: reception.class.php:2016
Reception\setClosed
setClosed()
Classify the reception as closed (this record also the stock movement)
Definition: reception.class.php:1524
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4419
Reception\update
update($user=null, $notrigger=0)
Update database.
Definition: reception.class.php:881
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:3003
Reception\reOpen
reOpen()
Classify the reception as validated/opened.
Definition: reception.class.php:1698
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5981
CommandeFournisseur\STATUS_RECEIVED_PARTIALLY
const STATUS_RECEIVED_PARTIALLY
Received partially.
Definition: fournisseur.commande.class.php:329
user
$conf db user
Definition: repair.php:124
Reception\setDraft
setDraft($user)
Set draft status.
Definition: reception.class.php:1820
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:116
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8705
CommandeFournisseurLigne
Class to manage line orders.
Definition: fournisseur.commande.class.php:3680
Reception\list_delivery_methods
list_delivery_methods($id='')
Fetch all deliveries method and return an array.
Definition: reception.class.php:1461