dolibarr  7.0.0-beta
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 {
37  public $db;
38  public $error;
39  public $errors=array();
40  public $element='commandefournisseurdispatch';
41  public $table_element='commande_fournisseur_dispatch';
42  public $lines=array();
43 
44  public $id;
45 
46  public $fk_commande;
47  public $fk_product;
48  public $fk_commandefourndet;
49  public $qty;
50  public $fk_entrepot;
51  public $fk_user;
52  public $datec='';
53  public $comment;
54  public $status;
55  public $tms='';
56  public $batch;
57  public $eatby='';
58  public $sellby='';
59 
60 
61 
62 
68  function __construct($db)
69  {
70  $this->db = $db;
71 
72  // List of language codes for status
73  $this->statuts[0] = 'Received';
74  $this->statuts[1] = 'Verified';
75  $this->statuts[2] = 'Denied';
76  $this->statutshort[0] = 'Received';
77  $this->statutshort[1] = 'Verified';
78  $this->statutshort[2] = 'Denied';
79 
80  return 1;
81  }
82 
83 
91  function create($user, $notrigger=0)
92  {
93  global $conf, $langs;
94  $error=0;
95 
96  // Clean parameters
97 
98  if (isset($this->fk_commande)) $this->fk_commande=trim($this->fk_commande);
99  if (isset($this->fk_product)) $this->fk_product=trim($this->fk_product);
100  if (isset($this->fk_commandefourndet)) $this->fk_commandefourndet=trim($this->fk_commandefourndet);
101  if (isset($this->qty)) $this->qty=trim($this->qty);
102  if (isset($this->fk_entrepot)) $this->fk_entrepot=trim($this->fk_entrepot);
103  if (isset($this->fk_user)) $this->fk_user=trim($this->fk_user);
104  if (isset($this->comment)) $this->comment=trim($this->comment);
105  if (isset($this->status)) $this->status=trim($this->status);
106  if (isset($this->batch)) $this->batch=trim($this->batch);
107 
108  // Check parameters
109  // Put here code to add control on parameters values
110 
111  // Insert request
112  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
113  $sql.= "fk_commande,";
114  $sql.= "fk_product,";
115  $sql.= "fk_commandefourndet,";
116  $sql.= "qty,";
117  $sql.= "fk_entrepot,";
118  $sql.= "fk_user,";
119  $sql.= "datec,";
120  $sql.= "comment,";
121  $sql.= "status,";
122  $sql.= "batch,";
123  $sql.= "eatby,";
124  $sql.= "sellby";
125  $sql.= ") VALUES (";
126  $sql.= " ".(! isset($this->fk_commande)?'NULL':"'".$this->db->escape($this->fk_commande)."'").",";
127  $sql.= " ".(! isset($this->fk_product)?'NULL':"'".$this->db->escape($this->fk_product)."'").",";
128  $sql.= " ".(! isset($this->fk_commandefourndet)?'NULL':"'".$this->db->escape($this->fk_commandefourndet)."'").",";
129  $sql.= " ".(! isset($this->qty)?'NULL':"'".$this->db->escape($this->qty)."'").",";
130  $sql.= " ".(! isset($this->fk_entrepot)?'NULL':"'".$this->db->escape($this->fk_entrepot)."'").",";
131  $sql.= " ".(! isset($this->fk_user)?'NULL':"'".$this->db->escape($this->fk_user)."'").",";
132  $sql.= " ".(! isset($this->datec) || dol_strlen($this->datec)==0?'NULL':"'".$this->db->idate($this->datec)."'").",";
133  $sql.= " ".(! isset($this->comment)?'NULL':"'".$this->db->escape($this->comment)."'").",";
134  $sql.= " ".(! isset($this->status)?'NULL':"'".$this->db->escape($this->status)."'").",";
135  $sql.= " ".(! isset($this->batch)?'NULL':"'".$this->db->escape($this->batch)."'").",";
136  $sql.= " ".(! isset($this->eatby) || dol_strlen($this->eatby)==0?'NULL':"'".$this->db->idate($this->eatby)."'").",";
137  $sql.= " ".(! isset($this->sellby) || dol_strlen($this->sellby)==0?'NULL':"'".$this->db->idate($this->sellby)."'")."";
138  $sql.= ")";
139 
140  $this->db->begin();
141 
142  dol_syslog(__METHOD__, LOG_DEBUG);
143  $resql=$this->db->query($sql);
144  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
145 
146  if (! $error)
147  {
148  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
149 
150  if (! $notrigger)
151  {
152  // Uncomment this and change MYOBJECT to your own tag if you
153  // want this action calls a trigger.
154 
156  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
157  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
159  }
160  }
161 
162  // Commit or rollback
163  if ($error)
164  {
165  foreach($this->errors as $errmsg)
166  {
167  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
168  $this->error.=($this->error?', '.$errmsg:$errmsg);
169  }
170  $this->db->rollback();
171  return -1*$error;
172  }
173  else
174  {
175  $this->db->commit();
176  return $this->id;
177  }
178  }
179 
180 
188  function fetch($id,$ref='')
189  {
190  global $langs;
191  $sql = "SELECT";
192  $sql.= " t.rowid,";
193 
194  $sql.= " t.fk_commande,";
195  $sql.= " t.fk_product,";
196  $sql.= " t.fk_commandefourndet,";
197  $sql.= " t.qty,";
198  $sql.= " t.fk_entrepot,";
199  $sql.= " t.fk_user,";
200  $sql.= " t.datec,";
201  $sql.= " t.comment,";
202  $sql.= " t.status,";
203  $sql.= " t.tms,";
204  $sql.= " t.batch,";
205  $sql.= " t.eatby,";
206  $sql.= " t.sellby";
207 
208 
209  $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
210  if ($ref) $sql.= " WHERE t.ref = '".$ref."'";
211  else $sql.= " WHERE t.rowid = ".$id;
212 
213  dol_syslog(get_class($this)."::fetch");
214  $resql=$this->db->query($sql);
215  if ($resql)
216  {
217  if ($this->db->num_rows($resql))
218  {
219  $obj = $this->db->fetch_object($resql);
220 
221  $this->id = $obj->rowid;
222 
223  $this->fk_commande = $obj->fk_commande;
224  $this->fk_product = $obj->fk_product;
225  $this->fk_commandefourndet = $obj->fk_commandefourndet;
226  $this->qty = $obj->qty;
227  $this->fk_entrepot = $obj->fk_entrepot;
228  $this->fk_user = $obj->fk_user;
229  $this->datec = $this->db->jdate($obj->datec);
230  $this->comment = $obj->comment;
231  $this->status = $obj->status;
232  $this->tms = $this->db->jdate($obj->tms);
233  $this->batch = $obj->batch;
234  $this->eatby = $this->db->jdate($obj->eatby);
235  $this->sellby = $this->db->jdate($obj->sellby);
236 
237 
238  }
239  $this->db->free($resql);
240 
241  return 1;
242  }
243  else
244  {
245  $this->error="Error ".$this->db->lasterror();
246  return -1;
247  }
248  }
249 
250 
258  function update($user, $notrigger=0)
259  {
260  global $conf, $langs;
261  $error=0;
262 
263  // Clean parameters
264 
265  if (isset($this->fk_commande)) $this->fk_commande=trim($this->fk_commande);
266  if (isset($this->fk_product)) $this->fk_product=trim($this->fk_product);
267  if (isset($this->fk_commandefourndet)) $this->fk_commandefourndet=trim($this->fk_commandefourndet);
268  if (isset($this->qty)) $this->qty=trim($this->qty);
269  if (isset($this->fk_entrepot)) $this->fk_entrepot=trim($this->fk_entrepot);
270  if (isset($this->fk_user)) $this->fk_user=trim($this->fk_user);
271  if (isset($this->comment)) $this->comment=trim($this->comment);
272  if (isset($this->status)) $this->status=trim($this->status);
273  if (isset($this->batch)) $this->batch=trim($this->batch);
274 
275 
276 
277  // Check parameters
278  // Put here code to add a control on parameters values
279 
280  // Update request
281  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
282 
283  $sql.= " fk_commande=".(isset($this->fk_commande)?$this->fk_commande:"null").",";
284  $sql.= " fk_product=".(isset($this->fk_product)?$this->fk_product:"null").",";
285  $sql.= " fk_commandefourndet=".(isset($this->fk_commandefourndet)?$this->fk_commandefourndet:"null").",";
286  $sql.= " qty=".(isset($this->qty)?$this->qty:"null").",";
287  $sql.= " fk_entrepot=".(isset($this->fk_entrepot)?$this->fk_entrepot:"null").",";
288  $sql.= " fk_user=".(isset($this->fk_user)?$this->fk_user:"null").",";
289  $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
290  $sql.= " comment=".(isset($this->comment)?"'".$this->db->escape($this->comment)."'":"null").",";
291  $sql.= " status=".(isset($this->status)?$this->status:"null").",";
292  $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
293  $sql.= " batch=".(isset($this->batch)?"'".$this->db->escape($this->batch)."'":"null").",";
294  $sql.= " eatby=".(dol_strlen($this->eatby)!=0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
295  $sql.= " sellby=".(dol_strlen($this->sellby)!=0 ? "'".$this->db->idate($this->sellby)."'" : 'null')."";
296 
297 
298  $sql.= " WHERE rowid=".$this->id;
299 
300  $this->db->begin();
301 
302  dol_syslog(__METHOD__);
303  $resql = $this->db->query($sql);
304  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
305 
306  if (! $error)
307  {
308  if (! $notrigger)
309  {
310  // Uncomment this and change MYOBJECT to your own tag if you
311  // want this action calls a trigger.
312 
314  //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
315  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
317  }
318  }
319 
320  // Commit or rollback
321  if ($error)
322  {
323  foreach($this->errors as $errmsg)
324  {
325  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
326  $this->error.=($this->error?', '.$errmsg:$errmsg);
327  }
328  $this->db->rollback();
329  return -1*$error;
330  }
331  else
332  {
333  $this->db->commit();
334  return 1;
335  }
336  }
337 
338 
346  function delete($user, $notrigger=0)
347  {
348  global $conf, $langs;
349  $error=0;
350 
351  $this->db->begin();
352 
353  if (! $error)
354  {
355  if (! $notrigger)
356  {
357  // Uncomment this and change MYOBJECT to your own tag if you
358  // want this action calls a trigger.
359 
361  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
362  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
364  }
365  }
366 
367  if (! $error)
368  {
369  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
370  $sql.= " WHERE rowid=".$this->id;
371 
372  dol_syslog(__METHOD__);
373  $resql = $this->db->query($sql);
374  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
375  }
376 
377  // Commit or rollback
378  if ($error)
379  {
380  foreach($this->errors as $errmsg)
381  {
382  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
383  $this->error.=($this->error?', '.$errmsg:$errmsg);
384  }
385  $this->db->rollback();
386  return -1*$error;
387  }
388  else
389  {
390  $this->db->commit();
391  return 1;
392  }
393  }
394 
395 
396 
403  function createFromClone($fromid)
404  {
405  global $user,$langs;
406 
407  $error=0;
408 
409  $object=new Commandefournisseurdispatch($this->db);
410 
411  $this->db->begin();
412 
413  // Load source object
414  $object->fetch($fromid);
415  $object->id=0;
416  $object->statut=0;
417 
418  // Clear fields
419  // ...
420 
421  // Create clone
422  $result=$object->create($user);
423 
424  // Other options
425  if ($result < 0)
426  {
427  $this->error=$object->error;
428  $error++;
429  }
430 
431  if (! $error)
432  {
433 
434 
435  }
436 
437  // End
438  if (! $error)
439  {
440  $this->db->commit();
441  return $object->id;
442  }
443  else
444  {
445  $this->db->rollback();
446  return -1;
447  }
448  }
449 
450 
451 
458  function getLibStatut($mode=0)
459  {
460  return $this->LibStatut($this->status,$mode);
461  }
462 
470  function LibStatut($statut,$mode=0)
471  {
472  global $langs;
473  $langs->load('orders');
474 
475  if ($mode == 0)
476  {
477  return $langs->trans($this->statuts[$statut]);
478  }
479  if ($mode == 1)
480  {
481  return $langs->trans($this->statutshort[$statut]);
482  }
483  if ($mode == 2)
484  {
485  return $langs->trans($this->statuts[$statut]);
486  }
487  if ($mode == 3)
488  {
489  if ($statut==0) return img_picto($langs->trans($this->statuts[$statut]),'statut0');
490  if ($statut==1) return img_picto($langs->trans($this->statuts[$statut]),'statut4');
491  if ($statut==2) return img_picto($langs->trans($this->statuts[$statut]),'statut8');
492  }
493  if ($mode == 4)
494  {
495  if ($statut==0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
496  if ($statut==1) return img_picto($langs->trans($this->statuts[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
497  if ($statut==2) return img_picto($langs->trans($this->statuts[$statut]),'statut8').' '.$langs->trans($this->statuts[$statut]);
498  }
499  if ($mode == 5)
500  {
501  if ($statut==0) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut0');
502  if ($statut==1) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut4');
503  if ($statut==2) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut8');
504  }
505  }
506 
507 
514  function initAsSpecimen()
515  {
516  $this->id=0;
517 
518  $this->fk_commande='';
519  $this->fk_product='';
520  $this->fk_commandefourndet='';
521  $this->qty='';
522  $this->fk_entrepot='';
523  $this->fk_user='';
524  $this->datec='';
525  $this->comment='';
526  $this->status='';
527  $this->tms='';
528  $this->batch='';
529  $this->eatby='';
530  $this->sellby='';
531 
532 
533  }
534 
547  public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
548  {
549  dol_syslog(__METHOD__, LOG_DEBUG);
550 
551  $sql = "SELECT";
552  $sql.= " t.rowid,";
553 
554  $sql.= " t.fk_commande,";
555  $sql.= " t.fk_product,";
556  $sql.= " t.fk_commandefourndet,";
557  $sql.= " t.qty,";
558  $sql.= " t.fk_entrepot,";
559  $sql.= " t.fk_user,";
560  $sql.= " t.datec,";
561  $sql.= " t.comment,";
562  $sql.= " t.status,";
563  $sql.= " t.tms,";
564  $sql.= " t.batch,";
565  $sql.= " t.eatby,";
566  $sql.= " t.sellby";
567 
568  $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
569 
570  // Manage filter
571  $sqlwhere = array();
572  if (count($filter) > 0) {
573  foreach ($filter as $key => $value) {
574  if ($key=='t.comment') {
575  $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
576  } elseif ($key=='t.datec' || $key=='t.tms' || $key=='t.eatby' || $key=='t.sellby' || $key=='t.batch') {
577  $sqlwhere [] = $key . ' = \'' . $this->db->escape($value) . '\'';
578  } else {
579  $sqlwhere [] = $key . ' = ' . $this->db->escape($value);
580  }
581  }
582  }
583  if (count($sqlwhere) > 0) {
584  $sql .= ' WHERE ' . implode(' '.$filtermode.' ', $sqlwhere);
585  }
586 
587  if (!empty($sortfield)) {
588  $sql .= $this->db->order($sortfield,$sortorder);
589  }
590  if (!empty($limit)) {
591  $sql .= ' ' . $this->db->plimit($limit, $offset);
592  }
593  $this->lines = array();
594 
595  $resql = $this->db->query($sql);
596  if ($resql) {
597  $num = $this->db->num_rows($resql);
598 
599  while ($obj = $this->db->fetch_object($resql)) {
600  $line = new self($this->db);
601 
602  $line->id = $obj->rowid;
603 
604  $line->fk_commande = $obj->fk_commande;
605  $line->fk_product = $obj->fk_product;
606  $line->fk_commandefourndet = $obj->fk_commandefourndet;
607  $line->qty = $obj->qty;
608  $line->fk_entrepot = $obj->fk_entrepot;
609  $line->fk_user = $obj->fk_user;
610  $line->datec = $this->db->jdate($obj->datec);
611  $line->comment = $obj->comment;
612  $line->status = $obj->status;
613  $line->tms = $this->db->jdate($obj->tms);
614  $line->batch = $obj->batch;
615  $line->eatby = $this->db->jdate($obj->eatby);
616  $line->sellby = $this->db->jdate($obj->sellby);
617 
618  $this->lines[$line->id] = $line;
619  }
620  $this->db->free($resql);
621 
622  return $num;
623  } else {
624  $this->errors[] = 'Error ' . $this->db->lasterror();
625  dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
626 
627  return - 1;
628  }
629  }
630 
631 }
createFromClone($fromid)
Load an object from its id and create a new one in database.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
Class to manage table commandefournisseurdispatch.
fetch($id, $ref='')
Load object in memory from the database.
$error
To return error code (or message)
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.
$element
Id that identify managed objects.
create($user, $notrigger=0)
Create object into database.
$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)&&$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:1013
LibStatut($statut, $mode=0)
Return label of a status.
$errors
To return several error codes (or messages)
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.
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.