29require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
45 public $element =
'productbatch';
49 public $fk_product_stock;
108 public function create($user, $notrigger = 0)
119 $sql =
"INSERT INTO ".$this->db->prefix().
"product_batch (";
120 $sql .=
"fk_product_stock,";
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).
"'").
",";
129 $sql .=
" ".(!isset($this->eatby) ||
dol_strlen($this->eatby) == 0 ?
'NULL' :
"'".$this->db->idate($this->eatby).
"'").
",";
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).
"'");
137 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
138 $resql = $this->db->query($sql);
141 $this->errors[] =
"Error ".$this->db->lasterror();
144 $this->
id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
149 $this->db->rollback();
169 $sql .=
" t.fk_product_stock,";
170 $sql .=
" t.sellby as oldsellby,";
171 $sql .=
" t.eatby as oldeatby,";
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";
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);
184 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
185 $resql = $this->db->query($sql);
187 if ($this->db->num_rows($resql)) {
188 $obj = $this->db->fetch_object($resql);
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;
201 $this->db->free($resql);
205 $this->error =
"Error ".$this->db->lasterror();
217 public function update($user =
null, $notrigger = 0)
225 if ($this->qty < 0) {
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);
240 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
241 $resql = $this->db->query($sql);
244 $this->errors[] =
"Error ".$this->db->lasterror();
249 foreach ($this->errors as $errmsg) {
250 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
251 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
253 $this->db->rollback();
268 public function delete($user, $notrigger = 0)
275 $sql =
"DELETE FROM ".$this->db->prefix().self::$_table_element;
276 $sql .=
" WHERE rowid=".((int) $this->
id);
278 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
279 $resql = $this->db->query($sql);
282 $this->errors[] =
"Error ".$this->db->lasterror();
288 foreach ($this->errors as $errmsg) {
289 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
290 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
292 $this->db->rollback();
326 $object->context[
'createfromclone'] =
'createfromclone';
327 $result =
$object->create($user);
332 $this->errors = array_merge($this->errors,
$object->errors);
339 unset(
$object->context[
'createfromclone']);
346 $this->db->rollback();
363 $this->fk_product_stock =
'';
367 $this->import_key =
'';
379 if (isset($this->fk_product_stock)) {
380 $this->fk_product_stock = (int) trim($this->fk_product_stock);
382 if (isset($this->batch)) {
383 $this->batch = trim($this->batch);
385 if (isset($this->qty)) {
386 $this->qty = (float) trim((
string) $this->qty);
388 if (isset($this->import_key)) {
389 $this->import_key = trim($this->import_key);
403 public function find($fk_product_stock = 0, $eatby =
null, $sellby =
null, $batch_number =
'', $fk_warehouse = 0)
410 $sql .=
" t.fk_product_stock,";
411 $sql .=
" t.sellby,";
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);
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);
423 if (!empty($eatby)) {
424 array_push($where,
" eatby = '".$this->db->idate($eatby).
"'");
426 if (!empty($sellby)) {
427 array_push($where,
" sellby = '".$this->db->idate($sellby).
"'");
430 if (!empty($batch_number)) {
431 $sql .=
" AND batch = '".$this->db->escape($batch_number).
"'";
434 if (!empty($where)) {
435 $sql .=
" AND (".$this->db->sanitize(implode(
" OR ", $where), 1, 1, 1).
")";
438 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
439 $resql = $this->db->query($sql);
441 if ($this->db->num_rows($resql)) {
442 $obj = $this->db->fetch_object($resql);
444 $this->
id = $obj->rowid;
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);
449 $this->eatby = $this->db->jdate($obj->eatby);
450 $this->batch = $obj->batch;
451 $this->qty = $obj->qty;
452 $this->import_key = $obj->import_key;
454 $this->db->free($resql);
458 $this->error =
"Error ".$this->db->lasterror();
471 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
480 $sql .=
" t.fk_product_stock,";
481 $sql .=
" t.sellby as oldsellby,";
482 $sql .=
" t.eatby as oldeatby,";
486 $sql .=
" MAX(sm.datem) as date_entree,";
488 $sql .=
" t.import_key";
489 if ($fk_product > 0) {
490 $sql .=
", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
493 $sql .=
" FROM ".$dbs->prefix().
"product_batch as t";
494 if ($fk_product > 0) {
495 $sql .=
" LEFT JOIN ".$dbs->prefix().
"product_lot as pl ON pl.fk_product = ".((int) $fk_product).
" AND pl.batch = t.batch";
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))';
502 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
504 $sql .=
" AND t.qty <> 0";
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';
512 $sql .=
" ORDER BY ";
515 $sql .=
'date_entree ASC,t.batch ASC,';
517 if ($fk_product > 0) {
518 $sql .=
"pl.eatby ASC, pl.sellby ASC, ";
520 $sql .=
"t.eatby ASC, t.sellby ASC ";
521 $sql .=
", t.qty ".(!
getDolGlobalString(
'DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE') ?
'ASC' :
'DESC');
522 $sql .=
", t.batch ASC";
524 dol_syslog(
"productbatch::findAll", LOG_DEBUG);
526 $resql = $dbs->query($sql);
528 $num = $dbs->num_rows($resql);
531 $obj = $dbs->fetch_object($resql);
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;
542 $tmp->context[
'stock_entry_date'] = $dbs->jdate($obj->date_entree);
545 if ($fk_product > 0) {
547 $tmp->lotid = $obj->lotid;
548 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
549 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
552 $ret[$tmp->batch] = $tmp;
576 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min =
null, $sortfield =
null, $sortorder =
null)
578 $productBatchList = array();
580 dol_syslog(__METHOD__.
' fk_product='.$fk_product.
', fk_warehouse='.$fk_warehouse.
', qty_min='.$qty_min.
', sortfield='.$sortfield.
', sortorder='.$sortorder, LOG_DEBUG);
584 $sql .=
", pl.fk_product";
585 $sql .=
", pl.batch";
586 $sql .=
", pl.sellby";
587 $sql .=
", pl.eatby";
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);
598 if ($qty_min !==
null) {
599 $sql .=
" AND pb.qty > ".((float)
price2num($qty_min,
'MS'));
601 $sql .= $this->db->order($sortfield, $sortorder);
603 $resql = $this->db->query($sql);
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;
615 $this->db->free($resql);
617 return $productBatchList;
619 dol_syslog(__METHOD__.
' Error: '.$this->db->lasterror(), LOG_ERR);
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
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.