dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
26 
27 
32 {
37 
41  public $element = 'productbatch';
42 
43  private static $_table_element = 'product_batch';
44 
45  public $tms = '';
46  public $fk_product_stock;
47  public $sellby = '';
48  public $eatby = '';
49  public $batch = '';
50  public $qty;
51  public $warehouseid;
52 
56  public $fk_product;
57 
58 
59 
65  public function __construct($db)
66  {
67  $this->db = $db;
68  }
69 
70 
78  public function create($user, $notrigger = 0)
79  {
80  global $conf, $langs;
81  $error = 0;
82 
83  // Clean parameters
84  $this->cleanParam();
85 
86  // Check parameters
87  // Put here code to add control on parameters values
88 
89  // Insert request
90  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_batch (";
91  $sql .= "fk_product_stock,";
92  $sql .= "sellby,"; // no more used
93  $sql .= "eatby,"; // no more used
94  $sql .= "batch,";
95  $sql .= "qty,";
96  $sql .= "import_key";
97  $sql .= ") VALUES (";
98  $sql .= " ".(!isset($this->fk_product_stock) ? 'NULL' : $this->fk_product_stock).",";
99  $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; // no more used
100  $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; // no more used
101  $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
102  $sql .= " ".(!isset($this->qty) ? 'NULL' : $this->qty).",";
103  $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'")."";
104  $sql .= ")";
105 
106  $this->db->begin();
107 
108  dol_syslog(get_class($this)."::create", LOG_DEBUG);
109  $resql = $this->db->query($sql);
110  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
111  if (!$error)
112  {
113  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.self::$_table_element);
114  }
115 
116  // Commit or rollback
117  if ($error)
118  {
119  $this->db->rollback();
120  return -1 * $error;
121  } else {
122  $this->db->commit();
123  return $this->id;
124  }
125  }
126 
127 
134  public function fetch($id)
135  {
136  global $langs;
137  $sql = "SELECT";
138  $sql .= " t.rowid,";
139 
140  $sql .= " t.tms,";
141  $sql .= " t.fk_product_stock,";
142  $sql .= " t.sellby as oldsellby,";
143  $sql .= " t.eatby as oldeatby,";
144  $sql .= " t.batch,";
145  $sql .= " t.qty,";
146  $sql .= " t.import_key,";
147  $sql .= " w.fk_entrepot,";
148  $sql .= " w.fk_product,";
149  $sql .= " pl.eatby,";
150  $sql .= " pl.sellby";
151 
152  $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as t INNER JOIN ".MAIN_DB_PREFIX."product_stock w on t.fk_product_stock = w.rowid";
153  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
154  $sql .= " WHERE t.rowid = ".$id;
155 
156  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
157  $resql = $this->db->query($sql);
158  if ($resql)
159  {
160  if ($this->db->num_rows($resql))
161  {
162  $obj = $this->db->fetch_object($resql);
163 
164  $this->id = $obj->rowid;
165  $this->tms = $this->db->jdate($obj->tms);
166  $this->fk_product_stock = $obj->fk_product_stock;
167  $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
168  $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
169  $this->batch = $obj->batch;
170  $this->qty = $obj->qty;
171  $this->import_key = $obj->import_key;
172  $this->warehouseid = $obj->fk_entrepot;
173  $this->fk_product = $obj->fk_product;
174  }
175  $this->db->free($resql);
176 
177  return 1;
178  } else {
179  $this->error = "Error ".$this->db->lasterror();
180  return -1;
181  }
182  }
183 
191  public function update($user = null, $notrigger = 0)
192  {
193  global $conf, $langs;
194  $error = 0;
195 
196  // Clean parameters
197  $this->cleanParam();
198 
199  // TODO Check qty is ok for stock move. Negative may not be allowed.
200  if ($this->qty < 0)
201  {
202  }
203 
204  // Update request
205  $sql = "UPDATE ".MAIN_DB_PREFIX.self::$_table_element." SET";
206  $sql .= " fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock : "null").",";
207  $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').",";
208  $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
209  $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
210  $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
211  $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null")."";
212  $sql .= " WHERE rowid=".$this->id;
213 
214  $this->db->begin();
215 
216  dol_syslog(get_class($this)."::update", LOG_DEBUG);
217  $resql = $this->db->query($sql);
218  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
219 
220  // Commit or rollback
221  if ($error)
222  {
223  foreach ($this->errors as $errmsg)
224  {
225  dol_syslog(get_class($this)."::update ".$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 1;
233  }
234  }
235 
243  public function delete($user, $notrigger = 0)
244  {
245  global $conf, $langs;
246  $error = 0;
247 
248  $this->db->begin();
249 
250  if (!$error)
251  {
252  $sql = "DELETE FROM ".MAIN_DB_PREFIX.self::$_table_element."";
253  $sql .= " WHERE rowid=".$this->id;
254 
255  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
256  $resql = $this->db->query($sql);
257  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
258  }
259 
260  // Commit or rollback
261  if ($error)
262  {
263  foreach ($this->errors as $errmsg)
264  {
265  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
266  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
267  }
268  $this->db->rollback();
269  return -1 * $error;
270  } else {
271  $this->db->commit();
272  return 1;
273  }
274  }
275 
276 
277 
285  public function createFromClone(User $user, $fromid)
286  {
287  $error = 0;
288 
289  $object = new Productbatch($this->db);
290 
291  $this->db->begin();
292 
293  // Load source object
294  $object->fetch($fromid);
295  $object->id = 0;
296  $object->statut = 0;
297 
298  // Clear fields
299  // ...
300 
301  // Create clone
302  $object->context['createfromclone'] = 'createfromclone';
303  $result = $object->create($user);
304 
305  // Other options
306  if ($result < 0)
307  {
308  $this->error = $object->error;
309  $this->errors = array_merge($this->errors, $object->errors);
310  $error++;
311  }
312 
313  if (!$error)
314  {
315  }
316 
317  unset($object->context['createfromclone']);
318 
319  // End
320  if (!$error)
321  {
322  $this->db->commit();
323  return $object->id;
324  } else {
325  $this->db->rollback();
326  return -1;
327  }
328  }
329 
330 
337  public function initAsSpecimen()
338  {
339  $this->id = 0;
340 
341  $this->tms = '';
342  $this->fk_product_stock = '';
343  $this->sellby = '';
344  $this->eatby = '';
345  $this->batch = '';
346  $this->import_key = '';
347  }
348 
354  private function cleanParam()
355  {
356  if (isset($this->fk_product_stock)) $this->fk_product_stock = (int) trim($this->fk_product_stock);
357  if (isset($this->batch)) $this->batch = trim($this->batch);
358  if (isset($this->qty)) $this->qty = (float) trim($this->qty);
359  if (isset($this->import_key)) $this->import_key = trim($this->import_key);
360  }
361 
371  public function find($fk_product_stock = 0, $eatby = '', $sellby = '', $batch_number = '')
372  {
373  global $langs;
374 
375  $where = array();
376  $sql = "SELECT";
377  $sql .= " t.rowid,";
378  $sql .= " t.tms,";
379  $sql .= " t.fk_product_stock,";
380  $sql .= " t.sellby,"; // deprecated
381  $sql .= " t.eatby,"; // deprecated
382  $sql .= " t.batch,";
383  $sql .= " t.qty,";
384  $sql .= " t.import_key";
385  $sql .= " FROM ".MAIN_DB_PREFIX.self::$_table_element." as t";
386  $sql .= " WHERE fk_product_stock=".$fk_product_stock;
387 
388  if (!empty($eatby)) array_push($where, " eatby = '".$this->db->idate($eatby)."'"); // deprecated
389  if (!empty($sellby)) array_push($where, " sellby = '".$this->db->idate($sellby)."'"); // deprecated
390 
391  if (!empty($batch_number)) $sql .= " AND batch = '".$this->db->escape($batch_number)."'";
392 
393  if (!empty($where)) $sql .= " AND (".implode(" OR ", $where).")";
394 
395  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
396  $resql = $this->db->query($sql);
397  if ($resql)
398  {
399  if ($this->db->num_rows($resql))
400  {
401  $obj = $this->db->fetch_object($resql);
402 
403  $this->id = $obj->rowid;
404 
405  $this->tms = $this->db->jdate($obj->tms);
406  $this->fk_product_stock = $obj->fk_product_stock;
407  $this->sellby = $this->db->jdate($obj->sellby);
408  $this->eatby = $this->db->jdate($obj->eatby);
409  $this->batch = $obj->batch;
410  $this->qty = $obj->qty;
411  $this->import_key = $obj->import_key;
412  }
413  $this->db->free($resql);
414 
415  return 1;
416  } else {
417  $this->error = "Error ".$this->db->lasterror();
418  return -1;
419  }
420  }
430  public static function findAll($db, $fk_product_stock, $with_qty = 0, $fk_product = 0)
431  {
432  global $langs;
433  $ret = array();
434 
435  $sql = "SELECT";
436  $sql .= " t.rowid,";
437  $sql .= " t.tms,";
438  $sql .= " t.fk_product_stock,";
439  $sql .= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
440  $sql .= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
441  $sql .= " t.batch,";
442  $sql .= " t.qty,";
443  $sql .= " t.import_key";
444  if ($fk_product > 0)
445  {
446  $sql .= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
447  // TODO May add extrafields to ?
448  }
449  $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as t";
450  if ($fk_product > 0)
451  {
452  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.fk_product = ".$fk_product." AND pl.batch = t.batch";
453  // TODO May add extrafields to ?
454  }
455  $sql .= " WHERE fk_product_stock=".$fk_product_stock;
456  if ($with_qty) $sql .= " AND t.qty <> 0";
457 
458  dol_syslog("productbatch::findAll", LOG_DEBUG);
459  $resql = $db->query($sql);
460  if ($resql) {
461  $num = $db->num_rows($resql);
462  $i = 0;
463  while ($i < $num)
464  {
465  $obj = $db->fetch_object($resql);
466 
467  $tmp = new Productbatch($db);
468  $tmp->id = $obj->rowid;
469  $tmp->lotid = $obj->lotid;
470  $tmp->tms = $db->jdate($obj->tms);
471  $tmp->fk_product_stock = $obj->fk_product_stock;
472  $tmp->sellby = $db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
473  $tmp->eatby = $db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
474  $tmp->batch = $obj->batch;
475  $tmp->qty = $obj->qty;
476  $tmp->import_key = $obj->import_key;
477 
478  $ret[$tmp->batch] = $tmp; // $ret is for a $fk_product_stock and unique key is on $fk_product_stock+batch
479  $i++;
480  }
481  $db->free($resql);
482 
483  return $ret;
484  } else {
485  $error = "Error ".$db->lasterror();
486  return -1;
487  }
488  }
489 
503  public static function findAllForProduct($db, $fk_product, $fk_warehouse = 0, $qty_min = null, $sortfield = null, $sortorder = null)
504  {
505  $productBatchList = array();
506 
507  dol_syslog(__METHOD__.' fk_product='.$fk_product.', fk_warehouse='.$fk_warehouse.', qty_min='.$qty_min.', sortfield='.$sortfield.', sortorder='.$sortorder, LOG_DEBUG);
508 
509  $sql = "SELECT";
510  $sql .= " pl.rowid";
511  $sql .= ", pl.fk_product";
512  $sql .= ", pl.batch";
513  $sql .= ", pl.sellby";
514  $sql .= ", pl.eatby";
515  $sql .= ", pb.qty";
516  $sql .= " FROM ".MAIN_DB_PREFIX."product_lot as pl";
517  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = pl.fk_product";
518  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_batch AS pb ON pl.batch = pb.batch";
519  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock AS ps ON ps.rowid = pb.fk_product_stock";
520  $sql .= " WHERE p.entity IN (".getEntity('product').")";
521  $sql .= " AND pl.fk_product = ".$fk_product;
522  if ($fk_warehouse > 0) {
523  $sql .= " AND ps.fk_entrepot = ".$fk_warehouse;
524  }
525  if ($qty_min !== null) {
526  $sql .= " AND pb.qty > ".$qty_min;
527  }
528  $sql .= $db->order($sortfield, $sortorder);
529 
530  $resql = $db->query($sql);
531  if ($resql) {
532  while ($obj = $db->fetch_object($resql)) {
533  $productBatch = new self($db);
534  $productBatch->id = $obj->rowid;
535  $productBatch->fk_product = $obj->fk_product;
536  $productBatch->batch = $obj->batch;
537  $productBatch->eatby = $db->jdate($obj->eatby);
538  $productBatch->sellby = $db->jdate($obj->sellby);
539  $productBatch->qty = $obj->qty;
540  $productBatchList[] = $productBatch;
541  }
542  $db->free($resql);
543 
544  return $productBatchList;
545  } else {
546  dol_syslog(__METHOD__.' Error: '.$db->lasterror(), LOG_ERR);
547  return -1;
548  }
549  }
550 }
fetch($id)
Load object in memory from the database.
Class to manage Dolibarr users.
Definition: user.class.php:44
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...
__construct($db)
Constructor.
const BATCH_RULE_SELLBY_EATBY_DATES_FIRST
Batches rules.
$conf db
API class for accounts.
Definition: inc.php:54
create($user, $notrigger=0)
Create object into database.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in 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.
update($user=null, $notrigger=0)
Update object into database.
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.
static findAllForProduct($db, $fk_product, $fk_warehouse=0, $qty_min=null, $sortfield=null, $sortorder=null)
Return all batch for a product and a warehouse.
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, ...)