dolibarr  16.0.5
productbatch.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2021 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 = ''; // dlc
48  public $eatby = ''; // dmd/dluo
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 ".$this->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) {
111  $error++; $this->errors[] = "Error ".$this->db->lasterror();
112  }
113  if (!$error) {
114  $this->id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
115  }
116 
117  // Commit or rollback
118  if ($error) {
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 ".$this->db->prefix()."product_batch as t INNER JOIN ".$this->db->prefix()."product_stock w on t.fk_product_stock = w.rowid";
153  $sql .= " LEFT JOIN ".$this->db->prefix()."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
154  $sql .= " WHERE t.rowid = ".((int) $id);
155 
156  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
157  $resql = $this->db->query($sql);
158  if ($resql) {
159  if ($this->db->num_rows($resql)) {
160  $obj = $this->db->fetch_object($resql);
161 
162  $this->id = $obj->rowid;
163  $this->tms = $this->db->jdate($obj->tms);
164  $this->fk_product_stock = $obj->fk_product_stock;
165  $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
166  $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
167  $this->batch = $obj->batch;
168  $this->qty = $obj->qty;
169  $this->import_key = $obj->import_key;
170  $this->warehouseid = $obj->fk_entrepot;
171  $this->fk_product = $obj->fk_product;
172  }
173  $this->db->free($resql);
174 
175  return 1;
176  } else {
177  $this->error = "Error ".$this->db->lasterror();
178  return -1;
179  }
180  }
181 
189  public function update($user = null, $notrigger = 0)
190  {
191  global $conf, $langs;
192  $error = 0;
193 
194  // Clean parameters
195  $this->cleanParam();
196 
197  // TODO Check qty is ok for stock move. Negative may not be allowed.
198  if ($this->qty < 0) {
199  }
200 
201  // Update request
202  $sql = "UPDATE ".$this->db->prefix().self::$_table_element." SET";
203  $sql .= " fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock : "null").",";
204  $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').",";
205  $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
206  $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
207  $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
208  $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null")."";
209  $sql .= " WHERE rowid=".((int) $this->id);
210 
211  $this->db->begin();
212 
213  dol_syslog(get_class($this)."::update", LOG_DEBUG);
214  $resql = $this->db->query($sql);
215  if (!$resql) {
216  $error++; $this->errors[] = "Error ".$this->db->lasterror();
217  }
218 
219  // Commit or rollback
220  if ($error) {
221  foreach ($this->errors as $errmsg) {
222  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
223  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
224  }
225  $this->db->rollback();
226  return -1 * $error;
227  } else {
228  $this->db->commit();
229  return 1;
230  }
231  }
232 
240  public function delete($user, $notrigger = 0)
241  {
242  global $conf, $langs;
243  $error = 0;
244 
245  $this->db->begin();
246 
247  if (!$error) {
248  $sql = "DELETE FROM ".$this->db->prefix().self::$_table_element."";
249  $sql .= " WHERE rowid=".((int) $this->id);
250 
251  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
252  $resql = $this->db->query($sql);
253  if (!$resql) {
254  $error++; $this->errors[] = "Error ".$this->db->lasterror();
255  }
256  }
257 
258  // Commit or rollback
259  if ($error) {
260  foreach ($this->errors as $errmsg) {
261  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
262  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
263  }
264  $this->db->rollback();
265  return -1 * $error;
266  } else {
267  $this->db->commit();
268  return 1;
269  }
270  }
271 
272 
273 
281  public function createFromClone(User $user, $fromid)
282  {
283  $error = 0;
284 
285  $object = new Productbatch($this->db);
286 
287  $this->db->begin();
288 
289  // Load source object
290  $object->fetch($fromid);
291  $object->id = 0;
292  $object->statut = 0;
293 
294  // Clear fields
295  // ...
296 
297  // Create clone
298  $object->context['createfromclone'] = 'createfromclone';
299  $result = $object->create($user);
300 
301  // Other options
302  if ($result < 0) {
303  $this->error = $object->error;
304  $this->errors = array_merge($this->errors, $object->errors);
305  $error++;
306  }
307 
308  if (!$error) {
309  }
310 
311  unset($object->context['createfromclone']);
312 
313  // End
314  if (!$error) {
315  $this->db->commit();
316  return $object->id;
317  } else {
318  $this->db->rollback();
319  return -1;
320  }
321  }
322 
323 
330  public function initAsSpecimen()
331  {
332  $this->id = 0;
333 
334  $this->tms = '';
335  $this->fk_product_stock = '';
336  $this->sellby = '';
337  $this->eatby = '';
338  $this->batch = '';
339  $this->import_key = '';
340  }
341 
347  private function cleanParam()
348  {
349  if (isset($this->fk_product_stock)) {
350  $this->fk_product_stock = (int) trim($this->fk_product_stock);
351  }
352  if (isset($this->batch)) {
353  $this->batch = trim($this->batch);
354  }
355  if (isset($this->qty)) {
356  $this->qty = (float) trim($this->qty);
357  }
358  if (isset($this->import_key)) {
359  $this->import_key = trim($this->import_key);
360  }
361  }
362 
372  public function find($fk_product_stock = 0, $eatby = '', $sellby = '', $batch_number = '')
373  {
374  global $langs;
375 
376  $where = array();
377  $sql = "SELECT";
378  $sql .= " t.rowid,";
379  $sql .= " t.tms,";
380  $sql .= " t.fk_product_stock,";
381  $sql .= " t.sellby,"; // deprecated
382  $sql .= " t.eatby,"; // deprecated
383  $sql .= " t.batch,";
384  $sql .= " t.qty,";
385  $sql .= " t.import_key";
386  $sql .= " FROM ".$this->db->prefix().self::$_table_element." as t";
387  $sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
388 
389  if (!empty($eatby)) {
390  array_push($where, " eatby = '".$this->db->idate($eatby)."'"); // deprecated
391  }
392  if (!empty($sellby)) {
393  array_push($where, " sellby = '".$this->db->idate($sellby)."'"); // deprecated
394  }
395 
396  if (!empty($batch_number)) {
397  $sql .= " AND batch = '".$this->db->escape($batch_number)."'";
398  }
399 
400  if (!empty($where)) {
401  $sql .= " AND (".implode(" OR ", $where).")";
402  }
403 
404  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
405  $resql = $this->db->query($sql);
406  if ($resql) {
407  if ($this->db->num_rows($resql)) {
408  $obj = $this->db->fetch_object($resql);
409 
410  $this->id = $obj->rowid;
411 
412  $this->tms = $this->db->jdate($obj->tms);
413  $this->fk_product_stock = $obj->fk_product_stock;
414  $this->sellby = $this->db->jdate($obj->sellby);
415  $this->eatby = $this->db->jdate($obj->eatby);
416  $this->batch = $obj->batch;
417  $this->qty = $obj->qty;
418  $this->import_key = $obj->import_key;
419  }
420  $this->db->free($resql);
421 
422  return 1;
423  } else {
424  $this->error = "Error ".$this->db->lasterror();
425  return -1;
426  }
427  }
437  public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
438  {
439  global $conf;
440 
441  $ret = array();
442 
443  $sql = "SELECT";
444  $sql .= " t.rowid,";
445  $sql .= " t.tms,";
446  $sql .= " t.fk_product_stock,";
447  $sql .= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
448  $sql .= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
449  $sql .= " t.batch,";
450  $sql .= " t.qty,";
451  $sql .= " t.import_key";
452  if ($fk_product > 0) {
453  $sql .= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
454  // TODO May add extrafields to ?
455  }
456  $sql .= " FROM ".$dbs->prefix()."product_batch as t";
457  if ($fk_product > 0) {
458  $sql .= " LEFT JOIN ".$dbs->prefix()."product_lot as pl ON pl.fk_product = ".((int) $fk_product)." AND pl.batch = t.batch";
459  // TODO May add extrafields to ?
460  }
461  $sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
462  if ($with_qty) {
463  $sql .= " AND t.qty <> 0";
464  }
465 
466  $sql .= " ORDER BY ";
467  // TODO : use product lifo and fifo when product will implement it
468  if ($fk_product > 0) { $sql .= "pl.eatby ASC, pl.sellby ASC, "; }
469  $sql .= "t.eatby ASC, t.sellby ASC ";
470  $sql .= ", t.qty ".(!empty($conf->global->DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE)?'DESC':'ASC'); // Note : qty ASC is important for expedition card, to avoid stock fragmentation
471 
472  dol_syslog("productbatch::findAll", LOG_DEBUG);
473  $resql = $dbs->query($sql);
474  if ($resql) {
475  $num = $dbs->num_rows($resql);
476  $i = 0;
477  while ($i < $num) {
478  $obj = $dbs->fetch_object($resql);
479 
480  $tmp = new Productbatch($dbs);
481  $tmp->id = $obj->rowid;
482  $tmp->lotid = $obj->lotid;
483  $tmp->tms = $dbs->jdate($obj->tms);
484  $tmp->fk_product_stock = $obj->fk_product_stock;
485  $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
486  $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
487  $tmp->batch = $obj->batch;
488  $tmp->qty = $obj->qty;
489  $tmp->import_key = $obj->import_key;
490 
491  $ret[$tmp->batch] = $tmp; // $ret is for a $fk_product_stock and unique key is on $fk_product_stock+batch
492  $i++;
493  }
494  $dbs->free($resql);
495 
496  return $ret;
497  } else {
498  $error = "Error ".$dbs->lasterror();
499  return -1;
500  }
501  }
502 
515  public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min = null, $sortfield = null, $sortorder = null)
516  {
517  $productBatchList = array();
518 
519  dol_syslog(__METHOD__.' fk_product='.$fk_product.', fk_warehouse='.$fk_warehouse.', qty_min='.$qty_min.', sortfield='.$sortfield.', sortorder='.$sortorder, LOG_DEBUG);
520 
521  $sql = "SELECT";
522  $sql .= " pl.rowid";
523  $sql .= ", pl.fk_product";
524  $sql .= ", pl.batch";
525  $sql .= ", pl.sellby";
526  $sql .= ", pl.eatby";
527  $sql .= ", pb.qty";
528  $sql .= " FROM ".$this->db->prefix()."product_lot as pl";
529  $sql .= " LEFT JOIN ".$this->db->prefix()."product as p ON p.rowid = pl.fk_product";
530  $sql .= " LEFT JOIN ".$this->db->prefix()."product_batch AS pb ON pl.batch = pb.batch";
531  $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
532  $sql .= " WHERE p.entity IN (".getEntity('product').")";
533  $sql .= " AND pl.fk_product = ".((int) $fk_product);
534  if ($fk_warehouse > 0) {
535  $sql .= " AND ps.fk_entrepot = ".((int) $fk_warehouse);
536  }
537  if ($qty_min !== null) {
538  $sql .= " AND pb.qty > ".((float) price2num($qty_min, 'MS'));
539  }
540  $sql .= $this->db->order($sortfield, $sortorder);
541 
542  $resql = $this->db->query($sql);
543  if ($resql) {
544  while ($obj = $this->db->fetch_object($resql)) {
545  $productBatch = new self($this->db);
546  $productBatch->id = $obj->rowid;
547  $productBatch->fk_product = $obj->fk_product;
548  $productBatch->batch = $obj->batch;
549  $productBatch->eatby = $this->db->jdate($obj->eatby);
550  $productBatch->sellby = $this->db->jdate($obj->sellby);
551  $productBatch->qty = $obj->qty;
552  $productBatchList[] = $productBatch;
553  }
554  $this->db->free($resql);
555 
556  return $productBatchList;
557  } else {
558  dol_syslog(__METHOD__.' Error: '.$this->db->lasterror(), LOG_ERR);
559  return -1;
560  }
561  }
562 }
Productbatch\findAll
static findAll($dbs, $fk_product_stock, $with_qty=0, $fk_product=0)
Return all batch detail records for a given product and warehouse.
Definition: productbatch.class.php:437
db
$conf db
API class for accounts.
Definition: inc.php:41
Productbatch
Manage record for batch number management.
Definition: productbatch.class.php:31
Productbatch\createFromClone
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Definition: productbatch.class.php:281
Productbatch\find
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.
Definition: productbatch.class.php:372
Productbatch\__construct
__construct($db)
Constructor.
Definition: productbatch.class.php:65
Productbatch\update
update($user=null, $notrigger=0)
Update object into database.
Definition: productbatch.class.php:189
Productbatch\create
create($user, $notrigger=0)
Create object into database.
Definition: productbatch.class.php:78
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
Productbatch\cleanParam
cleanParam()
Clean fields (triming)
Definition: productbatch.class.php:347
Productbatch\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: productbatch.class.php:330
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Productbatch\findAllForProduct
findAllForProduct($fk_product, $fk_warehouse=0, $qty_min=null, $sortfield=null, $sortorder=null)
Return all batch for a product and a warehouse.
Definition: productbatch.class.php:515
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
Productbatch\fetch
fetch($id)
Load object in memory from the database.
Definition: productbatch.class.php:134
Productbatch\$_table_element
static $_table_element
Name of table without prefix where object is stored.
Definition: productbatch.class.php:43
Productbatch\BATCH_RULE_SELLBY_EATBY_DATES_FIRST
const BATCH_RULE_SELLBY_EATBY_DATES_FIRST
Batches rules.
Definition: productbatch.class.php:36
float
div float
Buy price without taxes.
Definition: style.css.php:809