chargesociales.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. <?php
  2. /* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2007 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2016-2022 Frédéric France <frederic.france@netlogic.fr>
  5. * Copyright (C) 2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  6. * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/compta/sociales/class/chargesociales.class.php
  23. * \ingroup facture
  24. * \brief Fichier de la classe des charges sociales
  25. */
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  27. /**
  28. * Classe permettant la gestion des paiements des charges
  29. * La tva collectee n'est calculee que sur les factures payees.
  30. */
  31. class ChargeSociales extends CommonObject
  32. {
  33. /**
  34. * @var string ID to identify managed object
  35. */
  36. public $element = 'chargesociales';
  37. public $table = 'chargesociales';
  38. /**
  39. * @var string Name of table without prefix where object is stored
  40. */
  41. public $table_element = 'chargesociales';
  42. /**
  43. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  44. */
  45. public $picto = 'bill';
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected $table_ref_field = 'ref';
  50. /**
  51. * @var integer|string $date_ech
  52. */
  53. public $date_ech;
  54. public $label;
  55. public $type;
  56. public $type_label;
  57. public $amount;
  58. public $paye;
  59. public $periode;
  60. /**
  61. * @var integer|string date_creation
  62. */
  63. public $date_creation;
  64. /**
  65. * @var integer|string $date_modification
  66. */
  67. public $date_modification;
  68. /**
  69. * @var integer|string $date_validation
  70. */
  71. public $date_validation;
  72. /**
  73. * @deprecated Use label instead
  74. */
  75. public $lib;
  76. /**
  77. * @var int account ID
  78. */
  79. public $fk_account;
  80. /**
  81. * @var int account ID (identical to fk_account)
  82. */
  83. public $accountid;
  84. /**
  85. * @var int payment type (identical to mode_reglement_id in commonobject class)
  86. */
  87. public $paiementtype;
  88. /**
  89. * @var int ID
  90. */
  91. public $fk_project;
  92. /**
  93. * @var int ID
  94. */
  95. public $fk_user;
  96. /**
  97. * @var double total
  98. */
  99. public $total;
  100. public $totalpaid;
  101. const STATUS_UNPAID = 0;
  102. const STATUS_PAID = 1;
  103. /**
  104. * Constructor
  105. *
  106. * @param DoliDB $db Database handler
  107. */
  108. public function __construct(DoliDB $db)
  109. {
  110. $this->db = $db;
  111. }
  112. /**
  113. * Retrouve et charge une charge sociale
  114. *
  115. * @param int $id Id
  116. * @param string $ref Ref
  117. * @return int <0 KO >0 OK
  118. */
  119. public function fetch($id, $ref = '')
  120. {
  121. $sql = "SELECT cs.rowid, cs.date_ech";
  122. $sql .= ", cs.libelle as label, cs.fk_type, cs.amount, cs.fk_projet as fk_project, cs.paye, cs.periode, cs.import_key";
  123. $sql .= ", cs.fk_account, cs.fk_mode_reglement, cs.fk_user, note_public, note_private";
  124. $sql .= ", c.libelle as type_label";
  125. $sql .= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
  126. $sql .= " FROM ".MAIN_DB_PREFIX."chargesociales as cs";
  127. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_chargesociales as c ON cs.fk_type = c.id";
  128. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON cs.fk_mode_reglement = p.id';
  129. $sql .= ' WHERE cs.entity IN ('.getEntity('tax').')';
  130. if ($ref) {
  131. $sql .= " AND cs.ref = '".$this->db->escape($ref)."'";
  132. } else {
  133. $sql .= " AND cs.rowid = ".((int) $id);
  134. }
  135. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  136. $resql = $this->db->query($sql);
  137. if ($resql) {
  138. if ($this->db->num_rows($resql)) {
  139. $obj = $this->db->fetch_object($resql);
  140. $this->id = $obj->rowid;
  141. $this->ref = $obj->rowid;
  142. $this->date_ech = $this->db->jdate($obj->date_ech);
  143. $this->lib = $obj->label;
  144. $this->label = $obj->label;
  145. $this->type = $obj->fk_type;
  146. $this->type_label = $obj->type_label;
  147. $this->fk_account = $obj->fk_account;
  148. $this->mode_reglement_id = $obj->fk_mode_reglement;
  149. $this->mode_reglement_code = $obj->mode_reglement_code;
  150. $this->mode_reglement = $obj->mode_reglement_libelle;
  151. $this->amount = $obj->amount;
  152. $this->fk_project = $obj->fk_project;
  153. $this->fk_user = $obj->fk_user;
  154. $this->note_public = $obj->note_public;
  155. $this->note_private = $obj->note_private;
  156. $this->paye = $obj->paye;
  157. $this->periode = $this->db->jdate($obj->periode);
  158. $this->import_key = $this->import_key;
  159. $this->db->free($resql);
  160. return 1;
  161. } else {
  162. return 0;
  163. }
  164. } else {
  165. $this->error = $this->db->lasterror();
  166. return -1;
  167. }
  168. }
  169. /**
  170. * Check if a social contribution can be created into database
  171. *
  172. * @return boolean True or false
  173. */
  174. public function check()
  175. {
  176. $newamount = price2num($this->amount, 'MT');
  177. // Validation of parameters
  178. if ($newamount == 0 || empty($this->date_ech) || empty($this->periode)) {
  179. return false;
  180. }
  181. return true;
  182. }
  183. /**
  184. * Create a social contribution into database
  185. *
  186. * @param User $user User making creation
  187. * @return int <0 if KO, id if OK
  188. */
  189. public function create($user)
  190. {
  191. global $conf;
  192. $error = 0;
  193. $now = dol_now();
  194. // Nettoyage parametres
  195. $newamount = price2num($this->amount, 'MT');
  196. if (!$this->check()) {
  197. $this->error = "ErrorBadParameter";
  198. return -2;
  199. }
  200. $this->db->begin();
  201. $sql = "INSERT INTO ".MAIN_DB_PREFIX."chargesociales (fk_type, fk_account, fk_mode_reglement, libelle, date_ech, periode, amount, fk_projet, entity, fk_user_author, fk_user, date_creation)";
  202. $sql .= " VALUES (".((int) $this->type);
  203. $sql .= ", ".($this->fk_account > 0 ? ((int) $this->fk_account) : 'NULL');
  204. $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : "NULL");
  205. $sql .= ", '".$this->db->escape($this->label ? $this->label : $this->lib)."'";
  206. $sql .= ", '".$this->db->idate($this->date_ech)."'";
  207. $sql .= ", '".$this->db->idate($this->periode)."'";
  208. $sql .= ", '".price2num($newamount)."'";
  209. $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 'NULL');
  210. $sql .= ", ".((int) $conf->entity);
  211. $sql .= ", ".((int) $user->id);
  212. $sql .= ", ".($this->fk_user > 0 ? ((int) $this->fk_user) : 'NULL');
  213. $sql .= ", '".$this->db->idate($now)."'";
  214. $sql .= ")";
  215. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  216. $resql = $this->db->query($sql);
  217. if ($resql) {
  218. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."chargesociales");
  219. //dol_syslog("ChargesSociales::create this->id=".$this->id);
  220. $result = $this->call_trigger('SOCIALCONTRIBUTION_CREATE', $user);
  221. if ($result < 0) {
  222. $error++;
  223. }
  224. if (empty($error)) {
  225. $this->db->commit();
  226. return $this->id;
  227. } else {
  228. $this->db->rollback();
  229. return -1 * $error;
  230. }
  231. } else {
  232. $this->error = $this->db->error();
  233. $this->db->rollback();
  234. return -1;
  235. }
  236. }
  237. /**
  238. * Delete a social contribution
  239. *
  240. * @param User $user Object user making delete
  241. * @return int <0 if KO, >0 if OK
  242. */
  243. public function delete($user)
  244. {
  245. $error = 0;
  246. $this->db->begin();
  247. // Get bank transaction lines for this social contributions
  248. include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  249. $account = new Account($this->db);
  250. $lines_url = $account->get_url('', $this->id, 'sc');
  251. // Delete bank urls
  252. foreach ($lines_url as $line_url) {
  253. if (!$error) {
  254. $accountline = new AccountLine($this->db);
  255. $accountline->fetch($line_url['fk_bank']);
  256. $result = $accountline->delete_urls($user);
  257. if ($result < 0) {
  258. $error++;
  259. }
  260. }
  261. }
  262. // Delete payments
  263. if (!$error) {
  264. $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge WHERE fk_charge=".((int) $this->id);
  265. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  266. $resql = $this->db->query($sql);
  267. if (!$resql) {
  268. $error++;
  269. $this->error = $this->db->lasterror();
  270. }
  271. }
  272. if (!$error) {
  273. $sql = "DELETE FROM ".MAIN_DB_PREFIX."chargesociales WHERE rowid=".((int) $this->id);
  274. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  275. $resql = $this->db->query($sql);
  276. if (!$resql) {
  277. $error++;
  278. $this->error = $this->db->lasterror();
  279. }
  280. }
  281. if (!$error) {
  282. $this->db->commit();
  283. return 1;
  284. } else {
  285. $this->db->rollback();
  286. return -1;
  287. }
  288. }
  289. /**
  290. * Update social or fiscal contribution
  291. *
  292. * @param User $user User that modify
  293. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  294. * @return int <0 if KO, >0 if OK
  295. */
  296. public function update($user, $notrigger = 0)
  297. {
  298. $error = 0;
  299. $this->db->begin();
  300. $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales";
  301. $sql .= " SET libelle='".$this->db->escape($this->label ? $this->label : $this->lib)."'";
  302. $sql .= ", date_ech='".$this->db->idate($this->date_ech)."'";
  303. $sql .= ", periode='".$this->db->idate($this->periode)."'";
  304. $sql .= ", amount='".price2num($this->amount, 'MT')."'";
  305. $sql .= ", fk_projet=".($this->fk_project > 0 ? $this->db->escape($this->fk_project) : "NULL");
  306. $sql .= ", fk_user=".($this->fk_user > 0 ? $this->db->escape($this->fk_user) : "NULL");
  307. $sql .= ", fk_user_modif=".$user->id;
  308. $sql .= " WHERE rowid=".((int) $this->id);
  309. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  310. $resql = $this->db->query($sql);
  311. if (!$resql) {
  312. $error++;
  313. $this->errors[] = "Error ".$this->db->lasterror();
  314. }
  315. if (!$error) {
  316. if (!$notrigger) {
  317. // Call trigger
  318. $result = $this->call_trigger('SOCIALCONTRIBUTION_MODIFY', $user);
  319. if ($result < 0) {
  320. $error++;
  321. }
  322. // End call triggers
  323. }
  324. }
  325. // Commit or rollback
  326. if ($error) {
  327. foreach ($this->errors as $errmsg) {
  328. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  329. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  330. }
  331. $this->db->rollback();
  332. return -1 * $error;
  333. } else {
  334. $this->db->commit();
  335. return 1;
  336. }
  337. }
  338. /**
  339. * Calculate amount remaining to pay by year
  340. *
  341. * @param int $year Year
  342. * @return number
  343. */
  344. public function solde($year = 0)
  345. {
  346. global $conf;
  347. $sql = "SELECT SUM(f.amount) as amount";
  348. $sql .= " FROM ".MAIN_DB_PREFIX."chargesociales as f";
  349. $sql .= " WHERE f.entity = ".$conf->entity;
  350. $sql .= " AND paye = 0";
  351. if ($year) {
  352. $sql .= " AND f.datev >= '".((int) $year)."-01-01' AND f.datev <= '".((int) $year)."-12-31' ";
  353. }
  354. $result = $this->db->query($sql);
  355. if ($result) {
  356. if ($this->db->num_rows($result)) {
  357. $obj = $this->db->fetch_object($result);
  358. $this->db->free($result);
  359. return $obj->amount;
  360. } else {
  361. return 0;
  362. }
  363. } else {
  364. print $this->db->error();
  365. return -1;
  366. }
  367. }
  368. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  369. /**
  370. * Tag social contribution as paid completely
  371. *
  372. * @deprecated
  373. * @see setPaid()
  374. * @param User $user Object user making change
  375. * @return int <0 if KO, >0 if OK
  376. */
  377. public function set_paid($user)
  378. {
  379. // phpcs:enable
  380. dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
  381. return $this->setPaid($user);
  382. }
  383. /**
  384. * Tag social contribution as paid completely
  385. *
  386. * @param User $user Object user making change
  387. * @return int <0 if KO, >0 if OK
  388. */
  389. public function setPaid($user)
  390. {
  391. $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
  392. $sql .= " paye = 1";
  393. $sql .= " WHERE rowid = ".((int) $this->id);
  394. $return = $this->db->query($sql);
  395. if ($return) {
  396. return 1;
  397. } else {
  398. return -1;
  399. }
  400. }
  401. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  402. /**
  403. * Remove tag paid on social contribution
  404. *
  405. * @deprecated
  406. * @see setUnpaid()
  407. * @param User $user Object user making change
  408. * @return int <0 if KO, >0 if OK
  409. */
  410. public function set_unpaid($user)
  411. {
  412. // phpcs:enable
  413. dol_syslog(get_class($this)."::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
  414. return $this->setUnpaid($user);
  415. }
  416. /**
  417. * Remove tag paid on social contribution
  418. *
  419. * @param User $user Object user making change
  420. * @return int <0 if KO, >0 if OK
  421. */
  422. public function setUnpaid($user)
  423. {
  424. $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
  425. $sql .= " paye = 0";
  426. $sql .= " WHERE rowid = ".((int) $this->id);
  427. $return = $this->db->query($sql);
  428. if ($return) {
  429. return 1;
  430. } else {
  431. return -1;
  432. }
  433. }
  434. /**
  435. * Retourne le libelle du statut d'une charge (impaye, payee)
  436. *
  437. * @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
  438. * @param double $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount paid if you have it, 1 otherwise)
  439. * @return string Label
  440. */
  441. public function getLibStatut($mode = 0, $alreadypaid = -1)
  442. {
  443. return $this->LibStatut($this->paye, $mode, $alreadypaid);
  444. }
  445. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  446. /**
  447. * Renvoi le libelle d'un statut donne
  448. *
  449. * @param int $status Id status
  450. * @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
  451. * @param double $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount paid if you have it, 1 otherwise)
  452. * @return string Label
  453. */
  454. public function LibStatut($status, $mode = 0, $alreadypaid = -1)
  455. {
  456. // phpcs:enable
  457. global $langs;
  458. // Load translation files required by the page
  459. $langs->loadLangs(array("customers", "bills"));
  460. // We reinit status array to force to redefine them because label may change according to properties values.
  461. $this->labelStatus = array();
  462. $this->labelStatusShort = array();
  463. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  464. global $langs;
  465. //$langs->load("mymodule");
  466. $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('Unpaid');
  467. $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('Paid');
  468. if ($status == self::STATUS_UNPAID && $alreadypaid > 0) {
  469. $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
  470. }
  471. $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('Unpaid');
  472. $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('Paid');
  473. if ($status == self::STATUS_UNPAID && $alreadypaid > 0) {
  474. $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
  475. }
  476. }
  477. $statusType = 'status1';
  478. if ($status == 0 && $alreadypaid > 0) {
  479. $statusType = 'status3';
  480. }
  481. if ($status == 1) {
  482. $statusType = 'status6';
  483. }
  484. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  485. }
  486. /**
  487. * Return a link to the object card (with optionaly the picto)
  488. *
  489. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  490. * @param string $option On what the link point to ('nolink', ...)
  491. * @param int $notooltip 1=Disable tooltip
  492. * @param int $short 1=Return just URL
  493. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  494. * @return string String with link
  495. */
  496. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $short = 0, $save_lastsearch_value = -1)
  497. {
  498. global $langs, $conf, $user, $form, $hookmanager;
  499. if (!empty($conf->dol_no_mouse_hover)) {
  500. $notooltip = 1; // Force disable tooltips
  501. }
  502. $result = '';
  503. $url = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$this->id;
  504. if ($short) {
  505. return $url;
  506. }
  507. if ($option !== 'nolink') {
  508. // Add param to save lastsearch_values or not
  509. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  510. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  511. $add_save_lastsearch_values = 1;
  512. }
  513. if ($add_save_lastsearch_values) {
  514. $url .= '&save_lastsearch_values=1';
  515. }
  516. }
  517. if (empty($this->ref)) {
  518. $this->ref = $this->label;
  519. }
  520. $label = img_picto('', 'tax').'<u class="paddingrightonly">'.$langs->trans("SocialContribution").'</u>';
  521. if (isset($this->paye)) {
  522. $label .= ' '.$this->getLibStatut(5);
  523. }
  524. if (!empty($this->ref)) {
  525. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  526. }
  527. if (!empty($this->label)) {
  528. $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
  529. }
  530. if (!empty($this->type_label)) {
  531. $label .= '<br><b>'.$langs->trans('Type').':</b> '.$this->type_label;
  532. }
  533. $linkclose = '';
  534. if (empty($notooltip) && $user->rights->facture->lire) {
  535. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  536. $label = $langs->trans("SocialContribution");
  537. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  538. }
  539. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  540. $linkclose .= ' class="classfortooltip"';
  541. }
  542. $linkstart = '<a href="'.$url.'"';
  543. $linkstart .= $linkclose.'>';
  544. $linkend = '</a>';
  545. $result .= $linkstart;
  546. if ($withpicto) {
  547. $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);
  548. }
  549. if ($withpicto != 2) {
  550. $result .= $this->ref;
  551. }
  552. $result .= $linkend;
  553. global $action;
  554. $hookmanager->initHooks(array($this->element . 'dao'));
  555. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  556. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  557. if ($reshook > 0) {
  558. $result = $hookmanager->resPrint;
  559. } else {
  560. $result .= $hookmanager->resPrint;
  561. }
  562. return $result;
  563. }
  564. /**
  565. * Return amount of payments already done
  566. *
  567. * @return int Amount of payment already done, <0 if KO
  568. */
  569. public function getSommePaiement()
  570. {
  571. $table = 'paiementcharge';
  572. $field = 'fk_charge';
  573. $sql = 'SELECT sum(amount) as amount';
  574. $sql .= ' FROM '.MAIN_DB_PREFIX.$table;
  575. $sql .= " WHERE ".$field." = ".((int) $this->id);
  576. dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
  577. $resql = $this->db->query($sql);
  578. if ($resql) {
  579. $amount = 0;
  580. $obj = $this->db->fetch_object($resql);
  581. if ($obj) {
  582. $amount = $obj->amount ? $obj->amount : 0;
  583. }
  584. $this->db->free($resql);
  585. return $amount;
  586. } else {
  587. return -1;
  588. }
  589. }
  590. /**
  591. * Charge les informations d'ordre info dans l'objet entrepot
  592. *
  593. * @param int $id Id of social contribution
  594. * @return int <0 if KO, >0 if OK
  595. */
  596. public function info($id)
  597. {
  598. $sql = "SELECT e.rowid, e.tms as datem, e.date_creation as datec, e.date_valid as datev, e.import_key,";
  599. $sql .= " e.fk_user_author, e.fk_user_modif, e.fk_user_valid";
  600. $sql .= " FROM ".MAIN_DB_PREFIX."chargesociales as e";
  601. $sql .= " WHERE e.rowid = ".((int) $id);
  602. dol_syslog(get_class($this)."::info", LOG_DEBUG);
  603. $result = $this->db->query($sql);
  604. if ($result) {
  605. if ($this->db->num_rows($result)) {
  606. $obj = $this->db->fetch_object($result);
  607. $this->id = $obj->rowid;
  608. if ($obj->fk_user_author) {
  609. $cuser = new User($this->db);
  610. $cuser->fetch($obj->fk_user_author);
  611. $this->user_creation = $cuser;
  612. }
  613. if ($obj->fk_user_modif) {
  614. $muser = new User($this->db);
  615. $muser->fetch($obj->fk_user_modif);
  616. $this->user_modification = $muser;
  617. }
  618. if ($obj->fk_user_valid) {
  619. $vuser = new User($this->db);
  620. $vuser->fetch($obj->fk_user_valid);
  621. $this->user_validation = $vuser;
  622. }
  623. $this->date_creation = $this->db->jdate($obj->datec);
  624. $this->date_modification = $this->db->jdate($obj->datem);
  625. $this->date_validation = $this->db->jdate($obj->datev);
  626. $this->import_key = $obj->import_key;
  627. }
  628. $this->db->free($result);
  629. } else {
  630. dol_print_error($this->db);
  631. }
  632. }
  633. /**
  634. * Initialise an instance with random values.
  635. * Used to build previews or test instances.
  636. * id must be 0 if object instance is a specimen.
  637. *
  638. * @return void
  639. */
  640. public function initAsSpecimen()
  641. {
  642. // Initialize parameters
  643. $this->id = 0;
  644. $this->ref = 'SPECIMEN';
  645. $this->specimen = 1;
  646. $this->paye = 0;
  647. $this->date = dol_now();
  648. $this->date_ech = $this->date + 3600 * 24 * 30;
  649. $this->periode = $this->date + 3600 * 24 * 30;
  650. $this->amount = 100;
  651. $this->label = 'Social contribution label';
  652. $this->type = 1;
  653. $this->type_label = 'Type of social contribution';
  654. }
  655. }