dolibarr 21.0.0-alpha
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 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
47 private static $_table_element = 'product_batch';
48
49 public $fk_product_stock;
50
54 public $batch = '';
55
59 public $qty;
60
64 public $warehouseid;
65
69 public $fk_product;
70
76 public $lotid;
77
82 public $sellby = ''; // dlc
87 public $eatby = ''; // dmd/dluo
88
89
95 public function __construct($db)
96 {
97 $this->db = $db;
98 }
99
100
108 public function create($user, $notrigger = 0)
109 {
110 $error = 0;
111
112 // Clean parameters
113 $this->cleanParam();
114
115 // Check parameters
116 // Put here code to add control on parameters values
117
118 // Insert request
119 $sql = "INSERT INTO ".$this->db->prefix()."product_batch (";
120 $sql .= "fk_product_stock,";
121 $sql .= "sellby,"; // no more used
122 $sql .= "eatby,"; // no more used
123 $sql .= "batch,";
124 $sql .= "qty,";
125 $sql .= "import_key";
126 $sql .= ") VALUES (";
127 $sql .= " ".(!isset($this->fk_product_stock) ? 'NULL' : $this->fk_product_stock).",";
128 $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; // no more used
129 $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; // no more used
130 $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
131 $sql .= " ".(!isset($this->qty) ? 'NULL' : $this->qty).",";
132 $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
133 $sql .= ")";
134
135 $this->db->begin();
136
137 dol_syslog(get_class($this)."::create", LOG_DEBUG);
138 $resql = $this->db->query($sql);
139 if (!$resql) {
140 $error++;
141 $this->errors[] = "Error ".$this->db->lasterror();
142 }
143 if (!$error) {
144 $this->id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
145 }
146
147 // Commit or rollback
148 if ($error) {
149 $this->db->rollback();
150 return -1 * $error;
151 } else {
152 $this->db->commit();
153 return $this->id;
154 }
155 }
156
157
164 public function fetch($id)
165 {
166 $sql = "SELECT";
167 $sql .= " t.rowid,";
168 $sql .= " t.tms,";
169 $sql .= " t.fk_product_stock,";
170 $sql .= " t.sellby as oldsellby,";
171 $sql .= " t.eatby as oldeatby,";
172 $sql .= " t.batch,";
173 $sql .= " t.qty,";
174 $sql .= " t.import_key,";
175 $sql .= " w.fk_entrepot,";
176 $sql .= " w.fk_product,";
177 $sql .= " pl.eatby,";
178 $sql .= " pl.sellby";
179 $sql .= " FROM ".$this->db->prefix()."product_batch as t";
180 $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
181 $sql .= " LEFT JOIN ".$this->db->prefix()."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
182 $sql .= " WHERE t.rowid = ".((int) $id);
183
184 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
185 $resql = $this->db->query($sql);
186 if ($resql) {
187 if ($this->db->num_rows($resql)) {
188 $obj = $this->db->fetch_object($resql);
189
190 $this->id = $obj->rowid;
191 $this->tms = $this->db->jdate($obj->tms);
192 $this->fk_product_stock = $obj->fk_product_stock;
193 $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
194 $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
195 $this->batch = $obj->batch;
196 $this->qty = $obj->qty;
197 $this->import_key = $obj->import_key;
198 $this->warehouseid = $obj->fk_entrepot;
199 $this->fk_product = $obj->fk_product;
200 }
201 $this->db->free($resql);
202
203 return 1;
204 } else {
205 $this->error = "Error ".$this->db->lasterror();
206 return -1;
207 }
208 }
209
217 public function update($user = null, $notrigger = 0)
218 {
219 $error = 0;
220
221 // Clean parameters
222 $this->cleanParam();
223
224 // TODO Check qty is ok for stock move. Negative may not be allowed.
225 if ($this->qty < 0) {
226 }
227
228 // Update request
229 $sql = "UPDATE ".$this->db->prefix().self::$_table_element." SET";
230 $sql .= " fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock : "null").",";
231 $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').",";
232 $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
233 $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
234 $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
235 $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
236 $sql .= " WHERE rowid=".((int) $this->id);
237
238 $this->db->begin();
239
240 dol_syslog(get_class($this)."::update", LOG_DEBUG);
241 $resql = $this->db->query($sql);
242 if (!$resql) {
243 $error++;
244 $this->errors[] = "Error ".$this->db->lasterror();
245 }
246
247 // Commit or rollback
248 if ($error) {
249 foreach ($this->errors as $errmsg) {
250 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
251 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
252 }
253 $this->db->rollback();
254 return -1 * $error;
255 } else {
256 $this->db->commit();
257 return 1;
258 }
259 }
260
268 public function delete($user, $notrigger = 0)
269 {
270 $error = 0;
271
272 $this->db->begin();
273
274 if (!$error) {
275 $sql = "DELETE FROM ".$this->db->prefix().self::$_table_element;
276 $sql .= " WHERE rowid=".((int) $this->id);
277
278 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
279 $resql = $this->db->query($sql);
280 if (!$resql) {
281 $error++;
282 $this->errors[] = "Error ".$this->db->lasterror();
283 }
284 }
285
286 // Commit or rollback
287 if ($error) {
288 foreach ($this->errors as $errmsg) {
289 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
290 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
291 }
292 $this->db->rollback();
293 return -1 * $error;
294 } else {
295 $this->db->commit();
296 return 1;
297 }
298 }
299
300
301
309 public function createFromClone(User $user, $fromid)
310 {
311 $error = 0;
312
313 $object = new Productbatch($this->db);
314
315 $this->db->begin();
316
317 // Load source object
318 $object->fetch($fromid);
319 $object->id = 0;
320 $object->statut = 0;
321
322 // Clear fields
323 // ...
324
325 // Create clone
326 $object->context['createfromclone'] = 'createfromclone';
327 $result = $object->create($user);
328
329 // Other options
330 if ($result < 0) {
331 $this->error = $object->error;
332 $this->errors = array_merge($this->errors, $object->errors);
333 $error++;
334 }
335
336 if (!$error) {
337 }
338
339 unset($object->context['createfromclone']);
340
341 // End
342 if (!$error) {
343 $this->db->commit();
344 return $object->id;
345 } else {
346 $this->db->rollback();
347 return -1;
348 }
349 }
350
351
358 public function initAsSpecimen()
359 {
360 $this->id = 0;
361
362 $this->tms = dol_now();
363 $this->fk_product_stock = '';
364 $this->sellby = '';
365 $this->eatby = '';
366 $this->batch = '';
367 $this->import_key = '';
368
369 return 1;
370 }
371
377 private function cleanParam()
378 {
379 if (isset($this->fk_product_stock)) {
380 $this->fk_product_stock = (int) trim($this->fk_product_stock);
381 }
382 if (isset($this->batch)) {
383 $this->batch = trim($this->batch);
384 }
385 if (isset($this->qty)) {
386 $this->qty = (float) trim((string) $this->qty);
387 }
388 if (isset($this->import_key)) {
389 $this->import_key = trim($this->import_key);
390 }
391 }
392
403 public function find($fk_product_stock = 0, $eatby = null, $sellby = null, $batch_number = '', $fk_warehouse = 0)
404 {
405 $where = array();
406
407 $sql = "SELECT";
408 $sql .= " t.rowid,";
409 $sql .= " t.tms,";
410 $sql .= " t.fk_product_stock,";
411 $sql .= " t.sellby,"; // deprecated
412 $sql .= " t.eatby,"; // deprecated
413 $sql .= " t.batch,";
414 $sql .= " t.qty,";
415 $sql .= " t.import_key";
416 $sql .= " FROM ".$this->db->prefix().self::$_table_element." as t";
417 if ($fk_product_stock > 0 || empty($fk_warehouse)) {
418 $sql .= " WHERE t.fk_product_stock = ".((int) $fk_product_stock);
419 } else {
420 $sql .= ", ".$this->db->prefix()."product_stock as ps";
421 $sql .= " WHERE t.fk_product_stock = ps.rowid AND ps.fk_entrepot = ".((int) $fk_warehouse);
422 }
423 if (!empty($eatby)) {
424 array_push($where, " eatby = '".$this->db->idate($eatby)."'"); // deprecated
425 }
426 if (!empty($sellby)) {
427 array_push($where, " sellby = '".$this->db->idate($sellby)."'"); // deprecated
428 }
429
430 if (!empty($batch_number)) {
431 $sql .= " AND batch = '".$this->db->escape($batch_number)."'";
432 }
433
434 if (!empty($where)) {
435 $sql .= " AND (".$this->db->sanitize(implode(" OR ", $where), 1, 1, 1).")";
436 }
437
438 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
439 $resql = $this->db->query($sql);
440 if ($resql) {
441 if ($this->db->num_rows($resql)) {
442 $obj = $this->db->fetch_object($resql);
443
444 $this->id = $obj->rowid;
445
446 $this->tms = $this->db->jdate($obj->tms);
447 $this->fk_product_stock = $obj->fk_product_stock;
448 $this->sellby = $this->db->jdate($obj->sellby); // deprecated. do no tuse this data.
449 $this->eatby = $this->db->jdate($obj->eatby); // deprecated. do not use this data.
450 $this->batch = $obj->batch;
451 $this->qty = $obj->qty;
452 $this->import_key = $obj->import_key;
453 }
454 $this->db->free($resql);
455
456 return 1;
457 } else {
458 $this->error = "Error ".$this->db->lasterror();
459 return -1;
460 }
461 }
471 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
472 {
473 global $conf;
474
475 $ret = array();
476
477 $sql = "SELECT";
478 $sql .= " t.rowid,";
479 $sql .= " t.tms,";
480 $sql .= " t.fk_product_stock,";
481 $sql .= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
482 $sql .= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
483 $sql .= " t.batch,";
484 $sql .= " t.qty,";
485 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
486 $sql .= " MAX(sm.datem) as date_entree,";
487 }
488 $sql .= " t.import_key";
489 if ($fk_product > 0) {
490 $sql .= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
491 // TODO May add extrafields to ?
492 }
493 $sql .= " FROM ".$dbs->prefix()."product_batch as t";
494 if ($fk_product > 0) { // Add link to the table of details of a lot
495 $sql .= " LEFT JOIN ".$dbs->prefix()."product_lot as pl ON pl.fk_product = ".((int) $fk_product)." AND pl.batch = t.batch";
496 // TODO May add extrafields to ?
497 }
498 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
499 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock AS ps ON (ps.rowid = fk_product_stock)';
500 $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))';
501 }
502 $sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
503 if ($with_qty) {
504 $sql .= " AND t.qty <> 0";
505 }
506 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
507 $sql .= ' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
508 if ($fk_product > 0) {
509 $sql .= ', pl.rowid, pl.eatby, pl.sellby';
510 }
511 }
512 $sql .= " ORDER BY ";
513 // TODO : use product lifo and fifo when product will implement it
514 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
515 $sql .= 'date_entree ASC,t.batch ASC,';
516 }
517 if ($fk_product > 0) {
518 $sql .= "pl.eatby ASC, pl.sellby ASC, ";
519 }
520 $sql .= "t.eatby ASC, t.sellby ASC ";
521 $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
522 $sql .= ", t.batch ASC";
523
524 dol_syslog("productbatch::findAll", LOG_DEBUG);
525
526 $resql = $dbs->query($sql);
527 if ($resql) {
528 $num = $dbs->num_rows($resql);
529 $i = 0;
530 while ($i < $num) {
531 $obj = $dbs->fetch_object($resql);
532
533 $tmp = new Productbatch($dbs);
534 $tmp->id = $obj->rowid;
535 $tmp->tms = $dbs->jdate($obj->tms);
536 $tmp->fk_product_stock = $obj->fk_product_stock;
537 $tmp->batch = $obj->batch;
538 $tmp->qty = $obj->qty;
539 $tmp->import_key = $obj->import_key;
540
541 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
542 $tmp->context['stock_entry_date'] = $dbs->jdate($obj->date_entree);
543 }
544
545 if ($fk_product > 0) {
546 // Some properties of the lot
547 $tmp->lotid = $obj->lotid; // ID in table of the details of properties of each lots
548 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
549 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
550 }
551
552 $ret[$tmp->batch] = $tmp; // $ret is for a $fk_product_stock and unique key is on $fk_product_stock+batch
553 $i++;
554 }
555 $dbs->free($resql);
556
557 return $ret;
558 } else {
559 //$error = "Error ".$dbs->lasterror();
560 return -1;
561 }
562 }
563
576 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min = null, $sortfield = null, $sortorder = null)
577 {
578 $productBatchList = array();
579
580 dol_syslog(__METHOD__.' fk_product='.$fk_product.', fk_warehouse='.$fk_warehouse.', qty_min='.$qty_min.', sortfield='.$sortfield.', sortorder='.$sortorder, LOG_DEBUG);
581
582 $sql = "SELECT";
583 $sql .= " pl.rowid";
584 $sql .= ", pl.fk_product";
585 $sql .= ", pl.batch";
586 $sql .= ", pl.sellby";
587 $sql .= ", pl.eatby";
588 $sql .= ", pb.qty";
589 $sql .= " FROM ".$this->db->prefix()."product_lot as pl";
590 $sql .= " LEFT JOIN ".$this->db->prefix()."product as p ON p.rowid = pl.fk_product";
591 $sql .= " LEFT JOIN ".$this->db->prefix()."product_batch AS pb ON pl.batch = pb.batch";
592 $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
593 $sql .= " WHERE p.entity IN (".getEntity('product').")";
594 $sql .= " AND pl.fk_product = ".((int) $fk_product);
595 if ($fk_warehouse > 0) {
596 $sql .= " AND ps.fk_entrepot = ".((int) $fk_warehouse);
597 }
598 if ($qty_min !== null) {
599 $sql .= " AND pb.qty > ".((float) price2num($qty_min, 'MS'));
600 }
601 $sql .= $this->db->order($sortfield, $sortorder);
602
603 $resql = $this->db->query($sql);
604 if ($resql) {
605 while ($obj = $this->db->fetch_object($resql)) {
606 $productBatch = new self($this->db);
607 $productBatch->id = $obj->rowid;
608 $productBatch->fk_product = $obj->fk_product;
609 $productBatch->batch = $obj->batch;
610 $productBatch->eatby = $this->db->jdate($obj->eatby);
611 $productBatch->sellby = $this->db->jdate($obj->sellby);
612 $productBatch->qty = $obj->qty;
613 $productBatchList[] = $productBatch;
614 }
615 $this->db->free($resql);
616
617 return $productBatchList;
618 } else {
619 dol_syslog(__METHOD__.' Error: '.$this->db->lasterror(), LOG_ERR);
620 return -1;
621 }
622 }
623}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.
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.
find($fk_product_stock=0, $eatby=null, $sellby=null, $batch_number='', $fk_warehouse=0)
Find first detailed record that match either eat-by, sell-by or batch within the warehouse.
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.