dolibarr  13.0.2
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 $qty;
83 
87  public $fk_entrepot;
88 
92  public $fk_user;
93 
94  public $datec = '';
95  public $comment;
96 
100  public $status;
101 
102  public $tms = '';
103  public $batch;
104  public $eatby = '';
105  public $sellby = '';
106 
107 
108 
109 
115  public function __construct($db)
116  {
117  $this->db = $db;
118 
119  // List of language codes for status
120  $this->statuts[0] = 'Received';
121  $this->statuts[1] = 'Verified';
122  $this->statuts[2] = 'Denied';
123  $this->statutshort[0] = 'Received';
124  $this->statutshort[1] = 'Verified';
125  $this->statutshort[2] = 'Denied';
126  }
127 
128 
136  public function create($user, $notrigger = 0)
137  {
138  global $conf, $langs, $hookmanager;
139  $error = 0;
140 
141  // Clean parameters
142 
143  if (isset($this->fk_commande)) $this->fk_commande = trim($this->fk_commande);
144  if (isset($this->fk_product)) $this->fk_product = trim($this->fk_product);
145  if (isset($this->fk_commandefourndet)) $this->fk_commandefourndet = trim($this->fk_commandefourndet);
146  if (isset($this->qty)) $this->qty = trim($this->qty);
147  if (isset($this->fk_entrepot)) $this->fk_entrepot = trim($this->fk_entrepot);
148  if (isset($this->fk_user)) $this->fk_user = trim($this->fk_user);
149  if (isset($this->comment)) $this->comment = trim($this->comment);
150  if (isset($this->status)) $this->status = trim($this->status);
151  if (isset($this->batch)) $this->batch = trim($this->batch);
152  if (empty($this->datec)) $this->datec = dol_now();
153 
154 
155  // Check parameters
156  // Put here code to add control on parameters values
157 
158  // Insert request
159  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
160  $sql .= "fk_commande,";
161  $sql .= "fk_product,";
162  $sql .= "fk_commandefourndet,";
163  $sql .= "qty,";
164  $sql .= "fk_entrepot,";
165  $sql .= "fk_user,";
166  $sql .= "datec,";
167  $sql .= "comment,";
168  $sql .= "status,";
169  $sql .= "batch,";
170  $sql .= "eatby,";
171  $sql .= "sellby,";
172  $sql .= "fk_reception";
173 
174 
175  $sql .= ") VALUES (";
176  $sql .= " ".(!isset($this->fk_commande) ? 'NULL' : "'".$this->db->escape($this->fk_commande)."'").",";
177  $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
178  $sql .= " ".(!isset($this->fk_commandefourndet) ? 'NULL' : "'".$this->db->escape($this->fk_commandefourndet)."'").",";
179  $sql .= " ".(!isset($this->qty) ? 'NULL' : "'".$this->db->escape($this->qty)."'").",";
180  $sql .= " ".(!isset($this->fk_entrepot) ? 'NULL' : "'".$this->db->escape($this->fk_entrepot)."'").",";
181  $sql .= " ".(!isset($this->fk_user) ? 'NULL' : "'".$this->db->escape($this->fk_user)."'").",";
182  $sql .= " ".(!isset($this->datec) || dol_strlen($this->datec) == 0 ? 'NULL' : "'".$this->db->idate($this->datec)."'").",";
183  $sql .= " ".(!isset($this->comment) ? 'NULL' : "'".$this->db->escape($this->comment)."'").",";
184  $sql .= " ".(!isset($this->status) ? 'NULL' : "'".$this->db->escape($this->status)."'").",";
185  $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
186  $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").",";
187  $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").",";
188  $sql .= " ".(!isset($this->fk_reception) ? 'NULL' : "'".$this->db->escape($this->fk_reception)."'")."";
189  $sql .= ")";
190 
191  $this->db->begin();
192 
193  dol_syslog(__METHOD__, LOG_DEBUG);
194  $resql = $this->db->query($sql);
195  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
196 
197  if (!$error)
198  {
199  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
200 
201  if (!$notrigger)
202  {
203  // Uncomment this and change MYOBJECT to your own tag if you
204  // want this action calls a trigger.
205 
207  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
208  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
210  }
211  }
212 
213  // Create extrafields
214  if (!$error)
215  {
216  $result = $this->insertExtraFields();
217  if ($result < 0) $error++;
218  }
219 
220  // Commit or rollback
221  if ($error)
222  {
223  foreach ($this->errors as $errmsg)
224  {
225  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
226  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
227  }
228  $this->db->rollback();
229  return -1 * $error;
230  } else {
231  $this->db->commit();
232  return $this->id;
233  }
234  }
235 
236 
244  public function fetch($id, $ref = '')
245  {
246  global $langs;
247  $sql = "SELECT";
248  $sql .= " t.rowid,";
249 
250  $sql .= " t.fk_commande,";
251  $sql .= " t.fk_product,";
252  $sql .= " t.fk_commandefourndet,";
253  $sql .= " t.qty,";
254  $sql .= " t.fk_entrepot,";
255  $sql .= " t.fk_user,";
256  $sql .= " t.datec,";
257  $sql .= " t.comment,";
258  $sql .= " t.status,";
259  $sql .= " t.tms,";
260  $sql .= " t.batch,";
261  $sql .= " t.eatby,";
262  $sql .= " t.sellby,";
263  $sql .= " t.fk_reception";
264 
265 
266  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
267  if ($ref) $sql .= " WHERE t.ref = '".$ref."'";
268  else $sql .= " WHERE t.rowid = ".$id;
269 
270  dol_syslog(get_class($this)."::fetch");
271  $resql = $this->db->query($sql);
272  if ($resql)
273  {
274  if ($this->db->num_rows($resql))
275  {
276  $obj = $this->db->fetch_object($resql);
277 
278  $this->id = $obj->rowid;
279 
280  $this->fk_commande = $obj->fk_commande;
281  $this->fk_product = $obj->fk_product;
282  $this->fk_commandefourndet = $obj->fk_commandefourndet;
283  $this->qty = $obj->qty;
284  $this->fk_entrepot = $obj->fk_entrepot;
285  $this->fk_user = $obj->fk_user;
286  $this->datec = $this->db->jdate($obj->datec);
287  $this->comment = $obj->comment;
288  $this->status = $obj->status;
289  $this->tms = $this->db->jdate($obj->tms);
290  $this->batch = $obj->batch;
291  $this->eatby = $this->db->jdate($obj->eatby);
292  $this->sellby = $this->db->jdate($obj->sellby);
293  $this->fk_reception = $obj->fk_reception;
294 
295  $this->fetch_optionals();
296  }
297  $this->db->free($resql);
298 
299  return 1;
300  } else {
301  $this->error = "Error ".$this->db->lasterror();
302  return -1;
303  }
304  }
305 
306 
314  public function update($user, $notrigger = 0)
315  {
316  global $conf, $langs;
317  $error = 0;
318 
319  // Clean parameters
320 
321  if (isset($this->fk_commande)) $this->fk_commande = trim($this->fk_commande);
322  if (isset($this->fk_product)) $this->fk_product = trim($this->fk_product);
323  if (isset($this->fk_commandefourndet)) $this->fk_commandefourndet = trim($this->fk_commandefourndet);
324  if (isset($this->qty)) $this->qty = trim($this->qty);
325  if (isset($this->fk_entrepot)) $this->fk_entrepot = trim($this->fk_entrepot);
326  if (isset($this->fk_user)) $this->fk_user = trim($this->fk_user);
327  if (isset($this->comment)) $this->comment = trim($this->comment);
328  if (isset($this->status)) $this->status = trim($this->status);
329  if (isset($this->batch)) $this->batch = trim($this->batch);
330 
331 
332 
333  // Check parameters
334  // Put here code to add a control on parameters values
335 
336  // Update request
337  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
338 
339  $sql .= " fk_commande=".(isset($this->fk_commande) ? $this->fk_commande : "null").",";
340  $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
341  $sql .= " fk_commandefourndet=".(isset($this->fk_commandefourndet) ? $this->fk_commandefourndet : "null").",";
342  $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
343  $sql .= " fk_entrepot=".(isset($this->fk_entrepot) ? $this->fk_entrepot : "null").",";
344  $sql .= " fk_user=".(isset($this->fk_user) ? $this->fk_user : "null").",";
345  $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
346  $sql .= " comment=".(isset($this->comment) ? "'".$this->db->escape($this->comment)."'" : "null").",";
347  $sql .= " status=".(isset($this->status) ? $this->status : "null").",";
348  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
349  $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
350  $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
351  $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null')."";
352 
353 
354  $sql .= " WHERE rowid=".$this->id;
355 
356  $this->db->begin();
357 
358  dol_syslog(__METHOD__);
359  $resql = $this->db->query($sql);
360  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
361 
362  if (!$error)
363  {
364  if (!$error)
365  {
366  if (empty($this->id) && !empty($this->rowid))$this->id = $this->rowid;
367  $result = $this->insertExtraFields();
368  if ($result < 0)
369  {
370  $error++;
371  }
372  }
373 
374  if (!$notrigger)
375  {
376  // Uncomment this and change MYOBJECT to your own tag if you
377  $result = $this->call_trigger('LINERECEPTION_UPDATE', $user);
378  if ($result < 0) $error++;
380  }
381  }
382 
383  // Commit or rollback
384  if ($error)
385  {
386  foreach ($this->errors as $errmsg)
387  {
388  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
389  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
390  }
391  $this->db->rollback();
392  return -1 * $error;
393  } else {
394  $this->db->commit();
395  return 1;
396  }
397  }
398 
399 
407  public function delete($user, $notrigger = 0)
408  {
409  global $conf, $langs;
410  $error = 0;
411 
412  $this->db->begin();
413 
414  if (!$error)
415  {
416  if (!$notrigger)
417  {
418  // Uncomment this and change MYOBJECT to your own tag if you
419  // want this action calls a trigger.
420 
422  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
423  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
425  }
426  }
427 
428  // Remove extrafields
429  if (!$error) {
430  $result = $this->deleteExtraFields();
431  if ($result < 0)
432  {
433  $error++;
434  dol_syslog(get_class($this)."::delete error deleteExtraFields ".$this->error, LOG_ERR);
435  }
436  }
437 
438  if (!$error)
439  {
440  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
441  $sql .= " WHERE rowid=".$this->id;
442 
443  dol_syslog(__METHOD__);
444  $resql = $this->db->query($sql);
445  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
446  }
447 
448  // Commit or rollback
449  if ($error)
450  {
451  foreach ($this->errors as $errmsg)
452  {
453  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
454  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
455  }
456  $this->db->rollback();
457  return -1 * $error;
458  } else {
459  $this->db->commit();
460  return 1;
461  }
462  }
463 
464 
465 
473  public function createFromClone(User $user, $fromid)
474  {
475  $error = 0;
476 
477  $object = new Commandefournisseurdispatch($this->db);
478 
479  $this->db->begin();
480 
481  // Load source object
482  $object->fetch($fromid);
483  $object->id = 0;
484  $object->statut = 0;
485 
486  // Clear fields
487  // ...
488 
489  // Create clone
490  $object->context['createfromclone'] = 'createfromclone';
491  $result = $object->create($user);
492 
493  // Other options
494  if ($result < 0)
495  {
496  $this->error = $object->error;
497  $error++;
498  }
499 
500  if (!$error)
501  {
502  }
503 
504  unset($object->context['createfromclone']);
505 
506  // End
507  if (!$error)
508  {
509  $this->db->commit();
510  return $object->id;
511  } else {
512  $this->db->rollback();
513  return -1;
514  }
515  }
516 
517 
518 
525  public function getLibStatut($mode = 0)
526  {
527  return $this->LibStatut($this->status, $mode);
528  }
529 
530  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
538  public function LibStatut($status, $mode = 0)
539  {
540  // phpcs:enable
541  global $langs;
542  $langs->load('orders');
543 
544  if ($mode == 0)
545  {
546  return $langs->trans($this->statuts[$status]);
547  } elseif ($mode == 1)
548  {
549  return $langs->trans($this->statutshort[$status]);
550  } elseif ($mode == 2)
551  {
552  return $langs->trans($this->statuts[$status]);
553  } elseif ($mode == 3)
554  {
555  if ($status == 0) return img_picto($langs->trans($this->statuts[$status]), 'statut0');
556  elseif ($status == 1) return img_picto($langs->trans($this->statuts[$status]), 'statut4');
557  elseif ($status == 2) return img_picto($langs->trans($this->statuts[$status]), 'statut8');
558  } elseif ($mode == 4)
559  {
560  if ($status == 0) return img_picto($langs->trans($this->statuts[$status]), 'statut0').' '.$langs->trans($this->statuts[$status]);
561  elseif ($status == 1) return img_picto($langs->trans($this->statuts[$status]), 'statut4').' '.$langs->trans($this->statuts[$status]);
562  elseif ($status == 2) return img_picto($langs->trans($this->statuts[$status]), 'statut8').' '.$langs->trans($this->statuts[$status]);
563  } elseif ($mode == 5)
564  {
565  if ($status == 0) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$status]).' </span>'.img_picto($langs->trans($this->statuts[$status]), 'statut0');
566  elseif ($status == 1) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$status]).' </span>'.img_picto($langs->trans($this->statuts[$status]), 'statut4');
567  elseif ($status == 2) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$status]).' </span>'.img_picto($langs->trans($this->statuts[$status]), 'statut8');
568  }
569  }
570 
571 
578  public function initAsSpecimen()
579  {
580  $this->id = 0;
581 
582  $this->fk_commande = '';
583  $this->fk_product = '';
584  $this->fk_commandefourndet = '';
585  $this->qty = '';
586  $this->fk_entrepot = '';
587  $this->fk_user = '';
588  $this->datec = '';
589  $this->comment = '';
590  $this->status = '';
591  $this->tms = '';
592  $this->batch = '';
593  $this->eatby = '';
594  $this->sellby = '';
595  }
596 
609  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
610  {
611  dol_syslog(__METHOD__, LOG_DEBUG);
612 
613  $sql = "SELECT";
614  $sql .= " t.rowid,";
615 
616  $sql .= " t.fk_commande,";
617  $sql .= " t.fk_product,";
618  $sql .= " t.fk_commandefourndet,";
619  $sql .= " t.qty,";
620  $sql .= " t.fk_entrepot,";
621  $sql .= " t.fk_user,";
622  $sql .= " t.datec,";
623  $sql .= " t.comment,";
624  $sql .= " t.status,";
625  $sql .= " t.tms,";
626  $sql .= " t.batch,";
627  $sql .= " t.eatby,";
628  $sql .= " t.sellby";
629 
630  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
631 
632  // Manage filter
633  $sqlwhere = array();
634  if (count($filter) > 0) {
635  foreach ($filter as $key => $value) {
636  if ($key == 't.comment') {
637  $sqlwhere [] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
638  } elseif ($key == 't.datec' || $key == 't.tms' || $key == 't.eatby' || $key == 't.sellby' || $key == 't.batch') {
639  $sqlwhere [] = $key.' = \''.$this->db->escape($value).'\'';
640  } else {
641  $sqlwhere [] = $key.' = '.$this->db->escape($value);
642  }
643  }
644  }
645  if (count($sqlwhere) > 0) {
646  $sql .= ' WHERE '.implode(' '.$filtermode.' ', $sqlwhere);
647  }
648 
649  if (!empty($sortfield)) {
650  $sql .= $this->db->order($sortfield, $sortorder);
651  }
652  if (!empty($limit)) {
653  $sql .= ' '.$this->db->plimit($limit, $offset);
654  }
655  $this->lines = array();
656 
657  $resql = $this->db->query($sql);
658  if ($resql) {
659  $num = $this->db->num_rows($resql);
660 
661  while ($obj = $this->db->fetch_object($resql)) {
662  $line = new self($this->db);
663 
664  $line->id = $obj->rowid;
665 
666  $line->fk_commande = $obj->fk_commande;
667  $line->fk_product = $obj->fk_product;
668  $line->fk_commandefourndet = $obj->fk_commandefourndet;
669  $line->qty = $obj->qty;
670  $line->fk_entrepot = $obj->fk_entrepot;
671  $line->fk_user = $obj->fk_user;
672  $line->datec = $this->db->jdate($obj->datec);
673  $line->comment = $obj->comment;
674  $line->status = $obj->status;
675  $line->tms = $this->db->jdate($obj->tms);
676  $line->batch = $obj->batch;
677  $line->eatby = $this->db->jdate($obj->eatby);
678  $line->sellby = $this->db->jdate($obj->sellby);
679  $line->fetch_optionals();
680 
681  $this->lines[$line->id] = $line;
682  }
683  $this->db->free($resql);
684 
685  return $num;
686  } else {
687  $this->errors[] = 'Error '.$this->db->lasterror();
688  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
689 
690  return -1;
691  }
692  }
693 }
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Class to manage table commandefournisseurdispatch.
</td >< tdcolspan="3">< spanclass="opacitymedium"></span ></td ></tr >< trclass="liste_total"> CREANCES DETTES< tdcolspan="3"class="right"></td >< tdcolspan="3"class="right"></td ></tr > CREANCES DETTES RECETTES DEPENSES trips CREANCES DETTES Y m expensereport p date_valid Y m expensereport pe datep $db idate($date_start)."' AND $column < p rowid
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
fetch($id, $ref= '')
Load object in memory from the database.
LibStatut($status, $mode=0)
Return label of a status.
$conf db
API class for accounts.
Definition: inc.php:54
insertExtraFields($trigger= '', $userused=null)
Add/Update all extra fields values for the current object.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
fetchAll($sortorder= '', $sortfield= '', $limit=0, $offset=0, array $filter=array(), $filtermode= 'AND')
Load object in memory from the database.
getLibStatut($mode=0)
Return label of the status of object.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
deleteExtraFields()
Delete all extra fields values for the current object.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this-&gt;array_options This method is in most cases call...
create($user, $notrigger=0)
Create object into database.
call_trigger($triggerName, $user)
Call trigger based on this instance.
$table_element
Name of table without prefix where object is stored.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
update($user, $notrigger=0)
Update object into database.