26require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
42 public $element =
'productbatch';
47 public $fk_product_stock;
82 public function create($user, $notrigger = 0)
94 $sql =
"INSERT INTO ".$this->db->prefix().
"product_batch (";
95 $sql .=
"fk_product_stock,";
100 $sql .=
"import_key";
101 $sql .=
") VALUES (";
102 $sql .=
" ".(!isset($this->fk_product_stock) ?
'NULL' : $this->fk_product_stock).
",";
103 $sql .=
" ".(!isset($this->sellby) ||
dol_strlen($this->sellby) == 0 ?
'NULL' :
"'".$this->db->idate($this->sellby).
"'").
",";
104 $sql .=
" ".(!isset($this->eatby) ||
dol_strlen($this->eatby) == 0 ?
'NULL' :
"'".$this->db->idate($this->eatby).
"'").
",";
105 $sql .=
" ".(!isset($this->batch) ?
'NULL' :
"'".$this->db->escape($this->batch).
"'").
",";
106 $sql .=
" ".(!isset($this->qty) ?
'NULL' : $this->qty).
",";
107 $sql .=
" ".(!isset($this->import_key) ?
'NULL' :
"'".$this->db->escape($this->import_key).
"'");
112 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
113 $resql = $this->db->query($sql);
115 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
118 $this->
id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
123 $this->db->rollback();
144 $sql .=
" t.fk_product_stock,";
145 $sql .=
" t.sellby as oldsellby,";
146 $sql .=
" t.eatby as oldeatby,";
149 $sql .=
" t.import_key,";
150 $sql .=
" w.fk_entrepot,";
151 $sql .=
" w.fk_product,";
152 $sql .=
" pl.eatby,";
153 $sql .=
" pl.sellby";
154 $sql .=
" FROM ".$this->db->prefix().
"product_batch as t";
155 $sql .=
" INNER JOIN ".$this->db->prefix().
"product_stock w on t.fk_product_stock = w.rowid";
156 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
157 $sql .=
" WHERE t.rowid = ".((int) $id);
159 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
160 $resql = $this->db->query($sql);
162 if ($this->db->num_rows($resql)) {
163 $obj = $this->db->fetch_object($resql);
165 $this->
id = $obj->rowid;
166 $this->tms = $this->db->jdate($obj->tms);
167 $this->fk_product_stock = $obj->fk_product_stock;
168 $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
169 $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
170 $this->batch = $obj->batch;
171 $this->qty = $obj->qty;
172 $this->import_key = $obj->import_key;
173 $this->warehouseid = $obj->fk_entrepot;
174 $this->fk_product = $obj->fk_product;
176 $this->db->free($resql);
180 $this->error =
"Error ".$this->db->lasterror();
192 public function update($user =
null, $notrigger = 0)
194 global $conf, $langs;
201 if ($this->qty < 0) {
205 $sql =
"UPDATE ".$this->db->prefix().self::$_table_element.
" SET";
206 $sql .=
" fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock :
"null").
",";
207 $sql .=
" sellby=".(dol_strlen($this->sellby) != 0 ?
"'".$this->db->idate($this->sellby).
"'" :
'null').
",";
208 $sql .=
" eatby=".(dol_strlen($this->eatby) != 0 ?
"'".$this->db->idate($this->eatby).
"'" :
'null').
",";
209 $sql .=
" batch=".(isset($this->batch) ?
"'".$this->db->escape($this->batch).
"'" :
"null").
",";
210 $sql .=
" qty=".(isset($this->qty) ? $this->qty :
"null").
",";
211 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
212 $sql .=
" WHERE rowid=".((int) $this->
id);
216 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
217 $resql = $this->db->query($sql);
219 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
224 foreach ($this->errors as $errmsg) {
225 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
226 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
228 $this->db->rollback();
243 public function delete($user, $notrigger = 0)
245 global $conf, $langs;
251 $sql =
"DELETE FROM ".$this->db->prefix().self::$_table_element;
252 $sql .=
" WHERE rowid=".((int) $this->
id);
254 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
255 $resql = $this->db->query($sql);
257 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
263 foreach ($this->errors as $errmsg) {
264 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
265 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
267 $this->db->rollback();
293 $object->fetch($fromid);
301 $object->context[
'createfromclone'] =
'createfromclone';
302 $result = $object->create($user);
306 $this->error = $object->error;
307 $this->errors = array_merge($this->errors, $object->errors);
314 unset($object->context[
'createfromclone']);
321 $this->db->rollback();
338 $this->fk_product_stock =
'';
342 $this->import_key =
'';
352 if (isset($this->fk_product_stock)) {
353 $this->fk_product_stock = (int) trim($this->fk_product_stock);
355 if (isset($this->batch)) {
356 $this->batch = trim($this->batch);
358 if (isset($this->qty)) {
359 $this->qty = (float) trim($this->qty);
361 if (isset($this->import_key)) {
362 $this->import_key = trim($this->import_key);
375 public function find($fk_product_stock = 0, $eatby =
'', $sellby =
'', $batch_number =
'')
383 $sql .=
" t.fk_product_stock,";
384 $sql .=
" t.sellby,";
388 $sql .=
" t.import_key";
389 $sql .=
" FROM ".$this->db->prefix().self::$_table_element.
" as t";
390 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
392 if (!empty($eatby)) {
393 array_push($where,
" eatby = '".$this->db->idate($eatby).
"'");
395 if (!empty($sellby)) {
396 array_push($where,
" sellby = '".$this->db->idate($sellby).
"'");
399 if (!empty($batch_number)) {
400 $sql .=
" AND batch = '".$this->db->escape($batch_number).
"'";
403 if (!empty($where)) {
404 $sql .=
" AND (".implode(
" OR ", $where).
")";
407 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
408 $resql = $this->db->query($sql);
410 if ($this->db->num_rows($resql)) {
411 $obj = $this->db->fetch_object($resql);
413 $this->
id = $obj->rowid;
415 $this->tms = $this->db->jdate($obj->tms);
416 $this->fk_product_stock = $obj->fk_product_stock;
417 $this->sellby = $this->db->jdate($obj->sellby);
418 $this->eatby = $this->db->jdate($obj->eatby);
419 $this->batch = $obj->batch;
420 $this->qty = $obj->qty;
421 $this->import_key = $obj->import_key;
423 $this->db->free($resql);
427 $this->error =
"Error ".$this->db->lasterror();
440 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
449 $sql .=
" t.fk_product_stock,";
450 $sql .=
" t.sellby as oldsellby,";
451 $sql .=
" t.eatby as oldeatby,";
454 $sql .=
" t.import_key";
455 if ($fk_product > 0) {
456 $sql .=
", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
459 $sql .=
" FROM ".$dbs->prefix().
"product_batch as t";
460 if ($fk_product > 0) {
461 $sql .=
" LEFT JOIN ".$dbs->prefix().
"product_lot as pl ON pl.fk_product = ".((int) $fk_product).
" AND pl.batch = t.batch";
464 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
466 $sql .=
" AND t.qty <> 0";
469 $sql .=
" ORDER BY ";
471 if ($fk_product > 0) { $sql .=
"pl.eatby ASC, pl.sellby ASC, "; }
472 $sql .=
"t.eatby ASC, t.sellby ASC ";
473 $sql .=
", t.qty ".(empty($conf->global->DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE)?
'ASC':
'DESC');
475 dol_syslog(
"productbatch::findAll", LOG_DEBUG);
477 $resql = $dbs->query($sql);
479 $num = $dbs->num_rows($resql);
482 $obj = $dbs->fetch_object($resql);
485 $tmp->id = $obj->rowid;
486 $tmp->tms = $dbs->jdate($obj->tms);
487 $tmp->fk_product_stock = $obj->fk_product_stock;
488 $tmp->batch = $obj->batch;
489 $tmp->qty = $obj->qty;
490 $tmp->import_key = $obj->import_key;
492 $tmp->lotid = $obj->lotid;
493 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
494 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
496 $ret[$tmp->batch] = $tmp;
503 $error =
"Error ".$dbs->lasterror();
520 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min =
null, $sortfield =
null, $sortorder =
null)
522 $productBatchList = array();
524 dol_syslog(__METHOD__.
' fk_product='.$fk_product.
', fk_warehouse='.$fk_warehouse.
', qty_min='.$qty_min.
', sortfield='.$sortfield.
', sortorder='.$sortorder, LOG_DEBUG);
528 $sql .=
", pl.fk_product";
529 $sql .=
", pl.batch";
530 $sql .=
", pl.sellby";
531 $sql .=
", pl.eatby";
533 $sql .=
" FROM ".$this->db->prefix().
"product_lot as pl";
534 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = pl.fk_product";
535 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_batch AS pb ON pl.batch = pb.batch";
536 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
537 $sql .=
" WHERE p.entity IN (".getEntity(
'product').
")";
538 $sql .=
" AND pl.fk_product = ".((int) $fk_product);
539 if ($fk_warehouse > 0) {
540 $sql .=
" AND ps.fk_entrepot = ".((int) $fk_warehouse);
542 if ($qty_min !==
null) {
543 $sql .=
" AND pb.qty > ".((float)
price2num($qty_min,
'MS'));
545 $sql .= $this->db->order($sortfield, $sortorder);
547 $resql = $this->db->query($sql);
549 while ($obj = $this->db->fetch_object($resql)) {
550 $productBatch =
new self($this->db);
551 $productBatch->id = $obj->rowid;
552 $productBatch->fk_product = $obj->fk_product;
553 $productBatch->batch = $obj->batch;
554 $productBatch->eatby = $this->db->jdate($obj->eatby);
555 $productBatch->sellby = $this->db->jdate($obj->sellby);
556 $productBatch->qty = $obj->qty;
557 $productBatchList[] = $productBatch;
559 $this->db->free($resql);
561 return $productBatchList;
563 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.
find($fk_product_stock=0, $eatby='', $sellby='', $batch_number='')
Find first detail record that match eather eat-by or sell-by or batch within given warehouse.
create($user, $notrigger=0)
Create object into database.
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 for a product and a warehouse.
__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_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.