dolibarr 22.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-2024 Ferran Marcet <fmarcet@2byte.es>
13 * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
14 * Copyright (C) 2018-2025 Frédéric France <frederic.france@free.fr>
15 * Copyright (C) 2020 Lenin Rivas <lenin@leninrivas.com>
16 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
17 * Copyright (C) 2024 William Mead <william.mead@manchenumerique.fr>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <https://www.gnu.org/licenses/>.
31 */
32
39require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
40require_once DOL_DOCUMENT_ROOT."/expedition/class/expeditionligne.class.php";
41require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
42if (isModEnabled("propal")) {
43 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
44}
45if (isModEnabled('order')) {
46 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
47}
48require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionlinebatch.class.php';
49require_once DOL_DOCUMENT_ROOT.'/core/class/commonsignedobject.class.php';
50require_once DOL_DOCUMENT_ROOT.'/subtotals/class/commonsubtotal.class.php';
51
58{
59 use CommonIncoterm;
60 use CommonSignedObject;
61 use CommonSubtotal;
62
66 public $element = "shipping";
67
71 public $fk_element = "fk_expedition";
72
76 public $table_element = "expedition";
77
81 public $table_element_line = "expeditiondet";
82
86 public $picto = 'dolly';
87
88
92 public $fields = array();
93
97 public $user_author_id;
98
102 public $fk_user_author;
103
107 public $fk_user_valid;
108
112 public $socid;
113
119 public $ref_client;
120
124 public $ref_customer;
125
129 public $entrepot_id;
130
134 public $tracking_number;
135
139 public $tracking_url;
143 public $billed;
144
148 public $trueWeight;
152 public $weight_units;
156 public $trueWidth;
160 public $width_units;
164 public $trueHeight;
168 public $height_units;
172 public $trueDepth;
176 public $depth_units;
180 public $trueSize;
181
185 public $livraison_id;
186
190 public $multicurrency_subprice;
191
195 public $size_units;
196
200 public $sizeH;
201
205 public $sizeS;
206
210 public $sizeW;
211
215 public $weight;
216
220 public $date_delivery;
221
227 public $date;
228
234 public $date_expedition;
235
240 public $date_shipping;
241
245 public $date_valid;
246
250 public $meths;
254 public $listmeths; // List of carriers
255
259 public $commande_id;
260
264 public $commande;
265
269 public $lines = array();
270
271 // Multicurrency
275 public $fk_multicurrency;
276
280 public $multicurrency_code;
284 public $multicurrency_tx;
288 public $multicurrency_total_ht;
292 public $multicurrency_total_tva;
296 public $multicurrency_total_ttc;
297
301 const STATUS_DRAFT = 0;
302
310
316 const STATUS_CLOSED = 2;
317
321 const STATUS_CANCELED = -1;
322
331
337 public function __construct($db)
338 {
339 global $conf;
340
341 $this->db = $db;
342
343 $this->ismultientitymanaged = 1;
344 $this->isextrafieldmanaged = 1;
345
346 // List of long language codes for status
347 $this->labelStatus = array();
348 $this->labelStatus[-1] = 'StatusSendingCanceled';
349 $this->labelStatus[0] = 'StatusSendingDraft';
350 $this->labelStatus[1] = 'StatusSendingValidated';
351 $this->labelStatus[2] = 'StatusSendingProcessed';
352
353 // List of short language codes for status
354 $this->labelStatusShort = array();
355 $this->labelStatusShort[-1] = 'StatusSendingCanceledShort';
356 $this->labelStatusShort[0] = 'StatusSendingDraftShort';
357 $this->labelStatusShort[1] = 'StatusSendingValidatedShort';
358 $this->labelStatusShort[2] = 'StatusSendingProcessedShort';
359 }
360
367 public function getNextNumRef($soc)
368 {
369 global $langs, $conf;
370 $langs->load("sendings");
371
372 if (getDolGlobalString('EXPEDITION_ADDON_NUMBER')) {
373 $mybool = false;
374
375 $file = getDolGlobalString('EXPEDITION_ADDON_NUMBER') . ".php";
376 $classname = getDolGlobalString('EXPEDITION_ADDON_NUMBER');
377
378 // Include file with class
379 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
380
381 foreach ($dirmodels as $reldir) {
382 $dir = dol_buildpath($reldir."core/modules/expedition/");
383
384 // Load file with numbering class (if found)
385 $mybool = ((bool) @include_once $dir.$file) || $mybool;
386 }
387
388 if (!$mybool) {
389 dol_print_error(null, "Failed to include file ".$file);
390 return '';
391 }
392
393 $obj = new $classname();
394 '@phan-var-force ModelNumRefExpedition $obj';
395 $numref = $obj->getNextValue($soc, $this);
396
397 if ($numref != "") {
398 return $numref;
399 } else {
400 dol_print_error($this->db, get_class($this)."::getNextNumRef ".$obj->error);
401 return "";
402 }
403 } else {
404 print $langs->trans("Error")." ".$langs->trans("Error_EXPEDITION_ADDON_NUMBER_NotDefined");
405 return "";
406 }
407 }
408
416 public function create($user, $notrigger = 0)
417 {
418 $now = dol_now();
419
420 require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
421 $error = 0;
422
423 // Clean parameters
424 $this->tracking_number = dol_sanitizeFileName($this->tracking_number);
425 if (empty($this->fk_project)) {
426 $this->fk_project = 0;
427 }
428 if (empty($this->date_creation)) {
429 $this->date_creation = $now;
430 }
431 if (empty($this->date_shipping) && !empty($this->date_expedition)) {
432 $this->date_shipping = $this->date_expedition;
433 }
434 $this->entity = setEntity($this);
435
436 $this->user = $user;
437
438 $this->db->begin();
439
440 $sql = "INSERT INTO ".MAIN_DB_PREFIX."expedition (";
441 $sql .= "ref";
442 $sql .= ", entity";
443 $sql .= ", ref_customer";
444 $sql .= ", ref_ext";
445 $sql .= ", date_creation";
446 $sql .= ", fk_user_author";
447 $sql .= ", date_expedition";
448 $sql .= ", date_delivery";
449 $sql .= ", fk_soc";
450 $sql .= ", fk_projet";
451 $sql .= ", fk_address";
452 $sql .= ", fk_shipping_method";
453 $sql .= ", tracking_number";
454 $sql .= ", weight";
455 $sql .= ", size";
456 $sql .= ", width";
457 $sql .= ", height";
458 $sql .= ", weight_units";
459 $sql .= ", size_units";
460 $sql .= ", note_private";
461 $sql .= ", note_public";
462 $sql .= ", model_pdf";
463 $sql .= ", fk_incoterms, location_incoterms";
464 $sql .= ", signed_status";
465 $sql .= ") VALUES (";
466 $sql .= "'(PROV)'";
467 $sql .= ", ".((int) $this->entity);
468 $sql .= ", ".($this->ref_customer ? "'".$this->db->escape($this->ref_customer)."'" : "null");
469 $sql .= ", ".($this->ref_ext ? "'".$this->db->escape($this->ref_ext)."'" : "null");
470 $sql .= ", '".$this->db->idate($this->date_creation)."'";
471 $sql .= ", ".((int) $user->id);
472 $sql .= ", ".($this->date_shipping > 0 ? "'".$this->db->idate($this->date_shipping)."'" : "null");
473 $sql .= ", ".($this->date_delivery > 0 ? "'".$this->db->idate($this->date_delivery)."'" : "null");
474 $sql .= ", ".($this->socid > 0 ? ((int) $this->socid) : "null");
475 $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : "null");
476 $sql .= ", ".($this->fk_delivery_address > 0 ? $this->fk_delivery_address : "null");
477 $sql .= ", ".($this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) : "null");
478 $sql .= ", '".$this->db->escape($this->tracking_number)."'";
479 $sql .= ", ".(is_numeric($this->weight) ? $this->weight : 'NULL');
480 $sql .= ", ".(is_numeric($this->sizeS) ? $this->sizeS : 'NULL'); // TODO Should use this->trueDepth
481 $sql .= ", ".(is_numeric($this->sizeW) ? $this->sizeW : 'NULL'); // TODO Should use this->trueWidth
482 $sql .= ", ".(is_numeric($this->sizeH) ? $this->sizeH : 'NULL'); // TODO Should use this->trueHeight
483 $sql .= ", ".($this->weight_units != '' ? (int) $this->weight_units : 'NULL');
484 $sql .= ", ".($this->size_units != '' ? (int) $this->size_units : 'NULL');
485 $sql .= ", ".(!empty($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null");
486 $sql .= ", ".(!empty($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null");
487 $sql .= ", ".(!empty($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null");
488 $sql .= ", ".(int) $this->fk_incoterms;
489 $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
490 $sql .= ", ".($this->signed_status);
491 $sql .= ")";
492
493 dol_syslog(get_class($this)."::create", LOG_DEBUG);
494 $resql = $this->db->query($sql);
495 if ($resql) {
496 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expedition");
497
498 $sql = "UPDATE ".MAIN_DB_PREFIX."expedition";
499 $sql .= " SET ref = '(PROV".$this->id.")'";
500 $sql .= " WHERE rowid = ".((int) $this->id);
501
502 dol_syslog(get_class($this)."::create", LOG_DEBUG);
503 if ($this->db->query($sql)) {
504 // Insert of lines
505 $num = count($this->lines);
506 $kits_list = array();
507 if (getDolGlobalInt('PRODUIT_SOUSPRODUITS')) {
508 for ($i = 0; $i < $num; $i++) {
509 $objectsrc = new OrderLine($this->db);
510 $objectsrc->fetch($this->lines[$i]->origin_line_id);
511 if ($this->lines[$i]->product_type == "9" && $objectsrc->special_code == SUBTOTALS_SPECIAL_CODE) {
512 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) {
513 $error++;
514 }
515 continue;
516 }
517 if (empty($this->lines[$i]->product_type) || getDolGlobalString('STOCK_SUPPORTS_SERVICES') || getDolGlobalString('SHIPMENT_SUPPORTS_SERVICES')) {
518 // virtual products
519 $line = $this->lines[$i];
520 if ($line->fk_product > 0) {
521 // We group kit lines by the fulfilled order line (fk_elementdet) and not by product id: when a same kit product
522 // is present on several distinct order lines, each order line must keep its own shipment line so the ordered
523 // qty stays consistent with the shipped qty (grouping by product would sum the shipped qty of all lines while
524 // keeping the ordered qty of a single line, resulting in a wrong/negative remaining qty to ship). Several
525 // warehouse pickings of the same order line still share the same fk_elementdet and are grouped together.
526 if (!isset($kits_list[$line->fk_elementdet])) {
527 if (!is_object($line->product)) {
528 $line_product = new Product($this->db);
529 $result = $line_product->fetch($line->fk_product, '', '', '', 1, 1, 1);
530 if ($result <= 0) {
531 $error++;
532 }
533 } else {
534 $line_product = $line->product;
535 }
536
537 // get all children of virtual product
538 $line_product->get_sousproduits_arbo();
539 $prods_arbo = $line_product->get_arbo_each_prod($line->qty);
540 if (count($prods_arbo) > 0) {
541 $kits_list[$line->fk_elementdet] = array(
542 'arbo' => $prods_arbo,
543 'total_qty' => $line->qty,
544 );
545 }
546 } else {
547 $kits_list[$line->fk_elementdet]['total_qty'] += $line->qty;
548 }
549 }
550 }
551 }
552 }
553 $kits_id_cached = array();
554 $sub_kits_id_cached = array();
555 $sub_parents_cached = array();
556 for ($i = 0; $i < $num; $i++) {
557 $line = $this->lines[$i];
558 if (empty($line->product_type) || getDolGlobalString('STOCK_SUPPORTS_SERVICES') || getDolGlobalString('SHIPMENT_SUPPORTS_SERVICES')) {
559 $line_id = 0;
560 if (!isset($kits_id_cached[$line->fk_elementdet])) {
561 if (!isset($line->detail_batch) || (isset($kits_list[$line->fk_elementdet]) && !getDolGlobalInt('PRODUIT_SOUSPRODUITS_ALSO_ENABLE_PARENT_STOCK_MOVE'))) { // no batch management or is kit
562 $qty = isset($kits_list[$line->fk_elementdet]) ? $kits_list[$line->fk_elementdet]['total_qty'] : $line->qty;
563 $warehouse_id = (isset($kits_list[$line->fk_elementdet]) && !getDolGlobalInt('PRODUIT_SOUSPRODUITS_ALSO_ENABLE_PARENT_STOCK_MOVE')) ? 0 : $line->entrepot_id;
564 $line_id = $this->create_line($warehouse_id, $line->origin_line_id, $qty, $line->rang, $line->array_options, 0, $line->fk_product);
565 if ($line_id <= 0) {
566 $error++;
567 }
568 if (isset($kits_list[$line->fk_elementdet])) $kits_id_cached[$line->fk_elementdet] = $line_id;
569 } else { // with batch management
570 if ($this->create_line_batch($line, $line->array_options) <= 0) {
571 $error++;
572 }
573 }
574 } else {
575 $line_id = $kits_id_cached[$line->fk_elementdet];
576 }
577
578 // virtual products
579 if (isset($kits_list[$line->fk_elementdet]) && !getDolGlobalInt('PRODUIT_SOUSPRODUITS_ALSO_ENABLE_PARENT_STOCK_MOVE')) {
580 $prods_arbo = $kits_list[$line->fk_elementdet]['arbo'];
581
582 // get all children of virtual product
583 $parent_line_id = $line_id; // parent line created
584 $level_last = 1;
585 $product_child_id = 0;
586 foreach ($prods_arbo as $index => $product_child_arr) {
587 // 'id' => Id product
588 // 'id_parent' => Id parent product
589 // 'ref' => Ref product
590 // 'nb' => Nb of units that compose parent product
591 // 'nb_total' => // Nb of units for all nb of product
592 // 'stock' => Stock
593 // 'stock_alert' => Stock alert
594 // 'label' => Label
595 // 'fullpath' => // Full path label
596 // 'type' =>
597 // 'desiredstock' => Desired stock
598 // 'level' => Level
599 // 'incdec' => Need to be incremented or decremented
600 // 'entity' => Entity
601 $product_child_level = (int) $product_child_arr['level'];
602 $product_child_incdec = !empty($product_child_arr['incdec']);
603
604 // detect new level
605 if ($product_child_level != $level_last) {
606 $parent_line_id = $line_id; // last line id
607 $parent_product_id = $product_child_id; // last line id
608 if (isset($sub_parents_cached[$line->fk_elementdet][$parent_product_id])) {
609 $parent_line_id = $sub_parents_cached[$line->fk_elementdet][$parent_product_id];
610 } else {
611 $sub_parents_cached[$line->fk_elementdet][$parent_product_id] = $parent_line_id;
612 }
613 }
614
615 // determine if it's a kit : check next level
616 $is_kit = false;
617 $next_level = $product_child_level;
618 $next_index = $index + 1;
619 if (isset($prods_arbo[$next_index])) {
620 $next_level = (int) $prods_arbo[$next_index]['level'];
621 }
622 if ($next_level > $product_child_level) {
623 $is_kit = true;
624 }
625
626 // determine quantity of sub-product
627 $product_child_id = (int) $product_child_arr['id'];
628 $product_child_qty = (float) $product_child_arr['nb_total']; // by default
629 $warehouse_id = $line->entrepot_id; // by default
630 if ($is_kit || !$product_child_incdec) {
631 if (!$product_child_incdec) {
632 $product_child_qty = 0;
633 }
634 $warehouse_id = 0; // no warehouse used for a kit or if stock is not managed (empty incdec)
635 }
636
637 // create line for a child of virtual product
638 if (!isset($sub_kits_id_cached[$line->fk_elementdet][$product_child_id]) || $warehouse_id > 0) {
639 $line_id = $this->create_line($warehouse_id, ($parent_line_id ? 0 : $line->origin_line_id), $product_child_qty, $line->rang, $line->array_options, $parent_line_id, $product_child_id);
640 if ($line_id <= 0) {
641 $error++;
642 dol_syslog(__METHOD__ . ' : ' . $this->errorsToString(), LOG_ERR);
643 break;
644 }
645
646 // if kit or not manage stock (empty incdec)
647 if (empty($warehouse_id)) {
648 $sub_kits_id_cached[$line->fk_elementdet][$product_child_id] = $line_id;
649 }
650 }
651
652 $level_last = $product_child_level;
653 }
654 }
655 }
656 }
657
658 if (!$error && $this->id && $this->origin_id) {
659 $ret = $this->add_object_linked();
660 if (!$ret) {
661 $error++;
662 }
663 }
664
665 if (!$error && $this->id && getDolGlobalInt('SHIPPING_USE_ITS_OWN_CONTACTS') && getDolGlobalString('MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin_type) && !empty($this->origin_id)) { // Get contact from origin object
666 $originforcontact = $this->origin_type;
667 $originidforcontact = $this->origin_id;
668
669 $sqlcontact = "SELECT ctc.code, ctc.source, ec.fk_socpeople FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as ctc";
670 $sqlcontact .= " WHERE element_id = ".((int) $originidforcontact)." AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact)."'";
671
672 $resqlcontact = $this->db->query($sqlcontact);
673 if ($resqlcontact) {
674 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
675 $this->add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source); // May failed because of duplicate key or because code of contact type does not exists for new object
676 }
677 } else {
678 dol_print_error($this->db);
679 }
680 }
681
682 // Actions on extra fields
683 if (!$error) {
684 $result = $this->insertExtraFields();
685 if ($result < 0) {
686 $error++;
687 }
688 }
689
690 if (!$error && !$notrigger) {
691 // Call trigger
692 $result = $this->call_trigger('SHIPPING_CREATE', $user);
693 if ($result < 0) {
694 $error++;
695 }
696 // End call triggers
697
698 if (!$error) {
699 $this->db->commit();
700 return $this->id;
701 } else {
702 foreach ($this->errors as $errmsg) {
703 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
704 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
705 }
706 $this->db->rollback();
707 return -1 * $error;
708 }
709 } else {
710 $error++;
711 $this->db->rollback();
712 return -3;
713 }
714 } else {
715 $error++;
716 $this->error = $this->db->lasterror()." - sql=$sql";
717 $this->db->rollback();
718 return -2;
719 }
720 } else {
721 $error++;
722 $this->error = $this->db->error()." - sql=$sql";
723 $this->db->rollback();
724 return -1;
725 }
726 }
727
728 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
741 public function create_line($entrepot_id, $origin_line_id, $qty, $rang = 0, $array_options = [], $parent_line_id = 0, $product_id = 0)
742 {
743 //phpcs:enable
744 global $user;
745
746 $expeditionline = new ExpeditionLigne($this->db);
747 $expeditionline->fk_expedition = $this->id;
748 $expeditionline->entrepot_id = $entrepot_id;
749 $expeditionline->fk_elementdet = $origin_line_id;
750 $expeditionline->element_type = $this->origin;
751 $expeditionline->fk_parent = $parent_line_id;
752 $expeditionline->fk_product = $product_id;
753 $expeditionline->qty = $qty;
754 $expeditionline->rang = $rang;
755 $expeditionline->array_options = $array_options;
756
757 if (!($expeditionline->fk_product > 0)) {
758 $order_line = new OrderLine($this->db);
759 $order_line->fetch($expeditionline->fk_elementdet);
760 $expeditionline->fk_product = $order_line->fk_product;
761 }
762
763 if (($lineId = $expeditionline->insert($user)) < 0) {
764 // insert() reports through errors[], so copying the singular error only lost the message
765 $this->setErrorsFromObject($expeditionline);
766 }
767 return $lineId;
768 }
769
770
771 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
779 public function create_line_batch($line_ext, $array_options = [])
780 {
781 // phpcs:enable
782 $error = 0;
783 $stockLocationQty = array(); // associated array with batch qty in stock location
784
785 $tab = $line_ext->detail_batch;
786 // create stockLocation Qty array
787 foreach ($tab as $detbatch) {
788 if (!empty($detbatch->fk_warehouse)) {
789 if (empty($stockLocationQty[$detbatch->fk_warehouse])) {
790 $stockLocationQty[$detbatch->fk_warehouse] = 0;
791 }
792 $stockLocationQty[$detbatch->fk_warehouse] += $detbatch->qty;
793 }
794 }
795 // create shipment lines
796 foreach ($stockLocationQty as $stockLocation => $qty) {
797 $line_id = $this->create_line($stockLocation, $line_ext->origin_line_id, $qty, $line_ext->rang, $array_options);
798 if ($line_id < 0) {
799 $error++;
800 } else {
801 // create shipment batch lines for stockLocation
802 foreach ($tab as $detbatch) {
803 if ($detbatch->fk_warehouse == $stockLocation) {
804 if (!($detbatch->create($line_id) > 0)) { // Create an ExpeditionLineBatch
805 $this->errors = $detbatch->errors;
806 $error++;
807 }
808 }
809 }
810 }
811 }
812
813 if (!$error) {
814 return 1;
815 } else {
816 return -1;
817 }
818 }
819
829 public function fetch($id, $ref = '', $ref_ext = '', $notused = '')
830 {
831 global $conf;
832
833 // Check parameters
834 if (empty($id) && empty($ref) && empty($ref_ext)) {
835 return -1;
836 }
837
838 $sql = "SELECT e.rowid, e.entity, e.ref, e.fk_soc as socid, e.date_creation, e.ref_customer, e.ref_ext, e.fk_user_author, e.fk_statut, e.signed_status, e.fk_projet as fk_project, e.billed";
839 $sql .= ", e.date_valid, e.fk_user_valid";
840 $sql .= ", e.weight, e.weight_units, e.size, e.size_units, e.width, e.height";
841 $sql .= ", e.date_expedition as date_expedition, e.model_pdf, e.fk_address, e.date_delivery";
842 $sql .= ", e.fk_shipping_method, e.tracking_number";
843 $sql .= ", e.note_private, e.note_public";
844 $sql .= ', e.fk_incoterms, e.location_incoterms';
845 $sql .= ', e.signed_status';
846 $sql .= ', i.libelle as label_incoterms';
847 $sql .= ', s.libelle as shipping_method';
848 $sql .= ", el.fk_source as origin_id, el.sourcetype as origin_type";
849 $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
850 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = e.rowid AND el.targettype = '".$this->db->escape($this->element)."'";
851 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON e.fk_incoterms = i.rowid';
852 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_shipment_mode as s ON e.fk_shipping_method = s.rowid';
853 $sql .= " WHERE e.entity IN (".getEntity('expedition').")";
854 if ($id) {
855 $sql .= " AND e.rowid = ".((int) $id);
856 }
857 if ($ref) {
858 $sql .= " AND e.ref='".$this->db->escape($ref)."'";
859 }
860 if ($ref_ext) {
861 $sql .= " AND e.ref_ext='".$this->db->escape($ref_ext)."'";
862 }
863
864 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
865 $result = $this->db->query($sql);
866 if ($result) {
867 if ($this->db->num_rows($result)) {
868 $obj = $this->db->fetch_object($result);
869
870 $this->id = $obj->rowid;
871 $this->entity = $obj->entity;
872 $this->ref = $obj->ref;
873 $this->socid = $obj->socid;
874 $this->ref_customer = $obj->ref_customer;
875 $this->ref_ext = $obj->ref_ext;
876 $this->status = $obj->fk_statut;
877 $this->statut = $this->status; // Deprecated
878 $this->signed_status = $obj->signed_status;
879 $this->user_author_id = $obj->fk_user_author;
880 $this->fk_user_author = $obj->fk_user_author;
881 $this->fk_user_valid = $obj->fk_user_valid;
882 $this->date_creation = $this->db->jdate($obj->date_creation);
883 $this->date_valid = $this->db->jdate($obj->date_valid);
884 $this->date = $this->db->jdate($obj->date_expedition); // TODO deprecated
885 $this->date_expedition = $this->db->jdate($obj->date_expedition); // TODO deprecated
886 $this->date_shipping = $this->db->jdate($obj->date_expedition); // Date real
887 $this->date_delivery = $this->db->jdate($obj->date_delivery); // Date planned
888 $this->fk_delivery_address = $obj->fk_address;
889 $this->model_pdf = $obj->model_pdf;
890 $this->shipping_method_id = $obj->fk_shipping_method;
891 $this->shipping_method = $obj->shipping_method;
892 $this->tracking_number = $obj->tracking_number;
893 $this->origin = ($obj->origin_type ? $obj->origin_type : 'commande'); // For compatibility
894 $this->origin_type = ($obj->origin_type ? $obj->origin_type : 'commande');
895 $this->origin_id = $obj->origin_id;
896 $this->billed = $obj->billed;
897 $this->fk_project = $obj->fk_project;
898 $this->signed_status = $obj->signed_status;
899 $this->trueWeight = $obj->weight;
900 $this->weight_units = $obj->weight_units;
901
902 $this->trueWidth = $obj->width;
903 $this->width_units = $obj->size_units;
904 $this->trueHeight = $obj->height;
905 $this->height_units = $obj->size_units;
906 $this->trueDepth = $obj->size;
907 $this->depth_units = $obj->size_units;
908
909 $this->note_public = $obj->note_public;
910 $this->note_private = $obj->note_private;
911
912 // A denormalized value
913 $this->trueSize = $obj->size."x".$obj->width."x".$obj->height;
914 $this->size_units = $obj->size_units;
915
916 //Incoterms
917 $this->fk_incoterms = $obj->fk_incoterms;
918 $this->location_incoterms = $obj->location_incoterms;
919 $this->label_incoterms = $obj->label_incoterms;
920
921 $this->db->free($result);
922
923 // Tracking url
924 $this->getUrlTrackingStatus($obj->tracking_number);
925
926 // Thirdparty
927 $result = $this->fetch_thirdparty(); // TODO Remove this
928
929 // Retrieve extrafields
930 $this->fetch_optionals();
931
932 // Fix Get multicurrency param for transmitted
933 if (isModEnabled('multicurrency')) {
934 if (!empty($this->multicurrency_code)) {
935 $this->multicurrency_code = $this->thirdparty->multicurrency_code;
936 }
937 if (getDolGlobalString('MULTICURRENCY_USE_ORIGIN_TX') && !empty($this->thirdparty->multicurrency_tx)) {
938 $this->multicurrency_tx = $this->thirdparty->multicurrency_tx;
939 }
940 }
941
942 /*
943 * Lines
944 */
945 $result = $this->fetch_lines();
946 if ($result < 0) {
947 return -3;
948 }
949
950 return 1;
951 } else {
952 dol_syslog(get_class($this).'::Fetch no expedition found', LOG_ERR);
953 $this->error = 'Shipment with id '.$id.' not found';
954 return 0;
955 }
956 } else {
957 $this->error = $this->db->error();
958 return -1;
959 }
960 }
961
969 public function valid($user, $notrigger = 0)
970 {
971 global $conf;
972
973 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
974
975 dol_syslog(get_class($this)."::valid");
976
977 // Protection
978 if ($this->status) {
979 dol_syslog(get_class($this)."::valid not in draft status", LOG_WARNING);
980 return 0;
981 }
982
983 if (!((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('expedition', 'creer'))
984 || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('expedition', 'shipping_advance', 'validate')))) {
985 $this->error = 'Permission denied';
986 dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
987 return -1;
988 }
989
990 $this->db->begin();
991
992 $error = 0;
993
994 // Define new ref
995 $soc = new Societe($this->db);
996 $soc->fetch($this->socid);
997
998 // Class of company linked to order
999 $result = $soc->setAsCustomer();
1000
1001 // Define new ref
1002 if (!$error && (preg_match('/^[\‍(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
1003 $numref = $this->getNextNumRef($soc);
1004 } elseif (!empty($this->ref)) {
1005 $numref = $this->ref;
1006 } else {
1007 $numref = "EXP".$this->id;
1008 }
1009 $this->newref = dol_sanitizeFileName($numref);
1010
1011 $now = dol_now();
1012
1013 // Validate
1014 $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
1015 $sql .= " ref='".$this->db->escape($numref)."'";
1016 $sql .= ", fk_statut = 1";
1017 $sql .= ", date_valid = '".$this->db->idate($now)."'";
1018 $sql .= ", fk_user_valid = ".((int) $user->id);
1019 $sql .= " WHERE rowid = ".((int) $this->id);
1020
1021 dol_syslog(get_class($this)."::valid update expedition", LOG_DEBUG);
1022 $resql = $this->db->query($sql);
1023 if (!$resql) {
1024 $this->error = $this->db->lasterror();
1025 $error++;
1026 }
1027
1028 // If stock increment is done on sending (recommended choice)
1029 if (!$error && isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT')) {
1030 $result = $this->manageStockMvtOnEvt($user, "ShipmentValidatedInDolibarr");
1031 if ($result < 0) {
1032 $error++;
1033 }
1034 }
1035
1036 // Change status of order to "shipment in process"
1037 if (!$error) {
1038 $triggerKey = 'SHIPPING_'; // Because when the trigger is fired the object is a shipping and not the real target object, so I add a prefix like SHIPPING_ to avoid confusion
1039 if ($this->origin == 'commande') {
1040 $triggerKey.= 'ORDER_SHIPMENTONPROCESS';
1041 } else {
1042 $triggerKey.= strtoupper($this->origin).'_SHIPMENTONPROCESS';
1043 }
1044
1045 // TODO : load the origin object to trigger the right setStatus according to origin object
1046 $ret = $this->setStatut(Commande::STATUS_SHIPMENTONPROCESS, $this->origin_id, $this->origin, $triggerKey);
1047 if (!$ret) {
1048 $error++;
1049 }
1050 }
1051
1052 if (!$error && !$notrigger) {
1053 // Call trigger
1054 $result = $this->call_trigger('SHIPPING_VALIDATE', $user);
1055 if ($result < 0) {
1056 $error++;
1057 }
1058 // End call triggers
1059 }
1060
1061 if (!$error) {
1062 $this->oldref = $this->ref;
1063
1064 // Rename directory if dir was a temporary ref
1065 if (preg_match('/^[\‍(]?PROV/i', $this->ref)) {
1066 // Now we rename also files into index
1067 $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)."'";
1068 $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'expedition/sending/".$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
1069 $resql = $this->db->query($sql);
1070 if (!$resql) {
1071 $error++;
1072 $this->error = $this->db->lasterror();
1073 }
1074 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'expedition/sending/".$this->db->escape($this->newref)."'";
1075 $sql .= " WHERE filepath = 'expedition/sending/".$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
1076 $resql = $this->db->query($sql);
1077 if (!$resql) {
1078 $error++;
1079 $this->error = $this->db->lasterror();
1080 }
1081
1082 // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
1083 $oldref = dol_sanitizeFileName($this->ref);
1084 $newref = dol_sanitizeFileName($numref);
1085 $dirsource = $conf->expedition->dir_output.'/sending/'.$oldref;
1086 $dirdest = $conf->expedition->dir_output.'/sending/'.$newref;
1087 if (!$error && file_exists($dirsource)) {
1088 dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
1089
1090 if (@rename($dirsource, $dirdest)) {
1091 dol_syslog("Rename ok");
1092 // Rename docs starting with $oldref with $newref
1093 $listoffiles = dol_dir_list($conf->expedition->dir_output.'/sending/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
1094 foreach ($listoffiles as $fileentry) {
1095 $dirsource = $fileentry['name'];
1096 $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
1097 $dirsource = $fileentry['path'].'/'.$dirsource;
1098 $dirdest = $fileentry['path'].'/'.$dirdest;
1099 @rename($dirsource, $dirdest);
1100 }
1101 }
1102 }
1103 }
1104 }
1105
1106 // Set new ref and current status
1107 if (!$error) {
1108 $this->ref = $numref;
1109 $this->statut = self::STATUS_VALIDATED;
1111 }
1112
1113 if (!$error) {
1114 $this->db->commit();
1115 return 1;
1116 } else {
1117 $this->db->rollback();
1118 return -1 * $error;
1119 }
1120 }
1121
1122
1123 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1130 public function create_delivery($user)
1131 {
1132 // phpcs:enable
1133 global $conf;
1134
1135 if (getDolGlobalInt('MAIN_SUBMODULE_DELIVERY')) {
1136 if ($this->status == self::STATUS_VALIDATED || $this->status == self::STATUS_CLOSED) {
1137 // Expedition validee
1138 include_once DOL_DOCUMENT_ROOT.'/delivery/class/delivery.class.php';
1139 $delivery = new Delivery($this->db);
1140 $result = $delivery->create_from_sending($user, $this->id);
1141 if ($result > 0) {
1142 return $result;
1143 } else {
1144 $this->error = $delivery->error;
1145 return $result;
1146 }
1147 } else {
1148 return 0;
1149 }
1150 } else {
1151 return 0;
1152 }
1153 }
1154
1169 public function addline($entrepot_id, $id, $qty, $array_options = [], $fk_product = 0, $fk_parent = 0)
1170 {
1171 global $conf, $langs;
1172
1173 $num = count($this->lines);
1174 $line = new ExpeditionLigne($this->db);
1175
1176 $line->entrepot_id = $entrepot_id;
1177 $line->origin_line_id = $id;
1178 $line->fk_elementdet = $id;
1179 $line->element_type = 'order';
1180 $line->fk_parent = $fk_parent;
1181 $line->fk_product = $fk_product;
1182 $line->qty = $qty;
1183
1184 $orderline = new OrderLine($this->db);
1185 $orderline->fetch($id);
1186
1187 // Copy the rang of the order line to the expedition line
1188 $line->rang = $orderline->rang;
1189 $line->product_type = $orderline->product_type;
1190 if (!($line->fk_product > 0)) {
1191 $line->fk_product = $orderline->fk_product;
1192 }
1193
1194 if (isModEnabled('stock') && !empty($orderline->fk_product)) {
1195 $product = new Product($this->db);
1196 $product->fetch($orderline->fk_product);
1197
1198 if (!($entrepot_id > 0) && !getDolGlobalString('STOCK_WAREHOUSE_NOT_REQUIRED_FOR_SHIPMENTS') && !(getDolGlobalString('SHIPMENT_SUPPORTS_SERVICES') && $line->product_type == Product::TYPE_SERVICE) && $product->stockable_product == Product::ENABLED_STOCK && !empty($line->qty)) {
1199 $langs->load("errors");
1200 $this->error = $langs->trans("ErrorWarehouseRequiredIntoShipmentLine");
1201 return -1;
1202 }
1203
1204 if (getDolGlobalString('STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT') && ($qty > 0 || !getDolGlobalString('SHIPMENT_GETS_ALL_ORDER_PRODUCTS'))) {
1205 $productChildrenNb = 0;
1206 if (getDolGlobalInt('PRODUIT_SOUSPRODUITS')) {
1207 $productChildrenNb = $product->hasFatherOrChild(1);
1208 }
1209 if ($productChildrenNb > 0) {
1210 $product_stock = null;
1211 $product->loadStockForVirtualProduct('warehouseopen', $line->qty);
1212 if ($entrepot_id > 0) {
1213 if (isset($product->stock_warehouse[$entrepot_id])) {
1214 $product_stock = $product->stock_warehouse[$entrepot_id]->real;
1215 }
1216 } else {
1217 foreach ($product->stock_warehouse as $componentStockWarehouse) {
1218 if ($product_stock === null) {
1219 $product_stock = $componentStockWarehouse->real;
1220 } else {
1221 $product_stock = min($product_stock, $componentStockWarehouse->real);
1222 }
1223 }
1224 }
1225 if ($product_stock === null) {
1226 $product_stock = 0;
1227 }
1228 } else {
1229 // Check must be done for stock of product into warehouse if $entrepot_id defined
1230 if ($entrepot_id > 0) {
1231 $product->load_stock('warehouseopen');
1232 $product_stock = $product->stock_warehouse[$entrepot_id]->real;
1233 } else {
1234 $product_stock = $product->stock_reel;
1235 }
1236 }
1237
1238 $product_type = $product->type;
1239 if ($product_type == 0 || getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
1240 $isavirtualproduct = ($productChildrenNb > 0);
1241 // The product is qualified for a check of quantity (must be enough in stock to be added into shipment).
1242 if (!$isavirtualproduct || !getDolGlobalString('PRODUIT_SOUSPRODUITS') || ($isavirtualproduct && !getDolGlobalString('STOCK_EXCLUDE_VIRTUAL_PRODUCTS'))) { // If STOCK_EXCLUDE_VIRTUAL_PRODUCTS is set, we do not manage stock for kits/virtual products.
1243 if ($product->stockable_product == Product::ENABLED_STOCK && $product_stock < $qty) {
1244 $langs->load("errors");
1245 $this->error = $langs->trans('ErrorStockIsNotEnoughToAddProductOnShipment', $product->ref);
1246 $this->errorhidden = 'ErrorStockIsNotEnoughToAddProductOnShipment';
1247
1248 $this->db->rollback();
1249 return -3;
1250 }
1251 }
1252 }
1253 }
1254 }
1255
1256 // If product need a batch number, we should not have called this function but addline_batch instead.
1257 // If this happen, we may have a bug in card.php page
1258 if (isModEnabled('productbatch') && !empty($line->fk_product) && !empty($orderline->product_tobatch)) {
1259 $this->error = 'ADDLINE_WAS_CALLED_INSTEAD_OF_ADDLINEBATCH '.$orderline->id.' '.$line->fk_product; //
1260 return -4;
1261 }
1262
1263 // extrafields
1264 if (!getDolGlobalString('MAIN_EXTRAFIELDS_DISABLED') && is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
1265 $line->array_options = $array_options;
1266 }
1267
1268 $this->lines[$num] = $line;
1269
1270 return 1;
1271 }
1272
1273 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1282 public function addline_batch($dbatch, $array_options = [], $origin_line = null)
1283 {
1284 // phpcs:enable
1285 global $conf, $langs;
1286
1287 $num = count($this->lines);
1288 $linebatch = null;
1289 if ($dbatch['qty'] > 0 || ($dbatch['qty'] == 0 && getDolGlobalString('SHIPMENT_GETS_ALL_ORDER_PRODUCTS'))) {
1290 $line = new ExpeditionLigne($this->db);
1291 $tab = array();
1292 foreach ($dbatch['detail'] as $key => $value) {
1293 if ($value['q'] > 0 || ($value['q'] == 0 && getDolGlobalString('SHIPMENT_GETS_ALL_ORDER_PRODUCTS'))) {
1294 // $value['q']=qty to move
1295 // $value['id_batch']=id into llx_product_batch of record to move
1296 //var_dump($value);
1297
1298 $linebatch = new ExpeditionLineBatch($this->db);
1299 $ret = $linebatch->fetchFromStock($value['id_batch']); // load serial, sellby, eatby
1300 if ($ret < 0) {
1301 $this->setErrorsFromObject($linebatch);
1302 return -1;
1303 }
1304 $linebatch->qty = $value['q'];
1305 if ($linebatch->qty == 0 && getDolGlobalString('SHIPMENT_GETS_ALL_ORDER_PRODUCTS')) {
1306 $linebatch->batch = null;
1307 }
1308 $tab[] = $linebatch;
1309
1310 if (getDolGlobalString("STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT", '0') && ($linebatch->qty > 0 || !getDolGlobalString('SHIPMENT_GETS_ALL_ORDER_PRODUCTS'))) {
1311 require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
1312 $prod_batch = new Productbatch($this->db);
1313 $prod_batch->fetch($value['id_batch']);
1314
1315 if ($prod_batch->qty < $linebatch->qty) {
1316 $langs->load("errors");
1317 $this->errors[] = $langs->trans('ErrorStockIsNotEnoughToAddProductOnShipment', $prod_batch->fk_product);
1318 dol_syslog(get_class($this)."::addline_batch error=Product ".$prod_batch->batch.": ".$this->errorsToString(), LOG_ERR);
1319 $this->db->rollback();
1320 return -1;
1321 }
1322 }
1323
1324 //var_dump($linebatch);
1325 }
1326 }
1327 if (is_object($linebatch)) {
1328 $line->entrepot_id = $linebatch->fk_warehouse;
1329 }
1330 $line->origin_line_id = $dbatch['ix_l']; // deprecated
1331 $line->fk_elementdet = $dbatch['ix_l'];
1332 $line->qty = $dbatch['qty'];
1333 $line->detail_batch = $tab;
1334 if (!($line->rang > 0)) {
1335 $line->rang = $origin_line->rang;
1336 }
1337 if (!($line->fk_product > 0)) {
1338 $line->fk_product = $origin_line->fk_product;
1339 }
1340
1341 // extrafields
1342 if (!getDolGlobalString('MAIN_EXTRAFIELDS_DISABLED') && is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
1343 $line->array_options = $array_options;
1344 }
1345
1346 //var_dump($line);
1347 $this->lines[$num] = $line;
1348 return 1;
1349 }
1350 return 0;
1351 }
1352
1360 public function update($user = null, $notrigger = 0)
1361 {
1362 global $conf;
1363 $error = 0;
1364
1365 // Clean parameters
1366
1367 if (isset($this->ref)) {
1368 $this->ref = trim($this->ref);
1369 }
1370 if (isset($this->entity)) {
1371 $this->entity = (int) $this->entity;
1372 }
1373 if (isset($this->ref_customer)) {
1374 $this->ref_customer = trim($this->ref_customer);
1375 }
1376 if (isset($this->socid)) {
1377 $this->socid = (int) $this->socid;
1378 }
1379 if (isset($this->fk_user_author)) {
1380 $this->fk_user_author = (int) $this->fk_user_author;
1381 }
1382 if (isset($this->fk_user_valid)) { // @phan-ignore-current-line PhanUndeclaredProperty
1383 // If set, then accept @phan-ignore-next-line PhanUndeclaredProperty
1384 $this->fk_user_valid = (int) $this->fk_user_valid;
1385 }
1386 if (isset($this->fk_delivery_address)) {
1387 $this->fk_delivery_address = (int) $this->fk_delivery_address;
1388 }
1389 if (isset($this->shipping_method_id)) {
1390 $this->shipping_method_id = (int) $this->shipping_method_id;
1391 }
1392 if (isset($this->tracking_number)) {
1393 $this->tracking_number = trim($this->tracking_number);
1394 }
1395 if (isset($this->statut)) {
1396 $this->statut = (int) $this->statut;
1397 }
1398 if (isset($this->status)) {
1399 $this->status = (int) $this->status;
1400 }
1401 if (isset($this->trueDepth)) {
1402 $this->trueDepth = trim($this->trueDepth);
1403 }
1404 if (isset($this->trueWidth)) {
1405 $this->trueWidth = trim($this->trueWidth);
1406 }
1407 if (isset($this->trueHeight)) {
1408 $this->trueHeight = trim($this->trueHeight);
1409 }
1410 if (isset($this->size_units)) {
1411 $this->size_units = trim($this->size_units);
1412 }
1413 if (isset($this->weight_units)) {
1414 $this->weight_units = (int) $this->weight_units;
1415 }
1416 if (isset($this->trueWeight)) {
1417 $this->weight = trim((string) $this->trueWeight);
1418 }
1419 if (isset($this->note_private)) {
1420 $this->note_private = trim($this->note_private);
1421 }
1422 if (isset($this->note_public)) {
1423 $this->note_public = trim($this->note_public);
1424 }
1425 if (isset($this->model_pdf)) {
1426 $this->model_pdf = trim($this->model_pdf);
1427 }
1428 if (!empty($this->date_expedition)) {
1429 $this->date_shipping = $this->date_expedition;
1430 }
1431
1432 // Check parameters
1433 // Put here code to add control on parameters values
1434
1435 // Update request
1436 $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
1437 $sql .= " ref = ".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
1438 $sql .= " ref_ext = ".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").",";
1439 $sql .= " ref_customer = ".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
1440 $sql .= " fk_soc = ".(isset($this->socid) ? $this->socid : "null").",";
1441 $sql .= " date_creation = ".(dol_strlen($this->date_creation) != 0 ? "'".$this->db->idate($this->date_creation)."'" : 'null').",";
1442 $sql .= " fk_user_author = ".(isset($this->fk_user_author) ? $this->fk_user_author : "null").",";
1443 $sql .= " date_valid = ".(dol_strlen($this->date_valid) != 0 ? "'".$this->db->idate($this->date_valid)."'" : 'null').",";
1444 $sql .= " fk_user_valid = ".(isset($this->fk_user_valid) ? $this->fk_user_valid : "null").",";
1445 $sql .= " date_expedition = ".(dol_strlen($this->date_shipping) != 0 ? "'".$this->db->idate($this->date_shipping)."'" : 'null').",";
1446 $sql .= " date_delivery = ".(dol_strlen($this->date_delivery) != 0 ? "'".$this->db->idate($this->date_delivery)."'" : 'null').",";
1447 $sql .= " fk_address = ".(isset($this->fk_delivery_address) ? $this->fk_delivery_address : "null").",";
1448 $sql .= " fk_shipping_method = ".((isset($this->shipping_method_id) && $this->shipping_method_id > 0) ? $this->shipping_method_id : "null").",";
1449 $sql .= " tracking_number = ".(isset($this->tracking_number) ? "'".$this->db->escape($this->tracking_number)."'" : "null").",";
1450 $sql .= " fk_statut = ".(isset($this->status) ? $this->status : "null").",";
1451 $sql .= " fk_projet = ".(isset($this->fk_project) ? $this->fk_project : "null").",";
1452 $sql .= " height = ".(($this->trueHeight != '') ? $this->trueHeight : "null").",";
1453 $sql .= " width = ".(($this->trueWidth != '') ? $this->trueWidth : "null").",";
1454 $sql .= " size_units = ".(isset($this->size_units) ? $this->size_units : "null").",";
1455 $sql .= " size = ".(($this->trueDepth != '') ? $this->trueDepth : "null").",";
1456 $sql .= " weight_units = ".(isset($this->weight_units) ? $this->weight_units : "null").",";
1457 $sql .= " weight = ".(($this->trueWeight != '') ? $this->trueWeight : "null").",";
1458 $sql .= " note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
1459 $sql .= " note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
1460 $sql .= " model_pdf = ".(isset($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null").",";
1461 $sql .= " entity = ".((int) $conf->entity);
1462 $sql .= " WHERE rowid = ".((int) $this->id);
1463
1464 $this->db->begin();
1465
1466 dol_syslog(get_class($this)."::update", LOG_DEBUG);
1467 $resql = $this->db->query($sql);
1468 if (!$resql) {
1469 $error++;
1470 $this->errors[] = "Error ".$this->db->lasterror();
1471 }
1472
1473 // Actions on extra fields
1474 if (!$error) {
1475 $result = $this->insertExtraFields();
1476 if ($result < 0) {
1477 $error++;
1478 }
1479 }
1480
1481 if (!$error && !$notrigger) {
1482 // Call trigger
1483 $result = $this->call_trigger('SHIPPING_MODIFY', $user);
1484 if ($result < 0) {
1485 $error++;
1486 }
1487 // End call triggers
1488 }
1489
1490 // Commit or rollback
1491 if ($error) {
1492 foreach ($this->errors as $errmsg) {
1493 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1494 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1495 }
1496 $this->db->rollback();
1497 return -1 * $error;
1498 } else {
1499 $this->db->commit();
1500 return 1;
1501 }
1502 }
1503
1504
1512 public function cancel($notrigger = 0, $also_update_stock = false)
1513 {
1514 global $conf, $langs, $user;
1515
1516 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1517
1518 $error = 0;
1519 $this->error = '';
1520
1521 $this->db->begin();
1522
1523 // Add a protection to refuse deleting if shipment has at least one delivery
1524 $this->fetchObjectLinked($this->id, 'shipping', 0, 'delivery'); // Get deliveries linked to this shipment
1525 if (isset($this->linkedObjectsIds['delivery']) && count($this->linkedObjectsIds['delivery']) > 0) {
1526 $this->error = 'ErrorThereIsSomeDeliveries';
1527 $error++;
1528 }
1529
1530 if (!$error && !$notrigger) {
1531 // Call trigger
1532 $result = $this->call_trigger('SHIPPING_CANCEL', $user);
1533 if ($result < 0) {
1534 $error++;
1535 }
1536 // End call triggers
1537 }
1538
1539 // Stock control
1540 $can_update_stock = isModEnabled('stock') &&
1541 ((getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT') && $this->status > self::STATUS_DRAFT) ||
1542 (getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT_CLOSE') && $this->status == self::STATUS_CLOSED && $also_update_stock));
1543 if (!$error) {
1544 require_once DOL_DOCUMENT_ROOT."/product/stock/class/mouvementstock.class.php";
1545
1546 $langs->load("agenda");
1547
1548 // Loop on each product line to add a stock movement (contain sub-products)
1549 $sql = "SELECT ";
1550 $sql .= " ed.fk_product";
1551 $sql .= ", ed.qty, ed.fk_entrepot, ed.rowid as expeditiondet_id";
1552 $sql .= ", SUM(".$this->db->ifsql("pa.rowid IS NOT NULL", "1", "0").") as iskit";
1553 $sql .= ", ".$this->db->ifsql("pai.incdec IS NULL", "1", "pai.incdec")." as incdec";
1554 $sql .= " FROM ".$this->db->prefix()."expeditiondet as ed";
1555 $sql .= " LEFT JOIN ".$this->db->prefix()."product_association as pa ON pa.fk_product_pere = ed.fk_product";
1556 $sql .= " LEFT JOIN ".$this->db->prefix()."expeditiondet as edp ON edp.rowid = ed.fk_parent";
1557 $sql .= " LEFT JOIN ".$this->db->prefix()."product_association as pai ON pai.fk_product_pere = edp.fk_product AND pai.fk_product_fils = ed.fk_product";
1558 $sql .= " WHERE ed.fk_expedition = ".((int) $this->id);
1559 $sql .= " GROUP BY ed.fk_product, ed.qty, ed.fk_entrepot, ed.rowid, pai.incdec";
1560 $sql .= $this->db->order("ed.rowid", "DESC");
1561
1562 dol_syslog(get_class($this)."::delete select details", LOG_DEBUG);
1563 $resql = $this->db->query($sql);
1564 if ($resql) {
1565 $cpt = $this->db->num_rows($resql);
1566
1567 $shipmentlinebatch = new ExpeditionLineBatch($this->db);
1568
1569 for ($i = 0; $i < $cpt; $i++) {
1570 dol_syslog(get_class($this)."::delete movement index ".$i);
1571 $obj = $this->db->fetch_object($resql);
1572 $line_id = (int) $obj->expeditiondet_id;
1573
1574 if ($can_update_stock && (empty($obj->iskit) || getDolGlobalInt('PRODUIT_SOUSPRODUITS_ALSO_ENABLE_PARENT_STOCK_MOVE')) && !empty($obj->incdec)) {
1575 $mouvS = new MouvementStock($this->db);
1576 // we do not log origin because it will be deleted
1577 $mouvS->origin = '';
1578 // get lot/serial
1579 $lotArray = null;
1580 if (isModEnabled('productbatch')) {
1581 $lotArray = $shipmentlinebatch->fetchAll($obj->expeditiondet_id);
1582 if (!is_array($lotArray)) {
1583 $error++;
1584 $this->errors[] = "Error ".$this->db->lasterror();
1585 }
1586 }
1587
1588 if (empty($lotArray)) {
1589 // no lot/serial
1590 // We increment stock of product (and sub-products)
1591 // We use warehouse selected for each line
1592 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, 0, $langs->trans("ShipmentCanceledInDolibarr", $this->ref), '', '', '', '', 0, '', 0, (empty($obj->iskit) ? 1 : 0)); // Price is set to 0, because we don't want to see WAP changed
1593 if ($result < 0) {
1594 $error++;
1595 $this->errors = array_merge($this->errors, $mouvS->errors);
1596 break;
1597 }
1598 } else {
1599 // We increment stock of batches
1600 // We use warehouse selected for each line
1601 foreach ($lotArray as $lot) {
1602 if (empty($lot->qty)) {
1603 continue;
1604 }
1605 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $lot->qty, 0, $langs->trans("ShipmentCanceledInDolibarr", $this->ref), '', '', (string) $lot->batch, '', 0, '', 0, (empty($obj->iskit) ? 1 : 0)); // Price is set to 0, because we don't want to see WAP changed. Dates are not given: those of the lot are used (the copy on the line may be outdated)
1606 if ($result < 0) {
1607 $error++;
1608 $this->errors = array_merge($this->errors, $mouvS->errors);
1609 break;
1610 }
1611 }
1612 if ($error) {
1613 break; // break for loop in case of error
1614 }
1615 }
1616 }
1617
1618 if (!$error) {
1619 // delete all children and batches of this shipment line
1620 $shipment_line = new ExpeditionLigne($this->db);
1621 $res = $shipment_line->fetch($line_id);
1622 if ($res > 0) {
1623 $result = $shipment_line->delete($user);
1624 if ($result < 0) {
1625 $error++;
1626 $this->errors[] = "Error ".$shipment_line->errorsToString();
1627 }
1628 } else {
1629 $error++;
1630 $this->errors[] = "Error ".$shipment_line->errorsToString();
1631 }
1632 }
1633
1634 if ($error) {
1635 break;
1636 }
1637 }
1638 } else {
1639 $error++;
1640 $this->errors[] = "Error ".$this->db->lasterror();
1641 }
1642 }
1643
1644 if (!$error) {
1645 // Delete linked object
1646 $res = $this->deleteObjectLinked();
1647 if ($res < 0) {
1648 $error++;
1649 }
1650
1651 // No delete expedition
1652 if (!$error) {
1653 $sql = "SELECT rowid FROM ".$this->db->prefix()."expedition";
1654 $sql .= " WHERE rowid = ".((int) $this->id);
1655
1656 if ($this->db->query($sql)) {
1657 if (!empty($this->origin) && $this->origin_id > 0) {
1658 $this->fetch_origin();
1660 '@phan-var-force Facture|Commande $origin_object';
1661 if ($origin_object->status == Commande::STATUS_SHIPMENTONPROCESS) { // If order source of shipment is "shipment in progress"
1662 // Check if there is no more shipment. If not, we can move back status of order to "validated" instead of "shipment in progress"
1663 $origin_object->loadExpeditions();
1664 //var_dump($this->$origin->expeditions);exit;
1665 if (count($origin_object->expeditions) <= 0) {
1667 }
1668 }
1669 }
1670
1671 if (!$error) {
1672 $this->db->commit();
1673
1674 // We delete PDFs
1675 $ref = dol_sanitizeFileName($this->ref);
1676 if (!empty($conf->expedition->dir_output)) {
1677 $dir = $conf->expedition->dir_output.'/sending/'.$ref;
1678 $file = $dir.'/'.$ref.'.pdf';
1679 if (file_exists($file)) {
1680 if (!dol_delete_file($file)) {
1681 return 0;
1682 }
1683 }
1684 if (file_exists($dir)) {
1685 if (!dol_delete_dir_recursive($dir)) {
1686 $this->error = $langs->trans("ErrorCanNotDeleteDir", $dir);
1687 return 0;
1688 }
1689 }
1690 }
1691
1692 return 1;
1693 } else {
1694 $this->db->rollback();
1695 return -1;
1696 }
1697 } else {
1698 $this->error = $this->db->lasterror()." - sql=$sql";
1699 $this->db->rollback();
1700 return -3;
1701 }
1702 } else {
1703 $this->db->rollback();
1704 return -2;
1705 }
1706 } else {
1707 $this->db->rollback();
1708 return -1;
1709 }
1710 }
1711
1721 public function delete($user = null, $notrigger = 0, $also_update_stock = false)
1722 {
1723 global $conf, $langs;
1724
1725 if (empty($user)) {
1726 global $user;
1727 }
1728
1729 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1730
1731 $error = 0;
1732 $this->error = '';
1733
1734 $this->db->begin();
1735
1736 // Add a protection to refuse deleting if shipment has at least one delivery
1737 $this->fetchObjectLinked($this->id, 'shipping', 0, 'delivery'); // Get deliveries linked to this shipment
1738 if (isset($this->linkedObjectsIds['delivery']) && count($this->linkedObjectsIds['delivery']) > 0) {
1739 $this->error = 'ErrorThereIsSomeDeliveries';
1740 $error++;
1741 }
1742
1743 if (!$error && !$notrigger) {
1744 // Call trigger
1745 $result = $this->call_trigger('SHIPPING_DELETE', $user);
1746 if ($result < 0) {
1747 $error++;
1748 }
1749 // End call triggers
1750 }
1751
1752 // Stock control
1753 $can_update_stock = isModEnabled('stock') &&
1754 ((getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT') && $this->status > self::STATUS_DRAFT) ||
1755 (getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT_CLOSE') && $this->status == self::STATUS_CLOSED && $also_update_stock));
1756 if (!$error) {
1757 require_once DOL_DOCUMENT_ROOT."/product/stock/class/mouvementstock.class.php";
1758
1759 $langs->load("agenda");
1760
1761 // we try deletion of batch line even if module batch not enabled in case of the module were enabled and disabled previously
1762 $shipmentlinebatch = new ExpeditionLineBatch($this->db);
1763
1764 // Loop on each product line to add a stock movement (contain sub-products)
1765 $sql = "SELECT ";
1766 $sql .= " ed.fk_product";
1767 $sql .= ", ed.qty, ed.fk_entrepot, ed.rowid as expeditiondet_id";
1768 $sql .= ", SUM(".$this->db->ifsql("pa.rowid IS NOT NULL", "1", "0").") as iskit";
1769 $sql .= ", ".$this->db->ifsql("pai.incdec IS NULL", "1", "pai.incdec")." as incdec";
1770 $sql .= " FROM ".$this->db->prefix()."expeditiondet as ed";
1771 $sql .= " LEFT JOIN ".$this->db->prefix()."product_association as pa ON pa.fk_product_pere = ed.fk_product";
1772 $sql .= " LEFT JOIN ".$this->db->prefix()."expeditiondet as edp ON edp.rowid = ed.fk_parent";
1773 $sql .= " LEFT JOIN ".$this->db->prefix()."product_association as pai ON pai.fk_product_pere = edp.fk_product AND pai.fk_product_fils = ed.fk_product";
1774 $sql .= " WHERE ed.fk_expedition = ".((int) $this->id);
1775 $sql .= " GROUP BY ed.fk_product, ed.qty, ed.fk_entrepot, ed.rowid, pai.incdec";
1776 $sql .= $this->db->order("ed.rowid", "DESC");
1777
1778 dol_syslog(get_class($this)."::delete select details", LOG_DEBUG);
1779 $resql = $this->db->query($sql);
1780 if ($resql) {
1781 $cpt = $this->db->num_rows($resql);
1782 for ($i = 0; $i < $cpt; $i++) {
1783 dol_syslog(get_class($this)."::delete movement index ".$i);
1784 $obj = $this->db->fetch_object($resql);
1785 $line_id = (int) $obj->expeditiondet_id;
1786
1787 if ($can_update_stock && (empty($obj->iskit) || getDolGlobalInt('PRODUIT_SOUSPRODUITS_ALSO_ENABLE_PARENT_STOCK_MOVE')) && !empty($obj->incdec)) {
1788 $mouvS = new MouvementStock($this->db);
1789 // we do not log origin because it will be deleted
1790 $mouvS->origin = '';
1791 // get lot/serial
1792 $lotArray = $shipmentlinebatch->fetchAll($line_id);
1793 if (!is_array($lotArray)) {
1794 $error++;
1795 $this->errors[] = "Error ".$this->db->lasterror();
1796 }
1797 if (empty($lotArray)) {
1798 // no lot/serial
1799 // We increment stock of product (disable for sub-products : already in shipment lines)
1800 // We use warehouse selected for each line
1801 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, 0, $langs->trans("ShipmentDeletedInDolibarr", $this->ref), '', '', '', '', 0, '', 0, (empty($obj->iskit) ? 1 : 0)); // Price is set to 0, because we don't want to see WAP changed
1802 if ($result < 0) {
1803 $error++;
1804 $this->errors = array_merge($this->errors, $mouvS->errors);
1805 break;
1806 }
1807 } else {
1808 // We increment stock of batches
1809 // We use warehouse selected for each line
1810 foreach ($lotArray as $lot) {
1811 if (empty($lot->qty)) {
1812 continue;
1813 }
1814 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $lot->qty, 0, $langs->trans("ShipmentDeletedInDolibarr", $this->ref), '', '', (string) $lot->batch, '', 0, '', 0, (empty($obj->iskit) ? 1 : 0)); // Price is set to 0, because we don't want to see WAP changed. Dates are not given: those of the lot are used (the copy on the line may be outdated)
1815 if ($result < 0) {
1816 $error++;
1817 $this->errors = array_merge($this->errors, $mouvS->errors);
1818 break;
1819 }
1820 }
1821 if ($error) {
1822 break; // break for loop in case of error
1823 }
1824 }
1825 }
1826
1827 if (!$error) {
1828 // delete all children and batches of this shipment line
1829 $shipment_line = new ExpeditionLigne($this->db);
1830 $res = $shipment_line->fetch($line_id);
1831 if ($res > 0) {
1832 $result = $shipment_line->delete($user);
1833 if ($result < 0) {
1834 $error++;
1835 $this->errors[] = "Error ".$shipment_line->errorsToString();
1836 }
1837 } else {
1838 $error++;
1839 $this->errors[] = "Error ".$shipment_line->errorsToString();
1840 }
1841 }
1842
1843 if ($error) {
1844 break;
1845 }
1846 }
1847 } else {
1848 $error++;
1849 $this->errors[] = "Error ".$this->db->lasterror();
1850 }
1851 }
1852
1853 if (!$error) {
1854 // Delete linked object
1855 $res = $this->deleteObjectLinked();
1856 if ($res < 0) {
1857 $error++;
1858 }
1859
1860 // delete extrafields
1861 $res = $this->deleteExtraFields();
1862 if ($res < 0) {
1863 $error++;
1864 }
1865
1866 if (!$error) {
1867 // Delete linked contacts
1868 $res = $this->delete_linked_contact();
1869 if ($res < 0) {
1870 $error++;
1871 }
1872 }
1873 if (!$error) {
1874 $sql = "DELETE FROM ".$this->db->prefix()."expedition";
1875 $sql .= " WHERE rowid = ".((int) $this->id);
1876
1877 if ($this->db->query($sql)) {
1878 if (!empty($this->origin) && $this->origin_id > 0) {
1879 $this->fetch_origin();
1881 '@phan-var-force Facture|Commande $origin_object';
1882 if ($origin_object->status == Commande::STATUS_SHIPMENTONPROCESS) { // If order source of shipment is "shipment in progress"
1883 // Check if there is no more shipment. If not, we can move back status of order to "validated" instead of "shipment in progress"
1884 $origin_object->loadExpeditions();
1885 //var_dump($this->$origin->expeditions);exit;
1886 if (count($origin_object->expeditions) <= 0) {
1888 }
1889 }
1890 }
1891
1892 if (!$error) {
1893 $this->db->commit();
1894
1895 // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
1896 $this->deleteEcmFiles(0); // Deleting files physically is done later with the dol_delete_dir_recursive
1897 $this->deleteEcmFiles(1); // Deleting files physically is done later with the dol_delete_dir_recursive
1898
1899 // We delete PDFs
1900 $ref = dol_sanitizeFileName($this->ref);
1901 if (!empty($conf->expedition->dir_output) && !empty($ref)) {
1902 $dir = $conf->expedition->dir_output.'/sending/'.$ref;
1903 $file = $dir.'/'.$ref.'.pdf';
1904 if (file_exists($file)) {
1905 if (!dol_delete_file($file)) {
1906 return 0;
1907 }
1908 }
1909 if (file_exists($dir)) {
1910 if (!dol_delete_dir_recursive($dir)) {
1911 $this->error = $langs->trans("ErrorCanNotDeleteDir", $dir);
1912 return 0;
1913 }
1914 }
1915 }
1916
1917 return 1;
1918 } else {
1919 $this->db->rollback();
1920 return -1;
1921 }
1922 } else {
1923 $this->error = $this->db->lasterror()." - sql=$sql";
1924 $this->db->rollback();
1925 return -3;
1926 }
1927 } else {
1928 $this->db->rollback();
1929 return -2;
1930 }
1931 } else {
1932 $this->db->rollback();
1933 return -1;
1934 }
1935 }
1936
1937 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1943 public function fetch_lines()
1944 {
1945 // phpcs:enable
1946 global $mysoc;
1947
1948 $this->lines = array();
1949
1950 // NOTE: This fetch_lines is special because it groups all lines with the same origin_line_id into one line.
1951 // TODO: See if we can restore a common fetch_lines (one line = one record)
1952
1953 $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";
1954 $sql .= ", cd.total_ht, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.total_tva";
1955 $sql .= ", cd.fk_remise_except, cd.fk_product_fournisseur_price as fk_fournprice";
1956 $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";
1957 $sql .= ", cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc, cd.rang, cd.date_start, cd.date_end, cd.special_code";
1958 $sql .= ", ed.rowid as line_id, ed.qty as qty_shipped, ed.fk_element, ed.fk_elementdet, ed.element_type, ed.fk_entrepot, ed.extraparams";
1959 $sql .= ", p.ref as product_ref, p.label as product_label, p.fk_product_type, p.barcode as product_barcode";
1960 $sql .= ", p.weight, p.weight_units, p.length, p.length_units, p.width, p.width_units, p.height, p.height_units";
1961 $sql .= ", p.surface, p.surface_units, p.volume, p.volume_units, p.tosell as product_tosell, p.tobuy as product_tobuy";
1962 $sql .= ", p.tobatch as product_tobatch, p.stockable_product";
1963 $sql .= " FROM ".MAIN_DB_PREFIX."expeditiondet as ed, ".MAIN_DB_PREFIX."commandedet as cd";
1964 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = cd.fk_product";
1965 $sql .= " WHERE ed.fk_expedition = ".((int) $this->id);
1966 $sql .= " AND ed.fk_elementdet = cd.rowid";
1967 $sql .= " ORDER BY cd.rang, ed.fk_elementdet"; // We need after a break on fk_elementdet but when there is no break on fk_elementdet, cd.rang is same so we can add it as first order criteria.
1968
1969 dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
1970 $resql = $this->db->query($sql);
1971 if ($resql) {
1972 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1973
1974 $num = $this->db->num_rows($resql);
1975 $i = 0;
1976 $line = new ExpeditionLigne($this->db);
1977 $lineindex = 0;
1978 $originline = 0;
1979
1980 $this->total_ht = 0;
1981 $this->total_tva = 0;
1982 $this->total_ttc = 0;
1983 $this->total_localtax1 = 0;
1984 $this->total_localtax2 = 0;
1985
1986 $this->multicurrency_total_ht = 0;
1987 $this->multicurrency_total_tva = 0;
1988 $this->multicurrency_total_ttc = 0;
1989
1990 $shipmentlinebatch = new ExpeditionLineBatch($this->db);
1991
1992 $line = new ExpeditionLigne($this->db); // always set $line for PHAN analyser. @phan-var-force muse be used after an assignation, and there is no assignation for $line.
1993 while ($i < $num) {
1994 $obj = $this->db->fetch_object($resql);
1995
1996 if ($originline > 0 && $originline == $obj->fk_elementdet) {
1997 // '@phan-var-force ExpeditionLigne $line'; // $line from previous loop
1998 $line->entrepot_id = 0; // entrepod_id in details_entrepot
1999 $line->qty_shipped += $obj->qty_shipped;
2000 } else {
2001 $line = new ExpeditionLigne($this->db); // new group to start
2002 $line->entrepot_id = $obj->fk_entrepot; // this is a property of a shipment line
2003 $line->qty_shipped = $obj->qty_shipped; // this is a property of a shipment line
2004 }
2005
2006 $detail_entrepot = new stdClass();
2007 $detail_entrepot->entrepot_id = $obj->fk_entrepot;
2008 $detail_entrepot->qty_shipped = $obj->qty_shipped;
2009 $detail_entrepot->line_id = $obj->line_id;
2010 $line->details_entrepot[] = $detail_entrepot;
2011
2012 $line->line_id = $obj->line_id; // TODO deprecated
2013 $line->rowid = $obj->line_id; // TODO deprecated
2014 $line->id = $obj->line_id;
2015
2016 $line->fk_origin = 'orderline'; // TODO deprecated, we already have element_type that can be use to guess type of line
2017
2018 $line->fk_element = $obj->fk_element;
2019 $line->origin_id = $obj->fk_element;
2020 $line->fk_elementdet = $obj->fk_elementdet;
2021 $line->origin_line_id = $obj->fk_elementdet;
2022 $line->element_type = $obj->element_type;
2023
2024 $line->fk_expedition = $this->id; // id of parent
2025
2026 $line->stockable_product = $obj->stockable_product;
2027 $line->product_type = $obj->product_type;
2028 $line->fk_product = $obj->fk_product;
2029 $line->fk_product_type = $obj->fk_product_type;
2030 $line->ref = $obj->product_ref; // TODO deprecated
2031 $line->product_ref = $obj->product_ref;
2032 $line->product_label = $obj->product_label;
2033 $line->libelle = $obj->product_label; // TODO deprecated
2034 $line->product_barcode = $obj->product_barcode; // Barcode number product
2035 $line->product_tosell = $obj->product_tosell;
2036 $line->product_tobuy = $obj->product_tobuy;
2037 $line->product_tobatch = $obj->product_tobatch;
2038 $line->fk_fournprice = $obj->fk_fournprice;
2039 $line->label = $obj->custom_label;
2040 $line->description = $obj->description;
2041 $line->qty_asked = $obj->qty_asked;
2042 $line->rang = $obj->rang;
2043 $line->weight = $obj->weight;
2044 $line->weight_units = $obj->weight_units;
2045 $line->length = $obj->length;
2046 $line->length_units = $obj->length_units;
2047 $line->width = $obj->width;
2048 $line->width_units = $obj->width_units;
2049 $line->height = $obj->height;
2050 $line->height_units = $obj->height_units;
2051 $line->surface = $obj->surface;
2052 $line->surface_units = $obj->surface_units;
2053 $line->volume = $obj->volume;
2054 $line->volume_units = $obj->volume_units;
2055 $line->stockable_product = $obj->stockable_product;
2056 $line->fk_unit = $obj->fk_unit;
2057
2058 $line->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : array();
2059
2060 $line->pa_ht = $obj->pa_ht;
2061
2062 // Local taxes
2063 $localtax_array = array(0 => $obj->localtax1_type, 1 => $obj->localtax1_tx, 2 => $obj->localtax2_type, 3 => $obj->localtax2_tx);
2064 $localtax1_tx = get_localtax($obj->tva_tx, 1, $this->thirdparty);
2065 $localtax2_tx = get_localtax($obj->tva_tx, 2, $this->thirdparty);
2066
2067 // For invoicing
2068 $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
2069 $line->desc = $obj->description; // We need ->desc because some code into CommonObject use desc (property defined for other elements)
2070 $line->qty = $line->qty_shipped;
2071 $line->total_ht = (float) $tabprice[0];
2072 $line->total_localtax1 = (float) $tabprice[9];
2073 $line->total_localtax2 = (float) $tabprice[10];
2074 $line->total_ttc = (float) $tabprice[2];
2075 $line->total_tva = (float) $tabprice[1];
2076 $line->vat_src_code = $obj->vat_src_code;
2077 $line->tva_tx = $obj->tva_tx;
2078 $line->localtax1_tx = $obj->localtax1_tx;
2079 $line->localtax2_tx = $obj->localtax2_tx;
2080 $line->info_bits = $obj->info_bits;
2081 $line->price = $obj->price;
2082 $line->subprice = $obj->subprice;
2083 $line->fk_remise_except = $obj->fk_remise_except;
2084 $line->remise_percent = $obj->remise_percent;
2085
2086 $this->total_ht += $tabprice[0];
2087 $this->total_tva += $tabprice[1];
2088 $this->total_ttc += $tabprice[2];
2089 $this->total_localtax1 += $tabprice[9];
2090 $this->total_localtax2 += $tabprice[10];
2091
2092 $line->date_start = $this->db->jdate($obj->date_start);
2093 $line->date_end = $this->db->jdate($obj->date_end);
2094
2095 $line->special_code = $obj->special_code;
2096
2097 // Multicurrency
2098 $this->fk_multicurrency = $obj->fk_multicurrency;
2099 $this->multicurrency_code = $obj->multicurrency_code;
2100 $line->multicurrency_subprice = $obj->multicurrency_subprice;
2101 $line->multicurrency_total_ht = $obj->multicurrency_total_ht;
2102 $line->multicurrency_total_tva = $obj->multicurrency_total_tva;
2103 $line->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
2104
2105 $this->multicurrency_total_ht += $obj->multicurrency_total_ht;
2106 $this->multicurrency_total_tva += $obj->multicurrency_total_tva;
2107 $this->multicurrency_total_ttc += $obj->multicurrency_total_ttc;
2108
2109 if ($originline != $obj->fk_elementdet) {
2110 $line->detail_batch = array();
2111 }
2112
2113 // Detail of batch
2114 if (isModEnabled('productbatch') && $obj->line_id > 0 && $obj->product_tobatch > 0) {
2115 $newdetailbatch = $shipmentlinebatch->fetchAll($obj->line_id, $obj->fk_product);
2116
2117 if (is_array($newdetailbatch)) {
2118 if ($originline != $obj->fk_elementdet) {
2119 $line->detail_batch = $newdetailbatch;
2120 } else {
2121 $line->detail_batch = array_merge($line->detail_batch, $newdetailbatch);
2122 }
2123 }
2124 }
2125
2126 // virtual product : find all children stock (group by product id and warehouse id)
2127 if (getDolGlobalInt('PRODUIT_SOUSPRODUITS')) {
2128 $detail_children = array(); // detail by product : array of [warehouse_id => total_qty]
2129 $line_child_list = array();
2130 $res = $line->findAllChild($line->id, $line_child_list, 1);
2131 if ($res > 0) {
2132 foreach ($line_child_list as $child_line) {
2133 foreach ($child_line as $child_obj) {
2134 $child_product_id = (int) $child_obj->fk_product;
2135 $child_warehouse_id = (int) $child_obj->fk_warehouse;
2136
2137 if ($child_warehouse_id > 0) {
2138 // child quantities group by warehouses
2139 if (!isset($detail_children[$child_product_id])) {
2140 $detail_children[$child_product_id] = array();
2141 }
2142 if (!isset($detail_children[$child_product_id][$child_warehouse_id])) {
2143 $detail_children[$child_product_id][$child_warehouse_id] = 0;
2144 }
2145 $detail_children[$child_product_id][$child_warehouse_id] += $child_obj->qty;
2146 }
2147 }
2148 }
2149 }
2150 $line->detail_children = $detail_children;
2151 }
2152
2153 $line->fetch_optionals();
2154
2155 if ($originline != $obj->fk_elementdet) {
2156 $this->lines[$lineindex] = $line;
2157 $lineindex++;
2158 } else {
2159 $line->total_ht += $tabprice[0];
2160 $line->total_localtax1 += $tabprice[9];
2161 $line->total_localtax2 += $tabprice[10];
2162 $line->total_ttc += $tabprice[2];
2163 $line->total_tva += $tabprice[1];
2164 }
2165
2166 $i++;
2167 $originline = $obj->fk_elementdet;
2168 }
2169 $this->db->free($resql);
2170 return 1;
2171 } else {
2172 $this->error = $this->db->error();
2173 return -3;
2174 }
2175 }
2176
2184 public function deleteLine($user, $lineid)
2185 {
2186 global $user;
2187
2188 if ($this->status == self::STATUS_DRAFT) {
2189 $this->db->begin();
2190
2191 $line = new ExpeditionLigne($this->db);
2192
2193 // For triggers
2194 $line->fetch($lineid);
2195
2196 if ($this->id > 0 && (int) $line->fk_expedition !== (int) $this->id) {
2197 $this->db->rollback();
2198 $this->error = 'ErrorLineIDDoesNotMatchWithObjectID';
2199 return -1;
2200 }
2201
2202 if ($line->delete($user) > 0) {
2203 //$this->update_price(1);
2204
2205 $this->db->commit();
2206 return 1;
2207 } else {
2208 $this->db->rollback();
2209 return -1;
2210 }
2211 } else {
2212 $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
2213 return -2;
2214 }
2215 }
2216
2217
2224 public function getTooltipContentArray($params)
2225 {
2226 global $conf, $langs;
2227
2228 $langs->load('sendings');
2229
2230 $nofetch = !empty($params['nofetch']);
2231
2232 $datas = array();
2233 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Shipment").'</u>';
2234 if (isset($this->status)) {
2235 $datas['picto'] .= ' '.$this->getLibStatut(5);
2236 }
2237 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
2238 $datas['refcustomer'] = '<br><b>'.$langs->trans('RefCustomer').':</b> '.($this->ref_customer ? $this->ref_customer : $this->ref_client);
2239 if (!$nofetch) {
2240 $langs->load('companies');
2241 if (empty($this->thirdparty)) {
2242 $this->fetch_thirdparty();
2243 }
2244 $datas['customer'] = '<br><b>'.$langs->trans('Customer').':</b> '.$this->thirdparty->getNomUrl(1, '', 0, 1);
2245 }
2246
2247 return $datas;
2248 }
2249
2261 public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1)
2262 {
2263 global $langs, $hookmanager;
2264
2265 $result = '';
2266 $params = [
2267 'id' => $this->id,
2268 'objecttype' => $this->element,
2269 'option' => $option,
2270 'nofetch' => 1,
2271 ];
2272 $classfortooltip = 'classfortooltip';
2273 $dataparams = '';
2274 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
2275 $classfortooltip = 'classforajaxtooltip';
2276 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
2277 $label = '';
2278 } else {
2279 $label = implode($this->getTooltipContentArray($params));
2280 }
2281
2282 $url = DOL_URL_ROOT.'/expedition/card.php?id='.$this->id;
2283
2284 if ($short) {
2285 return $url;
2286 }
2287
2288 if ($option !== 'nolink') {
2289 // Add param to save lastsearch_values or not
2290 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2291 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
2292 $add_save_lastsearch_values = 1;
2293 }
2294 if ($add_save_lastsearch_values) {
2295 $url .= '&save_lastsearch_values=1';
2296 }
2297 }
2298
2299 $linkclose = '';
2300 if (empty($notooltip)) {
2301 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
2302 $label = $langs->trans("Shipment");
2303 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
2304 }
2305 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
2306 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
2307 }
2308
2309 $linkstart = '<a href="'.$url.'"';
2310 $linkstart .= $linkclose.'>';
2311 $linkend = '</a>';
2312
2313 $result .= $linkstart;
2314 if ($withpicto) {
2315 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
2316 }
2317 if ($withpicto != 2) {
2318 $result .= $this->ref;
2319 }
2320 $result .= $linkend;
2321 global $action;
2322 $hookmanager->initHooks(array($this->element . 'dao'));
2323 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
2324 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
2325 if ($reshook > 0) {
2326 $result = $hookmanager->resPrint;
2327 } else {
2328 $result .= $hookmanager->resPrint;
2329 }
2330 return $result;
2331 }
2332
2339 public function getLibStatut($mode = 0)
2340 {
2341 return $this->LibStatut($this->status, $mode);
2342 }
2343
2344 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2352 public function LibStatut($status, $mode)
2353 {
2354 // phpcs:enable
2355 global $langs;
2356
2357 $labelStatus = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
2358 $labelStatusShort = $langs->transnoentitiesnoconv($this->labelStatusShort[$status]);
2359
2360 $statusType = 'status'.$status;
2361 if ($status == self::STATUS_VALIDATED) {
2362 $statusType = 'status4';
2363 }
2364 if ($status == self::STATUS_CLOSED) {
2365 $statusType = 'status6';
2366 }
2367 if ($status == self::STATUS_CANCELED) {
2368 $statusType = 'status9';
2369 }
2370
2371 $signed_label = ' (' . $this->getLibSignedStatus() . ')';
2372 $status_label = $this->signed_status ? $labelStatus . $signed_label : $labelStatus;
2373 $status_label_short = $this->signed_status ? $labelStatusShort . $signed_label : $labelStatusShort;
2374
2375 return dolGetStatus($status_label, $status_label_short, '', $statusType, $mode);
2376 }
2377
2385 public function getKanbanView($option = '', $arraydata = null)
2386 {
2387 global $langs, $conf;
2388
2389 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2390
2391 $return = '<div class="box-flex-item box-flex-grow-zero">';
2392 $return .= '<div class="info-box info-box-sm">';
2393 $return .= '<div class="info-box-icon bg-infobox-action">';
2394 $return .= img_picto('', 'order');
2395 $return .= '</div>';
2396 $return .= '<div class="info-box-content">';
2397 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
2398 if ($selected >= 0) {
2399 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2400 }
2401 if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2402 $return .= '<br><div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2403 }
2404 if (property_exists($this, 'total_ht')) {
2405 $return .= '<div class="info-box-ref amount">'.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency).' '.$langs->trans('HT').'</div>';
2406 }
2407 if (method_exists($this, 'getLibStatut')) {
2408 $return .= '<div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2409 }
2410 $return .= '</div>';
2411 $return .= '</div>';
2412 $return .= '</div>';
2413
2414 return $return;
2415 }
2416
2424 public function initAsSpecimen()
2425 {
2426 global $langs;
2427
2428 $now = dol_now();
2429
2430 dol_syslog(get_class($this)."::initAsSpecimen");
2431
2432 $order = new Commande($this->db);
2433 $order->initAsSpecimen();
2434
2435 // Initialise parameters
2436 $this->id = 0;
2437 $this->ref = 'SPECIMEN';
2438 $this->specimen = 1;
2440 $this->livraison_id = 0;
2441 $this->date = $now;
2442 $this->date_creation = $now;
2443 $this->date_valid = $now;
2444 $this->date_delivery = $now + 24 * 3600;
2445 $this->date_expedition = $now + 24 * 3600;
2446
2447 $this->entrepot_id = 0;
2448 $this->fk_delivery_address = 0;
2449 $this->socid = 1;
2450
2451 $this->commande_id = 0;
2452 $this->commande = $order;
2453
2454 $this->origin_id = 1;
2455 $this->origin_type = 'commande';
2456
2457 $this->note_private = 'Private note';
2458 $this->note_public = 'Public note';
2459
2460 $nbp = min(1000, GETPOSTINT('nblines') ? GETPOSTINT('nblines') : 5); // We can force the nb of lines to test from command line (but not more than 1000)
2461 $xnbp = 0;
2462 while ($xnbp < $nbp) {
2463 $line = new ExpeditionLigne($this->db);
2464 $line->product_desc = $langs->trans("Description")." ".$xnbp;
2465 $line->product_label = $langs->trans("Description")." ".$xnbp;
2466 $line->qty = 10;
2467 $line->qty_asked = 5;
2468 $line->qty_shipped = 4;
2469 $line->fk_product = $this->commande->lines[$xnbp]->fk_product;
2470
2471 $line->weight = 1.123456;
2472 $line->weight_units = 0; // kg
2473
2474 $line->volume = 2.34567;
2475 $line->volume_unit = 0;
2476
2477 $this->lines[] = $line;
2478 $xnbp++;
2479 }
2480
2481 return 1;
2482 }
2483
2484 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2493 public function set_date_livraison($user, $delivery_date)
2494 {
2495 // phpcs:enable
2496 return $this->setDeliveryDate($user, $delivery_date);
2497 }
2498
2506 public function setDeliveryDate($user, $delivery_date)
2507 {
2508 if ($user->hasRight('expedition', 'creer')) {
2509 $sql = "UPDATE ".MAIN_DB_PREFIX."expedition";
2510 $sql .= " SET date_delivery = ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : 'null');
2511 $sql .= " WHERE rowid = ".((int) $this->id);
2512
2513 dol_syslog(get_class($this)."::setDeliveryDate", LOG_DEBUG);
2514 $resql = $this->db->query($sql);
2515 if ($resql) {
2516 $this->date_delivery = $delivery_date;
2517 return 1;
2518 } else {
2519 $this->error = $this->db->error();
2520 return -1;
2521 }
2522 } else {
2523 return -2;
2524 }
2525 }
2526
2534 public function setShippingDate($user, $shipping_date)
2535 {
2536 if ($user->hasRight('expedition', 'creer')) {
2537 $sql = "UPDATE ".MAIN_DB_PREFIX."expedition";
2538 $sql .= " SET date_expedition = ".($shipping_date ? "'".$this->db->idate($shipping_date)."'" : 'null');
2539 $sql .= " WHERE rowid = ".((int) $this->id);
2540
2541 dol_syslog(get_class($this)."::setShippingDate", LOG_DEBUG);
2542 $resql = $this->db->query($sql);
2543 if ($resql) {
2544 $this->date_shipping = $shipping_date;
2545 return 1;
2546 } else {
2547 $this->error = $this->db->error();
2548 return -1;
2549 }
2550 } else {
2551 return -2;
2552 }
2553 }
2554
2555 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2561 public function fetch_delivery_methods()
2562 {
2563 // phpcs:enable
2564 global $langs;
2565 $this->meths = array();
2566
2567 $sql = "SELECT em.rowid, em.code, em.libelle as label";
2568 $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as em";
2569 $sql .= " WHERE em.active = 1";
2570 $sql .= " ORDER BY em.libelle ASC";
2571
2572 $resql = $this->db->query($sql);
2573 if ($resql) {
2574 while ($obj = $this->db->fetch_object($resql)) {
2575 $label = $langs->trans('SendingMethod'.$obj->code);
2576 $this->meths[$obj->rowid] = ($label != 'SendingMethod'.$obj->code ? $label : $obj->label);
2577 }
2578 }
2579 }
2580
2581 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2588 public function list_delivery_methods($id = 0)
2589 {
2590 // phpcs:enable
2591 global $langs;
2592
2593 $this->listmeths = array();
2594 $i = 0;
2595
2596 $sql = "SELECT em.rowid, em.code, em.libelle as label, em.description, em.tracking, em.active";
2597 $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as em";
2598 if (!empty($id)) {
2599 $sql .= " WHERE em.rowid=".((int) $id);
2600 }
2601
2602 $resql = $this->db->query($sql);
2603 if ($resql) {
2604 while ($obj = $this->db->fetch_object($resql)) {
2605 $this->listmeths[$i]['rowid'] = $obj->rowid;
2606 $this->listmeths[$i]['code'] = $obj->code;
2607 $label = $langs->trans('SendingMethod'.$obj->code);
2608 $this->listmeths[$i]['libelle'] = ($label != 'SendingMethod'.$obj->code ? $label : $obj->label);
2609 $this->listmeths[$i]['description'] = $obj->description;
2610 $this->listmeths[$i]['tracking'] = $obj->tracking;
2611 $this->listmeths[$i]['active'] = $obj->active;
2612 $i++;
2613 }
2614 }
2615 }
2616
2623 public function getUrlTrackingStatus($value = '')
2624 {
2625 if (!empty($this->shipping_method_id)) {
2626 $sql = "SELECT em.code, em.tracking";
2627 $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as em";
2628 $sql .= " WHERE em.rowid = ".((int) $this->shipping_method_id);
2629
2630 $resql = $this->db->query($sql);
2631 if ($resql) {
2632 if ($obj = $this->db->fetch_object($resql)) {
2633 $tracking = $obj->tracking;
2634 }
2635 }
2636 }
2637
2638 if (!empty($tracking) && !empty($value)) {
2639 $url = str_replace('{TRACKID}', $value, $tracking);
2640 $this->tracking_url = sprintf('<a target="_blank" rel="noopener noreferrer" href="%s">%s</a>', $url, ($value ? $value : 'url'));
2641 } else {
2642 $this->tracking_url = $value;
2643 }
2644 }
2645
2651 public function setClosed()
2652 {
2653 global $langs, $user;
2654
2655 $error = 0;
2656
2657 // Protection. This avoid to move stock later when we should not
2658 if ($this->status == self::STATUS_CLOSED) {
2659 return 0;
2660 }
2661
2662 $this->db->begin();
2663
2664 $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET fk_statut = ".self::STATUS_CLOSED;
2665 if (empty($this->date_shipping)) { // Date of real shipment was not yet set, we force it on closing
2666 $sql .= ", date_expedition = '".$this->db->escape($this->db->idate(dol_now()))."'";
2667 }
2668 $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut > 0";
2669 $sql .= " AND fk_statut <> ".self::STATUS_CLOSED;
2670
2671 $resql = $this->db->query($sql);
2672 if ($resql && $this->db->affected_rows($resql) <= 0) {
2673 // The shipment is not validated (draft, canceled or already closed): nothing to close and no stock must be moved.
2674 // Return 0 (nothing done) as when it is already closed, so the workflow closing the shipments of an invoice is not stopped by a draft one.
2675 $this->db->rollback();
2676 dol_syslog(get_class($this)."::setClosed shipment ".$this->id." is not validated, nothing done", LOG_WARNING);
2677 return 0;
2678 }
2679 if ($resql) {
2680 // Set order billed if 100% of order is shipped (qty in shipment lines match qty in order lines)
2681 if ($this->origin == 'commande' && $this->origin_id > 0) {
2682 $order = new Commande($this->db);
2683 $order->fetch($this->origin_id);
2684
2685 $order->loadExpeditions(self::STATUS_CLOSED); // Fill $order->expeditions = array(orderlineid => qty)
2686
2687 $shipments_match_order = 1;
2688 foreach ($order->lines as $line) {
2689 $lineid = $line->id;
2690 $qty = $line->qty;
2691 if (($line->product_type == 0 || getDolGlobalString('STOCK_SUPPORTS_SERVICES')) && $order->expeditions[$lineid] != $qty) {
2692 $shipments_match_order = 0;
2693 $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';
2694 dol_syslog($text);
2695 break;
2696 }
2697 }
2698 if ($shipments_match_order) {
2699 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');
2700 // We close the order
2701 $order->cloture($user, 0, 0); // 0 = do not check the close permission: this is an automatic action of the shipment closing. Note this may also create an invoice if module workflow ask it
2702 }
2703 }
2704
2705 $this->statut = self::STATUS_CLOSED; // Will be revert to STATUS_VALIDATED at end if there is a rollback
2706 $this->status = self::STATUS_CLOSED; // Will be revert to STATUS_VALIDATED at end if there is a rollback
2707
2708 // If stock increment is done on closing
2709 if (!$error && isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT_CLOSE')) {
2710 $result = $this->manageStockMvtOnEvt($user);
2711 if ($result < 0) {
2712 $error++;
2713 }
2714 }
2715
2716 // Call trigger
2717 if (!$error) {
2718 $result = $this->call_trigger('SHIPPING_CLOSED', $user);
2719 if ($result < 0) {
2720 $error++;
2721 }
2722 }
2723 } else {
2724 dol_print_error($this->db);
2725 $error++;
2726 }
2727
2728 if (!$error) {
2729 $this->db->commit();
2730 return 1;
2731 } else {
2732 $this->statut = self::STATUS_VALIDATED;
2734
2735 $this->db->rollback();
2736 return -1;
2737 }
2738 }
2739
2748 private function manageStockMvtOnEvt($user, $labelmovement = 'ShipmentClassifyClosedInDolibarr')
2749 {
2750 global $langs;
2751
2752 $error = 0;
2753
2754 require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php';
2755
2756 $langs->load("agenda");
2757
2758 // Loop on each product line to add a stock movement
2759 $sql = "SELECT";
2760 $sql .= " ed.rowid as edid, ed.fk_product, ed.qty, ed.fk_entrepot";
2761 $sql .= ", cd.rowid as cdid";
2762 $sql .= ", cd.subprice";
2763 $sql .= ", edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
2764 $sql .= ", e.ref";
2765 $sql .= " FROM " . $this->db->prefix() . "expeditiondet as ed";
2766 $sql .= " LEFT JOIN " . $this->db->prefix() . "commandedet as cd ON cd.rowid = ed.fk_elementdet";
2767 $sql .= " LEFT JOIN " . $this->db->prefix() . "expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
2768 $sql .= " INNER JOIN " . $this->db->prefix() . "expedition as e ON ed.fk_expedition = e.rowid";
2769 $sql .= " WHERE ed.fk_expedition = " . ((int) $this->id);
2770 //$sql .= " AND cd.rowid = ed.fk_elementdet";
2771
2772 dol_syslog(get_class($this) . "::valid select details", LOG_DEBUG);
2773 $resql = $this->db->query($sql);
2774 if ($resql) {
2775 $cpt = $this->db->num_rows($resql);
2776 for ($i = 0; $i < $cpt; $i++) {
2777 $obj = $this->db->fetch_object($resql);
2778 if (empty($obj->edbrowid)) {
2779 $qty = $obj->qty;
2780 } else {
2781 $qty = $obj->edbqty;
2782 }
2783 if ($qty <= 0 || ($qty < 0 && !getDolGlobalInt('SHIPMENT_ALLOW_NEGATIVE_QTY'))) {
2784 continue;
2785 }
2786 dol_syslog(get_class($this) . "::valid movement index " . $i . " ed.rowid=" . $obj->edid . " edb.rowid=" . $obj->edbrowid);
2787
2788 $mouvS = new MouvementStock($this->db);
2789 $mouvS->origin = &$this;
2790 $mouvS->setOrigin($this->element, $this->id, $obj->cdid, $obj->edid);
2791
2792 if (empty($obj->edbrowid)) {
2793 // line without batch detail
2794
2795 // 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
2796 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans($labelmovement, $obj->ref));
2797 if ($result < 0) {
2798 $this->error = $mouvS->error;
2799 $this->errors = $mouvS->errors;
2800 $error++;
2801 break;
2802 }
2803 } else {
2804 // line with batch detail
2805
2806 // 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
2807 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans($labelmovement, $obj->ref), '', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, $obj->fk_origin_stock);
2808 if ($result < 0) {
2809 $this->error = $mouvS->error;
2810 $this->errors = $mouvS->errors;
2811 $error++;
2812 break;
2813 }
2814 }
2815
2816 // 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
2817 // having a lot1/qty=X and lot2/qty=-X, so 0 but we must not loose repartition of different lot.
2818 $sqldelete = "DELETE FROM ".$this->db->prefix()."product_stock WHERE reel = 0 AND rowid NOT IN (SELECT fk_product_stock FROM ".$this->db->prefix()."product_batch as pb)";
2819 $resqldelete = $this->db->query($sqldelete);
2820 // We do not test error, it can fails if there is child in batch details
2821 }
2822 } else {
2823 $this->error = $this->db->lasterror();
2824 $this->errors[] = $this->db->lasterror();
2825 $error++;
2826 }
2827
2828 if (!$error) {
2829 return 1;
2830 } else {
2831 return -1;
2832 }
2833 }
2834
2840 public function setBilled()
2841 {
2842 global $user;
2843 $error = 0;
2844
2845 $this->db->begin();
2846
2847 $sql = 'UPDATE '.MAIN_DB_PREFIX.'expedition SET billed = 1';
2848 $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut > 0';
2849
2850 $resql = $this->db->query($sql);
2851 if ($resql) {
2852 $this->billed = 1;
2853
2854 // Call trigger
2855 $result = $this->call_trigger('SHIPPING_BILLED', $user);
2856 if ($result < 0) {
2857 $this->billed = 0;
2858 $error++;
2859 }
2860 } else {
2861 $error++;
2862 $this->errors[] = $this->db->lasterror;
2863 }
2864
2865 if (empty($error)) {
2866 $this->db->commit();
2867 return 1;
2868 } else {
2869 $this->db->rollback();
2870 return -1;
2871 }
2872 }
2873
2881 public function setDraft($user, $notrigger = 0)
2882 {
2883 // Protection
2884 if ($this->status <= self::STATUS_DRAFT) {
2885 return 0;
2886 }
2887
2888 return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'SHIPMENT_UNVALIDATE');
2889 }
2890
2896 public function reOpen()
2897 {
2898 global $langs, $user;
2899
2900 $error = 0;
2901
2902 // Protection. This avoid to move stock later when we should not
2903 if ($this->status == self::STATUS_VALIDATED) {
2904 return 0;
2905 }
2906
2907 $this->db->begin();
2908
2909 $oldbilled = $this->billed;
2910
2911 $sql = 'UPDATE '.MAIN_DB_PREFIX.'expedition SET fk_statut = 1';
2912 $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut = '.self::STATUS_CLOSED;
2913
2914 $resql = $this->db->query($sql);
2915 if ($resql && $this->db->affected_rows($resql) <= 0) {
2916 // The shipment is not closed (draft or canceled), so no stock must be moved
2917 $this->db->rollback();
2918 $langs->load("sendings");
2919 $this->error = $langs->trans("StatusOfRefMustBe", $this->ref, $langs->transnoentitiesnoconv("StatusSendingProcessedShort"));
2920 $this->errors[] = $this->error;
2921 return -1;
2922 }
2923 if ($resql) {
2924 $this->statut = self::STATUS_VALIDATED;
2926 $this->billed = 0;
2927
2928 // If stock increment is done on closing
2929 if (isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT_CLOSE')) {
2930 require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
2931
2932 $langs->load("agenda");
2933
2934 // Loop on each product line to add a stock movement
2935 // TODO possibilite d'expedier a partir d'une propale ou autre origine
2936 $sql = "SELECT cd.fk_product, cd.subprice,";
2937 $sql .= " ed.rowid, ed.qty, ed.fk_entrepot,";
2938 $sql .= " edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
2939 $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd,";
2940 $sql .= " ".MAIN_DB_PREFIX."expeditiondet as ed";
2941 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
2942 $sql .= " WHERE ed.fk_expedition = ".((int) $this->id);
2943 $sql .= " AND cd.rowid = ed.fk_elementdet";
2944
2945 dol_syslog(get_class($this)."::valid select details", LOG_DEBUG);
2946 $resql = $this->db->query($sql);
2947 if ($resql) {
2948 $cpt = $this->db->num_rows($resql);
2949 for ($i = 0; $i < $cpt; $i++) {
2950 $obj = $this->db->fetch_object($resql);
2951 if (empty($obj->edbrowid)) {
2952 $qty = $obj->qty;
2953 } else {
2954 $qty = $obj->edbqty;
2955 }
2956 if ($qty <= 0) {
2957 continue;
2958 }
2959 dol_syslog(get_class($this)."::reopen expedition movement index ".$i." ed.rowid=".$obj->rowid." edb.rowid=".$obj->edbrowid);
2960
2961 //var_dump($this->lines[$i]);
2962 $mouvS = new MouvementStock($this->db);
2963 $mouvS->origin = &$this;
2964 $mouvS->setOrigin($this->element, $this->id);
2965
2966 if (empty($obj->edbrowid)) {
2967 // line without batch detail
2968
2969 // 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
2970 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, -$qty, $obj->subprice, $langs->trans("ShipmentUnClassifyCloseddInDolibarr", $this->ref));
2971 if ($result < 0) {
2972 $this->error = $mouvS->error;
2973 $this->errors = $mouvS->errors;
2974 $error++;
2975 break;
2976 }
2977 } else {
2978 // line with batch detail
2979
2980 // 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
2981 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, -$qty, $obj->subprice, $langs->trans("ShipmentUnClassifyCloseddInDolibarr", $this->ref), '', '', '', $obj->batch, $obj->fk_origin_stock); // Dates are not given: those of the lot are used (the copy on the line may be outdated)
2982 if ($result < 0) {
2983 $this->error = $mouvS->error;
2984 $this->errors = $mouvS->errors;
2985 $error++;
2986 break;
2987 }
2988 }
2989 }
2990 } else {
2991 $this->error = $this->db->lasterror();
2992 $error++;
2993 }
2994 }
2995
2996 if (!$error) {
2997 // Call trigger
2998 $result = $this->call_trigger('SHIPPING_REOPEN', $user);
2999 if ($result < 0) {
3000 $error++;
3001 }
3002 }
3003 } else {
3004 $error++;
3005 $this->errors[] = $this->db->lasterror();
3006 }
3007
3008 if (!$error) {
3009 $this->db->commit();
3010 return 1;
3011 } else {
3012 $this->statut = self::STATUS_CLOSED;
3013 $this->status = self::STATUS_CLOSED;
3014 $this->billed = $oldbilled;
3015 $this->db->rollback();
3016 return -1;
3017 }
3018 }
3019
3031 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
3032 {
3033 $outputlangs->load("products");
3034
3035 if (!dol_strlen($modele)) {
3036 $modele = 'rouget';
3037
3038 if (!empty($this->model_pdf)) {
3039 $modele = $this->model_pdf;
3040 } elseif (getDolGlobalString('EXPEDITION_ADDON_PDF')) {
3041 $modele = getDolGlobalString('EXPEDITION_ADDON_PDF');
3042 }
3043 }
3044
3045 $modelpath = "core/modules/expedition/doc/";
3046
3047 $this->fetch_origin();
3048
3049 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3050 }
3051
3060 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
3061 {
3062 $tables = array(
3063 'expedition'
3064 );
3065
3066 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
3067 }
3068}
$object ref
Definition info.php:90
Class to manage customers orders.
const STATUS_SHIPMENTONPROCESS
Shipment on process.
const STATUS_VALIDATED
Validated status.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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...
deleteEcmFiles($mode=0)
Delete related files of object in database.
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
setErrorsFromObject($object)
setErrorsFromObject
setStatut($status, $elementId=null, $elementType='', $trigkey='', $fieldstatus='fk_statut')
Set status of an object.
deleteExtraFields()
Delete all extra fields values for the current object.
setStatusCommon($user, $status, $notrigger=0, $triggercode='')
Set to a status.
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).
errorsToString()
Method to output saved errors.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
fetch_origin()
Read linked origin object.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
call_trigger($triggerName, $user)
Call trigger based on this instance.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Class to manage receptions.
Class to manage Dolibarr database access.
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $notooltip=0, $save_lastsearch_value=-1)
Return clickable link of object (with eventually picto)
addline_batch($dbatch, $array_options=[], $origin_line=null)
Add a shipment line with batch record.
create_delivery($user)
Create a delivery receipt from a shipment.
setDraft($user, $notrigger=0)
Set draft status.
const STATUS_SHIPMENT_IN_PROGRESS
Expedition in progress -> package exit the warehouse and is now in the truck or into the hand of the ...
getUrlTrackingStatus($value='')
Forge an set tracking url.
setClosed()
Classify the shipping as closed (this records also the stock movement)
__construct($db)
Constructor.
create_line($entrepot_id, $origin_line_id, $qty, $rang=0, $array_options=[], $parent_line_id=0, $product_id=0)
Create a expedition line.
fetch_lines()
Load lines.
addline($entrepot_id, $id, $qty, $array_options=[], $fk_product=0, $fk_parent=0)
Add an expedition line.
create($user, $notrigger=0)
Create expedition en base.
LibStatut($status, $mode)
Return label of a status.
setBilled()
Classify the shipping as invoiced (used for example by trigger when WORKFLOW_SHIPPING_CLASSIFY_BILLED...
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
getTooltipContentArray($params)
getTooltipContentArray
setDeliveryDate($user, $delivery_date)
Set the planned delivery date.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
setShippingDate($user, $shipping_date)
Set the shipping date.
const STATUS_DRAFT
Draft status.
const STATUS_CANCELED
Canceled status.
getLibStatut($mode=0)
Return status label.
set_date_livraison($user, $delivery_date)
Set delivery date.
initAsSpecimen()
Initialise an instance with random values.
getNextNumRef($soc)
Return next expedition ref.
const STATUS_CLOSED
Closed status -> parcel was received by customer / end of process prev status : validated or shipment...
const STATUS_VALIDATED
Validated status -> parcel is ready to be sent prev status : draft next status : closed or shipment_i...
create_line_batch($line_ext, $array_options=[])
Create the detail of the expedition line.
manageStockMvtOnEvt($user, $labelmovement='ShipmentClassifyClosedInDolibarr')
Manage Stock MVt onb Close or valid Shipment.
valid($user, $notrigger=0)
Validate object and update stock if option enabled.
update($user=null, $notrigger=0)
Update database.
cancel($notrigger=0, $also_update_stock=false)
Cancel shipment.
fetch_delivery_methods()
Fetch deliveries method and return an array.
fetch($id, $ref='', $ref_ext='', $notused='')
Get object and lines from database.
reOpen()
Classify the shipping as validated/opened.
deleteLine($user, $lineid)
Delete detail line.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
list_delivery_methods($id=0)
Fetch all deliveries method and return an array.
Class to manage lines of shipment.
CRUD class for batch number management within shipment.
Class to manage stock movements.
Class to manage order lines.
Class to manage products or services.
const TYPE_SERVICE
Service.
Manage record for batch number management.
Class to manage third parties objects (customers, suppliers, prospects...)
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:171
getLibSignedStatus(int $mode=0)
Returns the label for signed status.
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.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:63
setEntity($currentobject)
Set entity id to use when to create an object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
get_localtax($vatrate, $local, $thirdparty_buyer=null, $thirdparty_seller=null, $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller=null, $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition price.lib.php:90
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:162