dolibarr  9.0.0
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 <http://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."/societe/class/societe.class.php";
29 //require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
30 
31 
36 {
40  public $db;
41 
45  public $error;
46 
50  public $errors = array();
51 
55  public $element='commandefournisseurdispatch';
56 
60  public $table_element='commande_fournisseur_dispatch';
61  public $lines=array();
62 
66  public $id;
67 
71  public $fk_commande;
72 
76  public $fk_product;
77 
81  public $fk_commandefourndet;
82 
83  public $qty;
84 
88  public $fk_entrepot;
89 
93  public $fk_user;
94 
95  public $datec='';
96  public $comment;
97 
101  public $status;
102 
103  public $tms='';
104  public $batch;
105  public $eatby='';
106  public $sellby='';
107 
108 
109 
110 
116  function __construct($db)
117  {
118  $this->db = $db;
119 
120  // List of language codes for status
121  $this->statuts[0] = 'Received';
122  $this->statuts[1] = 'Verified';
123  $this->statuts[2] = 'Denied';
124  $this->statutshort[0] = 'Received';
125  $this->statutshort[1] = 'Verified';
126  $this->statutshort[2] = 'Denied';
127  }
128 
129 
137  function create($user, $notrigger=0)
138  {
139  global $conf, $langs;
140  $error=0;
141 
142  // Clean parameters
143 
144  if (isset($this->fk_commande)) $this->fk_commande=trim($this->fk_commande);
145  if (isset($this->fk_product)) $this->fk_product=trim($this->fk_product);
146  if (isset($this->fk_commandefourndet)) $this->fk_commandefourndet=trim($this->fk_commandefourndet);
147  if (isset($this->qty)) $this->qty=trim($this->qty);
148  if (isset($this->fk_entrepot)) $this->fk_entrepot=trim($this->fk_entrepot);
149  if (isset($this->fk_user)) $this->fk_user=trim($this->fk_user);
150  if (isset($this->comment)) $this->comment=trim($this->comment);
151  if (isset($this->status)) $this->status=trim($this->status);
152  if (isset($this->batch)) $this->batch=trim($this->batch);
153 
154  // Check parameters
155  // Put here code to add control on parameters values
156 
157  // Insert request
158  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
159  $sql.= "fk_commande,";
160  $sql.= "fk_product,";
161  $sql.= "fk_commandefourndet,";
162  $sql.= "qty,";
163  $sql.= "fk_entrepot,";
164  $sql.= "fk_user,";
165  $sql.= "datec,";
166  $sql.= "comment,";
167  $sql.= "status,";
168  $sql.= "batch,";
169  $sql.= "eatby,";
170  $sql.= "sellby";
171  $sql.= ") VALUES (";
172  $sql.= " ".(! isset($this->fk_commande)?'NULL':"'".$this->db->escape($this->fk_commande)."'").",";
173  $sql.= " ".(! isset($this->fk_product)?'NULL':"'".$this->db->escape($this->fk_product)."'").",";
174  $sql.= " ".(! isset($this->fk_commandefourndet)?'NULL':"'".$this->db->escape($this->fk_commandefourndet)."'").",";
175  $sql.= " ".(! isset($this->qty)?'NULL':"'".$this->db->escape($this->qty)."'").",";
176  $sql.= " ".(! isset($this->fk_entrepot)?'NULL':"'".$this->db->escape($this->fk_entrepot)."'").",";
177  $sql.= " ".(! isset($this->fk_user)?'NULL':"'".$this->db->escape($this->fk_user)."'").",";
178  $sql.= " ".(! isset($this->datec) || dol_strlen($this->datec)==0?'NULL':"'".$this->db->idate($this->datec)."'").",";
179  $sql.= " ".(! isset($this->comment)?'NULL':"'".$this->db->escape($this->comment)."'").",";
180  $sql.= " ".(! isset($this->status)?'NULL':"'".$this->db->escape($this->status)."'").",";
181  $sql.= " ".(! isset($this->batch)?'NULL':"'".$this->db->escape($this->batch)."'").",";
182  $sql.= " ".(! isset($this->eatby) || dol_strlen($this->eatby)==0?'NULL':"'".$this->db->idate($this->eatby)."'").",";
183  $sql.= " ".(! isset($this->sellby) || dol_strlen($this->sellby)==0?'NULL':"'".$this->db->idate($this->sellby)."'")."";
184  $sql.= ")";
185 
186  $this->db->begin();
187 
188  dol_syslog(__METHOD__, LOG_DEBUG);
189  $resql=$this->db->query($sql);
190  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
191 
192  if (! $error)
193  {
194  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
195 
196  if (! $notrigger)
197  {
198  // Uncomment this and change MYOBJECT to your own tag if you
199  // want this action calls a trigger.
200 
202  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
203  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
205  }
206  }
207 
208  // Commit or rollback
209  if ($error)
210  {
211  foreach($this->errors as $errmsg)
212  {
213  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
214  $this->error.=($this->error?', '.$errmsg:$errmsg);
215  }
216  $this->db->rollback();
217  return -1*$error;
218  }
219  else
220  {
221  $this->db->commit();
222  return $this->id;
223  }
224  }
225 
226 
234  function fetch($id,$ref='')
235  {
236  global $langs;
237  $sql = "SELECT";
238  $sql.= " t.rowid,";
239 
240  $sql.= " t.fk_commande,";
241  $sql.= " t.fk_product,";
242  $sql.= " t.fk_commandefourndet,";
243  $sql.= " t.qty,";
244  $sql.= " t.fk_entrepot,";
245  $sql.= " t.fk_user,";
246  $sql.= " t.datec,";
247  $sql.= " t.comment,";
248  $sql.= " t.status,";
249  $sql.= " t.tms,";
250  $sql.= " t.batch,";
251  $sql.= " t.eatby,";
252  $sql.= " t.sellby";
253 
254 
255  $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
256  if ($ref) $sql.= " WHERE t.ref = '".$ref."'";
257  else $sql.= " WHERE t.rowid = ".$id;
258 
259  dol_syslog(get_class($this)."::fetch");
260  $resql=$this->db->query($sql);
261  if ($resql)
262  {
263  if ($this->db->num_rows($resql))
264  {
265  $obj = $this->db->fetch_object($resql);
266 
267  $this->id = $obj->rowid;
268 
269  $this->fk_commande = $obj->fk_commande;
270  $this->fk_product = $obj->fk_product;
271  $this->fk_commandefourndet = $obj->fk_commandefourndet;
272  $this->qty = $obj->qty;
273  $this->fk_entrepot = $obj->fk_entrepot;
274  $this->fk_user = $obj->fk_user;
275  $this->datec = $this->db->jdate($obj->datec);
276  $this->comment = $obj->comment;
277  $this->status = $obj->status;
278  $this->tms = $this->db->jdate($obj->tms);
279  $this->batch = $obj->batch;
280  $this->eatby = $this->db->jdate($obj->eatby);
281  $this->sellby = $this->db->jdate($obj->sellby);
282  }
283  $this->db->free($resql);
284 
285  return 1;
286  }
287  else
288  {
289  $this->error="Error ".$this->db->lasterror();
290  return -1;
291  }
292  }
293 
294 
302  function update($user, $notrigger=0)
303  {
304  global $conf, $langs;
305  $error=0;
306 
307  // Clean parameters
308 
309  if (isset($this->fk_commande)) $this->fk_commande=trim($this->fk_commande);
310  if (isset($this->fk_product)) $this->fk_product=trim($this->fk_product);
311  if (isset($this->fk_commandefourndet)) $this->fk_commandefourndet=trim($this->fk_commandefourndet);
312  if (isset($this->qty)) $this->qty=trim($this->qty);
313  if (isset($this->fk_entrepot)) $this->fk_entrepot=trim($this->fk_entrepot);
314  if (isset($this->fk_user)) $this->fk_user=trim($this->fk_user);
315  if (isset($this->comment)) $this->comment=trim($this->comment);
316  if (isset($this->status)) $this->status=trim($this->status);
317  if (isset($this->batch)) $this->batch=trim($this->batch);
318 
319 
320 
321  // Check parameters
322  // Put here code to add a control on parameters values
323 
324  // Update request
325  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
326 
327  $sql.= " fk_commande=".(isset($this->fk_commande)?$this->fk_commande:"null").",";
328  $sql.= " fk_product=".(isset($this->fk_product)?$this->fk_product:"null").",";
329  $sql.= " fk_commandefourndet=".(isset($this->fk_commandefourndet)?$this->fk_commandefourndet:"null").",";
330  $sql.= " qty=".(isset($this->qty)?$this->qty:"null").",";
331  $sql.= " fk_entrepot=".(isset($this->fk_entrepot)?$this->fk_entrepot:"null").",";
332  $sql.= " fk_user=".(isset($this->fk_user)?$this->fk_user:"null").",";
333  $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
334  $sql.= " comment=".(isset($this->comment)?"'".$this->db->escape($this->comment)."'":"null").",";
335  $sql.= " status=".(isset($this->status)?$this->status:"null").",";
336  $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
337  $sql.= " batch=".(isset($this->batch)?"'".$this->db->escape($this->batch)."'":"null").",";
338  $sql.= " eatby=".(dol_strlen($this->eatby)!=0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
339  $sql.= " sellby=".(dol_strlen($this->sellby)!=0 ? "'".$this->db->idate($this->sellby)."'" : 'null')."";
340 
341 
342  $sql.= " WHERE rowid=".$this->id;
343 
344  $this->db->begin();
345 
346  dol_syslog(__METHOD__);
347  $resql = $this->db->query($sql);
348  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
349 
350  if (! $error)
351  {
352  if (! $notrigger)
353  {
354  // Uncomment this and change MYOBJECT to your own tag if you
355  // want this action calls a trigger.
356 
358  //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
359  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
361  }
362  }
363 
364  // Commit or rollback
365  if ($error)
366  {
367  foreach($this->errors as $errmsg)
368  {
369  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
370  $this->error.=($this->error?', '.$errmsg:$errmsg);
371  }
372  $this->db->rollback();
373  return -1*$error;
374  }
375  else
376  {
377  $this->db->commit();
378  return 1;
379  }
380  }
381 
382 
390  function delete($user, $notrigger=0)
391  {
392  global $conf, $langs;
393  $error=0;
394 
395  $this->db->begin();
396 
397  if (! $error)
398  {
399  if (! $notrigger)
400  {
401  // Uncomment this and change MYOBJECT to your own tag if you
402  // want this action calls a trigger.
403 
405  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
406  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
408  }
409  }
410 
411  if (! $error)
412  {
413  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
414  $sql.= " WHERE rowid=".$this->id;
415 
416  dol_syslog(__METHOD__);
417  $resql = $this->db->query($sql);
418  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
419  }
420 
421  // Commit or rollback
422  if ($error)
423  {
424  foreach($this->errors as $errmsg)
425  {
426  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
427  $this->error.=($this->error?', '.$errmsg:$errmsg);
428  }
429  $this->db->rollback();
430  return -1*$error;
431  }
432  else
433  {
434  $this->db->commit();
435  return 1;
436  }
437  }
438 
439 
440 
447  function createFromClone($fromid)
448  {
449  global $user,$langs;
450 
451  $error=0;
452 
453  $object=new Commandefournisseurdispatch($this->db);
454 
455  $this->db->begin();
456 
457  // Load source object
458  $object->fetch($fromid);
459  $object->id=0;
460  $object->statut=0;
461 
462  // Clear fields
463  // ...
464 
465  // Create clone
466  $object->context['createfromclone'] = 'createfromclone';
467  $result=$object->create($user);
468 
469  // Other options
470  if ($result < 0)
471  {
472  $this->error=$object->error;
473  $error++;
474  }
475 
476  if (! $error)
477  {
478 
479  }
480 
481  unset($object->context['createfromclone']);
482 
483  // End
484  if (! $error)
485  {
486  $this->db->commit();
487  return $object->id;
488  }
489  else
490  {
491  $this->db->rollback();
492  return -1;
493  }
494  }
495 
496 
497 
504  function getLibStatut($mode=0)
505  {
506  return $this->LibStatut($this->status,$mode);
507  }
508 
509  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
517  function LibStatut($statut,$mode=0)
518  {
519  // phpcs:enable
520  global $langs;
521  $langs->load('orders');
522 
523  if ($mode == 0)
524  {
525  return $langs->trans($this->statuts[$statut]);
526  }
527  elseif ($mode == 1)
528  {
529  return $langs->trans($this->statutshort[$statut]);
530  }
531  elseif ($mode == 2)
532  {
533  return $langs->trans($this->statuts[$statut]);
534  }
535  elseif ($mode == 3)
536  {
537  if ($statut==0) return img_picto($langs->trans($this->statuts[$statut]),'statut0');
538  if ($statut==1) return img_picto($langs->trans($this->statuts[$statut]),'statut4');
539  if ($statut==2) return img_picto($langs->trans($this->statuts[$statut]),'statut8');
540  }
541  elseif ($mode == 4)
542  {
543  if ($statut==0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
544  if ($statut==1) return img_picto($langs->trans($this->statuts[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
545  if ($statut==2) return img_picto($langs->trans($this->statuts[$statut]),'statut8').' '.$langs->trans($this->statuts[$statut]);
546  }
547  elseif ($mode == 5)
548  {
549  if ($statut==0) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut0');
550  if ($statut==1) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut4');
551  if ($statut==2) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut8');
552  }
553  }
554 
555 
562  function initAsSpecimen()
563  {
564  $this->id=0;
565 
566  $this->fk_commande='';
567  $this->fk_product='';
568  $this->fk_commandefourndet='';
569  $this->qty='';
570  $this->fk_entrepot='';
571  $this->fk_user='';
572  $this->datec='';
573  $this->comment='';
574  $this->status='';
575  $this->tms='';
576  $this->batch='';
577  $this->eatby='';
578  $this->sellby='';
579  }
580 
593  public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
594  {
595  dol_syslog(__METHOD__, LOG_DEBUG);
596 
597  $sql = "SELECT";
598  $sql.= " t.rowid,";
599 
600  $sql.= " t.fk_commande,";
601  $sql.= " t.fk_product,";
602  $sql.= " t.fk_commandefourndet,";
603  $sql.= " t.qty,";
604  $sql.= " t.fk_entrepot,";
605  $sql.= " t.fk_user,";
606  $sql.= " t.datec,";
607  $sql.= " t.comment,";
608  $sql.= " t.status,";
609  $sql.= " t.tms,";
610  $sql.= " t.batch,";
611  $sql.= " t.eatby,";
612  $sql.= " t.sellby";
613 
614  $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
615 
616  // Manage filter
617  $sqlwhere = array();
618  if (count($filter) > 0) {
619  foreach ($filter as $key => $value) {
620  if ($key=='t.comment') {
621  $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
622  } elseif ($key=='t.datec' || $key=='t.tms' || $key=='t.eatby' || $key=='t.sellby' || $key=='t.batch') {
623  $sqlwhere [] = $key . ' = \'' . $this->db->escape($value) . '\'';
624  } else {
625  $sqlwhere [] = $key . ' = ' . $this->db->escape($value);
626  }
627  }
628  }
629  if (count($sqlwhere) > 0) {
630  $sql .= ' WHERE ' . implode(' '.$filtermode.' ', $sqlwhere);
631  }
632 
633  if (!empty($sortfield)) {
634  $sql .= $this->db->order($sortfield,$sortorder);
635  }
636  if (!empty($limit)) {
637  $sql .= ' ' . $this->db->plimit($limit, $offset);
638  }
639  $this->lines = array();
640 
641  $resql = $this->db->query($sql);
642  if ($resql) {
643  $num = $this->db->num_rows($resql);
644 
645  while ($obj = $this->db->fetch_object($resql)) {
646  $line = new self($this->db);
647 
648  $line->id = $obj->rowid;
649 
650  $line->fk_commande = $obj->fk_commande;
651  $line->fk_product = $obj->fk_product;
652  $line->fk_commandefourndet = $obj->fk_commandefourndet;
653  $line->qty = $obj->qty;
654  $line->fk_entrepot = $obj->fk_entrepot;
655  $line->fk_user = $obj->fk_user;
656  $line->datec = $this->db->jdate($obj->datec);
657  $line->comment = $obj->comment;
658  $line->status = $obj->status;
659  $line->tms = $this->db->jdate($obj->tms);
660  $line->batch = $obj->batch;
661  $line->eatby = $this->db->jdate($obj->eatby);
662  $line->sellby = $this->db->jdate($obj->sellby);
663 
664  $this->lines[$line->id] = $line;
665  }
666  $this->db->free($resql);
667 
668  return $num;
669  } else {
670  $this->errors[] = 'Error ' . $this->db->lasterror();
671  dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
672 
673  return - 1;
674  }
675  }
676 }
createFromClone($fromid)
Load an object from its id and create a new one in database.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
Class to manage table commandefournisseurdispatch.
fetch($id, $ref='')
Load object in memory from the database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getLibStatut($mode=0)
Return label of the status of object.
create($user, $notrigger=0)
Create object into database.
$table_element
Name of table without prefix where object is stored.
LibStatut($statut, $mode=0)
Return label of a status.
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
update($user, $notrigger=0)
Update object into database.