asset.class.php 15 KB

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