25 require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
41 public $element =
'productbatch';
46 public $fk_product_stock;
78 public function create($user, $notrigger = 0)
90 $sql =
"INSERT INTO ".$this->db->prefix().
"product_batch (";
91 $sql .=
"fk_product_stock,";
98 $sql .=
" ".(!isset($this->fk_product_stock) ?
'NULL' : $this->fk_product_stock).
",";
99 $sql .=
" ".(!isset($this->sellby) ||
dol_strlen($this->sellby) == 0 ?
'NULL' :
"'".$this->db->idate($this->sellby).
"'").
",";
100 $sql .=
" ".(!isset($this->eatby) ||
dol_strlen($this->eatby) == 0 ?
'NULL' :
"'".$this->db->idate($this->eatby).
"'").
",";
101 $sql .=
" ".(!isset($this->batch) ?
'NULL' :
"'".$this->db->escape($this->batch).
"'").
",";
102 $sql .=
" ".(!isset($this->qty) ?
'NULL' : $this->qty).
",";
103 $sql .=
" ".(!isset($this->import_key) ?
'NULL' :
"'".$this->db->escape($this->import_key).
"'");
108 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
111 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
114 $this->
id = $this->
db->last_insert_id($this->
db->prefix().self::$_table_element);
119 $this->
db->rollback();
140 $sql .=
" t.fk_product_stock,";
141 $sql .=
" t.sellby as oldsellby,";
142 $sql .=
" t.eatby as oldeatby,";
145 $sql .=
" t.import_key,";
146 $sql .=
" w.fk_entrepot,";
147 $sql .=
" w.fk_product,";
148 $sql .=
" pl.eatby,";
149 $sql .=
" pl.sellby";
150 $sql .=
" FROM ".$this->db->prefix().
"product_batch as t";
151 $sql .=
" INNER JOIN ".$this->db->prefix().
"product_stock w on t.fk_product_stock = w.rowid";
152 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
153 $sql .=
" WHERE t.rowid = ".((int) $id);
155 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
159 $obj = $this->
db->fetch_object(
$resql);
161 $this->
id = $obj->rowid;
162 $this->tms = $this->
db->jdate($obj->tms);
163 $this->fk_product_stock = $obj->fk_product_stock;
164 $this->sellby = $this->
db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
165 $this->eatby = $this->
db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
166 $this->batch = $obj->batch;
167 $this->qty = $obj->qty;
168 $this->import_key = $obj->import_key;
169 $this->warehouseid = $obj->fk_entrepot;
170 $this->fk_product = $obj->fk_product;
176 $this->error =
"Error ".$this->db->lasterror();
188 public function update($user =
null, $notrigger = 0)
190 global $conf, $langs;
197 if ($this->qty < 0) {
201 $sql =
"UPDATE ".$this->db->prefix().self::$_table_element.
" SET";
202 $sql .=
" fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock :
"null").
",";
203 $sql .=
" sellby=".(dol_strlen($this->sellby) != 0 ?
"'".$this->db->idate($this->sellby).
"'" :
'null').
",";
204 $sql .=
" eatby=".(dol_strlen($this->eatby) != 0 ?
"'".$this->db->idate($this->eatby).
"'" :
'null').
",";
205 $sql .=
" batch=".(isset($this->batch) ?
"'".$this->db->escape($this->batch).
"'" :
"null").
",";
206 $sql .=
" qty=".(isset($this->qty) ? $this->qty :
"null").
",";
207 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
208 $sql .=
" WHERE rowid=".((int) $this->
id);
212 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
215 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
220 foreach ($this->errors as $errmsg) {
221 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
222 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
224 $this->
db->rollback();
239 public function delete($user, $notrigger = 0)
241 global $conf, $langs;
247 $sql =
"DELETE FROM ".$this->db->prefix().self::$_table_element;
248 $sql .=
" WHERE rowid=".((int) $this->
id);
250 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
253 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
259 foreach ($this->errors as $errmsg) {
260 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
261 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
263 $this->
db->rollback();
289 $object->fetch($fromid);
297 $object->context[
'createfromclone'] =
'createfromclone';
298 $result = $object->create($user);
302 $this->error = $object->error;
303 $this->errors = array_merge($this->errors, $object->errors);
310 unset($object->context[
'createfromclone']);
317 $this->
db->rollback();
334 $this->fk_product_stock =
'';
338 $this->import_key =
'';
348 if (isset($this->fk_product_stock)) {
349 $this->fk_product_stock = (int) trim($this->fk_product_stock);
351 if (isset($this->batch)) {
352 $this->batch = trim($this->batch);
354 if (isset($this->qty)) {
355 $this->qty = (
float) trim($this->qty);
357 if (isset($this->import_key)) {
358 $this->import_key = trim($this->import_key);
371 public function find($fk_product_stock = 0, $eatby =
'', $sellby =
'', $batch_number =
'')
379 $sql .=
" t.fk_product_stock,";
380 $sql .=
" t.sellby,";
384 $sql .=
" t.import_key";
385 $sql .=
" FROM ".$this->db->prefix().self::$_table_element.
" as t";
386 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
388 if (!empty($eatby)) {
389 array_push($where,
" eatby = '".$this->
db->idate($eatby).
"'");
391 if (!empty($sellby)) {
392 array_push($where,
" sellby = '".$this->
db->idate($sellby).
"'");
395 if (!empty($batch_number)) {
396 $sql .=
" AND batch = '".$this->db->escape($batch_number).
"'";
399 if (!empty($where)) {
400 $sql .=
" AND (".implode(
" OR ", $where).
")";
403 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
407 $obj = $this->
db->fetch_object(
$resql);
409 $this->
id = $obj->rowid;
411 $this->tms = $this->
db->jdate($obj->tms);
412 $this->fk_product_stock = $obj->fk_product_stock;
413 $this->sellby = $this->
db->jdate($obj->sellby);
414 $this->eatby = $this->
db->jdate($obj->eatby);
415 $this->batch = $obj->batch;
416 $this->qty = $obj->qty;
417 $this->import_key = $obj->import_key;
423 $this->error =
"Error ".$this->db->lasterror();
436 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
445 $sql .=
" t.fk_product_stock,";
446 $sql .=
" t.sellby as oldsellby,";
447 $sql .=
" t.eatby as oldeatby,";
450 $sql .=
" t.import_key";
451 if ($fk_product > 0) {
452 $sql .=
", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
455 $sql .=
" FROM ".$dbs->prefix().
"product_batch as t";
456 if ($fk_product > 0) {
457 $sql .=
" LEFT JOIN ".$dbs->prefix().
"product_lot as pl ON pl.fk_product = ".((int) $fk_product).
" AND pl.batch = t.batch";
460 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
462 $sql .=
" AND t.qty <> 0";
465 $sql .=
" ORDER BY ";
467 if ($fk_product > 0) { $sql .=
"pl.eatby ASC, pl.sellby ASC, "; }
468 $sql .=
"t.eatby ASC, t.sellby ASC ";
469 $sql .=
", t.qty ".(!empty($conf->global->DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE)?
'DESC':
'ASC');
471 dol_syslog(
"productbatch::findAll", LOG_DEBUG);
472 $resql = $dbs->query($sql);
474 $num = $dbs->num_rows(
$resql);
477 $obj = $dbs->fetch_object(
$resql);
480 $tmp->id = $obj->rowid;
481 $tmp->lotid = $obj->lotid;
482 $tmp->tms = $dbs->jdate($obj->tms);
483 $tmp->fk_product_stock = $obj->fk_product_stock;
484 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
485 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
486 $tmp->batch = $obj->batch;
487 $tmp->qty = $obj->qty;
488 $tmp->import_key = $obj->import_key;
490 $ret[$tmp->batch] = $tmp;
497 $error =
"Error ".$dbs->lasterror();
514 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min =
null, $sortfield =
null, $sortorder =
null)
516 $productBatchList = array();
518 dol_syslog(__METHOD__.
' fk_product='.$fk_product.
', fk_warehouse='.$fk_warehouse.
', qty_min='.$qty_min.
', sortfield='.$sortfield.
', sortorder='.$sortorder, LOG_DEBUG);
522 $sql .=
", pl.fk_product";
523 $sql .=
", pl.batch";
524 $sql .=
", pl.sellby";
525 $sql .=
", pl.eatby";
527 $sql .=
" FROM ".$this->db->prefix().
"product_lot as pl";
528 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = pl.fk_product";
529 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_batch AS pb ON pl.batch = pb.batch";
530 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
531 $sql .=
" WHERE p.entity IN (".getEntity(
'product').
")";
532 $sql .=
" AND pl.fk_product = ".((int) $fk_product);
533 if ($fk_warehouse > 0) {
534 $sql .=
" AND ps.fk_entrepot = ".((int) $fk_warehouse);
536 if ($qty_min !==
null) {
537 $sql .=
" AND pb.qty > ".((float)
price2num($qty_min,
'MS'));
539 $sql .= $this->
db->order($sortfield, $sortorder);
543 while ($obj = $this->
db->fetch_object(
$resql)) {
544 $productBatch =
new self($this->db);
545 $productBatch->id = $obj->rowid;
546 $productBatch->fk_product = $obj->fk_product;
547 $productBatch->batch = $obj->batch;
548 $productBatch->eatby = $this->
db->jdate($obj->eatby);
549 $productBatch->sellby = $this->
db->jdate($obj->sellby);
550 $productBatch->qty = $obj->qty;
551 $productBatchList[] = $productBatch;
555 return $productBatchList;
557 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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
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_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
div float
Buy price without taxes.
$conf db
API class for accounts.