29 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
41 public $element =
'productlot';
46 public $table_element =
'product_lot';
51 public $picto =
'lot';
57 public $ismultientitymanaged = 1;
88 public $fields = array(
89 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'noteditable'=>1,
'notnull'=> 1,
'index'=>1,
'position'=>1,
'comment'=>
'Id',
'css'=>
'left'),
90 'fk_product' => array(
'type'=>
'integer:Product:product/class/product.class.php',
'label'=>
'Product',
'enabled'=>1,
'visible'=>1,
'position'=>5,
'notnull'=>1,
'index'=>1,
'searchall'=>1),
91 'batch' => array(
'type'=>
'varchar(30)',
'label'=>
'Batch',
'enabled'=>1,
'visible'=>1,
'notnull'=>0,
'showoncombobox'=>1,
'index'=>1,
'position'=>10,
'comment'=>
'Batch',
'searchall'=>1),
92 'entity' => array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'default'=>1,
'notnull'=>1,
'index'=>1,
'position'=>20),
93 'sellby' => array(
'type'=>
'date',
'label'=>
'SellByDate',
'enabled'=>
'empty($conf->global->PRODUCT_DISABLE_SELLBY)?1:0',
'visible'=>5,
'position'=>60),
94 'eol_date' => array(
'type'=>
'date',
'label'=>
'EndOfLife',
'enabled'=>
'empty($conf->global->PRODUCT_ENABLE_TRACEABILITY)?0:1',
'visible'=>5,
'position'=>70),
95 'manufacturing_date' => array(
'type'=>
'date',
'label'=>
'ManufacturingDate',
'enabled'=>
'empty($conf->global->PRODUCT_ENABLE_TRACEABILITY)?0:1',
'visible'=>5,
'position'=>80),
96 'scrapping_date' => array(
'type'=>
'date',
'label'=>
'DestructionDate',
'enabled'=>
'empty($conf->global->PRODUCT_ENABLE_TRACEABILITY)?0:1',
'visible'=>5,
'position'=>90),
99 'eatby' => array(
'type'=>
'date',
'label'=>
'EatByDate',
'enabled'=>
'empty($conf->global->PRODUCT_DISABLE_EATBY)?1:0',
'visible'=>5,
'position'=>62),
100 'datec' => array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'position'=>500),
101 'tms' => array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>501),
102 'fk_user_creat' => array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>510,
'foreignkey'=>
'llx_user.rowid'),
103 'fk_user_modif' => array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>511),
104 'import_key' => array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'index'=>0,
'position'=>1000),
120 public $eol_date =
'';
121 public $manufacturing_date =
'';
122 public $scrapping_date =
'';
131 public $fk_user_creat;
136 public $fk_user_modif;
168 if (isset($this->entity)) {
169 $this->entity = (int) $this->entity;
171 if (isset($this->fk_product)) {
172 $this->fk_product = (int) $this->fk_product;
174 if (isset($this->batch)) {
175 $this->batch = trim($this->batch);
177 if (isset($this->fk_user_creat)) {
178 $this->fk_user_creat = (int) $this->fk_user_creat;
180 if (isset($this->fk_user_modif)) {
181 $this->fk_user_modif = (int) $this->fk_user_modif;
183 if (isset($this->import_key)) {
184 $this->import_key = trim($this->import_key);
191 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element.
'(';
193 $sql .=
'fk_product,';
198 $sql .=
'manufacturing_date,';
199 $sql .=
'scrapping_date,';
203 $sql .=
'fk_user_creat,';
204 $sql .=
'fk_user_modif,';
205 $sql .=
'import_key';
206 $sql .=
') VALUES (';
207 $sql .=
' '.(!isset($this->entity) ? $conf->entity : $this->entity).
',';
208 $sql .=
' '.(!isset($this->fk_product) ?
'NULL' : $this->fk_product).
',';
209 $sql .=
' '.(!isset($this->batch) ?
'NULL' :
"'".$this->db->escape($this->batch).
"'").
',';
210 $sql .=
' '.(!isset($this->eatby) ||
dol_strlen($this->eatby) == 0 ?
'NULL' :
"'".$this->db->idate($this->eatby).
"'").
',';
211 $sql .=
' '.(!isset($this->sellby) ||
dol_strlen($this->sellby) == 0 ?
'NULL' :
"'".$this->db->idate($this->sellby).
"'").
',';
212 $sql .=
' '.(!isset($this->eol_date) ||
dol_strlen($this->eol_date) == 0 ?
'NULL' :
"'".$this->db->idate($this->eol_date).
"'").
',';
213 $sql .=
' '.(!isset($this->manufacturing_date) ||
dol_strlen($this->manufacturing_date) == 0 ?
'NULL' :
"'".$this->db->idate($this->manufacturing_date).
"'").
',';
214 $sql .=
' '.(!isset($this->scrapping_date) ||
dol_strlen($this->scrapping_date) == 0 ?
'NULL' :
"'".$this->db->idate($this->scrapping_date).
"'").
',';
217 $sql .=
' '.
"'".$this->
db->idate(
dol_now()).
"'".
',';
218 $sql .=
' '.(!isset($this->fk_user_creat) ?
'NULL' : $this->fk_user_creat).
',';
219 $sql .=
' '.(!isset($this->fk_user_modif) ?
'NULL' : $this->fk_user_modif).
',';
220 $sql .=
' '.(!isset($this->import_key) ?
'NULL' : $this->import_key);
228 $this->errors[] =
'Error '.$this->db->lasterror();
229 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
233 $this->
id = $this->
db->last_insert_id($this->
db->prefix().$this->table_element);
243 if (!$error && !$notrigger) {
248 $result = $this->
call_trigger(
'PRODUCTLOT_CREATE', $user);
258 $this->
db->rollback();
277 public function fetch($id = 0, $product_id = 0, $batch =
'')
284 $sql .=
" t.entity,";
285 $sql .=
" t.fk_product,";
288 $sql .=
" t.sellby,";
289 $sql .=
" t.eol_date,";
290 $sql .=
" t.manufacturing_date,";
291 $sql .=
" t.scrapping_date,";
296 $sql .=
" t.fk_user_creat,";
297 $sql .=
" t.fk_user_modif,";
298 $sql .=
" t.import_key";
299 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
300 if ($product_id > 0 && $batch !=
'') {
301 $sql .=
" WHERE t.batch = '".$this->db->escape($batch).
"' AND t.fk_product = ".((int) $product_id);
303 $sql .=
" WHERE t.rowid = ".((int) $id);
308 $numrows = $this->
db->num_rows(
$resql);
310 $obj = $this->
db->fetch_object(
$resql);
312 $this->
id = $obj->rowid;
313 $this->
ref = $obj->rowid;
316 $this->batch = $obj->batch;
317 $this->entity = (!empty($obj->entity) ? $obj->entity : $conf->entity);
318 $this->fk_product = $obj->fk_product;
319 $this->eatby = $this->
db->jdate($obj->eatby);
320 $this->sellby = $this->
db->jdate($obj->sellby);
321 $this->eol_date = $this->
db->jdate($obj->eol_date);
322 $this->manufacturing_date = $this->
db->jdate($obj->manufacturing_date);
323 $this->scrapping_date = $this->
db->jdate($obj->scrapping_date);
327 $this->datec = $this->
db->jdate($obj->datec);
328 $this->tms = $this->
db->jdate($obj->tms);
329 $this->fk_user_creat = $obj->fk_user_creat;
330 $this->fk_user_modif = $obj->fk_user_modif;
331 $this->import_key = $obj->import_key;
345 $this->errors[] =
'Error '.$this->db->lasterror();
346 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
368 if (isset($this->entity)) {
369 $this->entity = (int) $this->entity;
371 if (isset($this->fk_product)) {
372 $this->fk_product = (int) $this->fk_product;
374 if (isset($this->batch)) {
375 $this->batch = trim($this->batch);
377 if (isset($this->fk_user_creat)) {
378 $this->fk_user_creat = (int) $this->fk_user_creat;
380 if (isset($this->fk_user_modif)) {
381 $this->fk_user_modif = (int) $this->fk_user_modif;
383 if (isset($this->import_key)) {
384 $this->import_key = trim($this->import_key);
390 if (empty($this->oldcopy)) {
391 $org =
new self($this->db);
392 $org->fetch($this->
id);
393 $this->oldcopy = $org;
397 $sql =
'UPDATE '.$this->db->prefix().$this->table_element.
' SET';
398 $sql .=
' entity = '.(isset($this->entity) ? $this->entity :
"null").
',';
399 $sql .=
' fk_product = '.(isset($this->fk_product) ? $this->fk_product :
"null").
',';
400 $sql .=
' batch = '.(isset($this->batch) ?
"'".$this->db->escape($this->batch).
"'" :
"null").
',';
401 $sql .=
' eatby = '.(!isset($this->eatby) ||
dol_strlen($this->eatby) != 0 ?
"'".$this->db->idate($this->eatby).
"'" :
'null').
',';
402 $sql .=
' sellby = '.(!isset($this->sellby) ||
dol_strlen($this->sellby) != 0 ?
"'".$this->db->idate($this->sellby).
"'" :
'null').
',';
403 $sql .=
' eol_date = '.(!isset($this->eol_date) ||
dol_strlen($this->eol_date) != 0 ?
"'".$this->db->idate($this->eol_date).
"'" :
'null').
',';
404 $sql .=
' manufacturing_date = '.(!isset($this->manufacturing_date) ||
dol_strlen($this->manufacturing_date) != 0 ?
"'".$this->db->idate($this->manufacturing_date).
"'" :
'null').
',';
405 $sql .=
' scrapping_date = '.(!isset($this->scrapping_date) ||
dol_strlen($this->scrapping_date) != 0 ?
"'".$this->db->idate($this->scrapping_date).
"'" :
'null').
',';
408 $sql .=
' datec = '.(!isset($this->datec) ||
dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
',';
409 $sql .=
' tms = '.(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
"'".$this->db->idate(
dol_now()).
"'").
',';
410 $sql .=
' fk_user_creat = '.(isset($this->fk_user_creat) ? $this->fk_user_creat :
"null").
',';
411 $sql .=
' fk_user_modif = '.(isset($this->fk_user_modif) ? $this->fk_user_modif :
"null").
',';
412 $sql .=
' import_key = '.(isset($this->import_key) ? $this->import_key :
"null");
413 $sql .=
' WHERE rowid='.((int) $this->
id);
420 $this->errors[] =
'Error '.$this->db->lasterror();
421 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
432 if (!$error && !$notrigger) {
434 $result = $this->
call_trigger(
'PRODUCTLOT_MODIFY', $user);
443 $this->
db->rollback();
461 public function delete(
User $user, $notrigger =
false)
482 $sql =
'DELETE FROM '.$this->db->prefix().$this->table_element;
483 $sql .=
' WHERE rowid='.((int) $this->
id);
488 $this->errors[] =
'Error '.$this->db->lasterror();
489 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
495 $this->
db->rollback();
522 $object->fetch($fromid);
530 $object->context[
'createfromclone'] =
'createfromclone';
531 $result = $object->create($user);
536 $this->errors = $object->errors;
537 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
540 unset($object->context[
'createfromclone']);
548 $this->
db->rollback();
597 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $maxlen = 24, $morecss =
'', $save_lastsearch_value = -1)
599 global $langs, $conf, $db;
600 global $dolibarr_main_authentication, $dolibarr_main_demo;
605 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Batch").
'</u>';
606 $label .=
'<div width="100%">';
607 $label .=
'<b>'.$langs->trans(
'Batch').
':</b> '.$this->batch;
608 if ($this->eatby && empty($conf->global->PRODUCT_DISABLE_EATBY)) {
609 $label .=
'<br><b>'.$langs->trans(
'EatByDate').
':</b> '.
dol_print_date($this->eatby,
'day');
611 if ($this->sellby && empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
612 $label .=
'<br><b>'.$langs->trans(
'SellByDate').
':</b> '.
dol_print_date($this->sellby,
'day');
615 $url = DOL_URL_ROOT.
'/product/stock/productlot_card.php?id='.$this->id;
617 if ($option !=
'nolink') {
619 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
620 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
621 $add_save_lastsearch_values = 1;
623 if ($add_save_lastsearch_values) {
624 $url .=
'&save_lastsearch_values=1';
629 if (empty($notooltip)) {
630 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
631 $label = $langs->trans(
"ShowMyObject");
632 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
634 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
635 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
637 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
640 if ($option ==
'nolink') {
641 $linkstart =
'<span';
643 $linkstart =
'<a href="'.$url.
'"';
645 $linkstart .= $linkclose.
'>';
646 if ($option ==
'nolink') {
647 $linkend =
'</span>';
652 $result .= $linkstart;
654 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
656 if ($withpicto != 2) {
657 $result .= $this->batch;
675 $this->entity =
null;
676 $this->fk_product =
null;
682 $this->fk_user_creat =
null;
683 $this->fk_user_modif =
null;
684 $this->import_key =
'';