asset.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018 Alexandre Spangaro <aspangaro@zendsi.com>
  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. public $entity;
  97. /**
  98. * @var string Asset label
  99. */
  100. public $label;
  101. public $amount;
  102. public $fk_soc;
  103. /**
  104. * @var string description
  105. */
  106. public $description;
  107. public $note_public;
  108. public $note_private;
  109. public $date_creation;
  110. public $tms;
  111. public $fk_user_creat;
  112. public $fk_user_modif;
  113. public $import_key;
  114. public $status;
  115. // If this object has a subtable with lines
  116. /**
  117. * @var int Name of subtable line
  118. */
  119. //public $table_element_line = 'assetdet';
  120. /**
  121. * @var int Field with ID of parent key if this field has a parent
  122. */
  123. //public $fk_element = 'fk_asset';
  124. /**
  125. * @var int Name of subtable class that manage subtable lines
  126. */
  127. //public $class_element_line = 'Assetline';
  128. /**
  129. * @var array Array of child tables (child tables to delete before deleting a record)
  130. */
  131. //protected $childtables=array('assetdet');
  132. /**
  133. * @var AssetLine[] Array of subtable lines
  134. */
  135. //public $lines = array();
  136. /**
  137. * Constructor
  138. *
  139. * @param DoliDb $db Database handler
  140. */
  141. public function __construct(DoliDB $db)
  142. {
  143. global $conf;
  144. $this->db = $db;
  145. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) $this->fields['rowid']['visible']=0;
  146. if (empty($conf->multicompany->enabled)) $this->fields['entity']['enabled']=0;
  147. }
  148. /**
  149. * Create object into database
  150. *
  151. * @param User $user User that creates
  152. * @param bool $notrigger false=launch triggers after, true=disable triggers
  153. * @return int <0 if KO, Id of created object if OK
  154. */
  155. public function create(User $user, $notrigger = false)
  156. {
  157. return $this->createCommon($user, $notrigger);
  158. }
  159. /**
  160. * Clone and object into another one
  161. *
  162. * @param User $user User that creates
  163. * @param int $fromid Id of object to clone
  164. * @return mixed New object created, <0 if KO
  165. */
  166. public function createFromClone(User $user, $fromid)
  167. {
  168. global $hookmanager, $langs;
  169. $error = 0;
  170. dol_syslog(__METHOD__, LOG_DEBUG);
  171. $object = new self($this->db);
  172. $this->db->begin();
  173. // Load source object
  174. $object->fetchCommon($fromid);
  175. // Reset some properties
  176. unset($object->id);
  177. unset($object->fk_user_creat);
  178. unset($object->import_key);
  179. // Clear fields
  180. $object->ref = "copy_of_".$object->ref;
  181. $object->title = $langs->trans("CopyOf")." ".$object->title;
  182. // Create clone
  183. $object->context['createfromclone'] = 'createfromclone';
  184. $result = $object->createCommon($user);
  185. if ($result < 0) {
  186. $error++;
  187. $this->error = $object->error;
  188. $this->errors = $object->errors;
  189. }
  190. // End
  191. if (!$error) {
  192. $this->db->commit();
  193. return $object;
  194. } else {
  195. $this->db->rollback();
  196. return -1;
  197. }
  198. }
  199. /**
  200. * Load object in memory from the database
  201. *
  202. * @param int $id Id object
  203. * @param string $ref Ref
  204. * @return int <0 if KO, 0 if not found, >0 if OK
  205. */
  206. public function fetch($id, $ref = null)
  207. {
  208. $result = $this->fetchCommon($id, $ref);
  209. //if ($result > 0 && ! empty($this->table_element_line)) $this->fetchLines();
  210. return $result;
  211. }
  212. /**
  213. * Load object lines in memory from the database
  214. *
  215. * @return int <0 if KO, 0 if not found, >0 if OK
  216. */
  217. /*public function fetchLines()
  218. {
  219. $this->lines=array();
  220. // Load lines with object AssetsLine
  221. return count($this->lines)?1:0;
  222. }*/
  223. /**
  224. * Update object into database
  225. *
  226. * @param User $user User that modifies
  227. * @param bool $notrigger false=launch triggers after, true=disable triggers
  228. * @return int <0 if KO, >0 if OK
  229. */
  230. public function update(User $user, $notrigger = false)
  231. {
  232. return $this->updateCommon($user, $notrigger);
  233. }
  234. /**
  235. * Delete object in database
  236. *
  237. * @param User $user User that deletes
  238. * @param bool $notrigger false=launch triggers after, true=disable triggers
  239. * @return int <0 if KO, >0 if OK
  240. */
  241. public function delete(User $user, $notrigger = false)
  242. {
  243. return $this->deleteCommon($user, $notrigger);
  244. }
  245. /**
  246. * Return a link to the object card (with optionaly the picto)
  247. *
  248. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  249. * @param string $option On what the link point to ('nolink', ...)
  250. * @param int $notooltip 1=Disable tooltip
  251. * @param string $morecss Add more css on link
  252. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  253. * @return string String with URL
  254. */
  255. function getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
  256. {
  257. global $db, $conf, $langs;
  258. global $dolibarr_main_authentication, $dolibarr_main_demo;
  259. global $menumanager;
  260. if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
  261. $result = '';
  262. $companylink = '';
  263. $label = '<u>' . $langs->trans("Asset") . '</u>';
  264. $label.= '<br>';
  265. $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  266. $url = dol_buildpath('/assets/card.php',1).'?id='.$this->id;
  267. if ($option != 'nolink')
  268. {
  269. // Add param to save lastsearch_values or not
  270. $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
  271. if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
  272. if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
  273. }
  274. $linkclose='';
  275. if (empty($notooltip))
  276. {
  277. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  278. {
  279. $label=$langs->trans("ShowAssets");
  280. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  281. }
  282. $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
  283. $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
  284. }
  285. else $linkclose = ($morecss?' class="'.$morecss.'"':'');
  286. $linkstart = '<a href="'.$url.'"';
  287. $linkstart.=$linkclose.'>';
  288. $linkend='</a>';
  289. $result .= $linkstart;
  290. 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);
  291. if ($withpicto != 2) $result.= $this->ref;
  292. $result .= $linkend;
  293. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  294. return $result;
  295. }
  296. /**
  297. * Retourne le libelle du status d'un user (actif, inactif)
  298. *
  299. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  300. * @return string Label of status
  301. */
  302. function getLibStatut($mode=0)
  303. {
  304. return $this->LibStatut($this->status,$mode);
  305. }
  306. /**
  307. * Return the status
  308. *
  309. * @param int $status Id status
  310. * @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
  311. * @return string Label of status
  312. */
  313. // phpcs:ignore PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  314. static function LibStatut($status,$mode=0)
  315. {
  316. global $langs;
  317. if ($mode == 0)
  318. {
  319. $prefix='';
  320. if ($status == 1) return $langs->trans('Enabled');
  321. if ($status == 0) return $langs->trans('Disabled');
  322. }
  323. if ($mode == 1)
  324. {
  325. if ($status == 1) return $langs->trans('Enabled');
  326. if ($status == 0) return $langs->trans('Disabled');
  327. }
  328. if ($mode == 2)
  329. {
  330. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  331. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  332. }
  333. if ($mode == 3)
  334. {
  335. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
  336. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
  337. }
  338. if ($mode == 4)
  339. {
  340. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  341. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  342. }
  343. if ($mode == 5)
  344. {
  345. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
  346. if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
  347. }
  348. if ($mode == 6)
  349. {
  350. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
  351. if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
  352. }
  353. }
  354. /**
  355. * Charge les informations d'ordre info dans l'objet commande
  356. *
  357. * @param int $id Id of order
  358. * @return void
  359. */
  360. function info($id)
  361. {
  362. $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
  363. $sql.= ' fk_user_creat, fk_user_modif';
  364. $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  365. $sql.= ' WHERE t.rowid = '.$id;
  366. $result=$this->db->query($sql);
  367. if ($result)
  368. {
  369. if ($this->db->num_rows($result))
  370. {
  371. $obj = $this->db->fetch_object($result);
  372. $this->id = $obj->rowid;
  373. if ($obj->fk_user_author)
  374. {
  375. $cuser = new User($this->db);
  376. $cuser->fetch($obj->fk_user_author);
  377. $this->user_creation = $cuser;
  378. }
  379. if ($obj->fk_user_valid)
  380. {
  381. $vuser = new User($this->db);
  382. $vuser->fetch($obj->fk_user_valid);
  383. $this->user_validation = $vuser;
  384. }
  385. if ($obj->fk_user_cloture)
  386. {
  387. $cluser = new User($this->db);
  388. $cluser->fetch($obj->fk_user_cloture);
  389. $this->user_cloture = $cluser;
  390. }
  391. $this->date_creation = $this->db->jdate($obj->datec);
  392. $this->date_modification = $this->db->jdate($obj->datem);
  393. $this->date_validation = $this->db->jdate($obj->datev);
  394. }
  395. $this->db->free($result);
  396. }
  397. else
  398. {
  399. dol_print_error($this->db);
  400. }
  401. }
  402. /**
  403. * Initialise object with example values
  404. * Id must be 0 if object instance is a specimen
  405. *
  406. * @return void
  407. */
  408. public function initAsSpecimen()
  409. {
  410. $this->initAsSpecimenCommon();
  411. }
  412. /**
  413. * Action executed by scheduler
  414. * CAN BE A CRON TASK
  415. *
  416. * @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
  417. */
  418. public function doScheduledJob()
  419. {
  420. global $conf, $langs;
  421. $this->output = '';
  422. $this->error='';
  423. dol_syslog(__METHOD__, LOG_DEBUG);
  424. return 0;
  425. }
  426. }