Passed
Branch develop (049034)
by
unknown
31:10
created

BOM::update()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 5
rs 10
1
<?php
2
/* Copyright (C) 2019  Laurent Destailleur <[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 * \file        bom/class/bom.class.php
20
 * \ingroup     bom
21
 * \brief       This file is a CRUD class file for BOM (Create/Read/Update/Delete)
22
 */
23
24
// Put here all includes required by your class file
25
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
27
//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
28
29
/**
30
 * Class for BOM
31
 */
32
class BOM extends CommonObject
33
{
34
	/**
35
	 * @var string ID to identify managed object
36
	 */
37
	public $element = 'bom';
38
39
	/**
40
	 * @var string Name of table without prefix where object is stored
41
	 */
42
	public $table_element = 'bom_bom';
43
44
	/**
45
	 * @var int  Does bom support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
46
	 */
47
	public $ismultientitymanaged = 1;
48
49
	/**
50
	 * @var int  Does object support extrafields ? 0=No, 1=Yes
51
	 */
52
	public $isextrafieldmanaged = 1;
53
54
	/**
55
	 * @var string String with name of icon for bom. Must be the part after the 'object_' into object_bom.png
56
	 */
57
	public $picto = 'bom';
58
59
60
	const STATUS_DRAFT = 0;
61
	const STATUS_VALIDATED = 1;
62
	const STATUS_CANCELED = 9;
63
64
65
	/**
66
	 *  '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')
67
	 *         Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
68
	 *  'label' the translation key.
69
	 *  'enabled' is a condition when the field must be managed.
70
	 *  '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)
71
	 *  'noteditable' says if field is not editable (1 or 0)
72
	 *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
73
	 *  'default' is a default value for creation (can still be replaced by the global setup of default values)
74
	 *  'index' if we want an index in database.
75
	 *  'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
76
	 *  'position' is the sort order of field.
77
	 *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.
78
	 *  '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).
79
	 *  'css' is the CSS style to use on field. For example: 'maxwidth200'
80
	 *  'help' is a string visible as a tooltip on field
81
	 *  'comment' is not used. You can store here any text of your choice. It is not used by application.
82
	 *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record
83
	 *  'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
84
	 */
85
86
	// BEGIN MODULEBUILDER PROPERTIES
87
	/**
88
	 * @var array  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
89
	 */
90
	public $fields = array(
91
		'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",),
92
		'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>5),
93
		'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'noteditable'=>1, 'visible'=>4, 'position'=>10, 'notnull'=>1, 'default'=>'(PROV)', 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of BOM", 'showoncombobox'=>'1',),
94
		'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'notnull'=>1, 'searchall'=>1, 'showoncombobox'=>'1', 'autofocusoncreate'=>1),
95
		'description' => array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>60, 'notnull'=>-1,),
96
		'fk_product' => array('type'=>'integer:Product:product/class/product.class.php:1:(finished IS NULL or finished <> 0)', 'label'=>'Product', 'enabled'=>1, 'visible'=>1, 'position'=>35, 'notnull'=>1, 'index'=>1, 'help'=>'ProductBOMHelp'),
97
	    'qty' => array('type'=>'real', 'label'=>'Quantity', 'enabled'=>1, 'visible'=>1, 'default'=>1, 'position'=>55, 'notnull'=>1, 'isameasure'=>'1', 'css'=>'maxwidth75imp'),
98
		//'efficiency' => array('type'=>'real', 'label'=>'ManufacturingEfficiency', 'enabled'=>1, 'visible'=>-1, 'default'=>1, 'position'=>100, 'notnull'=>0, 'css'=>'maxwidth50imp', 'help'=>'ValueOfMeansLossForProductProduced'),
99
		'duration' => array('type'=>'duration', 'label'=>'EstimatedDuration', 'enabled'=>1, 'visible'=>-1, 'position'=>101, 'notnull'=>-1, 'css'=>'maxwidth50imp', 'help'=>'EstimatedDurationDesc'),
100
		'fk_warehouse' => array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php:0', 'label'=>'WarehouseForProduction', 'enabled'=>1, 'visible'=>-1, 'position'=>102),
101
		'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>-2, 'position'=>161, 'notnull'=>-1,),
102
		'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>-2, 'position'=>162, 'notnull'=>-1,),
103
		'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>300, 'notnull'=>1,),
104
		'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'position'=>501, 'notnull'=>1,),
105
		'date_valid' => array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-2, 'position'=>502, 'notnull'=>0,),
106
		'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>510, 'notnull'=>1, 'foreignkey'=>'user.rowid',),
107
		'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'position'=>511, 'notnull'=>-1,),
108
		'fk_user_valid' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-2, 'position'=>512, 'notnull'=>0,),
109
		'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000, 'notnull'=>-1,),
110
		'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>1010),
111
		'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>2, 'position'=>1000, 'notnull'=>1, 'default'=>0, 'index'=>1, 'arrayofkeyval'=>array(0=>'Draft', 1=>'Enabled', 9=>'Disabled')),
112
	);
113
	public $rowid;
114
	public $ref;
115
	public $label;
116
	public $description;
117
	public $note_public;
118
	public $note_private;
119
120
	/**
121
     * @var integer|string date_creation
122
     */
123
	public $date_creation;
124
125
126
	public $tms;
127
	public $fk_user_creat;
128
	public $fk_user_modif;
129
	public $import_key;
130
	public $status;
131
	public $fk_product;
132
	public $qty;
133
	public $efficiency;
134
	// END MODULEBUILDER PROPERTIES
135
136
137
	// If this object has a subtable with lines
138
139
	/**
140
	 * @var int    Name of subtable line
141
	 */
142
	public $table_element_line = 'bom_bomline';
143
144
	/**
145
	 * @var int    Field with ID of parent key if this field has a parent
146
	 */
147
	public $fk_element = 'fk_bom';
148
149
	/**
150
	 * @var int    Name of subtable class that manage subtable lines
151
	 */
152
	public $class_element_line = 'BOMLine';
153
154
	/**
155
	 * @var array	List of child tables. To test if we can delete object.
156
	 */
157
	//protected $childtables=array();
158
159
	/**
160
	 * @var array	List of child tables. To know object to delete on cascade.
161
	 */
162
	protected $childtablesoncascade = array('bom_bomline');
163
164
	/**
165
	 * @var BOMLine[]     Array of subtable lines
166
	 */
167
	public $lines = array();
168
169
	/**
170
	 * @var int		Calculated cost for the BOM
171
	 */
172
	public $total_cost = 0;
173
174
	/**
175
	 * @var int		Calculated cost for 1 unit of the product in BOM
176
	 */
177
	public $unit_cost = 0;
178
179
180
181
	/**
182
	 * Constructor
183
	 *
184
	 * @param DoliDb $db Database handler
185
	 */
186
	public function __construct(DoliDB $db)
187
	{
188
		global $conf, $langs;
189
190
		$this->db = $db;
191
192
		if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible'] = 0;
193
		if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) $this->fields['entity']['enabled'] = 0;
194
195
		// Unset fields that are disabled
196
		foreach ($this->fields as $key => $val)
197
		{
198
			if (isset($val['enabled']) && empty($val['enabled']))
199
			{
200
				unset($this->fields[$key]);
201
			}
202
		}
203
204
		// Translate some data of arrayofkeyval
205
		foreach ($this->fields as $key => $val)
206
		{
207
			if (is_array($val['arrayofkeyval']))
208
			{
209
				foreach ($val['arrayofkeyval'] as $key2 => $val2)
210
				{
211
					$this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
212
				}
213
			}
214
		}
215
	}
216
217
	/**
218
	 * Create object into database
219
	 *
220
	 * @param  User $user      User that creates
221
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
222
	 * @return int             <0 if KO, Id of created object if OK
223
	 */
224
	public function create(User $user, $notrigger = false)
225
	{
226
		if ($this->efficiency <= 0 || $this->efficiency > 1) $this->efficiency = 1;
227
228
		return $this->createCommon($user, $notrigger);
229
	}
230
231
	/**
232
	 * Clone an object into another one
233
	 *
234
	 * @param  	User 	$user      	User that creates
235
	 * @param  	int 	$fromid     Id of object to clone
236
	 * @return 	mixed 				New object created, <0 if KO
237
	 */
238
	public function createFromClone(User $user, $fromid)
239
	{
240
		global $langs, $hookmanager, $extrafields;
241
	    $error = 0;
242
243
	    dol_syslog(__METHOD__, LOG_DEBUG);
244
245
	    $object = new self($this->db);
246
247
	    $this->db->begin();
248
249
	    // Load source object
250
	    $result = $object->fetchCommon($fromid);
251
	    if ($result > 0 && !empty($object->table_element_line)) $object->fetchLines();
252
253
	    // Get lines so they will be clone
254
	    //foreach ($object->lines as $line)
255
	    //	$line->fetch_optionals();
256
257
	    // Reset some properties
258
	    unset($object->id);
259
	    unset($object->fk_user_creat);
260
	    unset($object->import_key);
261
262
	    // Clear fields
263
	    $object->ref = empty($this->fields['ref']['default']) ? $langs->trans("copy_of_").$object->ref : $this->fields['ref']['default'];
264
	    $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default'];
265
	    $object->status = self::STATUS_DRAFT;
266
	    // ...
267
	    // Clear extrafields that are unique
268
	    if (is_array($object->array_options) && count($object->array_options) > 0)
269
	    {
270
	    	$extrafields->fetch_name_optionals_label($object->table_element);
271
	    	foreach ($object->array_options as $key => $option)
272
	    	{
273
	    		$shortkey = preg_replace('/options_/', '', $key);
274
	    		if (!empty($extrafields->attributes[$this->element]['unique'][$shortkey]))
275
	    		{
276
	    			//var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
277
	    			unset($object->array_options[$key]);
278
	    		}
279
	    	}
280
	    }
281
282
	    // Create clone
283
		$object->context['createfromclone'] = 'createfromclone';
284
	    $result = $object->createCommon($user);
285
	    if ($result < 0) {
286
	        $error++;
287
	        $this->error = $object->error;
288
	        $this->errors = $object->errors;
289
	    }
290
291
	    if (!$error)
292
	    {
293
	    	// copy internal contacts
294
	    	if ($this->copy_linked_contact($object, 'internal') < 0)
295
	    	{
296
	    		$error++;
297
	    	}
298
	    }
299
300
	    if (!$error)
301
	    {
302
	    	// copy external contacts if same company
303
	    	if (property_exists($this, 'socid') && $this->socid == $object->socid)
304
	    	{
305
	    		if ($this->copy_linked_contact($object, 'external') < 0)
306
	    			$error++;
307
	    	}
308
	    }
309
310
	    // If there is lines, create lines too
311
312
313
314
	    unset($object->context['createfromclone']);
315
316
	    // End
317
	    if (!$error) {
318
	        $this->db->commit();
319
	        return $object;
320
	    } else {
321
	        $this->db->rollback();
322
	        return -1;
323
	    }
324
	}
325
326
	/**
327
	 * Load object in memory from the database
328
	 *
329
	 * @param int    $id   Id object
330
	 * @param string $ref  Ref
331
	 * @return int         <0 if KO, 0 if not found, >0 if OK
332
	 */
333
	public function fetch($id, $ref = null)
334
	{
335
		$result = $this->fetchCommon($id, $ref);
336
337
		if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
338
		$this->calculateCosts();
339
340
		return $result;
341
	}
342
343
	/**
344
	 * Load object lines in memory from the database
345
	 *
346
	 * @return int         <0 if KO, 0 if not found, >0 if OK
347
	 */
348
	public function fetchLines()
349
	{
350
		$this->lines = array();
351
352
		$result = $this->fetchLinesCommon();
353
		return $result;
354
	}
355
356
	/**
357
	 * Load list of objects in memory from the database.
358
	 *
359
	 * @param  string      $sortorder    Sort Order
360
	 * @param  string      $sortfield    Sort field
361
	 * @param  int         $limit        limit
362
	 * @param  int         $offset       Offset
363
	 * @param  array       $filter       Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
364
	 * @param  string      $filtermode   Filter mode (AND or OR)
365
	 * @return array|int                 int <0 if KO, array of pages if OK
366
	 */
367
	public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
368
	{
369
		global $conf;
370
371
		dol_syslog(__METHOD__, LOG_DEBUG);
372
373
		$records = array();
374
375
		$sql = 'SELECT ';
376
		$sql .= $this->getFieldList();
377
		$sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
378
		if ($this->ismultientitymanaged) $sql .= ' WHERE t.entity IN ('.getEntity($this->table_element).')';
379
		else $sql .= ' WHERE 1 = 1';
380
		// Manage filter
381
		$sqlwhere = array();
382
		if (count($filter) > 0) {
383
			foreach ($filter as $key => $value) {
384
				if ($key == 't.rowid') {
385
					$sqlwhere[] = $key.'='.$value;
386
				} elseif (strpos($key, 'date') !== false) {
387
					$sqlwhere[] = $key.' = \''.$this->db->idate($value).'\'';
388
				} elseif ($key == 'customsql') {
389
					$sqlwhere[] = $value;
390
				} else {
391
					$sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
392
				}
393
			}
394
		}
395
		if (count($sqlwhere) > 0) {
396
			$sql .= ' AND ('.implode(' '.$filtermode.' ', $sqlwhere).')';
397
		}
398
399
		if (!empty($sortfield)) {
400
			$sql .= $this->db->order($sortfield, $sortorder);
401
		}
402
		if (!empty($limit)) {
403
			$sql .= ' '.$this->db->plimit($limit, $offset);
404
		}
405
406
		$resql = $this->db->query($sql);
407
		if ($resql) {
408
			$num = $this->db->num_rows($resql);
409
410
			while ($obj = $this->db->fetch_object($resql))
411
			{
412
				$record = new self($this->db);
413
				$record->setVarsFromFetchObj($obj);
414
415
				$records[$record->id] = $record;
416
			}
417
			$this->db->free($resql);
418
419
			return $records;
420
		} else {
421
			$this->errors[] = 'Error '.$this->db->lasterror();
422
			dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
423
424
			return -1;
425
		}
426
	}
427
428
	/**
429
	 * Update object into database
430
	 *
431
	 * @param  User $user      User that modifies
432
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
433
	 * @return int             <0 if KO, >0 if OK
434
	 */
435
	public function update(User $user, $notrigger = false)
436
	{
437
		if ($this->efficiency <= 0 || $this->efficiency > 1) $this->efficiency = 1;
438
439
		return $this->updateCommon($user, $notrigger);
440
	}
441
442
	/**
443
	 * Delete object in database
444
	 *
445
	 * @param User $user       User that deletes
446
	 * @param bool $notrigger  false=launch triggers after, true=disable triggers
447
	 * @return int             <0 if KO, >0 if OK
448
	 */
449
	public function delete(User $user, $notrigger = false)
450
	{
451
		return $this->deleteCommon($user, $notrigger);
452
		//return $this->deleteCommon($user, $notrigger, 1);
453
	}
454
455
	/**
456
	 *  Delete a line of object in database
457
	 *
458
	 *	@param  User	$user       User that delete
459
	 *  @param	int		$idline		Id of line to delete
460
	 *  @param 	bool 	$notrigger  false=launch triggers after, true=disable triggers
461
	 *  @return int         		>0 if OK, <0 if KO
462
	 */
463
	public function deleteLine(User $user, $idline, $notrigger = false)
464
	{
465
		if ($this->status < 0)
466
		{
467
			$this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
468
			return -2;
469
		}
470
471
		return $this->deleteLineCommon($user, $idline, $notrigger);
472
	}
473
474
	/**
475
	 *  Returns the reference to the following non used BOM depending on the active numbering module
476
	 *  defined into BOM_ADDON
477
	 *
478
	 *  @param	Product		$prod 	Object product
479
	 *  @return string      		BOM free reference
480
	 */
481
	public function getNextNumRef($prod)
482
	{
483
	    global $langs, $conf;
484
	    $langs->load("mrp");
485
486
	    if (!empty($conf->global->BOM_ADDON))
487
	    {
488
	        $mybool = false;
489
490
	        $file = $conf->global->BOM_ADDON.".php";
491
	        $classname = $conf->global->BOM_ADDON;
492
493
	        // Include file with class
494
	        $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
495
	        foreach ($dirmodels as $reldir)
496
	        {
497
	            $dir = dol_buildpath($reldir."core/modules/bom/");
498
499
	            // Load file with numbering class (if found)
500
	            $mybool |= @include_once $dir.$file;
501
	        }
502
503
	        if ($mybool === false)
504
	        {
505
	            dol_print_error('', "Failed to include file ".$file);
506
	            return '';
507
	        }
508
509
	        $obj = new $classname();
510
	        $numref = $obj->getNextValue($prod, $this);
511
512
	        if ($numref != "")
513
	        {
514
	            return $numref;
515
	        } else {
516
	            $this->error = $obj->error;
517
	            //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
518
	            return "";
519
	        }
520
	    } else {
521
	        print $langs->trans("Error")." ".$langs->trans("Error_BOM_ADDON_NotDefined");
522
	        return "";
523
	    }
524
	}
525
526
	/**
527
	 *	Validate bom
528
	 *
529
	 *	@param		User	$user     		User making status change
530
	 *  @param		int		$notrigger		1=Does not execute triggers, 0= execute triggers
531
	 *	@return  	int						<=0 if OK, 0=Nothing done, >0 if KO
532
	 */
533
	public function validate($user, $notrigger = 0)
534
	{
535
	    global $conf, $langs;
536
537
	    require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
538
539
	    $error = 0;
540
541
	    // Protection
542
	    if ($this->status == self::STATUS_VALIDATED)
543
	    {
544
	        dol_syslog(get_class($this)."::validate action abandonned: already validated", LOG_WARNING);
545
	        return 0;
546
	    }
547
548
	    /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->create))
549
	        || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate))))
550
	    {
551
	        $this->error='NotEnoughPermissions';
552
	        dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
553
	        return -1;
554
	    }*/
555
556
	    $now = dol_now();
557
558
	    $this->db->begin();
559
560
	    // Define new ref
561
	    if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) // empty should not happened, but when it occurs, the test save life
562
	    {
563
	        $this->fetch_product();
564
	        $num = $this->getNextNumRef($this->product);
565
	    } else {
566
	        $num = $this->ref;
567
	    }
568
	    $this->newref = dol_sanitizeFileName($num);
569
570
	    // Validate
571
	    $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
572
	    $sql .= " SET ref = '".$this->db->escape($num)."',";
573
	    $sql .= " status = ".self::STATUS_VALIDATED.",";
574
	    $sql .= " date_valid='".$this->db->idate($now)."',";
575
	    $sql .= " fk_user_valid = ".$user->id;
576
	    $sql .= " WHERE rowid = ".$this->id;
577
578
	    dol_syslog(get_class($this)."::validate()", LOG_DEBUG);
579
	    $resql = $this->db->query($sql);
580
	    if (!$resql)
581
	    {
582
	        dol_print_error($this->db);
583
	        $this->error = $this->db->lasterror();
584
	        $error++;
585
	    }
586
587
	    if (!$error && !$notrigger)
588
	    {
589
	        // Call trigger
590
	        $result = $this->call_trigger('BOM_VALIDATE', $user);
591
	        if ($result < 0) $error++;
592
	        // End call triggers
593
	    }
594
595
	    if (!$error)
596
	    {
597
	        $this->oldref = $this->ref;
598
599
	        // Rename directory if dir was a temporary ref
600
	        if (preg_match('/^[\(]?PROV/i', $this->ref))
601
	        {
602
	        	// Now we rename also files into index
603
	        	$sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'bom/".$this->db->escape($this->newref)."'";
604
	        	$sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'bom/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
605
	        	$resql = $this->db->query($sql);
606
	        	if (!$resql) { $error++; $this->error = $this->db->lasterror(); }
607
608
	        	// We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
609
	        	$oldref = dol_sanitizeFileName($this->ref);
610
	            $newref = dol_sanitizeFileName($num);
611
	            $dirsource = $conf->bom->dir_output.'/'.$oldref;
612
	            $dirdest = $conf->bom->dir_output.'/'.$newref;
613
	            if (!$error && file_exists($dirsource))
614
	            {
615
	                dol_syslog(get_class($this)."::validate() rename dir ".$dirsource." into ".$dirdest);
616
617
	                if (@rename($dirsource, $dirdest))
618
	                {
619
	                    dol_syslog("Rename ok");
620
	                    // Rename docs starting with $oldref with $newref
621
	                    $listoffiles = dol_dir_list($conf->bom->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
622
	                    foreach ($listoffiles as $fileentry)
623
	                    {
624
	                        $dirsource = $fileentry['name'];
625
	                        $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
626
	                        $dirsource = $fileentry['path'].'/'.$dirsource;
627
	                        $dirdest = $fileentry['path'].'/'.$dirdest;
628
	                        @rename($dirsource, $dirdest);
629
	                    }
630
	                }
631
	            }
632
	        }
633
	    }
634
635
	    // Set new ref and current status
636
	    if (!$error)
637
	    {
638
	        $this->ref = $num;
639
	        $this->status = self::STATUS_VALIDATED;
640
	    }
641
642
	    if (!$error)
643
	    {
644
	        $this->db->commit();
645
	        return 1;
646
	    } else {
647
	        $this->db->rollback();
648
	        return -1;
649
	    }
650
	}
651
652
	/**
653
	 *	Set draft status
654
	 *
655
	 *	@param	User	$user			Object user that modify
656
	 *  @param	int		$notrigger		1=Does not execute triggers, 0=Execute triggers
657
	 *	@return	int						<0 if KO, >0 if OK
658
	 */
659
	public function setDraft($user, $notrigger = 0)
660
	{
661
		// Protection
662
		if ($this->status <= self::STATUS_DRAFT)
663
		{
664
			return 0;
665
		}
666
667
		/*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->write))
668
		 || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate))))
669
		 {
670
		 $this->error='Permission denied';
671
		 return -1;
672
		 }*/
673
674
		return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'BOM_UNVALIDATE');
675
	}
676
677
	/**
678
	 *	Set cancel status
679
	 *
680
	 *	@param	User	$user			Object user that modify
681
	 *  @param	int		$notrigger		1=Does not execute triggers, 0=Execute triggers
682
	 *	@return	int						<0 if KO, 0=Nothing done, >0 if OK
683
	 */
684
	public function cancel($user, $notrigger = 0)
685
	{
686
		// Protection
687
		if ($this->status != self::STATUS_VALIDATED)
688
		{
689
			return 0;
690
		}
691
692
		/*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->write))
693
		 || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate))))
694
		 {
695
		 $this->error='Permission denied';
696
		 return -1;
697
		 }*/
698
699
		return $this->setStatusCommon($user, self::STATUS_CANCELED, $notrigger, 'BOM_CLOSE');
700
	}
701
702
	/**
703
	 *	Set cancel status
704
	 *
705
	 *	@param	User	$user			Object user that modify
706
	 *  @param	int		$notrigger		1=Does not execute triggers, 0=Execute triggers
707
	 *	@return	int						<0 if KO, 0=Nothing done, >0 if OK
708
	 */
709
	public function reopen($user, $notrigger = 0)
710
	{
711
		// Protection
712
		if ($this->status != self::STATUS_CANCELED)
713
		{
714
			return 0;
715
		}
716
717
		/*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->write))
718
		 || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate))))
719
		 {
720
		 $this->error='Permission denied';
721
		 return -1;
722
		 }*/
723
724
		return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'BOM_REOPEN');
725
	}
726
727
728
	/**
729
	 *  Return a link to the object card (with optionaly the picto)
730
	 *
731
	 *	@param	int		$withpicto					Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
732
	 *	@param	string	$option						On what the link point to ('nolink', ...)
733
     *  @param	int  	$notooltip					1=Disable tooltip
734
     *  @param  string  $morecss            		Add more css on link
735
     *  @param  int     $save_lastsearch_value    	-1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
736
	 *	@return	string								String with URL
737
	 */
738
	public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
739
	{
740
		global $db, $conf, $langs, $hookmanager;
741
        global $dolibarr_main_authentication, $dolibarr_main_demo;
742
        global $menumanager;
743
744
        if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
745
746
        $result = '';
747
748
        $label = '<u>'.$langs->trans("BillOfMaterials").'</u>';
749
        $label .= '<br>';
750
        $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
751
        if (isset($this->status)) {
752
        	$label .= '<br><b>'.$langs->trans("Status").":</b> ".$this->getLibStatut(5);
753
        }
754
755
        $url = dol_buildpath('/bom/bom_card.php', 1).'?id='.$this->id;
756
757
        if ($option != 'nolink')
758
        {
759
	        // Add param to save lastsearch_values or not
760
	        $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
761
	        if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
762
	        if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
763
        }
764
765
        $linkclose = '';
766
        if (empty($notooltip))
767
        {
768
            if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
769
            {
770
                $label = $langs->trans("ShowBillOfMaterials");
771
                $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
772
            }
773
            $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
774
            $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
775
776
            /*
777
             $hookmanager->initHooks(array('bomdao'));
778
             $parameters=array('id'=>$this->id);
779
             $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action);    // Note that $action and $object may have been modified by some hooks
780
             if ($reshook > 0) $linkclose = $hookmanager->resPrint;
781
             */
782
        } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
783
784
		$linkstart = '<a href="'.$url.'"';
785
		$linkstart .= $linkclose.'>';
786
		$linkend = '</a>';
787
788
		$result .= $linkstart;
789
		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);
790
		if ($withpicto != 2) $result .= $this->ref;
791
		$result .= $linkend;
792
		//if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
793
794
		global $action, $hookmanager;
795
		$hookmanager->initHooks(array('bomdao'));
796
		$parameters = array('id'=>$this->id, 'getnomurl'=>$result);
797
		$reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
798
		if ($reshook > 0) $result = $hookmanager->resPrint;
799
		else $result .= $hookmanager->resPrint;
800
801
		return $result;
802
	}
803
804
	/**
805
	 *  Return label of the status
806
	 *
807
	 *  @param  int		$mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
808
	 *  @return	string 			       Label of status
809
	 */
810
	public function getLibStatut($mode = 0)
811
	{
812
		return $this->LibStatut($this->status, $mode);
813
	}
814
815
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
816
	/**
817
	 *  Return the status
818
	 *
819
	 *  @param	int		$status        Id status
820
	 *  @param  int		$mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
821
	 *  @return string 			       Label of status
822
	 */
823
	public function LibStatut($status, $mode = 0)
824
	{
825
		// phpcs:enable
826
		if (empty($this->labelStatus))
827
		{
828
			global $langs;
829
			//$langs->load("mrp");
830
			$this->labelStatus[self::STATUS_DRAFT] = $langs->trans('Draft');
831
			$this->labelStatus[self::STATUS_VALIDATED] = $langs->trans('Enabled');
832
			$this->labelStatus[self::STATUS_CANCELED] = $langs->trans('Disabled');
833
		}
834
835
		$statusType = 'status'.$status;
836
		if ($status == self::STATUS_VALIDATED) $statusType = 'status4';
837
		if ($status == self::STATUS_CANCELED) $statusType = 'status6';
838
839
		return dolGetStatus($this->labelStatus[$status], $this->labelStatus[$status], '', $statusType, $mode);
840
	}
841
842
	/**
843
	 *	Load the info information in the object
844
	 *
845
	 *	@param  int		$id       Id of object
846
	 *	@return	void
847
	 */
848
	public function info($id)
849
	{
850
		$sql = 'SELECT rowid, date_creation as datec, tms as datem,';
851
		$sql .= ' fk_user_creat, fk_user_modif';
852
		$sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
853
		$sql .= ' WHERE t.rowid = '.$id;
854
		$result = $this->db->query($sql);
855
		if ($result)
856
		{
857
			if ($this->db->num_rows($result))
858
			{
859
				$obj = $this->db->fetch_object($result);
860
				$this->id = $obj->rowid;
861
				if ($obj->fk_user_author)
862
				{
863
					$cuser = new User($this->db);
864
					$cuser->fetch($obj->fk_user_author);
865
					$this->user_creation = $cuser;
866
				}
867
868
				if ($obj->fk_user_valid)
869
				{
870
					$vuser = new User($this->db);
871
					$vuser->fetch($obj->fk_user_valid);
872
					$this->user_validation = $vuser;
873
				}
874
875
				if ($obj->fk_user_cloture)
876
				{
877
					$cluser = new User($this->db);
878
					$cluser->fetch($obj->fk_user_cloture);
879
					$this->user_cloture = $cluser;
880
				}
881
882
				$this->date_creation     = $this->db->jdate($obj->datec);
883
				$this->date_modification = $this->db->jdate($obj->datem);
884
				$this->date_validation   = $this->db->jdate($obj->datev);
885
			}
886
887
			$this->db->free($result);
888
		} else {
889
			dol_print_error($this->db);
890
		}
891
	}
892
893
	/**
894
	 * 	Create an array of lines
895
	 *
896
	 * 	@return array|int		array of lines if OK, <0 if KO
897
	 */
898
	public function getLinesArray()
899
	{
900
	    $this->lines = array();
901
902
	    $objectline = new BOMLine($this->db);
903
	    $result = $objectline->fetchAll('ASC', 'position', 0, 0, array('customsql'=>'fk_bom = '.$this->id));
904
905
	    if (is_numeric($result))
906
	    {
907
	        $this->error = $this->error;
908
	        $this->errors = $this->errors;
909
	        return $result;
910
	    } else {
911
	        $this->lines = $result;
912
	        return $this->lines;
913
	    }
914
	}
915
916
	/**
917
	 *  Create a document onto disk according to template module.
918
	 *
919
	 *  @param	    string		$modele			Force template to use ('' to not force)
920
	 *  @param		Translate	$outputlangs	objet lang a utiliser pour traduction
921
	 *  @param      int			$hidedetails    Hide details of lines
922
	 *  @param      int			$hidedesc       Hide description
923
	 *  @param      int			$hideref        Hide ref
924
	 *  @param      null|array  $moreparams     Array to provide more information
925
	 *  @return     int         				0 if KO, 1 if OK
926
	 */
927
	public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
928
	{
929
		global $conf, $langs;
930
931
		$langs->load("mrp");
932
933
		if (!dol_strlen($modele)) {
934
			$modele = 'standard';
935
936
			if ($this->modelpdf) {
937
				$modele = $this->modelpdf;
938
			} elseif (!empty($conf->global->BOM_ADDON_PDF)) {
939
				$modele = $conf->global->BOM_ADDON_PDF;
940
			}
941
		}
942
943
		$modelpath = "core/modules/bom/doc/";
944
945
		return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
946
	}
947
948
	/**
949
	 * Initialise object with example values
950
	 * Id must be 0 if object instance is a specimen
951
	 *
952
	 * @return void
953
	 */
954
	public function initAsSpecimen()
955
	{
956
	    $this->initAsSpecimenCommon();
957
	    $this->ref = 'BOM-123';
958
	    $this->date = $this->date_creation;
959
	}
960
961
962
	/**
963
	 * Action executed by scheduler
964
	 * CAN BE A CRON TASK. In such a case, parameters come from the schedule job setup field 'Parameters'
965
	 *
966
	 * @return	int			0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
967
	 */
968
	public function doScheduledJob()
969
	{
970
		global $conf, $langs;
971
972
		//$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
973
974
		$error = 0;
975
		$this->output = '';
976
		$this->error = '';
977
978
		dol_syslog(__METHOD__, LOG_DEBUG);
979
980
		$now = dol_now();
981
982
		$this->db->begin();
983
984
		// ...
985
986
		$this->db->commit();
987
988
		return $error;
989
	}
990
991
	/**
992
	 * BOM costs calculation based on cost_price or pmp of each BOM line
993
	 *
994
	 * @return void
995
	 */
996
	public function calculateCosts()
997
	{
998
		include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
999
		$this->unit_cost = 0;
1000
		$this->total_cost = 0;
1001
1002
		require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
1003
		$productFournisseur = new ProductFournisseur($this->db);
1004
1005
		foreach ($this->lines as &$line) {
1006
			$tmpproduct = new Product($this->db);
1007
			$result= $tmpproduct->fetch($line->fk_product);
1008
			if ($result < 0) {
1009
				$this->error=$tmpproduct->error;
1010
				return -1;
1011
			}
1012
			$line->unit_cost = price2num((!empty($tmpproduct->cost_price)) ? $tmpproduct->cost_price : $tmpproduct->pmp);
1013
			if (empty($line->unit_cost)) {
1014
				if ($productFournisseur->find_min_price_product_fournisseur($line->fk_product) > 0)
1015
				{
1016
					$line->unit_cost = $productFournisseur->fourn_unitprice;
1017
				}
1018
			}
1019
1020
			$line->total_cost = price2num($line->qty * $line->unit_cost, 'MT');
1021
			$this->total_cost += $line->total_cost;
1022
		}
1023
1024
		$this->total_cost = price2num($this->total_cost, 'MT');
1025
		if ($this->qty) {
1026
			$this->unit_cost = price2num($this->total_cost / $this->qty, 'MU');
1027
		}
1028
	}
1029
}
1030
1031
1032
/**
1033
 * Class for BOMLine
1034
 */
1035
class BOMLine extends CommonObjectLine
1036
{
1037
	/**
1038
	 * @var string ID to identify managed object
1039
	 */
1040
	public $element = 'bomline';
1041
1042
	/**
1043
	 * @var string Name of table without prefix where object is stored
1044
	 */
1045
	public $table_element = 'bom_bomline';
1046
1047
	/**
1048
	 * @var int  Does bomline support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
1049
	 */
1050
	public $ismultientitymanaged = 0;
1051
1052
	/**
1053
	 * @var int  Does bomline support extrafields ? 0=No, 1=Yes
1054
	 */
1055
	public $isextrafieldmanaged = 1;
1056
1057
	/**
1058
	 * @var string String with name of icon for bomline. Must be the part after the 'object_' into object_bomline.png
1059
	 */
1060
	public $picto = 'bomline';
1061
1062
1063
	/**
1064
	 *  'type' if the field format.
1065
	 *  'label' the translation key.
1066
	 *  'enabled' is a condition when the field must be managed.
1067
	 *  '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. Using a negative value means field is not shown by default on list but can be selected for viewing)
1068
	 *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
1069
	 *  'default' is a default value for creation (can still be replaced by the global setup of default values)
1070
	 *  'index' if we want an index in database.
1071
	 *  'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
1072
	 *  'position' is the sort order of field.
1073
	 *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.
1074
	 *  '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).
1075
	 *  'css' is the CSS style to use on field. For example: 'maxwidth200'
1076
	 *  'help' is a string visible as a tooltip on field
1077
	 *  'comment' is not used. You can store here any text of your choice. It is not used by application.
1078
	 *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record
1079
	 *  'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
1080
	 */
1081
1082
	// BEGIN MODULEBUILDER PROPERTIES
1083
	/**
1084
	 * @var array  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
1085
	 */
1086
	public $fields = array(
1087
		'rowid' => array('type'=>'integer', 'label'=>'LineID', 'enabled'=>1, 'visible'=>-1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",),
1088
		'fk_bom' => array('type'=>'integer:BillOfMaterials:societe/class/bom.class.php', 'label'=>'BillOfMaterials', 'enabled'=>1, 'visible'=>1, 'position'=>10, 'notnull'=>1, 'index'=>1,),
1089
		'fk_product' => array('type'=>'integer:Product:product/class/product.class.php', 'label'=>'Product', 'enabled'=>1, 'visible'=>1, 'position'=>20, 'notnull'=>1, 'index'=>1,),
1090
		'description' => array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>60, 'notnull'=>-1,),
1091
		'qty' => array('type'=>'double(24,8)', 'label'=>'Quantity', 'enabled'=>1, 'visible'=>1, 'position'=>100, 'notnull'=>1, 'isameasure'=>'1',),
1092
		'qty_frozen' => array('type'=>'smallint', 'label'=>'QuantityFrozen', 'enabled'=>1, 'visible'=>1, 'default'=>0, 'position'=>105, 'css'=>'maxwidth50imp', 'help'=>'QuantityConsumedInvariable'),
1093
	    'disable_stock_change' => array('type'=>'smallint', 'label'=>'DisableStockChange', 'enabled'=>1, 'visible'=>1, 'default'=>0, 'position'=>108, 'css'=>'maxwidth50imp', 'help'=>'DisableStockChangeHelp'),
1094
	    'efficiency' => array('type'=>'double(24,8)', 'label'=>'ManufacturingEfficiency', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'position'=>110, 'notnull'=>1, 'css'=>'maxwidth50imp', 'help'=>'ValueOfEfficiencyConsumedMeans'),
1095
		'position' => array('type'=>'integer', 'label'=>'Rank', 'enabled'=>1, 'visible'=>0, 'default'=>0, 'position'=>200, 'notnull'=>1,),
1096
		'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000, 'notnull'=>-1,),
1097
	);
1098
	public $rowid;
1099
	public $fk_bom;
1100
	public $fk_product;
1101
	public $description;
1102
	public $qty;
1103
	public $qty_frozen;
1104
	public $disable_stock_change;
1105
	public $efficiency;
1106
	public $position;
1107
	public $import_key;
1108
	// END MODULEBUILDER PROPERTIES
1109
1110
	/**
1111
	 * @var int		Calculated cost for the BOM line
1112
	 */
1113
	public $total_cost = 0;
1114
1115
	/**
1116
	 * @var int		Line unit cost based on product cost price or pmp
1117
	 */
1118
	public $unit_cost = 0;
1119
1120
1121
	/**
1122
	 * Constructor
1123
	 *
1124
	 * @param DoliDb $db Database handler
1125
	 */
1126
	public function __construct(DoliDB $db)
1127
	{
1128
		global $conf, $langs;
1129
1130
		$this->db = $db;
1131
1132
		if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible'] = 0;
1133
		if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) $this->fields['entity']['enabled'] = 0;
1134
1135
		// Unset fields that are disabled
1136
		foreach ($this->fields as $key => $val)
1137
		{
1138
			if (isset($val['enabled']) && empty($val['enabled']))
1139
			{
1140
				unset($this->fields[$key]);
1141
			}
1142
		}
1143
1144
		// Translate some data of arrayofkeyval
1145
		foreach ($this->fields as $key => $val)
1146
		{
1147
			if (is_array($val['arrayofkeyval']))
1148
			{
1149
				foreach ($val['arrayofkeyval'] as $key2 => $val2)
1150
				{
1151
					$this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
1152
				}
1153
			}
1154
		}
1155
	}
1156
1157
	/**
1158
	 * Create object into database
1159
	 *
1160
	 * @param  User $user      User that creates
1161
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
1162
	 * @return int             <0 if KO, Id of created object if OK
1163
	 */
1164
	public function create(User $user, $notrigger = false)
1165
	{
1166
		if ($this->efficiency < 0 || $this->efficiency > 1) $this->efficiency = 1;
1167
1168
		return $this->createCommon($user, $notrigger);
1169
	}
1170
1171
	/**
1172
	 * Load object in memory from the database
1173
	 *
1174
	 * @param int    $id   Id object
1175
	 * @param string $ref  Ref
1176
	 * @return int         <0 if KO, 0 if not found, >0 if OK
1177
	 */
1178
	public function fetch($id, $ref = null)
1179
	{
1180
		$result = $this->fetchCommon($id, $ref);
1181
		//if ($result > 0 && ! empty($this->table_element_line)) $this->fetchLines();
1182
		return $result;
1183
	}
1184
1185
	/**
1186
	 * Load list of objects in memory from the database.
1187
	 *
1188
	 * @param  string      $sortorder    Sort Order
1189
	 * @param  string      $sortfield    Sort field
1190
	 * @param  int         $limit        limit
1191
	 * @param  int         $offset       Offset
1192
	 * @param  array       $filter       Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
1193
	 * @param  string      $filtermode   Filter mode (AND or OR)
1194
	 * @return array|int                 int <0 if KO, array of pages if OK
1195
	 */
1196
	public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
1197
	{
1198
		global $conf;
1199
1200
		dol_syslog(__METHOD__, LOG_DEBUG);
1201
1202
		$records = array();
1203
1204
		$sql = 'SELECT ';
1205
		$sql .= $this->getFieldList();
1206
		$sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
1207
		if ($this->ismultientitymanaged) $sql .= ' WHERE t.entity IN ('.getEntity($this->table_element).')';
1208
		else $sql .= ' WHERE 1 = 1';
1209
		// Manage filter
1210
		$sqlwhere = array();
1211
		if (count($filter) > 0) {
1212
			foreach ($filter as $key => $value) {
1213
				if ($key == 't.rowid') {
1214
					$sqlwhere[] = $key.'='.$value;
1215
				} elseif (strpos($key, 'date') !== false) {
1216
					$sqlwhere[] = $key.' = \''.$this->db->idate($value).'\'';
1217
				} elseif ($key == 'customsql') {
1218
					$sqlwhere[] = $value;
1219
				} else {
1220
					$sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
1221
				}
1222
			}
1223
		}
1224
		if (count($sqlwhere) > 0) {
1225
			$sql .= ' AND ('.implode(' '.$filtermode.' ', $sqlwhere).')';
1226
		}
1227
1228
		if (!empty($sortfield)) {
1229
			$sql .= $this->db->order($sortfield, $sortorder);
1230
		}
1231
		if (!empty($limit)) {
1232
			$sql .= ' '.$this->db->plimit($limit, $offset);
1233
		}
1234
1235
		$resql = $this->db->query($sql);
1236
		if ($resql) {
1237
			$num = $this->db->num_rows($resql);
1238
1239
			while ($obj = $this->db->fetch_object($resql))
1240
			{
1241
				$record = new self($this->db);
1242
				$record->setVarsFromFetchObj($obj);
1243
1244
				$records[$record->id] = $record;
1245
			}
1246
			$this->db->free($resql);
1247
1248
			return $records;
1249
		} else {
1250
			$this->errors[] = 'Error '.$this->db->lasterror();
1251
			dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
1252
1253
			return -1;
1254
		}
1255
	}
1256
1257
	/**
1258
	 * Update object into database
1259
	 *
1260
	 * @param  User $user      User that modifies
1261
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
1262
	 * @return int             <0 if KO, >0 if OK
1263
	 */
1264
	public function update(User $user, $notrigger = false)
1265
	{
1266
		if ($this->efficiency < 0 || $this->efficiency > 1) $this->efficiency = 1;
1267
1268
		return $this->updateCommon($user, $notrigger);
1269
	}
1270
1271
	/**
1272
	 * Delete object in database
1273
	 *
1274
	 * @param User $user       User that deletes
1275
	 * @param bool $notrigger  false=launch triggers after, true=disable triggers
1276
	 * @return int             <0 if KO, >0 if OK
1277
	 */
1278
	public function delete(User $user, $notrigger = false)
1279
	{
1280
		return $this->deleteCommon($user, $notrigger);
1281
		//return $this->deleteCommon($user, $notrigger, 1);
1282
	}
1283
1284
	/**
1285
	 *  Return a link to the object card (with optionaly the picto)
1286
	 *
1287
	 *	@param	int		$withpicto					Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
1288
	 *	@param	string	$option						On what the link point to ('nolink', ...)
1289
     *  @param	int  	$notooltip					1=Disable tooltip
1290
     *  @param  string  $morecss            		Add more css on link
1291
     *  @param  int     $save_lastsearch_value    	-1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
1292
     *  @return	string								String with URL
1293
     */
1294
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
1295
    {
1296
        global $db, $conf, $langs, $hookmanager;
1297
1298
        if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
1299
1300
        $result = '';
1301
1302
        $label = '<u>'.$langs->trans("BillOfMaterialsLine").'</u>';
1303
        $label .= '<br>';
1304
        $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
1305
1306
        $url = dol_buildpath('/bom/bomline_card.php', 1).'?id='.$this->id;
1307
1308
        if ($option != 'nolink')
1309
        {
1310
	        // Add param to save lastsearch_values or not
1311
	        $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1312
	        if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
1313
	        if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
1314
        }
1315
1316
        $linkclose = '';
1317
        if (empty($notooltip))
1318
        {
1319
            if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
1320
            {
1321
                $label = $langs->trans("ShowBillOfMaterialsLine");
1322
                $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1323
            }
1324
            $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1325
            $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1326
1327
            /*
1328
             $hookmanager->initHooks(array('bomlinedao'));
1329
             $parameters=array('id'=>$this->id);
1330
             $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action);    // Note that $action and $object may have been modified by some hooks
1331
             if ($reshook > 0) $linkclose = $hookmanager->resPrint;
1332
             */
1333
        } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1334
1335
		$linkstart = '<a href="'.$url.'"';
1336
		$linkstart .= $linkclose.'>';
1337
		$linkend = '</a>';
1338
1339
		$result .= $linkstart;
1340
		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);
1341
		if ($withpicto != 2) $result .= $this->ref;
1342
		$result .= $linkend;
1343
		//if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1344
1345
		global $action, $hookmanager;
1346
		$hookmanager->initHooks(array('bomlinedao'));
1347
		$parameters = array('id'=>$this->id, 'getnomurl'=>$result);
1348
		$reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1349
		if ($reshook > 0) $result = $hookmanager->resPrint;
1350
		else $result .= $hookmanager->resPrint;
1351
1352
		return $result;
1353
    }
1354
1355
	/**
1356
	 *  Return label of the status
1357
	 *
1358
	 *  @param  int		$mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
1359
	 *  @return	string 			       Label of status
1360
	 */
1361
	public function getLibStatut($mode = 0)
1362
	{
1363
		return $this->LibStatut($this->status, $mode);
1364
	}
1365
1366
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1367
	/**
1368
	 *  Return the status
1369
	 *
1370
	 *  @param	int		$status        Id status
1371
	 *  @param  int		$mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
1372
	 *  @return string 			       Label of status
1373
	 */
1374
	public function LibStatut($status, $mode = 0)
1375
	{
1376
		// phpcs:enable
1377
		return '';
1378
	}
1379
1380
	/**
1381
	 *	Load the info information in the object
1382
	 *
1383
	 *	@param  int		$id       Id of object
1384
	 *	@return	void
1385
	 */
1386
	public function info($id)
1387
	{
1388
		$sql = 'SELECT rowid, date_creation as datec, tms as datem,';
1389
		$sql .= ' fk_user_creat, fk_user_modif';
1390
		$sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
1391
		$sql .= ' WHERE t.rowid = '.$id;
1392
		$result = $this->db->query($sql);
1393
		if ($result)
1394
		{
1395
			if ($this->db->num_rows($result))
1396
			{
1397
				$obj = $this->db->fetch_object($result);
1398
				$this->id = $obj->rowid;
1399
				if ($obj->fk_user_author)
1400
				{
1401
					$cuser = new User($this->db);
1402
					$cuser->fetch($obj->fk_user_author);
1403
					$this->user_creation = $cuser;
1404
				}
1405
1406
				if ($obj->fk_user_valid)
1407
				{
1408
					$vuser = new User($this->db);
1409
					$vuser->fetch($obj->fk_user_valid);
1410
					$this->user_validation = $vuser;
1411
				}
1412
1413
				if ($obj->fk_user_cloture)
1414
				{
1415
					$cluser = new User($this->db);
1416
					$cluser->fetch($obj->fk_user_cloture);
1417
					$this->user_cloture = $cluser;
1418
				}
1419
1420
				$this->date_creation     = $this->db->jdate($obj->datec);
1421
				$this->date_modification = $this->db->jdate($obj->datem);
1422
				$this->date_validation   = $this->db->jdate($obj->datev);
1423
			}
1424
1425
			$this->db->free($result);
1426
		} else {
1427
			dol_print_error($this->db);
1428
		}
1429
	}
1430
1431
	/**
1432
	 * Initialise object with example values
1433
	 * Id must be 0 if object instance is a specimen
1434
	 *
1435
	 * @return void
1436
	 */
1437
	public function initAsSpecimen()
1438
	{
1439
		$this->initAsSpecimenCommon();
1440
	}
1441
}
1442