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();
141 $sql .=
" t.fk_product_stock,";
142 $sql .=
" t.sellby as oldsellby,";
143 $sql .=
" t.eatby as oldeatby,";
146 $sql .=
" t.import_key,";
147 $sql .=
" w.fk_entrepot,";
148 $sql .=
" w.fk_product,";
149 $sql .=
" pl.eatby,";
150 $sql .=
" pl.sellby";
152 $sql .=
" FROM ".$this->db->prefix().
"product_batch as t INNER JOIN ".$this->
db->prefix().
"product_stock w on t.fk_product_stock = w.rowid";
153 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
154 $sql .=
" WHERE t.rowid = ".((int) $id);
156 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
160 $obj = $this->
db->fetch_object(
$resql);
162 $this->
id = $obj->rowid;
163 $this->tms = $this->
db->jdate($obj->tms);
164 $this->fk_product_stock = $obj->fk_product_stock;
165 $this->sellby = $this->
db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
166 $this->eatby = $this->
db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
167 $this->batch = $obj->batch;
168 $this->qty = $obj->qty;
169 $this->import_key = $obj->import_key;
170 $this->warehouseid = $obj->fk_entrepot;
171 $this->fk_product = $obj->fk_product;
177 $this->error =
"Error ".$this->db->lasterror();
189 public function update($user =
null, $notrigger = 0)
191 global $conf, $langs;
198 if ($this->qty < 0) {
202 $sql =
"UPDATE ".$this->db->prefix().self::$_table_element.
" SET";
203 $sql .=
" fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock :
"null").
",";
204 $sql .=
" sellby=".(dol_strlen($this->sellby) != 0 ?
"'".$this->db->idate($this->sellby).
"'" :
'null').
",";
205 $sql .=
" eatby=".(dol_strlen($this->eatby) != 0 ?
"'".$this->db->idate($this->eatby).
"'" :
'null').
",";
206 $sql .=
" batch=".(isset($this->batch) ?
"'".$this->db->escape($this->batch).
"'" :
"null").
",";
207 $sql .=
" qty=".(isset($this->qty) ? $this->qty :
"null").
",";
208 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
"";
209 $sql .=
" WHERE rowid=".((int) $this->
id);
213 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
216 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
221 foreach ($this->errors as $errmsg) {
222 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
223 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
225 $this->
db->rollback();
240 public function delete($user, $notrigger = 0)
242 global $conf, $langs;
248 $sql =
"DELETE FROM ".$this->db->prefix().self::$_table_element.
"";
249 $sql .=
" WHERE rowid=".((int) $this->
id);
251 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
254 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
260 foreach ($this->errors as $errmsg) {
261 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
262 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
264 $this->
db->rollback();
290 $object->fetch($fromid);
298 $object->context[
'createfromclone'] =
'createfromclone';
299 $result = $object->create($user);
303 $this->error = $object->error;
304 $this->errors = array_merge($this->errors, $object->errors);
311 unset($object->context[
'createfromclone']);
318 $this->
db->rollback();
335 $this->fk_product_stock =
'';
339 $this->import_key =
'';
349 if (isset($this->fk_product_stock)) {
350 $this->fk_product_stock = (int) trim($this->fk_product_stock);
352 if (isset($this->batch)) {
353 $this->batch = trim($this->batch);
355 if (isset($this->qty)) {
356 $this->qty = (
float) trim($this->qty);
358 if (isset($this->import_key)) {
359 $this->import_key = trim($this->import_key);
372 public function find($fk_product_stock = 0, $eatby =
'', $sellby =
'', $batch_number =
'')
380 $sql .=
" t.fk_product_stock,";
381 $sql .=
" t.sellby,";
385 $sql .=
" t.import_key";
386 $sql .=
" FROM ".$this->db->prefix().self::$_table_element.
" as t";
387 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
389 if (!empty($eatby)) {
390 array_push($where,
" eatby = '".$this->
db->idate($eatby).
"'");
392 if (!empty($sellby)) {
393 array_push($where,
" sellby = '".$this->
db->idate($sellby).
"'");
396 if (!empty($batch_number)) {
397 $sql .=
" AND batch = '".$this->db->escape($batch_number).
"'";
400 if (!empty($where)) {
401 $sql .=
" AND (".implode(
" OR ", $where).
")";
404 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
408 $obj = $this->
db->fetch_object(
$resql);
410 $this->
id = $obj->rowid;
412 $this->tms = $this->
db->jdate($obj->tms);
413 $this->fk_product_stock = $obj->fk_product_stock;
414 $this->sellby = $this->
db->jdate($obj->sellby);
415 $this->eatby = $this->
db->jdate($obj->eatby);
416 $this->batch = $obj->batch;
417 $this->qty = $obj->qty;
418 $this->import_key = $obj->import_key;
424 $this->error =
"Error ".$this->db->lasterror();
437 public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
446 $sql .=
" t.fk_product_stock,";
447 $sql .=
" t.sellby as oldsellby,";
448 $sql .=
" t.eatby as oldeatby,";
451 $sql .=
" t.import_key";
452 if ($fk_product > 0) {
453 $sql .=
", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
456 $sql .=
" FROM ".$dbs->prefix().
"product_batch as t";
457 if ($fk_product > 0) {
458 $sql .=
" LEFT JOIN ".$dbs->prefix().
"product_lot as pl ON pl.fk_product = ".((int) $fk_product).
" AND pl.batch = t.batch";
461 $sql .=
" WHERE fk_product_stock=".((int) $fk_product_stock);
463 $sql .=
" AND t.qty <> 0";
466 $sql .=
" ORDER BY ";
468 if ($fk_product > 0) { $sql .=
"pl.eatby ASC, pl.sellby ASC, "; }
469 $sql .=
"t.eatby ASC, t.sellby ASC ";
470 $sql .=
", t.qty ".(!empty($conf->global->DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE)?
'DESC':
'ASC');
472 dol_syslog(
"productbatch::findAll", LOG_DEBUG);
473 $resql = $dbs->query($sql);
475 $num = $dbs->num_rows(
$resql);
478 $obj = $dbs->fetch_object(
$resql);
481 $tmp->id = $obj->rowid;
482 $tmp->lotid = $obj->lotid;
483 $tmp->tms = $dbs->jdate($obj->tms);
484 $tmp->fk_product_stock = $obj->fk_product_stock;
485 $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
486 $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
487 $tmp->batch = $obj->batch;
488 $tmp->qty = $obj->qty;
489 $tmp->import_key = $obj->import_key;
491 $ret[$tmp->batch] = $tmp;
498 $error =
"Error ".$dbs->lasterror();
515 public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min =
null, $sortfield =
null, $sortorder =
null)
517 $productBatchList = array();
519 dol_syslog(__METHOD__.
' fk_product='.$fk_product.
', fk_warehouse='.$fk_warehouse.
', qty_min='.$qty_min.
', sortfield='.$sortfield.
', sortorder='.$sortorder, LOG_DEBUG);
523 $sql .=
", pl.fk_product";
524 $sql .=
", pl.batch";
525 $sql .=
", pl.sellby";
526 $sql .=
", pl.eatby";
528 $sql .=
" FROM ".$this->db->prefix().
"product_lot as pl";
529 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = pl.fk_product";
530 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_batch AS pb ON pl.batch = pb.batch";
531 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
532 $sql .=
" WHERE p.entity IN (".getEntity(
'product').
")";
533 $sql .=
" AND pl.fk_product = ".((int) $fk_product);
534 if ($fk_warehouse > 0) {
535 $sql .=
" AND ps.fk_entrepot = ".((int) $fk_warehouse);
537 if ($qty_min !==
null) {
538 $sql .=
" AND pb.qty > ".((float)
price2num($qty_min,
'MS'));
540 $sql .= $this->
db->order($sortfield, $sortorder);
544 while ($obj = $this->
db->fetch_object(
$resql)) {
545 $productBatch =
new self($this->db);
546 $productBatch->id = $obj->rowid;
547 $productBatch->fk_product = $obj->fk_product;
548 $productBatch->batch = $obj->batch;
549 $productBatch->eatby = $this->
db->jdate($obj->eatby);
550 $productBatch->sellby = $this->
db->jdate($obj->sellby);
551 $productBatch->qty = $obj->qty;
552 $productBatchList[] = $productBatch;
556 return $productBatchList;
558 dol_syslog(__METHOD__.
' Error: '.$this->db->lasterror(), LOG_ERR);