29require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
45 public $element =
'productbatch';
55 public $fk_product_stock;
114 public function create($user, $notrigger = 0)
125 $sql =
"INSERT INTO ".$this->db->prefix().
"product_batch (";
126 $sql .=
"fk_product_stock,";
131 $sql .=
"import_key";
132 $sql .=
") VALUES (";
133 $sql .=
" ".(!isset($this->fk_product_stock) ?
'NULL' : $this->fk_product_stock).
",";
134 $sql .=
" ".(!isset($this->sellby) ||
dol_strlen($this->sellby) == 0 ?
'NULL' :
"'".$this->db->idate($this->sellby).
"'").
",";
135 $sql .=
" ".(!isset($this->eatby) ||
dol_strlen($this->eatby) == 0 ?
'NULL' :
"'".$this->db->idate($this->eatby).
"'").
",";
136 $sql .=
" ".(!isset($this->batch) ?
'NULL' :
"'".$this->db->escape($this->batch).
"'").
",";
137 $sql .=
" ".(!isset($this->qty) ?
'NULL' : $this->qty).
",";
138 $sql .=
" ".(!isset($this->import_key) ?
'NULL' :
"'".$this->db->escape($this->import_key).
"'");
143 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
144 $resql = $this->db->query($sql);
147 $this->errors[] =
"Error ".$this->db->lasterror();
150 $this->
id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
155 $this->db->rollback();
175 $sql .=
" t.fk_product_stock,";
176 $sql .=
" t.sellby as oldsellby,";
177 $sql .=
" t.eatby as oldeatby,";
180 $sql .=
" t.import_key,";
181 $sql .=
" w.fk_entrepot,";
182 $sql .=
" w.fk_product,";
183 $sql .=
" pl.eatby,";
184 $sql .=
" pl.sellby";
185 $sql .=
" FROM ".$this->db->prefix().
"product_batch as t";
186 $sql .=
" INNER JOIN ".$this->db->prefix().
"product_stock w on t.fk_product_stock = w.rowid";
187 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
188 $sql .=
" WHERE t.rowid = ".((int) $id);
190 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
191 $resql = $this->db->query($sql);
193 if ($this->db->num_rows($resql)) {
194 $obj = $this->db->fetch_object($resql);
196 $this->
id = $obj->rowid;
197 $this->tms = $this->db->jdate($obj->tms);
198 $this->fk_product_stock = $obj->fk_product_stock;
199 $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
200 $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
201 $this->batch = $obj->batch;
202 $this->qty = $obj->qty;
203 $this->import_key = $obj->import_key;
204 $this->warehouseid = $obj->fk_entrepot;
205 $this->fk_product = $obj->fk_product;
207 $this->db->free($resql);
211 $this->error =
"Error ".$this->db->lasterror();
223 public function update($user =
null, $notrigger = 0)
237 $sql =
"UPDATE ".$this->db->prefix().self::$_table_element.
" SET";
238 $sql .=
" fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock :
"null").
",";
239 $sql .=
" sellby=".(dol_strlen($this->sellby) != 0 ?
"'".$this->db->idate($this->sellby).
"'" :
'null').
",";
240 $sql .=
" eatby=".(dol_strlen($this->eatby) != 0 ?
"'".$this->db->idate($this->eatby).
"'" :
'null').
",";
241 $sql .=
" batch=".(isset($this->batch) ?
"'".$this->db->escape($this->batch).
"'" :
"null").
",";
242 $sql .=
" qty=".(isset($this->qty) ? $this->qty :
"null").
",";
243 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
244 $sql .=
" WHERE rowid=".((int) $this->
id);
248 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
249 $resql = $this->db->query($sql);
252 $this->errors[] =
"Error ".$this->db->lasterror();
257 foreach ($this->errors as $errmsg) {
258 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
259 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
261 $this->db->rollback();
276 public function delete($user, $notrigger = 0)
283 $sql =
"DELETE FROM ".$this->db->prefix().self::$_table_element;
284 $sql .=
" WHERE rowid=".((int) $this->
id);
286 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
287 $resql = $this->db->query($sql);
290 $this->errors[] =
"Error ".$this->db->lasterror();
296 foreach ($this->errors as $errmsg) {
297 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
298 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
300 $this->db->rollback();
334 $object->context[
'createfromclone'] =
'createfromclone';
335 $result =
$object->create($user);
340 $this->errors = array_merge($this->errors,
$object->errors);
347 unset(
$object->context[
'createfromclone']);
354 $this->db->rollback();
371 $this->fk_product_stock = 0;
375 $this->import_key =
'';
387 if (isset($this->fk_product_stock)) {
388 $this->fk_product_stock = (int) trim((
string) $this->fk_product_stock);
390 if (isset($this->batch)) {
391 $this->batch = trim($this->batch);
393 if (isset($this->qty)) {
394 $this->qty = (float) trim((
string) $this->qty);
396 if (isset($this->import_key)) {
397 $this->import_key = trim($this->import_key);
411 public function find($fk_product_stock = 0, $eatby =
null, $sellby =
null, $batch_number =
'', $fk_warehouse = 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);
431 if (!empty($eatby)) {
432 array_push($where,
" eatby = '".$this->db->idate($eatby).
"'");
434 if (!empty($sellby)) {
435 array_push($where,
" sellby = '".$this->db->idate($sellby).
"'");
438 if (!empty($batch_number)) {
439 $sql .=
" AND batch = '".$this->db->escape($batch_number).
"'";
442 if (!empty($where)) {
443 $sql .=
" AND (".$this->db->sanitize(implode(
" OR ", $where), 1, 1, 1).
")";
446 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
447 $resql = $this->db->query($sql);
449 if ($this->db->num_rows($resql)) {
450 $obj = $this->db->fetch_object($resql);
452 $this->
id = $obj->rowid;
454 $this->tms = $this->db->jdate($obj->tms);
455 $this->fk_product_stock = $obj->fk_product_stock;
456 $this->sellby = $this->db->jdate($obj->sellby);
457 $this->eatby = $this->db->jdate($obj->eatby);
458 $this->batch = $obj->batch;
459 $this->qty = $obj->qty;
460 $this->import_key = $obj->import_key;
462 $this->db->free($resql);
466 $this->error =
"Error ".$this->db->lasterror();
479 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
488 $sql .=
" t.fk_product_stock,";
489 $sql .=
" t.sellby as oldsellby,";
490 $sql .=
" t.eatby as oldeatby,";
494 $sql .=
" MAX(sm.datem) as date_entree,";
496 $sql .=
" t.import_key";
497 if ($fk_product > 0) {
498 $sql .=
", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
501 $sql .=
" FROM ".$dbs->prefix().
"product_batch as t";
502 if ($fk_product > 0) {
503 $sql .=
" LEFT JOIN ".$dbs->prefix().
"product_lot as pl ON pl.fk_product = ".((int) $fk_product).
" AND pl.batch = t.batch";
507 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product_stock AS ps ON (ps.rowid = fk_product_stock)';
508 $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))';
510 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
512 $sql .=
" AND t.qty <> 0";
515 $sql .=
' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
516 if ($fk_product > 0) {
517 $sql .=
', pl.rowid, pl.eatby, pl.sellby';
520 $sql .=
" ORDER BY ";
523 $sql .=
'date_entree ASC,t.batch ASC,';
525 if ($fk_product > 0) {
526 $sql .=
"pl.eatby ASC, pl.sellby ASC, ";
528 $sql .=
"t.eatby ASC, t.sellby ASC ";
529 $sql .=
", t.qty ".(!
getDolGlobalString(
'DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE') ?
'ASC' :
'DESC');
530 $sql .=
", t.batch ASC";
532 dol_syslog(
"productbatch::findAll", LOG_DEBUG);
534 $resql = $dbs->query($sql);
536 $num = $dbs->num_rows($resql);
539 $obj = $dbs->fetch_object($resql);
542 $tmp->id = $obj->rowid;
543 $tmp->tms = $dbs->jdate($obj->tms);
544 $tmp->fk_product_stock = $obj->fk_product_stock;
545 $tmp->batch = $obj->batch;
546 $tmp->qty = $obj->qty;
547 $tmp->import_key = $obj->import_key;
550 $tmp->context[
'stock_entry_date'] = $dbs->jdate($obj->date_entree);
553 if ($fk_product > 0) {
555 $tmp->lotid = $obj->lotid;
556 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
557 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
560 $ret[$tmp->batch] = $tmp;
584 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min =
null, $sortfield =
null, $sortorder =
null)
586 $productBatchList = array();
588 dol_syslog(__METHOD__.
' fk_product='.$fk_product.
', fk_warehouse='.$fk_warehouse.
', qty_min='.$qty_min.
', sortfield='.$sortfield.
', sortorder='.$sortorder, LOG_DEBUG);
592 $sql .=
", pl.fk_product";
593 $sql .=
", pl.batch";
594 $sql .=
", pl.sellby";
595 $sql .=
", pl.eatby";
597 $sql .=
" FROM ".$this->db->prefix().
"product_lot as pl";
598 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = pl.fk_product";
599 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_batch AS pb ON pl.batch = pb.batch";
600 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
601 $sql .=
" WHERE p.entity IN (".getEntity(
'product').
")";
602 $sql .=
" AND pl.fk_product = ".((int) $fk_product);
603 if ($fk_warehouse > 0) {
604 $sql .=
" AND ps.fk_entrepot = ".((int) $fk_warehouse);
606 if ($qty_min !==
null) {
607 $sql .=
" AND pb.qty > ".((float)
price2num($qty_min,
'MS'));
609 $sql .= $this->db->order($sortfield, $sortorder);
611 $resql = $this->db->query($sql);
613 while ($obj = $this->db->fetch_object($resql)) {
614 $productBatch =
new self($this->db);
615 $productBatch->id = $obj->rowid;
616 $productBatch->fk_product = $obj->fk_product;
617 $productBatch->batch = $obj->batch;
618 $productBatch->eatby = $this->db->jdate($obj->eatby);
619 $productBatch->sellby = $this->db->jdate($obj->sellby);
620 $productBatch->qty = $obj->qty;
621 $productBatchList[] = $productBatch;
623 $this->db->free($resql);
625 return $productBatchList;
627 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 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...