accountingaccount.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2015 Ari Elbaz (elarifr) <github@accedinfo.com>
  7. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/accountancy/class/accountingaccount.class.php
  24. * \ingroup Accountancy (Double entries)
  25. * \brief File of class to manage accounting accounts
  26. */
  27. /**
  28. * Class to manage accounting accounts
  29. */
  30. class AccountingAccount extends CommonObject
  31. {
  32. /**
  33. * @var string Name of element
  34. */
  35. public $element='accounting_account';
  36. /**
  37. * @var string Name of table without prefix where object is stored
  38. */
  39. public $table_element='accounting_account';
  40. /**
  41. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  42. */
  43. public $picto = 'billr';
  44. /**
  45. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  46. * @var int
  47. */
  48. public $ismultientitymanaged = 1;
  49. /**
  50. * 0=Default, 1=View may be restricted to sales representative only if no permission to see all or to company of external user if external user
  51. * @var integer
  52. */
  53. public $restrictiononfksoc = 1;
  54. /**
  55. * @var DoliDB Database handler.
  56. */
  57. public $db;
  58. /**
  59. * @var int ID
  60. */
  61. public $id;
  62. /**
  63. * @var int ID
  64. */
  65. public $rowid;
  66. /**
  67. * Date creation record (datec)
  68. *
  69. * @var integer
  70. */
  71. public $datec;
  72. /**
  73. * @var string pcg version
  74. */
  75. public $fk_pcg_version;
  76. /**
  77. * @var string pcg type
  78. */
  79. public $pcg_type;
  80. /**
  81. * @var string pcg subtype
  82. */
  83. public $pcg_subtype;
  84. /**
  85. * @var string account number
  86. */
  87. public $account_number;
  88. /**
  89. * @var int ID parent account
  90. */
  91. public $account_parent;
  92. /**
  93. * @var int ID category account
  94. */
  95. public $account_category;
  96. /**
  97. * @var int Status
  98. */
  99. public $status;
  100. /**
  101. * @var string Label of account
  102. */
  103. public $label;
  104. /**
  105. * @var int ID
  106. */
  107. public $fk_user_author;
  108. /**
  109. * @var int ID
  110. */
  111. public $fk_user_modif;
  112. /**
  113. * @var int active (duplicate with status)
  114. */
  115. public $active;
  116. /**
  117. * Constructor
  118. *
  119. * @param DoliDB $db Database handle
  120. */
  121. public function __construct($db)
  122. {
  123. global $conf;
  124. $this->db = $db;
  125. $this->next_prev_filter='fk_pcg_version IN (SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS . ')'; // Used to add a filter in Form::showrefnav method
  126. }
  127. /**
  128. * Load record in memory
  129. *
  130. * @param int $rowid Id
  131. * @param string $account_number Account number
  132. * @param int|boolean $limittocurrentchart 1 or true=Load record only if it is into current active char of account
  133. * @param string $limittoachartaccount 'ABC'=Load record only if it is into chart account with code 'ABC'.
  134. * @return int <0 if KO, 0 if not found, Id of record if OK and found
  135. */
  136. public function fetch($rowid = null, $account_number = null, $limittocurrentchart = 0, $limittoachartaccount = '')
  137. {
  138. global $conf;
  139. if ($rowid || $account_number) {
  140. $sql = "SELECT a.rowid as rowid, a.datec, a.tms, a.fk_pcg_version, a.pcg_type, a.pcg_subtype, a.account_number, a.account_parent, a.label, a.fk_accounting_category, a.fk_user_author, a.fk_user_modif, a.active";
  141. $sql .= ", ca.label as category_label";
  142. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as a";
  143. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
  144. $sql .= " WHERE";
  145. if ($rowid) {
  146. $sql .= " a.rowid = " . (int) $rowid;
  147. } elseif ($account_number) {
  148. $sql .= " a.account_number = '" . $this->db->escape($account_number) . "'";
  149. }
  150. if (! empty($limittocurrentchart)) {
  151. $sql .= ' AND a.fk_pcg_version IN (SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $this->db->escape($conf->global->CHARTOFACCOUNTS) . ')';
  152. }
  153. if (! empty($limittoachartaccount)) {
  154. $sql .= " AND a.fk_pcg_version = '".$this->db->escape($limittoachartaccount)."'";
  155. }
  156. dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
  157. $result = $this->db->query($sql);
  158. if ($result) {
  159. $obj = $this->db->fetch_object($result);
  160. if ($obj) {
  161. $this->id = $obj->rowid;
  162. $this->rowid = $obj->rowid;
  163. $this->ref = $obj->account_number;
  164. $this->datec = $obj->datec;
  165. $this->tms = $obj->tms;
  166. $this->fk_pcg_version = $obj->fk_pcg_version;
  167. $this->pcg_type = $obj->pcg_type;
  168. $this->pcg_subtype = $obj->pcg_subtype;
  169. $this->account_number = $obj->account_number;
  170. $this->account_parent = $obj->account_parent;
  171. $this->label = $obj->label;
  172. $this->account_category = $obj->fk_accounting_category;
  173. $this->account_category_label = $obj->category_label;
  174. $this->fk_user_author = $obj->fk_user_author;
  175. $this->fk_user_modif = $obj->fk_user_modif;
  176. $this->active = $obj->active;
  177. $this->status = $obj->active;
  178. return $this->id;
  179. } else {
  180. return 0;
  181. }
  182. } else {
  183. $this->error = "Error " . $this->db->lasterror();
  184. $this->errors[] = "Error " . $this->db->lasterror();
  185. }
  186. }
  187. return -1;
  188. }
  189. /**
  190. * Insert new accounting account in chart of accounts
  191. *
  192. * @param User $user User making action
  193. * @param int $notrigger Disable triggers
  194. * @return int <0 if KO, >0 if OK
  195. */
  196. public function create($user, $notrigger = 0)
  197. {
  198. global $conf;
  199. $error = 0;
  200. $now = dol_now();
  201. // Clean parameters
  202. if (isset($this->fk_pcg_version))
  203. $this->fk_pcg_version = trim($this->fk_pcg_version);
  204. if (isset($this->pcg_type))
  205. $this->pcg_type = trim($this->pcg_type);
  206. if (isset($this->pcg_subtype))
  207. $this->pcg_subtype = trim($this->pcg_subtype);
  208. if (isset($this->account_number))
  209. $this->account_number = trim($this->account_number);
  210. if (isset($this->label))
  211. $this->label = trim($this->label);
  212. if (empty($this->pcg_type) || $this->pcg_type == '-1')
  213. {
  214. $this->pcg_type = 'XXXXXX';
  215. }
  216. if (empty($this->pcg_subtype) || $this->pcg_subtype == '-1')
  217. {
  218. $this->pcg_subtype = 'XXXXXX';
  219. }
  220. // Check parameters
  221. // Put here code to add control on parameters values
  222. // Insert request
  223. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_account(";
  224. $sql .= "datec";
  225. $sql .= ", entity";
  226. $sql .= ", fk_pcg_version";
  227. $sql .= ", pcg_type";
  228. $sql .= ", pcg_subtype";
  229. $sql .= ", account_number";
  230. $sql .= ", account_parent";
  231. $sql .= ", label";
  232. $sql .= ", fk_accounting_category";
  233. $sql .= ", fk_user_author";
  234. $sql .= ", active";
  235. $sql .= ") VALUES (";
  236. $sql .= " '" . $this->db->idate($now) . "'";
  237. $sql .= ", " . $conf->entity;
  238. $sql .= ", " . (empty($this->fk_pcg_version) ? 'NULL' : "'" . $this->db->escape($this->fk_pcg_version) . "'");
  239. $sql .= ", " . (empty($this->pcg_type) ? 'NULL' : "'" . $this->db->escape($this->pcg_type) . "'");
  240. $sql .= ", " . (empty($this->pcg_subtype) ? 'NULL' : "'" . $this->db->escape($this->pcg_subtype) . "'");
  241. $sql .= ", " . (empty($this->account_number) ? 'NULL' : "'" . $this->db->escape($this->account_number) . "'");
  242. $sql .= ", " . (empty($this->account_parent) ? 0 : (int) $this->account_parent);
  243. $sql .= ", " . (empty($this->label) ? "''" : "'" . $this->db->escape($this->label) . "'");
  244. $sql .= ", " . (empty($this->account_category) ? 0 : (int) $this->account_category);
  245. $sql .= ", " . $user->id;
  246. $sql .= ", " . (int) $this->active;
  247. $sql .= ")";
  248. $this->db->begin();
  249. dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG);
  250. $resql = $this->db->query($sql);
  251. if (! $resql) {
  252. $error++;
  253. $this->errors[] = "Error " . $this->db->lasterror();
  254. }
  255. if (! $error) {
  256. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "accounting_account");
  257. // if (! $notrigger) {
  258. // Uncomment this and change MYOBJECT to your own tag if you
  259. // want this action calls a trigger.
  260. // // Call triggers
  261. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  262. // $interface=new Interfaces($this->db);
  263. // $result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  264. // if ($result < 0) { $error++; $this->errors=$interface->errors; }
  265. // // End call triggers
  266. // }
  267. }
  268. // Commit or rollback
  269. if ($error) {
  270. foreach ($this->errors as $errmsg) {
  271. dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
  272. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  273. }
  274. $this->db->rollback();
  275. return -1 * $error;
  276. } else {
  277. $this->db->commit();
  278. return $this->id;
  279. }
  280. }
  281. /**
  282. * Update record
  283. *
  284. * @param User $user Use making update
  285. * @return int <0 if KO, >0 if OK
  286. */
  287. public function update($user)
  288. {
  289. // Check parameters
  290. if (empty($this->pcg_type) || $this->pcg_type == '-1')
  291. {
  292. $this->pcg_type = 'XXXXXX';
  293. }
  294. if (empty($this->pcg_subtype) || $this->pcg_subtype == '-1')
  295. {
  296. $this->pcg_subtype = 'XXXXXX';
  297. }
  298. $this->db->begin();
  299. $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
  300. $sql .= " SET fk_pcg_version = " . ($this->fk_pcg_version ? "'" . $this->db->escape($this->fk_pcg_version) . "'" : "null");
  301. $sql .= " , pcg_type = " . ($this->pcg_type ? "'" . $this->db->escape($this->pcg_type) . "'" : "null");
  302. $sql .= " , pcg_subtype = " . ($this->pcg_subtype ? "'" . $this->db->escape($this->pcg_subtype) . "'" : "null");
  303. $sql .= " , account_number = '" . $this->db->escape($this->account_number) . "'";
  304. $sql .= " , account_parent = " . (int) $this->account_parent;
  305. $sql .= " , label = " . ($this->label ? "'" . $this->db->escape($this->label) . "'" : "''");
  306. $sql .= " , fk_accounting_category = " . (empty($this->account_category) ? 0 : (int) $this->account_category);
  307. $sql .= " , fk_user_modif = " . $user->id;
  308. $sql .= " , active = " . (int) $this->active;
  309. $sql .= " WHERE rowid = " . $this->id;
  310. dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
  311. $result = $this->db->query($sql);
  312. if ($result) {
  313. $this->db->commit();
  314. return 1;
  315. } else {
  316. $this->error = $this->db->lasterror();
  317. $this->db->rollback();
  318. return - 1;
  319. }
  320. }
  321. /**
  322. * Check usage of accounting code
  323. *
  324. * @return int <0 if KO, >0 if OK
  325. */
  326. public function checkUsage()
  327. {
  328. global $langs;
  329. $sql = "(SELECT fk_code_ventilation FROM " . MAIN_DB_PREFIX . "facturedet";
  330. $sql.= " WHERE fk_code_ventilation=" . $this->id . ")";
  331. $sql.= "UNION";
  332. $sql.= " (SELECT fk_code_ventilation FROM " . MAIN_DB_PREFIX . "facture_fourn_det";
  333. $sql.= " WHERE fk_code_ventilation=" . $this->id . ")";
  334. dol_syslog(get_class($this) . "::checkUsage sql=" . $sql, LOG_DEBUG);
  335. $resql = $this->db->query($sql);
  336. if ($resql) {
  337. $num = $this->db->num_rows($resql);
  338. if ($num > 0) {
  339. $this->error = $langs->trans('ErrorAccountancyCodeIsAlreadyUse');
  340. return 0;
  341. } else {
  342. return 1;
  343. }
  344. } else {
  345. $this->error = $this->db->lasterror();
  346. return - 1;
  347. }
  348. }
  349. /**
  350. * Delete object in database
  351. *
  352. * @param User $user User that deletes
  353. * @param int $notrigger 0=triggers after, 1=disable triggers
  354. * @return int <0 if KO, >0 if OK
  355. */
  356. public function delete($user, $notrigger = 0)
  357. {
  358. $error = 0;
  359. $result = $this->checkUsage();
  360. if ($result > 0) {
  361. $this->db->begin();
  362. // if (! $error) {
  363. // if (! $notrigger) {
  364. // Uncomment this and change MYOBJECT to your own tag if you
  365. // want this action calls a trigger.
  366. // // Call triggers
  367. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  368. // $interface=new Interfaces($this->db);
  369. // $result=$interface->run_triggers('ACCOUNTANCY_ACCOUNT_DELETE',$this,$user,$langs,$conf);
  370. // if ($result < 0) { $error++; $this->errors=$interface->errors; }
  371. // // End call triggers
  372. // }
  373. // }
  374. if (! $error) {
  375. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "accounting_account";
  376. $sql .= " WHERE rowid=" . $this->id;
  377. dol_syslog(get_class($this) . "::delete sql=" . $sql);
  378. $resql = $this->db->query($sql);
  379. if (! $resql) {
  380. $error ++;
  381. $this->errors[] = "Error " . $this->db->lasterror();
  382. }
  383. }
  384. // Commit or rollback
  385. if ($error) {
  386. foreach ($this->errors as $errmsg) {
  387. dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
  388. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  389. }
  390. $this->db->rollback();
  391. return - 1 * $error;
  392. } else {
  393. $this->db->commit();
  394. return 1;
  395. }
  396. } else {
  397. return - 1;
  398. }
  399. }
  400. /**
  401. * Return clicable name (with picto eventually)
  402. *
  403. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  404. * @param int $withlabel 0=No label, 1=Include label of account
  405. * @param int $nourl 1=Disable url
  406. * @param string $moretitle Add more text to title tooltip
  407. * @param int $notooltip 1=Disable tooltip
  408. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  409. * @return string String with URL
  410. */
  411. public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1)
  412. {
  413. global $langs, $conf, $user;
  414. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  415. if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
  416. $result = '';
  417. $url = DOL_URL_ROOT . '/accountancy/admin/card.php?id=' . $this->id;
  418. // Add param to save lastsearch_values or not
  419. $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
  420. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
  421. if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
  422. $picto = 'billr';
  423. $label='';
  424. $label = '<u>' . $langs->trans("ShowAccountingAccount") . '</u>';
  425. if (! empty($this->account_number))
  426. $label .= '<br><b>'.$langs->trans('AccountAccounting') . ':</b> ' . length_accountg($this->account_number);
  427. if (! empty($this->label))
  428. $label .= '<br><b>'.$langs->trans('Label') . ':</b> ' . $this->label;
  429. if ($moretitle) $label.=' - '.$moretitle;
  430. $linkclose='';
  431. if (empty($notooltip))
  432. {
  433. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  434. {
  435. $label=$langs->trans("ShowAccoutingAccount");
  436. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  437. }
  438. $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
  439. $linkclose.=' class="classfortooltip"';
  440. }
  441. $linkstart='<a href="'.$url.'"';
  442. $linkstart.=$linkclose.'>';
  443. $linkend='</a>';
  444. if ($nourl)
  445. {
  446. $linkstart = '';
  447. $linkclose = '';
  448. $linkend = '';
  449. }
  450. $label_link = length_accountg($this->account_number);
  451. if ($withlabel) $label_link .= ' - ' . $this->label;
  452. if ($withpicto) $result.=($linkstart.img_object(($notooltip?'':$label), $picto, ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend);
  453. if ($withpicto && $withpicto != 2) $result .= ' ';
  454. if ($withpicto != 2) $result.=$linkstart . $label_link . $linkend;
  455. return $result;
  456. }
  457. /**
  458. * Information on record
  459. *
  460. * @param int $id of record
  461. * @return void
  462. */
  463. public function info($id)
  464. {
  465. $sql = 'SELECT a.rowid, a.datec, a.fk_user_author, a.fk_user_modif, a.tms';
  466. $sql .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as a';
  467. $sql .= ' WHERE a.rowid = ' . $id;
  468. dol_syslog(get_class($this) . '::info sql=' . $sql);
  469. $result = $this->db->query($sql);
  470. if ($result) {
  471. if ($this->db->num_rows($result)) {
  472. $obj = $this->db->fetch_object($result);
  473. $this->id = $obj->rowid;
  474. if ($obj->fk_user_author) {
  475. $cuser = new User($this->db);
  476. $cuser->fetch($obj->fk_user_author);
  477. $this->user_creation = $cuser;
  478. }
  479. if ($obj->fk_user_modif) {
  480. $muser = new User($this->db);
  481. $muser->fetch($obj->fk_user_modif);
  482. $this->user_modification = $muser;
  483. }
  484. $this->date_creation = $this->db->jdate($obj->datec);
  485. $this->date_modification = $this->db->jdate($obj->tms);
  486. }
  487. $this->db->free($result);
  488. } else {
  489. dol_print_error($this->db);
  490. }
  491. }
  492. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  493. /**
  494. * Account deactivated
  495. *
  496. * @param int $id Id
  497. * @return int <0 if KO, >0 if OK
  498. */
  499. public function account_desactivate($id)
  500. {
  501. // phpcs:enable
  502. $result = $this->checkUsage();
  503. if ($result > 0) {
  504. $this->db->begin();
  505. $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
  506. $sql .= "SET active = '0'";
  507. $sql .= " WHERE rowid = " . $this->db->escape($id);
  508. dol_syslog(get_class($this) . "::desactivate sql=" . $sql, LOG_DEBUG);
  509. $result = $this->db->query($sql);
  510. if ($result) {
  511. $this->db->commit();
  512. return 1;
  513. } else {
  514. $this->error = $this->db->lasterror();
  515. $this->db->rollback();
  516. return - 1;
  517. }
  518. } else {
  519. return - 1;
  520. }
  521. }
  522. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  523. /**
  524. * Account activated
  525. *
  526. * @param int $id Id
  527. * @return int <0 if KO, >0 if OK
  528. */
  529. public function account_activate($id)
  530. {
  531. // phpcs:enable
  532. $this->db->begin();
  533. $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
  534. $sql .= "SET active = '1'";
  535. $sql .= " WHERE rowid = " . $this->db->escape($id);
  536. dol_syslog(get_class($this) . "::activate sql=" . $sql, LOG_DEBUG);
  537. $result = $this->db->query($sql);
  538. if ($result) {
  539. $this->db->commit();
  540. return 1;
  541. } else {
  542. $this->error = $this->db->lasterror();
  543. $this->db->rollback();
  544. return - 1;
  545. }
  546. }
  547. /**
  548. * Retourne le libelle du statut d'un user (actif, inactif)
  549. *
  550. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  551. * @return string Label of status
  552. */
  553. public function getLibStatut($mode = 0)
  554. {
  555. return $this->LibStatut($this->status, $mode);
  556. }
  557. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  558. /**
  559. * Renvoi le libelle d'un statut donne
  560. *
  561. * @param int $statut Id statut
  562. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  563. * @return string Label of status
  564. */
  565. public function LibStatut($statut, $mode = 0)
  566. {
  567. // phpcs:enable
  568. global $langs;
  569. $langs->loadLangs(array("users"));
  570. if ($mode == 0)
  571. {
  572. $prefix='';
  573. if ($statut == 1) return $langs->trans('Enabled');
  574. elseif ($statut == 0) return $langs->trans('Disabled');
  575. }
  576. elseif ($mode == 1)
  577. {
  578. if ($statut == 1) return $langs->trans('Enabled');
  579. elseif ($statut == 0) return $langs->trans('Disabled');
  580. }
  581. elseif ($mode == 2)
  582. {
  583. if ($statut == 1) return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
  584. elseif ($statut == 0) return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
  585. }
  586. elseif ($mode == 3)
  587. {
  588. if ($statut == 1) return img_picto($langs->trans('Enabled'), 'statut4');
  589. elseif ($statut == 0) return img_picto($langs->trans('Disabled'), 'statut5');
  590. }
  591. elseif ($mode == 4)
  592. {
  593. if ($statut == 1) return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
  594. elseif ($statut == 0) return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
  595. }
  596. elseif ($mode == 5)
  597. {
  598. if ($statut == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
  599. elseif ($statut == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
  600. }
  601. }
  602. }