29 require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
45 public $element =
'productbatch';
49 public $fk_product_stock;
93 public function create($user, $notrigger = 0)
104 $sql =
"INSERT INTO ".$this->db->prefix().
"product_batch (";
105 $sql .=
"fk_product_stock,";
110 $sql .=
"import_key";
111 $sql .=
") VALUES (";
112 $sql .=
" ".(!isset($this->fk_product_stock) ?
'NULL' : $this->fk_product_stock).
",";
113 $sql .=
" ".(!isset($this->sellby) ||
dol_strlen($this->sellby) == 0 ?
'NULL' :
"'".$this->db->idate($this->sellby).
"'").
",";
114 $sql .=
" ".(!isset($this->eatby) ||
dol_strlen($this->eatby) == 0 ?
'NULL' :
"'".$this->db->idate($this->eatby).
"'").
",";
115 $sql .=
" ".(!isset($this->batch) ?
'NULL' :
"'".$this->db->escape($this->batch).
"'").
",";
116 $sql .=
" ".(!isset($this->qty) ?
'NULL' : $this->qty).
",";
117 $sql .=
" ".(!isset($this->import_key) ?
'NULL' :
"'".$this->db->escape($this->import_key).
"'");
122 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
123 $resql = $this->db->query(
$sql);
126 $this->errors[] =
"Error ".$this->db->lasterror();
129 $this->
id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
134 $this->db->rollback();
154 $sql .=
" t.fk_product_stock,";
155 $sql .=
" t.sellby as oldsellby,";
156 $sql .=
" t.eatby as oldeatby,";
159 $sql .=
" t.import_key,";
160 $sql .=
" w.fk_entrepot,";
161 $sql .=
" w.fk_product,";
162 $sql .=
" pl.eatby,";
163 $sql .=
" pl.sellby";
164 $sql .=
" FROM ".$this->db->prefix().
"product_batch as t";
165 $sql .=
" INNER JOIN ".$this->db->prefix().
"product_stock w on t.fk_product_stock = w.rowid";
166 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
167 $sql .=
" WHERE t.rowid = ".((int) $id);
169 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
170 $resql = $this->db->query(
$sql);
172 if ($this->db->num_rows($resql)) {
173 $obj = $this->db->fetch_object($resql);
175 $this->
id = $obj->rowid;
176 $this->tms = $this->db->jdate($obj->tms);
177 $this->fk_product_stock = $obj->fk_product_stock;
178 $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
179 $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
180 $this->batch = $obj->batch;
181 $this->qty = $obj->qty;
182 $this->import_key = $obj->import_key;
183 $this->warehouseid = $obj->fk_entrepot;
184 $this->fk_product = $obj->fk_product;
186 $this->db->free($resql);
190 $this->error =
"Error ".$this->db->lasterror();
202 public function update($user =
null, $notrigger = 0)
210 if ($this->qty < 0) {
214 $sql =
"UPDATE ".$this->db->prefix().self::$_table_element.
" SET";
215 $sql .=
" fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock :
"null").
",";
216 $sql .=
" sellby=".(dol_strlen($this->sellby) != 0 ?
"'".$this->db->idate($this->sellby).
"'" :
'null').
",";
217 $sql .=
" eatby=".(dol_strlen($this->eatby) != 0 ?
"'".$this->db->idate($this->eatby).
"'" :
'null').
",";
218 $sql .=
" batch=".(isset($this->batch) ?
"'".$this->db->escape($this->batch).
"'" :
"null").
",";
219 $sql .=
" qty=".(isset($this->qty) ? $this->qty :
"null").
",";
220 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
221 $sql .=
" WHERE rowid=".((int) $this->
id);
225 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
226 $resql = $this->db->query(
$sql);
229 $this->errors[] =
"Error ".$this->db->lasterror();
234 foreach ($this->errors as $errmsg) {
235 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
236 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
238 $this->db->rollback();
253 public function delete($user, $notrigger = 0)
260 $sql =
"DELETE FROM ".$this->db->prefix().self::$_table_element;
261 $sql .=
" WHERE rowid=".((int) $this->
id);
263 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
264 $resql = $this->db->query(
$sql);
267 $this->errors[] =
"Error ".$this->db->lasterror();
273 foreach ($this->errors as $errmsg) {
274 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
275 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
277 $this->db->rollback();
311 $object->context[
'createfromclone'] =
'createfromclone';
312 $result =
$object->create($user);
317 $this->errors = array_merge($this->errors,
$object->errors);
324 unset(
$object->context[
'createfromclone']);
331 $this->db->rollback();
348 $this->fk_product_stock =
'';
352 $this->import_key =
'';
364 if (isset($this->fk_product_stock)) {
365 $this->fk_product_stock = (int) trim($this->fk_product_stock);
367 if (isset($this->batch)) {
368 $this->batch = trim($this->batch);
370 if (isset($this->qty)) {
371 $this->qty = (
float) trim((
string) $this->qty);
373 if (isset($this->import_key)) {
374 $this->import_key = trim($this->import_key);
388 public function find($fk_product_stock = 0, $eatby =
null, $sellby =
null, $batch_number =
'', $fk_warehouse = 0)
395 $sql .=
" t.fk_product_stock,";
396 $sql .=
" t.sellby,";
400 $sql .=
" t.import_key";
401 $sql .=
" FROM ".$this->db->prefix().self::$_table_element.
" as t";
402 if ($fk_product_stock > 0 || empty($fk_warehouse)) {
403 $sql .=
" WHERE t.fk_product_stock = ".((int) $fk_product_stock);
405 $sql .=
", ".$this->db->prefix().
"product_stock as ps";
406 $sql .=
" WHERE t.fk_product_stock = ps.rowid AND ps.fk_entrepot = ".((int) $fk_warehouse);
408 if (!empty($eatby)) {
409 array_push($where,
" eatby = '".$this->db->idate($eatby).
"'");
411 if (!empty($sellby)) {
412 array_push($where,
" sellby = '".$this->db->idate($sellby).
"'");
415 if (!empty($batch_number)) {
416 $sql .=
" AND batch = '".$this->db->escape($batch_number).
"'";
419 if (!empty($where)) {
420 $sql .=
" AND (".$this->db->sanitize(implode(
" OR ", $where), 1, 1, 1).
")";
423 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
424 $resql = $this->db->query(
$sql);
426 if ($this->db->num_rows($resql)) {
427 $obj = $this->db->fetch_object($resql);
429 $this->
id = $obj->rowid;
431 $this->tms = $this->db->jdate($obj->tms);
432 $this->fk_product_stock = $obj->fk_product_stock;
433 $this->sellby = $this->db->jdate($obj->sellby);
434 $this->eatby = $this->db->jdate($obj->eatby);
435 $this->batch = $obj->batch;
436 $this->qty = $obj->qty;
437 $this->import_key = $obj->import_key;
439 $this->db->free($resql);
443 $this->error =
"Error ".$this->db->lasterror();
456 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
465 $sql .=
" t.fk_product_stock,";
466 $sql .=
" t.sellby as oldsellby,";
467 $sql .=
" t.eatby as oldeatby,";
471 $sql .=
" MAX(sm.datem) as date_entree,";
473 $sql .=
" t.import_key";
474 if ($fk_product > 0) {
475 $sql .=
", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
478 $sql .=
" FROM ".$dbs->prefix().
"product_batch as t";
479 if ($fk_product > 0) {
480 $sql .=
" LEFT JOIN ".$dbs->prefix().
"product_lot as pl ON pl.fk_product = ".((int) $fk_product).
" AND pl.batch = t.batch";
484 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product_stock AS ps ON (ps.rowid = fk_product_stock)';
485 $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))';
487 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
489 $sql .=
" AND t.qty <> 0";
492 $sql .=
' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
493 if ($fk_product > 0) {
494 $sql .=
', pl.rowid, pl.eatby, pl.sellby';
497 $sql .=
" ORDER BY ";
500 $sql .=
'date_entree ASC,t.batch ASC,';
502 if ($fk_product > 0) {
503 $sql .=
"pl.eatby ASC, pl.sellby ASC, ";
505 $sql .=
"t.eatby ASC, t.sellby ASC ";
507 $sql .=
", t.batch ASC";
509 dol_syslog(
"productbatch::findAll", LOG_DEBUG);
511 $resql = $dbs->query(
$sql);
513 $num = $dbs->num_rows($resql);
516 $obj = $dbs->fetch_object($resql);
519 $tmp->id = $obj->rowid;
520 $tmp->tms = $dbs->jdate($obj->tms);
521 $tmp->fk_product_stock = $obj->fk_product_stock;
522 $tmp->batch = $obj->batch;
523 $tmp->qty = $obj->qty;
524 $tmp->import_key = $obj->import_key;
527 $tmp->context[
'stock_entry_date'] = $dbs->jdate($obj->date_entree);
530 if ($fk_product > 0) {
532 $tmp->lotid = $obj->lotid;
533 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
534 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
537 $ret[$tmp->batch] = $tmp;
561 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min =
null, $sortfield =
null, $sortorder =
null)
563 $productBatchList = array();
565 dol_syslog(__METHOD__.
' fk_product='.$fk_product.
', fk_warehouse='.$fk_warehouse.
', qty_min='.$qty_min.
', sortfield='.$sortfield.
', sortorder='.$sortorder, LOG_DEBUG);
569 $sql .=
", pl.fk_product";
570 $sql .=
", pl.batch";
571 $sql .=
", pl.sellby";
572 $sql .=
", pl.eatby";
574 $sql .=
" FROM ".$this->db->prefix().
"product_lot as pl";
575 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = pl.fk_product";
576 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_batch AS pb ON pl.batch = pb.batch";
577 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
578 $sql .=
" WHERE p.entity IN (".getEntity(
'product').
")";
579 $sql .=
" AND pl.fk_product = ".((int) $fk_product);
580 if ($fk_warehouse > 0) {
581 $sql .=
" AND ps.fk_entrepot = ".((int) $fk_warehouse);
583 if ($qty_min !==
null) {
584 $sql .=
" AND pb.qty > ".((float)
price2num($qty_min,
'MS'));
586 $sql .= $this->db->order($sortfield, $sortorder);
588 $resql = $this->db->query(
$sql);
590 while ($obj = $this->db->fetch_object($resql)) {
591 $productBatch =
new self($this->db);
592 $productBatch->id = $obj->rowid;
593 $productBatch->fk_product = $obj->fk_product;
594 $productBatch->batch = $obj->batch;
595 $productBatch->eatby = $this->db->jdate($obj->eatby);
596 $productBatch->sellby = $this->db->jdate($obj->sellby);
597 $productBatch->qty = $obj->qty;
598 $productBatchList[] = $productBatch;
600 $this->db->free($resql);
602 return $productBatchList;
604 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.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
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.
div float
Buy price without taxes.