dolibarr  13.0.2
productlot.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
5  * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
6  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
28 // Put here all includes required by your class file
29 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
30 //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
31 //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
32 
36 class Productlot extends CommonObject
37 {
41  public $element = 'productlot';
42 
46  public $table_element = 'product_lot';
47 
51  public $picto = 'lot';
52 
57  public $ismultientitymanaged = 1;
58 
59 
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'=>15, '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  'eatby' => array('type'=>'date', 'label'=>'EatByDate', 'enabled'=>'empty($conf->global->PRODUCT_DISABLE_EATBY)?1:0', 'visible'=>5, 'position'=>62),
95  'datec' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>500),
96  'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>501),
97  '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'),
98  'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511)
99  );
100 
104  public $entity;
105 
109  public $fk_product;
110 
111  public $batch;
112  public $eatby = '';
113  public $sellby = '';
114  public $datec = '';
115  public $tms = '';
116 
120  public $fk_user_creat;
121 
125  public $fk_user_modif;
126 
127  public $import_key;
128 
129 
135  public function __construct(DoliDB $db)
136  {
137  $this->db = $db;
138  }
139 
148  public function create(User $user, $notrigger = false)
149  {
150  global $conf;
151  dol_syslog(__METHOD__, LOG_DEBUG);
152 
153  $error = 0;
154 
155  // Clean parameters
156 
157  if (isset($this->entity)) {
158  $this->entity = (int) $this->entity;
159  }
160  if (isset($this->fk_product)) {
161  $this->fk_product = (int) $this->fk_product;
162  }
163  if (isset($this->batch)) {
164  $this->batch = trim($this->batch);
165  }
166  if (isset($this->fk_user_creat)) {
167  $this->fk_user_creat = (int) $this->fk_user_creat;
168  }
169  if (isset($this->fk_user_modif)) {
170  $this->fk_user_modif = (int) $this->fk_user_modif;
171  }
172  if (isset($this->import_key)) {
173  $this->import_key = trim($this->import_key);
174  }
175 
176 
177 
178  // Check parameters
179  // Put here code to add control on parameters values
180 
181  // Insert request
182  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
183  $sql .= 'entity,';
184  $sql .= 'fk_product,';
185  $sql .= 'batch,';
186  $sql .= 'eatby,';
187  $sql .= 'sellby,';
188  $sql .= 'datec,';
189  $sql .= 'fk_user_creat,';
190  $sql .= 'fk_user_modif,';
191  $sql .= 'import_key';
192  $sql .= ') VALUES (';
193  $sql .= ' '.(!isset($this->entity) ? $conf->entity : $this->entity).',';
194  $sql .= ' '.(!isset($this->fk_product) ? 'NULL' : $this->fk_product).',';
195  $sql .= ' '.(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").',';
196  $sql .= ' '.(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").',';
197  $sql .= ' '.(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").',';
198  $sql .= ' '."'".$this->db->idate(dol_now())."'".',';
199  $sql .= ' '.(!isset($this->fk_user_creat) ? 'NULL' : $this->fk_user_creat).',';
200  $sql .= ' '.(!isset($this->fk_user_modif) ? 'NULL' : $this->fk_user_modif).',';
201  $sql .= ' '.(!isset($this->import_key) ? 'NULL' : $this->import_key);
202  $sql .= ')';
203 
204  $this->db->begin();
205 
206  $resql = $this->db->query($sql);
207  if (!$resql) {
208  $error++;
209  $this->errors[] = 'Error '.$this->db->lasterror();
210  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
211  }
212 
213  if (!$error) {
214  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
215 
216  // Actions on extra fields
217  if (!$error)
218  {
219  $result = $this->insertExtraFields();
220  if ($result < 0)
221  {
222  $error++;
223  }
224  }
225 
226  if (!$error && !$notrigger) {
227  // Uncomment this and change MYOBJECT to your own tag if you
228  // want this action to call a trigger.
229 
230  // Call triggers
231  $result = $this->call_trigger('PRODUCTLOT_CREATE', $user);
232  if ($result < 0) $error++;
233  // End call triggers
234  }
235  }
236 
237  // Commit or rollback
238  if ($error) {
239  $this->db->rollback();
240 
241  return -1 * $error;
242  } else {
243  $this->db->commit();
244 
245  return $this->id;
246  }
247  }
248 
258  public function fetch($id = 0, $product_id = 0, $batch = '')
259  {
260  global $conf;
261  dol_syslog(__METHOD__, LOG_DEBUG);
262 
263  $sql = 'SELECT';
264  $sql .= ' t.rowid,';
265  $sql .= " t.entity,";
266  $sql .= " t.fk_product,";
267  $sql .= " t.batch,";
268  $sql .= " t.eatby,";
269  $sql .= " t.sellby,";
270  $sql .= " t.datec,";
271  $sql .= " t.tms,";
272  $sql .= " t.fk_user_creat,";
273  $sql .= " t.fk_user_modif,";
274  $sql .= " t.import_key";
275  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
276  if ($product_id > 0 && $batch != '') {
277  $sql .= " WHERE t.batch = '".$this->db->escape($batch)."' AND t.fk_product = ".$product_id;
278  } else {
279  $sql .= ' WHERE t.rowid = '.$id;
280  }
281 
282  $resql = $this->db->query($sql);
283  if ($resql) {
284  $numrows = $this->db->num_rows($resql);
285  if ($numrows) {
286  $obj = $this->db->fetch_object($resql);
287 
288  $this->id = $obj->rowid;
289  $this->ref = $obj->rowid;
290  //$this->ref = $obj->fk_product.'_'.$obj->batch;
291 
292  $this->batch = $obj->batch;
293  $this->entity = (!empty($obj->entity) ? $obj->entity : $conf->entity); // Prevent "null" entity
294  $this->fk_product = $obj->fk_product;
295  $this->eatby = $this->db->jdate($obj->eatby);
296  $this->sellby = $this->db->jdate($obj->sellby);
297  $this->datec = $this->db->jdate($obj->datec);
298  $this->tms = $this->db->jdate($obj->tms);
299  $this->fk_user_creat = $obj->fk_user_creat;
300  $this->fk_user_modif = $obj->fk_user_modif;
301  $this->import_key = $obj->import_key;
302 
303  // Retrieve all extrafield
304  // fetch optionals attributes and labels
305  $this->fetch_optionals();
306  }
307  $this->db->free($resql);
308 
309  if ($numrows) {
310  return 1;
311  } else {
312  return 0;
313  }
314  } else {
315  $this->errors[] = 'Error '.$this->db->lasterror();
316  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
317 
318  return -1;
319  }
320  }
321 
330  public function update(User $user, $notrigger = false)
331  {
332  $error = 0;
333 
334  dol_syslog(__METHOD__, LOG_DEBUG);
335 
336  // Clean parameters
337 
338  if (isset($this->entity)) {
339  $this->entity = (int) $this->entity;
340  }
341  if (isset($this->fk_product)) {
342  $this->fk_product = (int) $this->fk_product;
343  }
344  if (isset($this->batch)) {
345  $this->batch = trim($this->batch);
346  }
347  if (isset($this->fk_user_creat)) {
348  $this->fk_user_creat = (int) $this->fk_user_creat;
349  }
350  if (isset($this->fk_user_modif)) {
351  $this->fk_user_modif = (int) $this->fk_user_modif;
352  }
353  if (isset($this->import_key)) {
354  $this->import_key = trim($this->import_key);
355  }
356 
357  // Check parameters
358  // Put here code to add a control on parameters values
359 
360  if (empty($this->oldcopy))
361  {
362  $org = new self($this->db);
363  $org->fetch($this->id);
364  $this->oldcopy = $org;
365  }
366 
367  // Update request
368  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
369  $sql .= ' entity = '.(isset($this->entity) ? $this->entity : "null").',';
370  $sql .= ' fk_product = '.(isset($this->fk_product) ? $this->fk_product : "null").',';
371  $sql .= ' batch = '.(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").',';
372  $sql .= ' eatby = '.(!isset($this->eatby) || dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').',';
373  $sql .= ' sellby = '.(!isset($this->sellby) || dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').',';
374  $sql .= ' datec = '.(!isset($this->datec) || dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').',';
375  $sql .= ' tms = '.(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : "'".$this->db->idate(dol_now())."'").',';
376  $sql .= ' fk_user_creat = '.(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").',';
377  $sql .= ' fk_user_modif = '.(isset($this->fk_user_modif) ? $this->fk_user_modif : "null").',';
378  $sql .= ' import_key = '.(isset($this->import_key) ? $this->import_key : "null");
379  $sql .= ' WHERE rowid='.$this->id;
380 
381  $this->db->begin();
382 
383  $resql = $this->db->query($sql);
384  if (!$resql) {
385  $error++;
386  $this->errors[] = 'Error '.$this->db->lasterror();
387  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
388  }
389 
390  // Actions on extra fields
391  if (!$error)
392  {
393  $result = $this->insertExtraFields();
394  if ($result < 0)
395  {
396  $error++;
397  }
398  }
399 
400  if (!$error && !$notrigger) {
401  // Call triggers
402  $result = $this->call_trigger('PRODUCTLOT_MODIFY', $user);
403  if ($result < 0) { $error++; }
404  // End call triggers
405  }
406 
407  // Commit or rollback
408  if ($error) {
409  $this->db->rollback();
410 
411  return -1 * $error;
412  } else {
413  $this->db->commit();
414 
415  return 1;
416  }
417  }
418 
427  public function delete(User $user, $notrigger = false)
428  {
429  dol_syslog(__METHOD__, LOG_DEBUG);
430 
431  $error = 0;
432 
433  $this->db->begin();
434 
435  //if (!$error) {
436  //if (!$notrigger) {
437  // Uncomment this and change MYOBJECT to your own tag if you
438  // want this action calls a trigger.
439 
441  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
442  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
444  //}
445  //}
446 
447  if (!$error) {
448  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
449  $sql .= ' WHERE rowid='.$this->id;
450 
451  $resql = $this->db->query($sql);
452  if (!$resql) {
453  $error++;
454  $this->errors[] = 'Error '.$this->db->lasterror();
455  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
456  }
457  }
458 
459  // Commit or rollback
460  if ($error) {
461  $this->db->rollback();
462 
463  return -1 * $error;
464  } else {
465  $this->db->commit();
466 
467  return 1;
468  }
469  }
470 
478  public function createFromClone(User $user, $fromid)
479  {
480  dol_syslog(__METHOD__, LOG_DEBUG);
481 
482  $error = 0;
483  $object = new Productlot($this->db);
484 
485  $this->db->begin();
486 
487  // Load source object
488  $object->fetch($fromid);
489  // Reset object
490  $object->id = 0;
491 
492  // Clear fields
493  // ...
494 
495  // Create clone
496  $object->context['createfromclone'] = 'createfromclone';
497  $result = $object->create($user);
498 
499  // Other options
500  if ($result < 0) {
501  $error++;
502  $this->errors = $object->errors;
503  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
504  }
505 
506  unset($object->context['createfromclone']);
507 
508  // End
509  if (!$error) {
510  $this->db->commit();
511 
512  return $object->id;
513  } else {
514  $this->db->rollback();
515 
516  return -1;
517  }
518  }
519 
520 
527  public function getLibStatut($mode = 0)
528  {
529  return $this->LibStatut(0, $mode);
530  }
531 
532  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
540  public function LibStatut($status, $mode = 0)
541  {
542  // phpcs:enable
543  global $langs;
544 
545  //$langs->load('stocks');
546 
547  return '';
548  }
549 
550 
563  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '', $save_lastsearch_value = -1)
564  {
565  global $langs, $conf, $db;
566  global $dolibarr_main_authentication, $dolibarr_main_demo;
567  global $menumanager;
568 
569  $result = '';
570 
571  $label = '<u>'.$langs->trans("Batch").'</u>';
572  $label .= '<div width="100%">';
573  $label .= '<b>'.$langs->trans('Batch').':</b> '.$this->batch;
574  if ($this->eatby)
575  {
576  $label .= '<br><b>'.$langs->trans('EatByDate').':</b> '.dol_print_date($this->eatby, 'day');
577  }
578  if ($this->sellby)
579  {
580  $label .= '<br><b>'.$langs->trans('SellByDate').':</b> '.dol_print_date($this->sellby, 'day');
581  }
582 
583  $url = DOL_URL_ROOT.'/product/stock/productlot_card.php?id='.$this->id;
584 
585  if ($option != 'nolink')
586  {
587  // Add param to save lastsearch_values or not
588  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
589  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
590  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
591  }
592 
593  $linkclose = '';
594  if (empty($notooltip))
595  {
596  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
597  {
598  $label = $langs->trans("ShowMyObject");
599  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
600  }
601  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
602  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
603  } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
604 
605  $linkstart = '<a href="'.$url.'"';
606  $linkstart .= $linkclose.'>';
607  $linkend = '</a>';
608 
609  $result .= $linkstart;
610  if ($withpicto) $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);
611  if ($withpicto != 2) $result .= $this->batch;
612  $result .= $linkend;
613 
614  return $result;
615  }
616 
617 
624  public function initAsSpecimen()
625  {
626  $this->id = 0;
627 
628  $this->entity = null;
629  $this->fk_product = null;
630  $this->batch = '';
631  $this->eatby = '';
632  $this->sellby = '';
633  $this->datec = '';
634  $this->tms = '';
635  $this->fk_user_creat = null;
636  $this->fk_user_modif = null;
637  $this->import_key = '';
638  }
639 }
create(User $user, $notrigger=false)
Create object into database.
Class with list of lots and properties.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
LibStatut($status, $mode=0)
Return label of a given status.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
fetch($id=0, $product_id=0, $batch= '')
Load object in memory from the database.
update(User $user, $notrigger=false)
Update object into database.
$conf db
API class for accounts.
Definition: inc.php:54
getNomUrl($withpicto=0, $option= '', $notooltip=0, $maxlen=24, $morecss= '', $save_lastsearch_value=-1)
Return a link to the a lot card (with optionaly the picto) Use this-&gt;id,this-&gt;lastname, this-&gt;firstname.
insertExtraFields($trigger= '', $userused=null)
Add/Update all extra fields values for the current object.
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.
getLibStatut($mode=0)
Return label of status of object.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this-&gt;array_options This method is in most cases call...
__construct(DoliDB $db)
Constructor.
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
call_trigger($triggerName, $user)
Call trigger based on this instance.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)