dolibarr 19.0.4
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 * Copyright (C) 2024 Ferran Marcet <fmarcet@2byte.es>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
27require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
28
29
34{
39
43 public $element = 'productbatch';
44
45 private static $_table_element = 'product_batch';
46
47 public $tms = '';
48 public $fk_product_stock;
49
50 public $batch = '';
51 public $qty;
52 public $warehouseid;
53
57 public $fk_product;
58
59 // Properties of the lot
60 public $lotid; // ID in table of the details of properties of each lots
61
66 public $sellby = ''; // dlc
71 public $eatby = ''; // dmd/dluo
72
73
79 public function __construct($db)
80 {
81 $this->db = $db;
82 }
83
84
92 public function create($user, $notrigger = 0)
93 {
94 $error = 0;
95
96 // Clean parameters
97 $this->cleanParam();
98
99 // Check parameters
100 // Put here code to add control on parameters values
101
102 // Insert request
103 $sql = "INSERT INTO ".$this->db->prefix()."product_batch (";
104 $sql .= "fk_product_stock,";
105 $sql .= "sellby,"; // no more used
106 $sql .= "eatby,"; // no more used
107 $sql .= "batch,";
108 $sql .= "qty,";
109 $sql .= "import_key";
110 $sql .= ") VALUES (";
111 $sql .= " ".(!isset($this->fk_product_stock) ? 'NULL' : $this->fk_product_stock).",";
112 $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; // no more used
113 $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; // no more used
114 $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
115 $sql .= " ".(!isset($this->qty) ? 'NULL' : $this->qty).",";
116 $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
117 $sql .= ")";
118
119 $this->db->begin();
120
121 dol_syslog(get_class($this)."::create", LOG_DEBUG);
122 $resql = $this->db->query($sql);
123 if (!$resql) {
124 $error++;
125 $this->errors[] = "Error ".$this->db->lasterror();
126 }
127 if (!$error) {
128 $this->id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
129 }
130
131 // Commit or rollback
132 if ($error) {
133 $this->db->rollback();
134 return -1 * $error;
135 } else {
136 $this->db->commit();
137 return $this->id;
138 }
139 }
140
141
148 public function fetch($id)
149 {
150 $sql = "SELECT";
151 $sql .= " t.rowid,";
152 $sql .= " t.tms,";
153 $sql .= " t.fk_product_stock,";
154 $sql .= " t.sellby as oldsellby,";
155 $sql .= " t.eatby as oldeatby,";
156 $sql .= " t.batch,";
157 $sql .= " t.qty,";
158 $sql .= " t.import_key,";
159 $sql .= " w.fk_entrepot,";
160 $sql .= " w.fk_product,";
161 $sql .= " pl.eatby,";
162 $sql .= " pl.sellby";
163 $sql .= " FROM ".$this->db->prefix()."product_batch as t";
164 $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
165 $sql .= " LEFT JOIN ".$this->db->prefix()."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
166 $sql .= " WHERE t.rowid = ".((int) $id);
167
168 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
169 $resql = $this->db->query($sql);
170 if ($resql) {
171 if ($this->db->num_rows($resql)) {
172 $obj = $this->db->fetch_object($resql);
173
174 $this->id = $obj->rowid;
175 $this->tms = $this->db->jdate($obj->tms);
176 $this->fk_product_stock = $obj->fk_product_stock;
177 $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
178 $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
179 $this->batch = $obj->batch;
180 $this->qty = $obj->qty;
181 $this->import_key = $obj->import_key;
182 $this->warehouseid = $obj->fk_entrepot;
183 $this->fk_product = $obj->fk_product;
184 }
185 $this->db->free($resql);
186
187 return 1;
188 } else {
189 $this->error = "Error ".$this->db->lasterror();
190 return -1;
191 }
192 }
193
201 public function update($user = null, $notrigger = 0)
202 {
203 $error = 0;
204
205 // Clean parameters
206 $this->cleanParam();
207
208 // TODO Check qty is ok for stock move. Negative may not be allowed.
209 if ($this->qty < 0) {
210 }
211
212 // Update request
213 $sql = "UPDATE ".$this->db->prefix().self::$_table_element." SET";
214 $sql .= " fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock : "null").",";
215 $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').",";
216 $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
217 $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
218 $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
219 $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
220 $sql .= " WHERE rowid=".((int) $this->id);
221
222 $this->db->begin();
223
224 dol_syslog(get_class($this)."::update", LOG_DEBUG);
225 $resql = $this->db->query($sql);
226 if (!$resql) {
227 $error++;
228 $this->errors[] = "Error ".$this->db->lasterror();
229 }
230
231 // Commit or rollback
232 if ($error) {
233 foreach ($this->errors as $errmsg) {
234 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
235 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
236 }
237 $this->db->rollback();
238 return -1 * $error;
239 } else {
240 $this->db->commit();
241 return 1;
242 }
243 }
244
252 public function delete($user, $notrigger = 0)
253 {
254 $error = 0;
255
256 $this->db->begin();
257
258 if (!$error) {
259 $sql = "DELETE FROM ".$this->db->prefix().self::$_table_element;
260 $sql .= " WHERE rowid=".((int) $this->id);
261
262 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
263 $resql = $this->db->query($sql);
264 if (!$resql) {
265 $error++;
266 $this->errors[] = "Error ".$this->db->lasterror();
267 }
268 }
269
270 // Commit or rollback
271 if ($error) {
272 foreach ($this->errors as $errmsg) {
273 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
274 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
275 }
276 $this->db->rollback();
277 return -1 * $error;
278 } else {
279 $this->db->commit();
280 return 1;
281 }
282 }
283
284
285
293 public function createFromClone(User $user, $fromid)
294 {
295 $error = 0;
296
297 $object = new Productbatch($this->db);
298
299 $this->db->begin();
300
301 // Load source object
302 $object->fetch($fromid);
303 $object->id = 0;
304 $object->statut = 0;
305
306 // Clear fields
307 // ...
308
309 // Create clone
310 $object->context['createfromclone'] = 'createfromclone';
311 $result = $object->create($user);
312
313 // Other options
314 if ($result < 0) {
315 $this->error = $object->error;
316 $this->errors = array_merge($this->errors, $object->errors);
317 $error++;
318 }
319
320 if (!$error) {
321 }
322
323 unset($object->context['createfromclone']);
324
325 // End
326 if (!$error) {
327 $this->db->commit();
328 return $object->id;
329 } else {
330 $this->db->rollback();
331 return -1;
332 }
333 }
334
335
342 public function initAsSpecimen()
343 {
344 $this->id = 0;
345
346 $this->tms = '';
347 $this->fk_product_stock = '';
348 $this->sellby = '';
349 $this->eatby = '';
350 $this->batch = '';
351 $this->import_key = '';
352 }
353
359 private function cleanParam()
360 {
361 if (isset($this->fk_product_stock)) {
362 $this->fk_product_stock = (int) trim($this->fk_product_stock);
363 }
364 if (isset($this->batch)) {
365 $this->batch = trim($this->batch);
366 }
367 if (isset($this->qty)) {
368 $this->qty = (float) trim($this->qty);
369 }
370 if (isset($this->import_key)) {
371 $this->import_key = trim($this->import_key);
372 }
373 }
374
385 public function find($fk_product_stock = 0, $eatby = '', $sellby = '', $batch_number = '', $fk_warehouse = 0)
386 {
387 $where = array();
388 $sql = "SELECT";
389 $sql .= " t.rowid,";
390 $sql .= " t.tms,";
391 $sql .= " t.fk_product_stock,";
392 $sql .= " t.sellby,"; // deprecated
393 $sql .= " t.eatby,"; // deprecated
394 $sql .= " t.batch,";
395 $sql .= " t.qty,";
396 $sql .= " t.import_key";
397 $sql .= " FROM ".$this->db->prefix().self::$_table_element." as t";
398 if ($fk_product_stock > 0 || empty($fk_warehouse)) {
399 $sql .= " WHERE t.fk_product_stock = ".((int) $fk_product_stock);
400 } else {
401 $sql .= ", ".$this->db->prefix()."product_stock as ps";
402 $sql .= " WHERE t.fk_product_stock = ps.rowid AND ps.fk_entrepot = ".((int) $fk_warehouse);
403 }
404 if (!empty($eatby)) {
405 array_push($where, " eatby = '".$this->db->idate($eatby)."'"); // deprecated
406 }
407 if (!empty($sellby)) {
408 array_push($where, " sellby = '".$this->db->idate($sellby)."'"); // deprecated
409 }
410
411 if (!empty($batch_number)) {
412 $sql .= " AND batch = '".$this->db->escape($batch_number)."'";
413 }
414
415 if (!empty($where)) {
416 $sql .= " AND (".implode(" OR ", $where).")";
417 }
418
419 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
420 $resql = $this->db->query($sql);
421 if ($resql) {
422 if ($this->db->num_rows($resql)) {
423 $obj = $this->db->fetch_object($resql);
424
425 $this->id = $obj->rowid;
426
427 $this->tms = $this->db->jdate($obj->tms);
428 $this->fk_product_stock = $obj->fk_product_stock;
429 $this->sellby = $this->db->jdate($obj->sellby); // deprecated. do no tuse this data.
430 $this->eatby = $this->db->jdate($obj->eatby); // deprecated. do not use this data.
431 $this->batch = $obj->batch;
432 $this->qty = $obj->qty;
433 $this->import_key = $obj->import_key;
434 }
435 $this->db->free($resql);
436
437 return 1;
438 } else {
439 $this->error = "Error ".$this->db->lasterror();
440 return -1;
441 }
442 }
452 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
453 {
454 global $conf;
455
456 $ret = array();
457
458 $sql = "SELECT";
459 $sql .= " t.rowid,";
460 $sql .= " t.tms,";
461 $sql .= " t.fk_product_stock,";
462 $sql .= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
463 $sql .= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
464 $sql .= " t.batch,";
465 $sql .= " t.qty,";
466 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
467 $sql .= " MAX(sm.datem) as date_entree,";
468 }
469 $sql .= " t.import_key";
470 if ($fk_product > 0) {
471 $sql .= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
472 // TODO May add extrafields to ?
473 }
474 $sql .= " FROM ".$dbs->prefix()."product_batch as t";
475 if ($fk_product > 0) { // Add link to the table of details of a lot
476 $sql .= " LEFT JOIN ".$dbs->prefix()."product_lot as pl ON pl.fk_product = ".((int) $fk_product)." AND pl.batch = t.batch";
477 // TODO May add extrafields to ?
478 }
479 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
480 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock AS ps ON (ps.rowid = fk_product_stock)';
481 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'stock_mouvement AS sm ON (sm.batch = t.batch AND ps.fk_entrepot=sm.fk_entrepot AND sm.type_mouvement IN (0,3))';
482 }
483 $sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
484 if ($with_qty) {
485 $sql .= " AND t.qty <> 0";
486 }
487 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
488 $sql .= ' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
489 if ($fk_product > 0) {
490 $sql .= ', pl.rowid, pl.eatby, pl.sellby';
491 }
492 }
493 $sql .= " ORDER BY ";
494 // TODO : use product lifo and fifo when product will implement it
495 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
496 $sql .= 'date_entree ASC,t.batch ASC,';
497 }
498 if ($fk_product > 0) {
499 $sql .= "pl.eatby ASC, pl.sellby ASC, ";
500 }
501 $sql .= "t.eatby ASC, t.sellby ASC ";
502 $sql .= ", t.qty ".(!getDolGlobalString('DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE') ? 'ASC' : 'DESC'); // Note : qty ASC is important for expedition card, to avoid stock fragmentation
503 $sql .= ", t.batch ASC";
504
505 dol_syslog("productbatch::findAll", LOG_DEBUG);
506
507 $resql = $dbs->query($sql);
508 if ($resql) {
509 $num = $dbs->num_rows($resql);
510 $i = 0;
511 while ($i < $num) {
512 $obj = $dbs->fetch_object($resql);
513
514 $tmp = new Productbatch($dbs);
515 $tmp->id = $obj->rowid;
516 $tmp->tms = $dbs->jdate($obj->tms);
517 $tmp->fk_product_stock = $obj->fk_product_stock;
518 $tmp->batch = $obj->batch;
519 $tmp->qty = $obj->qty;
520 $tmp->import_key = $obj->import_key;
521
522 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
523 $tmp->context['stock_entry_date'] = $dbs->jdate($obj->date_entree);
524 }
525
526 if ($fk_product > 0) {
527 // Some properties of the lot
528 $tmp->lotid = $obj->lotid; // ID in table of the details of properties of each lots
529 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
530 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
531 }
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 $dbs->free($resql);
537
538 return $ret;
539 } else {
540 //$error = "Error ".$dbs->lasterror();
541 return -1;
542 }
543 }
544
557 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min = null, $sortfield = null, $sortorder = null)
558 {
559 $productBatchList = array();
560
561 dol_syslog(__METHOD__.' fk_product='.$fk_product.', fk_warehouse='.$fk_warehouse.', qty_min='.$qty_min.', sortfield='.$sortfield.', sortorder='.$sortorder, LOG_DEBUG);
562
563 $sql = "SELECT";
564 $sql .= " pl.rowid";
565 $sql .= ", pl.fk_product";
566 $sql .= ", pl.batch";
567 $sql .= ", pl.sellby";
568 $sql .= ", pl.eatby";
569 $sql .= ", pb.qty";
570 $sql .= " FROM ".$this->db->prefix()."product_lot as pl";
571 $sql .= " LEFT JOIN ".$this->db->prefix()."product as p ON p.rowid = pl.fk_product";
572 $sql .= " LEFT JOIN ".$this->db->prefix()."product_batch AS pb ON pl.batch = pb.batch";
573 $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
574 $sql .= " WHERE p.entity IN (".getEntity('product').")";
575 $sql .= " AND pl.fk_product = ".((int) $fk_product);
576 if ($fk_warehouse > 0) {
577 $sql .= " AND ps.fk_entrepot = ".((int) $fk_warehouse);
578 }
579 if ($qty_min !== null) {
580 $sql .= " AND pb.qty > ".((float) price2num($qty_min, 'MS'));
581 }
582 $sql .= $this->db->order($sortfield, $sortorder);
583
584 $resql = $this->db->query($sql);
585 if ($resql) {
586 while ($obj = $this->db->fetch_object($resql)) {
587 $productBatch = new self($this->db);
588 $productBatch->id = $obj->rowid;
589 $productBatch->fk_product = $obj->fk_product;
590 $productBatch->batch = $obj->batch;
591 $productBatch->eatby = $this->db->jdate($obj->eatby);
592 $productBatch->sellby = $this->db->jdate($obj->sellby);
593 $productBatch->qty = $obj->qty;
594 $productBatchList[] = $productBatch;
595 }
596 $this->db->free($resql);
597
598 return $productBatchList;
599 } else {
600 dol_syslog(__METHOD__.' Error: '.$this->db->lasterror(), LOG_ERR);
601 return -1;
602 }
603 }
604}
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.
create($user, $notrigger=0)
Create object into database.
find($fk_product_stock=0, $eatby='', $sellby='', $batch_number='', $fk_warehouse=0)
Find first detail record that match eather eat-by or sell-by or batch within given warehouse.
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 known for a product and a warehouse (batch that was one day used)
__construct($db)
Constructor.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Class to manage Dolibarr users.
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.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.