dolibarr  13.0.2
asset.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26 
30 class Asset extends CommonObject
31 {
35  public $element = 'asset';
36 
40  public $table_element = 'asset';
41 
45  public $ismultientitymanaged = 0;
46 
50  public $isextrafieldmanaged = 1;
51 
55  public $picto = 'asset';
56 
57 
58  const STATUS_DRAFT = 0;
59  const STATUS_VALIDATED = 1;
60 
61 
82  public $fields = array(
83  'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'visible'=>-1, 'enabled'=>1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",),
84  'ref' => array('type'=>'varchar(10)', 'label'=>'Ref', 'visible'=>1, 'enabled'=>1, 'position'=>10, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object", 'showoncombobox'=>1),
85  'entity' => array('type'=>'integer', 'label'=>'Entity', 'visible'=>0, 'enabled'=>1, 'position'=>20, 'notnull'=>1, 'index'=>1,),
86  'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>-1, 'searchall'=>1),
87  'fk_asset_type' => array('type'=>'integer:AssetType:asset/class/asset_type.class.php', 'label'=>'AssetsType', 'visible'=>1, 'enabled'=>1, 'position'=>35, 'notnull'=>1, 'index'=>1, 'searchall'=>1),
88  'amount_ht' => array('type'=>'double(24,8)', 'label'=>'AmountHTShort', 'visible'=>1, 'enabled'=>1, 'position'=>40, 'notnull'=>-1, 'isameasure'=>'1', 'help'=>"Help text",),
89  'amount_vat' => array('type'=>'double(24,8)', 'label'=>'AmountVAT', 'visible'=>1, 'enabled'=>1, 'position'=>41, 'notnull'=>-1, 'isameasure'=>'1', 'help'=>"Help text",),
90  'description' => array('type'=>'text', 'label'=>'Description', 'visible'=>-1, 'enabled'=>1, 'position'=>90, 'notnull'=>-1,),
91  'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'visible'=>-1, 'enabled'=>1, 'position'=>91, 'notnull'=>-1,),
92  'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'visible'=>-1, 'enabled'=>1, 'position'=>92, 'notnull'=>-1,),
93  'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
94  'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'visible'=>-2, 'enabled'=>1, 'position'=>501, 'notnull'=>1,),
95  'fk_user_creat' => array('type'=>'integer', 'label'=>'UserAuthor', 'visible'=>-2, 'enabled'=>1, 'position'=>510, 'notnull'=>1,),
96  'fk_user_modif' => array('type'=>'integer', 'label'=>'UserModif', 'visible'=>-2, 'enabled'=>1, 'position'=>511, 'notnull'=>-1,),
97  'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'visible'=>-2, 'enabled'=>1, 'position'=>1000, 'notnull'=>-1,),
98  'status' => array('type'=>'integer', 'label'=>'Status', 'visible'=>1, 'enabled'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Draft', '1'=>'Active', '-1'=>'Cancel')),
99  );
100 
104  public $rowid;
105 
109  public $ref;
110 
114  public $entity;
115 
119  public $label;
120 
121  public $amount;
122 
126  public $fk_soc;
127 
131  public $description;
132 
136  public $date_creation;
137 
138 
139  public $tms;
140 
144  public $fk_user_creat;
145 
149  public $fk_user_modif;
150 
154  public $import_key;
155 
159  public $status;
160 
161 
167  public function __construct(DoliDB $db)
168  {
169  global $conf;
170 
171  $this->db = $db;
172 
173  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) $this->fields['rowid']['visible'] = 0;
174  if (empty($conf->multicompany->enabled)) $this->fields['entity']['enabled'] = 0;
175  }
176 
184  public function create(User $user, $notrigger = false)
185  {
186  return $this->createCommon($user, $notrigger);
187  }
188 
196  public function createFromClone(User $user, $fromid)
197  {
198  global $hookmanager, $langs;
199  $error = 0;
200 
201  dol_syslog(__METHOD__, LOG_DEBUG);
202 
203  $object = new self($this->db);
204 
205  $this->db->begin();
206 
207  // Load source object
208  $object->fetchCommon($fromid);
209  // Reset some properties
210  unset($object->id);
211  unset($object->fk_user_creat);
212  unset($object->import_key);
213 
214  // Clear fields
215  $object->ref = "copy_of_".$object->ref;
216  $object->title = $langs->trans("CopyOf")." ".$object->title;
217 
218  // Create clone
219  $object->context['createfromclone'] = 'createfromclone';
220  $result = $object->createCommon($user);
221  if ($result < 0) {
222  $error++;
223  $this->error = $object->error;
224  $this->errors = $object->errors;
225  }
226 
227  unset($object->context['createfromclone']);
228 
229  // End
230  if (!$error) {
231  $this->db->commit();
232  return $object;
233  } else {
234  $this->db->rollback();
235  return -1;
236  }
237  }
238 
246  public function fetch($id, $ref = null)
247  {
248  $result = $this->fetchCommon($id, $ref);
249  //if ($result > 0 && ! empty($this->table_element_line)) $this->fetchLines();
250  return $result;
251  }
252 
258  /*public function fetchLines()
259  {
260  $this->lines=array();
261 
262  // Load lines with object AssetsLine
263 
264  return count($this->lines)?1:0;
265  }*/
266 
274  public function update(User $user, $notrigger = false)
275  {
276  return $this->updateCommon($user, $notrigger);
277  }
278 
286  public function delete(User $user, $notrigger = false)
287  {
288  return $this->deleteCommon($user, $notrigger);
289  }
290 
301  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
302  {
303  global $db, $conf, $langs;
304  global $dolibarr_main_authentication, $dolibarr_main_demo;
305  global $menumanager;
306 
307  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
308 
309  $result = '';
310  $companylink = '';
311 
312  $label = '<u>'.$langs->trans("Asset").'</u>';
313  $label .= '<br>';
314  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
315 
316  $url = dol_buildpath('/asset/card.php', 1).'?id='.$this->id;
317 
318  if ($option != 'nolink')
319  {
320  // Add param to save lastsearch_values or not
321  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
322  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
323  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
324  }
325 
326  $linkclose = '';
327  if (empty($notooltip))
328  {
329  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
330  {
331  $label = $langs->trans("ShowAssets");
332  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
333  }
334  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
335  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
336  } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
337 
338  $linkstart = '<a href="'.$url.'"';
339  $linkstart .= $linkclose.'>';
340  $linkend = '</a>';
341 
342  $result .= $linkstart;
343  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);
344  if ($withpicto != 2) $result .= $this->ref;
345  $result .= $linkend;
346  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
347 
348  return $result;
349  }
350 
357  public function getLibStatut($mode = 0)
358  {
359  return $this->LibStatut($this->status, $mode);
360  }
361 
362  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
370  public static function LibStatut($status, $mode = 0)
371  {
372  // phpcs:enable
373  global $langs;
374 
375  $langs->load("contracts");
376  $labelStatus = array();
377  $labelStatus[self::STATUS_DRAFT] = $langs->trans('Disabled');
378  $labelStatus[self::STATUS_VALIDATED] = $langs->trans('Enabled');
379  $labelStatusShort = array();
380  $labelStatusShort[self::STATUS_DRAFT] = $langs->trans('Disabled');
381  $labelStatusShort[self::STATUS_VALIDATED] = $langs->trans('Enabled');
382 
383  $statusType = 'status0';
384  if ($status == self::STATUS_VALIDATED) $statusType = 'status4';
385 
386  return dolGetStatus($labelStatus[$status], $labelStatusShort[$status], '', $statusType, $mode);
387  }
388 
395  public function info($id)
396  {
397  $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
398  $sql .= ' fk_user_creat, fk_user_modif';
399  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
400  $sql .= ' WHERE t.rowid = '.$id;
401  $result = $this->db->query($sql);
402  if ($result)
403  {
404  if ($this->db->num_rows($result))
405  {
406  $obj = $this->db->fetch_object($result);
407  $this->id = $obj->rowid;
408  if ($obj->fk_user_author)
409  {
410  $cuser = new User($this->db);
411  $cuser->fetch($obj->fk_user_author);
412  $this->user_creation = $cuser;
413  }
414 
415  if ($obj->fk_user_valid)
416  {
417  $vuser = new User($this->db);
418  $vuser->fetch($obj->fk_user_valid);
419  $this->user_validation = $vuser;
420  }
421 
422  if ($obj->fk_user_cloture)
423  {
424  $cluser = new User($this->db);
425  $cluser->fetch($obj->fk_user_cloture);
426  $this->user_cloture = $cluser;
427  }
428 
429  $this->date_creation = $this->db->jdate($obj->datec);
430  $this->date_modification = $this->db->jdate($obj->datem);
431  $this->date_validation = $this->db->jdate($obj->datev);
432  }
433 
434  $this->db->free($result);
435  } else {
436  dol_print_error($this->db);
437  }
438  }
439 
446  public function initAsSpecimen()
447  {
448  $this->initAsSpecimenCommon();
449  }
450 
451 
458  public function doScheduledJob()
459  {
460  global $conf, $langs;
461 
462  $this->output = '';
463  $this->error = '';
464 
465  dol_syslog(__METHOD__, LOG_DEBUG);
466 
467  return 0;
468  }
469 }
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
foreach($object->fields as $key=> $val) if(is_array($extrafields->attributes[$object->table_element]['label'])&&count($extrafields->attributes[$object->table_element]['label']) > 0) $object fields
Class to manage Dolibarr users.
Definition: user.class.php:44
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Class to manage Dolibarr database access.
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
createCommon(User $user, $notrigger=false)
Create object into database.
info($id)
Load info into asset object.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
$conf db
API class for accounts.
Definition: inc.php:54
fetch($id, $ref=null)
Load object in memory from the database.
getLibStatut($mode=0)
Retourne le libelle du status d&#39;un user (actif, inactif)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
__construct(DoliDB $db)
Constructor.
getNomUrl($withpicto=0, $option= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
updateCommon(User $user, $notrigger=false)
Update object into database.
print $_SERVER["PHP_SELF"]
Edit parameters.
update(User $user, $notrigger=false)
Load object lines in memory from the database.
Class for Asset.
Definition: asset.class.php:30
createFromClone(User $user, $fromid)
Clone and object into another one.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
doScheduledJob()
Action executed by scheduler CAN BE A CRON TASK.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
static LibStatut($status, $mode=0)
Return the status.
fetchCommon($id, $ref=null, $morewhere= '')
Load object in memory from the database.
create(User $user, $notrigger=false)
Create object into database.