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
51 public $batch = '';
52
56 public $qty;
57 public $warehouseid;
58
62 public $fk_product;
63
64 // Properties of the lot
65 public $lotid; // ID in table of the details of properties of each lots
66
71 public $sellby = ''; // dlc
76 public $eatby = ''; // dmd/dluo
77
78
84 public function __construct($db)
85 {
86 $this->db = $db;
87 }
88
89
97 public function create($user, $notrigger = 0)
98 {
99 $error = 0;
100
101 // Clean parameters
102 $this->cleanParam();
103
104 // Check parameters
105 // Put here code to add control on parameters values
106
107 // Insert request
108 $sql = "INSERT INTO ".$this->db->prefix()."product_batch (";
109 $sql .= "fk_product_stock,";
110 $sql .= "sellby,"; // no more used
111 $sql .= "eatby,"; // no more used
112 $sql .= "batch,";
113 $sql .= "qty,";
114 $sql .= "import_key";
115 $sql .= ") VALUES (";
116 $sql .= " ".(!isset($this->fk_product_stock) ? 'NULL' : $this->fk_product_stock).",";
117 $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; // no more used
118 $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; // no more used
119 $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
120 $sql .= " ".(!isset($this->qty) ? 'NULL' : $this->qty).",";
121 $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
122 $sql .= ")";
123
124 $this->db->begin();
125
126 dol_syslog(get_class($this)."::create", LOG_DEBUG);
127 $resql = $this->db->query($sql);
128 if (!$resql) {
129 $error++;
130 $this->errors[] = "Error ".$this->db->lasterror();
131 }
132 if (!$error) {
133 $this->id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
134 }
135
136 // Commit or rollback
137 if ($error) {
138 $this->db->rollback();
139 return -1 * $error;
140 } else {
141 $this->db->commit();
142 return $this->id;
143 }
144 }
145
146
153 public function fetch($id)
154 {
155 $sql = "SELECT";
156 $sql .= " t.rowid,";
157 $sql .= " t.tms,";
158 $sql .= " t.fk_product_stock,";
159 $sql .= " t.sellby as oldsellby,";
160 $sql .= " t.eatby as oldeatby,";
161 $sql .= " t.batch,";
162 $sql .= " t.qty,";
163 $sql .= " t.import_key,";
164 $sql .= " w.fk_entrepot,";
165 $sql .= " w.fk_product,";
166 $sql .= " pl.eatby,";
167 $sql .= " pl.sellby";
168 $sql .= " FROM ".$this->db->prefix()."product_batch as t";
169 $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
170 $sql .= " LEFT JOIN ".$this->db->prefix()."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
171 $sql .= " WHERE t.rowid = ".((int) $id);
172
173 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
174 $resql = $this->db->query($sql);
175 if ($resql) {
176 if ($this->db->num_rows($resql)) {
177 $obj = $this->db->fetch_object($resql);
178
179 $this->id = $obj->rowid;
180 $this->tms = $this->db->jdate($obj->tms);
181 $this->fk_product_stock = $obj->fk_product_stock;
182 $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
183 $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
184 $this->batch = $obj->batch;
185 $this->qty = $obj->qty;
186 $this->import_key = $obj->import_key;
187 $this->warehouseid = $obj->fk_entrepot;
188 $this->fk_product = $obj->fk_product;
189 }
190 $this->db->free($resql);
191
192 return 1;
193 } else {
194 $this->error = "Error ".$this->db->lasterror();
195 return -1;
196 }
197 }
198
206 public function update($user = null, $notrigger = 0)
207 {
208 $error = 0;
209
210 // Clean parameters
211 $this->cleanParam();
212
213 // TODO Check qty is ok for stock move. Negative may not be allowed.
214 if ($this->qty < 0) {
215 }
216
217 // Update request
218 $sql = "UPDATE ".$this->db->prefix().self::$_table_element." SET";
219 $sql .= " fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock : "null").",";
220 $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').",";
221 $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
222 $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
223 $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
224 $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
225 $sql .= " WHERE rowid=".((int) $this->id);
226
227 $this->db->begin();
228
229 dol_syslog(get_class($this)."::update", LOG_DEBUG);
230 $resql = $this->db->query($sql);
231 if (!$resql) {
232 $error++;
233 $this->errors[] = "Error ".$this->db->lasterror();
234 }
235
236 // Commit or rollback
237 if ($error) {
238 foreach ($this->errors as $errmsg) {
239 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
240 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
241 }
242 $this->db->rollback();
243 return -1 * $error;
244 } else {
245 $this->db->commit();
246 return 1;
247 }
248 }
249
257 public function delete($user, $notrigger = 0)
258 {
259 $error = 0;
260
261 $this->db->begin();
262
263 if (!$error) {
264 $sql = "DELETE FROM ".$this->db->prefix().self::$_table_element;
265 $sql .= " WHERE rowid=".((int) $this->id);
266
267 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
268 $resql = $this->db->query($sql);
269 if (!$resql) {
270 $error++;
271 $this->errors[] = "Error ".$this->db->lasterror();
272 }
273 }
274
275 // Commit or rollback
276 if ($error) {
277 foreach ($this->errors as $errmsg) {
278 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
279 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
280 }
281 $this->db->rollback();
282 return -1 * $error;
283 } else {
284 $this->db->commit();
285 return 1;
286 }
287 }
288
289
290
298 public function createFromClone(User $user, $fromid)
299 {
300 $error = 0;
301
302 $object = new Productbatch($this->db);
303
304 $this->db->begin();
305
306 // Load source object
307 $object->fetch($fromid);
308 $object->id = 0;
309 $object->statut = 0;
310
311 // Clear fields
312 // ...
313
314 // Create clone
315 $object->context['createfromclone'] = 'createfromclone';
316 $result = $object->create($user);
317
318 // Other options
319 if ($result < 0) {
320 $this->error = $object->error;
321 $this->errors = array_merge($this->errors, $object->errors);
322 $error++;
323 }
324
325 if (!$error) {
326 }
327
328 unset($object->context['createfromclone']);
329
330 // End
331 if (!$error) {
332 $this->db->commit();
333 return $object->id;
334 } else {
335 $this->db->rollback();
336 return -1;
337 }
338 }
339
340
347 public function initAsSpecimen()
348 {
349 $this->id = 0;
350
351 $this->tms = dol_now();
352 $this->fk_product_stock = '';
353 $this->sellby = '';
354 $this->eatby = '';
355 $this->batch = '';
356 $this->import_key = '';
357
358 return 1;
359 }
360
366 private function cleanParam()
367 {
368 if (isset($this->fk_product_stock)) {
369 $this->fk_product_stock = (int) trim($this->fk_product_stock);
370 }
371 if (isset($this->batch)) {
372 $this->batch = trim($this->batch);
373 }
374 if (isset($this->qty)) {
375 $this->qty = (float) trim((string) $this->qty);
376 }
377 if (isset($this->import_key)) {
378 $this->import_key = trim($this->import_key);
379 }
380 }
381
392 public function find($fk_product_stock = 0, $eatby = null, $sellby = null, $batch_number = '', $fk_warehouse = 0)
393 {
394 $where = array();
395
396 $sql = "SELECT";
397 $sql .= " t.rowid,";
398 $sql .= " t.tms,";
399 $sql .= " t.fk_product_stock,";
400 $sql .= " t.sellby,"; // deprecated
401 $sql .= " t.eatby,"; // deprecated
402 $sql .= " t.batch,";
403 $sql .= " t.qty,";
404 $sql .= " t.import_key";
405 $sql .= " FROM ".$this->db->prefix().self::$_table_element." as t";
406 if ($fk_product_stock > 0 || empty($fk_warehouse)) {
407 $sql .= " WHERE t.fk_product_stock = ".((int) $fk_product_stock);
408 } else {
409 $sql .= ", ".$this->db->prefix()."product_stock as ps";
410 $sql .= " WHERE t.fk_product_stock = ps.rowid AND ps.fk_entrepot = ".((int) $fk_warehouse);
411 }
412 if (!empty($eatby)) {
413 array_push($where, " eatby = '".$this->db->idate($eatby)."'"); // deprecated
414 }
415 if (!empty($sellby)) {
416 array_push($where, " sellby = '".$this->db->idate($sellby)."'"); // deprecated
417 }
418
419 if (!empty($batch_number)) {
420 $sql .= " AND batch = '".$this->db->escape($batch_number)."'";
421 }
422
423 if (!empty($where)) {
424 $sql .= " AND (".$this->db->sanitize(implode(" OR ", $where), 1, 1, 1).")";
425 }
426
427 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
428 $resql = $this->db->query($sql);
429 if ($resql) {
430 if ($this->db->num_rows($resql)) {
431 $obj = $this->db->fetch_object($resql);
432
433 $this->id = $obj->rowid;
434
435 $this->tms = $this->db->jdate($obj->tms);
436 $this->fk_product_stock = $obj->fk_product_stock;
437 $this->sellby = $this->db->jdate($obj->sellby); // deprecated. do no tuse this data.
438 $this->eatby = $this->db->jdate($obj->eatby); // deprecated. do not use this data.
439 $this->batch = $obj->batch;
440 $this->qty = $obj->qty;
441 $this->import_key = $obj->import_key;
442 }
443 $this->db->free($resql);
444
445 return 1;
446 } else {
447 $this->error = "Error ".$this->db->lasterror();
448 return -1;
449 }
450 }
460 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
461 {
462 global $conf;
463
464 $ret = array();
465
466 $sql = "SELECT";
467 $sql .= " t.rowid,";
468 $sql .= " t.tms,";
469 $sql .= " t.fk_product_stock,";
470 $sql .= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
471 $sql .= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
472 $sql .= " t.batch,";
473 $sql .= " t.qty,";
474 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
475 $sql .= " MAX(sm.datem) as date_entree,";
476 }
477 $sql .= " t.import_key";
478 if ($fk_product > 0) {
479 $sql .= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
480 // TODO May add extrafields to ?
481 }
482 $sql .= " FROM ".$dbs->prefix()."product_batch as t";
483 if ($fk_product > 0) { // Add link to the table of details of a lot
484 $sql .= " LEFT JOIN ".$dbs->prefix()."product_lot as pl ON pl.fk_product = ".((int) $fk_product)." AND pl.batch = t.batch";
485 // TODO May add extrafields to ?
486 }
487 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
488 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock AS ps ON (ps.rowid = fk_product_stock)';
489 $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))';
490 }
491 $sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
492 if ($with_qty) {
493 $sql .= " AND t.qty <> 0";
494 }
495 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
496 $sql .= ' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
497 if ($fk_product > 0) {
498 $sql .= ', pl.rowid, pl.eatby, pl.sellby';
499 }
500 }
501 $sql .= " ORDER BY ";
502 // TODO : use product lifo and fifo when product will implement it
503 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
504 $sql .= 'date_entree ASC,t.batch ASC,';
505 }
506 if ($fk_product > 0) {
507 $sql .= "pl.eatby ASC, pl.sellby ASC, ";
508 }
509 $sql .= "t.eatby ASC, t.sellby ASC ";
510 $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
511 $sql .= ", t.batch ASC";
512
513 dol_syslog("productbatch::findAll", LOG_DEBUG);
514
515 $resql = $dbs->query($sql);
516 if ($resql) {
517 $num = $dbs->num_rows($resql);
518 $i = 0;
519 while ($i < $num) {
520 $obj = $dbs->fetch_object($resql);
521
522 $tmp = new Productbatch($dbs);
523 $tmp->id = $obj->rowid;
524 $tmp->tms = $dbs->jdate($obj->tms);
525 $tmp->fk_product_stock = $obj->fk_product_stock;
526 $tmp->batch = $obj->batch;
527 $tmp->qty = $obj->qty;
528 $tmp->import_key = $obj->import_key;
529
530 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
531 $tmp->context['stock_entry_date'] = $dbs->jdate($obj->date_entree);
532 }
533
534 if ($fk_product > 0) {
535 // Some properties of the lot
536 $tmp->lotid = $obj->lotid; // ID in table of the details of properties of each lots
537 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
538 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
539 }
540
541 $ret[$tmp->batch] = $tmp; // $ret is for a $fk_product_stock and unique key is on $fk_product_stock+batch
542 $i++;
543 }
544 $dbs->free($resql);
545
546 return $ret;
547 } else {
548 //$error = "Error ".$dbs->lasterror();
549 return -1;
550 }
551 }
552
565 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min = null, $sortfield = null, $sortorder = null)
566 {
567 $productBatchList = array();
568
569 dol_syslog(__METHOD__.' fk_product='.$fk_product.', fk_warehouse='.$fk_warehouse.', qty_min='.$qty_min.', sortfield='.$sortfield.', sortorder='.$sortorder, LOG_DEBUG);
570
571 $sql = "SELECT";
572 $sql .= " pl.rowid";
573 $sql .= ", pl.fk_product";
574 $sql .= ", pl.batch";
575 $sql .= ", pl.sellby";
576 $sql .= ", pl.eatby";
577 $sql .= ", pb.qty";
578 $sql .= " FROM ".$this->db->prefix()."product_lot as pl";
579 $sql .= " LEFT JOIN ".$this->db->prefix()."product as p ON p.rowid = pl.fk_product";
580 $sql .= " LEFT JOIN ".$this->db->prefix()."product_batch AS pb ON pl.batch = pb.batch";
581 $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
582 $sql .= " WHERE p.entity IN (".getEntity('product').")";
583 $sql .= " AND pl.fk_product = ".((int) $fk_product);
584 if ($fk_warehouse > 0) {
585 $sql .= " AND ps.fk_entrepot = ".((int) $fk_warehouse);
586 }
587 if ($qty_min !== null) {
588 $sql .= " AND pb.qty > ".((float) price2num($qty_min, 'MS'));
589 }
590 $sql .= $this->db->order($sortfield, $sortorder);
591
592 $resql = $this->db->query($sql);
593 if ($resql) {
594 while ($obj = $this->db->fetch_object($resql)) {
595 $productBatch = new self($this->db);
596 $productBatch->id = $obj->rowid;
597 $productBatch->fk_product = $obj->fk_product;
598 $productBatch->batch = $obj->batch;
599 $productBatch->eatby = $this->db->jdate($obj->eatby);
600 $productBatch->sellby = $this->db->jdate($obj->sellby);
601 $productBatch->qty = $obj->qty;
602 $productBatchList[] = $productBatch;
603 }
604 $this->db->free($resql);
605
606 return $productBatchList;
607 } else {
608 dol_syslog(__METHOD__.' Error: '.$this->db->lasterror(), LOG_ERR);
609 return -1;
610 }
611 }
612}
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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.