dolibarr  18.0.0
productbatch.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2023 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 
26 require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
27 
28 
33 {
38 
42  public $element = 'productbatch';
43 
44  private static $_table_element = 'product_batch';
45 
46  public $tms = '';
47  public $fk_product_stock;
48 
49  public $batch = '';
50  public $qty;
51  public $warehouseid;
52 
56  public $fk_product;
57 
58  // Properties of the lot
59  public $lotid; // ID in table of the details of properties of each lots
60  public $sellby = ''; // dlc
61  public $eatby = ''; // dmd/dluo
62 
63 
69  public function __construct($db)
70  {
71  $this->db = $db;
72  }
73 
74 
82  public function create($user, $notrigger = 0)
83  {
84  global $conf, $langs;
85  $error = 0;
86 
87  // Clean parameters
88  $this->cleanParam();
89 
90  // Check parameters
91  // Put here code to add control on parameters values
92 
93  // Insert request
94  $sql = "INSERT INTO ".$this->db->prefix()."product_batch (";
95  $sql .= "fk_product_stock,";
96  $sql .= "sellby,"; // no more used
97  $sql .= "eatby,"; // no more used
98  $sql .= "batch,";
99  $sql .= "qty,";
100  $sql .= "import_key";
101  $sql .= ") VALUES (";
102  $sql .= " ".(!isset($this->fk_product_stock) ? 'NULL' : $this->fk_product_stock).",";
103  $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; // no more used
104  $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; // no more used
105  $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
106  $sql .= " ".(!isset($this->qty) ? 'NULL' : $this->qty).",";
107  $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
108  $sql .= ")";
109 
110  $this->db->begin();
111 
112  dol_syslog(get_class($this)."::create", LOG_DEBUG);
113  $resql = $this->db->query($sql);
114  if (!$resql) {
115  $error++; $this->errors[] = "Error ".$this->db->lasterror();
116  }
117  if (!$error) {
118  $this->id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
119  }
120 
121  // Commit or rollback
122  if ($error) {
123  $this->db->rollback();
124  return -1 * $error;
125  } else {
126  $this->db->commit();
127  return $this->id;
128  }
129  }
130 
131 
138  public function fetch($id)
139  {
140  global $langs;
141  $sql = "SELECT";
142  $sql .= " t.rowid,";
143  $sql .= " t.tms,";
144  $sql .= " t.fk_product_stock,";
145  $sql .= " t.sellby as oldsellby,";
146  $sql .= " t.eatby as oldeatby,";
147  $sql .= " t.batch,";
148  $sql .= " t.qty,";
149  $sql .= " t.import_key,";
150  $sql .= " w.fk_entrepot,";
151  $sql .= " w.fk_product,";
152  $sql .= " pl.eatby,";
153  $sql .= " pl.sellby";
154  $sql .= " FROM ".$this->db->prefix()."product_batch as t";
155  $sql .= " INNER JOIN ".$this->db->prefix()."product_stock w on t.fk_product_stock = w.rowid"; // llx_product_stock is a parent table so this link does NOT generate duplicate record
156  $sql .= " LEFT JOIN ".$this->db->prefix()."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
157  $sql .= " WHERE t.rowid = ".((int) $id);
158 
159  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
160  $resql = $this->db->query($sql);
161  if ($resql) {
162  if ($this->db->num_rows($resql)) {
163  $obj = $this->db->fetch_object($resql);
164 
165  $this->id = $obj->rowid;
166  $this->tms = $this->db->jdate($obj->tms);
167  $this->fk_product_stock = $obj->fk_product_stock;
168  $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
169  $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
170  $this->batch = $obj->batch;
171  $this->qty = $obj->qty;
172  $this->import_key = $obj->import_key;
173  $this->warehouseid = $obj->fk_entrepot;
174  $this->fk_product = $obj->fk_product;
175  }
176  $this->db->free($resql);
177 
178  return 1;
179  } else {
180  $this->error = "Error ".$this->db->lasterror();
181  return -1;
182  }
183  }
184 
192  public function update($user = null, $notrigger = 0)
193  {
194  global $conf, $langs;
195  $error = 0;
196 
197  // Clean parameters
198  $this->cleanParam();
199 
200  // TODO Check qty is ok for stock move. Negative may not be allowed.
201  if ($this->qty < 0) {
202  }
203 
204  // Update request
205  $sql = "UPDATE ".$this->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=".((int) $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) {
219  $error++; $this->errors[] = "Error ".$this->db->lasterror();
220  }
221 
222  // Commit or rollback
223  if ($error) {
224  foreach ($this->errors as $errmsg) {
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  $sql = "DELETE FROM ".$this->db->prefix().self::$_table_element;
252  $sql .= " WHERE rowid=".((int) $this->id);
253 
254  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
255  $resql = $this->db->query($sql);
256  if (!$resql) {
257  $error++; $this->errors[] = "Error ".$this->db->lasterror();
258  }
259  }
260 
261  // Commit or rollback
262  if ($error) {
263  foreach ($this->errors as $errmsg) {
264  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
265  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
266  }
267  $this->db->rollback();
268  return -1 * $error;
269  } else {
270  $this->db->commit();
271  return 1;
272  }
273  }
274 
275 
276 
284  public function createFromClone(User $user, $fromid)
285  {
286  $error = 0;
287 
288  $object = new Productbatch($this->db);
289 
290  $this->db->begin();
291 
292  // Load source object
293  $object->fetch($fromid);
294  $object->id = 0;
295  $object->statut = 0;
296 
297  // Clear fields
298  // ...
299 
300  // Create clone
301  $object->context['createfromclone'] = 'createfromclone';
302  $result = $object->create($user);
303 
304  // Other options
305  if ($result < 0) {
306  $this->error = $object->error;
307  $this->errors = array_merge($this->errors, $object->errors);
308  $error++;
309  }
310 
311  if (!$error) {
312  }
313 
314  unset($object->context['createfromclone']);
315 
316  // End
317  if (!$error) {
318  $this->db->commit();
319  return $object->id;
320  } else {
321  $this->db->rollback();
322  return -1;
323  }
324  }
325 
326 
333  public function initAsSpecimen()
334  {
335  $this->id = 0;
336 
337  $this->tms = '';
338  $this->fk_product_stock = '';
339  $this->sellby = '';
340  $this->eatby = '';
341  $this->batch = '';
342  $this->import_key = '';
343  }
344 
350  private function cleanParam()
351  {
352  if (isset($this->fk_product_stock)) {
353  $this->fk_product_stock = (int) trim($this->fk_product_stock);
354  }
355  if (isset($this->batch)) {
356  $this->batch = trim($this->batch);
357  }
358  if (isset($this->qty)) {
359  $this->qty = (float) trim($this->qty);
360  }
361  if (isset($this->import_key)) {
362  $this->import_key = trim($this->import_key);
363  }
364  }
365 
375  public function find($fk_product_stock = 0, $eatby = '', $sellby = '', $batch_number = '')
376  {
377  global $langs;
378 
379  $where = array();
380  $sql = "SELECT";
381  $sql .= " t.rowid,";
382  $sql .= " t.tms,";
383  $sql .= " t.fk_product_stock,";
384  $sql .= " t.sellby,"; // deprecated
385  $sql .= " t.eatby,"; // deprecated
386  $sql .= " t.batch,";
387  $sql .= " t.qty,";
388  $sql .= " t.import_key";
389  $sql .= " FROM ".$this->db->prefix().self::$_table_element." as t";
390  $sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
391 
392  if (!empty($eatby)) {
393  array_push($where, " eatby = '".$this->db->idate($eatby)."'"); // deprecated
394  }
395  if (!empty($sellby)) {
396  array_push($where, " sellby = '".$this->db->idate($sellby)."'"); // deprecated
397  }
398 
399  if (!empty($batch_number)) {
400  $sql .= " AND batch = '".$this->db->escape($batch_number)."'";
401  }
402 
403  if (!empty($where)) {
404  $sql .= " AND (".implode(" OR ", $where).")";
405  }
406 
407  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
408  $resql = $this->db->query($sql);
409  if ($resql) {
410  if ($this->db->num_rows($resql)) {
411  $obj = $this->db->fetch_object($resql);
412 
413  $this->id = $obj->rowid;
414 
415  $this->tms = $this->db->jdate($obj->tms);
416  $this->fk_product_stock = $obj->fk_product_stock;
417  $this->sellby = $this->db->jdate($obj->sellby);
418  $this->eatby = $this->db->jdate($obj->eatby);
419  $this->batch = $obj->batch;
420  $this->qty = $obj->qty;
421  $this->import_key = $obj->import_key;
422  }
423  $this->db->free($resql);
424 
425  return 1;
426  } else {
427  $this->error = "Error ".$this->db->lasterror();
428  return -1;
429  }
430  }
440  public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
441  {
442  global $conf;
443 
444  $ret = array();
445 
446  $sql = "SELECT";
447  $sql .= " t.rowid,";
448  $sql .= " t.tms,";
449  $sql .= " t.fk_product_stock,";
450  $sql .= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
451  $sql .= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
452  $sql .= " t.batch,";
453  $sql .= " t.qty,";
454  $sql .= " t.import_key";
455  if ($fk_product > 0) {
456  $sql .= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
457  // TODO May add extrafields to ?
458  }
459  $sql .= " FROM ".$dbs->prefix()."product_batch as t";
460  if ($fk_product > 0) { // Add link to the table of details of a lot
461  $sql .= " LEFT JOIN ".$dbs->prefix()."product_lot as pl ON pl.fk_product = ".((int) $fk_product)." AND pl.batch = t.batch";
462  // TODO May add extrafields to ?
463  }
464  $sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
465  if ($with_qty) {
466  $sql .= " AND t.qty <> 0";
467  }
468 
469  $sql .= " ORDER BY ";
470  // TODO : use product lifo and fifo when product will implement it
471  if ($fk_product > 0) { $sql .= "pl.eatby ASC, pl.sellby ASC, "; }
472  $sql .= "t.eatby ASC, t.sellby ASC ";
473  $sql .= ", t.qty ".(empty($conf->global->DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE)?'ASC':'DESC'); // Note : qty ASC is important for expedition card, to avoid stock fragmentation
474 
475  dol_syslog("productbatch::findAll", LOG_DEBUG);
476 
477  $resql = $dbs->query($sql);
478  if ($resql) {
479  $num = $dbs->num_rows($resql);
480  $i = 0;
481  while ($i < $num) {
482  $obj = $dbs->fetch_object($resql);
483 
484  $tmp = new Productbatch($dbs);
485  $tmp->id = $obj->rowid;
486  $tmp->tms = $dbs->jdate($obj->tms);
487  $tmp->fk_product_stock = $obj->fk_product_stock;
488  $tmp->batch = $obj->batch;
489  $tmp->qty = $obj->qty;
490  $tmp->import_key = $obj->import_key;
491  // Some properties of the lot
492  $tmp->lotid = $obj->lotid; // ID in table of the details of properties of each lots
493  $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
494  $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
495 
496  $ret[$tmp->batch] = $tmp; // $ret is for a $fk_product_stock and unique key is on $fk_product_stock+batch
497  $i++;
498  }
499  $dbs->free($resql);
500 
501  return $ret;
502  } else {
503  $error = "Error ".$dbs->lasterror();
504  return -1;
505  }
506  }
507 
520  public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min = null, $sortfield = null, $sortorder = null)
521  {
522  $productBatchList = array();
523 
524  dol_syslog(__METHOD__.' fk_product='.$fk_product.', fk_warehouse='.$fk_warehouse.', qty_min='.$qty_min.', sortfield='.$sortfield.', sortorder='.$sortorder, LOG_DEBUG);
525 
526  $sql = "SELECT";
527  $sql .= " pl.rowid";
528  $sql .= ", pl.fk_product";
529  $sql .= ", pl.batch";
530  $sql .= ", pl.sellby";
531  $sql .= ", pl.eatby";
532  $sql .= ", pb.qty";
533  $sql .= " FROM ".$this->db->prefix()."product_lot as pl";
534  $sql .= " LEFT JOIN ".$this->db->prefix()."product as p ON p.rowid = pl.fk_product";
535  $sql .= " LEFT JOIN ".$this->db->prefix()."product_batch AS pb ON pl.batch = pb.batch";
536  $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
537  $sql .= " WHERE p.entity IN (".getEntity('product').")";
538  $sql .= " AND pl.fk_product = ".((int) $fk_product);
539  if ($fk_warehouse > 0) {
540  $sql .= " AND ps.fk_entrepot = ".((int) $fk_warehouse);
541  }
542  if ($qty_min !== null) {
543  $sql .= " AND pb.qty > ".((float) price2num($qty_min, 'MS'));
544  }
545  $sql .= $this->db->order($sortfield, $sortorder);
546 
547  $resql = $this->db->query($sql);
548  if ($resql) {
549  while ($obj = $this->db->fetch_object($resql)) {
550  $productBatch = new self($this->db);
551  $productBatch->id = $obj->rowid;
552  $productBatch->fk_product = $obj->fk_product;
553  $productBatch->batch = $obj->batch;
554  $productBatch->eatby = $this->db->jdate($obj->eatby);
555  $productBatch->sellby = $this->db->jdate($obj->sellby);
556  $productBatch->qty = $obj->qty;
557  $productBatchList[] = $productBatch;
558  }
559  $this->db->free($resql);
560 
561  return $productBatchList;
562  } else {
563  dol_syslog(__METHOD__.' Error: '.$this->db->lasterror(), LOG_ERR);
564  return -1;
565  }
566  }
567 }
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Manage record for batch number management.
update($user=null, $notrigger=0)
Update object into database.
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.
create($user, $notrigger=0)
Create object into database.
fetch($id)
Load object in memory from the database.
cleanParam()
Clean fields (triming)
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
const BATCH_RULE_SELLBY_EATBY_DATES_FIRST
Batches rules.
static $_table_element
Name of table without prefix where object is stored.
static findAll($dbs, $fk_product_stock, $with_qty=0, $fk_product=0)
Return all batch detail records for a given product and warehouse.
findAllForProduct($fk_product, $fk_warehouse=0, $qty_min=null, $sortfield=null, $sortorder=null)
Return all batch for a product and a warehouse.
__construct($db)
Constructor.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Class to manage Dolibarr users.
Definition: user.class.php:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
div float
Buy price without taxes.
Definition: style.css.php:921