cchargesociales.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. <?php
  2. /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/sociales/class/cchargesociales.class.php
  22. * \ingroup tax
  23. * \brief File to manage type of social/fiscal taxes
  24. */
  25. // Put here all includes required by your class file
  26. //require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
  27. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  28. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  29. /**
  30. * Class Cchargesociales
  31. */
  32. class Cchargesociales
  33. {
  34. /**
  35. * @var string Id to identify managed objects
  36. */
  37. public $element = 'cchargesociales';
  38. /**
  39. * @var string Name of table without prefix where object is stored
  40. */
  41. public $table_element = 'c_chargesociales';
  42. /**
  43. * @var string Label
  44. * @deprecated
  45. */
  46. public $libelle;
  47. /**
  48. * @var string Label
  49. */
  50. public $label;
  51. public $deductible;
  52. public $active;
  53. public $code;
  54. /**
  55. * @var int ID
  56. */
  57. public $fk_pays;
  58. /**
  59. * @var string module
  60. */
  61. public $module;
  62. public $accountancy_code;
  63. /**
  64. * Constructor
  65. *
  66. * @param DoliDb $db Database handler
  67. */
  68. public function __construct(DoliDB $db)
  69. {
  70. $this->db = $db;
  71. }
  72. /**
  73. * Create object into database
  74. *
  75. * @param User $user User that creates
  76. * @param bool $notrigger false=launch triggers after, true=disable triggers
  77. *
  78. * @return int <0 if KO, Id of created object if OK
  79. */
  80. public function create(User $user, $notrigger = false)
  81. {
  82. dol_syslog(__METHOD__, LOG_DEBUG);
  83. $error = 0;
  84. // Clean parameters
  85. $this->trimParameters(
  86. array(
  87. 'libelle',
  88. 'deductible',
  89. 'active',
  90. 'code',
  91. 'fk_pays',
  92. 'module',
  93. 'accountancy_code',
  94. )
  95. );
  96. // Check parameters
  97. // Put here code to add control on parameters values
  98. // Insert request
  99. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
  100. $sql .= 'libelle,';
  101. $sql .= 'deductible,';
  102. $sql .= 'active,';
  103. $sql .= 'code,';
  104. $sql .= 'fk_pays,';
  105. $sql .= 'module';
  106. $sql .= 'accountancy_code';
  107. $sql .= ') VALUES (';
  108. $sql .= ' '.(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").',';
  109. $sql .= ' '.(!isset($this->deductible) ? 'NULL' : $this->deductible).',';
  110. $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active).',';
  111. $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
  112. $sql .= ' '.(!isset($this->fk_pays) ? 'NULL' : $this->fk_pays).',';
  113. $sql .= ' '.(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'").',';
  114. $sql .= ' '.(!isset($this->accountancy_code) ? 'NULL' : "'".$this->db->escape($this->accountancy_code)."'");
  115. $sql .= ')';
  116. $this->db->begin();
  117. $resql = $this->db->query($sql);
  118. if (!$resql) {
  119. $error++;
  120. $this->errors[] = 'Error '.$this->db->lasterror();
  121. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  122. }
  123. if (!$error) {
  124. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  125. //if (!$notrigger) {
  126. // Uncomment this and change MYOBJECT to your own tag if you
  127. // want this action to call a trigger.
  128. //// Call triggers
  129. //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
  130. //if ($result < 0) $error++;
  131. //// End call triggers
  132. //}
  133. }
  134. // Commit or rollback
  135. if ($error) {
  136. $this->db->rollback();
  137. return -1 * $error;
  138. } else {
  139. $this->db->commit();
  140. return $this->id;
  141. }
  142. }
  143. /**
  144. * Load object in memory from the database
  145. *
  146. * @param int $id Id object
  147. * @param string $ref Ref
  148. *
  149. * @return int <0 if KO, 0 if not found, >0 if OK
  150. */
  151. public function fetch($id, $ref = null)
  152. {
  153. dol_syslog(__METHOD__, LOG_DEBUG);
  154. $sql = 'SELECT';
  155. $sql .= " t.id,";
  156. $sql .= " t.libelle as label,";
  157. $sql .= " t.deductible,";
  158. $sql .= " t.active,";
  159. $sql .= " t.code,";
  160. $sql .= " t.fk_pays,";
  161. $sql .= " t.module,";
  162. $sql .= " t.accountancy_code";
  163. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  164. if (null !== $ref) {
  165. $sql .= " WHERE t.code = '".$this->db->escape($ref)."'";
  166. } else {
  167. $sql .= ' WHERE t.id = '.((int) $id);
  168. }
  169. $resql = $this->db->query($sql);
  170. if ($resql) {
  171. $numrows = $this->db->num_rows($resql);
  172. if ($numrows) {
  173. $obj = $this->db->fetch_object($resql);
  174. $this->id = $obj->id;
  175. $this->libelle = $obj->label;
  176. $this->label = $obj->label;
  177. $this->deductible = $obj->deductible;
  178. $this->active = $obj->active;
  179. $this->code = $obj->code;
  180. $this->fk_pays = $obj->fk_pays;
  181. $this->module = $obj->module;
  182. $this->accountancy_code = $obj->accountancy_code;
  183. }
  184. $this->db->free($resql);
  185. if ($numrows) {
  186. return 1;
  187. } else {
  188. return 0;
  189. }
  190. } else {
  191. $this->errors[] = 'Error '.$this->db->lasterror();
  192. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  193. return -1;
  194. }
  195. }
  196. /**
  197. * Update object into database
  198. *
  199. * @param User $user User that modifies
  200. * @param bool $notrigger false=launch triggers after, true=disable triggers
  201. *
  202. * @return int <0 if KO, >0 if OK
  203. */
  204. public function update(User $user, $notrigger = false)
  205. {
  206. $error = 0;
  207. dol_syslog(__METHOD__, LOG_DEBUG);
  208. // Clean parameters
  209. $this->trimParameters(
  210. array(
  211. 'libelle',
  212. 'deductible',
  213. 'active',
  214. 'code',
  215. 'fk_pays',
  216. 'module',
  217. 'accountancy_code',
  218. )
  219. );
  220. // Check parameters
  221. // Put here code to add a control on parameters values
  222. // Update request
  223. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
  224. $sql .= ' libelle = '.(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").',';
  225. $sql .= ' deductible = '.(isset($this->deductible) ? ((int) $this->deductible) : "null").',';
  226. $sql .= ' active = '.(isset($this->active) ? ((int) $this->active) : "null").',';
  227. $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
  228. $sql .= ' fk_pays = '.((isset($this->fk_pays) && $this->fk_pays > 0) ? ((int) $this->fk_pays) : "null").',';
  229. $sql .= ' module = '.(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null").',';
  230. $sql .= ' accountancy_code = '.(isset($this->accountancy_code) ? "'".$this->db->escape($this->accountancy_code)."'" : "null");
  231. $sql .= ' WHERE id='.((int) $this->id);
  232. $this->db->begin();
  233. $resql = $this->db->query($sql);
  234. if (!$resql) {
  235. $error++;
  236. $this->errors[] = 'Error '.$this->db->lasterror();
  237. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  238. }
  239. //if (!$error && !$notrigger) {
  240. // Uncomment this and change MYOBJECT to your own tag if you
  241. // want this action calls a trigger.
  242. //// Call triggers
  243. //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  244. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  245. //// End call triggers
  246. //}
  247. // Commit or rollback
  248. if ($error) {
  249. $this->db->rollback();
  250. return -1 * $error;
  251. } else {
  252. $this->db->commit();
  253. return 1;
  254. }
  255. }
  256. /**
  257. * Delete object in database
  258. *
  259. * @param User $user User that deletes
  260. * @param bool $notrigger false=launch triggers after, true=disable triggers
  261. *
  262. * @return int <0 if KO, >0 if OK
  263. */
  264. public function delete(User $user, $notrigger = false)
  265. {
  266. dol_syslog(__METHOD__, LOG_DEBUG);
  267. $error = 0;
  268. $this->db->begin();
  269. //if (!$error) {
  270. //if (!$notrigger) {
  271. // Uncomment this and change MYOBJECT to your own tag if you
  272. // want this action calls a trigger.
  273. //// Call triggers
  274. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  275. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  276. //// End call triggers
  277. //}
  278. //}
  279. if (!$error) {
  280. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
  281. $sql .= ' WHERE id = '.((int) $this->id);
  282. $resql = $this->db->query($sql);
  283. if (!$resql) {
  284. $error++;
  285. $this->errors[] = 'Error '.$this->db->lasterror();
  286. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  287. }
  288. }
  289. // Commit or rollback
  290. if ($error) {
  291. $this->db->rollback();
  292. return -1 * $error;
  293. } else {
  294. $this->db->commit();
  295. return 1;
  296. }
  297. }
  298. /**
  299. * Load an object from its id and create a new one in database
  300. *
  301. * @param User $user User making the clone
  302. * @param int $fromid Id of object to clone
  303. * @return int New id of clone
  304. */
  305. public function createFromClone(User $user, $fromid)
  306. {
  307. dol_syslog(__METHOD__, LOG_DEBUG);
  308. $error = 0;
  309. $object = new Cchargesociales($this->db);
  310. $this->db->begin();
  311. // Load source object
  312. $object->fetch($fromid);
  313. // Reset object
  314. $object->id = 0;
  315. // Clear fields
  316. // ...
  317. // Create clone
  318. $this->context['createfromclone'] = 'createfromclone';
  319. $result = $object->create($user);
  320. // Other options
  321. if ($result < 0) {
  322. $error++;
  323. $this->errors = $object->errors;
  324. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  325. }
  326. unset($this->context['createfromclone']);
  327. // End
  328. if (!$error) {
  329. $this->db->commit();
  330. return $object->id;
  331. } else {
  332. $this->db->rollback();
  333. return -1;
  334. }
  335. }
  336. /**
  337. * Return a link to the user card (with optionaly the picto)
  338. * Use this->id,this->lastname, this->firstname
  339. *
  340. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  341. * @param string $option On what the link point to
  342. * @param integer $notooltip 1=Disable tooltip
  343. * @param int $maxlen Max length of visible user name
  344. * @param string $morecss Add more css on link
  345. * @return string String with URL
  346. */
  347. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
  348. {
  349. global $langs, $conf, $db;
  350. global $dolibarr_main_authentication, $dolibarr_main_demo;
  351. global $menumanager;
  352. $result = '';
  353. $companylink = '';
  354. $label = '<u>'.$langs->trans("MyModule").'</u>';
  355. $label .= '<div width="100%">';
  356. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  357. $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"';
  358. $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
  359. $link .= '>';
  360. $linkend = '</a>';
  361. if ($withpicto) {
  362. $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
  363. if ($withpicto != 2) {
  364. $result .= ' ';
  365. }
  366. }
  367. $result .= $link.$this->ref.$linkend;
  368. return $result;
  369. }
  370. /**
  371. * Retourne le libelle du status d'un user (actif, inactif)
  372. *
  373. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  374. * @return string Label of status
  375. */
  376. public function getLibStatut($mode = 0)
  377. {
  378. return $this->LibStatut($this->status, $mode);
  379. }
  380. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  381. /**
  382. * Renvoi le libelle d'un status donne
  383. *
  384. * @param int $status Id status
  385. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  386. * @return string Label of status
  387. */
  388. public function LibStatut($status, $mode = 0)
  389. {
  390. // phpcs:enable
  391. global $langs;
  392. if ($mode == 0) {
  393. if ($status == 1) {
  394. return $langs->trans('Enabled');
  395. } elseif ($status == 0) {
  396. return $langs->trans('Disabled');
  397. }
  398. } elseif ($mode == 1) {
  399. if ($status == 1) {
  400. return $langs->trans('Enabled');
  401. } elseif ($status == 0) {
  402. return $langs->trans('Disabled');
  403. }
  404. } elseif ($mode == 2) {
  405. if ($status == 1) {
  406. return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
  407. } elseif ($status == 0) {
  408. return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
  409. }
  410. } elseif ($mode == 3) {
  411. if ($status == 1) {
  412. return img_picto($langs->trans('Enabled'), 'statut4');
  413. } elseif ($status == 0) {
  414. return img_picto($langs->trans('Disabled'), 'statut5');
  415. }
  416. } elseif ($mode == 4) {
  417. if ($status == 1) {
  418. return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
  419. } elseif ($status == 0) {
  420. return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
  421. }
  422. } elseif ($mode == 5) {
  423. if ($status == 1) {
  424. return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
  425. } elseif ($status == 0) {
  426. return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
  427. }
  428. }
  429. }
  430. /**
  431. * Initialise object with example values
  432. * Id must be 0 if object instance is a specimen
  433. *
  434. * @return void
  435. */
  436. public function initAsSpecimen()
  437. {
  438. $this->id = 0;
  439. $this->libelle = '';
  440. $this->label = '';
  441. $this->deductible = '';
  442. $this->active = '';
  443. $this->code = '';
  444. $this->fk_pays = '';
  445. $this->module = '';
  446. $this->accountancy_code = '';
  447. }
  448. /**
  449. * Trim object parameters
  450. *
  451. * @param string[] $parameters array of parameters to trim
  452. * @return void
  453. */
  454. private function trimParameters($parameters)
  455. {
  456. foreach ($parameters as $parameter) {
  457. if (isset($this->$parameter)) {
  458. $this->$parameter = trim($this->$parameter);
  459. }
  460. }
  461. }
  462. }