dolibarr 19.0.3
delivery.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2005-2014 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2006-2007 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
6 * Copyright (C) 2011-2023 Philippe Grand <philippe.grand@atoo-net.com>
7 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8 * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
9 * Copyright (C) 2023 Frédéric France <frederic.france@netlogic.fr>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
31require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
32require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
34require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
35if (isModEnabled("propal")) {
36 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
37}
38if (isModEnabled('commande')) {
39 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
40}
41
42
47{
49
53 public $element = "delivery";
54
58 public $fk_element = "fk_delivery";
59
63 public $table_element = "delivery";
64
68 public $table_element_line = "deliverydet";
69
73 public $picto = 'sending';
74
78 public $draft;
79
83 public $socid;
84
88 public $ref_customer;
89
93 public $date_delivery;
94
98 public $date_creation;
99
103 public $date_valid;
104
108 public $model_pdf;
109
110 public $commande_id;
111
115 public $labelStatus;
116
120 public $lines = array();
121
125 public $user_author_id;
126
127
133 public function __construct($db)
134 {
135 $this->db = $db;
136
137 // List of short language codes for status
138 $this->labelStatus[-1] = 'StatusDeliveryCanceled';
139 $this->labelStatus[0] = 'StatusDeliveryDraft';
140 $this->labelStatus[1] = 'StatusDeliveryValidated';
141 }
142
149 public function create($user)
150 {
151 global $conf;
152
153 dol_syslog("Delivery::create");
154
155 if (empty($this->model_pdf)) {
156 $this->model_pdf = $conf->global->DELIVERY_ADDON_PDF;
157 }
158
159 $error = 0;
160
161 $now = dol_now();
162
163 /* Delivery note as draft On positionne en mode draft le bon de livraison */
164 $this->draft = 1;
165
166 $this->user = $user;
167
168 $this->db->begin();
169
170 $sql = "INSERT INTO ".MAIN_DB_PREFIX."delivery (";
171 $sql .= "ref";
172 $sql .= ", entity";
173 $sql .= ", fk_soc";
174 $sql .= ", ref_customer";
175 $sql .= ", date_creation";
176 $sql .= ", fk_user_author";
177 $sql .= ", date_delivery";
178 $sql .= ", fk_address";
179 $sql .= ", note_private";
180 $sql .= ", note_public";
181 $sql .= ", model_pdf";
182 $sql .= ", fk_incoterms, location_incoterms";
183 $sql .= ") VALUES (";
184 $sql .= "'(PROV)'";
185 $sql .= ", ".((int) $conf->entity);
186 $sql .= ", ".((int) $this->socid);
187 $sql .= ", '".$this->db->escape($this->ref_customer)."'";
188 $sql .= ", '".$this->db->idate($now)."'";
189 $sql .= ", ".((int) $user->id);
190 $sql .= ", ".($this->date_delivery ? "'".$this->db->idate($this->date_delivery)."'" : "null");
191 $sql .= ", ".($this->fk_delivery_address > 0 ? $this->fk_delivery_address : "null");
192 $sql .= ", ".(!empty($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null");
193 $sql .= ", ".(!empty($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null");
194 $sql .= ", ".(!empty($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null");
195 $sql .= ", ".(int) $this->fk_incoterms;
196 $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
197 $sql .= ")";
198
199 dol_syslog("Delivery::create", LOG_DEBUG);
200 $resql = $this->db->query($sql);
201 if ($resql) {
202 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."delivery");
203
204 $numref = "(PROV".$this->id.")";
205
206 $sql = "UPDATE ".MAIN_DB_PREFIX."delivery ";
207 $sql .= "SET ref = '".$this->db->escape($numref)."'";
208 $sql .= " WHERE rowid = ".((int) $this->id);
209
210 dol_syslog("Delivery::create", LOG_DEBUG);
211 $resql = $this->db->query($sql);
212 if ($resql) {
213 if (!getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION')) {
214 $commande = new Commande($this->db);
215 $commande->id = $this->commande_id;
216 $commande->fetch_lines();
217 }
218
219
220 /*
221 * Inserting products into the database
222 */
223 $num = count($this->lines);
224 for ($i = 0; $i < $num; $i++) {
225 $origin_id = $this->lines[$i]->origin_line_id;
226 if (!$origin_id) {
227 $origin_id = $this->lines[$i]->commande_ligne_id; // For backward compatibility
228 }
229
230 if (!$this->create_line($origin_id, $this->lines[$i]->qty, $this->lines[$i]->fk_product, $this->lines[$i]->description, $this->lines[$i]->array_options)) {
231 $error++;
232 }
233 }
234
235 if (!$error && $this->id && $this->origin_id) {
236 $ret = $this->add_object_linked();
237 if (!$ret) {
238 $error++;
239 }
240
241 if (!getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION')) {
242 $ret = $this->setStatut(2, $this->origin_id, $this->origin);
243 if (!$ret) {
244 $error++;
245 }
246 }
247 }
248
249 if (!$error) {
250 $this->db->commit();
251 return $this->id;
252 } else {
253 $error++;
254 $this->error = $this->db->lasterror()." - sql=".$this->db->lastqueryerror;
255 $this->db->rollback();
256 return -3;
257 }
258 } else {
259 $error++;
260 $this->error = $this->db->lasterror()." - sql=".$this->db->lastqueryerror;
261 $this->db->rollback();
262 return -2;
263 }
264 } else {
265 $error++;
266 $this->error = $this->db->lasterror()." - sql=".$this->db->lastqueryerror;
267 $this->db->rollback();
268 return -1;
269 }
270 }
271
272 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
283 public function create_line($origin_id, $qty, $fk_product, $description, $array_options = null)
284 {
285 // phpcs:enable
286 $error = 0;
287 $idprod = $fk_product;
288
289 $sql = "INSERT INTO ".MAIN_DB_PREFIX."deliverydet (fk_delivery, fk_origin_line,";
290 $sql .= " fk_product, description, qty)";
291 $sql .= " VALUES (".$this->id.",".((int) $origin_id).",";
292 $sql .= " ".($idprod > 0 ? ((int) $idprod) : "null").",";
293 $sql .= " ".($description ? "'".$this->db->escape($description)."'" : "null").",";
294 $sql .= (price2num($qty, 'MS')).")";
295
296 dol_syslog(get_class($this)."::create_line", LOG_DEBUG);
297 if (!$this->db->query($sql)) {
298 $error++;
299 }
300
301 $id = $this->db->last_insert_id(MAIN_DB_PREFIX."deliverydet");
302
303 if (is_array($array_options) && count($array_options) > 0) {
304 $line = new DeliveryLine($this->db);
305 $line->id = $id;
306 $line->array_options = $array_options;
307 $result = $line->insertExtraFields();
308 }
309
310 if (!$error) {
311 return 1;
312 }
313
314 return -1;
315 }
316
323 public function fetch($id)
324 {
325 $sql = "SELECT l.rowid, l.fk_soc, l.date_creation, l.date_valid, l.ref, l.ref_customer, l.fk_user_author,";
326 $sql .= " l.total_ht, l.fk_statut, l.fk_user_valid, l.note_private, l.note_public";
327 $sql .= ", l.date_delivery, l.fk_address, l.model_pdf";
328 $sql .= ", el.fk_source as origin_id, el.sourcetype as origin";
329 $sql .= ', l.fk_incoterms, l.location_incoterms';
330 $sql .= ", i.libelle as label_incoterms";
331 $sql .= " FROM ".MAIN_DB_PREFIX."delivery as l";
332 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = l.rowid AND el.targettype = '".$this->db->escape($this->element)."'";
333 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON l.fk_incoterms = i.rowid';
334 $sql .= " WHERE l.rowid = ".((int) $id);
335
336 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
337 $result = $this->db->query($sql);
338 if ($result) {
339 if ($this->db->num_rows($result)) {
340 $obj = $this->db->fetch_object($result);
341
342 $this->id = $obj->rowid;
343 $this->date_delivery = $this->db->jdate($obj->date_delivery);
344 $this->date_creation = $this->db->jdate($obj->date_creation);
345 $this->date_valid = $this->db->jdate($obj->date_valid);
346 $this->ref = $obj->ref;
347 $this->ref_customer = $obj->ref_customer;
348 $this->socid = $obj->fk_soc;
349 $this->statut = $obj->fk_statut;
350 $this->status = $obj->fk_statut;
351 $this->user_author_id = $obj->fk_user_author;
352 $this->user_validation_id = $obj->fk_user_valid;
353 $this->fk_delivery_address = $obj->fk_address;
354 $this->note = $obj->note_private; //TODO deprecated
355 $this->note_private = $obj->note_private;
356 $this->note_public = $obj->note_public;
357 $this->model_pdf = $obj->model_pdf;
358 $this->origin = $obj->origin; // May be 'shipping'
359 $this->origin_id = $obj->origin_id; // May be id of shipping
360
361 //Incoterms
362 $this->fk_incoterms = $obj->fk_incoterms;
363 $this->location_incoterms = $obj->location_incoterms;
364 $this->label_incoterms = $obj->label_incoterms;
365 $this->db->free($result);
366
367 if ($this->statut == 0) {
368 $this->draft = 1;
369 }
370
371 // Retrieve all extrafields
372 // fetch optionals attributes and labels
373 $this->fetch_optionals();
374
375 // Load lines
376 $result = $this->fetch_lines();
377 if ($result < 0) {
378 return -3;
379 }
380
381 return 1;
382 } else {
383 $this->error = 'Delivery with id '.$id.' not found sql='.$sql;
384 dol_syslog(get_class($this).'::fetch Error '.$this->error, LOG_ERR);
385 return -2;
386 }
387 } else {
388 $this->error = $this->db->error();
389 return -1;
390 }
391 }
392
400 public function valid($user, $notrigger = 0)
401 {
402 global $conf;
403
404 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
405
406 dol_syslog(get_class($this)."::valid begin");
407
408 $this->db->begin();
409
410 $error = 0;
411
412 if ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('expedition', 'delivery', 'creer'))
413 || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('expedition', 'delivery_advance', 'validate'))) {
414 if (getDolGlobalString('DELIVERY_ADDON_NUMBER')) {
415 // Setting the command numbering module name
416 $modName = $conf->global->DELIVERY_ADDON_NUMBER;
417
418 if (is_readable(DOL_DOCUMENT_ROOT.'/core/modules/delivery/'.$modName.'.php')) {
419 require_once DOL_DOCUMENT_ROOT.'/core/modules/delivery/'.$modName.'.php';
420
421 $now = dol_now();
422
423 // Retrieving the new reference
424 $objMod = new $modName($this->db);
425 $soc = new Societe($this->db);
426 $soc->fetch($this->socid);
427
428 if (preg_match('/^[\‍(]?PROV/i', $this->ref) || empty($this->ref)) { // empty should not happened, but when it occurs, the test save life
429 $numref = $objMod->delivery_get_num($soc, $this);
430 } else {
431 $numref = $this->ref;
432 }
433 $this->newref = dol_sanitizeFileName($numref);
434
435 // Test if is not already in valid status. If so, we stop to avoid decrementing the stock twice.
436 $sql = "SELECT ref";
437 $sql .= " FROM ".MAIN_DB_PREFIX."delivery";
438 $sql .= " WHERE ref = '".$this->db->escape($numref)."'";
439 $sql .= " AND fk_statut <> 0";
440 $sql .= " AND entity = ".((int) $conf->entity);
441
442 $resql = $this->db->query($sql);
443 if ($resql) {
444 $num = $this->db->num_rows($resql);
445 if ($num > 0) {
446 return 0;
447 }
448 }
449
450 $sql = "UPDATE ".MAIN_DB_PREFIX."delivery SET";
451 $sql .= " ref='".$this->db->escape($numref)."'";
452 $sql .= ", fk_statut = 1";
453 $sql .= ", date_valid = '".$this->db->idate($now)."'";
454 $sql .= ", fk_user_valid = ".$user->id;
455 $sql .= " WHERE rowid = ".((int) $this->id);
456 $sql .= " AND fk_statut = 0";
457
458 $resql = $this->db->query($sql);
459 if (!$resql) {
460 dol_print_error($this->db);
461 $this->error = $this->db->lasterror();
462 $error++;
463 }
464
465 if (!$error && !$notrigger) {
466 // Call trigger
467 $result = $this->call_trigger('DELIVERY_VALIDATE', $user);
468 if ($result < 0) {
469 $error++;
470 }
471 // End call triggers
472 }
473
474 if (!$error) {
475 $this->oldref = $this->ref;
476
477 // Rename directory if dir was a temporary ref
478 if (preg_match('/^[\‍(]?PROV/i', $this->ref)) {
479 // Now we rename also files into index
480 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'expedition/receipt/".$this->db->escape($this->newref)."'";
481 $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'expedition/receipt/".$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
482 $resql = $this->db->query($sql);
483 if (!$resql) {
484 $error++;
485 $this->error = $this->db->lasterror();
486 }
487 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'expedition/receipt/".$this->db->escape($this->newref)."'";
488 $sql .= " WHERE filepath = 'expedition/receipt/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
489 $resql = $this->db->query($sql);
490 if (!$resql) {
491 $error++;
492 $this->error = $this->db->lasterror();
493 }
494
495 // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
496 $oldref = dol_sanitizeFileName($this->ref);
497 $newref = dol_sanitizeFileName($numref);
498 $dirsource = $conf->expedition->dir_output.'/receipt/'.$oldref;
499 $dirdest = $conf->expedition->dir_output.'/receipt/'.$newref;
500 if (!$error && file_exists($dirsource)) {
501 dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
502
503 if (@rename($dirsource, $dirdest)) {
504 dol_syslog("Rename ok");
505 // Rename docs starting with $oldref with $newref
506 $listoffiles = dol_dir_list($conf->expedition->dir_output.'/receipt/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
507 foreach ($listoffiles as $fileentry) {
508 $dirsource = $fileentry['name'];
509 $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
510 $dirsource = $fileentry['path'].'/'.$dirsource;
511 $dirdest = $fileentry['path'].'/'.$dirdest;
512 @rename($dirsource, $dirdest);
513 }
514 }
515 }
516 }
517
518 // Set new ref and current status
519 if (!$error) {
520 $this->ref = $numref;
521 $this->statut = 1;
522 }
523
524 dol_syslog(get_class($this)."::valid ok");
525 }
526
527 if (!$error) {
528 $this->db->commit();
529 return 1;
530 } else {
531 $this->db->rollback();
532 return -1;
533 }
534 }
535 }
536
537 return -1;
538 } else {
539 $this->error = "NotAllowed";
540 dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
541 return -1;
542 }
543 }
544
545 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
553 public function create_from_sending($user, $sending_id)
554 {
555 // phpcs:enable
556 global $conf;
557
558 $expedition = new Expedition($this->db);
559 $result = $expedition->fetch($sending_id);
560 if ($result <= 0) {
561 return $result;
562 }
563
564 $this->lines = array();
565
566 $num = count($expedition->lines);
567 for ($i = 0; $i < $num; $i++) {
568 $line = new DeliveryLine($this->db);
569 $line->origin_line_id = $expedition->lines[$i]->origin_line_id;
570 $line->label = $expedition->lines[$i]->label;
571 $line->description = $expedition->lines[$i]->description;
572 $line->qty = $expedition->lines[$i]->qty_shipped;
573 $line->fk_product = $expedition->lines[$i]->fk_product;
574 if (!getDolGlobalString('MAIN_EXTRAFIELDS_DISABLED') && is_array($expedition->lines[$i]->array_options) && count($expedition->lines[$i]->array_options) > 0) { // For avoid conflicts if trigger used
575 $line->array_options = $expedition->lines[$i]->array_options;
576 }
577 $this->lines[$i] = $line;
578 }
579
580 $this->origin = $expedition->element;
581 $this->origin_id = $expedition->id;
582 $this->note_private = $expedition->note_private;
583 $this->note_public = $expedition->note_public;
584 $this->fk_project = $expedition->fk_project;
585 $this->date_delivery = $expedition->date_delivery;
586 $this->fk_delivery_address = $expedition->fk_delivery_address;
587 $this->socid = $expedition->socid;
588 $this->ref_customer = $expedition->ref_customer;
589
590 //Incoterms
591 $this->fk_incoterms = $expedition->fk_incoterms;
592 $this->location_incoterms = $expedition->location_incoterms;
593
594 return $this->create($user);
595 }
596
597 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
605 public function update_line($id, $array_options = 0)
606 {
607 // phpcs:enable
608 global $conf;
609 $error = 0;
610
611 if ($id > 0 && !$error && !getDolGlobalString('MAIN_EXTRAFIELDS_DISABLED') && is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
612 $line = new DeliveryLine($this->db);
613 $line->array_options = $array_options;
614 $line->id = $id;
615 $result = $line->insertExtraFields();
616
617 if ($result < 0) {
618 $this->error[] = $line->error;
619 $error++;
620 }
621 }
622
623 if (!$error) {
624 return 1;
625 } else {
626 return -1;
627 }
628 }
629
630
639 public function addline($origin_id, $qty, $array_options = null)
640 {
641 global $conf;
642
643 $num = count($this->lines);
644 $line = new DeliveryLine($this->db);
645
646 $line->origin_id = $origin_id;
647 $line->qty = $qty;
648 if (!getDolGlobalString('MAIN_EXTRAFIELDS_DISABLED') && is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
649 $line->array_options = $array_options;
650 }
651 $this->lines[$num] = $line;
652 }
653
660 public function deleteline($lineid)
661 {
662 if ($this->statut == 0) {
663 $sql = "DELETE FROM ".MAIN_DB_PREFIX."commandedet";
664 $sql .= " WHERE rowid = ".((int) $lineid);
665
666 if ($this->db->query($sql)) {
667 $this->update_price(1);
668
669 return 1;
670 } else {
671 return -1;
672 }
673 }
674
675 return 0;
676 }
677
684 public function delete($user = null)
685 {
686 global $conf, $langs;
687
688 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
689
690 $this->db->begin();
691
692 $error = 0;
693
694 $sql = "DELETE FROM ".MAIN_DB_PREFIX."deliverydet";
695 $sql .= " WHERE fk_delivery = ".((int) $this->id);
696 if ($this->db->query($sql)) {
697 // Delete linked object
698 $res = $this->deleteObjectLinked();
699 if ($res < 0) {
700 $error++;
701 }
702
703 if (!$error) {
704 $sql = "DELETE FROM ".MAIN_DB_PREFIX."delivery";
705 $sql .= " WHERE rowid = ".((int) $this->id);
706 if ($this->db->query($sql)) {
707 $this->db->commit();
708
709 // Deleting pdf folder's draft On efface le repertoire de pdf provisoire
710 $ref = dol_sanitizeFileName($this->ref);
711 if (!empty($conf->expedition->dir_output)) {
712 $dir = $conf->expedition->dir_output.'/receipt/'.$ref;
713 $file = $dir.'/'.$ref.'.pdf';
714 if (file_exists($file)) {
715 if (!dol_delete_file($file)) {
716 return 0;
717 }
718 }
719 if (file_exists($dir)) {
720 if (!dol_delete_dir($dir)) {
721 $this->error = $langs->trans("ErrorCanNotDeleteDir", $dir);
722 return 0;
723 }
724 }
725 }
726
727 // Call trigger
728 $result = $this->call_trigger('DELIVERY_DELETE', $user);
729 if ($result < 0) {
730 $this->db->rollback();
731 return -4;
732 }
733 // End call triggers
734
735 return 1;
736 } else {
737 $this->error = $this->db->lasterror()." - sql=$sql";
738 $this->db->rollback();
739 return -3;
740 }
741 } else {
742 $this->error = $this->db->lasterror()." - sql=$sql";
743 $this->db->rollback();
744 return -2;
745 }
746 } else {
747 $this->error = $this->db->lasterror()." - sql=$sql";
748 $this->db->rollback();
749 return -1;
750 }
751 }
752
759 public function getTooltipContentArray($params)
760 {
761 global $langs;
762
763 $langs->load('deliveries');
764
765 $datas = [];
766
767 $datas['picto'] = img_picto('', $this->picto).' <u>'.$langs->trans("ShowReceiving").'</u>:<br>';
768 $datas['picto'] .= '<b>'.$langs->trans("Status").'</b>: '.$this->ref;
769
770 return $datas;
771 }
772
780 public function getNomUrl($withpicto = 0, $save_lastsearch_value = -1)
781 {
782 global $langs, $hookmanager;
783
784 $result = '';
785
786 $params = [
787 'id' => $this->id,
788 'objecttype' => $this->element,
789 ];
790 $classfortooltip = 'classfortooltip';
791 $dataparams = '';
792 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
793 $classfortooltip = 'classforajaxtooltip';
794 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
795 $label = '';
796 } else {
797 $label = implode($this->getTooltipContentArray($params));
798 }
799
800 $url = DOL_URL_ROOT.'/delivery/card.php?id='.$this->id;
801
802 //if ($option !== 'nolink')
803 //{
804 // Add param to save lastsearch_values or not
805 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
806 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
807 $add_save_lastsearch_values = 1;
808 }
809 if ($add_save_lastsearch_values) {
810 $url .= '&save_lastsearch_values=1';
811 }
812 //}
813
814 $linkstart = '<a href="'.$url.'"';
815 $linkstart .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
816 $linkstart .= $dataparams.' class="'.$classfortooltip.'">';
817 $linkend = '</a>';
818
819 if ($withpicto) {
820 $result .= ($linkstart.img_object($label, $this->picto, $dataparams.' class="'.$classfortooltip.'"').$linkend);
821 }
822 if ($withpicto && $withpicto != 2) {
823 $result .= ' ';
824 }
825 $result .= $linkstart.$this->ref.$linkend;
826
827 global $action;
828 $hookmanager->initHooks(array($this->element . 'dao'));
829 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
830 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
831 if ($reshook > 0) {
832 $result = $hookmanager->resPrint;
833 } else {
834 $result .= $hookmanager->resPrint;
835 }
836 return $result;
837 }
838
839 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
845 public function fetch_lines()
846 {
847 // phpcs:enable
848 $this->lines = array();
849
850 $sql = "SELECT ld.rowid, ld.fk_product, ld.description, ld.subprice, ld.total_ht, ld.qty as qty_shipped, ld.fk_origin_line, ";
851 $sql .= " cd.qty as qty_asked, cd.label as custom_label, cd.fk_unit,";
852 $sql .= " p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc,";
853 $sql .= " p.weight, p.weight_units, p.width, p.width_units, p.length, p.length_units, p.height, p.height_units, p.surface, p.surface_units, p.volume, p.volume_units, p.tobatch as product_tobatch";
854 $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."deliverydet as ld";
855 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on p.rowid = ld.fk_product";
856 $sql .= " WHERE ld.fk_origin_line = cd.rowid";
857 $sql .= " AND ld.fk_delivery = ".((int) $this->id);
858
859 dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
860 $resql = $this->db->query($sql);
861 if ($resql) {
862 $num = $this->db->num_rows($resql);
863 $i = 0;
864 while ($i < $num) {
865 $obj = $this->db->fetch_object($resql);
866
867 $line = new DeliveryLine($this->db);
868
869 $line->id = $obj->rowid;
870 $line->label = $obj->custom_label;
871 $line->description = $obj->description;
872 $line->fk_product = $obj->fk_product;
873 $line->qty_asked = $obj->qty_asked;
874 $line->qty_shipped = $obj->qty_shipped;
875
876 $line->product_label = $obj->product_label; // Product label
877 $line->product_ref = $obj->product_ref; // Product ref
878 $line->product_desc = $obj->product_desc; // Product description
879 $line->product_type = $obj->fk_product_type;
880
881 $line->fk_origin_line = $obj->fk_origin_line;
882
883 $line->price = $obj->subprice;
884 $line->total_ht = $obj->total_ht;
885
886 // units
887 $line->weight = $obj->weight;
888 $line->weight_units = $obj->weight_units;
889 $line->width = $obj->width;
890 $line->width_units = $obj->width_units;
891 $line->height = $obj->height;
892 $line->height_units = $obj->height_units;
893 $line->length = $obj->length;
894 $line->length_units = $obj->length_units;
895 $line->surface = $obj->surface;
896 $line->surface_units = $obj->surface_units;
897 $line->volume = $obj->volume;
898 $line->volume_units = $obj->volume_units;
899
900 $line->fk_unit = $obj->fk_unit;
901 $line->fetch_optionals();
902
903 $this->lines[$i] = $line;
904
905 $i++;
906 }
907 $this->db->free($resql);
908
909 return 1;
910 } else {
911 return -1;
912 }
913 }
914
915
922 public function getLibStatut($mode = 0)
923 {
924 return $this->LibStatut($this->statut, $mode);
925 }
926
927 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
935 public function LibStatut($status, $mode)
936 {
937 // phpcs:enable
938 global $langs;
939
940 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
941 global $langs;
942 //$langs->load("mymodule");
943 $this->labelStatus[-1] = $langs->transnoentitiesnoconv('StatusDeliveryCanceled');
944 $this->labelStatus[0] = $langs->transnoentitiesnoconv('StatusDeliveryDraft');
945 $this->labelStatus[1] = $langs->transnoentitiesnoconv('StatusDeliveryValidated');
946 $this->labelStatusShort[-1] = $langs->transnoentitiesnoconv('StatusDeliveryCanceled');
947 $this->labelStatusShort[0] = $langs->transnoentitiesnoconv('StatusDeliveryDraft');
948 $this->labelStatusShort[1] = $langs->transnoentitiesnoconv('StatusDeliveryValidated');
949 }
950
951 $statusType = 'status0';
952 if ($status == -1) {
953 $statusType = 'status5';
954 }
955 if ($status == 1) {
956 $statusType = 'status4';
957 }
958
959 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
960 }
961
962
970 public function initAsSpecimen()
971 {
972 $now = dol_now();
973
974 // Load array of products prodids
975 $num_prods = 0;
976 $prodids = array();
977 $sql = "SELECT rowid";
978 $sql .= " FROM ".MAIN_DB_PREFIX."product";
979 $sql .= " WHERE entity IN (".getEntity('product').")";
980 $sql .= " AND tosell = 1";
981 $sql .= $this->db->plimit(100);
982
983 $resql = $this->db->query($sql);
984 if ($resql) {
985 $num_prods = $this->db->num_rows($resql);
986 $i = 0;
987 while ($i < $num_prods) {
988 $i++;
989 $row = $this->db->fetch_row($resql);
990 $prodids[$i] = $row[0];
991 }
992 }
993
994 // Initialise parametres
995 $this->id = 0;
996 $this->ref = 'SPECIMEN';
997 $this->specimen = 1;
998 $this->socid = 1;
999 $this->date_delivery = $now;
1000 $this->note_public = 'Public note';
1001 $this->note_private = 'Private note';
1002
1003 $i = 0;
1004 $line = new DeliveryLine($this->db);
1005 $line->fk_product = reset($prodids);
1006 $line->qty_asked = 10;
1007 $line->qty_shipped = 9;
1008 $line->ref = 'REFPROD';
1009 $line->label = 'Specimen';
1010 $line->description = 'Description';
1011 $line->price = 100;
1012 $line->total_ht = 100;
1013
1014 $this->lines[$i] = $line;
1015 }
1016
1023 public function getRemainingDelivered()
1024 {
1025 // Get the linked object
1026 $this->fetchObjectLinked('', '', $this->id, $this->element);
1027 //var_dump($this->linkedObjectsIds);
1028 // Get the product ref and qty in source
1029 $sqlSourceLine = "SELECT st.rowid, st.description, st.qty";
1030 $sqlSourceLine .= ", p.ref, p.label";
1031 $sqlSourceLine .= " FROM ".MAIN_DB_PREFIX.$this->linkedObjectsIds[0]['type']."det as st";
1032 $sqlSourceLine .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON st.fk_product = p.rowid";
1033 $sqlSourceLine .= " WHERE fk_".$this->linked_objects[0]['type']." = ".((int) $this->linked_objects[0]['linkid']);
1034
1035 $resultSourceLine = $this->db->query($sqlSourceLine);
1036 if ($resultSourceLine) {
1037 $num_lines = $this->db->num_rows($resultSourceLine);
1038 $i = 0;
1039 $array = array();
1040 while ($i < $num_lines) {
1041 $objSourceLine = $this->db->fetch_object($resultSourceLine);
1042
1043 // Get lines of sources alread delivered
1044 $sql = "SELECT ld.fk_origin_line, sum(ld.qty) as qty";
1045 $sql .= " FROM ".MAIN_DB_PREFIX."deliverydet as ld, ".MAIN_DB_PREFIX."delivery as l,";
1046 $sql .= " ".MAIN_DB_PREFIX.$this->linked_objects[0]['type']." as c";
1047 $sql .= ", ".MAIN_DB_PREFIX.$this->linked_objects[0]['type']."det as cd";
1048 $sql .= " WHERE ld.fk_delivery = l.rowid";
1049 $sql .= " AND ld.fk_origin_line = cd.rowid";
1050 $sql .= " AND cd.fk_".$this->linked_objects[0]['type']." = c.rowid";
1051 $sql .= " AND cd.fk_".$this->linked_objects[0]['type']." = ".((int) $this->linked_objects[0]['linkid']);
1052 $sql .= " AND ld.fk_origin_line = ".((int) $objSourceLine->rowid);
1053 $sql .= " GROUP BY ld.fk_origin_line";
1054
1055 $result = $this->db->query($sql);
1056 $row = $this->db->fetch_row($result);
1057
1058 if ($objSourceLine->qty - $row[1] > 0) {
1059 if ($row[0] == $objSourceLine->rowid) {
1060 $array[$i]['qty'] = $objSourceLine->qty - $row[1];
1061 } else {
1062 $array[$i]['qty'] = $objSourceLine->qty;
1063 }
1064
1065 $array[$i]['ref'] = $objSourceLine->ref;
1066 $array[$i]['label'] = $objSourceLine->label ? $objSourceLine->label : $objSourceLine->description;
1067 } elseif ($objSourceLine->qty - $row[1] < 0) {
1068 $array[$i]['qty'] = $objSourceLine->qty - $row[1]." Erreur livraison !";
1069 $array[$i]['ref'] = $objSourceLine->ref;
1070 $array[$i]['label'] = $objSourceLine->label ? $objSourceLine->label : $objSourceLine->description;
1071 }
1072
1073 $i++;
1074 }
1075 return $array;
1076 } else {
1077 $this->error = $this->db->error()." - sql=$sqlSourceLine";
1078 return -1;
1079 }
1080 }
1081
1089 public function setDeliveryDate($user, $delivery_date)
1090 {
1091 if ($user->hasRight('expedition', 'creer')) {
1092 $sql = "UPDATE ".MAIN_DB_PREFIX."delivery";
1093 $sql .= " SET date_delivery = ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : 'null');
1094 $sql .= " WHERE rowid = ".((int) $this->id);
1095
1096 dol_syslog(get_class($this)."::setDeliveryDate", LOG_DEBUG);
1097 $resql = $this->db->query($sql);
1098 if ($resql) {
1099 $this->date_delivery = $delivery_date;
1100 return 1;
1101 } else {
1102 $this->error = $this->db->error();
1103 return -1;
1104 }
1105 } else {
1106 return -2;
1107 }
1108 }
1109
1120 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1121 {
1122 global $conf, $langs;
1123
1124 $langs->load("deliveries");
1125 $outputlangs->load("products");
1126
1127 if (!dol_strlen($modele)) {
1128 $modele = 'typhon';
1129
1130 if ($this->model_pdf) {
1131 $modele = $this->model_pdf;
1132 } elseif (getDolGlobalString('DELIVERY_ADDON_PDF')) {
1133 $modele = $conf->global->DELIVERY_ADDON_PDF;
1134 }
1135 }
1136
1137 $modelpath = "core/modules/delivery/doc/";
1138
1139 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1140 }
1141
1150 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1151 {
1152 $tables = array(
1153 'delivery'
1154 );
1155
1156 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1157 }
1158
1167 public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
1168 {
1169 $tables = array(
1170 'deliverydet'
1171 );
1172
1173 return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
1174 }
1175}
1176
1177
1182{
1186 public $db;
1187
1191 public $element = 'deliverydet';
1192
1196 public $table_element = 'deliverydet';
1197
1201 public $label;
1202
1206 public $description;
1207
1212 public $ref;
1217 public $libelle;
1218
1219 // From llx_expeditiondet
1220 public $qty;
1221 public $qty_asked;
1222 public $qty_shipped;
1223
1224 public $fk_product;
1225 public $product_desc;
1226 public $product_type;
1227 public $product_ref;
1228 public $product_label;
1229
1230 public $price;
1231
1232 public $fk_origin_line;
1233 public $origin_id;
1234
1238 public $origin_line_id;
1239
1245 public $commande_ligne_id;
1246
1247
1253 public function __construct($db)
1254 {
1255 $this->db = $db;
1256 }
1257}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Definition security.php:604
$object ref
Definition info.php:79
Class to manage customers orders.
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...
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.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
setStatut($status, $elementId=null, $elementType='', $trigkey='', $fieldstatus='fk_statut')
Set status of an object.
update_price($exclspec=0, $roundingadjust='none', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
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).
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage receptions.
valid($user, $notrigger=0)
Validate object and update stock if option enabled.
addline($origin_id, $qty, $array_options=null)
Add line.
__construct($db)
Constructor.
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
setDeliveryDate($user, $delivery_date)
Set the planned delivery date.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create object on disk.
fetch($id)
Load a delivery receipt.
create($user)
Create delivery receipt in database.
create_line($origin_id, $qty, $fk_product, $description, $array_options=null)
Create a line.
initAsSpecimen()
Initialise an instance with random values.
create_from_sending($user, $sending_id)
Creating the delivery slip from an existing shipment.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
getTooltipContentArray($params)
getTooltipContentArray
getLibStatut($mode=0)
Return the label of the status.
LibStatut($status, $mode)
Return the label of a given status.
update_line($id, $array_options=0)
Update a livraison line (only extrafields)
deleteline($lineid)
Delete line.
fetch_lines()
Load lines insto $this->lines.
getRemainingDelivered()
Renvoie la quantite de produit restante a livrer pour une commande.
getNomUrl($withpicto=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Management class of delivery note lines.
__construct($db)
Constructor.
Class to manage Dolibarr database access.
Class to manage shipments.
Class to manage third parties objects (customers, suppliers, prospects...)
trait CommonIncoterm
Superclass for incoterm classes.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
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($dir, $nophperrors=0)
Remove a directory (not recursive, so content must be empty).
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:62
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e e e e e statut
Definition invoice.php:1926
$conf db user
Definition repair.php:125