asset.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file asset/class/asset.class.php
  20. * \ingroup asset
  21. * \brief This file is a CRUD class file for asset (Create/Read/Update/Delete)
  22. */
  23. require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
  24. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  25. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  26. /**
  27. * Class for Asset
  28. */
  29. class Asset extends CommonObject
  30. {
  31. /**
  32. * @var string ID to identify managed object
  33. */
  34. public $element = 'asset';
  35. /**
  36. * @var string Name of table without prefix where object is stored
  37. */
  38. public $table_element = 'asset';
  39. /**
  40. * @var int Does module support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  41. */
  42. public $ismultientitymanaged = 0;
  43. /**
  44. * @var int Does asset support extrafields ? 0=No, 1=Yes
  45. */
  46. public $isextrafieldmanaged = 1;
  47. /**
  48. * @var string String with name of icon for asset. Must be the part after the 'object_' into object_asset.png
  49. */
  50. public $picto = 'asset';
  51. /**
  52. * 'type' if the field format.
  53. * 'label' the translation key.
  54. * 'enabled' is a condition when the field must be managed.
  55. * '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)
  56. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  57. * 'index' if we want an index in database.
  58. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  59. * 'position' is the sort order of field.
  60. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  61. * '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).
  62. * 'help' is a string visible as a tooltip on field
  63. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  64. * 'default' is a default value for creation (can still be replaced by the global setup of default values)
  65. * 'showoncombobox' if field must be shown into the label of combobox
  66. */
  67. /**
  68. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  69. */
  70. public $fields=array(
  71. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'visible'=>-1, 'enabled'=>1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",),
  72. 'ref' => array('type'=>'varchar(10)', 'label'=>'Ref', 'visible'=>1, 'enabled'=>1, 'position'=>10, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object",),
  73. 'entity' => array('type'=>'integer', 'label'=>'Entity', 'visible'=>-1, 'enabled'=>1, 'position'=>20, 'notnull'=>1, 'index'=>1,),
  74. 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>-1, 'searchall'=>1, 'help'=>"Help text",),
  75. 'amount_ht' => array('type'=>'double(24,8)', 'label'=>'AmountHTShort', 'visible'=>1, 'enabled'=>1, 'position'=>40, 'notnull'=>-1, 'isameasure'=>'1', 'help'=>"Help text",),
  76. 'amount_vat' => array('type'=>'double(24,8)', 'label'=>'AmountVAT', 'visible'=>1, 'enabled'=>1, 'position'=>40, 'notnull'=>-1, 'isameasure'=>'1', 'help'=>"Help text",),
  77. 'fk_asset_type' => array('type'=>'integer:AssetType:asset/class/asset.class.php', 'label'=>'AssetsType', 'visible'=>1, 'enabled'=>1, 'position'=>50, 'notnull'=>-1, 'index'=>1, 'searchall'=>1, 'help'=>"LinkToThirparty",),
  78. 'description' => array('type'=>'text', 'label'=>'Description', 'visible'=>-1, 'enabled'=>1, 'position'=>90, 'notnull'=>-1,),
  79. 'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'visible'=>-1, 'enabled'=>1, 'position'=>91, 'notnull'=>-1,),
  80. 'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'visible'=>-1, 'enabled'=>1, 'position'=>92, 'notnull'=>-1,),
  81. 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
  82. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'visible'=>-2, 'enabled'=>1, 'position'=>501, 'notnull'=>1,),
  83. 'fk_user_creat' => array('type'=>'integer', 'label'=>'UserAuthor', 'visible'=>-2, 'enabled'=>1, 'position'=>510, 'notnull'=>1,),
  84. 'fk_user_modif' => array('type'=>'integer', 'label'=>'UserModif', 'visible'=>-2, 'enabled'=>1, 'position'=>511, 'notnull'=>-1,),
  85. 'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'visible'=>-2, 'enabled'=>1, 'position'=>1000, 'notnull'=>-1,),
  86. 'status' => array('type'=>'integer', 'label'=>'Status', 'visible'=>1, 'enabled'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Draft', '1'=>'Active', '-1'=>'Cancel')),
  87. );
  88. /**
  89. * @var int ID
  90. */
  91. public $rowid;
  92. /**
  93. * @var string Ref
  94. */
  95. public $ref;
  96. /**
  97. * @var int Entity
  98. */
  99. public $entity;
  100. /**
  101. * @var string Asset label
  102. */
  103. public $label;
  104. public $amount;
  105. /**
  106. * @var int Thirdparty ID
  107. */
  108. public $fk_soc;
  109. /**
  110. * @var string description
  111. */
  112. public $description;
  113. public $note_public;
  114. public $note_private;
  115. public $date_creation;
  116. public $tms;
  117. /**
  118. * @var int ID
  119. */
  120. public $fk_user_creat;
  121. /**
  122. * @var int ID
  123. */
  124. public $fk_user_modif;
  125. public $import_key;
  126. /**
  127. * @var int Status
  128. */
  129. public $status;
  130. // If this object has a subtable with lines
  131. /**
  132. * @var int Name of subtable line
  133. */
  134. //public $table_element_line = 'assetdet';
  135. /**
  136. * @var int Field with ID of parent key if this field has a parent
  137. */
  138. //public $fk_element = 'fk_asset';
  139. /**
  140. * @var int Name of subtable class that manage subtable lines
  141. */
  142. //public $class_element_line = 'Assetline';
  143. /**
  144. * @var array Array of child tables (child tables to delete before deleting a record)
  145. */
  146. //protected $childtables=array('assetdet');
  147. /**
  148. * @var AssetLine[] Array of subtable lines
  149. */
  150. //public $lines = array();
  151. /**
  152. * Constructor
  153. *
  154. * @param DoliDb $db Database handler
  155. */
  156. public function __construct(DoliDB $db)
  157. {
  158. global $conf;
  159. $this->db = $db;
  160. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) $this->fields['rowid']['visible']=0;
  161. if (empty($conf->multicompany->enabled)) $this->fields['entity']['enabled']=0;
  162. }
  163. /**
  164. * Create object into database
  165. *
  166. * @param User $user User that creates
  167. * @param bool $notrigger false=launch triggers after, true=disable triggers
  168. * @return int <0 if KO, Id of created object if OK
  169. */
  170. public function create(User $user, $notrigger = false)
  171. {
  172. return $this->createCommon($user, $notrigger);
  173. }
  174. /**
  175. * Clone and object into another one
  176. *
  177. * @param User $user User that creates
  178. * @param int $fromid Id of object to clone
  179. * @return mixed New object created, <0 if KO
  180. */
  181. public function createFromClone(User $user, $fromid)
  182. {
  183. global $hookmanager, $langs;
  184. $error = 0;
  185. dol_syslog(__METHOD__, LOG_DEBUG);
  186. $object = new self($this->db);
  187. $this->db->begin();
  188. // Load source object
  189. $object->fetchCommon($fromid);
  190. // Reset some properties
  191. unset($object->id);
  192. unset($object->fk_user_creat);
  193. unset($object->import_key);
  194. // Clear fields
  195. $object->ref = "copy_of_".$object->ref;
  196. $object->title = $langs->trans("CopyOf")." ".$object->title;
  197. // Create clone
  198. $object->context['createfromclone'] = 'createfromclone';
  199. $result = $object->createCommon($user);
  200. if ($result < 0) {
  201. $error++;
  202. $this->error = $object->error;
  203. $this->errors = $object->errors;
  204. }
  205. unset($object->context['createfromclone']);
  206. // End
  207. if (!$error) {
  208. $this->db->commit();
  209. return $object;
  210. } else {
  211. $this->db->rollback();
  212. return -1;
  213. }
  214. }
  215. /**
  216. * Load object in memory from the database
  217. *
  218. * @param int $id Id object
  219. * @param string $ref Ref
  220. * @return int <0 if KO, 0 if not found, >0 if OK
  221. */
  222. public function fetch($id, $ref = null)
  223. {
  224. $result = $this->fetchCommon($id, $ref);
  225. //if ($result > 0 && ! empty($this->table_element_line)) $this->fetchLines();
  226. return $result;
  227. }
  228. /**
  229. * Load object lines in memory from the database
  230. *
  231. * @return int <0 if KO, 0 if not found, >0 if OK
  232. */
  233. /*public function fetchLines()
  234. {
  235. $this->lines=array();
  236. // Load lines with object AssetsLine
  237. return count($this->lines)?1:0;
  238. }*/
  239. /**
  240. * Update object into database
  241. *
  242. * @param User $user User that modifies
  243. * @param bool $notrigger false=launch triggers after, true=disable triggers
  244. * @return int <0 if KO, >0 if OK
  245. */
  246. public function update(User $user, $notrigger = false)
  247. {
  248. return $this->updateCommon($user, $notrigger);
  249. }
  250. /**
  251. * Delete object in database
  252. *
  253. * @param User $user User that deletes
  254. * @param bool $notrigger false=launch triggers after, true=disable triggers
  255. * @return int <0 if KO, >0 if OK
  256. */
  257. public function delete(User $user, $notrigger = false)
  258. {
  259. return $this->deleteCommon($user, $notrigger);
  260. }
  261. /**
  262. * Return a link to the object card (with optionaly the picto)
  263. *
  264. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  265. * @param string $option On what the link point to ('nolink', ...)
  266. * @param int $notooltip 1=Disable tooltip
  267. * @param string $morecss Add more css on link
  268. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  269. * @return string String with URL
  270. */
  271. function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  272. {
  273. global $db, $conf, $langs;
  274. global $dolibarr_main_authentication, $dolibarr_main_demo;
  275. global $menumanager;
  276. if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
  277. $result = '';
  278. $companylink = '';
  279. $label = '<u>' . $langs->trans("Asset") . '</u>';
  280. $label.= '<br>';
  281. $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  282. $url = dol_buildpath('/assets/card.php',1).'?id='.$this->id;
  283. if ($option != 'nolink')
  284. {
  285. // Add param to save lastsearch_values or not
  286. $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
  287. if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
  288. if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
  289. }
  290. $linkclose='';
  291. if (empty($notooltip))
  292. {
  293. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  294. {
  295. $label=$langs->trans("ShowAssets");
  296. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  297. }
  298. $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
  299. $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
  300. }
  301. else $linkclose = ($morecss?' class="'.$morecss.'"':'');
  302. $linkstart = '<a href="'.$url.'"';
  303. $linkstart.=$linkclose.'>';
  304. $linkend='</a>';
  305. $result .= $linkstart;
  306. 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);
  307. if ($withpicto != 2) $result.= $this->ref;
  308. $result .= $linkend;
  309. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  310. return $result;
  311. }
  312. /**
  313. * Retourne le libelle du status d'un user (actif, inactif)
  314. *
  315. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  316. * @return string Label of status
  317. */
  318. function getLibStatut($mode = 0)
  319. {
  320. return $this->LibStatut($this->status,$mode);
  321. }
  322. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  323. /**
  324. * Return the status
  325. *
  326. * @param int $status Id status
  327. * @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
  328. * @return string Label of status
  329. */
  330. static function LibStatut($status, $mode = 0)
  331. {
  332. // phpcs:enable
  333. global $langs;
  334. if ($mode == 0 || $mode == 1)
  335. {
  336. if ($status == 1) return $langs->trans('Enabled');
  337. elseif ($status == 0) return $langs->trans('Disabled');
  338. }
  339. elseif ($mode == 2)
  340. {
  341. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  342. elseif ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  343. }
  344. elseif ($mode == 3)
  345. {
  346. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
  347. elseif ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
  348. }
  349. elseif ($mode == 4)
  350. {
  351. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  352. elseif ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  353. }
  354. elseif ($mode == 5)
  355. {
  356. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
  357. elseif ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
  358. }
  359. elseif ($mode == 6)
  360. {
  361. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
  362. elseif ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
  363. }
  364. }
  365. /**
  366. * Charge les informations d'ordre info dans l'objet commande
  367. *
  368. * @param int $id Id of order
  369. * @return void
  370. */
  371. function info($id)
  372. {
  373. $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
  374. $sql.= ' fk_user_creat, fk_user_modif';
  375. $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  376. $sql.= ' WHERE t.rowid = '.$id;
  377. $result=$this->db->query($sql);
  378. if ($result)
  379. {
  380. if ($this->db->num_rows($result))
  381. {
  382. $obj = $this->db->fetch_object($result);
  383. $this->id = $obj->rowid;
  384. if ($obj->fk_user_author)
  385. {
  386. $cuser = new User($this->db);
  387. $cuser->fetch($obj->fk_user_author);
  388. $this->user_creation = $cuser;
  389. }
  390. if ($obj->fk_user_valid)
  391. {
  392. $vuser = new User($this->db);
  393. $vuser->fetch($obj->fk_user_valid);
  394. $this->user_validation = $vuser;
  395. }
  396. if ($obj->fk_user_cloture)
  397. {
  398. $cluser = new User($this->db);
  399. $cluser->fetch($obj->fk_user_cloture);
  400. $this->user_cloture = $cluser;
  401. }
  402. $this->date_creation = $this->db->jdate($obj->datec);
  403. $this->date_modification = $this->db->jdate($obj->datem);
  404. $this->date_validation = $this->db->jdate($obj->datev);
  405. }
  406. $this->db->free($result);
  407. }
  408. else
  409. {
  410. dol_print_error($this->db);
  411. }
  412. }
  413. /**
  414. * Initialise object with example values
  415. * Id must be 0 if object instance is a specimen
  416. *
  417. * @return void
  418. */
  419. public function initAsSpecimen()
  420. {
  421. $this->initAsSpecimenCommon();
  422. }
  423. /**
  424. * Action executed by scheduler
  425. * CAN BE A CRON TASK
  426. *
  427. * @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
  428. */
  429. public function doScheduledJob()
  430. {
  431. global $conf, $langs;
  432. $this->output = '';
  433. $this->error='';
  434. dol_syslog(__METHOD__, LOG_DEBUG);
  435. return 0;
  436. }
  437. }