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);
410 public function find($fk_product_stock = 0, $eatby =
'', $sellby =
'', $batch_number =
'', $fk_warehouse = 0)
417 $sql .=
" t.fk_product_stock,";
418 $sql .=
" t.sellby,";
422 $sql .=
" t.import_key";
423 $sql .=
" FROM ".$this->db->prefix().self::$_table_element.
" as t";
424 if ($fk_product_stock > 0 || empty($fk_warehouse)) {
425 $sql .=
" WHERE t.fk_product_stock = ".((int) $fk_product_stock);
427 $sql .=
", ".$this->db->prefix().
"product_stock as ps";
428 $sql .=
" WHERE t.fk_product_stock = ps.rowid AND ps.fk_entrepot = ".((int) $fk_warehouse);
430 if (!empty($eatby)) {
431 array_push($where,
" eatby = '".$this->db->idate($eatby).
"'");
433 if (!empty($sellby)) {
434 array_push($where,
" sellby = '".$this->db->idate($sellby).
"'");
437 if (!empty($batch_number)) {
438 $sql .=
" AND batch = '".$this->db->escape($batch_number).
"'";
441 if (!empty($where)) {
442 $sql .=
" AND (".$this->db->sanitize(implode(
" OR ", $where), 1, 1, 1).
")";
445 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
446 $resql = $this->db->query($sql);
448 if ($this->db->num_rows($resql)) {
449 $obj = $this->db->fetch_object($resql);
451 $this->
id = $obj->rowid;
453 $this->tms = $this->db->jdate($obj->tms);
454 $this->fk_product_stock = $obj->fk_product_stock;
455 $this->sellby = $this->db->jdate($obj->sellby);
456 $this->eatby = $this->db->jdate($obj->eatby);
457 $this->batch = $obj->batch;
458 $this->qty = $obj->qty;
459 $this->import_key = $obj->import_key;
461 $this->db->free($resql);
465 $this->error =
"Error ".$this->db->lasterror();
478 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
487 $sql .=
" t.fk_product_stock,";
488 $sql .=
" t.sellby as oldsellby,";
489 $sql .=
" t.eatby as oldeatby,";
493 $sql .=
" MAX(sm.datem) as date_entree,";
495 $sql .=
" t.import_key";
496 if ($fk_product > 0) {
497 $sql .=
", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
500 $sql .=
" FROM ".$dbs->prefix().
"product_batch as t";
501 if ($fk_product > 0) {
502 $sql .=
" LEFT JOIN ".$dbs->prefix().
"product_lot as pl ON pl.fk_product = ".((int) $fk_product).
" AND pl.batch = t.batch";
506 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product_stock AS ps ON (ps.rowid = fk_product_stock)';
507 $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))';
509 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
511 $sql .=
" AND t.qty <> 0";
514 $sql .=
' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
515 if ($fk_product > 0) {
516 $sql .=
', pl.rowid, pl.eatby, pl.sellby';
519 $sql .=
" ORDER BY ";
522 $sql .=
'date_entree ASC,t.batch ASC,';
524 if ($fk_product > 0) {
525 $sql .=
"pl.eatby ASC, pl.sellby ASC, ";
527 $sql .=
"t.eatby ASC, t.sellby ASC ";
528 $sql .=
", t.qty ".(!
getDolGlobalString(
'DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE') ?
'ASC' :
'DESC');
529 $sql .=
", t.batch ASC";
531 dol_syslog(
"productbatch::findAll", LOG_DEBUG);
533 $resql = $dbs->query($sql);
535 $num = $dbs->num_rows($resql);
538 $obj = $dbs->fetch_object($resql);
541 $tmp->id = $obj->rowid;
542 $tmp->tms = $dbs->jdate($obj->tms);
543 $tmp->fk_product_stock = $obj->fk_product_stock;
544 $tmp->batch = $obj->batch;
545 $tmp->qty = $obj->qty;
546 $tmp->import_key = $obj->import_key;
549 $tmp->context[
'stock_entry_date'] = $dbs->jdate($obj->date_entree);
552 if ($fk_product > 0) {
554 $tmp->lotid = $obj->lotid;
555 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
556 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
559 $ret[$tmp->batch] = $tmp;
583 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min =
null, $sortfield =
null, $sortorder =
null)
585 $productBatchList = array();
587 dol_syslog(__METHOD__.
' fk_product='.$fk_product.
', fk_warehouse='.$fk_warehouse.
', qty_min='.$qty_min.
', sortfield='.$sortfield.
', sortorder='.$sortorder, LOG_DEBUG);
591 $sql .=
", pl.fk_product";
592 $sql .=
", pl.batch";
593 $sql .=
", pl.sellby";
594 $sql .=
", pl.eatby";
596 $sql .=
" FROM ".$this->db->prefix().
"product_lot as pl";
597 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = pl.fk_product";
598 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_batch AS pb ON pl.batch = pb.batch";
599 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
600 $sql .=
" WHERE p.entity IN (".getEntity(
'product').
")";
601 $sql .=
" AND pl.fk_product = ".((int) $fk_product);
602 if ($fk_warehouse > 0) {
603 $sql .=
" AND ps.fk_entrepot = ".((int) $fk_warehouse);
605 if ($qty_min !==
null) {
606 $sql .=
" AND pb.qty > ".((float)
price2num($qty_min,
'MS'));
608 $sql .= $this->db->order($sortfield, $sortorder);
610 $resql = $this->db->query($sql);
612 while ($obj = $this->db->fetch_object($resql)) {
613 $productBatch =
new self($this->db);
614 $productBatch->id = $obj->rowid;
615 $productBatch->fk_product = $obj->fk_product;
616 $productBatch->batch = $obj->batch;
617 $productBatch->eatby = $this->db->jdate($obj->eatby);
618 $productBatch->sellby = $this->db->jdate($obj->sellby);
619 $productBatch->qty = $obj->qty;
620 $productBatchList[] = $productBatch;
622 $this->db->free($resql);
624 return $productBatchList;
626 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.
find($fk_product_stock=0, $eatby='', $sellby='', $batch_number='', $fk_warehouse=0)
Find first detailed record that match either eat-by, sell-by or batch within the warehouse.
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.
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...