chargesociales.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 Frédéric France <frederic.france@free.fr>
  5. * Copyright (C) 2017 Alexandre Spangaro <aspangaro@zendsi.com>
  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 <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/sociales/class/chargesociales.class.php
  22. * \ingroup facture
  23. * \brief Fichier de la classe des charges sociales
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  26. /**
  27. * Classe permettant la gestion des paiements des charges
  28. * La tva collectee n'est calculee que sur les factures payees.
  29. */
  30. class ChargeSociales extends CommonObject
  31. {
  32. public $element='chargesociales';
  33. public $table='chargesociales';
  34. public $table_element='chargesociales';
  35. public $picto = 'bill';
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected $table_ref_field = 'ref';
  40. var $date_ech;
  41. var $lib;
  42. var $type;
  43. var $type_libelle;
  44. var $amount;
  45. var $paye;
  46. var $periode;
  47. var $date_creation;
  48. var $date_modification;
  49. var $date_validation;
  50. var $fk_account;
  51. var $fk_project;
  52. /**
  53. * Constructor
  54. *
  55. * @param DoliDB $db Database handler
  56. */
  57. function __construct($db)
  58. {
  59. $this->db = $db;
  60. return 1;
  61. }
  62. /**
  63. * Retrouve et charge une charge sociale
  64. *
  65. * @param int $id Id
  66. * @param string $ref Ref
  67. * @return int <0 KO >0 OK
  68. */
  69. function fetch($id, $ref='')
  70. {
  71. $sql = "SELECT cs.rowid, cs.date_ech";
  72. $sql.= ", cs.libelle as lib, cs.fk_type, cs.amount, cs.fk_projet as fk_project, cs.paye, cs.periode, cs.import_key";
  73. $sql.= ", cs.fk_account, cs.fk_mode_reglement";
  74. $sql.= ", c.libelle";
  75. $sql.= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
  76. $sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as cs";
  77. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_chargesociales as c ON cs.fk_type = c.id";
  78. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON cs.fk_mode_reglement = p.id AND p.entity IN ('.getEntity('c_paiement').')';
  79. $sql.= ' WHERE cs.entity IN ('.getEntity('tax').')';
  80. if ($ref) $sql.= " AND cs.rowid = ".$ref;
  81. else $sql.= " AND cs.rowid = ".$id;
  82. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  83. $resql=$this->db->query($sql);
  84. if ($resql)
  85. {
  86. if ($this->db->num_rows($resql))
  87. {
  88. $obj = $this->db->fetch_object($resql);
  89. $this->id = $obj->rowid;
  90. $this->ref = $obj->rowid;
  91. $this->date_ech = $this->db->jdate($obj->date_ech);
  92. $this->lib = $obj->lib;
  93. $this->type = $obj->fk_type;
  94. $this->type_libelle = $obj->libelle;
  95. $this->fk_account = $obj->fk_account;
  96. $this->mode_reglement_id = $obj->fk_mode_reglement;
  97. $this->mode_reglement_code = $obj->mode_reglement_code;
  98. $this->mode_reglement = $obj->mode_reglement_libelle;
  99. $this->amount = $obj->amount;
  100. $this->fk_project = $obj->fk_project;
  101. $this->paye = $obj->paye;
  102. $this->periode = $this->db->jdate($obj->periode);
  103. $this->import_key = $this->import_key;
  104. $this->db->free($resql);
  105. return 1;
  106. }
  107. else
  108. {
  109. return 0;
  110. }
  111. }
  112. else
  113. {
  114. $this->error=$this->db->lasterror();
  115. return -1;
  116. }
  117. }
  118. /**
  119. * Check if a social contribution can be created into database
  120. *
  121. * @return boolean True or false
  122. */
  123. function check()
  124. {
  125. $newamount=price2num($this->amount,'MT');
  126. // Validation parametres
  127. if (! $newamount > 0 || empty($this->date_ech) || empty($this->periode))
  128. {
  129. return false;
  130. }
  131. return true;
  132. }
  133. /**
  134. * Create a social contribution into database
  135. *
  136. * @param User $user User making creation
  137. * @return int <0 if KO, id if OK
  138. */
  139. function create($user)
  140. {
  141. global $conf;
  142. $now=dol_now();
  143. // Nettoyage parametres
  144. $newamount=price2num($this->amount,'MT');
  145. if (!$this->check())
  146. {
  147. $this->error="ErrorBadParameter";
  148. return -2;
  149. }
  150. $this->db->begin();
  151. $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, date_creation)";
  152. $sql.= " VALUES (".$this->type;
  153. $sql.= ", ".($this->fk_account>0 ? $this->fk_account:'NULL');
  154. $sql.= ", ".($this->mode_reglement_id>0 ? $this->mode_reglement_id:"NULL");
  155. $sql.= ", '".$this->db->escape($this->lib)."'";
  156. $sql.= ", '".$this->db->idate($this->date_ech)."'";
  157. $sql.= ", '".$this->db->idate($this->periode)."'";
  158. $sql.= ", '".price2num($newamount)."'";
  159. $sql.= ", ".($this->fk_project>0?$this->fk_project:'NULL');
  160. $sql.= ", ".$conf->entity;
  161. $sql.= ", ".$user->id;
  162. $sql.= ", '".$this->db->idate($now)."'";
  163. $sql.= ")";
  164. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  165. $resql=$this->db->query($sql);
  166. if ($resql)
  167. {
  168. $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."chargesociales");
  169. //dol_syslog("ChargesSociales::create this->id=".$this->id);
  170. $this->db->commit();
  171. return $this->id;
  172. }
  173. else
  174. {
  175. $this->error=$this->db->error();
  176. $this->db->rollback();
  177. return -1;
  178. }
  179. }
  180. /**
  181. * Delete a social contribution
  182. *
  183. * @param User $user Object user making delete
  184. * @return int <0 if KO, >0 if OK
  185. */
  186. function delete($user)
  187. {
  188. $error=0;
  189. $this->db->begin();
  190. // Get bank transaction lines for this social contributions
  191. include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  192. $account=new Account($this->db);
  193. $lines_url=$account->get_url('',$this->id,'sc');
  194. // Delete bank urls
  195. foreach ($lines_url as $line_url)
  196. {
  197. if (! $error)
  198. {
  199. $accountline=new AccountLine($this->db);
  200. $accountline->fetch($line_url['fk_bank']);
  201. $result=$accountline->delete_urls($user);
  202. if ($result < 0)
  203. {
  204. $error++;
  205. }
  206. }
  207. }
  208. // Delete payments
  209. if (! $error)
  210. {
  211. $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge WHERE fk_charge=".$this->id;
  212. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  213. $resql=$this->db->query($sql);
  214. if (! $resql)
  215. {
  216. $error++;
  217. $this->error=$this->db->lasterror();
  218. }
  219. }
  220. if (! $error)
  221. {
  222. $sql = "DELETE FROM ".MAIN_DB_PREFIX."chargesociales WHERE rowid=".$this->id;
  223. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  224. $resql=$this->db->query($sql);
  225. if (! $resql)
  226. {
  227. $error++;
  228. $this->error=$this->db->lasterror();
  229. }
  230. }
  231. if (! $error)
  232. {
  233. $this->db->commit();
  234. return 1;
  235. }
  236. else
  237. {
  238. $this->db->rollback();
  239. return -1;
  240. }
  241. }
  242. /**
  243. * Met a jour une charge sociale
  244. *
  245. * @param User $user Utilisateur qui modifie
  246. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  247. * @return int <0 si erreur, >0 si ok
  248. */
  249. function update($user,$notrigger=0)
  250. {
  251. $error=0;
  252. $this->db->begin();
  253. $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales";
  254. $sql.= " SET libelle='".$this->db->escape($this->lib)."'";
  255. $sql.= ", date_ech='".$this->db->idate($this->date_ech)."'";
  256. $sql.= ", periode='".$this->db->idate($this->periode)."'";
  257. $sql.= ", amount='".price2num($this->amount,'MT')."'";
  258. $sql.= ", fk_projet='".$this->db->escape($this->fk_project)."'";
  259. $sql.= ", fk_user_modif=".$user->id;
  260. $sql.= " WHERE rowid=".$this->id;
  261. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  262. $resql=$this->db->query($sql);
  263. if (! $resql) {
  264. $error++; $this->errors[]="Error ".$this->db->lasterror();
  265. }
  266. if (! $error)
  267. {
  268. if (! $notrigger)
  269. {
  270. // Call trigger
  271. $result=$this->call_trigger('SOCIALCHARGES_MODIFY',$user);
  272. if ($result < 0) $error++;
  273. // End call triggers
  274. }
  275. }
  276. // Commit or rollback
  277. if ($error)
  278. {
  279. foreach($this->errors as $errmsg)
  280. {
  281. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  282. $this->error.=($this->error?', '.$errmsg:$errmsg);
  283. }
  284. $this->db->rollback();
  285. return -1*$error;
  286. }
  287. else
  288. {
  289. $this->db->commit();
  290. return 1;
  291. }
  292. }
  293. /**
  294. * Calculate amount remaining to pay by year
  295. *
  296. * @param int $year Year
  297. * @return number
  298. */
  299. function solde($year = 0)
  300. {
  301. global $conf;
  302. $sql = "SELECT SUM(f.amount) as amount";
  303. $sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as f";
  304. $sql.= " WHERE f.entity = ".$conf->entity;
  305. $sql.= " AND paye = 0";
  306. if ($year) {
  307. $sql .= " AND f.datev >= '$y-01-01' AND f.datev <= '$y-12-31' ";
  308. }
  309. $result = $this->db->query($sql);
  310. if ($result)
  311. {
  312. if ($this->db->num_rows($result))
  313. {
  314. $obj = $this->db->fetch_object($result);
  315. $this->db->free($result);
  316. return $obj->amount;
  317. }
  318. else
  319. {
  320. return 0;
  321. }
  322. }
  323. else
  324. {
  325. print $this->db->error();
  326. return -1;
  327. }
  328. }
  329. /**
  330. * Tag social contribution as payed completely
  331. *
  332. * @param User $user Object user making change
  333. * @return int <0 if KO, >0 if OK
  334. */
  335. function set_paid($user)
  336. {
  337. $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
  338. $sql.= " paye = 1";
  339. $sql.= " WHERE rowid = ".$this->id;
  340. $return = $this->db->query($sql);
  341. if ($return) return 1;
  342. else return -1;
  343. }
  344. /**
  345. * Remove tag payed on social contribution
  346. *
  347. * @param User $user Object user making change
  348. * @return int <0 if KO, >0 if OK
  349. */
  350. function set_unpaid($user)
  351. {
  352. $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
  353. $sql.= " paye = 0";
  354. $sql.= " WHERE rowid = ".$this->id;
  355. $return = $this->db->query($sql);
  356. if ($return) return 1;
  357. else return -1;
  358. }
  359. /**
  360. * Retourne le libelle du statut d'une charge (impaye, payee)
  361. *
  362. * @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
  363. * @param double $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount payed if you have it, 1 otherwise)
  364. * @return string Label
  365. */
  366. function getLibStatut($mode=0,$alreadypaid=-1)
  367. {
  368. return $this->LibStatut($this->paye,$mode,$alreadypaid);
  369. }
  370. /**
  371. * Renvoi le libelle d'un statut donne
  372. *
  373. * @param int $statut Id statut
  374. * @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
  375. * @param double $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount payed if you have it, 1 otherwise)
  376. * @return string Label
  377. */
  378. function LibStatut($statut,$mode=0,$alreadypaid=-1)
  379. {
  380. global $langs;
  381. $langs->load('customers');
  382. $langs->load('bills');
  383. if ($mode == 0)
  384. {
  385. if ($statut == 0) return $langs->trans("Unpaid");
  386. if ($statut == 1) return $langs->trans("Paid");
  387. }
  388. if ($mode == 1)
  389. {
  390. if ($statut == 0) return $langs->trans("Unpaid");
  391. if ($statut == 1) return $langs->trans("Paid");
  392. }
  393. if ($mode == 2)
  394. {
  395. if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
  396. if ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
  397. if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
  398. }
  399. if ($mode == 3)
  400. {
  401. if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1');
  402. if ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3');
  403. if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6');
  404. }
  405. if ($mode == 4)
  406. {
  407. if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
  408. if ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
  409. if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
  410. }
  411. if ($mode == 5)
  412. {
  413. if ($statut == 0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
  414. if ($statut == 0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
  415. if ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
  416. }
  417. if ($mode == 6)
  418. {
  419. if ($statut == 0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
  420. if ($statut == 0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
  421. if ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
  422. }
  423. return "Error, mode/status not found";
  424. }
  425. /**
  426. * Return a link to the object card (with optionaly the picto)
  427. *
  428. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  429. * @param int $maxlen Max length of label
  430. * @param int $notooltip 1=Disable tooltip
  431. * @return string String with link
  432. */
  433. function getNomUrl($withpicto=0, $maxlen=0, $notooltip=0)
  434. {
  435. global $langs;
  436. $result='';
  437. if (empty($this->ref)) $this->ref=$this->lib;
  438. $label = $langs->trans("ShowSocialContribution").': '.$this->ref;
  439. $linkstart = '<a href="'.DOL_URL_ROOT.'/compta/sociales/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  440. $linkend='</a>';
  441. $result .= $linkstart;
  442. 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);
  443. if ($withpicto != 2) $result.= ($maxlen?dol_trunc($this->ref,$maxlen):$this->ref);
  444. $result .= $linkend;
  445. return $result;
  446. }
  447. /**
  448. * Return amount of payments already done
  449. *
  450. * @return int Amount of payment already done, <0 if KO
  451. */
  452. function getSommePaiement()
  453. {
  454. $table='paiementcharge';
  455. $field='fk_charge';
  456. $sql = 'SELECT sum(amount) as amount';
  457. $sql.= ' FROM '.MAIN_DB_PREFIX.$table;
  458. $sql.= ' WHERE '.$field.' = '.$this->id;
  459. dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
  460. $resql=$this->db->query($sql);
  461. if ($resql)
  462. {
  463. $amount=0;
  464. $obj = $this->db->fetch_object($resql);
  465. if ($obj) $amount=$obj->amount?$obj->amount:0;
  466. $this->db->free($resql);
  467. return $amount;
  468. }
  469. else
  470. {
  471. return -1;
  472. }
  473. }
  474. /**
  475. * Charge les informations d'ordre info dans l'objet entrepot
  476. *
  477. * @param int $id Id of social contribution
  478. * @return int <0 if KO, >0 if OK
  479. */
  480. function info($id)
  481. {
  482. $sql = "SELECT e.rowid, e.tms as datem, e.date_creation as datec, e.date_valid as datev, e.import_key,";
  483. $sql.= " e.fk_user_author, e.fk_user_modif, e.fk_user_valid";
  484. $sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as e";
  485. $sql.= " WHERE e.rowid = ".$id;
  486. dol_syslog(get_class($this)."::info", LOG_DEBUG);
  487. $result=$this->db->query($sql);
  488. if ($result)
  489. {
  490. if ($this->db->num_rows($result))
  491. {
  492. $obj = $this->db->fetch_object($result);
  493. $this->id = $obj->rowid;
  494. if ($obj->fk_user_author) {
  495. $cuser = new User($this->db);
  496. $cuser->fetch($obj->fk_user_author);
  497. $this->user_creation = $cuser;
  498. }
  499. if ($obj->fk_user_modif) {
  500. $muser = new User($this->db);
  501. $muser->fetch($obj->fk_user_modif);
  502. $this->user_modification = $muser;
  503. }
  504. if ($obj->fk_user_valid) {
  505. $vuser = new User($this->db);
  506. $vuser->fetch($obj->fk_user_valid);
  507. $this->user_validation = $vuser;
  508. }
  509. $this->date_creation = $this->db->jdate($obj->datec);
  510. $this->date_modification = $this->db->jdate($obj->datem);
  511. $this->date_validation = $this->db->jdate($obj->datev);
  512. $this->import_key = $obj->import_key;
  513. }
  514. $this->db->free($result);
  515. }
  516. else
  517. {
  518. dol_print_error($this->db);
  519. }
  520. }
  521. /**
  522. * Initialise an instance with random values.
  523. * Used to build previews or test instances.
  524. * id must be 0 if object instance is a specimen.
  525. *
  526. * @return void
  527. */
  528. function initAsSpecimen()
  529. {
  530. // Initialize parameters
  531. $this->id=0;
  532. $this->ref = 'SPECIMEN';
  533. $this->specimen=1;
  534. $this->paye = 0;
  535. $this->date = time();
  536. $this->date_ech=$this->date+3600*24*30;
  537. $this->periode=$this->date+3600*24*30;
  538. $this->amount=100;
  539. $this->lib = 0;
  540. $this->type = 1;
  541. $this->type_libelle = 'Social contribution label';
  542. }
  543. }