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
22/**
23 * \file    product/stock/class/productlot.class.php
24 * \ingroup stock
25 * \brief   This is CRUD class file to manage table productlot (Create/Read/Update/Delete)
26 */
27
28// Put here all includes required by your class file
29require_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
33/**
34 * Class with list of lots and properties
35 */
36class Productlot extends CommonObject
37{
38	/**
39	 * @var string Id to identify managed objects
40	 */
41	public $element = 'productlot';
42
43	/**
44	 * @var string Name of table without prefix where object is stored
45	 */
46	public $table_element = 'product_lot';
47
48	/**
49	 * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
50	 */
51	public $picto = 'lot';
52
53	/**
54	 * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
55	 * @var int
56	 */
57	public $ismultientitymanaged = 1;
58
59
60	/**
61	 *  'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
62	 *         Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
63	 *  'label' the translation key.
64	 *  'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
65	 *  'position' is the sort order of field.
66	 *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
67	 *  'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
68	 *  'noteditable' says if field is not editable (1 or 0)
69	 *  'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
70	 *  'index' if we want an index in database.
71	 *  'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
72	 *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.
73	 *  'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
74	 *  'css' is the CSS style to use on field. For example: 'maxwidth200'
75	 *  'help' is a string visible as a tooltip on field
76	 *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record
77	 *  'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
78	 *  'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
79	 *  'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
80	 *  'comment' is not used. You can store here any text of your choice. It is not used by application.
81	 *
82	 *  Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
83	 */
84
85	/**
86	 * @var array  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
87	 */
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),
97		//'commissionning_date'        => array('type'=>'date', 'label'=>'FirstUseDate', 'enabled'=>'empty($conf->global->PRODUCT_ENABLE_TRACEABILITY)?0:1', 'visible'=>5, 'position'=>100),
98		//'qc_frequency'        => array('type'=>'varchar(6)', 'label'=>'QCFrequency', 'enabled'=>'empty($conf->global->PRODUCT_ENABLE_QUALITYCONTROL)?1:0', 'visible'=>5, 'position'=>110),
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),
105	);
106
107	/**
108	 * @var int Entity
109	 */
110	public $entity;
111
112	/**
113	 * @var int ID
114	 */
115	public $fk_product;
116
117	public $batch;
118	public $eatby = '';
119	public $sellby = '';
120	public $eol_date = '';
121	public $manufacturing_date = '';
122	public $scrapping_date = '';
123	//public $commissionning_date = '';
124	//public $qc_frequency = '';
125	public $datec = '';
126	public $tms = '';
127
128	/**
129	 * @var int ID
130	 */
131	public $fk_user_creat;
132
133	/**
134	 * @var int ID
135	 */
136	public $fk_user_modif;
137
138	public $import_key;
139
140
141	/**
142	 * Constructor
143	 *
144	 * @param DoliDb $db Database handler
145	 */
146	public function __construct(DoliDB $db)
147	{
148		$this->db = $db;
149	}
150
151	/**
152	 * Create object into database
153	 *
154	 * @param  User $user      User that creates
155	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
156	 *
157	 * @return int <0 if KO, Id of created object if OK
158	 */
159	public function create(User $user, $notrigger = false)
160	{
161		global $conf;
162		dol_syslog(__METHOD__, LOG_DEBUG);
163
164		$error = 0;
165
166		// Clean parameters
167
168		if (isset($this->entity)) {
169			 $this->entity = (int) $this->entity;
170		}
171		if (isset($this->fk_product)) {
172			 $this->fk_product = (int) $this->fk_product;
173		}
174		if (isset($this->batch)) {
175			 $this->batch = trim($this->batch);
176		}
177		if (isset($this->fk_user_creat)) {
178			 $this->fk_user_creat = (int) $this->fk_user_creat;
179		}
180		if (isset($this->fk_user_modif)) {
181			 $this->fk_user_modif = (int) $this->fk_user_modif;
182		}
183		if (isset($this->import_key)) {
184			 $this->import_key = trim($this->import_key);
185		}
186
187		// Check parameters
188		// Put here code to add control on parameters values
189
190		// Insert request
191		$sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
192		$sql .= 'entity,';
193		$sql .= 'fk_product,';
194		$sql .= 'batch,';
195		$sql .= 'eatby,';
196		$sql .= 'sellby,';
197		$sql .= 'eol_date,';
198		$sql .= 'manufacturing_date,';
199		$sql .= 'scrapping_date,';
200		//$sql .= 'commissionning_date,';
201		//$sql .= 'qc_frequency,';
202		$sql .= 'datec,';
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)."'").',';
215		//$sql .= ' '.(!isset($this->commissionning_date) || dol_strlen($this->commissionning_date) == 0 ? 'NULL' : "'".$this->db->idate($this->commissionning_date)."'").',';
216		//$sql .= ' '.(!isset($this->qc_frequency) ? 'NULL' : $this->qc_frequency).',';
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);
221		$sql .= ')';
222
223		$this->db->begin();
224
225		$resql = $this->db->query($sql);
226		if (!$resql) {
227			$error++;
228			$this->errors[] = 'Error '.$this->db->lasterror();
229			dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
230		}
231
232		if (!$error) {
233			$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
234
235			// Actions on extra fields
236			if (!$error) {
237				$result = $this->insertExtraFields();
238				if ($result < 0) {
239					$error++;
240				}
241			}
242
243			if (!$error && !$notrigger) {
244				// Uncomment this and change MYOBJECT to your own tag if you
245				// want this action to call a trigger.
246
247				// Call triggers
248				$result = $this->call_trigger('PRODUCTLOT_CREATE', $user);
249				if ($result < 0) {
250					$error++;
251				}
252				// End call triggers
253			}
254		}
255
256		// Commit or rollback
257		if ($error) {
258			$this->db->rollback();
259
260			return -1 * $error;
261		} else {
262			$this->db->commit();
263
264			return $this->id;
265		}
266	}
267
268	/**
269	 * Load object in memory from the database
270	 *
271	 * @param int    $id  			Id of lot/batch
272	 * @param int    $product_id  	Id of product, batch number parameter required
273	 * @param string $batch 		batch number
274	 *
275	 * @return int <0 if KO, 0 if not found, >0 if OK
276	 */
277	public function fetch($id = 0, $product_id = 0, $batch = '')
278	{
279		global $conf;
280		dol_syslog(__METHOD__, LOG_DEBUG);
281
282		$sql = 'SELECT';
283		$sql .= ' t.rowid,';
284		$sql .= " t.entity,";
285		$sql .= " t.fk_product,";
286		$sql .= " t.batch,";
287		$sql .= " t.eatby,";
288		$sql .= " t.sellby,";
289		$sql .= " t.eol_date,";
290		$sql .= " t.manufacturing_date,";
291		$sql .= " t.scrapping_date,";
292		//$sql .= " t.commissionning_date,";
293		//$sql .= " t.qc_frequency,";
294		$sql .= " t.datec,";
295		$sql .= " t.tms,";
296		$sql .= " t.fk_user_creat,";
297		$sql .= " t.fk_user_modif,";
298		$sql .= " t.import_key";
299		$sql .= ' FROM '.MAIN_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);
302		} else {
303			$sql .= ' WHERE t.rowid = '.((int) $id);
304		}
305
306		$resql = $this->db->query($sql);
307		if ($resql) {
308			$numrows = $this->db->num_rows($resql);
309			if ($numrows) {
310				$obj = $this->db->fetch_object($resql);
311
312				$this->id = $obj->rowid;
313				$this->ref = $obj->rowid;
314				//$this->ref = $obj->fk_product.'_'.$obj->batch;
315
316				$this->batch = $obj->batch;
317				$this->entity = (!empty($obj->entity) ? $obj->entity : $conf->entity); // Prevent "null" 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);
324				//$this->commissionning_date = $this->db->jdate($obj->commissionning_date);
325				//$this->qc_frequency = $obj->qc_frequency;
326
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;
332
333				// Retrieve all extrafield
334				// fetch optionals attributes and labels
335				$this->fetch_optionals();
336			}
337			$this->db->free($resql);
338
339			if ($numrows) {
340				return 1;
341			} else {
342				return 0;
343			}
344		} else {
345			$this->errors[] = 'Error '.$this->db->lasterror();
346			dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
347
348			return -1;
349		}
350	}
351
352	/**
353	 * Update object into database
354	 *
355	 * @param  User $user      User that modifies
356	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
357	 *
358	 * @return int <0 if KO, >0 if OK
359	 */
360	public function update(User $user, $notrigger = false)
361	{
362		$error = 0;
363
364		dol_syslog(__METHOD__, LOG_DEBUG);
365
366		// Clean parameters
367
368		if (isset($this->entity)) {
369			 $this->entity = (int) $this->entity;
370		}
371		if (isset($this->fk_product)) {
372			 $this->fk_product = (int) $this->fk_product;
373		}
374		if (isset($this->batch)) {
375			 $this->batch = trim($this->batch);
376		}
377		if (isset($this->fk_user_creat)) {
378			 $this->fk_user_creat = (int) $this->fk_user_creat;
379		}
380		if (isset($this->fk_user_modif)) {
381			 $this->fk_user_modif = (int) $this->fk_user_modif;
382		}
383		if (isset($this->import_key)) {
384			 $this->import_key = trim($this->import_key);
385		}
386
387		// Check parameters
388		// Put here code to add a control on parameters values
389
390		if (empty($this->oldcopy)) {
391			$org = new self($this->db);
392			$org->fetch($this->id);
393			$this->oldcopy = $org;
394		}
395
396		// Update request
397		$sql = 'UPDATE '.MAIN_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').',';
406		//$sql .= ' commissionning_date = '.(!isset($this->first_use_date) || dol_strlen($this->first_use_date) != 0 ? "'".$this->db->idate($this->first_use_date)."'" : 'null').',';
407		//$sql .= ' qc_frequency = '.(!isset($this->qc_frequency) || dol_strlen($this->qc_frequency) != 0 ? "'".$this->db->escape($this->qc_frequency)."'" : '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);
414
415		$this->db->begin();
416
417		$resql = $this->db->query($sql);
418		if (!$resql) {
419			$error++;
420			$this->errors[] = 'Error '.$this->db->lasterror();
421			dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
422		}
423
424		// Actions on extra fields
425		if (!$error) {
426			$result = $this->insertExtraFields();
427			if ($result < 0) {
428				$error++;
429			}
430		}
431
432		if (!$error && !$notrigger) {
433			// Call triggers
434			$result = $this->call_trigger('PRODUCTLOT_MODIFY', $user);
435			if ($result < 0) {
436				$error++;
437			}
438			// End call triggers
439		}
440
441		// Commit or rollback
442		if ($error) {
443			$this->db->rollback();
444
445			return -1 * $error;
446		} else {
447			$this->db->commit();
448
449			return 1;
450		}
451	}
452
453	/**
454	 * Delete object in database
455	 *
456	 * @param User $user      User that deletes
457	 * @param bool $notrigger false=launch triggers after, true=disable triggers
458	 *
459	 * @return int <0 if KO, >0 if OK
460	 */
461	public function delete(User $user, $notrigger = false)
462	{
463		dol_syslog(__METHOD__, LOG_DEBUG);
464
465		$error = 0;
466
467		$this->db->begin();
468
469		//if (!$error) {
470			//if (!$notrigger) {
471				// Uncomment this and change MYOBJECT to your own tag if you
472				// want this action calls a trigger.
473
474				//// Call triggers
475				//$result=$this->call_trigger('MYOBJECT_DELETE',$user);
476				//if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
477				//// End call triggers
478			//}
479		//}
480
481		if (!$error) {
482			$sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
483			$sql .= ' WHERE rowid='.((int) $this->id);
484
485			$resql = $this->db->query($sql);
486			if (!$resql) {
487				$error++;
488				$this->errors[] = 'Error '.$this->db->lasterror();
489				dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
490			}
491		}
492
493		// Commit or rollback
494		if ($error) {
495			$this->db->rollback();
496
497			return -1 * $error;
498		} else {
499			$this->db->commit();
500
501			return 1;
502		}
503	}
504
505	/**
506	 * Load an object from its id and create a new one in database
507	 *
508	 * @param	User	$user		User making the clone
509	 * @param   int     $fromid     Id of object to clone
510	 * @return  int                 New id of clone
511	 */
512	public function createFromClone(User $user, $fromid)
513	{
514		dol_syslog(__METHOD__, LOG_DEBUG);
515
516		$error = 0;
517		$object = new Productlot($this->db);
518
519		$this->db->begin();
520
521		// Load source object
522		$object->fetch($fromid);
523		// Reset object
524		$object->id = 0;
525
526		// Clear fields
527		// ...
528
529		// Create clone
530		$object->context['createfromclone'] = 'createfromclone';
531		$result = $object->create($user);
532
533		// Other options
534		if ($result < 0) {
535			$error++;
536			$this->errors = $object->errors;
537			dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
538		}
539
540		unset($object->context['createfromclone']);
541
542		// End
543		if (!$error) {
544			$this->db->commit();
545
546			return $object->id;
547		} else {
548			$this->db->rollback();
549
550			return -1;
551		}
552	}
553
554
555	/**
556	 *	Return label of status of object
557	 *
558	 *	@param      int		$mode       0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
559	 *	@return     string      		Label of status
560	 */
561	public function getLibStatut($mode = 0)
562	{
563		return $this->LibStatut(0, $mode);
564	}
565
566	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
567	/**
568	 *	Return label of a given status
569	 *
570	 *	@param	int		$status     Status
571	 *	@param  int		$mode       0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
572	 *	@return string      		Label of status
573	 */
574	public function LibStatut($status, $mode = 0)
575	{
576		// phpcs:enable
577		global $langs;
578
579		//$langs->load('stocks');
580
581		return '';
582	}
583
584
585	/**
586	 *  Return a link to the a lot card (with optionaly the picto)
587	 * 	Use this->id,this->lastname, this->firstname
588	 *
589	 *	@param	int		$withpicto				Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
590	 *	@param	string	$option					On what the link point to
591	 *  @param	integer	$notooltip				1=Disable tooltip
592	 *  @param	int		$maxlen					Max length of visible user name
593	 *  @param  string  $morecss            	Add more css on link
594	 *  @param  int     $save_lastsearch_value	-1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
595	 *	@return	string							String with URL
596	 */
597	public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '', $save_lastsearch_value = -1)
598	{
599		global $langs, $conf, $db;
600		global $dolibarr_main_authentication, $dolibarr_main_demo;
601		global $menumanager;
602
603		$result = '';
604
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');
610		}
611		if ($this->sellby && empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
612			$label .= '<br><b>'.$langs->trans('SellByDate').':</b> '.dol_print_date($this->sellby, 'day');
613		}
614
615		$url = DOL_URL_ROOT.'/product/stock/productlot_card.php?id='.$this->id;
616
617		if ($option != 'nolink') {
618			// Add param to save lastsearch_values or not
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;
622			}
623			if ($add_save_lastsearch_values) {
624				$url .= '&save_lastsearch_values=1';
625			}
626		}
627
628		$linkclose = '';
629		if (empty($notooltip)) {
630			if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
631				$label = $langs->trans("ShowMyObject");
632				$linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
633			}
634			$linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
635			$linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
636		} else {
637			$linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
638		}
639
640		if ($option == 'nolink') {
641			$linkstart = '<span';
642		} else {
643			$linkstart = '<a href="'.$url.'"';
644		}
645		$linkstart .= $linkclose.'>';
646		if ($option == 'nolink') {
647			$linkend = '</span>';
648		} else {
649			$linkend = '</a>';
650		}
651
652		$result .= $linkstart;
653		if ($withpicto) {
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);
655		}
656		if ($withpicto != 2) {
657			$result .= $this->batch;
658		}
659		$result .= $linkend;
660
661		return $result;
662	}
663
664
665	/**
666	 * Initialise object with example values
667	 * Id must be 0 if object instance is a specimen
668	 *
669	 * @return void
670	 */
671	public function initAsSpecimen()
672	{
673		$this->id = 0;
674
675		$this->entity = null;
676		$this->fk_product = null;
677		$this->batch = '';
678		$this->eatby = '';
679		$this->sellby = '';
680		$this->datec = '';
681		$this->tms = '';
682		$this->fk_user_creat = null;
683		$this->fk_user_modif = null;
684		$this->import_key = '';
685	}
686}
687