dolibarr 21.0.0-alpha
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-2024 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
32require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
33require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
35require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
36if (isModEnabled("propal")) {
37 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
38}
39if (isModEnabled('order')) {
40 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
41}
42
43
48{
50
54 public $element = "delivery";
55
59 public $fk_element = "fk_delivery";
60
64 public $table_element = "delivery";
65
69 public $table_element_line = "deliverydet";
70
74 public $picto = 'sending';
75
79 public $draft;
80
84 public $socid;
85
89 public $ref_customer;
90
94 public $date_delivery;
95
99 public $date_valid;
100
104 public $model_pdf;
105
109 public $commande_id;
110
114 public $lines = array();
115
119 public $user_author_id;
120
121
127 public function __construct($db)
128 {
129 $this->db = $db;
130
131 // List of short language codes for status
132 $this->labelStatus[-1] = 'StatusDeliveryCanceled';
133 $this->labelStatus[0] = 'StatusDeliveryDraft';
134 $this->labelStatus[1] = 'StatusDeliveryValidated';
135 }
136
143 public function create($user)
144 {
145 global $conf;
146
147 dol_syslog("Delivery::create");
148
149 if (empty($this->model_pdf)) {
150 $this->model_pdf = getDolGlobalString('DELIVERY_ADDON_PDF');
151 }
152
153 $error = 0;
154
155 $now = dol_now();
156
157 /* Delivery note as draft On positionne en mode draft le bon de livraison */
158 $this->draft = 1;
159
160 $this->user = $user;
161
162 $this->db->begin();
163
164 $sql = "INSERT INTO ".MAIN_DB_PREFIX."delivery (";
165 $sql .= "ref";
166 $sql .= ", entity";
167 $sql .= ", fk_soc";
168 $sql .= ", ref_customer";
169 $sql .= ", date_creation";
170 $sql .= ", fk_user_author";
171 $sql .= ", date_delivery";
172 $sql .= ", fk_address";
173 $sql .= ", note_private";
174 $sql .= ", note_public";
175 $sql .= ", model_pdf";
176 $sql .= ", fk_incoterms, location_incoterms";
177 $sql .= ") VALUES (";
178 $sql .= "'(PROV)'";
179 $sql .= ", ".((int) $conf->entity);
180 $sql .= ", ".((int) $this->socid);
181 $sql .= ", '".$this->db->escape($this->ref_customer)."'";
182 $sql .= ", '".$this->db->idate($now)."'";
183 $sql .= ", ".((int) $user->id);
184 $sql .= ", ".($this->date_delivery ? "'".$this->db->idate($this->date_delivery)."'" : "null");
185 $sql .= ", ".($this->fk_delivery_address > 0 ? $this->fk_delivery_address : "null");
186 $sql .= ", ".(!empty($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null");
187 $sql .= ", ".(!empty($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null");
188 $sql .= ", ".(!empty($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null");
189 $sql .= ", ".(int) $this->fk_incoterms;
190 $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
191 $sql .= ")";
192
193 dol_syslog("Delivery::create", LOG_DEBUG);
194 $resql = $this->db->query($sql);
195 if ($resql) {
196 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."delivery");
197
198 $numref = "(PROV".$this->id.")";
199
200 $sql = "UPDATE ".MAIN_DB_PREFIX."delivery ";
201 $sql .= "SET ref = '".$this->db->escape($numref)."'";
202 $sql .= " WHERE rowid = ".((int) $this->id);
203
204 dol_syslog("Delivery::create", LOG_DEBUG);
205 $resql = $this->db->query($sql);
206 if ($resql) {
207 if (!getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION')) {
208 $commande = new Commande($this->db);
209 $commande->id = $this->commande_id;
210 $commande->fetch_lines();
211 }
212
213
214 /*
215 * Inserting products into the database
216 */
217 $num = count($this->lines);
218 for ($i = 0; $i < $num; $i++) {
219 $origin_id = $this->lines[$i]->origin_line_id;
220 if (!$origin_id) {
221 $origin_id = $this->lines[$i]->commande_ligne_id; // For backward compatibility
222 }
223
224 if (!$this->create_line($origin_id, $this->lines[$i]->qty, $this->lines[$i]->fk_product, $this->lines[$i]->description, $this->lines[$i]->array_options)) {
225 $error++;
226 }
227 }
228
229 if (!$error && $this->id && $this->origin_id) {
230 $ret = $this->add_object_linked();
231 if (!$ret) {
232 $error++;
233 }
234
235 if (!getDolGlobalInt('MAIN_SUBMODULE_EXPEDITION')) {
236 $ret = $this->setStatut(2, $this->origin_id, $this->origin);
237 if (!$ret) {
238 $error++;
239 }
240 }
241 }
242
243 if (!$error) {
244 $this->db->commit();
245 return $this->id;
246 } else {
247 $error++;
248 $this->error = $this->db->lasterror()." - sql=".$this->db->lastqueryerror;
249 $this->db->rollback();
250 return -3;
251 }
252 } else {
253 $error++;
254 $this->error = $this->db->lasterror()." - sql=".$this->db->lastqueryerror;
255 $this->db->rollback();
256 return -2;
257 }
258 } else {
259 $error++;
260 $this->error = $this->db->lasterror()." - sql=".$this->db->lastqueryerror;
261 $this->db->rollback();
262 return -1;
263 }
264 }
265
266 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
277 public function create_line($origin_id, $qty, $fk_product, $description, $array_options = [])
278 {
279 // phpcs:enable
280 $error = 0;
281 $idprod = $fk_product;
282
283 $sql = "INSERT INTO ".MAIN_DB_PREFIX."deliverydet (fk_delivery, fk_origin_line,";
284 $sql .= " fk_product, description, qty)";
285 $sql .= " VALUES (".$this->id.",".((int) $origin_id).",";
286 $sql .= " ".($idprod > 0 ? ((int) $idprod) : "null").",";
287 $sql .= " ".($description ? "'".$this->db->escape($description)."'" : "null").",";
288 $sql .= (price2num($qty, 'MS')).")";
289
290 dol_syslog(get_class($this)."::create_line", LOG_DEBUG);
291 if (!$this->db->query($sql)) {
292 $error++;
293 }
294
295 $id = $this->db->last_insert_id(MAIN_DB_PREFIX."deliverydet");
296
297 if (is_array($array_options) && count($array_options) > 0) {
298 $line = new DeliveryLine($this->db);
299 $line->id = $id;
300 $line->array_options = $array_options;
301 $result = $line->insertExtraFields();
302 }
303
304 if (!$error) {
305 return 1;
306 }
307
308 return -1;
309 }
310
317 public function fetch($id)
318 {
319 $sql = "SELECT l.rowid, l.fk_soc, l.date_creation, l.date_valid, l.ref, l.ref_customer, l.fk_user_author,";
320 $sql .= " l.total_ht, l.fk_statut, l.fk_user_valid, l.note_private, l.note_public";
321 $sql .= ", l.date_delivery, l.fk_address, l.model_pdf";
322 $sql .= ", el.fk_source as origin_id, el.sourcetype as origin";
323 $sql .= ', l.fk_incoterms, l.location_incoterms';
324 $sql .= ", i.libelle as label_incoterms";
325 $sql .= " FROM ".MAIN_DB_PREFIX."delivery as l";
326 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = l.rowid AND el.targettype = '".$this->db->escape($this->element)."'";
327 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON l.fk_incoterms = i.rowid';
328 $sql .= " WHERE l.rowid = ".((int) $id);
329
330 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
331 $result = $this->db->query($sql);
332 if ($result) {
333 if ($this->db->num_rows($result)) {
334 $obj = $this->db->fetch_object($result);
335
336 $this->id = $obj->rowid;
337 $this->date_delivery = $this->db->jdate($obj->date_delivery);
338 $this->date_creation = $this->db->jdate($obj->date_creation);
339 $this->date_valid = $this->db->jdate($obj->date_valid);
340 $this->ref = $obj->ref;
341 $this->ref_customer = $obj->ref_customer;
342 $this->socid = $obj->fk_soc;
343 $this->statut = $obj->fk_statut;
344 $this->status = $obj->fk_statut;
345 $this->user_author_id = $obj->fk_user_author;
346 $this->user_validation_id = $obj->fk_user_valid;
347 $this->fk_delivery_address = $obj->fk_address;
348 $this->note = $obj->note_private; //TODO deprecated
349 $this->note_private = $obj->note_private;
350 $this->note_public = $obj->note_public;
351 $this->model_pdf = $obj->model_pdf;
352 $this->origin = $obj->origin; // May be 'shipping'
353 $this->origin_id = $obj->origin_id; // May be id of shipping
354
355 //Incoterms
356 $this->fk_incoterms = $obj->fk_incoterms;
357 $this->location_incoterms = $obj->location_incoterms;
358 $this->label_incoterms = $obj->label_incoterms;
359 $this->db->free($result);
360
361 if ($this->status == 0) {
362 $this->draft = 1;
363 }
364
365 // Retrieve all extrafields
366 // fetch optionals attributes and labels
367 $this->fetch_optionals();
368
369 // Load lines
370 $result = $this->fetch_lines();
371 if ($result < 0) {
372 return -3;
373 }
374
375 return 1;
376 } else {
377 $this->error = 'Delivery with id '.$id.' not found sql='.$sql;
378 dol_syslog(get_class($this).'::fetch Error '.$this->error, LOG_ERR);
379 return -2;
380 }
381 } else {
382 $this->error = $this->db->error();
383 return -1;
384 }
385 }
386
394 public function valid($user, $notrigger = 0)
395 {
396 global $conf;
397
398 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
399
400 dol_syslog(get_class($this)."::valid begin");
401
402 $this->db->begin();
403
404 $error = 0;
405
406 if ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('expedition', 'delivery', 'creer'))
407 || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('expedition', 'delivery_advance', 'validate'))) {
408 if (getDolGlobalString('DELIVERY_ADDON_NUMBER')) {
409 // Setting the command numbering module name
410 $modName = getDolGlobalString('DELIVERY_ADDON_NUMBER');
411
412 if (is_readable(DOL_DOCUMENT_ROOT.'/core/modules/delivery/'.$modName.'.php')) {
413 require_once DOL_DOCUMENT_ROOT.'/core/modules/delivery/'.$modName.'.php';
414
415 $now = dol_now();
416
417 // Retrieving the new reference
418 $objMod = new $modName($this->db);
419 $soc = new Societe($this->db);
420 $soc->fetch($this->socid);
421
422 if (preg_match('/^[\‍(]?PROV/i', $this->ref) || empty($this->ref)) { // empty should not happened, but when it occurs, the test save life
423 $numref = $objMod->getNextValue($soc, $this);
424 } else {
425 $numref = $this->ref;
426 }
427 $this->newref = dol_sanitizeFileName($numref);
428
429 // Test if is not already in valid status. If so, we stop to avoid decrementing the stock twice.
430 $sql = "SELECT ref";
431 $sql .= " FROM ".MAIN_DB_PREFIX."delivery";
432 $sql .= " WHERE ref = '".$this->db->escape($numref)."'";
433 $sql .= " AND fk_statut <> 0";
434 $sql .= " AND entity = ".((int) $conf->entity);
435
436 $resql = $this->db->query($sql);
437 if ($resql) {
438 $num = $this->db->num_rows($resql);
439 if ($num > 0) {
440 return 0;
441 }
442 }
443
444 $sql = "UPDATE ".MAIN_DB_PREFIX."delivery SET";
445 $sql .= " ref='".$this->db->escape($numref)."'";
446 $sql .= ", fk_statut = 1";
447 $sql .= ", date_valid = '".$this->db->idate($now)."'";
448 $sql .= ", fk_user_valid = ".$user->id;
449 $sql .= " WHERE rowid = ".((int) $this->id);
450 $sql .= " AND fk_statut = 0";
451
452 $resql = $this->db->query($sql);
453 if (!$resql) {
454 dol_print_error($this->db);
455 $this->error = $this->db->lasterror();
456 $error++;
457 }
458
459 if (!$error && !$notrigger) {
460 // Call trigger
461 $result = $this->call_trigger('DELIVERY_VALIDATE', $user);
462 if ($result < 0) {
463 $error++;
464 }
465 // End call triggers
466 }
467
468 if (!$error) {
469 $this->oldref = $this->ref;
470
471 // Rename directory if dir was a temporary ref
472 if (preg_match('/^[\‍(]?PROV/i', $this->ref)) {
473 // Now we rename also files into index
474 $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)."'";
475 $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'expedition/receipt/".$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
476 $resql = $this->db->query($sql);
477 if (!$resql) {
478 $error++;
479 $this->error = $this->db->lasterror();
480 }
481 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'expedition/receipt/".$this->db->escape($this->newref)."'";
482 $sql .= " WHERE filepath = 'expedition/receipt/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
483 $resql = $this->db->query($sql);
484 if (!$resql) {
485 $error++;
486 $this->error = $this->db->lasterror();
487 }
488
489 // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
490 $oldref = dol_sanitizeFileName($this->ref);
491 $newref = dol_sanitizeFileName($numref);
492 $dirsource = $conf->expedition->dir_output.'/receipt/'.$oldref;
493 $dirdest = $conf->expedition->dir_output.'/receipt/'.$newref;
494 if (!$error && file_exists($dirsource)) {
495 dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
496
497 if (@rename($dirsource, $dirdest)) {
498 dol_syslog("Rename ok");
499 // Rename docs starting with $oldref with $newref
500 $listoffiles = dol_dir_list($conf->expedition->dir_output.'/receipt/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
501 foreach ($listoffiles as $fileentry) {
502 $dirsource = $fileentry['name'];
503 $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
504 $dirsource = $fileentry['path'].'/'.$dirsource;
505 $dirdest = $fileentry['path'].'/'.$dirdest;
506 @rename($dirsource, $dirdest);
507 }
508 }
509 }
510 }
511
512 // Set new ref and current status
513 if (!$error) {
514 $this->ref = $numref;
515 $this->status = 1;
516 }
517
518 dol_syslog(get_class($this)."::valid ok");
519 }
520
521 if (!$error) {
522 $this->db->commit();
523 return 1;
524 } else {
525 $this->db->rollback();
526 return -1;
527 }
528 }
529 }
530
531 return -1;
532 } else {
533 $this->error = "NotAllowed";
534 dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
535 return -1;
536 }
537 }
538
539 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
547 public function create_from_sending($user, $sending_id)
548 {
549 // phpcs:enable
550 global $conf;
551
552 $expedition = new Expedition($this->db);
553 $result = $expedition->fetch($sending_id);
554 if ($result <= 0) {
555 return $result;
556 }
557
558 $this->lines = array();
559
560 $num = count($expedition->lines);
561 for ($i = 0; $i < $num; $i++) {
562 $line = new DeliveryLine($this->db);
563 $line->origin_line_id = $expedition->lines[$i]->origin_line_id;
564 $line->label = $expedition->lines[$i]->label;
565 $line->description = $expedition->lines[$i]->description;
566 $line->qty = $expedition->lines[$i]->qty_shipped;
567 $line->fk_product = $expedition->lines[$i]->fk_product;
568 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
569 $line->array_options = $expedition->lines[$i]->array_options;
570 }
571 $this->lines[$i] = $line;
572 }
573
574 $this->origin = $expedition->element;
575 $this->origin_id = $expedition->id;
576 $this->note_private = $expedition->note_private;
577 $this->note_public = $expedition->note_public;
578 $this->fk_project = $expedition->fk_project;
579 $this->date_delivery = $expedition->date_delivery;
580 $this->fk_delivery_address = $expedition->fk_delivery_address;
581 $this->socid = $expedition->socid;
582 $this->ref_customer = $expedition->ref_customer;
583
584 //Incoterms
585 $this->fk_incoterms = $expedition->fk_incoterms;
586 $this->location_incoterms = $expedition->location_incoterms;
587
588 return $this->create($user);
589 }
590
591 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
599 public function update_line($id, $array_options = [])
600 {
601 // phpcs:enable
602 global $conf;
603 $error = 0;
604
605 if ($id > 0 && !$error && !getDolGlobalString('MAIN_EXTRAFIELDS_DISABLED') && is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
606 $line = new DeliveryLine($this->db);
607 $line->array_options = $array_options;
608 $line->id = $id;
609 $result = $line->insertExtraFields();
610
611 if ($result < 0) {
612 $this->errors[] = $line->error;
613 $error++;
614 }
615 }
616
617 if (!$error) {
618 return 1;
619 } else {
620 return -1;
621 }
622 }
623
624
633 public function addline($origin_id, $qty, $array_options = [])
634 {
635 global $conf;
636
637 $num = count($this->lines);
638 $line = new DeliveryLine($this->db);
639
640 $line->origin_id = $origin_id;
641 $line->qty = $qty;
642 if (!getDolGlobalString('MAIN_EXTRAFIELDS_DISABLED') && is_array($array_options) && count($array_options) > 0) { // For avoid conflicts if trigger used
643 $line->array_options = $array_options;
644 }
645 $this->lines[$num] = $line;
646 }
647
654 public function deleteLine($lineid)
655 {
656 if ($this->status == 0) {
657 $sql = "DELETE FROM ".MAIN_DB_PREFIX."commandedet";
658 $sql .= " WHERE rowid = ".((int) $lineid);
659
660 if ($this->db->query($sql)) {
661 $this->update_price(1);
662
663 return 1;
664 } else {
665 return -1;
666 }
667 }
668
669 return 0;
670 }
671
678 public function delete($user = null)
679 {
680 global $conf, $langs;
681
682 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
683
684 $this->db->begin();
685
686 $error = 0;
687
688 $sql = "DELETE FROM ".MAIN_DB_PREFIX."deliverydet";
689 $sql .= " WHERE fk_delivery = ".((int) $this->id);
690 if ($this->db->query($sql)) {
691 // Delete linked object
692 $res = $this->deleteObjectLinked();
693 if ($res < 0) {
694 $error++;
695 }
696
697 if (!$error) {
698 $sql = "DELETE FROM ".MAIN_DB_PREFIX."delivery";
699 $sql .= " WHERE rowid = ".((int) $this->id);
700 if ($this->db->query($sql)) {
701 $this->db->commit();
702
703 // Deleting pdf folder's draft On efface le repertoire de pdf provisoire
704 $ref = dol_sanitizeFileName($this->ref);
705 if (!empty($conf->expedition->dir_output)) {
706 $dir = $conf->expedition->dir_output.'/receipt/'.$ref;
707 $file = $dir.'/'.$ref.'.pdf';
708 if (file_exists($file)) {
709 if (!dol_delete_file($file)) {
710 return 0;
711 }
712 }
713 if (file_exists($dir)) {
714 if (!dol_delete_dir($dir)) {
715 $this->error = $langs->trans("ErrorCanNotDeleteDir", $dir);
716 return 0;
717 }
718 }
719 }
720
721 // Call trigger
722 $result = $this->call_trigger('DELIVERY_DELETE', $user);
723 if ($result < 0) {
724 $this->db->rollback();
725 return -4;
726 }
727 // End call triggers
728
729 return 1;
730 } else {
731 $this->error = $this->db->lasterror()." - sql=$sql";
732 $this->db->rollback();
733 return -3;
734 }
735 } else {
736 $this->error = $this->db->lasterror()." - sql=$sql";
737 $this->db->rollback();
738 return -2;
739 }
740 } else {
741 $this->error = $this->db->lasterror()." - sql=$sql";
742 $this->db->rollback();
743 return -1;
744 }
745 }
746
753 public function getTooltipContentArray($params)
754 {
755 global $langs;
756
757 $langs->load('deliveries');
758
759 $datas = [];
760
761 $datas['picto'] = img_picto('', $this->picto).' <u>'.$langs->trans("ShowReceiving").'</u>:<br>';
762 $datas['picto'] .= '<b>'.$langs->trans("Status").'</b>: '.$this->ref;
763
764 return $datas;
765 }
766
774 public function getNomUrl($withpicto = 0, $save_lastsearch_value = -1)
775 {
776 global $langs, $hookmanager;
777
778 $result = '';
779
780 $params = [
781 'id' => $this->id,
782 'objecttype' => $this->element,
783 ];
784 $classfortooltip = 'classfortooltip';
785 $dataparams = '';
786 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
787 $classfortooltip = 'classforajaxtooltip';
788 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
789 $label = '';
790 } else {
791 $label = implode($this->getTooltipContentArray($params));
792 }
793
794 $url = DOL_URL_ROOT.'/delivery/card.php?id='.$this->id;
795
796 //if ($option !== 'nolink')
797 //{
798 // Add param to save lastsearch_values or not
799 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
800 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
801 $add_save_lastsearch_values = 1;
802 }
803 if ($add_save_lastsearch_values) {
804 $url .= '&save_lastsearch_values=1';
805 }
806 //}
807
808 $linkstart = '<a href="'.$url.'"';
809 $linkstart .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
810 $linkstart .= $dataparams.' class="'.$classfortooltip.'">';
811 $linkend = '</a>';
812
813 if ($withpicto) {
814 $result .= ($linkstart.img_object($label, $this->picto, $dataparams.' class="'.$classfortooltip.'"').$linkend);
815 }
816 if ($withpicto && $withpicto != 2) {
817 $result .= ' ';
818 }
819 $result .= $linkstart.$this->ref.$linkend;
820
821 global $action;
822 $hookmanager->initHooks(array($this->element . 'dao'));
823 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
824 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
825 if ($reshook > 0) {
826 $result = $hookmanager->resPrint;
827 } else {
828 $result .= $hookmanager->resPrint;
829 }
830 return $result;
831 }
832
833 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
839 public function fetch_lines()
840 {
841 // phpcs:enable
842 $this->lines = array();
843
844 $sql = "SELECT ld.rowid, ld.fk_product, ld.description, ld.subprice, ld.total_ht, ld.qty as qty_shipped, ld.fk_origin_line, ";
845 $sql .= " cd.qty as qty_asked, cd.label as custom_label, cd.fk_unit,";
846 $sql .= " p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc,";
847 $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";
848 $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."deliverydet as ld";
849 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on p.rowid = ld.fk_product";
850 $sql .= " WHERE ld.fk_origin_line = cd.rowid";
851 $sql .= " AND ld.fk_delivery = ".((int) $this->id);
852
853 dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
854 $resql = $this->db->query($sql);
855 if ($resql) {
856 $num = $this->db->num_rows($resql);
857 $i = 0;
858 while ($i < $num) {
859 $obj = $this->db->fetch_object($resql);
860
861 $line = new DeliveryLine($this->db);
862
863 $line->id = $obj->rowid;
864 $line->label = $obj->custom_label;
865 $line->description = $obj->description;
866 $line->fk_product = $obj->fk_product;
867 $line->qty_asked = $obj->qty_asked;
868 $line->qty_shipped = $obj->qty_shipped;
869
870 $line->product_label = $obj->product_label; // Product label
871 $line->product_ref = $obj->product_ref; // Product ref
872 $line->product_desc = $obj->product_desc; // Product description
873 $line->product_type = $obj->fk_product_type;
874
875 $line->fk_origin_line = $obj->fk_origin_line;
876
877 $line->price = $obj->subprice;
878 $line->total_ht = $obj->total_ht;
879
880 // units
881 $line->weight = $obj->weight;
882 $line->weight_units = $obj->weight_units;
883 $line->width = $obj->width;
884 $line->width_units = $obj->width_units;
885 $line->height = $obj->height;
886 $line->height_units = $obj->height_units;
887 $line->length = $obj->length;
888 $line->length_units = $obj->length_units;
889 $line->surface = $obj->surface;
890 $line->surface_units = $obj->surface_units;
891 $line->volume = $obj->volume;
892 $line->volume_units = $obj->volume_units;
893
894 $line->fk_unit = $obj->fk_unit;
895 $line->fetch_optionals();
896
897 $this->lines[$i] = $line;
898
899 $i++;
900 }
901 $this->db->free($resql);
902
903 return 1;
904 } else {
905 return -1;
906 }
907 }
908
909
916 public function getLibStatut($mode = 0)
917 {
918 return $this->LibStatut($this->status, $mode);
919 }
920
921 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
929 public function LibStatut($status, $mode)
930 {
931 // phpcs:enable
932 global $langs;
933
934 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
935 global $langs;
936 //$langs->load("mymodule");
937 $this->labelStatus[-1] = $langs->transnoentitiesnoconv('StatusDeliveryCanceled');
938 $this->labelStatus[0] = $langs->transnoentitiesnoconv('StatusDeliveryDraft');
939 $this->labelStatus[1] = $langs->transnoentitiesnoconv('StatusDeliveryValidated');
940 $this->labelStatusShort[-1] = $langs->transnoentitiesnoconv('StatusDeliveryCanceled');
941 $this->labelStatusShort[0] = $langs->transnoentitiesnoconv('StatusDeliveryDraft');
942 $this->labelStatusShort[1] = $langs->transnoentitiesnoconv('StatusDeliveryValidated');
943 }
944
945 $statusType = 'status0';
946 if ($status == -1) {
947 $statusType = 'status5';
948 }
949 if ($status == 1) {
950 $statusType = 'status4';
951 }
952
953 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
954 }
955
956
964 public function initAsSpecimen()
965 {
966 $now = dol_now();
967
968 // Load array of products prodids
969 $num_prods = 0;
970 $prodids = array();
971 $sql = "SELECT rowid";
972 $sql .= " FROM ".MAIN_DB_PREFIX."product";
973 $sql .= " WHERE entity IN (".getEntity('product').")";
974 $sql .= " AND tosell = 1";
975 $sql .= $this->db->plimit(100);
976
977 $resql = $this->db->query($sql);
978 if ($resql) {
979 $num_prods = $this->db->num_rows($resql);
980 $i = 0;
981 while ($i < $num_prods) {
982 $i++;
983 $row = $this->db->fetch_row($resql);
984 $prodids[$i] = $row[0];
985 }
986 }
987
988 // Initialise parameters
989 $this->id = 0;
990 $this->ref = 'SPECIMEN';
991 $this->specimen = 1;
992 $this->socid = 1;
993 $this->date_delivery = $now;
994 $this->note_public = 'Public note';
995 $this->note_private = 'Private note';
996
997 $i = 0;
998 $line = new DeliveryLine($this->db);
999 $line->fk_product = reset($prodids);
1000 $line->qty_asked = 10;
1001 $line->qty_shipped = 9;
1002 $line->product_ref = 'REFPROD';
1003 $line->label = 'Specimen';
1004 $line->description = 'Description';
1005 $line->price = 100;
1006 $line->total_ht = 100;
1007
1008 $this->lines[$i] = $line;
1009
1010 return 1;
1011 }
1012
1019 public function getRemainingDelivered()
1020 {
1021 // Get the linked object
1022 $this->fetchObjectLinked('', '', $this->id, $this->element);
1023 //var_dump($this->linkedObjectsIds);
1024 // Get the product ref and qty in source
1025 $sqlSourceLine = "SELECT st.rowid, st.description, st.qty";
1026 $sqlSourceLine .= ", p.ref, p.label";
1027 $sqlSourceLine .= " FROM ".MAIN_DB_PREFIX.$this->linkedObjectsIds[0]['type']."det as st";
1028 $sqlSourceLine .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON st.fk_product = p.rowid";
1029 $sqlSourceLine .= " WHERE fk_".$this->linked_objects[0]['type']." = ".((int) $this->linked_objects[0]['linkid']);
1030
1031 $resultSourceLine = $this->db->query($sqlSourceLine);
1032 if ($resultSourceLine) {
1033 $num_lines = $this->db->num_rows($resultSourceLine);
1034 $i = 0;
1035 $array = array();
1036 while ($i < $num_lines) {
1037 $objSourceLine = $this->db->fetch_object($resultSourceLine);
1038
1039 // Get lines of sources already delivered
1040 $sql = "SELECT ld.fk_origin_line, sum(ld.qty) as qty";
1041 $sql .= " FROM ".MAIN_DB_PREFIX."deliverydet as ld, ".MAIN_DB_PREFIX."delivery as l,";
1042 $sql .= " ".MAIN_DB_PREFIX.$this->linked_objects[0]['type']." as c";
1043 $sql .= ", ".MAIN_DB_PREFIX.$this->linked_objects[0]['type']."det as cd";
1044 $sql .= " WHERE ld.fk_delivery = l.rowid";
1045 $sql .= " AND ld.fk_origin_line = cd.rowid";
1046 $sql .= " AND cd.fk_".$this->linked_objects[0]['type']." = c.rowid";
1047 $sql .= " AND cd.fk_".$this->linked_objects[0]['type']." = ".((int) $this->linked_objects[0]['linkid']);
1048 $sql .= " AND ld.fk_origin_line = ".((int) $objSourceLine->rowid);
1049 $sql .= " GROUP BY ld.fk_origin_line";
1050
1051 $result = $this->db->query($sql);
1052 $row = $this->db->fetch_row($result);
1053
1054 if ($objSourceLine->qty - $row[1] > 0) {
1055 if ($row[0] == $objSourceLine->rowid) {
1056 $array[$i]['qty'] = $objSourceLine->qty - $row[1];
1057 } else {
1058 $array[$i]['qty'] = $objSourceLine->qty;
1059 }
1060
1061 $array[$i]['ref'] = $objSourceLine->ref;
1062 $array[$i]['label'] = $objSourceLine->label ? $objSourceLine->label : $objSourceLine->description;
1063 } elseif ($objSourceLine->qty - $row[1] < 0) {
1064 $array[$i]['qty'] = $objSourceLine->qty - $row[1]." Erreur livraison !";
1065 $array[$i]['ref'] = $objSourceLine->ref;
1066 $array[$i]['label'] = $objSourceLine->label ? $objSourceLine->label : $objSourceLine->description;
1067 }
1068
1069 $i++;
1070 }
1071 return $array;
1072 } else {
1073 $this->error = $this->db->error()." - sql=$sqlSourceLine";
1074 return -1;
1075 }
1076 }
1077
1085 public function setDeliveryDate($user, $delivery_date)
1086 {
1087 if ($user->hasRight('expedition', 'creer')) {
1088 $sql = "UPDATE ".MAIN_DB_PREFIX."delivery";
1089 $sql .= " SET date_delivery = ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : 'null');
1090 $sql .= " WHERE rowid = ".((int) $this->id);
1091
1092 dol_syslog(get_class($this)."::setDeliveryDate", LOG_DEBUG);
1093 $resql = $this->db->query($sql);
1094 if ($resql) {
1095 $this->date_delivery = $delivery_date;
1096 return 1;
1097 } else {
1098 $this->error = $this->db->error();
1099 return -1;
1100 }
1101 } else {
1102 return -2;
1103 }
1104 }
1105
1116 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1117 {
1118 global $conf, $langs;
1119
1120 $langs->load("deliveries");
1121 $outputlangs->load("products");
1122
1123 if (!dol_strlen($modele)) {
1124 $modele = 'typhon';
1125
1126 if ($this->model_pdf) {
1127 $modele = $this->model_pdf;
1128 } elseif (getDolGlobalString('DELIVERY_ADDON_PDF')) {
1129 $modele = getDolGlobalString('DELIVERY_ADDON_PDF');
1130 }
1131 }
1132
1133 $modelpath = "core/modules/delivery/doc/";
1134
1135 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1136 }
1137
1146 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1147 {
1148 $tables = array(
1149 'delivery'
1150 );
1151
1152 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1153 }
1154
1163 public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
1164 {
1165 $tables = array(
1166 'deliverydet'
1167 );
1168
1169 return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
1170 }
1171}
1172
1173
1178{
1182 public $db;
1183
1187 public $element = 'deliverydet';
1188
1192 public $table_element = 'deliverydet';
1193
1197 public $label;
1198
1202 public $description;
1203
1208 public $ref;
1213 public $libelle;
1214
1215 // From llx_expeditiondet
1219 public $qty;
1220
1224 public $qty_asked;
1225
1229 public $qty_shipped;
1230
1231 public $fk_product;
1232 public $product_desc;
1233 public $product_type;
1234 public $product_ref;
1235 public $product_label;
1236
1237 public $price;
1238
1239 public $fk_origin_line;
1240 public $origin_id;
1241
1245 public $origin_line_id;
1246
1252 public $commande_ligne_id;
1253
1254
1260 public function __construct($db)
1261 {
1262 $this->db = $db;
1263 }
1264}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition security.php:626
$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...
update_price($exclspec=0, $roundingadjust='auto', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
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.
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.
create_line($origin_id, $qty, $fk_product, $description, $array_options=[])
Create a line.
valid($user, $notrigger=0)
Validate object and update stock if option enabled.
__construct($db)
Constructor.
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
deleteLine($lineid)
Delete line.
setDeliveryDate($user, $delivery_date)
Set the planned delivery date.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create object on disk.
update_line($id, $array_options=[])
Update a livraison line (only extrafields)
addline($origin_id, $qty, $array_options=[])
Add line.
fetch($id)
Load a delivery receipt.
create($user)
Create delivery receipt in database.
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.
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($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
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:143
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e e e e e statut
Definition invoice.php:1929