dolibarr  19.0.0-dev
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
27 require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
28 require_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->statuts[0] = 'Received';
133  $this->statuts[1] = 'Verified';
134  $this->statuts[2] = 'Denied';
135  $this->statuts_short[0] = 'Received';
136  $this->statuts_short[1] = 'Verified';
137  $this->statuts_short[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++; $this->errors[] = "Error ".$this->db->lasterror();
231  }
232 
233  if (!$error) {
234  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
235 
236  if (!$notrigger) {
237  // Call triggers
238  $result=$this->call_trigger('LINERECEPTION_CREATE', $user);
239  if ($result < 0) {
240  $error++;
241  }
242  // End call triggers
243  }
244  }
245 
246  // Create extrafields
247  if (!$error) {
248  $result = $this->insertExtraFields();
249  if ($result < 0) {
250  $error++;
251  }
252  }
253 
254  // Commit or rollback
255  if ($error) {
256  foreach ($this->errors as $errmsg) {
257  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
258  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
259  }
260  $this->db->rollback();
261  return -1 * $error;
262  } else {
263  $this->db->commit();
264  return $this->id;
265  }
266  }
267 
268 
276  public function fetch($id, $ref = '')
277  {
278  global $langs;
279  $sql = "SELECT";
280  $sql .= " t.rowid,";
281 
282  $sql .= " t.fk_commande,";
283  $sql .= " t.fk_product,";
284  $sql .= " t.fk_commandefourndet,";
285  $sql .= " t.qty,";
286  $sql .= " t.fk_entrepot,";
287  $sql .= " t.fk_user,";
288  $sql .= " t.datec,";
289  $sql .= " t.comment,";
290  $sql .= " t.status,";
291  $sql .= " t.tms,";
292  $sql .= " t.batch,";
293  $sql .= " t.eatby,";
294  $sql .= " t.sellby,";
295  $sql .= " t.fk_reception";
296 
297 
298  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
299  if ($ref) {
300  $sql .= " WHERE t.ref = '".$this->db->escape($ref)."'";
301  } else {
302  $sql .= " WHERE t.rowid = ".((int) $id);
303  }
304 
305  dol_syslog(get_class($this)."::fetch");
306  $resql = $this->db->query($sql);
307  if ($resql) {
308  if ($this->db->num_rows($resql)) {
309  $obj = $this->db->fetch_object($resql);
310 
311  $this->id = $obj->rowid;
312 
313  $this->fk_commande = $obj->fk_commande;
314  $this->fk_product = $obj->fk_product;
315  $this->fk_commandefourndet = $obj->fk_commandefourndet;
316  $this->qty = $obj->qty;
317  $this->fk_entrepot = $obj->fk_entrepot;
318  $this->fk_user = $obj->fk_user;
319  $this->datec = $this->db->jdate($obj->datec);
320  $this->comment = $obj->comment;
321  $this->status = $obj->status;
322  $this->tms = $this->db->jdate($obj->tms);
323  $this->batch = $obj->batch;
324  $this->eatby = $this->db->jdate($obj->eatby);
325  $this->sellby = $this->db->jdate($obj->sellby);
326  $this->fk_reception = $obj->fk_reception;
327 
328  $this->fetch_optionals();
329  }
330  $this->db->free($resql);
331 
332  return 1;
333  } else {
334  $this->error = "Error ".$this->db->lasterror();
335  return -1;
336  }
337  }
338 
339 
347  public function update($user, $notrigger = 0)
348  {
349  $error = 0;
350 
351  // Clean parameters
352 
353  if (isset($this->fk_commande)) {
354  $this->fk_commande = trim($this->fk_commande);
355  }
356  if (isset($this->fk_product)) {
357  $this->fk_product = trim($this->fk_product);
358  }
359  if (isset($this->fk_commandefourndet)) {
360  $this->fk_commandefourndet = trim($this->fk_commandefourndet);
361  }
362  if (isset($this->qty)) {
363  $this->qty = trim($this->qty);
364  }
365  if (isset($this->fk_entrepot)) {
366  $this->fk_entrepot = trim($this->fk_entrepot);
367  }
368  if (isset($this->fk_user)) {
369  $this->fk_user = trim($this->fk_user);
370  }
371  if (isset($this->comment)) {
372  $this->comment = trim($this->comment);
373  }
374  if (isset($this->status)) {
375  $this->status = trim($this->status);
376  }
377  if (isset($this->batch)) {
378  $this->batch = trim($this->batch);
379  }
380 
381 
382 
383  // Check parameters
384  // Put here code to add a control on parameters values
385 
386  // Update request
387  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
388  $sql .= " fk_commande=".(isset($this->fk_commande) ? $this->fk_commande : "null").",";
389  $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
390  $sql .= " fk_commandefourndet=".(isset($this->fk_commandefourndet) ? $this->fk_commandefourndet : "null").",";
391  $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
392  $sql .= " fk_entrepot=".(isset($this->fk_entrepot) ? $this->fk_entrepot : "null").",";
393  $sql .= " fk_user=".(isset($this->fk_user) ? $this->fk_user : "null").",";
394  $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
395  $sql .= " comment=".(isset($this->comment) ? "'".$this->db->escape($this->comment)."'" : "null").",";
396  $sql .= " status=".(isset($this->status) ? $this->status : "null").",";
397  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
398  $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
399  $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
400  $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null');
401  $sql .= " WHERE rowid=".((int) $this->id);
402 
403  $this->db->begin();
404 
405  dol_syslog(__METHOD__);
406  $resql = $this->db->query($sql);
407  if (!$resql) {
408  $error++; $this->errors[] = "Error ".$this->db->lasterror();
409  }
410 
411  if (!$error) {
412  if (!$error) {
413  if (empty($this->id) && !empty($this->rowid)) {
414  $this->id = $this->rowid;
415  }
416  $result = $this->insertExtraFields();
417  if ($result < 0) {
418  $error++;
419  }
420  }
421 
422  if (!$notrigger) {
423  // Call triggers
424  $result = $this->call_trigger('LINERECEPTION_MODIFY', $user);
425  if ($result < 0) {
426  $error++;
427  }
428  // End call triggers
429  }
430  }
431 
432  // Commit or rollback
433  if ($error) {
434  foreach ($this->errors as $errmsg) {
435  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
436  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
437  }
438  $this->db->rollback();
439  return -1 * $error;
440  } else {
441  $this->db->commit();
442  return 1;
443  }
444  }
445 
446 
454  public function delete($user, $notrigger = 0)
455  {
456  $error = 0;
457 
458  $this->db->begin();
459 
460  if (!$error) {
461  if (!$notrigger) {
462  // Call triggers
463  $result = $this->call_trigger('LINERECEPTION_DELETE', $user);
464  if ($result < 0) {
465  $error++;
466  }
467  // End call triggers
468  }
469  }
470 
471  // Remove extrafields
472  if (!$error) {
473  $result = $this->deleteExtraFields();
474  if ($result < 0) {
475  $error++;
476  dol_syslog(get_class($this)."::delete error deleteExtraFields ".$this->error, LOG_ERR);
477  }
478  }
479 
480  if (!$error) {
481  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
482  $sql .= " WHERE rowid=".((int) $this->id);
483 
484  dol_syslog(__METHOD__);
485  $resql = $this->db->query($sql);
486  if (!$resql) {
487  $error++; $this->errors[] = "Error ".$this->db->lasterror();
488  }
489  }
490 
491  // Commit or rollback
492  if ($error) {
493  foreach ($this->errors as $errmsg) {
494  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
495  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
496  }
497  $this->db->rollback();
498  return -1 * $error;
499  } else {
500  $this->db->commit();
501  return 1;
502  }
503  }
504 
505 
513  public function createFromClone(User $user, $fromid)
514  {
515  $error = 0;
516 
517  $object = new CommandeFournisseurDispatch($this->db);
518 
519  $this->db->begin();
520 
521  // Load source object
522  $object->fetch($fromid);
523  $object->id = 0;
524  $object->statut = 0;
525 
526  // Clear fields
527  // ...
528 
529  // Create clone
530  $object->context['createfromclone'] = 'createfromclone';
531  $result = $object->create($user);
532 
533  // Other options
534  if ($result < 0) {
535  $this->error = $object->error;
536  $error++;
537  }
538 
539  if (!$error) {
540  }
541 
542  unset($object->context['createfromclone']);
543 
544  // End
545  if (!$error) {
546  $this->db->commit();
547  return $object->id;
548  } else {
549  $this->db->rollback();
550  return -1;
551  }
552  }
553 
554 
555 
562  public function getLibStatut($mode = 0)
563  {
564  return $this->LibStatut($this->status, $mode);
565  }
566 
567  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
575  public function LibStatut($status, $mode = 0)
576  {
577  // phpcs:enable
578  global $langs;
579  $langs->load('orders');
580 
581  if ($mode == 0) {
582  return $langs->trans($this->statuts[$status]);
583  } elseif ($mode == 1) {
584  return $langs->trans($this->statuts_short[$status]);
585  } elseif ($mode == 2) {
586  return $langs->trans($this->statuts[$status]);
587  } elseif ($mode == 3) {
588  if ($status == 0) {
589  return img_picto($langs->trans($this->statuts[$status]), 'statut0');
590  } elseif ($status == 1) {
591  return img_picto($langs->trans($this->statuts[$status]), 'statut4');
592  } elseif ($status == 2) {
593  return img_picto($langs->trans($this->statuts[$status]), 'statut8');
594  }
595  } elseif ($mode == 4) {
596  if ($status == 0) {
597  return img_picto($langs->trans($this->statuts[$status]), 'statut0').' '.$langs->trans($this->statuts[$status]);
598  } elseif ($status == 1) {
599  return img_picto($langs->trans($this->statuts[$status]), 'statut4').' '.$langs->trans($this->statuts[$status]);
600  } elseif ($status == 2) {
601  return img_picto($langs->trans($this->statuts[$status]), 'statut8').' '.$langs->trans($this->statuts[$status]);
602  }
603  } elseif ($mode == 5) {
604  if ($status == 0) {
605  return '<span class="hideonsmartphone">'.$langs->trans($this->statuts_short[$status]).' </span>'.img_picto($langs->trans($this->statuts[$status]), 'statut0');
606  } elseif ($status == 1) {
607  return '<span class="hideonsmartphone">'.$langs->trans($this->statuts_short[$status]).' </span>'.img_picto($langs->trans($this->statuts[$status]), 'statut4');
608  } elseif ($status == 2) {
609  return '<span class="hideonsmartphone">'.$langs->trans($this->statuts_short[$status]).' </span>'.img_picto($langs->trans($this->statuts[$status]), 'statut8');
610  }
611  }
612  return "";
613  }
614 
615 
622  public function initAsSpecimen()
623  {
624  $this->id = 0;
625 
626  $this->fk_commande = '';
627  $this->fk_product = '';
628  $this->fk_commandefourndet = '';
629  $this->qty = '';
630  $this->fk_entrepot = '';
631  $this->fk_user = '';
632  $this->datec = '';
633  $this->comment = '';
634  $this->status = '';
635  $this->tms = '';
636  $this->batch = '';
637  $this->eatby = '';
638  $this->sellby = '';
639  }
640 
653  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
654  {
655  dol_syslog(__METHOD__, LOG_DEBUG);
656 
657  $sql = "SELECT";
658  $sql .= " t.rowid,";
659 
660  $sql .= " t.fk_commande,";
661  $sql .= " t.fk_product,";
662  $sql .= " t.fk_commandefourndet,";
663  $sql .= " t.qty,";
664  $sql .= " t.fk_entrepot,";
665  $sql .= " t.fk_user,";
666  $sql .= " t.datec,";
667  $sql .= " t.comment,";
668  $sql .= " t.status,";
669  $sql .= " t.tms,";
670  $sql .= " t.batch,";
671  $sql .= " t.eatby,";
672  $sql .= " t.sellby";
673 
674  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
675 
676  // Manage filter
677  $sqlwhere = array();
678  if (count($filter) > 0) {
679  foreach ($filter as $key => $value) {
680  if ($key == 't.comment') {
681  $sqlwhere [] = $key." LIKE '%".$this->db->escape($value)."%'";
682  } elseif ($key == 't.datec' || $key == 't.tms' || $key == 't.eatby' || $key == 't.sellby' || $key == 't.batch') {
683  $sqlwhere [] = $key." = '".$this->db->escape($value)."'";
684  } elseif ($key == 'qty') {
685  $sqlwhere [] = $key." = ".((float) $value);
686  } else {
687  $sqlwhere [] = $key." = ".((int) $value);
688  }
689  }
690  }
691  if (count($sqlwhere) > 0) {
692  $sql .= ' WHERE '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
693  }
694 
695  if (!empty($sortfield)) {
696  $sql .= $this->db->order($sortfield, $sortorder);
697  }
698  if (!empty($limit)) {
699  $sql .= $this->db->plimit($limit, $offset);
700  }
701  $this->lines = array();
702 
703  $resql = $this->db->query($sql);
704  if ($resql) {
705  $num = $this->db->num_rows($resql);
706 
707  while ($obj = $this->db->fetch_object($resql)) {
708  $line = new self($this->db);
709 
710  $line->id = $obj->rowid;
711 
712  $line->fk_commande = $obj->fk_commande;
713  $line->fk_product = $obj->fk_product;
714  $line->fk_commandefourndet = $obj->fk_commandefourndet;
715  $line->qty = $obj->qty;
716  $line->fk_entrepot = $obj->fk_entrepot;
717  $line->fk_user = $obj->fk_user;
718  $line->datec = $this->db->jdate($obj->datec);
719  $line->comment = $obj->comment;
720  $line->status = $obj->status;
721  $line->tms = $this->db->jdate($obj->tms);
722  $line->batch = $obj->batch;
723  $line->eatby = $this->db->jdate($obj->eatby);
724  $line->sellby = $this->db->jdate($obj->sellby);
725  $line->fetch_optionals();
726 
727  $this->lines[$line->id] = $line;
728  }
729  $this->db->free($resql);
730 
731  return $num;
732  } else {
733  $this->errors[] = 'Error '.$this->db->lasterror();
734  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
735 
736  return -1;
737  }
738  }
739 }
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.
Definition: user.class.php:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
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.
div float
Buy price without taxes.
Definition: style.css.php:921