dolibarr 21.0.0-beta
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
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
82 public $lotid;
83
88 public $sellby = ''; // dlc
93 public $eatby = ''; // dmd/dluo
94
95
101 public function __construct($db)
102 {
103 $this->db = $db;
104 }
105
106
114 public function create($user, $notrigger = 0)
115 {
116 $error = 0;
117
118 // Clean parameters
119 $this->cleanParam();
120
121 // Check parameters
122 // Put here code to add control on parameters values
123
124 // Insert request
125 $sql = "INSERT INTO ".$this->db->prefix()."product_batch (";
126 $sql .= "fk_product_stock,";
127 $sql .= "sellby,"; // no more used
128 $sql .= "eatby,"; // no more used
129 $sql .= "batch,";
130 $sql .= "qty,";
131 $sql .= "import_key";
132 $sql .= ") VALUES (";
133 $sql .= " ".(!isset($this->fk_product_stock) ? 'NULL' : $this->fk_product_stock).",";
134 $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; // no more used
135 $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; // no more used
136 $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
137 $sql .= " ".(!isset($this->qty) ? 'NULL' : $this->qty).",";
138 $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
139 $sql .= ")";
140
141 $this->db->begin();
142
143 dol_syslog(get_class($this)."::create", LOG_DEBUG);
144 $resql = $this->db->query($sql);
145 if (!$resql) {
146 $error++;
147 $this->errors[] = "Error ".$this->db->lasterror();
148 }
149 if (!$error) {
150 $this->id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
151 }
152
153 // Commit or rollback
154 if ($error) {
155 $this->db->rollback();
156 return -1 * $error;
157 } else {
158 $this->db->commit();
159 return $this->id;
160 }
161 }
162
163
170 public function fetch($id)
171 {
172 $sql = "SELECT";
173 $sql .= " t.rowid,";
174 $sql .= " t.tms,";
175 $sql .= " t.fk_product_stock,";
176 $sql .= " t.sellby as oldsellby,";
177 $sql .= " t.eatby as oldeatby,";
178 $sql .= " t.batch,";
179 $sql .= " t.qty,";
180 $sql .= " t.import_key,";
181 $sql .= " w.fk_entrepot,";
182 $sql .= " w.fk_product,";
183 $sql .= " pl.eatby,";
184 $sql .= " pl.sellby";
185 $sql .= " FROM ".$this->db->prefix()."product_batch as t";
186 $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
187 $sql .= " LEFT JOIN ".$this->db->prefix()."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
188 $sql .= " WHERE t.rowid = ".((int) $id);
189
190 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
191 $resql = $this->db->query($sql);
192 if ($resql) {
193 if ($this->db->num_rows($resql)) {
194 $obj = $this->db->fetch_object($resql);
195
196 $this->id = $obj->rowid;
197 $this->tms = $this->db->jdate($obj->tms);
198 $this->fk_product_stock = $obj->fk_product_stock;
199 $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
200 $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
201 $this->batch = $obj->batch;
202 $this->qty = $obj->qty;
203 $this->import_key = $obj->import_key;
204 $this->warehouseid = $obj->fk_entrepot;
205 $this->fk_product = $obj->fk_product;
206 }
207 $this->db->free($resql);
208
209 return 1;
210 } else {
211 $this->error = "Error ".$this->db->lasterror();
212 return -1;
213 }
214 }
215
223 public function update($user = null, $notrigger = 0)
224 {
225 $error = 0;
226
227 // Clean parameters
228 $this->cleanParam();
229
230 // TODO Check qty is ok for stock move. Negative may not be allowed.
231 /*
232 if ($this->qty < 0) {
233 }
234 */
235
236 // Update request
237 $sql = "UPDATE ".$this->db->prefix().self::$_table_element." SET";
238 $sql .= " fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock : "null").",";
239 $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').",";
240 $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
241 $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
242 $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
243 $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
244 $sql .= " WHERE rowid=".((int) $this->id);
245
246 $this->db->begin();
247
248 dol_syslog(get_class($this)."::update", LOG_DEBUG);
249 $resql = $this->db->query($sql);
250 if (!$resql) {
251 $error++;
252 $this->errors[] = "Error ".$this->db->lasterror();
253 }
254
255 // Commit or rollback
256 if ($error) {
257 foreach ($this->errors as $errmsg) {
258 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
259 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
260 }
261 $this->db->rollback();
262 return -1 * $error;
263 } else {
264 $this->db->commit();
265 return 1;
266 }
267 }
268
276 public function delete($user, $notrigger = 0)
277 {
278 $error = 0;
279
280 $this->db->begin();
281
282 if (!$error) {
283 $sql = "DELETE FROM ".$this->db->prefix().self::$_table_element;
284 $sql .= " WHERE rowid=".((int) $this->id);
285
286 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
287 $resql = $this->db->query($sql);
288 if (!$resql) {
289 $error++;
290 $this->errors[] = "Error ".$this->db->lasterror();
291 }
292 }
293
294 // Commit or rollback
295 if ($error) {
296 foreach ($this->errors as $errmsg) {
297 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
298 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
299 }
300 $this->db->rollback();
301 return -1 * $error;
302 } else {
303 $this->db->commit();
304 return 1;
305 }
306 }
307
308
309
317 public function createFromClone(User $user, $fromid)
318 {
319 $error = 0;
320
321 $object = new Productbatch($this->db);
322
323 $this->db->begin();
324
325 // Load source object
326 $object->fetch($fromid);
327 $object->id = 0;
328 $object->statut = 0;
329
330 // Clear fields
331 // ...
332
333 // Create clone
334 $object->context['createfromclone'] = 'createfromclone';
335 $result = $object->create($user);
336
337 // Other options
338 if ($result < 0) {
339 $this->error = $object->error;
340 $this->errors = array_merge($this->errors, $object->errors);
341 $error++;
342 }
343
344 if (!$error) {
345 }
346
347 unset($object->context['createfromclone']);
348
349 // End
350 if (!$error) {
351 $this->db->commit();
352 return $object->id;
353 } else {
354 $this->db->rollback();
355 return -1;
356 }
357 }
358
359
366 public function initAsSpecimen()
367 {
368 $this->id = 0;
369
370 $this->tms = dol_now();
371 $this->fk_product_stock = 0;
372 $this->sellby = '';
373 $this->eatby = '';
374 $this->batch = '';
375 $this->import_key = '';
376
377 return 1;
378 }
379
385 private function cleanParam()
386 {
387 if (isset($this->fk_product_stock)) {
388 $this->fk_product_stock = (int) trim((string) $this->fk_product_stock);
389 }
390 if (isset($this->batch)) {
391 $this->batch = trim($this->batch);
392 }
393 if (isset($this->qty)) {
394 $this->qty = (float) trim((string) $this->qty);
395 }
396 if (isset($this->import_key)) {
397 $this->import_key = trim($this->import_key);
398 }
399 }
400
411 public function find($fk_product_stock = 0, $eatby = null, $sellby = null, $batch_number = '', $fk_warehouse = 0)
412 {
413 $where = array();
414
415 $sql = "SELECT";
416 $sql .= " t.rowid,";
417 $sql .= " t.tms,";
418 $sql .= " t.fk_product_stock,";
419 $sql .= " t.sellby,"; // deprecated
420 $sql .= " t.eatby,"; // deprecated
421 $sql .= " t.batch,";
422 $sql .= " t.qty,";
423 $sql .= " t.import_key";
424 $sql .= " FROM ".$this->db->prefix().self::$_table_element." as t";
425 if ($fk_product_stock > 0 || empty($fk_warehouse)) {
426 $sql .= " WHERE t.fk_product_stock = ".((int) $fk_product_stock);
427 } else {
428 $sql .= ", ".$this->db->prefix()."product_stock as ps";
429 $sql .= " WHERE t.fk_product_stock = ps.rowid AND ps.fk_entrepot = ".((int) $fk_warehouse);
430 }
431 if (!empty($eatby)) {
432 array_push($where, " eatby = '".$this->db->idate($eatby)."'"); // deprecated
433 }
434 if (!empty($sellby)) {
435 array_push($where, " sellby = '".$this->db->idate($sellby)."'"); // deprecated
436 }
437
438 if (!empty($batch_number)) {
439 $sql .= " AND batch = '".$this->db->escape($batch_number)."'";
440 }
441
442 if (!empty($where)) {
443 $sql .= " AND (".$this->db->sanitize(implode(" OR ", $where), 1, 1, 1).")";
444 }
445
446 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
447 $resql = $this->db->query($sql);
448 if ($resql) {
449 if ($this->db->num_rows($resql)) {
450 $obj = $this->db->fetch_object($resql);
451
452 $this->id = $obj->rowid;
453
454 $this->tms = $this->db->jdate($obj->tms);
455 $this->fk_product_stock = $obj->fk_product_stock;
456 $this->sellby = $this->db->jdate($obj->sellby); // deprecated. do no tuse this data.
457 $this->eatby = $this->db->jdate($obj->eatby); // deprecated. do not use this data.
458 $this->batch = $obj->batch;
459 $this->qty = $obj->qty;
460 $this->import_key = $obj->import_key;
461 }
462 $this->db->free($resql);
463
464 return 1;
465 } else {
466 $this->error = "Error ".$this->db->lasterror();
467 return -1;
468 }
469 }
479 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
480 {
481 global $conf;
482
483 $ret = array();
484
485 $sql = "SELECT";
486 $sql .= " t.rowid,";
487 $sql .= " t.tms,";
488 $sql .= " t.fk_product_stock,";
489 $sql .= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
490 $sql .= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
491 $sql .= " t.batch,";
492 $sql .= " t.qty,";
493 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
494 $sql .= " MAX(sm.datem) as date_entree,";
495 }
496 $sql .= " t.import_key";
497 if ($fk_product > 0) {
498 $sql .= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
499 // TODO May add extrafields to ?
500 }
501 $sql .= " FROM ".$dbs->prefix()."product_batch as t";
502 if ($fk_product > 0) { // Add link to the table of details of a lot
503 $sql .= " LEFT JOIN ".$dbs->prefix()."product_lot as pl ON pl.fk_product = ".((int) $fk_product)." AND pl.batch = t.batch";
504 // TODO May add extrafields to ?
505 }
506 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
507 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock AS ps ON (ps.rowid = fk_product_stock)';
508 $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))';
509 }
510 $sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
511 if ($with_qty) {
512 $sql .= " AND t.qty <> 0";
513 }
514 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
515 $sql .= ' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
516 if ($fk_product > 0) {
517 $sql .= ', pl.rowid, pl.eatby, pl.sellby';
518 }
519 }
520 $sql .= " ORDER BY ";
521 // TODO : use product lifo and fifo when product will implement it
522 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
523 $sql .= 'date_entree ASC,t.batch ASC,';
524 }
525 if ($fk_product > 0) {
526 $sql .= "pl.eatby ASC, pl.sellby ASC, ";
527 }
528 $sql .= "t.eatby ASC, t.sellby ASC ";
529 $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
530 $sql .= ", t.batch ASC";
531
532 dol_syslog("productbatch::findAll", LOG_DEBUG);
533
534 $resql = $dbs->query($sql);
535 if ($resql) {
536 $num = $dbs->num_rows($resql);
537 $i = 0;
538 while ($i < $num) {
539 $obj = $dbs->fetch_object($resql);
540
541 $tmp = new Productbatch($dbs);
542 $tmp->id = $obj->rowid;
543 $tmp->tms = $dbs->jdate($obj->tms);
544 $tmp->fk_product_stock = $obj->fk_product_stock;
545 $tmp->batch = $obj->batch;
546 $tmp->qty = $obj->qty;
547 $tmp->import_key = $obj->import_key;
548
549 if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
550 $tmp->context['stock_entry_date'] = $dbs->jdate($obj->date_entree);
551 }
552
553 if ($fk_product > 0) {
554 // Some properties of the lot
555 $tmp->lotid = $obj->lotid; // ID in table of the details of properties of each lots
556 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
557 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
558 }
559
560 $ret[$tmp->batch] = $tmp; // $ret is for a $fk_product_stock and unique key is on $fk_product_stock+batch
561 $i++;
562 }
563 $dbs->free($resql);
564
565 return $ret;
566 } else {
567 //$error = "Error ".$dbs->lasterror();
568 return -1;
569 }
570 }
571
584 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min = null, $sortfield = null, $sortorder = null)
585 {
586 $productBatchList = array();
587
588 dol_syslog(__METHOD__.' fk_product='.$fk_product.', fk_warehouse='.$fk_warehouse.', qty_min='.$qty_min.', sortfield='.$sortfield.', sortorder='.$sortorder, LOG_DEBUG);
589
590 $sql = "SELECT";
591 $sql .= " pl.rowid";
592 $sql .= ", pl.fk_product";
593 $sql .= ", pl.batch";
594 $sql .= ", pl.sellby";
595 $sql .= ", pl.eatby";
596 $sql .= ", pb.qty";
597 $sql .= " FROM ".$this->db->prefix()."product_lot as pl";
598 $sql .= " LEFT JOIN ".$this->db->prefix()."product as p ON p.rowid = pl.fk_product";
599 $sql .= " LEFT JOIN ".$this->db->prefix()."product_batch AS pb ON pl.batch = pb.batch";
600 $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
601 $sql .= " WHERE p.entity IN (".getEntity('product').")";
602 $sql .= " AND pl.fk_product = ".((int) $fk_product);
603 if ($fk_warehouse > 0) {
604 $sql .= " AND ps.fk_entrepot = ".((int) $fk_warehouse);
605 }
606 if ($qty_min !== null) {
607 $sql .= " AND pb.qty > ".((float) price2num($qty_min, 'MS'));
608 }
609 $sql .= $this->db->order($sortfield, $sortorder);
610
611 $resql = $this->db->query($sql);
612 if ($resql) {
613 while ($obj = $this->db->fetch_object($resql)) {
614 $productBatch = new self($this->db);
615 $productBatch->id = $obj->rowid;
616 $productBatch->fk_product = $obj->fk_product;
617 $productBatch->batch = $obj->batch;
618 $productBatch->eatby = $this->db->jdate($obj->eatby);
619 $productBatch->sellby = $this->db->jdate($obj->sellby);
620 $productBatch->qty = $obj->qty;
621 $productBatchList[] = $productBatch;
622 }
623 $this->db->free($resql);
624
625 return $productBatchList;
626 } else {
627 dol_syslog(__METHOD__.' Error: '.$this->db->lasterror(), LOG_ERR);
628 return -1;
629 }
630 }
631}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79