dolibarr 22.0.5
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 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024 Ferran Marcet <fmarcet@2byte.es>
6 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
29require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
30
31
36{
41
45 public $element = 'productbatch';
46
50 private static $_table_element = 'product_batch';
51
55 public $fk_product_stock;
56
60 public $batch = '';
61
65 public $qty;
66
70 public $warehouseid;
71
75 public $fk_product;
76
81 public $lotid;
82
87 public $sellby = ''; // dlc
92 public $eatby = ''; // dmd/dluo
93
94
100 public function __construct($db)
101 {
102 $this->db = $db;
103 }
104
105
113 public function create($user, $notrigger = 0)
114 {
115 $error = 0;
116
117 // Clean parameters
118 $this->cleanParam();
119
120 // Check parameters
121 // Put here code to add control on parameters values
122
123 // Insert request
124 $sql = "INSERT INTO ".$this->db->prefix()."product_batch (";
125 $sql .= "fk_product_stock,";
126 $sql .= "sellby,"; // no more used
127 $sql .= "eatby,"; // no more used
128 $sql .= "batch,";
129 $sql .= "qty,";
130 $sql .= "import_key";
131 $sql .= ") VALUES (";
132 $sql .= " ".(!isset($this->fk_product_stock) ? 'NULL' : $this->fk_product_stock).",";
133 $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; // no more used
134 $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; // no more used
135 $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
136 $sql .= " ".(!isset($this->qty) ? 'NULL' : $this->qty).",";
137 $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
138 $sql .= ")";
139
140 $this->db->begin();
141
142 dol_syslog(get_class($this)."::create", LOG_DEBUG);
143 $resql = $this->db->query($sql);
144 if (!$resql) {
145 $error++;
146 $this->errors[] = "Error ".$this->db->lasterror();
147 }
148 if (!$error) {
149 $this->id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
150 }
151
152 // Commit or rollback
153 if ($error) {
154 $this->db->rollback();
155 return -1 * $error;
156 } else {
157 $this->db->commit();
158 return $this->id;
159 }
160 }
161
162
169 public function fetch($id)
170 {
171 $sql = "SELECT";
172 $sql .= " t.rowid,";
173 $sql .= " t.tms,";
174 $sql .= " t.fk_product_stock,";
175 $sql .= " t.sellby as oldsellby,";
176 $sql .= " t.eatby as oldeatby,";
177 $sql .= " t.batch,";
178 $sql .= " t.qty,";
179 $sql .= " t.import_key,";
180 $sql .= " w.fk_entrepot,";
181 $sql .= " w.fk_product,";
182 $sql .= " pl.eatby,";
183 $sql .= " pl.sellby";
184 $sql .= " FROM ".$this->db->prefix()."product_batch as t";
185 $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
186 $sql .= " LEFT JOIN ".$this->db->prefix()."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
187 $sql .= " WHERE t.rowid = ".((int) $id);
188
189 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
190 $resql = $this->db->query($sql);
191 if ($resql) {
192 if ($this->db->num_rows($resql)) {
193 $obj = $this->db->fetch_object($resql);
194
195 $this->id = $obj->rowid;
196 $this->tms = $this->db->jdate($obj->tms);
197 $this->fk_product_stock = $obj->fk_product_stock;
198 $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
199 $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
200 $this->batch = $obj->batch;
201 $this->qty = $obj->qty;
202 $this->import_key = $obj->import_key;
203 $this->warehouseid = $obj->fk_entrepot;
204 $this->fk_product = $obj->fk_product;
205 }
206 $this->db->free($resql);
207
208 return 1;
209 } else {
210 $this->error = "Error ".$this->db->lasterror();
211 return -1;
212 }
213 }
214
222 public function update($user = null, $notrigger = 0)
223 {
224 $error = 0;
225
226 // Clean parameters
227 $this->cleanParam();
228
229 // TODO Check qty is ok for stock move. Negative may not be allowed.
230 /*
231 if ($this->qty < 0) {
232 }
233 */
234
235 // Update request
236 $sql = "UPDATE ".$this->db->prefix().self::$_table_element." SET";
237 $sql .= " fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock : "null").",";
238 $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').",";
239 $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
240 $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
241 $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
242 $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
243 $sql .= " WHERE rowid=".((int) $this->id);
244
245 $this->db->begin();
246
247 dol_syslog(get_class($this)."::update", LOG_DEBUG);
248 $resql = $this->db->query($sql);
249 if (!$resql) {
250 $error++;
251 $this->errors[] = "Error ".$this->db->lasterror();
252 }
253
254 // Commit or rollback
255 if ($error) {
256 foreach ($this->errors as $errmsg) {
257 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
258 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
259 }
260 $this->db->rollback();
261 return -1 * $error;
262 } else {
263 $this->db->commit();
264 return 1;
265 }
266 }
267
275 public function delete($user, $notrigger = 0)
276 {
277 $error = 0;
278
279 $this->db->begin();
280
281 if (!$error) {
282 $sql = "DELETE FROM ".$this->db->prefix().self::$_table_element;
283 $sql .= " WHERE rowid=".((int) $this->id);
284
285 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
286 $resql = $this->db->query($sql);
287 if (!$resql) {
288 $error++;
289 $this->errors[] = "Error ".$this->db->lasterror();
290 }
291 }
292
293 // Commit or rollback
294 if ($error) {
295 foreach ($this->errors as $errmsg) {
296 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
297 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
298 }
299 $this->db->rollback();
300 return -1 * $error;
301 } else {
302 $this->db->commit();
303 return 1;
304 }
305 }
306
307
308
316 public function createFromClone(User $user, $fromid)
317 {
318 $error = 0;
319
320 $object = new Productbatch($this->db);
321
322 $this->db->begin();
323
324 // Load source object
325 $object->fetch($fromid);
326 $object->id = 0;
327 $object->statut = 0;
328
329 // Clear fields
330 // ...
331
332 // Create clone
333 $object->context['createfromclone'] = 'createfromclone';
334 $result = $object->create($user);
335
336 // Other options
337 if ($result < 0) {
338 $this->error = $object->error;
339 $this->errors = array_merge($this->errors, $object->errors);
340 $error++;
341 }
342
343 if (!$error) {
344 }
345
346 unset($object->context['createfromclone']);
347
348 // End
349 if (!$error) {
350 $this->db->commit();
351 return $object->id;
352 } else {
353 $this->db->rollback();
354 return -1;
355 }
356 }
357
358
365 public function initAsSpecimen()
366 {
367 $this->id = 0;
368
369 $this->tms = dol_now();
370 $this->fk_product_stock = 0;
371 $this->sellby = '';
372 $this->eatby = '';
373 $this->batch = '';
374 $this->import_key = '';
375
376 return 1;
377 }
378
384 private function cleanParam()
385 {
386 if (isset($this->fk_product_stock)) {
387 $this->fk_product_stock = (int) trim((string) $this->fk_product_stock);
388 }
389 if (isset($this->batch)) {
390 $this->batch = trim($this->batch);
391 }
392 if (isset($this->qty)) {
393 $this->qty = (float) trim((string) $this->qty);
394 }
395 if (isset($this->import_key)) {
396 $this->import_key = trim($this->import_key);
397 }
398 }
399
410 public function find($fk_product_stock = 0, $eatby = '', $sellby = '', $batch_number = '', $fk_warehouse = 0)
411 {
412 $where = array();
413
414 $sql = "SELECT";
415 $sql .= " t.rowid,";
416 $sql .= " t.tms,";
417 $sql .= " t.fk_product_stock,";
418 $sql .= " t.sellby,"; // deprecated
419 $sql .= " t.eatby,"; // deprecated
420 $sql .= " t.batch,";
421 $sql .= " t.qty,";
422 $sql .= " t.import_key";
423 $sql .= " FROM ".$this->db->prefix().self::$_table_element." as t";
424 if ($fk_product_stock > 0 || empty($fk_warehouse)) {
425 $sql .= " WHERE t.fk_product_stock = ".((int) $fk_product_stock);
426 } else {
427 $sql .= ", ".$this->db->prefix()."product_stock as ps";
428 $sql .= " WHERE t.fk_product_stock = ps.rowid AND ps.fk_entrepot = ".((int) $fk_warehouse);
429 }
430 if (!empty($eatby)) {
431 array_push($where, " eatby = '".$this->db->idate($eatby)."'"); // deprecated
432 }
433 if (!empty($sellby)) {
434 array_push($where, " sellby = '".$this->db->idate($sellby)."'"); // deprecated
435 }
436
437 if (!empty($batch_number)) {
438 $sql .= " AND batch = '".$this->db->escape($batch_number)."'";
439 }
440
441 if (!empty($where)) {
442 $sql .= " AND (".$this->db->sanitize(implode(" OR ", $where), 1, 1, 1).")";
443 }
444
445 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
446 $resql = $this->db->query($sql);
447 if ($resql) {
448 if ($this->db->num_rows($resql)) {
449 $obj = $this->db->fetch_object($resql);
450
451 $this->id = $obj->rowid;
452
453 $this->tms = $this->db->jdate($obj->tms);
454 $this->fk_product_stock = $obj->fk_product_stock;
455 $this->sellby = $this->db->jdate($obj->sellby); // deprecated. do no tuse this data.
456 $this->eatby = $this->db->jdate($obj->eatby); // deprecated. do not use this data.
457 $this->batch = $obj->batch;
458 $this->qty = $obj->qty;
459 $this->import_key = $obj->import_key;
460 }
461 $this->db->free($resql);
462
463 return 1;
464 } else {
465 $this->error = "Error ".$this->db->lasterror();
466 return -1;
467 }
468 }
478 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
479 {
480 global $conf;
481
482 $ret = array();
483
484 $sql = "SELECT";
485 $sql .= " t.rowid,";
486 $sql .= " t.tms,";
487 $sql .= " t.fk_product_stock,";
488 $sql .= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
489 $sql .= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
490 $sql .= " t.batch,";
491 $sql .= " t.qty,";
492 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
493 $sql .= " MAX(sm.datem) as date_entree,";
494 }
495 $sql .= " t.import_key";
496 if ($fk_product > 0) {
497 $sql .= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
498 // TODO May add extrafields to ?
499 }
500 $sql .= " FROM ".$dbs->prefix()."product_batch as t";
501 if ($fk_product > 0) { // Add link to the table of details of a lot
502 $sql .= " LEFT JOIN ".$dbs->prefix()."product_lot as pl ON pl.fk_product = ".((int) $fk_product)." AND pl.batch = t.batch";
503 // TODO May add extrafields to ?
504 }
505 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
506 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock AS ps ON (ps.rowid = fk_product_stock)';
507 $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))';
508 }
509 $sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
510 if ($with_qty) {
511 $sql .= " AND t.qty <> 0";
512 }
513 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
514 $sql .= ' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
515 if ($fk_product > 0) {
516 $sql .= ', pl.rowid, pl.eatby, pl.sellby';
517 }
518 }
519 $sql .= " ORDER BY ";
520 // TODO : use product lifo and fifo when product will implement it
521 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
522 $sql .= 'date_entree ASC,t.batch ASC,';
523 }
524 if ($fk_product > 0) {
525 $sql .= "pl.eatby ASC, pl.sellby ASC, ";
526 }
527 $sql .= "t.eatby ASC, t.sellby ASC ";
528 $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
529 $sql .= ", t.batch ASC";
530
531 dol_syslog("productbatch::findAll", LOG_DEBUG);
532
533 $resql = $dbs->query($sql);
534 if ($resql) {
535 $num = $dbs->num_rows($resql);
536 $i = 0;
537 while ($i < $num) {
538 $obj = $dbs->fetch_object($resql);
539
540 $tmp = new Productbatch($dbs);
541 $tmp->id = $obj->rowid;
542 $tmp->tms = $dbs->jdate($obj->tms);
543 $tmp->fk_product_stock = $obj->fk_product_stock;
544 $tmp->batch = $obj->batch;
545 $tmp->qty = $obj->qty;
546 $tmp->import_key = $obj->import_key;
547
548 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
549 $tmp->context['stock_entry_date'] = $dbs->jdate($obj->date_entree);
550 }
551
552 if ($fk_product > 0) {
553 // Some properties of the lot
554 $tmp->lotid = $obj->lotid; // ID in table of the details of properties of each lots
555 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
556 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
557 }
558
559 $ret[$tmp->batch] = $tmp; // $ret is for a $fk_product_stock and unique key is on $fk_product_stock+batch
560 $i++;
561 }
562 $dbs->free($resql);
563
564 return $ret;
565 } else {
566 //$error = "Error ".$dbs->lasterror();
567 return -1;
568 }
569 }
570
583 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min = null, $sortfield = null, $sortorder = null)
584 {
585 $productBatchList = array();
586
587 dol_syslog(__METHOD__.' fk_product='.$fk_product.', fk_warehouse='.$fk_warehouse.', qty_min='.$qty_min.', sortfield='.$sortfield.', sortorder='.$sortorder, LOG_DEBUG);
588
589 $sql = "SELECT";
590 $sql .= " pl.rowid";
591 $sql .= ", pl.fk_product";
592 $sql .= ", pl.batch";
593 $sql .= ", pl.sellby";
594 $sql .= ", pl.eatby";
595 $sql .= ", pb.qty";
596 $sql .= " FROM ".$this->db->prefix()."product_lot as pl";
597 $sql .= " LEFT JOIN ".$this->db->prefix()."product as p ON p.rowid = pl.fk_product";
598 $sql .= " LEFT JOIN ".$this->db->prefix()."product_batch AS pb ON pl.batch = pb.batch";
599 $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
600 $sql .= " WHERE p.entity IN (".getEntity('product').")";
601 $sql .= " AND pl.fk_product = ".((int) $fk_product);
602 if ($fk_warehouse > 0) {
603 $sql .= " AND ps.fk_entrepot = ".((int) $fk_warehouse);
604 }
605 if ($qty_min !== null) {
606 $sql .= " AND pb.qty > ".((float) price2num($qty_min, 'MS'));
607 }
608 $sql .= $this->db->order($sortfield, $sortorder);
609
610 $resql = $this->db->query($sql);
611 if ($resql) {
612 while ($obj = $this->db->fetch_object($resql)) {
613 $productBatch = new self($this->db);
614 $productBatch->id = $obj->rowid;
615 $productBatch->fk_product = $obj->fk_product;
616 $productBatch->batch = $obj->batch;
617 $productBatch->eatby = $this->db->jdate($obj->eatby);
618 $productBatch->sellby = $this->db->jdate($obj->sellby);
619 $productBatch->qty = $obj->qty;
620 $productBatchList[] = $productBatch;
621 }
622 $this->db->free($resql);
623
624 return $productBatchList;
625 } else {
626 dol_syslog(__METHOD__.' Error: '.$this->db->lasterror(), LOG_ERR);
627 return -1;
628 }
629 }
630}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
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 detailed record that match either eat-by, sell-by or batch within the warehouse.
fetch($id)
Load object in memory from the database.
cleanParam()
Clean fields (trimming)
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.
dol_now($mode='auto')
Return date for now.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79