Completed
Branch develop (68bb95)
by
unknown
53:54
created

BOM::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 <http://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 * \file        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 bom 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
	public $table_element_line = 'bom_bomline';
61
62
63
	/**
64
	 *  'type' if the field format.
65
	 *  'label' the translation key.
66
	 *  'enabled' is a condition when the field must be managed.
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. Using a negative value means field is not shown by default on list but can be selected for viewing)
68
	 *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
69
	 *  'default' is a default value for creation (can still be replaced by the global setup of default values)
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
	 *  'position' is the sort order of field.
73
	 *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.
74
	 *  '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).
75
	 *  'css' is the CSS style to use on field. For example: 'maxwidth200'
76
	 *  'help' is a string visible as a tooltip on field
77
	 *  'comment' is not used. You can store here any text of your choice. It is not used by application.
78
	 *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record
79
	 *  'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
80
	 */
81
82
	// BEGIN MODULEBUILDER PROPERTIES
83
	/**
84
	 * @var array  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
85
	 */
86
	public $fields=array(
87
		'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",),
88
		'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'position'=>10, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of BOM", 'showoncombobox'=>'1',),
89
		'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'notnull'=>1, 'searchall'=>1, 'showoncombobox'=>'1',),
90
		'description' => array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>60, 'notnull'=>-1,),
91
		'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>-1, 'position'=>61, 'notnull'=>-1,),
92
		'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>-1, 'position'=>62, 'notnull'=>-1,),
93
		'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>500, 'notnull'=>1,),
94
		'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'position'=>501, 'notnull'=>1,),
95
		'fk_user_creat' => array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'position'=>510, 'notnull'=>1, 'foreignkey'=>'llx_user.rowid',),
96
		'fk_user_modif' => array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'position'=>511, 'notnull'=>-1,),
97
		'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000, 'notnull'=>-1,),
98
		'fk_product' => array('type'=>'integer:Product:product/class/product.class.php', 'label'=>'Product', 'enabled'=>1, 'visible'=>1, 'position'=>50, 'notnull'=>1, 'index'=>1, 'help'=>'ProductBOMHelp'),
99
		'qty' => array('type'=>'double(24,8)', 'label'=>'Quantity', 'enabled'=>1, 'visible'=>1, 'default'=>1, 'position'=>55, 'notnull'=>1, 'isameasure'=>'1',),
100
	    'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>2, 'position'=>1000, 'notnull'=>1, 'default'=>0, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Draft', '1'=>'Enabled', '-1'=>'Disabled')),
101
	);
102
	public $rowid;
103
	public $ref;
104
	public $label;
105
	public $description;
106
	public $note_public;
107
	public $note_private;
108
	public $date_creation;
109
	public $tms;
110
	public $fk_user_creat;
111
	public $fk_user_modif;
112
	public $import_key;
113
	public $status;
114
	public $fk_product;
115
	public $qty;
116
	// END MODULEBUILDER PROPERTIES
117
118
119
120
	// If this object has a subtable with lines
121
122
	/**
123
	 * @var int    Name of subtable line
124
	 */
125
	//public $table_element_line = 'bomdet';
126
127
	/**
128
	 * @var int    Field with ID of parent key if this field has a parent
129
	 */
130
	//public $fk_element = 'fk_bom';
131
132
	/**
133
	 * @var int    Name of subtable class that manage subtable lines
134
	 */
135
	//public $class_element_line = 'BillOfMaterialsline';
136
137
	/**
138
	 * @var array  Array of child tables (child tables to delete before deleting a record)
139
	 */
140
	//protected $childtables=array('bomdet');
141
142
	/**
143
	 * @var BillOfMaterialsLine[]     Array of subtable lines
144
	 */
145
	//public $lines = array();
146
147
148
149
	/**
150
	 * Constructor
151
	 *
152
	 * @param DoliDb $db Database handler
153
	 */
154
	public function __construct(DoliDB $db)
155
	{
156
		global $conf, $langs, $user;
157
158
		$this->db = $db;
159
160
		if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible']=0;
161
		if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) $this->fields['entity']['enabled']=0;
162
163
		// Unset fields that are disabled
164
		foreach($this->fields as $key => $val)
165
		{
166
			if (isset($val['enabled']) && empty($val['enabled']))
167
			{
168
				unset($this->fields[$key]);
169
			}
170
		}
171
172
		// Translate some data of arrayofkeyval
173
		foreach($this->fields as $key => $val)
174
		{
175
			if (is_array($this->fields['status']['arrayofkeyval']))
176
			{
177
				foreach($this->fields['status']['arrayofkeyval'] as $key2 => $val2)
178
				{
179
					$this->fields['status']['arrayofkeyval'][$key2]=$langs->trans($val2);
180
				}
181
			}
182
		}
183
	}
184
185
	/**
186
	 * Create object into database
187
	 *
188
	 * @param  User $user      User that creates
189
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
190
	 * @return int             <0 if KO, Id of created object if OK
191
	 */
192
	public function create(User $user, $notrigger = false)
193
	{
194
		return $this->createCommon($user, $notrigger);
195
	}
196
197
	/**
198
	 * Clone an object into another one
199
	 *
200
	 * @param  	User 	$user      	User that creates
201
	 * @param  	int 	$fromid     Id of object to clone
202
	 * @return 	mixed 				New object created, <0 if KO
203
	 */
204
	public function createFromClone(User $user, $fromid)
205
	{
206
		global $langs, $hookmanager, $extrafields;
207
	    $error = 0;
208
209
	    dol_syslog(__METHOD__, LOG_DEBUG);
210
211
	    $object = new self($this->db);
212
213
	    $this->db->begin();
214
215
	    // Load source object
216
	    $object->fetchCommon($fromid);
217
	    // Reset some properties
218
	    unset($object->id);
219
	    unset($object->fk_user_creat);
220
	    unset($object->import_key);
221
222
	    // Clear fields
223
	    $object->ref = "copy_of_".$object->ref;
224
	    $object->title = $langs->trans("CopyOf")." ".$object->title;
225
	    // ...
226
	    // Clear extrafields that are unique
227
	    if (is_array($object->array_options) && count($object->array_options) > 0)
228
	    {
229
	    	$extrafields->fetch_name_optionals_label($this->element);
230
	    	foreach($object->array_options as $key => $option)
231
	    	{
232
	    		$shortkey = preg_replace('/options_/', '', $key);
233
	    		if (! empty($extrafields->attributes[$this->element]['unique'][$shortkey]))
234
	    		{
235
	    			//var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
236
	    			unset($object->array_options[$key]);
237
	    		}
238
	    	}
239
	    }
240
241
	    // Create clone
242
		$object->context['createfromclone'] = 'createfromclone';
243
	    $result = $object->createCommon($user);
244
	    if ($result < 0) {
245
	        $error++;
246
	        $this->error = $object->error;
247
	        $this->errors = $object->errors;
248
	    }
249
250
	    unset($object->context['createfromclone']);
251
252
	    // End
253
	    if (!$error) {
254
	        $this->db->commit();
255
	        return $object;
256
	    } else {
257
	        $this->db->rollback();
258
	        return -1;
259
	    }
260
	}
261
262
	/**
263
	 * Load object in memory from the database
264
	 *
265
	 * @param int    $id   Id object
266
	 * @param string $ref  Ref
267
	 * @return int         <0 if KO, 0 if not found, >0 if OK
268
	 */
269
	public function fetch($id, $ref = null)
270
	{
271
		$result = $this->fetchCommon($id, $ref);
272
		if ($result > 0 && ! empty($this->table_element_line)) $this->fetchLines();
0 ignored issues
show
Bug introduced by
The method fetchLines() does not seem to exist on object<BOM>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
273
		return $result;
274
	}
275
276
	/**
277
	 * Load object lines in memory from the database
278
	 *
279
	 * @return int         <0 if KO, 0 if not found, >0 if OK
280
	 */
281
	/*public function fetchLines()
282
	{
283
		$this->lines=array();
284
285
		// Load lines with object BillOfMaterialsLine
286
287
		return count($this->lines)?1:0;
288
	}*/
289
290
	/**
291
	 * Load list of objects in memory from the database.
292
	 *
293
	 * @param  string      $sortorder    Sort Order
294
	 * @param  string      $sortfield    Sort field
295
	 * @param  int         $limit        limit
296
	 * @param  int         $offset       Offset
297
	 * @param  array       $filter       Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
298
	 * @param  string      $filtermode   Filter mode (AND or OR)
299
	 * @return array|int                 int <0 if KO, array of pages if OK
300
	 */
301
	public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
302
	{
303
		global $conf;
304
305
		dol_syslog(__METHOD__, LOG_DEBUG);
306
307
		$records=array();
308
309
		$sql = 'SELECT';
310
		$sql .= ' t.rowid';
311
		// TODO Get all fields
312
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
313
		$sql .= ' WHERE t.entity = '.$conf->entity;
314
		// Manage filter
315
		$sqlwhere = array();
316
		if (count($filter) > 0) {
317
			foreach ($filter as $key => $value) {
318
				if ($key=='t.rowid') {
319
					$sqlwhere[] = $key . '='. $value;
320
				}
321
				elseif (strpos($key, 'date') !== false) {
322
					$sqlwhere[] = $key.' = \''.$this->db->idate($value).'\'';
323
				}
324
				elseif ($key=='customsql') {
325
					$sqlwhere[] = $value;
326
				}
327
				else {
328
					$sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
329
				}
330
			}
331
		}
332
		if (count($sqlwhere) > 0) {
333
			$sql .= ' AND (' . implode(' '.$filtermode.' ', $sqlwhere).')';
334
		}
335
336
		if (!empty($sortfield)) {
337
			$sql .= $this->db->order($sortfield, $sortorder);
338
		}
339
		if (!empty($limit)) {
340
			$sql .=  ' ' . $this->db->plimit($limit, $offset);
341
		}
342
343
		$resql = $this->db->query($sql);
344
		if ($resql) {
345
			$num = $this->db->num_rows($resql);
346
347
			while ($obj = $this->db->fetch_object($resql))
348
			{
349
				$record = new self($this->db);
350
351
				$record->id = $obj->rowid;
352
				// TODO Get other fields
353
354
				//var_dump($record->id);
355
				$records[$record->id] = $record;
356
			}
357
			$this->db->free($resql);
358
359
			return $records;
360
		} else {
361
			$this->errors[] = 'Error ' . $this->db->lasterror();
362
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
363
364
			return -1;
365
		}
366
	}
367
368
	/**
369
	 * Update object into database
370
	 *
371
	 * @param  User $user      User that modifies
372
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
373
	 * @return int             <0 if KO, >0 if OK
374
	 */
375
	public function update(User $user, $notrigger = false)
376
	{
377
		return $this->updateCommon($user, $notrigger);
378
	}
379
380
	/**
381
	 * Delete object in database
382
	 *
383
	 * @param User $user       User that deletes
384
	 * @param bool $notrigger  false=launch triggers after, true=disable triggers
385
	 * @return int             <0 if KO, >0 if OK
386
	 */
387
	public function delete(User $user, $notrigger = false)
388
	{
389
		return $this->deleteCommon($user, $notrigger);
390
		//return $this->deleteCommon($user, $notrigger, 1);
391
	}
392
393
	/**
394
	 *  Return a link to the object card (with optionaly the picto)
395
	 *
396
	 *	@param	int		$withpicto					Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
397
	 *	@param	string	$option						On what the link point to ('nolink', ...)
398
     *  @param	int  	$notooltip					1=Disable tooltip
399
     *  @param  string  $morecss            		Add more css on link
400
     *  @param  int     $save_lastsearch_value    	-1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
401
	 *	@return	string								String with URL
402
	 */
403
	public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
404
	{
405
		global $db, $conf, $langs, $hookmanager;
406
        global $dolibarr_main_authentication, $dolibarr_main_demo;
407
        global $menumanager;
408
409
        if (! empty($conf->dol_no_mouse_hover)) $notooltip=1;   // Force disable tooltips
410
411
        $result = '';
412
413
        $label = '<u>' . $langs->trans("BillOfMaterials") . '</u>';
414
        $label.= '<br>';
415
        $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
416
417
        $url = dol_buildpath('/bom/bom_card.php', 1).'?id='.$this->id;
418
419
        if ($option != 'nolink')
420
        {
421
	        // Add param to save lastsearch_values or not
422
	        $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
423
	        if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
424
	        if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
425
        }
426
427
        $linkclose='';
428
        if (empty($notooltip))
429
        {
430
            if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
431
            {
432
                $label=$langs->trans("ShowBillOfMaterials");
433
                $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
434
            }
435
            $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
436
            $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
437
438
            /*
439
             $hookmanager->initHooks(array('bomdao'));
440
             $parameters=array('id'=>$this->id);
441
             $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action);    // Note that $action and $object may have been modified by some hooks
442
             if ($reshook > 0) $linkclose = $hookmanager->resPrint;
443
             */
444
        }
445
        else $linkclose = ($morecss?' class="'.$morecss.'"':'');
446
447
		$linkstart = '<a href="'.$url.'"';
448
		$linkstart.=$linkclose.'>';
449
		$linkend='</a>';
450
451
		$result .= $linkstart;
452
		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);
453
		if ($withpicto != 2) $result.= $this->ref;
454
		$result .= $linkend;
455
		//if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
456
457
		global $action,$hookmanager;
458
		$hookmanager->initHooks(array('bomdao'));
459
		$parameters=array('id'=>$this->id, 'getnomurl'=>$result);
460
		$reshook=$hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);    // Note that $action and $object may have been modified by some hooks
461
		if ($reshook > 0) $result = $hookmanager->resPrint;
462
		else $result .= $hookmanager->resPrint;
463
464
		return $result;
465
	}
466
467
	/**
468
	 *  Return label of the status
469
	 *
470
	 *  @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
471
	 *  @return	string 			       Label of status
472
	 */
473
	public function getLibStatut($mode = 0)
474
	{
475
		return $this->LibStatut($this->status, $mode);
476
	}
477
478
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
479
	/**
480
	 *  Return the status
481
	 *
482
	 *  @param	int		$status        Id status
483
	 *  @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
484
	 *  @return string 			       Label of status
485
	 */
486
	public function LibStatut($status, $mode = 0)
487
	{
488
		// phpcs:enable
489
		if (empty($this->labelstatus))
490
		{
491
			global $langs;
492
			//$langs->load("mrp");
493
			$this->labelstatus[1] = $langs->trans('Enabled');
494
			$this->labelstatus[0] = $langs->trans('Disabled');
495
		}
496
497
		if ($mode == 0)
498
		{
499
			return $this->labelstatus[$status];
500
		}
501
		elseif ($mode == 1)
502
		{
503
			return $this->labelstatus[$status];
504
		}
505
		elseif ($mode == 2)
506
		{
507
			if ($status == 1) return img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
508
			elseif ($status == 0) return img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
509
		}
510
		elseif ($mode == 3)
511
		{
512
			if ($status == 1) return img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
513
			elseif ($status == 0) return img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
514
		}
515
		elseif ($mode == 4)
516
		{
517
			if ($status == 1) return img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
518
			elseif ($status == 0) return img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
519
		}
520
		elseif ($mode == 5)
521
		{
522
			if ($status == 1) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
523
			elseif ($status == 0) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
524
		}
525
		elseif ($mode == 6)
526
		{
527
			if ($status == 1) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
528
			elseif ($status == 0) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
529
		}
530
	}
531
532
	/**
533
	 *	Load the info information in the object
534
	 *
535
	 *	@param  int		$id       Id of object
536
	 *	@return	void
537
	 */
538
	public function info($id)
539
	{
540
		$sql = 'SELECT rowid, date_creation as datec, tms as datem,';
541
		$sql.= ' fk_user_creat, fk_user_modif';
542
		$sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
543
		$sql.= ' WHERE t.rowid = '.$id;
544
		$result=$this->db->query($sql);
545
		if ($result)
546
		{
547
			if ($this->db->num_rows($result))
548
			{
549
				$obj = $this->db->fetch_object($result);
550
				$this->id = $obj->rowid;
551
				if ($obj->fk_user_author)
552
				{
553
					$cuser = new User($this->db);
554
					$cuser->fetch($obj->fk_user_author);
555
					$this->user_creation   = $cuser;
556
				}
557
558
				if ($obj->fk_user_valid)
559
				{
560
					$vuser = new User($this->db);
561
					$vuser->fetch($obj->fk_user_valid);
562
					$this->user_validation = $vuser;
563
				}
564
565
				if ($obj->fk_user_cloture)
566
				{
567
					$cluser = new User($this->db);
568
					$cluser->fetch($obj->fk_user_cloture);
569
					$this->user_cloture   = $cluser;
570
				}
571
572
				$this->date_creation     = $this->db->jdate($obj->datec);
573
				$this->date_modification = $this->db->jdate($obj->datem);
574
				$this->date_validation   = $this->db->jdate($obj->datev);
575
			}
576
577
			$this->db->free($result);
578
		}
579
		else
580
		{
581
			dol_print_error($this->db);
582
		}
583
	}
584
585
	/**
586
	 * Initialise object with example values
587
	 * Id must be 0 if object instance is a specimen
588
	 *
589
	 * @return void
590
	 */
591
	public function initAsSpecimen()
592
	{
593
	    $this->initAsSpecimenCommon();
594
	    $this->date = $this->date_creation;
595
	}
596
597
598
	/**
599
	 * Action executed by scheduler
600
	 * CAN BE A CRON TASK. In such a case, parameters come from the schedule job setup field 'Parameters'
601
	 *
602
	 * @return	int			0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
603
	 */
604
	//public function doScheduledJob($param1, $param2, ...)
605
	public function doScheduledJob()
606
	{
607
		global $conf, $langs;
608
609
		//$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
610
611
		$error = 0;
612
		$this->output = '';
613
		$this->error='';
614
615
		dol_syslog(__METHOD__, LOG_DEBUG);
616
617
		$now = dol_now();
618
619
		$this->db->begin();
620
621
		// ...
622
623
		$this->db->commit();
624
625
		return $error;
626
	}
627
}
628
629
630
/**
631
 * Class for BillOfMaterialsLine
632
 */
633
class BillOfMaterialsLine extends CommonObject
634
{
635
	/**
636
	 * @var string ID to identify managed object
637
	 */
638
	public $element = 'bomline';
639
640
	/**
641
	 * @var string Name of table without prefix where object is stored
642
	 */
643
	public $table_element = 'bom_bomline';
644
645
	/**
646
	 * @var int  Does bomline support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
647
	 */
648
	public $ismultientitymanaged = 0;
649
650
	/**
651
	 * @var int  Does bomline support extrafields ? 0=No, 1=Yes
652
	 */
653
	public $isextrafieldmanaged = 1;
654
655
	/**
656
	 * @var string String with name of icon for bomline. Must be the part after the 'object_' into object_bomline.png
657
	 */
658
	public $picto = 'bomline@bom';
659
660
661
	/**
662
	 *  'type' if the field format.
663
	 *  'label' the translation key.
664
	 *  'enabled' is a condition when the field must be managed.
665
	 *  '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)
666
	 *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
667
	 *  'default' is a default value for creation (can still be replaced by the global setup of default values)
668
	 *  'index' if we want an index in database.
669
	 *  'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
670
	 *  'position' is the sort order of field.
671
	 *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.
672
	 *  '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).
673
	 *  'css' is the CSS style to use on field. For example: 'maxwidth200'
674
	 *  'help' is a string visible as a tooltip on field
675
	 *  'comment' is not used. You can store here any text of your choice. It is not used by application.
676
	 *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record
677
	 *  'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
678
	 */
679
680
	// BEGIN MODULEBUILDER PROPERTIES
681
	/**
682
	 * @var array  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
683
	 */
684
	public $fields=array(
685
		'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",),
686
		'description' => array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>60, 'notnull'=>-1,),
687
		'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000, 'notnull'=>-1,),
688
		'qty' => array('type'=>'double(24,8)', 'label'=>'Quantity', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'notnull'=>-1, 'isameasure'=>'1',),
689
		'fk_product' => array('type'=>'integer:Product:product/class/product.class.php', 'label'=>'Product', 'enabled'=>1, 'visible'=>1, 'position'=>20, 'notnull'=>-1, 'index'=>1,),
690
		'fk_bom' => array('type'=>'integer:BillOfMaterials:societe/class/bom.class.php', 'label'=>'BillOfMaterials', 'enabled'=>1, 'visible'=>1, 'position'=>10, 'notnull'=>-1, 'index'=>1,),
691
		'rank' => array('type'=>'integer', 'label'=>'Rank', 'enabled'=>1, 'visible'=>0, 'position'=>40, 'notnull'=>1,),
692
	);
693
	public $rowid;
694
	public $description;
695
	public $import_key;
696
	public $qty;
697
	public $fk_product;
698
	public $fk_bom;
699
	public $rank;
700
	// END MODULEBUILDER PROPERTIES
701
702
703
704
	// If this object has a subtable with lines
705
706
	/**
707
	 * @var int    Name of subtable line
708
	 */
709
	//public $table_element_line = 'bomlinedet';
710
711
	/**
712
	 * @var int    Field with ID of parent key if this field has a parent
713
	 */
714
	//public $fk_element = 'fk_bomline';
715
716
	/**
717
	 * @var int    Name of subtable class that manage subtable lines
718
	 */
719
	//public $class_element_line = 'BillOfMaterialsLineline';
720
721
	/**
722
	 * @var array  Array of child tables (child tables to delete before deleting a record)
723
	 */
724
	//protected $childtables=array('bomlinedet');
725
726
	/**
727
	 * @var BillOfMaterialsLineLine[]     Array of subtable lines
728
	 */
729
	//public $lines = array();
730
731
732
733
	/**
734
	 * Constructor
735
	 *
736
	 * @param DoliDb $db Database handler
737
	 */
738
	public function __construct(DoliDB $db)
739
	{
740
		global $conf, $langs, $user;
741
742
		$this->db = $db;
743
744
		if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible']=0;
745
		if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) $this->fields['entity']['enabled']=0;
746
747
		// Unset fields that are disabled
748
		foreach($this->fields as $key => $val)
749
		{
750
			if (isset($val['enabled']) && empty($val['enabled']))
751
			{
752
				unset($this->fields[$key]);
753
			}
754
		}
755
756
		// Translate some data of arrayofkeyval
757
		foreach($this->fields as $key => $val)
758
		{
759
			if (is_array($this->fields['status']['arrayofkeyval']))
760
			{
761
				foreach($this->fields['status']['arrayofkeyval'] as $key2 => $val2)
762
				{
763
					$this->fields['status']['arrayofkeyval'][$key2]=$langs->trans($val2);
764
				}
765
			}
766
		}
767
	}
768
769
	/**
770
	 * Create object into database
771
	 *
772
	 * @param  User $user      User that creates
773
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
774
	 * @return int             <0 if KO, Id of created object if OK
775
	 */
776
	public function create(User $user, $notrigger = false)
777
	{
778
		return $this->createCommon($user, $notrigger);
779
	}
780
781
	/**
782
	 * Clone an object into another one
783
	 *
784
	 * @param  	User 	$user      	User that creates
785
	 * @param  	int 	$fromid     Id of object to clone
786
	 * @return 	mixed 				New object created, <0 if KO
787
	 */
788
	public function createFromClone(User $user, $fromid)
789
	{
790
		global $langs, $hookmanager, $extrafields;
791
	    $error = 0;
792
793
	    dol_syslog(__METHOD__, LOG_DEBUG);
794
795
	    $object = new self($this->db);
796
797
	    $this->db->begin();
798
799
	    // Load source object
800
	    $object->fetchCommon($fromid);
801
	    // Reset some properties
802
	    unset($object->id);
803
	    unset($object->fk_user_creat);
804
	    unset($object->import_key);
805
806
	    // Clear fields
807
	    $object->ref = "copy_of_".$object->ref;
808
	    $object->title = $langs->trans("CopyOf")." ".$object->title;
809
	    // ...
810
	    // Clear extrafields that are unique
811
	    if (is_array($object->array_options) && count($object->array_options) > 0)
812
	    {
813
	    	$extrafields->fetch_name_optionals_label($this->element);
814
	    	foreach($object->array_options as $key => $option)
815
	    	{
816
	    		$shortkey = preg_replace('/options_/', '', $key);
817
	    		if (! empty($extrafields->attributes[$this->element]['unique'][$shortkey]))
818
	    		{
819
	    			//var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
820
	    			unset($object->array_options[$key]);
821
	    		}
822
	    	}
823
	    }
824
825
	    // Create clone
826
		$object->context['createfromclone'] = 'createfromclone';
827
	    $result = $object->createCommon($user);
828
	    if ($result < 0) {
829
	        $error++;
830
	        $this->error = $object->error;
831
	        $this->errors = $object->errors;
832
	    }
833
834
	    unset($object->context['createfromclone']);
835
836
	    // End
837
	    if (!$error) {
838
	        $this->db->commit();
839
	        return $object;
840
	    } else {
841
	        $this->db->rollback();
842
	        return -1;
843
	    }
844
	}
845
846
	/**
847
	 * Load object in memory from the database
848
	 *
849
	 * @param int    $id   Id object
850
	 * @param string $ref  Ref
851
	 * @return int         <0 if KO, 0 if not found, >0 if OK
852
	 */
853
	public function fetch($id, $ref = null)
854
	{
855
		$result = $this->fetchCommon($id, $ref);
856
		if ($result > 0 && ! empty($this->table_element_line)) $this->fetchLines();
0 ignored issues
show
Bug introduced by
The method fetchLines() does not seem to exist on object<BillOfMaterialsLine>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
857
		return $result;
858
	}
859
860
	/**
861
	 * Load object lines in memory from the database
862
	 *
863
	 * @return int         <0 if KO, 0 if not found, >0 if OK
864
	 */
865
	/*public function fetchLines()
866
	{
867
		$this->lines=array();
868
869
		// Load lines with object BillOfMaterialsLineLine
870
871
		return count($this->lines)?1:0;
872
	}*/
873
874
	/**
875
	 * Load list of objects in memory from the database.
876
	 *
877
	 * @param  string      $sortorder    Sort Order
878
	 * @param  string      $sortfield    Sort field
879
	 * @param  int         $limit        limit
880
	 * @param  int         $offset       Offset
881
	 * @param  array       $filter       Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
882
	 * @param  string      $filtermode   Filter mode (AND or OR)
883
	 * @return array|int                 int <0 if KO, array of pages if OK
884
	 */
885
	public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
886
	{
887
		global $conf;
888
889
		dol_syslog(__METHOD__, LOG_DEBUG);
890
891
		$records=array();
892
893
		$sql = 'SELECT';
894
		$sql .= ' t.rowid';
895
		// TODO Get all fields
896
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
897
		$sql .= ' WHERE t.entity = '.$conf->entity;
898
		// Manage filter
899
		$sqlwhere = array();
900
		if (count($filter) > 0) {
901
			foreach ($filter as $key => $value) {
902
				if ($key=='t.rowid') {
903
					$sqlwhere[] = $key . '='. $value;
904
				}
905
				elseif (strpos($key, 'date') !== false) {
906
					$sqlwhere[] = $key.' = \''.$this->db->idate($value).'\'';
907
				}
908
				elseif ($key=='customsql') {
909
					$sqlwhere[] = $value;
910
				}
911
				else {
912
					$sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
913
				}
914
			}
915
		}
916
		if (count($sqlwhere) > 0) {
917
			$sql .= ' AND (' . implode(' '.$filtermode.' ', $sqlwhere).')';
918
		}
919
920
		if (!empty($sortfield)) {
921
			$sql .= $this->db->order($sortfield, $sortorder);
922
		}
923
		if (!empty($limit)) {
924
			$sql .=  ' ' . $this->db->plimit($limit, $offset);
925
		}
926
927
		$resql = $this->db->query($sql);
928
		if ($resql) {
929
			$num = $this->db->num_rows($resql);
930
931
			while ($obj = $this->db->fetch_object($resql))
932
			{
933
				$record = new self($this->db);
934
935
				$record->id = $obj->rowid;
936
				// TODO Get other fields
937
938
				//var_dump($record->id);
939
				$records[$record->id] = $record;
940
			}
941
			$this->db->free($resql);
942
943
			return $records;
944
		} else {
945
			$this->errors[] = 'Error ' . $this->db->lasterror();
946
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
947
948
			return -1;
949
		}
950
	}
951
952
	/**
953
	 * Update object into database
954
	 *
955
	 * @param  User $user      User that modifies
956
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
957
	 * @return int             <0 if KO, >0 if OK
958
	 */
959
	public function update(User $user, $notrigger = false)
960
	{
961
		return $this->updateCommon($user, $notrigger);
962
	}
963
964
	/**
965
	 * Delete object in database
966
	 *
967
	 * @param User $user       User that deletes
968
	 * @param bool $notrigger  false=launch triggers after, true=disable triggers
969
	 * @return int             <0 if KO, >0 if OK
970
	 */
971
	public function delete(User $user, $notrigger = false)
972
	{
973
		return $this->deleteCommon($user, $notrigger);
974
		//return $this->deleteCommon($user, $notrigger, 1);
975
	}
976
977
	/**
978
	 *  Return a link to the object card (with optionaly the picto)
979
	 *
980
	 *	@param	int		$withpicto					Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
981
	 *	@param	string	$option						On what the link point to ('nolink', ...)
982
     *  @param	int  	$notooltip					1=Disable tooltip
983
     *  @param  string  $morecss            		Add more css on link
984
     *  @param  int     $save_lastsearch_value    	-1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
985
     *  @return	string								String with URL
986
     */
987
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
988
    {
989
        global $db, $conf, $langs, $hookmanager;
990
        global $dolibarr_main_authentication, $dolibarr_main_demo;
991
        global $menumanager;
992
993
        if (! empty($conf->dol_no_mouse_hover)) $notooltip=1;   // Force disable tooltips
994
995
        $result = '';
996
997
        $label = '<u>' . $langs->trans("BillOfMaterialsLine") . '</u>';
998
        $label.= '<br>';
999
        $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
1000
1001
        $url = dol_buildpath('/bom/bomline_card.php', 1).'?id='.$this->id;
1002
1003
        if ($option != 'nolink')
1004
        {
1005
	        // Add param to save lastsearch_values or not
1006
	        $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
1007
	        if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
1008
	        if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
1009
        }
1010
1011
        $linkclose='';
1012
        if (empty($notooltip))
1013
        {
1014
            if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
1015
            {
1016
                $label=$langs->trans("ShowBillOfMaterialsLine");
1017
                $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
1018
            }
1019
            $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
1020
            $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
1021
1022
            /*
1023
             $hookmanager->initHooks(array('bomlinedao'));
1024
             $parameters=array('id'=>$this->id);
1025
             $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action);    // Note that $action and $object may have been modified by some hooks
1026
             if ($reshook > 0) $linkclose = $hookmanager->resPrint;
1027
             */
1028
        }
1029
        else $linkclose = ($morecss?' class="'.$morecss.'"':'');
1030
1031
		$linkstart = '<a href="'.$url.'"';
1032
		$linkstart.=$linkclose.'>';
1033
		$linkend='</a>';
1034
1035
		$result .= $linkstart;
1036
		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);
1037
		if ($withpicto != 2) $result.= $this->ref;
1038
		$result .= $linkend;
1039
		//if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1040
1041
		global $action,$hookmanager;
1042
		$hookmanager->initHooks(array('bomlinedao'));
1043
		$parameters=array('id'=>$this->id, 'getnomurl'=>$result);
1044
		$reshook=$hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);    // Note that $action and $object may have been modified by some hooks
1045
		if ($reshook > 0) $result = $hookmanager->resPrint;
1046
		else $result .= $hookmanager->resPrint;
1047
1048
		return $result;
1049
    }
1050
1051
	/**
1052
	 *  Return label of the status
1053
	 *
1054
	 *  @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
1055
	 *  @return	string 			       Label of status
1056
	 */
1057
	public function getLibStatut($mode = 0)
1058
	{
1059
		return $this->LibStatut($this->status, $mode);
1060
	}
1061
1062
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1063
	/**
1064
	 *  Return the status
1065
	 *
1066
	 *  @param	int		$status        Id status
1067
	 *  @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
1068
	 *  @return string 			       Label of status
1069
	 */
1070
	public function LibStatut($status, $mode = 0)
1071
	{
1072
		// phpcs:enable
1073
		if (empty($this->labelstatus))
1074
		{
1075
			global $langs;
1076
			//$langs->load("mrp");
1077
			$this->labelstatus[1] = $langs->trans('Enabled');
1078
			$this->labelstatus[0] = $langs->trans('Disabled');
1079
		}
1080
1081
		if ($mode == 0)
1082
		{
1083
			return $this->labelstatus[$status];
1084
		}
1085
		elseif ($mode == 1)
1086
		{
1087
			return $this->labelstatus[$status];
1088
		}
1089
		elseif ($mode == 2)
1090
		{
1091
			if ($status == 1) return img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
1092
			elseif ($status == 0) return img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
1093
		}
1094
		elseif ($mode == 3)
1095
		{
1096
			if ($status == 1) return img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
1097
			elseif ($status == 0) return img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
1098
		}
1099
		elseif ($mode == 4)
1100
		{
1101
			if ($status == 1) return img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
1102
			elseif ($status == 0) return img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
1103
		}
1104
		elseif ($mode == 5)
1105
		{
1106
			if ($status == 1) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
1107
			elseif ($status == 0) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
1108
		}
1109
		elseif ($mode == 6)
1110
		{
1111
			if ($status == 1) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
1112
			elseif ($status == 0) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
1113
		}
1114
	}
1115
1116
	/**
1117
	 *	Load the info information in the object
1118
	 *
1119
	 *	@param  int		$id       Id of object
1120
	 *	@return	void
1121
	 */
1122
	public function info($id)
1123
	{
1124
		$sql = 'SELECT rowid, date_creation as datec, tms as datem,';
1125
		$sql.= ' fk_user_creat, fk_user_modif';
1126
		$sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
1127
		$sql.= ' WHERE t.rowid = '.$id;
1128
		$result=$this->db->query($sql);
1129
		if ($result)
1130
		{
1131
			if ($this->db->num_rows($result))
1132
			{
1133
				$obj = $this->db->fetch_object($result);
1134
				$this->id = $obj->rowid;
1135
				if ($obj->fk_user_author)
1136
				{
1137
					$cuser = new User($this->db);
1138
					$cuser->fetch($obj->fk_user_author);
1139
					$this->user_creation   = $cuser;
1140
				}
1141
1142
				if ($obj->fk_user_valid)
1143
				{
1144
					$vuser = new User($this->db);
1145
					$vuser->fetch($obj->fk_user_valid);
1146
					$this->user_validation = $vuser;
1147
				}
1148
1149
				if ($obj->fk_user_cloture)
1150
				{
1151
					$cluser = new User($this->db);
1152
					$cluser->fetch($obj->fk_user_cloture);
1153
					$this->user_cloture   = $cluser;
1154
				}
1155
1156
				$this->date_creation     = $this->db->jdate($obj->datec);
1157
				$this->date_modification = $this->db->jdate($obj->datem);
1158
				$this->date_validation   = $this->db->jdate($obj->datev);
1159
			}
1160
1161
			$this->db->free($result);
1162
		}
1163
		else
1164
		{
1165
			dol_print_error($this->db);
1166
		}
1167
	}
1168
1169
	/**
1170
	 * Initialise object with example values
1171
	 * Id must be 0 if object instance is a specimen
1172
	 *
1173
	 * @return void
1174
	 */
1175
	public function initAsSpecimen()
1176
	{
1177
		$this->initAsSpecimenCommon();
1178
	}
1179
1180
1181
	/**
1182
	 * Action executed by scheduler
1183
	 * CAN BE A CRON TASK. In such a case, parameters come from the schedule job setup field 'Parameters'
1184
	 *
1185
	 * @return  int	        0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
1186
	 */
1187
	//public function doScheduledJob($param1, $param2, ...)
1188
	public function doScheduledJob()
1189
	{
1190
		global $conf, $langs;
1191
1192
		//$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
1193
1194
		$error = 0;
1195
		$this->output = '';
1196
		$this->error='';
1197
1198
		dol_syslog(__METHOD__, LOG_DEBUG);
1199
1200
		$now = dol_now();
1201
1202
		$this->db->begin();
1203
1204
		// ...
1205
1206
		$this->db->commit();
1207
1208
		return $error;
1209
	}
1210
}
1211