29require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
45 public $element =
'productbatch';
55 public $fk_product_stock;
113 public function create($user, $notrigger = 0)
124 $sql =
"INSERT INTO ".$this->db->prefix().
"product_batch (";
125 $sql .=
"fk_product_stock,";
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).
"'").
",";
134 $sql .=
" ".(!isset($this->eatby) ||
dol_strlen($this->eatby) == 0 ?
'NULL' :
"'".$this->db->idate($this->eatby).
"'").
",";
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).
"'");
142 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
143 $resql = $this->db->query($sql);
146 $this->errors[] =
"Error ".$this->db->lasterror();
149 $this->
id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
154 $this->db->rollback();
174 $sql .=
" t.fk_product_stock,";
175 $sql .=
" t.sellby as oldsellby,";
176 $sql .=
" t.eatby as oldeatby,";
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";
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);
189 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
190 $resql = $this->db->query($sql);
192 if ($this->db->num_rows($resql)) {
193 $obj = $this->db->fetch_object($resql);
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;
206 $this->db->free($resql);
210 $this->error =
"Error ".$this->db->lasterror();
222 public function update($user =
null, $notrigger = 0)
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);
247 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
248 $resql = $this->db->query($sql);
251 $this->errors[] =
"Error ".$this->db->lasterror();
256 foreach ($this->errors as $errmsg) {
257 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
258 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
260 $this->db->rollback();
275 public function delete($user, $notrigger = 0)
282 $sql =
"DELETE FROM ".$this->db->prefix().self::$_table_element;
283 $sql .=
" WHERE rowid=".((int) $this->
id);
285 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
286 $resql = $this->db->query($sql);
289 $this->errors[] =
"Error ".$this->db->lasterror();
295 foreach ($this->errors as $errmsg) {
296 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
297 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
299 $this->db->rollback();
333 $object->context[
'createfromclone'] =
'createfromclone';
334 $result =
$object->create($user);
339 $this->errors = array_merge($this->errors,
$object->errors);
346 unset(
$object->context[
'createfromclone']);
353 $this->db->rollback();
370 $this->fk_product_stock = 0;
374 $this->import_key =
'';
386 if (isset($this->fk_product_stock)) {
387 $this->fk_product_stock = (int) trim((
string) $this->fk_product_stock);
389 if (isset($this->batch)) {
390 $this->batch = trim($this->batch);
392 if (isset($this->qty)) {
393 $this->qty = (float) trim((
string) $this->qty);
395 if (isset($this->import_key)) {
396 $this->import_key = trim($this->import_key);
411 public function find($fk_product_stock = 0, $eatby =
'', $sellby =
'', $batch_number =
'', $fk_warehouse = 0, $fk_product = 0)
418 $sql .=
" t.fk_product_stock,";
419 $sql .=
" t.sellby,";
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);
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 if ($fk_product > 0) {
431 $sql .=
" AND ps.fk_product = ".((int) $fk_product);
434 if (!empty($eatby)) {
435 array_push($where,
" eatby = '".$this->db->idate($eatby).
"'");
437 if (!empty($sellby)) {
438 array_push($where,
" sellby = '".$this->db->idate($sellby).
"'");
441 if (!empty($batch_number)) {
442 $sql .=
" AND batch = '".$this->db->escape($batch_number).
"'";
445 if (!empty($where)) {
446 $sql .=
" AND (".$this->db->sanitize(implode(
" OR ", $where), 1, 1, 1).
")";
449 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
450 $resql = $this->db->query($sql);
452 if ($this->db->num_rows($resql)) {
453 $obj = $this->db->fetch_object($resql);
455 $this->
id = $obj->rowid;
457 $this->tms = $this->db->jdate($obj->tms);
458 $this->fk_product_stock = $obj->fk_product_stock;
459 $this->sellby = $this->db->jdate($obj->sellby);
460 $this->eatby = $this->db->jdate($obj->eatby);
461 $this->batch = $obj->batch;
462 $this->qty = $obj->qty;
463 $this->import_key = $obj->import_key;
465 $this->db->free($resql);
469 $this->error =
"Error ".$this->db->lasterror();
482 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
491 $sql .=
" t.fk_product_stock,";
492 $sql .=
" t.sellby as oldsellby,";
493 $sql .=
" t.eatby as oldeatby,";
497 $sql .=
" MAX(sm.datem) as date_entree,";
499 $sql .=
" t.import_key";
500 if ($fk_product > 0) {
501 $sql .=
", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
504 $sql .=
" FROM ".$dbs->prefix().
"product_batch as t";
505 if ($fk_product > 0) {
506 $sql .=
" LEFT JOIN ".$dbs->prefix().
"product_lot as pl ON pl.fk_product = ".((int) $fk_product).
" AND pl.batch = t.batch";
510 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product_stock AS ps ON (ps.rowid = fk_product_stock)';
511 $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))';
513 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
515 $sql .=
" AND t.qty <> 0";
518 $sql .=
' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
519 if ($fk_product > 0) {
520 $sql .=
', pl.rowid, pl.eatby, pl.sellby';
523 $sql .=
" ORDER BY ";
526 $sql .=
'date_entree ASC,t.batch ASC,';
528 if ($fk_product > 0) {
529 $sql .=
"pl.eatby ASC, pl.sellby ASC, ";
531 $sql .=
"t.eatby ASC, t.sellby ASC ";
532 $sql .=
", t.qty ".(!
getDolGlobalString(
'DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE') ?
'ASC' :
'DESC');
533 $sql .=
", t.batch ASC";
535 dol_syslog(
"productbatch::findAll", LOG_DEBUG);
537 $resql = $dbs->query($sql);
539 $num = $dbs->num_rows($resql);
542 $obj = $dbs->fetch_object($resql);
545 $tmp->id = $obj->rowid;
546 $tmp->tms = $dbs->jdate($obj->tms);
547 $tmp->fk_product_stock = $obj->fk_product_stock;
548 $tmp->batch = $obj->batch;
549 $tmp->qty = $obj->qty;
550 $tmp->import_key = $obj->import_key;
553 $tmp->context[
'stock_entry_date'] = $dbs->jdate($obj->date_entree);
556 if ($fk_product > 0) {
558 $tmp->lotid = $obj->lotid;
559 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
560 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
563 $ret[$tmp->batch] = $tmp;
587 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min =
null, $sortfield =
null, $sortorder =
null)
589 $productBatchList = array();
591 dol_syslog(__METHOD__.
' fk_product='.$fk_product.
', fk_warehouse='.$fk_warehouse.
', qty_min='.$qty_min.
', sortfield='.$sortfield.
', sortorder='.$sortorder, LOG_DEBUG);
595 $sql .=
", pl.fk_product";
596 $sql .=
", pl.batch";
597 $sql .=
", pl.sellby";
598 $sql .=
", pl.eatby";
600 $sql .=
" FROM ".$this->db->prefix().
"product_lot as pl";
601 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = pl.fk_product";
602 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_batch AS pb ON pl.batch = pb.batch";
603 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
604 $sql .=
" WHERE p.entity IN (".getEntity(
'product').
")";
605 $sql .=
" AND pl.fk_product = ".((int) $fk_product);
606 if ($fk_warehouse > 0) {
607 $sql .=
" AND ps.fk_entrepot = ".((int) $fk_warehouse);
609 if ($qty_min !==
null) {
610 $sql .=
" AND pb.qty > ".((float)
price2num($qty_min,
'MS'));
612 $sql .= $this->db->order($sortfield, $sortorder);
614 $resql = $this->db->query($sql);
616 while ($obj = $this->db->fetch_object($resql)) {
617 $productBatch =
new self($this->db);
618 $productBatch->id = $obj->rowid;
619 $productBatch->fk_product = $obj->fk_product;
620 $productBatch->batch = $obj->batch;
621 $productBatch->eatby = $this->db->jdate($obj->eatby);
622 $productBatch->sellby = $this->db->jdate($obj->sellby);
623 $productBatch->qty = $obj->qty;
624 $productBatchList[] = $productBatch;
626 $this->db->free($resql);
628 return $productBatchList;
630 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.
const BATCH_RULE_SELLBY_EATBY_DATES_FIRST
Batches rules.
find($fk_product_stock=0, $eatby='', $sellby='', $batch_number='', $fk_warehouse=0, $fk_product=0)
Find first detailed record that match either eat-by, sell-by or batch within the warehouse.
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...