dolibarr  16.0.5
expedition.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@inodbox.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-2020 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-2017 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 Nicolas ZABOURI <info@inovea-conseil.com>
14  * Copyright (C) 2018-2022 Frédéric France <frederic.france@netlogic.fr>
15  * Copyright (C) 2020 Lenin Rivas <lenin@leninrivas.com>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <https://www.gnu.org/licenses/>.
29  */
30 
37 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
38 require_once DOL_DOCUMENT_ROOT."/core/class/commonobjectline.class.php";
39 require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
40 if (!empty($conf->propal->enabled)) {
41  require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
42 }
43 if (!empty($conf->commande->enabled)) {
44  require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
45 }
46 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionlinebatch.class.php';
47 
48 
52 class Expedition extends CommonObject
53 {
54  use CommonIncoterm;
55 
59  public $element = "shipping";
60 
64  public $fk_element = "fk_expedition";
65 
69  public $table_element = "expedition";
70 
74  public $table_element_line = "expeditiondet";
75 
80  public $ismultientitymanaged = 1;
81 
85  public $picto = 'dolly';
86 
87  public $socid;
88 
94  public $ref_client;
95 
99  public $ref_customer;
100 
105  public $ref_int;
106 
107  public $brouillon;
108 
112  public $entrepot_id;
113 
117  public $tracking_number;
118 
122  public $tracking_url;
123  public $billed;
124 
128  public $model_pdf;
129 
130  public $trueWeight;
131  public $weight_units;
132  public $trueWidth;
133  public $width_units;
134  public $trueHeight;
135  public $height_units;
136  public $trueDepth;
137  public $depth_units;
138  // A denormalized value
139  public $trueSize;
140 
144  public $date_delivery;
145 
150  public $date;
151 
157 
162  public $date_shipping;
163 
167  public $date_creation;
168 
172  public $date_valid;
173 
174  public $meths;
175  public $listmeths; // List of carriers
176 
177  public $lines = array();
178 
179 
183  const STATUS_DRAFT = 0;
184 
188  const STATUS_VALIDATED = 1;
189 
193  const STATUS_CLOSED = 2;
194 
198  const STATUS_CANCELED = -1;
199 
200 
206  public function __construct($db)
207  {
208  global $conf;
209 
210  $this->db = $db;
211 
212  // List of long language codes for status
213  $this->statuts = array();
214  $this->statuts[-1] = 'StatusSendingCanceled';
215  $this->statuts[0] = 'StatusSendingDraft';
216  $this->statuts[1] = 'StatusSendingValidated';
217  $this->statuts[2] = 'StatusSendingProcessed';
218 
219  // List of short language codes for status
220  $this->statutshorts = array();
221  $this->statutshorts[-1] = 'StatusSendingCanceledShort';
222  $this->statutshorts[0] = 'StatusSendingDraftShort';
223  $this->statutshorts[1] = 'StatusSendingValidatedShort';
224  $this->statutshorts[2] = 'StatusSendingProcessedShort';
225  }
226 
233  public function getNextNumRef($soc)
234  {
235  global $langs, $conf;
236  $langs->load("sendings");
237 
238  if (!empty($conf->global->EXPEDITION_ADDON_NUMBER)) {
239  $mybool = false;
240 
241  $file = $conf->global->EXPEDITION_ADDON_NUMBER.".php";
242  $classname = $conf->global->EXPEDITION_ADDON_NUMBER;
243 
244  // Include file with class
245  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
246 
247  foreach ($dirmodels as $reldir) {
248  $dir = dol_buildpath($reldir."core/modules/expedition/");
249 
250  // Load file with numbering class (if found)
251  $mybool |= @include_once $dir.$file;
252  }
253 
254  if (!$mybool) {
255  dol_print_error('', "Failed to include file ".$file);
256  return '';
257  }
258 
259  $obj = new $classname();
260  $numref = "";
261  $numref = $obj->getNextValue($soc, $this);
262 
263  if ($numref != "") {
264  return $numref;
265  } else {
266  dol_print_error($this->db, get_class($this)."::getNextNumRef ".$obj->error);
267  return "";
268  }
269  } else {
270  print $langs->trans("Error")." ".$langs->trans("Error_EXPEDITION_ADDON_NUMBER_NotDefined");
271  return "";
272  }
273  }
274 
282  public function create($user, $notrigger = 0)
283  {
284  global $conf, $hookmanager;
285 
286  $now = dol_now();
287 
288  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
289  $error = 0;
290 
291  // Clean parameters
292  $this->brouillon = 1;
293  $this->tracking_number = dol_sanitizeFileName($this->tracking_number);
294  if (empty($this->fk_project)) {
295  $this->fk_project = 0;
296  }
297 
298  $this->user = $user;
299 
300 
301  $this->db->begin();
302 
303  $sql = "INSERT INTO ".MAIN_DB_PREFIX."expedition (";
304  $sql .= "ref";
305  $sql .= ", entity";
306  $sql .= ", ref_customer";
307  $sql .= ", ref_int";
308  $sql .= ", ref_ext";
309  $sql .= ", date_creation";
310  $sql .= ", fk_user_author";
311  $sql .= ", date_expedition";
312  $sql .= ", date_delivery";
313  $sql .= ", fk_soc";
314  $sql .= ", fk_projet";
315  $sql .= ", fk_address";
316  $sql .= ", fk_shipping_method";
317  $sql .= ", tracking_number";
318  $sql .= ", weight";
319  $sql .= ", size";
320  $sql .= ", width";
321  $sql .= ", height";
322  $sql .= ", weight_units";
323  $sql .= ", size_units";
324  $sql .= ", note_private";
325  $sql .= ", note_public";
326  $sql .= ", model_pdf";
327  $sql .= ", fk_incoterms, location_incoterms";
328  $sql .= ") VALUES (";
329  $sql .= "'(PROV)'";
330  $sql .= ", ".((int) $conf->entity);
331  $sql .= ", ".($this->ref_customer ? "'".$this->db->escape($this->ref_customer)."'" : "null");
332  $sql .= ", ".($this->ref_int ? "'".$this->db->escape($this->ref_int)."'" : "null");
333  $sql .= ", ".($this->ref_ext ? "'".$this->db->escape($this->ref_ext)."'" : "null");
334  $sql .= ", '".$this->db->idate($now)."'";
335  $sql .= ", ".((int) $user->id);
336  $sql .= ", ".($this->date_expedition > 0 ? "'".$this->db->idate($this->date_expedition)."'" : "null");
337  $sql .= ", ".($this->date_delivery > 0 ? "'".$this->db->idate($this->date_delivery)."'" : "null");
338  $sql .= ", ".($this->socid > 0 ? ((int) $this->socid) : "null");
339  $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : "null");
340  $sql .= ", ".($this->fk_delivery_address > 0 ? $this->fk_delivery_address : "null");
341  $sql .= ", ".($this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) : "null");
342  $sql .= ", '".$this->db->escape($this->tracking_number)."'";
343  $sql .= ", ".(is_numeric($this->weight) ? $this->weight : 'NULL');
344  $sql .= ", ".(is_numeric($this->sizeS) ? $this->sizeS : 'NULL'); // TODO Should use this->trueDepth
345  $sql .= ", ".(is_numeric($this->sizeW) ? $this->sizeW : 'NULL'); // TODO Should use this->trueWidth
346  $sql .= ", ".(is_numeric($this->sizeH) ? $this->sizeH : 'NULL'); // TODO Should use this->trueHeight
347  $sql .= ", ".($this->weight_units != '' ? (int) $this->weight_units : 'NULL');
348  $sql .= ", ".($this->size_units != '' ? (int) $this->size_units : 'NULL');
349  $sql .= ", ".(!empty($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null");
350  $sql .= ", ".(!empty($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null");
351  $sql .= ", ".(!empty($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null");
352  $sql .= ", ".(int) $this->fk_incoterms;
353  $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
354  $sql .= ")";
355 
356  dol_syslog(get_class($this)."::create", LOG_DEBUG);
357  $resql = $this->db->query($sql);
358  if ($resql) {
359  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expedition");
360 
361  $sql = "UPDATE ".MAIN_DB_PREFIX."expedition";
362  $sql .= " SET ref = '(PROV".$this->id.")'";
363  $sql .= " WHERE rowid = ".((int) $this->id);
364 
365  dol_syslog(get_class($this)."::create", LOG_DEBUG);
366  if ($this->db->query($sql)) {
367  // Insert of lines
368  $num = count($this->lines);
369  for ($i = 0; $i < $num; $i++) {
370  if (empty($this->lines[$i]->product_type) || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
371  if (!isset($this->lines[$i]->detail_batch)) { // no batch management
372  if ($this->create_line($this->lines[$i]->entrepot_id, $this->lines[$i]->origin_line_id, $this->lines[$i]->qty, $this->lines[$i]->rang, $this->lines[$i]->array_options) <= 0) {
373  $error++;
374  }
375  } else { // with batch management
376  if ($this->create_line_batch($this->lines[$i], $this->lines[$i]->array_options) <= 0) {
377  $error++;
378  }
379  }
380  }
381  }
382 
383  if (!$error && $this->id && $this->origin_id) {
384  $ret = $this->add_object_linked();
385  if (!$ret) {
386  $error++;
387  }
388  }
389 
390  // Actions on extra fields
391  if (!$error) {
392  $result = $this->insertExtraFields();
393  if ($result < 0) {
394  $error++;
395  }
396  }
397 
398  if (!$error && !$notrigger) {
399  // Call trigger
400  $result = $this->call_trigger('SHIPPING_CREATE', $user);
401  if ($result < 0) {
402  $error++;
403  }
404  // End call triggers
405 
406  if (!$error) {
407  $this->db->commit();
408  return $this->id;
409  } else {
410  foreach ($this->errors as $errmsg) {
411  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
412  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
413  }
414  $this->db->rollback();
415  return -1 * $error;
416  }
417  } else {
418  $error++;
419  $this->db->rollback();
420  return -3;
421  }
422  } else {
423  $error++;
424  $this->error = $this->db->lasterror()." - sql=$sql";
425  $this->db->rollback();
426  return -2;
427  }
428  } else {
429  $error++;
430  $this->error = $this->db->error()." - sql=$sql";
431  $this->db->rollback();
432  return -1;
433  }
434  }
435 
436  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
447  public function create_line($entrepot_id, $origin_line_id, $qty, $rang = 0, $array_options = null)
448  {
449  //phpcs:enable
450  global $user;
451 
452  $expeditionline = new ExpeditionLigne($this->db);
453  $expeditionline->fk_expedition = $this->id;
454  $expeditionline->entrepot_id = $entrepot_id;
455  $expeditionline->fk_origin_line = $origin_line_id;
456  $expeditionline->qty = $qty;
457  $expeditionline->rang = $rang;
458  $expeditionline->array_options = $array_options;
459 
460  if (($lineId = $expeditionline->insert($user)) < 0) {
461  $this->errors[] = $expeditionline->error;
462  }
463  return $lineId;
464  }
465 
466 
467  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
475  public function create_line_batch($line_ext, $array_options = 0)
476  {
477  // phpcs:enable
478  $error = 0;
479  $stockLocationQty = array(); // associated array with batch qty in stock location
480 
481  $tab = $line_ext->detail_batch;
482  // create stockLocation Qty array
483  foreach ($tab as $detbatch) {
484  if ($detbatch->entrepot_id) {
485  $stockLocationQty[$detbatch->entrepot_id] += $detbatch->qty;
486  }
487  }
488  // create shipment lines
489  foreach ($stockLocationQty as $stockLocation => $qty) {
490  $line_id = $this->create_line($stockLocation, $line_ext->origin_line_id, $qty, $line_ext->rang, $array_options);
491  if ($line_id < 0) {
492  $error++;
493  } else {
494  // create shipment batch lines for stockLocation
495  foreach ($tab as $detbatch) {
496  if ($detbatch->entrepot_id == $stockLocation) {
497  if (!($detbatch->create($line_id) > 0)) { // Create an ExpeditionLineBatch
498  $error++;
499  }
500  }
501  }
502  }
503  }
504 
505  if (!$error) {
506  return 1;
507  } else {
508  return -1;
509  }
510  }
511 
521  public function fetch($id, $ref = '', $ref_ext = '', $notused = '')
522  {
523  global $conf;
524 
525  // Check parameters
526  if (empty($id) && empty($ref) && empty($ref_ext)) {
527  return -1;
528  }
529 
530  $sql = "SELECT e.rowid, e.entity, e.ref, e.fk_soc as socid, e.date_creation, e.ref_customer, e.ref_ext, e.ref_int, e.fk_user_author, e.fk_statut, e.fk_projet as fk_project, e.billed";
531  $sql .= ", e.date_valid";
532  $sql .= ", e.weight, e.weight_units, e.size, e.size_units, e.width, e.height";
533  $sql .= ", e.date_expedition as date_expedition, e.model_pdf, e.fk_address, e.date_delivery";
534  $sql .= ", e.fk_shipping_method, e.tracking_number";
535  $sql .= ", e.note_private, e.note_public";
536  $sql .= ', e.fk_incoterms, e.location_incoterms';
537  $sql .= ', i.libelle as label_incoterms';
538  $sql .= ', s.libelle as shipping_method';
539  $sql .= ", el.fk_source as origin_id, el.sourcetype as origin";
540  $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
541  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = e.rowid AND el.targettype = '".$this->db->escape($this->element)."'";
542  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON e.fk_incoterms = i.rowid';
543  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_shipment_mode as s ON e.fk_shipping_method = s.rowid';
544  $sql .= " WHERE e.entity IN (".getEntity('expedition').")";
545  if ($id) {
546  $sql .= " AND e.rowid = ".((int) $id);
547  }
548  if ($ref) {
549  $sql .= " AND e.ref='".$this->db->escape($ref)."'";
550  }
551  if ($ref_ext) {
552  $sql .= " AND e.ref_ext='".$this->db->escape($ref_ext)."'";
553  }
554  if ($notused) {
555  $sql .= " AND e.ref_int='".$this->db->escape($notused)."'";
556  }
557 
558  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
559  $result = $this->db->query($sql);
560  if ($result) {
561  if ($this->db->num_rows($result)) {
562  $obj = $this->db->fetch_object($result);
563 
564  $this->id = $obj->rowid;
565  $this->entity = $obj->entity;
566  $this->ref = $obj->ref;
567  $this->socid = $obj->socid;
568  $this->ref_customer = $obj->ref_customer;
569  $this->ref_ext = $obj->ref_ext;
570  $this->ref_int = $obj->ref_int;
571  $this->statut = $obj->fk_statut;
572  $this->user_author_id = $obj->fk_user_author;
573  $this->date_creation = $this->db->jdate($obj->date_creation);
574  $this->date_valid = $this->db->jdate($obj->date_valid);
575  $this->date = $this->db->jdate($obj->date_expedition); // TODO deprecated
576  $this->date_expedition = $this->db->jdate($obj->date_expedition); // TODO deprecated
577  $this->date_shipping = $this->db->jdate($obj->date_expedition); // Date real
578  $this->date_delivery = $this->db->jdate($obj->date_delivery); // Date planed
579  $this->fk_delivery_address = $obj->fk_address;
580  $this->model_pdf = $obj->model_pdf;
581  $this->modelpdf = $obj->model_pdf; // deprecated
582  $this->shipping_method_id = $obj->fk_shipping_method;
583  $this->shipping_method = $obj->shipping_method;
584  $this->tracking_number = $obj->tracking_number;
585  $this->origin = ($obj->origin ? $obj->origin : 'commande'); // For compatibility
586  $this->origin_id = $obj->origin_id;
587  $this->billed = $obj->billed;
588  $this->fk_project = $obj->fk_project;
589 
590  $this->trueWeight = $obj->weight;
591  $this->weight_units = $obj->weight_units;
592 
593  $this->trueWidth = $obj->width;
594  $this->width_units = $obj->size_units;
595  $this->trueHeight = $obj->height;
596  $this->height_units = $obj->size_units;
597  $this->trueDepth = $obj->size;
598  $this->depth_units = $obj->size_units;
599 
600  $this->note_public = $obj->note_public;
601  $this->note_private = $obj->note_private;
602 
603  // A denormalized value
604  $this->trueSize = $obj->size."x".$obj->width."x".$obj->height;
605  $this->size_units = $obj->size_units;
606 
607  //Incoterms
608  $this->fk_incoterms = $obj->fk_incoterms;
609  $this->location_incoterms = $obj->location_incoterms;
610  $this->label_incoterms = $obj->label_incoterms;
611 
612  $this->db->free($result);
613 
614  if ($this->statut == self::STATUS_DRAFT) {
615  $this->brouillon = 1;
616  }
617 
618  // Tracking url
619  $this->getUrlTrackingStatus($obj->tracking_number);
620 
621  // Thirdparty
622  $result = $this->fetch_thirdparty(); // TODO Remove this
623 
624  // Retrieve extrafields
625  $this->fetch_optionals();
626 
627  // Fix Get multicurrency param for transmited
628  if (!empty($conf->multicurrency->enabled)) {
629  if (!empty($this->multicurrency_code)) {
630  $this->multicurrency_code = $this->thirdparty->multicurrency_code;
631  }
632  if (!empty($conf->global->MULTICURRENCY_USE_ORIGIN_TX) && !empty($this->thirdparty->multicurrency_tx)) {
633  $this->multicurrency_tx = $this->thirdparty->multicurrency_tx;
634  }
635  }
636 
637  /*
638  * Lines
639  */
640  $result = $this->fetch_lines();
641  if ($result < 0) {
642  return -3;
643  }
644 
645  return 1;
646  } else {
647  dol_syslog(get_class($this).'::Fetch no expedition found', LOG_ERR);
648  $this->error = 'Delivery with id '.$id.' not found';
649  return 0;
650  }
651  } else {
652  $this->error = $this->db->error();
653  return -1;
654  }
655  }
656 
664  public function valid($user, $notrigger = 0)
665  {
666  global $conf, $langs;
667 
668  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
669 
670  dol_syslog(get_class($this)."::valid");
671 
672  // Protection
673  if ($this->statut) {
674  dol_syslog(get_class($this)."::valid not in draft status", LOG_WARNING);
675  return 0;
676  }
677 
678  if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->expedition->creer))
679  || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->expedition->shipping_advance->validate)))) {
680  $this->error = 'Permission denied';
681  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
682  return -1;
683  }
684 
685  $this->db->begin();
686 
687  $error = 0;
688 
689  // Define new ref
690  $soc = new Societe($this->db);
691  $soc->fetch($this->socid);
692 
693  // Class of company linked to order
694  $result = $soc->set_as_client();
695 
696  // Define new ref
697  if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
698  $numref = $this->getNextNumRef($soc);
699  } else {
700  $numref = "EXP".$this->id;
701  }
702  $this->newref = dol_sanitizeFileName($numref);
703 
704  $now = dol_now();
705 
706  // Validate
707  $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
708  $sql .= " ref='".$this->db->escape($numref)."'";
709  $sql .= ", fk_statut = 1";
710  $sql .= ", date_valid = '".$this->db->idate($now)."'";
711  $sql .= ", fk_user_valid = ".$user->id;
712  $sql .= " WHERE rowid = ".((int) $this->id);
713 
714  dol_syslog(get_class($this)."::valid update expedition", LOG_DEBUG);
715  $resql = $this->db->query($sql);
716  if (!$resql) {
717  $this->error = $this->db->lasterror();
718  $error++;
719  }
720 
721  // If stock increment is done on sending (recommanded choice)
722  if (!$error && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT)) {
723  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
724 
725  $langs->load("agenda");
726 
727  // Loop on each product line to add a stock movement
728  $sql = "SELECT cd.fk_product, cd.subprice,";
729  $sql .= " ed.rowid, ed.qty, ed.fk_entrepot,";
730  $sql .= " edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
731  $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd,";
732  $sql .= " ".MAIN_DB_PREFIX."expeditiondet as ed";
733  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
734  $sql .= " WHERE ed.fk_expedition = ".((int) $this->id);
735  $sql .= " AND cd.rowid = ed.fk_origin_line";
736 
737  dol_syslog(get_class($this)."::valid select details", LOG_DEBUG);
738  $resql = $this->db->query($sql);
739  if ($resql) {
740  $cpt = $this->db->num_rows($resql);
741  for ($i = 0; $i < $cpt; $i++) {
742  $obj = $this->db->fetch_object($resql);
743  if (empty($obj->edbrowid)) {
744  $qty = $obj->qty;
745  } else {
746  $qty = $obj->edbqty;
747  }
748  if ($qty <= 0) {
749  continue;
750  }
751  dol_syslog(get_class($this)."::valid movement index ".$i." ed.rowid=".$obj->rowid." edb.rowid=".$obj->edbrowid);
752 
753  //var_dump($this->lines[$i]);
754  $mouvS = new MouvementStock($this->db);
755  //$mouvS->origin = dol_clone($this, 1);
756  $mouvS->setOrigin($this->element, $this->id);
757 
758  if (empty($obj->edbrowid)) {
759  // line without batch detail
760 
761  // 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.
762  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans("ShipmentValidatedInDolibarr", $numref), '', '', '', '', 0, '', 1);
763 
764  if ($result < 0) {
765  $error++;
766  $this->error = $mouvS->error;
767  $this->errors = array_merge($this->errors, $mouvS->errors);
768  break;
769  }
770  } else {
771  // line with batch detail
772 
773  // 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.
774  // Note: ->fk_origin_stock = id into table llx_product_batch (may be rename into llx_product_stock_batch in another version)
775  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans("ShipmentValidatedInDolibarr", $numref), '', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, $obj->fk_origin_stock, '', 1);
776  if ($result < 0) {
777  $error++;
778  $this->error = $mouvS->error;
779  $this->errors = array_merge($this->errors, $mouvS->errors);
780  break;
781  }
782  }
783  }
784 
785  // If some stock lines are now 0, we can remove entry into llx_product_stock, but only if there is no child lines into llx_product_batch (detail of batch, because we can imagine
786  // having a lot1/qty=X and lot2/qty=-X, so 0 but we must not loose repartition of different lot.
787  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_stock WHERE reel = 0 AND rowid NOT IN (SELECT fk_product_stock FROM ".MAIN_DB_PREFIX."product_batch as pb)";
788  $resql = $this->db->query($sql);
789  // We do not test error, it can fails if there is child in batch details
790  } else {
791  $this->db->rollback();
792  $this->error = $this->db->error();
793  return -2;
794  }
795  }
796 
797  // Change status of order to "shipment in process"
798  $ret = $this->setStatut(Commande::STATUS_SHIPMENTONPROCESS, $this->origin_id, $this->origin);
799  if (!$ret) {
800  $error++;
801  }
802 
803  if (!$error && !$notrigger) {
804  // Call trigger
805  $result = $this->call_trigger('SHIPPING_VALIDATE', $user);
806  if ($result < 0) {
807  $error++;
808  }
809  // End call triggers
810  }
811 
812  if (!$error) {
813  $this->oldref = $this->ref;
814 
815  // Rename directory if dir was a temporary ref
816  if (preg_match('/^[\(]?PROV/i', $this->ref)) {
817  // Now we rename also files into index
818  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'expedition/sending/".$this->db->escape($this->newref)."'";
819  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'expedition/sending/".$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
820  $resql = $this->db->query($sql);
821  if (!$resql) {
822  $error++; $this->error = $this->db->lasterror();
823  }
824 
825  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
826  $oldref = dol_sanitizeFileName($this->ref);
827  $newref = dol_sanitizeFileName($numref);
828  $dirsource = $conf->expedition->dir_output.'/sending/'.$oldref;
829  $dirdest = $conf->expedition->dir_output.'/sending/'.$newref;
830  if (!$error && file_exists($dirsource)) {
831  dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
832 
833  if (@rename($dirsource, $dirdest)) {
834  dol_syslog("Rename ok");
835  // Rename docs starting with $oldref with $newref
836  $listoffiles = dol_dir_list($conf->expedition->dir_output.'/sending/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
837  foreach ($listoffiles as $fileentry) {
838  $dirsource = $fileentry['name'];
839  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
840  $dirsource = $fileentry['path'].'/'.$dirsource;
841  $dirdest = $fileentry['path'].'/'.$dirdest;
842  @rename($dirsource, $dirdest);
843  }
844  }
845  }
846  }
847  }
848 
849  // Set new ref and current status
850  if (!$error) {
851  $this->ref = $numref;
852  $this->statut = self::STATUS_VALIDATED;
853  }
854 
855  if (!$error) {
856  $this->db->commit();
857  return 1;
858  } else {
859  $this->db->rollback();
860  return -1 * $error;
861  }
862  }
863 
864 
865  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
872  public function create_delivery($user)
873  {
874  // phpcs:enable
875  global $conf;
876 
877  if ($conf->delivery_note->enabled) {
878  if ($this->statut == self::STATUS_VALIDATED || $this->statut == self::STATUS_CLOSED) {
879  // Expedition validee
880  include_once DOL_DOCUMENT_ROOT.'/delivery/class/delivery.class.php';
881  $delivery = new Delivery($this->db);
882  $result = $delivery->create_from_sending($user, $this->id);
883  if ($result > 0) {
884  return $result;
885  } else {
886  $this->error = $delivery->error;
887  return $result;
888  }
889  } else {
890  return 0;
891  }
892  } else {
893  return 0;
894  }
895  }
896 
909  public function addline($entrepot_id, $id, $qty, $array_options = 0)
910  {
911  global $conf, $langs;
912 
913  $num = count($this->lines);
914  $line = new ExpeditionLigne($this->db);
915 
916  $line->entrepot_id = $entrepot_id;
917  $line->origin_line_id = $id;
918  $line->fk_origin_line = $id;
919  $line->qty = $qty;
920 
921  $orderline = new OrderLine($this->db);
922  $orderline->fetch($id);
923 
924  // Copy the rang of the order line to the expedition line
925  $line->rang = $orderline->rang;
926  $line->product_type = $orderline->product_type;
927 
928  if (!empty($conf->stock->enabled) && !empty($orderline->fk_product)) {
929  $fk_product = $orderline->fk_product;
930 
931  if (!($entrepot_id > 0) && empty($conf->global->STOCK_WAREHOUSE_NOT_REQUIRED_FOR_SHIPMENTS)) {
932  $langs->load("errors");
933  $this->error = $langs->trans("ErrorWarehouseRequiredIntoShipmentLine");
934  return -1;
935  }
936 
937  if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT)) {
938  $product = new Product($this->db);
939  $product->fetch($fk_product);
940 
941  // Check must be done for stock of product into warehouse if $entrepot_id defined
942  if ($entrepot_id > 0) {
943  $product->load_stock('warehouseopen');
944  $product_stock = $product->stock_warehouse[$entrepot_id]->real;
945  } else {
946  $product_stock = $product->stock_reel;
947  }
948 
949  $product_type = $product->type;
950  if ($product_type == 0 || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
951  $isavirtualproduct = ($product->hasFatherOrChild(1) > 0);
952  // The product is qualified for a check of quantity (must be enough in stock to be added into shipment).
953  if (!$isavirtualproduct || empty($conf->global->PRODUIT_SOUSPRODUITS) || ($isavirtualproduct && empty($conf->global->STOCK_EXCLUDE_VIRTUAL_PRODUCTS))) { // If STOCK_EXCLUDE_VIRTUAL_PRODUCTS is set, we do not manage stock for kits/virtual products.
954  if ($product_stock < $qty) {
955  $langs->load("errors");
956  $this->error = $langs->trans('ErrorStockIsNotEnoughToAddProductOnShipment', $product->ref);
957  $this->errorhidden = 'ErrorStockIsNotEnoughToAddProductOnShipment';
958 
959  $this->db->rollback();
960  return -3;
961  }
962  }
963  }
964  }
965  }
966 
967  // If product need a batch number, we should not have called this function but addline_batch instead.
968  if (!empty($conf->productbatch->enabled) && !empty($orderline->fk_product) && !empty($orderline->product_tobatch)) {
969  $this->error = 'ADDLINE_WAS_CALLED_INSTEAD_OF_ADDLINEBATCH';
970  return -4;
971  }
972 
973  // extrafields
974  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
975  $line->array_options = $array_options;
976  }
977 
978  $this->lines[$num] = $line;
979 
980  return 1;
981  }
982 
983  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
991  public function addline_batch($dbatch, $array_options = 0)
992  {
993  // phpcs:enable
994  global $conf, $langs;
995 
996  $num = count($this->lines);
997  if ($dbatch['qty'] > 0) {
998  $line = new ExpeditionLigne($this->db);
999  $tab = array();
1000  foreach ($dbatch['detail'] as $key => $value) {
1001  if ($value['q'] > 0) {
1002  // $value['q']=qty to move
1003  // $value['id_batch']=id into llx_product_batch of record to move
1004  //var_dump($value);
1005 
1006  $linebatch = new ExpeditionLineBatch($this->db);
1007  $ret = $linebatch->fetchFromStock($value['id_batch']); // load serial, sellby, eatby
1008  if ($ret < 0) {
1009  $this->error = $linebatch->error;
1010  return -1;
1011  }
1012  $linebatch->qty = $value['q'];
1013  $tab[] = $linebatch;
1014 
1015  if ($conf->global->STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT) {
1016  require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
1017  $prod_batch = new Productbatch($this->db);
1018  $prod_batch->fetch($value['id_batch']);
1019 
1020  if ($prod_batch->qty < $linebatch->qty) {
1021  $langs->load("errors");
1022  $this->errors[] = $langs->trans('ErrorStockIsNotEnoughToAddProductOnShipment', $prod_batch->fk_product);
1023  dol_syslog(get_class($this)."::addline_batch error=Product ".$prod_batch->batch.": ".$this->errorsToString(), LOG_ERR);
1024  $this->db->rollback();
1025  return -1;
1026  }
1027  }
1028 
1029  //var_dump($linebatch);
1030  }
1031  }
1032  $line->entrepot_id = $linebatch->entrepot_id;
1033  $line->origin_line_id = $dbatch['ix_l']; // deprecated
1034  $line->fk_origin_line = $dbatch['ix_l'];
1035  $line->qty = $dbatch['qty'];
1036  $line->detail_batch = $tab;
1037 
1038  // extrafields
1039  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
1040  $line->array_options = $array_options;
1041  }
1042 
1043  //var_dump($line);
1044  $this->lines[$num] = $line;
1045  return 1;
1046  }
1047  }
1048 
1056  public function update($user = null, $notrigger = 0)
1057  {
1058  global $conf;
1059  $error = 0;
1060 
1061  // Clean parameters
1062 
1063  if (isset($this->ref)) {
1064  $this->ref = trim($this->ref);
1065  }
1066  if (isset($this->entity)) {
1067  $this->entity = (int) $this->entity;
1068  }
1069  if (isset($this->ref_customer)) {
1070  $this->ref_customer = trim($this->ref_customer);
1071  }
1072  if (isset($this->socid)) {
1073  $this->socid = (int) $this->socid;
1074  }
1075  if (isset($this->fk_user_author)) {
1076  $this->fk_user_author = (int) $this->fk_user_author;
1077  }
1078  if (isset($this->fk_user_valid)) {
1079  $this->fk_user_valid = (int) $this->fk_user_valid;
1080  }
1081  if (isset($this->fk_delivery_address)) {
1082  $this->fk_delivery_address = (int) $this->fk_delivery_address;
1083  }
1084  if (isset($this->shipping_method_id)) {
1085  $this->shipping_method_id = (int) $this->shipping_method_id;
1086  }
1087  if (isset($this->tracking_number)) {
1088  $this->tracking_number = trim($this->tracking_number);
1089  }
1090  if (isset($this->statut)) {
1091  $this->statut = (int) $this->statut;
1092  }
1093  if (isset($this->trueDepth)) {
1094  $this->trueDepth = trim($this->trueDepth);
1095  }
1096  if (isset($this->trueWidth)) {
1097  $this->trueWidth = trim($this->trueWidth);
1098  }
1099  if (isset($this->trueHeight)) {
1100  $this->trueHeight = trim($this->trueHeight);
1101  }
1102  if (isset($this->size_units)) {
1103  $this->size_units = trim($this->size_units);
1104  }
1105  if (isset($this->weight_units)) {
1106  $this->weight_units = trim($this->weight_units);
1107  }
1108  if (isset($this->trueWeight)) {
1109  $this->weight = trim($this->trueWeight);
1110  }
1111  if (isset($this->note_private)) {
1112  $this->note_private = trim($this->note_private);
1113  }
1114  if (isset($this->note_public)) {
1115  $this->note_public = trim($this->note_public);
1116  }
1117  if (isset($this->model_pdf)) {
1118  $this->model_pdf = trim($this->model_pdf);
1119  }
1120 
1121 
1122 
1123  // Check parameters
1124  // Put here code to add control on parameters values
1125 
1126  // Update request
1127  $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
1128 
1129  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
1130  $sql .= " ref_ext=".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").",";
1131  $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
1132  $sql .= " fk_soc=".(isset($this->socid) ? $this->socid : "null").",";
1133  $sql .= " date_creation=".(dol_strlen($this->date_creation) != 0 ? "'".$this->db->idate($this->date_creation)."'" : 'null').",";
1134  $sql .= " fk_user_author=".(isset($this->fk_user_author) ? $this->fk_user_author : "null").",";
1135  $sql .= " date_valid=".(dol_strlen($this->date_valid) != 0 ? "'".$this->db->idate($this->date_valid)."'" : 'null').",";
1136  $sql .= " fk_user_valid=".(isset($this->fk_user_valid) ? $this->fk_user_valid : "null").",";
1137  $sql .= " date_expedition=".(dol_strlen($this->date_expedition) != 0 ? "'".$this->db->idate($this->date_expedition)."'" : 'null').",";
1138  $sql .= " date_delivery=".(dol_strlen($this->date_delivery) != 0 ? "'".$this->db->idate($this->date_delivery)."'" : 'null').",";
1139  $sql .= " fk_address=".(isset($this->fk_delivery_address) ? $this->fk_delivery_address : "null").",";
1140  $sql .= " fk_shipping_method=".((isset($this->shipping_method_id) && $this->shipping_method_id > 0) ? $this->shipping_method_id : "null").",";
1141  $sql .= " tracking_number=".(isset($this->tracking_number) ? "'".$this->db->escape($this->tracking_number)."'" : "null").",";
1142  $sql .= " fk_statut=".(isset($this->statut) ? $this->statut : "null").",";
1143  $sql .= " fk_projet=".(isset($this->fk_project) ? $this->fk_project : "null").",";
1144  $sql .= " height=".(($this->trueHeight != '') ? $this->trueHeight : "null").",";
1145  $sql .= " width=".(($this->trueWidth != '') ? $this->trueWidth : "null").",";
1146  $sql .= " size_units=".(isset($this->size_units) ? $this->size_units : "null").",";
1147  $sql .= " size=".(($this->trueDepth != '') ? $this->trueDepth : "null").",";
1148  $sql .= " weight_units=".(isset($this->weight_units) ? $this->weight_units : "null").",";
1149  $sql .= " weight=".(($this->trueWeight != '') ? $this->trueWeight : "null").",";
1150  $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
1151  $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
1152  $sql .= " model_pdf=".(isset($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null").",";
1153  $sql .= " entity=".$conf->entity;
1154 
1155  $sql .= " WHERE rowid=".((int) $this->id);
1156 
1157  $this->db->begin();
1158 
1159  dol_syslog(get_class($this)."::update", LOG_DEBUG);
1160  $resql = $this->db->query($sql);
1161  if (!$resql) {
1162  $error++; $this->errors[] = "Error ".$this->db->lasterror();
1163  }
1164 
1165  if (!$error && !$notrigger) {
1166  // Call trigger
1167  $result = $this->call_trigger('SHIPPING_MODIFY', $user);
1168  if ($result < 0) {
1169  $error++;
1170  }
1171  // End call triggers
1172  }
1173 
1174  // Commit or rollback
1175  if ($error) {
1176  foreach ($this->errors as $errmsg) {
1177  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1178  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1179  }
1180  $this->db->rollback();
1181  return -1 * $error;
1182  } else {
1183  $this->db->commit();
1184  return 1;
1185  }
1186  }
1187 
1188 
1196  public function cancel($notrigger = 0, $also_update_stock = false)
1197  {
1198  global $conf, $langs, $user;
1199 
1200  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1201 
1202  $error = 0;
1203  $this->error = '';
1204 
1205  $this->db->begin();
1206 
1207  // Add a protection to refuse deleting if shipment has at least one delivery
1208  $this->fetchObjectLinked($this->id, 'shipping', 0, 'delivery'); // Get deliveries linked to this shipment
1209  if (count($this->linkedObjectsIds) > 0) {
1210  $this->error = 'ErrorThereIsSomeDeliveries';
1211  $error++;
1212  }
1213 
1214  if (!$error && !$notrigger) {
1215  // Call trigger
1216  $result = $this->call_trigger('SHIPPING_CANCEL', $user);
1217  if ($result < 0) {
1218  $error++;
1219  }
1220  // End call triggers
1221  }
1222 
1223  // Stock control
1224  if (!$error && $conf->stock->enabled &&
1225  (($conf->global->STOCK_CALCULATE_ON_SHIPMENT && $this->statut > self::STATUS_DRAFT) ||
1226  ($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE && $this->statut == self::STATUS_CLOSED && $also_update_stock))) {
1227  require_once DOL_DOCUMENT_ROOT."/product/stock/class/mouvementstock.class.php";
1228 
1229  $langs->load("agenda");
1230 
1231  // Loop on each product line to add a stock movement and delete features
1232  $sql = "SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot, ed.rowid as expeditiondet_id";
1233  $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd,";
1234  $sql .= " ".MAIN_DB_PREFIX."expeditiondet as ed";
1235  $sql .= " WHERE ed.fk_expedition = ".((int) $this->id);
1236  $sql .= " AND cd.rowid = ed.fk_origin_line";
1237 
1238  dol_syslog(get_class($this)."::delete select details", LOG_DEBUG);
1239  $resql = $this->db->query($sql);
1240  if ($resql) {
1241  $cpt = $this->db->num_rows($resql);
1242 
1243  $shipmentlinebatch = new ExpeditionLineBatch($this->db);
1244 
1245  for ($i = 0; $i < $cpt; $i++) {
1246  dol_syslog(get_class($this)."::delete movement index ".$i);
1247  $obj = $this->db->fetch_object($resql);
1248 
1249  $mouvS = new MouvementStock($this->db);
1250  // we do not log origin because it will be deleted
1251  $mouvS->origin = null;
1252  // get lot/serial
1253  $lotArray = null;
1254  if (isModEnabled('productbatch')) {
1255  $lotArray = $shipmentlinebatch->fetchAll($obj->expeditiondet_id);
1256  if (!is_array($lotArray)) {
1257  $error++;
1258  $this->errors[] = "Error ".$this->db->lasterror();
1259  }
1260  }
1261 
1262  if (empty($lotArray)) {
1263  // no lot/serial
1264  // We increment stock of product (and sub-products)
1265  // We use warehouse selected for each line
1266  $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, 0, $langs->trans("ShipmentCanceledInDolibarr", $this->ref)); // Price is set to 0, because we don't want to see WAP changed
1267  if ($result < 0) {
1268  $error++;
1269  $this->errors = array_merge($this->errors, $mouvS->errors);
1270  break;
1271  }
1272  } else {
1273  // We increment stock of batches
1274  // We use warehouse selected for each line
1275  foreach ($lotArray as $lot) {
1276  $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $lot->qty, 0, $langs->trans("ShipmentCanceledInDolibarr", $this->ref), $lot->eatby, $lot->sellby, $lot->batch); // Price is set to 0, because we don't want to see WAP changed
1277  if ($result < 0) {
1278  $error++;
1279  $this->errors = array_merge($this->errors, $mouvS->errors);
1280  break;
1281  }
1282  }
1283  if ($error) {
1284  break; // break for loop incase of error
1285  }
1286  }
1287  }
1288  } else {
1289  $error++; $this->errors[] = "Error ".$this->db->lasterror();
1290  }
1291  }
1292 
1293  // delete batch expedition line
1294  if (!$error && $conf->productbatch->enabled) {
1295  $shipmentlinebatch = new ExpeditionLineBatch($this->db);
1296  if ($shipmentlinebatch->deleteFromShipment($this->id) < 0) {
1297  $error++; $this->errors[] = "Error ".$this->db->lasterror();
1298  }
1299  }
1300 
1301 
1302  if (!$error) {
1303  $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet";
1304  $sql .= " WHERE fk_expedition = ".((int) $this->id);
1305 
1306  if ($this->db->query($sql)) {
1307  // Delete linked object
1308  $res = $this->deleteObjectLinked();
1309  if ($res < 0) {
1310  $error++;
1311  }
1312 
1313  // No delete expedition
1314  if (!$error) {
1315  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."expedition";
1316  $sql .= " WHERE rowid = ".((int) $this->id);
1317 
1318  if ($this->db->query($sql)) {
1319  if (!empty($this->origin) && $this->origin_id > 0) {
1320  $this->fetch_origin();
1321  $origin = $this->origin;
1322  if ($this->$origin->statut == Commande::STATUS_SHIPMENTONPROCESS) { // If order source of shipment is "shipment in progress"
1323  // Check if there is no more shipment. If not, we can move back status of order to "validated" instead of "shipment in progress"
1324  $this->$origin->loadExpeditions();
1325  //var_dump($this->$origin->expeditions);exit;
1326  if (count($this->$origin->expeditions) <= 0) {
1327  $this->$origin->setStatut(Commande::STATUS_VALIDATED);
1328  }
1329  }
1330  }
1331 
1332  if (!$error) {
1333  $this->db->commit();
1334 
1335  // We delete PDFs
1336  $ref = dol_sanitizeFileName($this->ref);
1337  if (!empty($conf->expedition->dir_output)) {
1338  $dir = $conf->expedition->dir_output.'/sending/'.$ref;
1339  $file = $dir.'/'.$ref.'.pdf';
1340  if (file_exists($file)) {
1341  if (!dol_delete_file($file)) {
1342  return 0;
1343  }
1344  }
1345  if (file_exists($dir)) {
1346  if (!dol_delete_dir_recursive($dir)) {
1347  $this->error = $langs->trans("ErrorCanNotDeleteDir", $dir);
1348  return 0;
1349  }
1350  }
1351  }
1352 
1353  return 1;
1354  } else {
1355  $this->db->rollback();
1356  return -1;
1357  }
1358  } else {
1359  $this->error = $this->db->lasterror()." - sql=$sql";
1360  $this->db->rollback();
1361  return -3;
1362  }
1363  } else {
1364  $this->error = $this->db->lasterror()." - sql=$sql";
1365  $this->db->rollback();
1366  return -2;
1367  }//*/
1368  } else {
1369  $this->error = $this->db->lasterror()." - sql=$sql";
1370  $this->db->rollback();
1371  return -1;
1372  }
1373  } else {
1374  $this->db->rollback();
1375  return -1;
1376  }
1377  }
1378 
1387  public function delete($notrigger = 0, $also_update_stock = false)
1388  {
1389  global $conf, $langs, $user;
1390 
1391  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1392 
1393  $error = 0;
1394  $this->error = '';
1395 
1396  $this->db->begin();
1397 
1398  // Add a protection to refuse deleting if shipment has at least one delivery
1399  $this->fetchObjectLinked($this->id, 'shipping', 0, 'delivery'); // Get deliveries linked to this shipment
1400  if (count($this->linkedObjectsIds) > 0) {
1401  $this->error = 'ErrorThereIsSomeDeliveries';
1402  $error++;
1403  }
1404 
1405  if (!$error && !$notrigger) {
1406  // Call trigger
1407  $result = $this->call_trigger('SHIPPING_DELETE', $user);
1408  if ($result < 0) {
1409  $error++;
1410  }
1411  // End call triggers
1412  }
1413 
1414  // Stock control
1415  if (!$error && $conf->stock->enabled &&
1416  (($conf->global->STOCK_CALCULATE_ON_SHIPMENT && $this->statut > self::STATUS_DRAFT) ||
1417  ($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE && $this->statut == self::STATUS_CLOSED && $also_update_stock))) {
1418  require_once DOL_DOCUMENT_ROOT."/product/stock/class/mouvementstock.class.php";
1419 
1420  $langs->load("agenda");
1421 
1422  // we try deletion of batch line even if module batch not enabled in case of the module were enabled and disabled previously
1423  $shipmentlinebatch = new ExpeditionLineBatch($this->db);
1424 
1425  // Loop on each product line to add a stock movement
1426  $sql = "SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot, ed.rowid as expeditiondet_id";
1427  $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd,";
1428  $sql .= " ".MAIN_DB_PREFIX."expeditiondet as ed";
1429  $sql .= " WHERE ed.fk_expedition = ".((int) $this->id);
1430  $sql .= " AND cd.rowid = ed.fk_origin_line";
1431 
1432  dol_syslog(get_class($this)."::delete select details", LOG_DEBUG);
1433  $resql = $this->db->query($sql);
1434  if ($resql) {
1435  $cpt = $this->db->num_rows($resql);
1436  for ($i = 0; $i < $cpt; $i++) {
1437  dol_syslog(get_class($this)."::delete movement index ".$i);
1438  $obj = $this->db->fetch_object($resql);
1439 
1440  $mouvS = new MouvementStock($this->db);
1441  // we do not log origin because it will be deleted
1442  $mouvS->origin = null;
1443  // get lot/serial
1444  $lotArray = $shipmentlinebatch->fetchAll($obj->expeditiondet_id);
1445  if (!is_array($lotArray)) {
1446  $error++; $this->errors[] = "Error ".$this->db->lasterror();
1447  }
1448  if (empty($lotArray)) {
1449  // no lot/serial
1450  // We increment stock of product (and sub-products)
1451  // We use warehouse selected for each line
1452  $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, 0, $langs->trans("ShipmentDeletedInDolibarr", $this->ref)); // Price is set to 0, because we don't want to see WAP changed
1453  if ($result < 0) {
1454  $error++;
1455  $this->errors = array_merge($this->errors, $mouvS->errors);
1456  break;
1457  }
1458  } else {
1459  // We increment stock of batches
1460  // We use warehouse selected for each line
1461  foreach ($lotArray as $lot) {
1462  $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $lot->qty, 0, $langs->trans("ShipmentDeletedInDolibarr", $this->ref), $lot->eatby, $lot->sellby, $lot->batch); // Price is set to 0, because we don't want to see WAP changed
1463  if ($result < 0) {
1464  $error++;
1465  $this->errors = array_merge($this->errors, $mouvS->errors);
1466  break;
1467  }
1468  }
1469  if ($error) {
1470  break; // break for loop incase of error
1471  }
1472  }
1473  }
1474  } else {
1475  $error++; $this->errors[] = "Error ".$this->db->lasterror();
1476  }
1477  }
1478 
1479  // delete batch expedition line
1480  if (!$error) {
1481  $shipmentlinebatch = new ExpeditionLineBatch($this->db);
1482  if ($shipmentlinebatch->deleteFromShipment($this->id) < 0) {
1483  $error++; $this->errors[] = "Error ".$this->db->lasterror();
1484  }
1485  }
1486 
1487  if (!$error) {
1488  $main = MAIN_DB_PREFIX.'expeditiondet';
1489  $ef = $main."_extrafields";
1490  $sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_expedition = ".((int) $this->id).")";
1491 
1492  $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet";
1493  $sql .= " WHERE fk_expedition = ".((int) $this->id);
1494 
1495  if ($this->db->query($sqlef) && $this->db->query($sql)) {
1496  // Delete linked object
1497  $res = $this->deleteObjectLinked();
1498  if ($res < 0) {
1499  $error++;
1500  }
1501 
1502  // delete extrafields
1503  $res = $this->deleteExtraFields();
1504  if ($res < 0) {
1505  $error++;
1506  }
1507 
1508  if (!$error) {
1509  $sql = "DELETE FROM ".MAIN_DB_PREFIX."expedition";
1510  $sql .= " WHERE rowid = ".((int) $this->id);
1511 
1512  if ($this->db->query($sql)) {
1513  if (!empty($this->origin) && $this->origin_id > 0) {
1514  $this->fetch_origin();
1515  $origin = $this->origin;
1516  if ($this->$origin->statut == Commande::STATUS_SHIPMENTONPROCESS) { // If order source of shipment is "shipment in progress"
1517  // Check if there is no more shipment. If not, we can move back status of order to "validated" instead of "shipment in progress"
1518  $this->$origin->loadExpeditions();
1519  //var_dump($this->$origin->expeditions);exit;
1520  if (count($this->$origin->expeditions) <= 0) {
1521  $this->$origin->setStatut(Commande::STATUS_VALIDATED);
1522  }
1523  }
1524  }
1525 
1526  if (!$error) {
1527  $this->db->commit();
1528 
1529  // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
1530  $this->deleteEcmFiles();
1531 
1532  // We delete PDFs
1533  $ref = dol_sanitizeFileName($this->ref);
1534  if (!empty($conf->expedition->dir_output)) {
1535  $dir = $conf->expedition->dir_output.'/sending/'.$ref;
1536  $file = $dir.'/'.$ref.'.pdf';
1537  if (file_exists($file)) {
1538  if (!dol_delete_file($file)) {
1539  return 0;
1540  }
1541  }
1542  if (file_exists($dir)) {
1543  if (!dol_delete_dir_recursive($dir)) {
1544  $this->error = $langs->trans("ErrorCanNotDeleteDir", $dir);
1545  return 0;
1546  }
1547  }
1548  }
1549 
1550  return 1;
1551  } else {
1552  $this->db->rollback();
1553  return -1;
1554  }
1555  } else {
1556  $this->error = $this->db->lasterror()." - sql=$sql";
1557  $this->db->rollback();
1558  return -3;
1559  }
1560  } else {
1561  $this->error = $this->db->lasterror()." - sql=$sql";
1562  $this->db->rollback();
1563  return -2;
1564  }
1565  } else {
1566  $this->error = $this->db->lasterror()." - sql=$sql";
1567  $this->db->rollback();
1568  return -1;
1569  }
1570  } else {
1571  $this->db->rollback();
1572  return -1;
1573  }
1574  }
1575 
1576  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1582  public function fetch_lines()
1583  {
1584  // phpcs:enable
1585  global $conf, $mysoc;
1586 
1587  $this->lines = array();
1588 
1589  // NOTE: This fetch_lines is special because it groups all lines with the same origin_line_id into one line.
1590  // TODO: See if we can restore a common fetch_lines (one line = one record)
1591 
1592  $sql = "SELECT cd.rowid, cd.fk_product, cd.label as custom_label, cd.description, cd.qty as qty_asked, cd.product_type, cd.fk_unit";
1593  $sql .= ", cd.total_ht, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.total_tva";
1594  $sql .= ", cd.vat_src_code, cd.tva_tx, cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.info_bits, cd.price, cd.subprice, cd.remise_percent,cd.buy_price_ht as pa_ht";
1595  $sql .= ", cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc, cd.rang";
1596  $sql .= ", ed.rowid as line_id, ed.qty as qty_shipped, ed.fk_origin_line, ed.fk_entrepot";
1597  $sql .= ", p.ref as product_ref, p.label as product_label, p.fk_product_type";
1598  $sql .= ", p.weight, p.weight_units, p.length, p.length_units, p.surface, p.surface_units, p.volume, p.volume_units, p.tosell as product_tosell, p.tobuy as product_tobuy, p.tobatch as product_tobatch";
1599  $sql .= " FROM ".MAIN_DB_PREFIX."expeditiondet as ed, ".MAIN_DB_PREFIX."commandedet as cd";
1600  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = cd.fk_product";
1601  $sql .= " WHERE ed.fk_expedition = ".((int) $this->id);
1602  $sql .= " AND ed.fk_origin_line = cd.rowid";
1603  $sql .= " ORDER BY cd.rang, ed.fk_origin_line"; // We need after a break on fk_origin_line but when there is no break on fk_origin_line, cd.rang is same so we can add it as first order criteria.
1604 
1605  dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
1606  $resql = $this->db->query($sql);
1607  if ($resql) {
1608  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1609 
1610  $num = $this->db->num_rows($resql);
1611  $i = 0;
1612  $lineindex = 0;
1613  $originline = 0;
1614 
1615  $this->total_ht = 0;
1616  $this->total_tva = 0;
1617  $this->total_ttc = 0;
1618  $this->total_localtax1 = 0;
1619  $this->total_localtax2 = 0;
1620 
1621  $shipmentlinebatch = new ExpeditionLineBatch($this->db);
1622 
1623  while ($i < $num) {
1624  $obj = $this->db->fetch_object($resql);
1625 
1626  if ($originline > 0 && $originline == $obj->fk_origin_line) {
1627  $line->entrepot_id = 0; // entrepod_id in details_entrepot
1628  $line->qty_shipped += $obj->qty_shipped;
1629  } else {
1630  $line = new ExpeditionLigne($this->db);
1631  $line->entrepot_id = $obj->fk_entrepot; // this is a property of a shipment line
1632  $line->qty_shipped = $obj->qty_shipped; // this is a property of a shipment line
1633  }
1634 
1635  $detail_entrepot = new stdClass();
1636  $detail_entrepot->entrepot_id = $obj->fk_entrepot;
1637  $detail_entrepot->qty_shipped = $obj->qty_shipped;
1638  $detail_entrepot->line_id = $obj->line_id;
1639  $line->details_entrepot[] = $detail_entrepot;
1640 
1641  $line->line_id = $obj->line_id;
1642  $line->rowid = $obj->line_id; // TODO deprecated
1643  $line->id = $obj->line_id;
1644 
1645  $line->fk_origin = 'orderline';
1646  $line->fk_origin_line = $obj->fk_origin_line;
1647  $line->origin_line_id = $obj->fk_origin_line; // TODO deprecated
1648 
1649  $line->fk_expedition = $this->id; // id of parent
1650 
1651  $line->product_type = $obj->product_type;
1652  $line->fk_product = $obj->fk_product;
1653  $line->fk_product_type = $obj->fk_product_type;
1654  $line->ref = $obj->product_ref; // TODO deprecated
1655  $line->product_ref = $obj->product_ref;
1656  $line->product_label = $obj->product_label;
1657  $line->libelle = $obj->product_label; // TODO deprecated
1658  $line->product_tosell = $obj->product_tosell;
1659  $line->product_tobuy = $obj->product_tobuy;
1660  $line->product_tobatch = $obj->product_tobatch;
1661  $line->label = $obj->custom_label;
1662  $line->description = $obj->description;
1663  $line->qty_asked = $obj->qty_asked;
1664  $line->rang = $obj->rang;
1665  $line->weight = $obj->weight;
1666  $line->weight_units = $obj->weight_units;
1667  $line->length = $obj->length;
1668  $line->length_units = $obj->length_units;
1669  $line->surface = $obj->surface;
1670  $line->surface_units = $obj->surface_units;
1671  $line->volume = $obj->volume;
1672  $line->volume_units = $obj->volume_units;
1673  $line->fk_unit = $obj->fk_unit;
1674 
1675  $line->pa_ht = $obj->pa_ht;
1676 
1677  // Local taxes
1678  $localtax_array = array(0=>$obj->localtax1_type, 1=>$obj->localtax1_tx, 2=>$obj->localtax2_type, 3=>$obj->localtax2_tx);
1679  $localtax1_tx = get_localtax($obj->tva_tx, 1, $this->thirdparty);
1680  $localtax2_tx = get_localtax($obj->tva_tx, 2, $this->thirdparty);
1681 
1682  // For invoicing
1683  $tabprice = calcul_price_total($obj->qty_shipped, $obj->subprice, $obj->remise_percent, $obj->tva_tx, $localtax1_tx, $localtax2_tx, 0, 'HT', $obj->info_bits, $obj->fk_product_type, $mysoc, $localtax_array); // We force type to 0
1684  $line->desc = $obj->description; // We need ->desc because some code into CommonObject use desc (property defined for other elements)
1685  $line->qty = $line->qty_shipped;
1686  $line->total_ht = $tabprice[0];
1687  $line->total_localtax1 = $tabprice[9];
1688  $line->total_localtax2 = $tabprice[10];
1689  $line->total_ttc = $tabprice[2];
1690  $line->total_tva = $tabprice[1];
1691  $line->vat_src_code = $obj->vat_src_code;
1692  $line->tva_tx = $obj->tva_tx;
1693  $line->localtax1_tx = $obj->localtax1_tx;
1694  $line->localtax2_tx = $obj->localtax2_tx;
1695  $line->info_bits = $obj->info_bits;
1696  $line->price = $obj->price;
1697  $line->subprice = $obj->subprice;
1698  $line->remise_percent = $obj->remise_percent;
1699 
1700  $this->total_ht += $tabprice[0];
1701  $this->total_tva += $tabprice[1];
1702  $this->total_ttc += $tabprice[2];
1703  $this->total_localtax1 += $tabprice[9];
1704  $this->total_localtax2 += $tabprice[10];
1705 
1706  // Multicurrency
1707  $this->fk_multicurrency = $obj->fk_multicurrency;
1708  $this->multicurrency_code = $obj->multicurrency_code;
1709  $this->multicurrency_subprice = $obj->multicurrency_subprice;
1710  $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1711  $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1712  $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1713 
1714  if ($originline != $obj->fk_origin_line) {
1715  $line->detail_batch = array();
1716  }
1717 
1718  // Detail of batch
1719  if (!empty($conf->productbatch->enabled) && $obj->line_id > 0 && $obj->product_tobatch > 0) {
1720  $newdetailbatch = $shipmentlinebatch->fetchAll($obj->line_id, $obj->fk_product);
1721 
1722  if (is_array($newdetailbatch)) {
1723  if ($originline != $obj->fk_origin_line) {
1724  $line->detail_batch = $newdetailbatch;
1725  } else {
1726  $line->detail_batch = array_merge($line->detail_batch, $newdetailbatch);
1727  }
1728  }
1729  }
1730 
1731  $line->fetch_optionals();
1732 
1733  if ($originline != $obj->fk_origin_line) {
1734  $this->lines[$lineindex] = $line;
1735  $lineindex++;
1736  } else {
1737  $line->total_ht += $tabprice[0];
1738  $line->total_localtax1 += $tabprice[9];
1739  $line->total_localtax2 += $tabprice[10];
1740  $line->total_ttc += $tabprice[2];
1741  $line->total_tva += $tabprice[1];
1742  }
1743 
1744  $i++;
1745  $originline = $obj->fk_origin_line;
1746  }
1747  $this->db->free($resql);
1748  return 1;
1749  } else {
1750  $this->error = $this->db->error();
1751  return -3;
1752  }
1753  }
1754 
1762  public function deleteline($user, $lineid)
1763  {
1764  global $user;
1765 
1766  if ($this->statut == self::STATUS_DRAFT) {
1767  $this->db->begin();
1768 
1769  $line = new ExpeditionLigne($this->db);
1770 
1771  // For triggers
1772  $line->fetch($lineid);
1773 
1774  if ($line->delete($user) > 0) {
1775  //$this->update_price(1);
1776 
1777  $this->db->commit();
1778  return 1;
1779  } else {
1780  $this->db->rollback();
1781  return -1;
1782  }
1783  } else {
1784  $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
1785  return -2;
1786  }
1787  }
1788 
1789 
1801  public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1)
1802  {
1803  global $langs, $conf, $hookmanager;
1804 
1805  $result = '';
1806  $label = '<u>'.$langs->trans("Shipment").'</u>';
1807  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1808  $label .= '<br><b>'.$langs->trans('RefCustomer').':</b> '.($this->ref_customer ? $this->ref_customer : $this->ref_client);
1809 
1810  $url = DOL_URL_ROOT.'/expedition/card.php?id='.$this->id;
1811 
1812  if ($short) {
1813  return $url;
1814  }
1815 
1816  if ($option !== 'nolink') {
1817  // Add param to save lastsearch_values or not
1818  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1819  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1820  $add_save_lastsearch_values = 1;
1821  }
1822  if ($add_save_lastsearch_values) {
1823  $url .= '&save_lastsearch_values=1';
1824  }
1825  }
1826 
1827  $linkclose = '';
1828  if (empty($notooltip)) {
1829  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1830  $label = $langs->trans("Shipment");
1831  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1832  }
1833  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1834  $linkclose .= ' class="classfortooltip"';
1835  }
1836 
1837  $linkstart = '<a href="'.$url.'"';
1838  $linkstart .= $linkclose.'>';
1839  $linkend = '</a>';
1840 
1841  $result .= $linkstart;
1842  if ($withpicto) {
1843  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1844  }
1845  if ($withpicto != 2) {
1846  $result .= $this->ref;
1847  }
1848  $result .= $linkend;
1849  global $action;
1850  $hookmanager->initHooks(array($this->element . 'dao'));
1851  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1852  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1853  if ($reshook > 0) {
1854  $result = $hookmanager->resPrint;
1855  } else {
1856  $result .= $hookmanager->resPrint;
1857  }
1858  return $result;
1859  }
1860 
1867  public function getLibStatut($mode = 0)
1868  {
1869  return $this->LibStatut($this->statut, $mode);
1870  }
1871 
1872  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1880  public function LibStatut($status, $mode)
1881  {
1882  // phpcs:enable
1883  global $langs;
1884 
1885  $labelStatus = $langs->transnoentitiesnoconv($this->statuts[$status]);
1886  $labelStatusShort = $langs->transnoentitiesnoconv($this->statutshorts[$status]);
1887 
1888  $statusType = 'status'.$status;
1889  if ($status == self::STATUS_VALIDATED) {
1890  $statusType = 'status4';
1891  }
1892  if ($status == self::STATUS_CLOSED) {
1893  $statusType = 'status6';
1894  }
1895  if ($status == self::STATUS_CANCELED) {
1896  $statusType = 'status9';
1897  }
1898 
1899  return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
1900  }
1901 
1909  public function initAsSpecimen()
1910  {
1911  global $langs;
1912 
1913  $now = dol_now();
1914 
1915  dol_syslog(get_class($this)."::initAsSpecimen");
1916 
1917  // Load array of products prodids
1918  $num_prods = 0;
1919  $prodids = array();
1920  $sql = "SELECT rowid";
1921  $sql .= " FROM ".MAIN_DB_PREFIX."product";
1922  $sql .= " WHERE entity IN (".getEntity('product').")";
1923  $resql = $this->db->query($sql);
1924  if ($resql) {
1925  $num_prods = $this->db->num_rows($resql);
1926  $i = 0;
1927  while ($i < $num_prods) {
1928  $i++;
1929  $row = $this->db->fetch_row($resql);
1930  $prodids[$i] = $row[0];
1931  }
1932  }
1933 
1934  $order = new Commande($this->db);
1935  $order->initAsSpecimen();
1936 
1937  // Initialise parametres
1938  $this->id = 0;
1939  $this->ref = 'SPECIMEN';
1940  $this->specimen = 1;
1941  $this->statut = self::STATUS_VALIDATED;
1942  $this->livraison_id = 0;
1943  $this->date = $now;
1944  $this->date_creation = $now;
1945  $this->date_valid = $now;
1946  $this->date_delivery = $now;
1947  $this->date_expedition = $now + 24 * 3600;
1948 
1949  $this->entrepot_id = 0;
1950  $this->fk_delivery_address = 0;
1951  $this->socid = 1;
1952 
1953  $this->commande_id = 0;
1954  $this->commande = $order;
1955 
1956  $this->origin_id = 1;
1957  $this->origin = 'commande';
1958 
1959  $this->note_private = 'Private note';
1960  $this->note_public = 'Public note';
1961 
1962  $nbp = 5;
1963  $xnbp = 0;
1964  while ($xnbp < $nbp) {
1965  $line = new ExpeditionLigne($this->db);
1966  $line->desc = $langs->trans("Description")." ".$xnbp;
1967  $line->libelle = $langs->trans("Description")." ".$xnbp; // deprecated
1968  $line->label = $langs->trans("Description")." ".$xnbp;
1969  $line->qty = 10;
1970  $line->qty_asked = 5;
1971  $line->qty_shipped = 4;
1972  $line->fk_product = $this->commande->lines[$xnbp]->fk_product;
1973 
1974  $this->lines[] = $line;
1975  $xnbp++;
1976  }
1977  }
1978 
1979  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1988  public function set_date_livraison($user, $delivery_date)
1989  {
1990  // phpcs:enable
1991  return $this->setDeliveryDate($user, $delivery_date);
1992  }
1993 
2001  public function setDeliveryDate($user, $delivery_date)
2002  {
2003  if ($user->rights->expedition->creer) {
2004  $sql = "UPDATE ".MAIN_DB_PREFIX."expedition";
2005  $sql .= " SET date_delivery = ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : 'null');
2006  $sql .= " WHERE rowid = ".((int) $this->id);
2007 
2008  dol_syslog(get_class($this)."::setDeliveryDate", LOG_DEBUG);
2009  $resql = $this->db->query($sql);
2010  if ($resql) {
2011  $this->date_delivery = $delivery_date;
2012  return 1;
2013  } else {
2014  $this->error = $this->db->error();
2015  return -1;
2016  }
2017  } else {
2018  return -2;
2019  }
2020  }
2021 
2022  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2028  public function fetch_delivery_methods()
2029  {
2030  // phpcs:enable
2031  global $langs;
2032  $this->meths = array();
2033 
2034  $sql = "SELECT em.rowid, em.code, em.libelle as label";
2035  $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as em";
2036  $sql .= " WHERE em.active = 1";
2037  $sql .= " ORDER BY em.libelle ASC";
2038 
2039  $resql = $this->db->query($sql);
2040  if ($resql) {
2041  while ($obj = $this->db->fetch_object($resql)) {
2042  $label = $langs->trans('SendingMethod'.$obj->code);
2043  $this->meths[$obj->rowid] = ($label != 'SendingMethod'.$obj->code ? $label : $obj->label);
2044  }
2045  }
2046  }
2047 
2048  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2055  public function list_delivery_methods($id = '')
2056  {
2057  // phpcs:enable
2058  global $langs;
2059 
2060  $this->listmeths = array();
2061  $i = 0;
2062 
2063  $sql = "SELECT em.rowid, em.code, em.libelle as label, em.description, em.tracking, em.active";
2064  $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as em";
2065  if ($id != '') {
2066  $sql .= " WHERE em.rowid=".((int) $id);
2067  }
2068 
2069  $resql = $this->db->query($sql);
2070  if ($resql) {
2071  while ($obj = $this->db->fetch_object($resql)) {
2072  $this->listmeths[$i]['rowid'] = $obj->rowid;
2073  $this->listmeths[$i]['code'] = $obj->code;
2074  $label = $langs->trans('SendingMethod'.$obj->code);
2075  $this->listmeths[$i]['libelle'] = ($label != 'SendingMethod'.$obj->code ? $label : $obj->label);
2076  $this->listmeths[$i]['description'] = $obj->description;
2077  $this->listmeths[$i]['tracking'] = $obj->tracking;
2078  $this->listmeths[$i]['active'] = $obj->active;
2079  $i++;
2080  }
2081  }
2082  }
2083 
2090  public function getUrlTrackingStatus($value = '')
2091  {
2092  if (!empty($this->shipping_method_id)) {
2093  $sql = "SELECT em.code, em.tracking";
2094  $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as em";
2095  $sql .= " WHERE em.rowid = ".((int) $this->shipping_method_id);
2096 
2097  $resql = $this->db->query($sql);
2098  if ($resql) {
2099  if ($obj = $this->db->fetch_object($resql)) {
2100  $tracking = $obj->tracking;
2101  }
2102  }
2103  }
2104 
2105  if (!empty($tracking) && !empty($value)) {
2106  $url = str_replace('{TRACKID}', $value, $tracking);
2107  $this->tracking_url = sprintf('<a target="_blank" rel="noopener noreferrer" href="%s">'.($value ? $value : 'url').'</a>', $url, $url);
2108  } else {
2109  $this->tracking_url = $value;
2110  }
2111  }
2112 
2118  public function setClosed()
2119  {
2120  global $conf, $langs, $user;
2121 
2122  $error = 0;
2123 
2124  // Protection. This avoid to move stock later when we should not
2125  if ($this->statut == self::STATUS_CLOSED) {
2126  return 0;
2127  }
2128 
2129  $this->db->begin();
2130 
2131  $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET fk_statut = ".self::STATUS_CLOSED;
2132  $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut > 0";
2133 
2134  $resql = $this->db->query($sql);
2135  if ($resql) {
2136  // Set order billed if 100% of order is shipped (qty in shipment lines match qty in order lines)
2137  if ($this->origin == 'commande' && $this->origin_id > 0) {
2138  $order = new Commande($this->db);
2139  $order->fetch($this->origin_id);
2140 
2141  $order->loadExpeditions(self::STATUS_CLOSED); // Fill $order->expeditions = array(orderlineid => qty)
2142 
2143  $shipments_match_order = 1;
2144  foreach ($order->lines as $line) {
2145  $lineid = $line->id;
2146  $qty = $line->qty;
2147  if (($line->product_type == 0 || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) && $order->expeditions[$lineid] != $qty) {
2148  $shipments_match_order = 0;
2149  $text = 'Qty for order line id '.$lineid.' is '.$qty.'. However in the shipments with status Expedition::STATUS_CLOSED='.self::STATUS_CLOSED.' we have qty = '.$order->expeditions[$lineid].', so we can t close order';
2150  dol_syslog($text);
2151  break;
2152  }
2153  }
2154  if ($shipments_match_order) {
2155  dol_syslog("Qty for the ".count($order->lines)." lines of the origin order is same than qty for lines in the shipment we close (shipments_match_order is true), with new status Expedition::STATUS_CLOSED=".self::STATUS_CLOSED.', so we close order');
2156  // We close the order
2157  $order->cloture($user); // Note this may also create an invoice if module workflow ask it
2158  }
2159  }
2160 
2161  $this->statut = self::STATUS_CLOSED; // Will be revert to STATUS_VALIDATED at end if there is a rollback
2162  $this->status = self::STATUS_CLOSED; // Will be revert to STATUS_VALIDATED at end if there is a rollback
2163 
2164  // If stock increment is done on closing
2165  if (!$error && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
2166  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
2167 
2168  $langs->load("agenda");
2169 
2170  // Loop on each product line to add a stock movement
2171  // TODO possibilite d'expedier a partir d'une propale ou autre origine ?
2172  $sql = "SELECT cd.fk_product, cd.subprice,";
2173  $sql .= " ed.rowid, ed.qty, ed.fk_entrepot,";
2174  $sql .= " e.ref,";
2175  $sql .= " edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
2176  $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd,";
2177  $sql .= " ".MAIN_DB_PREFIX."expeditiondet as ed";
2178  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
2179  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."expedition as e ON ed.fk_expedition = e.rowid";
2180  $sql .= " WHERE ed.fk_expedition = ".((int) $this->id);
2181  $sql .= " AND cd.rowid = ed.fk_origin_line";
2182 
2183  dol_syslog(get_class($this)."::valid select details", LOG_DEBUG);
2184  $resql = $this->db->query($sql);
2185  if ($resql) {
2186  $cpt = $this->db->num_rows($resql);
2187  for ($i = 0; $i < $cpt; $i++) {
2188  $obj = $this->db->fetch_object($resql);
2189  if (empty($obj->edbrowid)) {
2190  $qty = $obj->qty;
2191  } else {
2192  $qty = $obj->edbqty;
2193  }
2194  if ($qty <= 0) {
2195  continue;
2196  }
2197  dol_syslog(get_class($this)."::valid movement index ".$i." ed.rowid=".$obj->rowid." edb.rowid=".$obj->edbrowid);
2198 
2199  $mouvS = new MouvementStock($this->db);
2200  $mouvS->origin = &$this;
2201  $mouvS->setOrigin($this->element, $this->id);
2202 
2203  if (empty($obj->edbrowid)) {
2204  // line without batch detail
2205 
2206  // 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
2207  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans("ShipmentClassifyClosedInDolibarr", $obj->ref));
2208  if ($result < 0) {
2209  $this->error = $mouvS->error;
2210  $this->errors = $mouvS->errors;
2211  $error++;
2212  break;
2213  }
2214  } else {
2215  // line with batch detail
2216 
2217  // 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
2218  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans("ShipmentClassifyClosedInDolibarr", $obj->ref), '', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, $obj->fk_origin_stock);
2219  if ($result < 0) {
2220  $this->error = $mouvS->error;
2221  $this->errors = $mouvS->errors;
2222  $error++;
2223  break;
2224  }
2225  }
2226  }
2227  } else {
2228  $this->error = $this->db->lasterror();
2229  $error++;
2230  }
2231  }
2232 
2233  // Call trigger
2234  if (!$error) {
2235  $result = $this->call_trigger('SHIPPING_CLOSED', $user);
2236  if ($result < 0) {
2237  $error++;
2238  }
2239  }
2240  } else {
2241  dol_print_error($this->db);
2242  $error++;
2243  }
2244 
2245  if (!$error) {
2246  $this->db->commit();
2247  return 1;
2248  } else {
2249  $this->statut = self::STATUS_VALIDATED;
2250  $this->status = self::STATUS_VALIDATED;
2251 
2252  $this->db->rollback();
2253  return -1;
2254  }
2255  }
2256 
2257  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2265  public function set_billed()
2266  {
2267  // phpcs:enable
2268  dol_syslog(get_class($this)."::set_billed is deprecated, use setBilled instead", LOG_NOTICE);
2269  return $this->setBilled();
2270  }
2271 
2277  public function setBilled()
2278  {
2279  global $user;
2280  $error = 0;
2281 
2282  $this->db->begin();
2283 
2284  $sql = 'UPDATE '.MAIN_DB_PREFIX.'expedition SET fk_statut=2, billed=1'; // TODO Update only billed
2285  $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut > 0';
2286 
2287  $resql = $this->db->query($sql);
2288  if ($resql) {
2289  $this->statut = self::STATUS_CLOSED;
2290  $this->billed = 1;
2291 
2292  // Call trigger
2293  $result = $this->call_trigger('SHIPPING_BILLED', $user);
2294  if ($result < 0) {
2295  $error++;
2296  }
2297  } else {
2298  $error++;
2299  $this->errors[] = $this->db->lasterror;
2300  }
2301 
2302  if (empty($error)) {
2303  $this->db->commit();
2304  return 1;
2305  } else {
2306  $this->statut = self::STATUS_VALIDATED;
2307  $this->billed = 0;
2308  $this->db->rollback();
2309  return -1;
2310  }
2311  }
2312 
2318  public function reOpen()
2319  {
2320  global $conf, $langs, $user;
2321 
2322  $error = 0;
2323 
2324  // Protection. This avoid to move stock later when we should not
2325  if ($this->statut == self::STATUS_VALIDATED) {
2326  return 0;
2327  }
2328 
2329  $this->db->begin();
2330 
2331  $oldbilled = $this->billed;
2332 
2333  $sql = 'UPDATE '.MAIN_DB_PREFIX.'expedition SET fk_statut=1';
2334  $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut > 0';
2335 
2336  $resql = $this->db->query($sql);
2337  if ($resql) {
2338  $this->statut = self::STATUS_VALIDATED;
2339  $this->billed = 0;
2340 
2341  // If stock increment is done on closing
2342  if (!$error && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
2343  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
2344 
2345  $langs->load("agenda");
2346 
2347  // Loop on each product line to add a stock movement
2348  // TODO possibilite d'expedier a partir d'une propale ou autre origine
2349  $sql = "SELECT cd.fk_product, cd.subprice,";
2350  $sql .= " ed.rowid, ed.qty, ed.fk_entrepot,";
2351  $sql .= " edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
2352  $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd,";
2353  $sql .= " ".MAIN_DB_PREFIX."expeditiondet as ed";
2354  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
2355  $sql .= " WHERE ed.fk_expedition = ".((int) $this->id);
2356  $sql .= " AND cd.rowid = ed.fk_origin_line";
2357 
2358  dol_syslog(get_class($this)."::valid select details", LOG_DEBUG);
2359  $resql = $this->db->query($sql);
2360  if ($resql) {
2361  $cpt = $this->db->num_rows($resql);
2362  for ($i = 0; $i < $cpt; $i++) {
2363  $obj = $this->db->fetch_object($resql);
2364  if (empty($obj->edbrowid)) {
2365  $qty = $obj->qty;
2366  } else {
2367  $qty = $obj->edbqty;
2368  }
2369  if ($qty <= 0) {
2370  continue;
2371  }
2372  dol_syslog(get_class($this)."::reopen expedition movement index ".$i." ed.rowid=".$obj->rowid." edb.rowid=".$obj->edbrowid);
2373 
2374  //var_dump($this->lines[$i]);
2375  $mouvS = new MouvementStock($this->db);
2376  $mouvS->origin = &$this;
2377  $mouvS->setOrigin($this->element, $this->id);
2378 
2379  if (empty($obj->edbrowid)) {
2380  // line without batch detail
2381 
2382  // 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
2383  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, -$qty, $obj->subprice, $langs->trans("ShipmentUnClassifyCloseddInDolibarr", $numref));
2384  if ($result < 0) {
2385  $this->error = $mouvS->error;
2386  $this->errors = $mouvS->errors;
2387  $error++;
2388  break;
2389  }
2390  } else {
2391  // line with batch detail
2392 
2393  // 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
2394  $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, -$qty, $obj->subprice, $langs->trans("ShipmentUnClassifyCloseddInDolibarr", $numref), '', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, $obj->fk_origin_stock);
2395  if ($result < 0) {
2396  $this->error = $mouvS->error;
2397  $this->errors = $mouvS->errors;
2398  $error++;
2399  break;
2400  }
2401  }
2402  }
2403  } else {
2404  $this->error = $this->db->lasterror();
2405  $error++;
2406  }
2407  }
2408 
2409  if (!$error) {
2410  // Call trigger
2411  $result = $this->call_trigger('SHIPPING_REOPEN', $user);
2412  if ($result < 0) {
2413  $error++;
2414  }
2415  }
2416  } else {
2417  $error++;
2418  $this->errors[] = $this->db->lasterror();
2419  }
2420 
2421  if (!$error) {
2422  $this->db->commit();
2423  return 1;
2424  } else {
2425  $this->statut = self::STATUS_CLOSED;
2426  $this->billed = $oldbilled;
2427  $this->db->rollback();
2428  return -1;
2429  }
2430  }
2431 
2443  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
2444  {
2445  global $conf;
2446 
2447  $outputlangs->load("products");
2448 
2449  if (!dol_strlen($modele)) {
2450  $modele = 'rouget';
2451 
2452  if (!empty($this->model_pdf)) {
2453  $modele = $this->model_pdf;
2454  } elseif (!empty($this->modelpdf)) { // deprecated
2455  $modele = $this->modelpdf;
2456  } elseif (!empty($conf->global->EXPEDITION_ADDON_PDF)) {
2457  $modele = $conf->global->EXPEDITION_ADDON_PDF;
2458  }
2459  }
2460 
2461  $modelpath = "core/modules/expedition/doc/";
2462 
2463  $this->fetch_origin();
2464 
2465  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2466  }
2467 
2476  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
2477  {
2478  $tables = array(
2479  'expedition'
2480  );
2481 
2482  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
2483  }
2484 }
2485 
2486 
2491 {
2495  public $element = 'expeditiondet';
2496 
2500  public $table_element = 'expeditiondet';
2501 
2507 
2511  public $fk_origin_line;
2512 
2516  public $fk_expedition;
2517 
2521  public $db;
2522 
2526  public $qty;
2527 
2531  public $qty_shipped;
2532 
2536  public $fk_product;
2537  public $detail_batch;
2538 
2542  public $entrepot_id;
2543 
2544 
2548  public $qty_asked;
2549 
2554  public $ref;
2555 
2559  public $product_ref;
2560 
2565  public $libelle;
2566 
2570  public $product_label;
2571 
2577  public $desc;
2578 
2582  public $product_desc;
2583 
2588  public $product_type = 0;
2589 
2593  public $rang;
2594 
2598  public $weight;
2599  public $weight_units;
2600 
2604  public $length;
2605  public $length_units;
2606 
2610  public $surface;
2611  public $surface_units;
2612 
2616  public $volume;
2617  public $volume_units;
2618 
2619  // Invoicing
2620  public $remise_percent;
2621  public $tva_tx;
2622 
2626  public $total_ht;
2627 
2631  public $total_ttc;
2632 
2636  public $total_tva;
2637 
2641  public $total_localtax1;
2642 
2646  public $total_localtax2;
2647 
2648 
2654  public function __construct($db)
2655  {
2656  $this->db = $db;
2657  }
2658 
2665  public function fetch($rowid)
2666  {
2667  $sql = 'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.fk_origin_line, ed.qty, ed.rang';
2668  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as ed';
2669  $sql .= ' WHERE ed.rowid = '.((int) $rowid);
2670  $result = $this->db->query($sql);
2671  if ($result) {
2672  $objp = $this->db->fetch_object($result);
2673  $this->id = $objp->rowid;
2674  $this->fk_expedition = $objp->fk_expedition;
2675  $this->entrepot_id = $objp->fk_entrepot;
2676  $this->fk_origin_line = $objp->fk_origin_line;
2677  $this->qty = $objp->qty;
2678  $this->rang = $objp->rang;
2679 
2680  $this->db->free($result);
2681 
2682  return 1;
2683  } else {
2684  $this->errors[] = $this->db->lasterror();
2685  $this->error = $this->db->lasterror();
2686  return -1;
2687  }
2688  }
2689 
2697  public function insert($user, $notrigger = 0)
2698  {
2699  global $langs, $conf;
2700 
2701  $error = 0;
2702 
2703  // Check parameters
2704  if (empty($this->fk_expedition) || empty($this->fk_origin_line) || !is_numeric($this->qty)) {
2705  $this->error = 'ErrorMandatoryParametersNotProvided';
2706  return -1;
2707  }
2708 
2709  $this->db->begin();
2710 
2711  if (empty($this->rang)) {
2712  $this->rang = 0;
2713  }
2714 
2715  // Rank to use
2716  $ranktouse = $this->rang;
2717  if ($ranktouse == -1) {
2718  $rangmax = $this->line_max($this->fk_expedition);
2719  $ranktouse = $rangmax + 1;
2720  }
2721 
2722  $sql = "INSERT INTO ".MAIN_DB_PREFIX."expeditiondet (";
2723  $sql .= "fk_expedition";
2724  $sql .= ", fk_entrepot";
2725  $sql .= ", fk_origin_line";
2726  $sql .= ", qty";
2727  $sql .= ", rang";
2728  $sql .= ") VALUES (";
2729  $sql .= $this->fk_expedition;
2730  $sql .= ", ".(empty($this->entrepot_id) ? 'NULL' : $this->entrepot_id);
2731  $sql .= ", ".((int) $this->fk_origin_line);
2732  $sql .= ", ".price2num($this->qty, 'MS');
2733  $sql .= ", ".((int) $ranktouse);
2734  $sql .= ")";
2735 
2736  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2737  $resql = $this->db->query($sql);
2738  if ($resql) {
2739  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expeditiondet");
2740 
2741  if (!$error) {
2742  $result = $this->insertExtraFields();
2743  if ($result < 0) {
2744  $error++;
2745  }
2746  }
2747 
2748  if (!$error && !$notrigger) {
2749  // Call trigger
2750  $result = $this->call_trigger('LINESHIPPING_INSERT', $user);
2751  if ($result < 0) {
2752  $error++;
2753  }
2754  // End call triggers
2755  }
2756 
2757  if ($error) {
2758  foreach ($this->errors as $errmsg) {
2759  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
2760  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2761  }
2762  }
2763  } else {
2764  $error++;
2765  }
2766 
2767  if ($error) {
2768  $this->db->rollback();
2769  return -1;
2770  } else {
2771  $this->db->commit();
2772  return $this->id;
2773  }
2774  }
2775 
2783  public function delete($user = null, $notrigger = 0)
2784  {
2785  global $conf;
2786 
2787  $error = 0;
2788 
2789  $this->db->begin();
2790 
2791  // delete batch expedition line
2792  if (isModEnabled('productbatch')) {
2793  $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch";
2794  $sql .= " WHERE fk_expeditiondet = ".((int) $this->id);
2795 
2796  if (!$this->db->query($sql)) {
2797  $this->errors[] = $this->db->lasterror()." - sql=$sql";
2798  $error++;
2799  }
2800  }
2801 
2802  $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet";
2803  $sql .= " WHERE rowid = ".((int) $this->id);
2804 
2805  if (!$error && $this->db->query($sql)) {
2806  // Remove extrafields
2807  if (!$error) {
2808  $result = $this->deleteExtraFields();
2809  if ($result < 0) {
2810  $this->errors[] = $this->error;
2811  $error++;
2812  }
2813  }
2814  if (!$error && !$notrigger) {
2815  // Call trigger
2816  $result = $this->call_trigger('LINESHIPPING_DELETE', $user);
2817  if ($result < 0) {
2818  $this->errors[] = $this->error;
2819  $error++;
2820  }
2821  // End call triggers
2822  }
2823  } else {
2824  $this->errors[] = $this->db->lasterror()." - sql=$sql";
2825  $error++;
2826  }
2827 
2828  if (!$error) {
2829  $this->db->commit();
2830  return 1;
2831  } else {
2832  foreach ($this->errors as $errmsg) {
2833  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
2834  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2835  }
2836  $this->db->rollback();
2837  return -1 * $error;
2838  }
2839  }
2840 
2848  public function update($user = null, $notrigger = 0)
2849  {
2850  global $conf;
2851 
2852  $error = 0;
2853 
2854  dol_syslog(get_class($this)."::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty");
2855 
2856  $this->db->begin();
2857 
2858  // Clean parameters
2859  if (empty($this->qty)) {
2860  $this->qty = 0;
2861  }
2862  $qty = price2num($this->qty);
2863  $remainingQty = 0;
2864  $batch = null;
2865  $batch_id = null;
2866  $expedition_batch_id = null;
2867  if (is_array($this->detail_batch)) { // array of ExpeditionLineBatch
2868  if (count($this->detail_batch) > 1) {
2869  dol_syslog(get_class($this).'::update only possible for one batch', LOG_ERR);
2870  $this->errors[] = 'ErrorBadParameters';
2871  $error++;
2872  } else {
2873  $batch = $this->detail_batch[0]->batch;
2874  $batch_id = $this->detail_batch[0]->fk_origin_stock;
2875  $expedition_batch_id = $this->detail_batch[0]->id;
2876  if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id) {
2877  dol_syslog(get_class($this).'::update only possible for batch of same warehouse', LOG_ERR);
2878  $this->errors[] = 'ErrorBadParameters';
2879  $error++;
2880  }
2881  $qty = price2num($this->detail_batch[0]->qty);
2882  }
2883  } elseif (!empty($this->detail_batch)) {
2884  $batch = $this->detail_batch->batch;
2885  $batch_id = $this->detail_batch->fk_origin_stock;
2886  $expedition_batch_id = $this->detail_batch->id;
2887  if ($this->entrepot_id != $this->detail_batch->entrepot_id) {
2888  dol_syslog(get_class($this).'::update only possible for batch of same warehouse', LOG_ERR);
2889  $this->errors[] = 'ErrorBadParameters';
2890  $error++;
2891  }
2892  $qty = price2num($this->detail_batch->qty);
2893  }
2894 
2895  // check parameters
2896  if (!isset($this->id) || !isset($this->entrepot_id)) {
2897  dol_syslog(get_class($this).'::update missing line id and/or warehouse id', LOG_ERR);
2898  $this->errors[] = 'ErrorMandatoryParametersNotProvided';
2899  $error++;
2900  return -1;
2901  }
2902 
2903  // update lot
2904 
2905  if (!empty($batch) && $conf->productbatch->enabled) {
2906  dol_syslog(get_class($this)."::update expedition batch id=$expedition_batch_id, batch_id=$batch_id, batch=$batch");
2907 
2908  if (empty($batch_id) || empty($this->fk_product)) {
2909  dol_syslog(get_class($this).'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR);
2910  $this->errors[] = 'ErrorMandatoryParametersNotProvided';
2911  $error++;
2912  }
2913 
2914  // fetch remaining lot qty
2915  $shipmentlinebatch = new ExpeditionLineBatch($this->db);
2916 
2917  if (!$error && ($lotArray = $shipmentlinebatch->fetchAll($this->id)) < 0) {
2918  $this->errors[] = $this->db->lasterror()." - ExpeditionLineBatch::fetchAll";
2919  $error++;
2920  } else {
2921  // caculate new total line qty
2922  foreach ($lotArray as $lot) {
2923  if ($expedition_batch_id != $lot->id) {
2924  $remainingQty += $lot->qty;
2925  }
2926  }
2927  $qty += $remainingQty;
2928 
2929  //fetch lot details
2930 
2931  // fetch from product_lot
2932  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
2933  $lot = new Productlot($this->db);
2934  if ($lot->fetch(0, $this->fk_product, $batch) < 0) {
2935  $this->errors[] = $lot->errors;
2936  $error++;
2937  }
2938  if (!$error && !empty($expedition_batch_id)) {
2939  // delete lot expedition line
2940  $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch";
2941  $sql .= " WHERE fk_expeditiondet = ".((int) $this->id);
2942  $sql .= " AND rowid = ".((int) $expedition_batch_id);
2943 
2944  if (!$this->db->query($sql)) {
2945  $this->errors[] = $this->db->lasterror()." - sql=$sql";
2946  $error++;
2947  }
2948  }
2949  if (!$error && $this->detail_batch->qty > 0) {
2950  // create lot expedition line
2951  if (isset($lot->id)) {
2952  $shipmentLot = new ExpeditionLineBatch($this->db);
2953  $shipmentLot->batch = $lot->batch;
2954  $shipmentLot->eatby = $lot->eatby;
2955  $shipmentLot->sellby = $lot->sellby;
2956  $shipmentLot->entrepot_id = $this->detail_batch->entrepot_id;
2957  $shipmentLot->qty = $this->detail_batch->qty;
2958  $shipmentLot->fk_origin_stock = $batch_id;
2959  if ($shipmentLot->create($this->id) < 0) {
2960  $this->errors[] = $shipmentLot->errors;
2961  $error++;
2962  }
2963  }
2964  }
2965  }
2966  }
2967  if (!$error) {
2968  // update line
2969  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
2970  $sql .= " fk_entrepot = ".($this->entrepot_id > 0 ? $this->entrepot_id : 'null');
2971  $sql .= " , qty = ".((float) price2num($qty, 'MS'));
2972  $sql .= " WHERE rowid = ".((int) $this->id);
2973 
2974  if (!$this->db->query($sql)) {
2975  $this->errors[] = $this->db->lasterror()." - sql=$sql";
2976  $error++;
2977  }
2978  }
2979 
2980  if (!$error) {
2981  if (!$error) {
2982  $result = $this->insertExtraFields();
2983  if ($result < 0) {
2984  $this->errors[] = $this->error;
2985  $error++;
2986  }
2987  }
2988  }
2989 
2990  if (!$error && !$notrigger) {
2991  // Call trigger
2992  $result = $this->call_trigger('LINESHIPPING_MODIFY', $user);
2993  if ($result < 0) {
2994  $this->errors[] = $this->error;
2995  $error++;
2996  }
2997  // End call triggers
2998  }
2999  if (!$error) {
3000  $this->db->commit();
3001  return 1;
3002  } else {
3003  foreach ($this->errors as $errmsg) {
3004  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
3005  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
3006  }
3007  $this->db->rollback();
3008  return -1 * $error;
3009  }
3010  }
3011 }
Expedition\$date_expedition
$date_expedition
Definition: expedition.class.php:156
ExpeditionLigne\insert
insert($user, $notrigger=0)
Insert line into database.
Definition: expedition.class.php:2697
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
Expedition\setClosed
setClosed()
Classify the shipping as closed.
Definition: expedition.class.php:2118
db
$conf db
API class for accounts.
Definition: inc.php:41
Productbatch
Manage record for batch number management.
Definition: productbatch.class.php:31
Expedition\update
update($user=null, $notrigger=0)
Update database.
Definition: expedition.class.php:1056
Expedition\list_delivery_methods
list_delivery_methods($id='')
Fetch all deliveries method and return an array.
Definition: expedition.class.php:2055
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1212
Expedition
Class to manage shipments.
Definition: expedition.class.php:52
Expedition\addline_batch
addline_batch($dbatch, $array_options=0)
Add a shipment line with batch record.
Definition: expedition.class.php:991
Expedition\setDeliveryDate
setDeliveryDate($user, $delivery_date)
Set the planned delivery date.
Definition: expedition.class.php:2001
Expedition\__construct
__construct($db)
Constructor.
Definition: expedition.class.php:206
Expedition\reOpen
reOpen()
Classify the shipping as validated/opened.
Definition: expedition.class.php:2318
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:1383
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Expedition\replaceThirdparty
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: expedition.class.php:2476
CommonObject\fetchObjectLinked
fetchObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $clause='OR', $alsosametype=1, $orderby='sourcetype', $loadalsoobjects=1)
Fetch array of objects linked to current object (object of enabled modules only).
Definition: commonobject.class.php:3916
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:4830
ExpeditionLigne
Classe to manage lines of shipment.
Definition: expedition.class.php:2490
Expedition\STATUS_DRAFT
const STATUS_DRAFT
Draft status.
Definition: expedition.class.php:183
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
Expedition\create_line_batch
create_line_batch($line_ext, $array_options=0)
Create the detail of the expedition line.
Definition: expedition.class.php:475
Commande\STATUS_SHIPMENTONPROCESS
const STATUS_SHIPMENTONPROCESS
Shipment on process.
Definition: commande.class.php:389
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:4228
ref
$object ref
Definition: info.php:77
Expedition\addline
addline($entrepot_id, $id, $qty, $array_options=0)
Add an expedition line.
Definition: expedition.class.php:909
ExpeditionLineBatch
CRUD class for batch number management within shipment.
Definition: expeditionlinebatch.class.php:29
CommonObject\setStatut
setStatut($status, $elementId=null, $elementType='', $trigkey='', $fieldstatus='fk_statut')
Set status of an object.
Definition: commonobject.class.php:4364
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)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
Expedition\$date
$date
Definition: expedition.class.php:150
Expedition\deleteline
deleteline($user, $lineid)
Delete detail line.
Definition: expedition.class.php:1762
Expedition\set_billed
set_billed()
Classify the shipping as invoiced (used when WORKFLOW_BILL_ON_SHIPMENT is on)
Definition: expedition.class.php:2265
CommonObjectLine
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Definition: commonobjectline.class.php:32
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
Expedition\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
Definition: expedition.class.php:2443
Expedition\STATUS_CLOSED
const STATUS_CLOSED
Closed status.
Definition: expedition.class.php:193
CommonObject\fetch_origin
fetch_origin()
Read linked origin object.
Definition: commonobject.class.php:1929
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5647
Productlot
Class with list of lots and properties.
Definition: productlot.class.php:36
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:1736
Expedition\setBilled
setBilled()
Classify the shipping as invoiced (used when WORKFLOW_BILL_ON_SHIPMENT is on)
Definition: expedition.class.php:2277
Expedition\getUrlTrackingStatus
getUrlTrackingStatus($value='')
Forge an set tracking url.
Definition: expedition.class.php:2090
Commande\STATUS_VALIDATED
const STATUS_VALIDATED
Validated status.
Definition: commande.class.php:385
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:5406
calcul_price_total
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array='', $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:86
dol_delete_file
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
Definition: files.lib.php:1231
Expedition\valid
valid($user, $notrigger=0)
Validate object and update stock if option enabled.
Definition: expedition.class.php:664
OrderLine
Class to manage order lines.
Definition: commande.class.php:4146
Expedition\fetch_lines
fetch_lines()
Load lines.
Definition: expedition.class.php:1582
get_localtax
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
Definition: functions.lib.php:5823
Expedition\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: expedition.class.php:1909
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:6156
ExpeditionLigne\update
update($user=null, $notrigger=0)
Update a line in database.
Definition: expedition.class.php:2848
Expedition\create
create($user, $notrigger=0)
Create expedition en base.
Definition: expedition.class.php:282
ExpeditionLigne\fetch
fetch($rowid)
Load line expedition.
Definition: expedition.class.php:2665
Commande
Class to manage customers orders.
Definition: commande.class.php:46
CommonObject\deleteEcmFiles
deleteEcmFiles($mode=0)
Delete related files of object in database.
Definition: commonobject.class.php:9966
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8347
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1589
CommonObject\line_max
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
Definition: commonobject.class.php:3373
Expedition\getNomUrl
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $notooltip=0, $save_lastsearch_value=-1)
Return clicable link of object (with eventually picto)
Definition: expedition.class.php:1801
Expedition\LibStatut
LibStatut($status, $mode)
Return label of a status.
Definition: expedition.class.php:1880
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:6007
Expedition\fetch
fetch($id, $ref='', $ref_ext='', $notused='')
Get object and lines from database.
Definition: expedition.class.php:521
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3733
Expedition\cancel
cancel($notrigger=0, $also_update_stock=false)
Cancel shipment.
Definition: expedition.class.php:1196
Expedition\set_date_livraison
set_date_livraison($user, $delivery_date)
Set delivery date.
Definition: expedition.class.php:1988
Expedition\getNextNumRef
getNextNumRef($soc)
Return next expedition ref.
Definition: expedition.class.php:233
Expedition\create_delivery
create_delivery($user)
Create a delivery receipt from a shipment.
Definition: expedition.class.php:872
ExpeditionLigne\$libelle
$libelle
Definition: expedition.class.php:2565
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
Expedition\STATUS_CANCELED
const STATUS_CANCELED
Canceled status.
Definition: expedition.class.php:198
Expedition\STATUS_VALIDATED
const STATUS_VALIDATED
Validated status.
Definition: expedition.class.php:188
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6116
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:3818
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10323
ExpeditionLigne\__construct
__construct($db)
Constructor.
Definition: expedition.class.php:2654
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:4197
ExpeditionLigne\$ref
$ref
Definition: expedition.class.php:2554
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2831
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
Expedition\create_line
create_line($entrepot_id, $origin_line_id, $qty, $rang=0, $array_options=null)
Create a expedition line.
Definition: expedition.class.php:447
Expedition\getLibStatut
getLibStatut($mode=0)
Return status label.
Definition: expedition.class.php:1867
Delivery
Class to manage receptions.
Definition: delivery.class.php:45
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
user
$conf db user
Definition: repair.php:123
ExpeditionLigne\$origin_line_id
$origin_line_id
Definition: expedition.class.php:2506
Expedition\fetch_delivery_methods
fetch_delivery_methods()
Fetch deliveries method and return an array.
Definition: expedition.class.php:2028