dolibarr 19.0.3
fournisseur.commande.dispatch.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
26// Put here all includes required by your class file
27require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
28require_once DOL_DOCUMENT_ROOT."/reception/class/reception.class.php";
29
30
35{
39 public $db;
40
44 public $error;
45
49 public $errors = array();
50
54 public $element = 'commandefournisseurdispatch';
55
59 public $table_element = 'commande_fournisseur_dispatch';
60 public $lines = array();
61
65 public $id;
66
70 public $fk_commande;
71
75 public $fk_product;
76
80 public $fk_commandefourndet;
81
82 public $fk_reception;
83
84
85 public $qty;
86 public $qty_asked;
87
88 public $libelle;
89 public $label;
90 public $desc;
91 public $tva_tx;
92 public $vat_src_code;
93 public $ref_supplier;
94
98 public $fk_entrepot;
99
103 public $fk_user;
104
105 public $datec = '';
106 public $comment;
107
111 public $status;
112
113 public $tms = '';
114 public $batch;
115 public $eatby = '';
116 public $sellby = '';
117 public $cost_price = 0;
118
119
120
121
127 public function __construct($db)
128 {
129 $this->db = $db;
130
131 // List of language codes for status
132 $this->labelStatus[0] = 'Received';
133 $this->labelStatus[1] = 'Verified';
134 $this->labelStatus[2] = 'Denied';
135 $this->labelStatusShort[0] = 'Received';
136 $this->labelStatusShort[1] = 'Verified';
137 $this->labelStatusShort[2] = 'Denied';
138 }
139
140
148 public function create($user, $notrigger = 0)
149 {
150 global $conf, $langs, $hookmanager;
151 $error = 0;
152
153 // Clean parameters
154
155 if (isset($this->fk_commande)) {
156 $this->fk_commande = trim($this->fk_commande);
157 }
158 if (isset($this->fk_product)) {
159 $this->fk_product = trim($this->fk_product);
160 }
161 if (isset($this->fk_commandefourndet)) {
162 $this->fk_commandefourndet = trim($this->fk_commandefourndet);
163 }
164 if (isset($this->qty)) {
165 $this->qty = trim($this->qty);
166 }
167 if (isset($this->fk_entrepot)) {
168 $this->fk_entrepot = trim($this->fk_entrepot);
169 }
170 if (isset($this->fk_user)) {
171 $this->fk_user = trim($this->fk_user);
172 }
173 if (isset($this->comment)) {
174 $this->comment = trim($this->comment);
175 }
176 if (isset($this->status)) {
177 $this->status = trim($this->status);
178 }
179 if (isset($this->batch)) {
180 $this->batch = trim($this->batch);
181 }
182 if (empty($this->datec)) {
183 $this->datec = dol_now();
184 }
185
186
187 // Check parameters
188 // Put here code to add control on parameters values
189
190 // Insert request
191 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
192 $sql .= "fk_commande,";
193 $sql .= "fk_product,";
194 $sql .= "fk_commandefourndet,";
195 $sql .= "qty,";
196 $sql .= "fk_entrepot,";
197 $sql .= "fk_user,";
198 $sql .= "datec,";
199 $sql .= "comment,";
200 $sql .= "status,";
201 $sql .= "batch,";
202 $sql .= "eatby,";
203 $sql .= "sellby,";
204 $sql .= "fk_reception,";
205 $sql .= "cost_price";
206
207
208 $sql .= ") VALUES (";
209 $sql .= " ".(!isset($this->fk_commande) ? 'NULL' : "'".$this->db->escape($this->fk_commande)."'").",";
210 $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
211 $sql .= " ".(!isset($this->fk_commandefourndet) ? 'NULL' : "'".$this->db->escape($this->fk_commandefourndet)."'").",";
212 $sql .= " ".(!isset($this->qty) ? 'NULL' : "'".$this->db->escape($this->qty)."'").",";
213 $sql .= " ".(!isset($this->fk_entrepot) ? 'NULL' : "'".$this->db->escape($this->fk_entrepot)."'").",";
214 $sql .= " ".(!isset($this->fk_user) ? 'NULL' : "'".$this->db->escape($this->fk_user)."'").",";
215 $sql .= " ".(!isset($this->datec) || dol_strlen($this->datec) == 0 ? 'NULL' : "'".$this->db->idate($this->datec)."'").",";
216 $sql .= " ".(!isset($this->comment) ? 'NULL' : "'".$this->db->escape($this->comment)."'").",";
217 $sql .= " ".(!isset($this->status) ? 'NULL' : "'".$this->db->escape($this->status)."'").",";
218 $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
219 $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").",";
220 $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").",";
221 $sql .= " ".(!isset($this->fk_reception) ? 'NULL' : "'".$this->db->escape($this->fk_reception)."'").",";
222 $sql .= " ".(!isset($this->cost_price) ? '0' : "'".$this->db->escape($this->cost_price)."'");
223 $sql .= ")";
224
225 $this->db->begin();
226
227 dol_syslog(__METHOD__, LOG_DEBUG);
228 $resql = $this->db->query($sql);
229 if (!$resql) {
230 $error++;
231 $this->errors[] = "Error ".$this->db->lasterror();
232 }
233
234 if (!$error) {
235 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
236
237 if (!$notrigger) {
238 // Call triggers
239 $result=$this->call_trigger('LINERECEPTION_CREATE', $user);
240 if ($result < 0) {
241 $error++;
242 }
243 // End call triggers
244 }
245 }
246
247 // Create extrafields
248 if (!$error) {
249 $result = $this->insertExtraFields();
250 if ($result < 0) {
251 $error++;
252 }
253 }
254
255 // Commit or rollback
256 if ($error) {
257 foreach ($this->errors as $errmsg) {
258 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
259 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
260 }
261 $this->db->rollback();
262 return -1 * $error;
263 } else {
264 $this->db->commit();
265 return $this->id;
266 }
267 }
268
269
277 public function fetch($id, $ref = '')
278 {
279 global $langs;
280 $sql = "SELECT";
281 $sql .= " t.rowid,";
282
283 $sql .= " t.fk_commande,";
284 $sql .= " t.fk_product,";
285 $sql .= " t.fk_commandefourndet,";
286 $sql .= " t.qty,";
287 $sql .= " t.fk_entrepot,";
288 $sql .= " t.fk_user,";
289 $sql .= " t.datec,";
290 $sql .= " t.comment,";
291 $sql .= " t.status,";
292 $sql .= " t.tms,";
293 $sql .= " t.batch,";
294 $sql .= " t.eatby,";
295 $sql .= " t.sellby,";
296 $sql .= " t.fk_reception";
297
298
299 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
300 if ($ref) {
301 $sql .= " WHERE t.ref = '".$this->db->escape($ref)."'";
302 } else {
303 $sql .= " WHERE t.rowid = ".((int) $id);
304 }
305
306 dol_syslog(get_class($this)."::fetch");
307 $resql = $this->db->query($sql);
308 if ($resql) {
309 if ($this->db->num_rows($resql)) {
310 $obj = $this->db->fetch_object($resql);
311
312 $this->id = $obj->rowid;
313
314 $this->fk_commande = $obj->fk_commande;
315 $this->fk_product = $obj->fk_product;
316 $this->fk_commandefourndet = $obj->fk_commandefourndet;
317 $this->qty = $obj->qty;
318 $this->fk_entrepot = $obj->fk_entrepot;
319 $this->fk_user = $obj->fk_user;
320 $this->datec = $this->db->jdate($obj->datec);
321 $this->comment = $obj->comment;
322 $this->status = $obj->status;
323 $this->tms = $this->db->jdate($obj->tms);
324 $this->batch = $obj->batch;
325 $this->eatby = $this->db->jdate($obj->eatby);
326 $this->sellby = $this->db->jdate($obj->sellby);
327 $this->fk_reception = $obj->fk_reception;
328
329 $this->fetch_optionals();
330 }
331 $this->db->free($resql);
332
333 return 1;
334 } else {
335 $this->error = "Error ".$this->db->lasterror();
336 return -1;
337 }
338 }
339
340
348 public function update($user, $notrigger = 0)
349 {
350 $error = 0;
351
352 // Clean parameters
353
354 if (isset($this->fk_commande)) {
355 $this->fk_commande = trim($this->fk_commande);
356 }
357 if (isset($this->fk_product)) {
358 $this->fk_product = trim($this->fk_product);
359 }
360 if (isset($this->fk_commandefourndet)) {
361 $this->fk_commandefourndet = trim($this->fk_commandefourndet);
362 }
363 if (isset($this->qty)) {
364 $this->qty = trim($this->qty);
365 }
366 if (isset($this->fk_entrepot)) {
367 $this->fk_entrepot = trim($this->fk_entrepot);
368 }
369 if (isset($this->fk_user)) {
370 $this->fk_user = trim($this->fk_user);
371 }
372 if (isset($this->comment)) {
373 $this->comment = trim($this->comment);
374 }
375 if (isset($this->status)) {
376 $this->status = trim($this->status);
377 }
378 if (isset($this->batch)) {
379 $this->batch = trim($this->batch);
380 }
381
382
383
384 // Check parameters
385 // Put here code to add a control on parameters values
386
387 // Update request
388 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
389 $sql .= " fk_commande=".(isset($this->fk_commande) ? $this->fk_commande : "null").",";
390 $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
391 $sql .= " fk_commandefourndet=".(isset($this->fk_commandefourndet) ? $this->fk_commandefourndet : "null").",";
392 $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
393 $sql .= " fk_entrepot=".(isset($this->fk_entrepot) ? $this->fk_entrepot : "null").",";
394 $sql .= " fk_user=".(isset($this->fk_user) ? $this->fk_user : "null").",";
395 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
396 $sql .= " comment=".(isset($this->comment) ? "'".$this->db->escape($this->comment)."'" : "null").",";
397 $sql .= " status=".(isset($this->status) ? $this->status : "null").",";
398 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
399 $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
400 $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
401 $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null');
402 $sql .= " WHERE rowid=".((int) $this->id);
403
404 $this->db->begin();
405
406 dol_syslog(__METHOD__);
407 $resql = $this->db->query($sql);
408 if (!$resql) {
409 $error++;
410 $this->errors[] = "Error ".$this->db->lasterror();
411 }
412
413 if (!$error) {
414 if (!$error) {
415 if (empty($this->id) && !empty($this->rowid)) {
416 $this->id = $this->rowid;
417 }
418 $result = $this->insertExtraFields();
419 if ($result < 0) {
420 $error++;
421 }
422 }
423
424 if (!$notrigger) {
425 // Call triggers
426 $result = $this->call_trigger('LINERECEPTION_MODIFY', $user);
427 if ($result < 0) {
428 $error++;
429 }
430 // End call triggers
431 }
432 }
433
434 // Commit or rollback
435 if ($error) {
436 foreach ($this->errors as $errmsg) {
437 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
438 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
439 }
440 $this->db->rollback();
441 return -1 * $error;
442 } else {
443 $this->db->commit();
444 return 1;
445 }
446 }
447
448
456 public function delete($user, $notrigger = 0)
457 {
458 $error = 0;
459
460 $this->db->begin();
461
462 if (!$error) {
463 if (!$notrigger) {
464 // Call triggers
465 $result = $this->call_trigger('LINERECEPTION_DELETE', $user);
466 if ($result < 0) {
467 $error++;
468 }
469 // End call triggers
470 }
471 }
472
473 // Remove extrafields
474 if (!$error) {
475 $result = $this->deleteExtraFields();
476 if ($result < 0) {
477 $error++;
478 dol_syslog(get_class($this)."::delete error deleteExtraFields ".$this->error, LOG_ERR);
479 }
480 }
481
482 if (!$error) {
483 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
484 $sql .= " WHERE rowid=".((int) $this->id);
485
486 dol_syslog(__METHOD__);
487 $resql = $this->db->query($sql);
488 if (!$resql) {
489 $error++;
490 $this->errors[] = "Error ".$this->db->lasterror();
491 }
492 }
493
494 // Commit or rollback
495 if ($error) {
496 foreach ($this->errors as $errmsg) {
497 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
498 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
499 }
500 $this->db->rollback();
501 return -1 * $error;
502 } else {
503 $this->db->commit();
504 return 1;
505 }
506 }
507
508
516 public function createFromClone(User $user, $fromid)
517 {
518 $error = 0;
519
520 $object = new CommandeFournisseurDispatch($this->db);
521
522 $this->db->begin();
523
524 // Load source object
525 $object->fetch($fromid);
526 $object->id = 0;
527 $object->statut = 0;
528
529 // Clear fields
530 // ...
531
532 // Create clone
533 $object->context['createfromclone'] = 'createfromclone';
534 $result = $object->create($user);
535
536 // Other options
537 if ($result < 0) {
538 $this->error = $object->error;
539 $error++;
540 }
541
542 if (!$error) {
543 }
544
545 unset($object->context['createfromclone']);
546
547 // End
548 if (!$error) {
549 $this->db->commit();
550 return $object->id;
551 } else {
552 $this->db->rollback();
553 return -1;
554 }
555 }
556
557
558
565 public function getLibStatut($mode = 0)
566 {
567 return $this->LibStatut($this->status, $mode);
568 }
569
570 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
578 public function LibStatut($status, $mode = 0)
579 {
580 // phpcs:enable
581 global $langs;
582 $langs->load('orders');
583
584 if ($mode == 0) {
585 return $langs->trans($this->labelStatus[$status]);
586 } elseif ($mode == 1) {
587 return $langs->trans($this->labelStatusShort[$status]);
588 } elseif ($mode == 2) {
589 return $langs->trans($this->labelStatus[$status]);
590 } elseif ($mode == 3) {
591 if ($status == 0) {
592 return img_picto($langs->trans($this->labelStatus[$status]), 'statut0');
593 } elseif ($status == 1) {
594 return img_picto($langs->trans($this->labelStatus[$status]), 'statut4');
595 } elseif ($status == 2) {
596 return img_picto($langs->trans($this->labelStatus[$status]), 'statut8');
597 }
598 } elseif ($mode == 4) {
599 if ($status == 0) {
600 return img_picto($langs->trans($this->labelStatus[$status]), 'statut0').' '.$langs->trans($this->labelStatus[$status]);
601 } elseif ($status == 1) {
602 return img_picto($langs->trans($this->labelStatus[$status]), 'statut4').' '.$langs->trans($this->labelStatus[$status]);
603 } elseif ($status == 2) {
604 return img_picto($langs->trans($this->labelStatus[$status]), 'statut8').' '.$langs->trans($this->labelStatus[$status]);
605 }
606 } elseif ($mode == 5) {
607 if ($status == 0) {
608 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).' </span>'.img_picto($langs->trans($this->labelStatus[$status]), 'statut0');
609 } elseif ($status == 1) {
610 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).' </span>'.img_picto($langs->trans($this->labelStatus[$status]), 'statut4');
611 } elseif ($status == 2) {
612 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).' </span>'.img_picto($langs->trans($this->labelStatus[$status]), 'statut8');
613 }
614 }
615 return "";
616 }
617
618
625 public function initAsSpecimen()
626 {
627 $this->id = 0;
628
629 $this->fk_commande = '';
630 $this->fk_product = '';
631 $this->fk_commandefourndet = '';
632 $this->qty = '';
633 $this->fk_entrepot = '';
634 $this->fk_user = '';
635 $this->datec = '';
636 $this->comment = '';
637 $this->status = '';
638 $this->tms = '';
639 $this->batch = '';
640 $this->eatby = '';
641 $this->sellby = '';
642 }
643
656 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
657 {
658 dol_syslog(__METHOD__, LOG_DEBUG);
659
660 $sql = "SELECT";
661 $sql .= " t.rowid,";
662
663 $sql .= " t.fk_commande,";
664 $sql .= " t.fk_product,";
665 $sql .= " t.fk_commandefourndet,";
666 $sql .= " t.qty,";
667 $sql .= " t.fk_entrepot,";
668 $sql .= " t.fk_user,";
669 $sql .= " t.datec,";
670 $sql .= " t.comment,";
671 $sql .= " t.status,";
672 $sql .= " t.tms,";
673 $sql .= " t.batch,";
674 $sql .= " t.eatby,";
675 $sql .= " t.sellby";
676
677 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
678
679 // Manage filter
680 $sqlwhere = array();
681 if (count($filter) > 0) {
682 foreach ($filter as $key => $value) {
683 if ($key == 't.comment') {
684 $sqlwhere [] = $key." LIKE '%".$this->db->escape($value)."%'";
685 } elseif ($key == 't.datec' || $key == 't.tms' || $key == 't.eatby' || $key == 't.sellby' || $key == 't.batch') {
686 $sqlwhere [] = $key." = '".$this->db->escape($value)."'";
687 } elseif ($key == 'qty') {
688 $sqlwhere [] = $key." = ".((float) $value);
689 } else {
690 $sqlwhere [] = $key." = ".((int) $value);
691 }
692 }
693 }
694 if (count($sqlwhere) > 0) {
695 $sql .= ' WHERE '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
696 }
697
698 if (!empty($sortfield)) {
699 $sql .= $this->db->order($sortfield, $sortorder);
700 }
701 if (!empty($limit)) {
702 $sql .= $this->db->plimit($limit, $offset);
703 }
704 $this->lines = array();
705
706 $resql = $this->db->query($sql);
707 if ($resql) {
708 $num = $this->db->num_rows($resql);
709
710 while ($obj = $this->db->fetch_object($resql)) {
711 $line = new self($this->db);
712
713 $line->id = $obj->rowid;
714
715 $line->fk_commande = $obj->fk_commande;
716 $line->fk_product = $obj->fk_product;
717 $line->fk_commandefourndet = $obj->fk_commandefourndet;
718 $line->qty = $obj->qty;
719 $line->fk_entrepot = $obj->fk_entrepot;
720 $line->fk_user = $obj->fk_user;
721 $line->datec = $this->db->jdate($obj->datec);
722 $line->comment = $obj->comment;
723 $line->status = $obj->status;
724 $line->tms = $this->db->jdate($obj->tms);
725 $line->batch = $obj->batch;
726 $line->eatby = $this->db->jdate($obj->eatby);
727 $line->sellby = $this->db->jdate($obj->sellby);
728 $line->fetch_optionals();
729
730 $this->lines[$line->id] = $line;
731 }
732 $this->db->free($resql);
733
734 return $num;
735 } else {
736 $this->errors[] = 'Error '.$this->db->lasterror();
737 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
738
739 return -1;
740 }
741 }
742}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Definition security.php:604
Class to manage table commandefournisseurdispatch.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
update($user, $notrigger=0)
Update object into database.
$table_element
Name of table without prefix where object is stored.
fetch($id, $ref='')
Load object in memory from the database.
getLibStatut($mode=0)
Return label of the status of object.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.
LibStatut($status, $mode=0)
Return label of a status.
create($user, $notrigger=0)
Create object into database.
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...
deleteExtraFields()
Delete all extra fields values for the current object.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
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 Dolibarr users.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
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_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 rowid
Definition invoice.php:1926