29require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
45 public $element =
'productbatch';
49 public $fk_product_stock;
97 public function create($user, $notrigger = 0)
108 $sql =
"INSERT INTO ".$this->db->prefix().
"product_batch (";
109 $sql .=
"fk_product_stock,";
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).
"'").
",";
118 $sql .=
" ".(!isset($this->eatby) ||
dol_strlen($this->eatby) == 0 ?
'NULL' :
"'".$this->db->idate($this->eatby).
"'").
",";
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).
"'");
126 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
127 $resql = $this->db->query($sql);
130 $this->errors[] =
"Error ".$this->db->lasterror();
133 $this->
id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
138 $this->db->rollback();
158 $sql .=
" t.fk_product_stock,";
159 $sql .=
" t.sellby as oldsellby,";
160 $sql .=
" t.eatby as oldeatby,";
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";
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);
173 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
174 $resql = $this->db->query($sql);
176 if ($this->db->num_rows($resql)) {
177 $obj = $this->db->fetch_object($resql);
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;
190 $this->db->free($resql);
194 $this->error =
"Error ".$this->db->lasterror();
206 public function update($user =
null, $notrigger = 0)
214 if ($this->qty < 0) {
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);
229 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
230 $resql = $this->db->query($sql);
233 $this->errors[] =
"Error ".$this->db->lasterror();
238 foreach ($this->errors as $errmsg) {
239 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
240 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
242 $this->db->rollback();
257 public function delete($user, $notrigger = 0)
264 $sql =
"DELETE FROM ".$this->db->prefix().self::$_table_element;
265 $sql .=
" WHERE rowid=".((int) $this->
id);
267 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
268 $resql = $this->db->query($sql);
271 $this->errors[] =
"Error ".$this->db->lasterror();
277 foreach ($this->errors as $errmsg) {
278 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
279 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
281 $this->db->rollback();
315 $object->context[
'createfromclone'] =
'createfromclone';
316 $result =
$object->create($user);
321 $this->errors = array_merge($this->errors,
$object->errors);
328 unset(
$object->context[
'createfromclone']);
335 $this->db->rollback();
352 $this->fk_product_stock =
'';
356 $this->import_key =
'';
368 if (isset($this->fk_product_stock)) {
369 $this->fk_product_stock = (int) trim($this->fk_product_stock);
371 if (isset($this->batch)) {
372 $this->batch = trim($this->batch);
374 if (isset($this->qty)) {
375 $this->qty = (float) trim((
string) $this->qty);
377 if (isset($this->import_key)) {
378 $this->import_key = trim($this->import_key);
392 public function find($fk_product_stock = 0, $eatby =
null, $sellby =
null, $batch_number =
'', $fk_warehouse = 0)
399 $sql .=
" t.fk_product_stock,";
400 $sql .=
" t.sellby,";
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);
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);
412 if (!empty($eatby)) {
413 array_push($where,
" eatby = '".$this->db->idate($eatby).
"'");
415 if (!empty($sellby)) {
416 array_push($where,
" sellby = '".$this->db->idate($sellby).
"'");
419 if (!empty($batch_number)) {
420 $sql .=
" AND batch = '".$this->db->escape($batch_number).
"'";
423 if (!empty($where)) {
424 $sql .=
" AND (".$this->db->sanitize(implode(
" OR ", $where), 1, 1, 1).
")";
427 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
428 $resql = $this->db->query($sql);
430 if ($this->db->num_rows($resql)) {
431 $obj = $this->db->fetch_object($resql);
433 $this->
id = $obj->rowid;
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);
438 $this->eatby = $this->db->jdate($obj->eatby);
439 $this->batch = $obj->batch;
440 $this->qty = $obj->qty;
441 $this->import_key = $obj->import_key;
443 $this->db->free($resql);
447 $this->error =
"Error ".$this->db->lasterror();
460 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
469 $sql .=
" t.fk_product_stock,";
470 $sql .=
" t.sellby as oldsellby,";
471 $sql .=
" t.eatby as oldeatby,";
475 $sql .=
" MAX(sm.datem) as date_entree,";
477 $sql .=
" t.import_key";
478 if ($fk_product > 0) {
479 $sql .=
", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
482 $sql .=
" FROM ".$dbs->prefix().
"product_batch as t";
483 if ($fk_product > 0) {
484 $sql .=
" LEFT JOIN ".$dbs->prefix().
"product_lot as pl ON pl.fk_product = ".((int) $fk_product).
" AND pl.batch = t.batch";
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))';
491 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
493 $sql .=
" AND t.qty <> 0";
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';
501 $sql .=
" ORDER BY ";
504 $sql .=
'date_entree ASC,t.batch ASC,';
506 if ($fk_product > 0) {
507 $sql .=
"pl.eatby ASC, pl.sellby ASC, ";
509 $sql .=
"t.eatby ASC, t.sellby ASC ";
510 $sql .=
", t.qty ".(!
getDolGlobalString(
'DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE') ?
'ASC' :
'DESC');
511 $sql .=
", t.batch ASC";
513 dol_syslog(
"productbatch::findAll", LOG_DEBUG);
515 $resql = $dbs->query($sql);
517 $num = $dbs->num_rows($resql);
520 $obj = $dbs->fetch_object($resql);
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;
531 $tmp->context[
'stock_entry_date'] = $dbs->jdate($obj->date_entree);
534 if ($fk_product > 0) {
536 $tmp->lotid = $obj->lotid;
537 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
538 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
541 $ret[$tmp->batch] = $tmp;
565 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min =
null, $sortfield =
null, $sortorder =
null)
567 $productBatchList = array();
569 dol_syslog(__METHOD__.
' fk_product='.$fk_product.
', fk_warehouse='.$fk_warehouse.
', qty_min='.$qty_min.
', sortfield='.$sortfield.
', sortorder='.$sortorder, LOG_DEBUG);
573 $sql .=
", pl.fk_product";
574 $sql .=
", pl.batch";
575 $sql .=
", pl.sellby";
576 $sql .=
", pl.eatby";
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);
587 if ($qty_min !==
null) {
588 $sql .=
" AND pb.qty > ".((float)
price2num($qty_min,
'MS'));
590 $sql .= $this->db->order($sortfield, $sortorder);
592 $resql = $this->db->query($sql);
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;
604 $this->db->free($resql);
606 return $productBatchList;
608 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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.