dolibarr  9.0.0
productbatch.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2013-2014 Cedric GROSS <c.gross@kreiz-it.fr>
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 
25 require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
26 
27 
32 {
36  public $element='productbatch';
37 
38  private static $_table_element='product_batch';
39 
40  public $tms='';
41  public $fk_product_stock;
42  public $sellby='';
43  public $eatby='';
44  public $batch='';
45  public $qty;
46  public $warehouseid;
47 
51  public $fk_product;
52 
53 
54 
60  function __construct($db)
61  {
62  $this->db = $db;
63  }
64 
65 
73  function create($user, $notrigger=0)
74  {
75  global $conf, $langs;
76  $error=0;
77 
78  // Clean parameters
79  $this->cleanParam();
80 
81  // Check parameters
82  // Put here code to add control on parameters values
83 
84  // Insert request
85  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_batch (";
86  $sql.= "fk_product_stock,";
87  $sql.= "sellby,";
88  $sql.= "eatby,";
89  $sql.= "batch,";
90  $sql.= "qty,";
91  $sql.= "import_key";
92  $sql.= ") VALUES (";
93  $sql.= " ".(! isset($this->fk_product_stock)?'NULL':$this->fk_product_stock).",";
94  $sql.= " ".(! isset($this->sellby) || dol_strlen($this->sellby)==0?'NULL':"'".$this->db->idate($this->sellby)."'").",";
95  $sql.= " ".(! isset($this->eatby) || dol_strlen($this->eatby)==0?'NULL':"'".$this->db->idate($this->eatby)."'").",";
96  $sql.= " ".(! isset($this->batch)?'NULL':"'".$this->db->escape($this->batch)."'").",";
97  $sql.= " ".(! isset($this->qty)?'NULL':$this->qty).",";
98  $sql.= " ".(! isset($this->import_key)?'NULL':"'".$this->db->escape($this->import_key)."'")."";
99 
100  $sql.= ")";
101 
102  $this->db->begin();
103 
104  dol_syslog(get_class($this)."::create", LOG_DEBUG);
105  $resql=$this->db->query($sql);
106  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
107  if (! $error)
108  {
109  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.self::$_table_element);
110 
111  if (! $notrigger)
112  {
113  // Uncomment this and change MYOBJECT to your own tag if you
114  // want this action calls a trigger.
115 
117  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
118  //$interface=new Interfaces($this->db);
119  //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
120  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
122  }
123  }
124 
125  // Commit or rollback
126  if ($error)
127  {
128  $this->db->rollback();
129  return -1*$error;
130  }
131  else
132  {
133  $this->db->commit();
134  return $this->id;
135  }
136  }
137 
138 
145  function fetch($id)
146  {
147  global $langs;
148  $sql = "SELECT";
149  $sql.= " t.rowid,";
150 
151  $sql.= " t.tms,";
152  $sql.= " t.fk_product_stock,";
153  $sql.= " t.sellby as oldsellby,";
154  $sql.= " t.eatby as oldeatby,";
155  $sql.= " t.batch,";
156  $sql.= " t.qty,";
157  $sql.= " t.import_key,";
158  $sql.= " w.fk_entrepot,";
159  $sql.= " w.fk_product,";
160  $sql.= " pl.eatby,";
161  $sql.= " pl.sellby";
162 
163  $sql.= " FROM ".MAIN_DB_PREFIX."product_batch as t INNER JOIN ".MAIN_DB_PREFIX."product_stock w on t.fk_product_stock = w.rowid";
164  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
165  $sql.= " WHERE t.rowid = ".$id;
166 
167  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
168  $resql = $this->db->query($sql);
169  if ($resql)
170  {
171  if ($this->db->num_rows($resql))
172  {
173  $obj = $this->db->fetch_object($resql);
174 
175  $this->id = $obj->rowid;
176  $this->tms = $this->db->jdate($obj->tms);
177  $this->fk_product_stock = $obj->fk_product_stock;
178  $this->sellby = $this->db->jdate($obj->sellby?$obj->sellby:$obj->oldsellby);
179  $this->eatby = $this->db->jdate($obj->eatby?$obj->eatby:$obj->oldeatby);
180  $this->batch = $obj->batch;
181  $this->qty = $obj->qty;
182  $this->import_key = $obj->import_key;
183  $this->warehouseid= $obj->fk_entrepot;
184  $this->fk_product= $obj->fk_product;
185  }
186  $this->db->free($resql);
187 
188  return 1;
189  }
190  else
191  {
192  $this->error = "Error ".$this->db->lasterror();
193  return -1;
194  }
195  }
196 
204  function update($user=null, $notrigger=0)
205  {
206  global $conf, $langs;
207  $error=0;
208 
209  // Clean parameters
210  $this->cleanParam();
211 
212  // TODO Check qty is ok for stock move. Negative may not be allowed.
213  if ($this->qty < 0)
214  {
215 
216  }
217 
218  // Update request
219  $sql = "UPDATE ".MAIN_DB_PREFIX.self::$_table_element." SET";
220  $sql.= " fk_product_stock=".(isset($this->fk_product_stock)?$this->fk_product_stock:"null").",";
221  $sql.= " sellby=".(dol_strlen($this->sellby)!=0 ? "'".$this->db->idate($this->sellby)."'" : 'null').",";
222  $sql.= " eatby=".(dol_strlen($this->eatby)!=0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
223  $sql.= " batch=".(isset($this->batch)?"'".$this->db->escape($this->batch)."'":"null").",";
224  $sql.= " qty=".(isset($this->qty)?$this->qty:"null").",";
225  $sql.= " import_key=".(isset($this->import_key)?"'".$this->db->escape($this->import_key)."'":"null")."";
226  $sql.= " WHERE rowid=".$this->id." AND tms='".$this->db->idate($this->tms)."'";
227 
228  $this->db->begin();
229 
230  dol_syslog(get_class($this)."::update", LOG_DEBUG);
231  $resql = $this->db->query($sql);
232  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
233  if (! $error)
234  {
235  if (! $notrigger)
236  {
237  // Uncomment this and change MYOBJECT to your own tag if you
238  // want this action calls a trigger.
239 
241  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
242  //$interface=new Interfaces($this->db);
243  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
244  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
246  }
247  }
248 
249  // Commit or rollback
250  if ($error)
251  {
252  foreach($this->errors as $errmsg)
253  {
254  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
255  $this->error.=($this->error?', '.$errmsg:$errmsg);
256  }
257  $this->db->rollback();
258  return -1*$error;
259  }
260  else
261  {
262  $this->db->commit();
263  return 1;
264  }
265  }
266 
274  function delete($user, $notrigger=0)
275  {
276  global $conf, $langs;
277  $error=0;
278 
279  $this->db->begin();
280 
281  if (! $error)
282  {
283  if (! $notrigger)
284  {
285  // Uncomment this and change MYOBJECT to your own tag if you
286  // want this action calls a trigger.
287 
289  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
290  //$interface=new Interfaces($this->db);
291  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
292  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
294  }
295  }
296 
297  if (! $error)
298  {
299  $sql = "DELETE FROM ".MAIN_DB_PREFIX.self::$_table_element."";
300  $sql.= " WHERE rowid=".$this->id;
301 
302  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
303  $resql = $this->db->query($sql);
304  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
305  }
306 
307  // Commit or rollback
308  if ($error)
309  {
310  foreach($this->errors as $errmsg)
311  {
312  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
313  $this->error.=($this->error?', '.$errmsg:$errmsg);
314  }
315  $this->db->rollback();
316  return -1*$error;
317  }
318  else
319  {
320  $this->db->commit();
321  return 1;
322  }
323  }
324 
325 
326 
333  function createFromClone($fromid)
334  {
335  global $user,$langs;
336 
337  $error=0;
338 
339  $object=new Productbatch($this->db);
340 
341  $this->db->begin();
342 
343  // Load source object
344  $object->fetch($fromid);
345  $object->id=0;
346  $object->statut=0;
347 
348  // Clear fields
349  // ...
350 
351  // Create clone
352  $object->context['createfromclone']='createfromclone';
353  $result=$object->create($user);
354 
355  // Other options
356  if ($result < 0)
357  {
358  $this->error=$object->error;
359  $this->errors=array_merge($this->errors, $object->errors);
360  $error++;
361  }
362 
363  if (! $error)
364  {
365 
366  }
367 
368  unset($object->context['createfromclone']);
369 
370  // End
371  if (! $error)
372  {
373  $this->db->commit();
374  return $object->id;
375  }
376  else
377  {
378  $this->db->rollback();
379  return -1;
380  }
381  }
382 
383 
390  function initAsSpecimen()
391  {
392  $this->id=0;
393 
394  $this->tms='';
395  $this->fk_product_stock='';
396  $this->sellby='';
397  $this->eatby='';
398  $this->batch='';
399  $this->import_key='';
400  }
401 
407  private function cleanParam()
408  {
409  if (isset($this->fk_product_stock)) $this->fk_product_stock=(int) trim($this->fk_product_stock);
410  if (isset($this->batch)) $this->batch=trim($this->batch);
411  if (isset($this->qty)) $this->qty=(float) trim($this->qty);
412  if (isset($this->import_key)) $this->import_key=trim($this->import_key);
413  }
414 
424  function find($fk_product_stock=0, $eatby='',$sellby='',$batch_number='')
425  {
426  global $langs;
427  $where = array();
428  $sql = "SELECT";
429  $sql.= " t.rowid,";
430  $sql.= " t.tms,";
431  $sql.= " t.fk_product_stock,";
432  $sql.= " t.sellby,"; // deprecated
433  $sql.= " t.eatby,"; // deprecated
434  $sql.= " t.batch,";
435  $sql.= " t.qty,";
436  $sql.= " t.import_key";
437  $sql.= " FROM ".MAIN_DB_PREFIX.self::$_table_element." as t";
438  $sql.= " WHERE fk_product_stock=".$fk_product_stock;
439 
440  if (! empty($eatby)) array_push($where," eatby = '".$this->db->idate($eatby)."'"); // deprecated
441  if (! empty($sellby)) array_push($where," sellby = '".$this->db->idate($sellby)."'"); // deprecated
442 
443  if (! empty($batch_number)) $sql.= " AND batch = '".$this->db->escape($batch_number)."'";
444 
445  if (! empty($where)) $sql.= " AND (".implode(" OR ",$where).")";
446 
447  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
448  $resql=$this->db->query($sql);
449  if ($resql)
450  {
451  if ($this->db->num_rows($resql))
452  {
453  $obj = $this->db->fetch_object($resql);
454 
455  $this->id = $obj->rowid;
456 
457  $this->tms = $this->db->jdate($obj->tms);
458  $this->fk_product_stock = $obj->fk_product_stock;
459  $this->sellby = $this->db->jdate($obj->sellby);
460  $this->eatby = $this->db->jdate($obj->eatby);
461  $this->batch = $obj->batch;
462  $this->qty = $obj->qty;
463  $this->import_key = $obj->import_key;
464  }
465  $this->db->free($resql);
466 
467  return 1;
468  }
469  else
470  {
471  $this->error="Error ".$this->db->lasterror();
472  return -1;
473  }
474  }
484  public static function findAll($db, $fk_product_stock, $with_qty=0, $fk_product=0)
485  {
486  global $langs;
487  $ret = array();
488 
489  $sql = "SELECT";
490  $sql.= " t.rowid,";
491  $sql.= " t.tms,";
492  $sql.= " t.fk_product_stock,";
493  $sql.= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
494  $sql.= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
495  $sql.= " t.batch,";
496  $sql.= " t.qty,";
497  $sql.= " t.import_key";
498  if ($fk_product > 0)
499  {
500  $sql.= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
501  // TODO May add extrafields to ?
502  }
503  $sql.= " FROM ".MAIN_DB_PREFIX."product_batch as t";
504  if ($fk_product > 0)
505  {
506  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.fk_product = ".$fk_product." AND pl.batch = t.batch";
507  // TODO May add extrafields to ?
508  }
509  $sql.= " WHERE fk_product_stock=".$fk_product_stock;
510  if ($with_qty) $sql.= " AND t.qty <> 0";
511 
512  dol_syslog("productbatch::findAll", LOG_DEBUG);
513  $resql=$db->query($sql);
514  if ($resql)
515  {
516  $num = $db->num_rows($resql);
517  $i=0;
518  while ($i < $num)
519  {
520  $obj = $db->fetch_object($resql);
521 
522  $tmp = new Productbatch($db);
523  $tmp->id = $obj->rowid;
524  $tmp->lotid = $obj->lotid;
525  $tmp->tms = $db->jdate($obj->tms);
526  $tmp->fk_product_stock = $obj->fk_product_stock;
527  $tmp->sellby = $db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
528  $tmp->eatby = $db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
529  $tmp->batch = $obj->batch;
530  $tmp->qty = $obj->qty;
531  $tmp->import_key = $obj->import_key;
532 
533  $ret[$tmp->batch] = $tmp; // $ret is for a $fk_product_stock and unique key is on $fk_product_stock+batch
534  $i++;
535  }
536  $db->free($resql);
537 
538  return $ret;
539  }
540  else
541  {
542  $error="Error ".$db->lasterror();
543  return -1;
544  }
545  }
546 }
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
fetch($id)
Load object in memory from the database.
__construct($db)
Constructor.
find($fk_product_stock=0, $eatby='', $sellby='', $batch_number='')
Find first detail record that match eather eat-by or sell-by or batch within given warehouse...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
create($user, $notrigger=0)
Create object into database.
static findAll($db, $fk_product_stock, $with_qty=0, $fk_product=0)
Return all batch detail records for a given product and warehouse.
Manage record for batch number management.
createFromClone($fromid)
Load an object from its id and create a new one in database.
update($user=null, $notrigger=0)
Update object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
cleanParam()
Clean fields (triming)
static $_table_element
Name of table without prefix where object is stored.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)