27require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
43 public $element =
'productbatch';
48 public $fk_product_stock;
92 public function create($user, $notrigger = 0)
103 $sql =
"INSERT INTO ".$this->db->prefix().
"product_batch (";
104 $sql .=
"fk_product_stock,";
109 $sql .=
"import_key";
110 $sql .=
") VALUES (";
111 $sql .=
" ".(!isset($this->fk_product_stock) ?
'NULL' : $this->fk_product_stock).
",";
112 $sql .=
" ".(!isset($this->sellby) ||
dol_strlen($this->sellby) == 0 ?
'NULL' :
"'".$this->db->idate($this->sellby).
"'").
",";
113 $sql .=
" ".(!isset($this->eatby) ||
dol_strlen($this->eatby) == 0 ?
'NULL' :
"'".$this->db->idate($this->eatby).
"'").
",";
114 $sql .=
" ".(!isset($this->batch) ?
'NULL' :
"'".$this->db->escape($this->batch).
"'").
",";
115 $sql .=
" ".(!isset($this->qty) ?
'NULL' : $this->qty).
",";
116 $sql .=
" ".(!isset($this->import_key) ?
'NULL' :
"'".$this->db->escape($this->import_key).
"'");
121 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
122 $resql = $this->db->query($sql);
125 $this->errors[] =
"Error ".$this->db->lasterror();
128 $this->
id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
133 $this->db->rollback();
153 $sql .=
" t.fk_product_stock,";
154 $sql .=
" t.sellby as oldsellby,";
155 $sql .=
" t.eatby as oldeatby,";
158 $sql .=
" t.import_key,";
159 $sql .=
" w.fk_entrepot,";
160 $sql .=
" w.fk_product,";
161 $sql .=
" pl.eatby,";
162 $sql .=
" pl.sellby";
163 $sql .=
" FROM ".$this->db->prefix().
"product_batch as t";
164 $sql .=
" INNER JOIN ".$this->db->prefix().
"product_stock w on t.fk_product_stock = w.rowid";
165 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
166 $sql .=
" WHERE t.rowid = ".((int) $id);
168 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
169 $resql = $this->db->query($sql);
171 if ($this->db->num_rows($resql)) {
172 $obj = $this->db->fetch_object($resql);
174 $this->
id = $obj->rowid;
175 $this->tms = $this->db->jdate($obj->tms);
176 $this->fk_product_stock = $obj->fk_product_stock;
177 $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
178 $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
179 $this->batch = $obj->batch;
180 $this->qty = $obj->qty;
181 $this->import_key = $obj->import_key;
182 $this->warehouseid = $obj->fk_entrepot;
183 $this->fk_product = $obj->fk_product;
185 $this->db->free($resql);
189 $this->error =
"Error ".$this->db->lasterror();
201 public function update($user =
null, $notrigger = 0)
209 if ($this->qty < 0) {
213 $sql =
"UPDATE ".$this->db->prefix().self::$_table_element.
" SET";
214 $sql .=
" fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock :
"null").
",";
215 $sql .=
" sellby=".(dol_strlen($this->sellby) != 0 ?
"'".$this->db->idate($this->sellby).
"'" :
'null').
",";
216 $sql .=
" eatby=".(dol_strlen($this->eatby) != 0 ?
"'".$this->db->idate($this->eatby).
"'" :
'null').
",";
217 $sql .=
" batch=".(isset($this->batch) ?
"'".$this->db->escape($this->batch).
"'" :
"null").
",";
218 $sql .=
" qty=".(isset($this->qty) ? $this->qty :
"null").
",";
219 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
220 $sql .=
" WHERE rowid=".((int) $this->
id);
224 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
225 $resql = $this->db->query($sql);
228 $this->errors[] =
"Error ".$this->db->lasterror();
233 foreach ($this->errors as $errmsg) {
234 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
235 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
237 $this->db->rollback();
252 public function delete($user, $notrigger = 0)
259 $sql =
"DELETE FROM ".$this->db->prefix().self::$_table_element;
260 $sql .=
" WHERE rowid=".((int) $this->
id);
262 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
263 $resql = $this->db->query($sql);
266 $this->errors[] =
"Error ".$this->db->lasterror();
272 foreach ($this->errors as $errmsg) {
273 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
274 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
276 $this->db->rollback();
302 $object->fetch($fromid);
310 $object->context[
'createfromclone'] =
'createfromclone';
311 $result = $object->create($user);
315 $this->error = $object->error;
316 $this->errors = array_merge($this->errors, $object->errors);
323 unset($object->context[
'createfromclone']);
330 $this->db->rollback();
347 $this->fk_product_stock =
'';
351 $this->import_key =
'';
361 if (isset($this->fk_product_stock)) {
362 $this->fk_product_stock = (int) trim($this->fk_product_stock);
364 if (isset($this->batch)) {
365 $this->batch = trim($this->batch);
367 if (isset($this->qty)) {
368 $this->qty = (float) trim($this->qty);
370 if (isset($this->import_key)) {
371 $this->import_key = trim($this->import_key);
385 public function find($fk_product_stock = 0, $eatby =
'', $sellby =
'', $batch_number =
'', $fk_warehouse = 0)
391 $sql .=
" t.fk_product_stock,";
392 $sql .=
" t.sellby,";
396 $sql .=
" t.import_key";
397 $sql .=
" FROM ".$this->db->prefix().self::$_table_element.
" as t";
398 if ($fk_product_stock > 0 || empty($fk_warehouse)) {
399 $sql .=
" WHERE t.fk_product_stock = ".((int) $fk_product_stock);
401 $sql .=
", ".$this->db->prefix().
"product_stock as ps";
402 $sql .=
" WHERE t.fk_product_stock = ps.rowid AND ps.fk_entrepot = ".((int) $fk_warehouse);
404 if (!empty($eatby)) {
405 array_push($where,
" eatby = '".$this->db->idate($eatby).
"'");
407 if (!empty($sellby)) {
408 array_push($where,
" sellby = '".$this->db->idate($sellby).
"'");
411 if (!empty($batch_number)) {
412 $sql .=
" AND batch = '".$this->db->escape($batch_number).
"'";
415 if (!empty($where)) {
416 $sql .=
" AND (".implode(
" OR ", $where).
")";
419 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
420 $resql = $this->db->query($sql);
422 if ($this->db->num_rows($resql)) {
423 $obj = $this->db->fetch_object($resql);
425 $this->
id = $obj->rowid;
427 $this->tms = $this->db->jdate($obj->tms);
428 $this->fk_product_stock = $obj->fk_product_stock;
429 $this->sellby = $this->db->jdate($obj->sellby);
430 $this->eatby = $this->db->jdate($obj->eatby);
431 $this->batch = $obj->batch;
432 $this->qty = $obj->qty;
433 $this->import_key = $obj->import_key;
435 $this->db->free($resql);
439 $this->error =
"Error ".$this->db->lasterror();
452 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
461 $sql .=
" t.fk_product_stock,";
462 $sql .=
" t.sellby as oldsellby,";
463 $sql .=
" t.eatby as oldeatby,";
467 $sql .=
" MAX(sm.datem) as date_entree,";
469 $sql .=
" t.import_key";
470 if ($fk_product > 0) {
471 $sql .=
", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
474 $sql .=
" FROM ".$dbs->prefix().
"product_batch as t";
475 if ($fk_product > 0) {
476 $sql .=
" LEFT JOIN ".$dbs->prefix().
"product_lot as pl ON pl.fk_product = ".((int) $fk_product).
" AND pl.batch = t.batch";
480 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product_stock AS ps ON (ps.rowid = fk_product_stock)';
481 $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))';
483 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
485 $sql .=
" AND t.qty <> 0";
488 $sql .=
' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
489 if ($fk_product > 0) {
490 $sql .=
', pl.rowid, pl.eatby, pl.sellby';
493 $sql .=
" ORDER BY ";
496 $sql .=
'date_entree ASC,t.batch ASC,';
498 if ($fk_product > 0) {
499 $sql .=
"pl.eatby ASC, pl.sellby ASC, ";
501 $sql .=
"t.eatby ASC, t.sellby ASC ";
502 $sql .=
", t.qty ".(!
getDolGlobalString(
'DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE') ?
'ASC' :
'DESC');
503 $sql .=
", t.batch ASC";
505 dol_syslog(
"productbatch::findAll", LOG_DEBUG);
507 $resql = $dbs->query($sql);
509 $num = $dbs->num_rows($resql);
512 $obj = $dbs->fetch_object($resql);
515 $tmp->id = $obj->rowid;
516 $tmp->tms = $dbs->jdate($obj->tms);
517 $tmp->fk_product_stock = $obj->fk_product_stock;
518 $tmp->batch = $obj->batch;
519 $tmp->qty = $obj->qty;
520 $tmp->import_key = $obj->import_key;
523 $tmp->context[
'stock_entry_date'] = $dbs->jdate($obj->date_entree);
526 if ($fk_product > 0) {
528 $tmp->lotid = $obj->lotid;
529 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
530 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
533 $ret[$tmp->batch] = $tmp;
557 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min =
null, $sortfield =
null, $sortorder =
null)
559 $productBatchList = array();
561 dol_syslog(__METHOD__.
' fk_product='.$fk_product.
', fk_warehouse='.$fk_warehouse.
', qty_min='.$qty_min.
', sortfield='.$sortfield.
', sortorder='.$sortorder, LOG_DEBUG);
565 $sql .=
", pl.fk_product";
566 $sql .=
", pl.batch";
567 $sql .=
", pl.sellby";
568 $sql .=
", pl.eatby";
570 $sql .=
" FROM ".$this->db->prefix().
"product_lot as pl";
571 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = pl.fk_product";
572 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_batch AS pb ON pl.batch = pb.batch";
573 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
574 $sql .=
" WHERE p.entity IN (".getEntity(
'product').
")";
575 $sql .=
" AND pl.fk_product = ".((int) $fk_product);
576 if ($fk_warehouse > 0) {
577 $sql .=
" AND ps.fk_entrepot = ".((int) $fk_warehouse);
579 if ($qty_min !==
null) {
580 $sql .=
" AND pb.qty > ".((float)
price2num($qty_min,
'MS'));
582 $sql .= $this->db->order($sortfield, $sortorder);
584 $resql = $this->db->query($sql);
586 while ($obj = $this->db->fetch_object($resql)) {
587 $productBatch =
new self($this->db);
588 $productBatch->id = $obj->rowid;
589 $productBatch->fk_product = $obj->fk_product;
590 $productBatch->batch = $obj->batch;
591 $productBatch->eatby = $this->db->jdate($obj->eatby);
592 $productBatch->sellby = $this->db->jdate($obj->sellby);
593 $productBatch->qty = $obj->qty;
594 $productBatchList[] = $productBatch;
596 $this->db->free($resql);
598 return $productBatchList;
600 dol_syslog(__METHOD__.
' Error: '.$this->db->lasterror(), LOG_ERR);
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.
find($fk_product_stock=0, $eatby='', $sellby='', $batch_number='', $fk_warehouse=0)
Find first detail record that match eather eat-by or sell-by or batch within given warehouse.
fetch($id)
Load object in memory from the database.
cleanParam()
Clean fields (triming)
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.
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.
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.