PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/compta/sociales/class/chargesociales.class.php

https://github.com/asterix14/dolibarr
PHP | 423 lines | 283 code | 57 blank | 83 comment | 64 complexity | 5d46e954c5c61d6f6d3cb887db4d39af MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2007 Laurent Destailleur <eldy@users.sourceforge.net>
  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 2 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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/compta/sociales/class/chargesociales.class.php
  20. * \ingroup facture
  21. * \brief Fichier de la classe des charges sociales
  22. */
  23. require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
  24. /** \class ChargeSociales
  25. * \brief Classe permettant la gestion des paiements des charges
  26. * La tva collectee n'est calculee que sur les factures payees.
  27. */
  28. class ChargeSociales extends CommonObject
  29. {
  30. public $element='rowid';
  31. public $table='chargesociales';
  32. public $table_element='chargesociales';
  33. var $id;
  34. var $date_ech;
  35. var $lib;
  36. var $type;
  37. var $type_libelle;
  38. var $amount;
  39. var $paye;
  40. var $periode;
  41. function ChargeSociales($DB)
  42. {
  43. $this->db = $DB;
  44. return 1;
  45. }
  46. /**
  47. * \brief Retrouve et charge une charge sociale
  48. * \return int 1 si trouve, 0 sinon
  49. */
  50. function fetch($id)
  51. {
  52. $sql = "SELECT cs.rowid, cs.date_ech,";
  53. $sql.= " cs.libelle as lib, cs.fk_type, cs.amount, cs.paye, cs.periode,";
  54. $sql.= " c.libelle";
  55. $sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as cs, ".MAIN_DB_PREFIX."c_chargesociales as c";
  56. $sql.= " WHERE cs.fk_type = c.id";
  57. $sql.= " AND cs.rowid = ".$id;
  58. dol_syslog("ChargesSociales::fetch sql=".$sql);
  59. $resql=$this->db->query($sql);
  60. if ($resql)
  61. {
  62. if ($this->db->num_rows($resql))
  63. {
  64. $obj = $this->db->fetch_object($resql);
  65. $this->id = $obj->rowid;
  66. $this->ref = $obj->rowid;
  67. $this->date_ech = $this->db->jdate($obj->date_ech);
  68. $this->lib = $obj->lib;
  69. $this->type = $obj->fk_type;
  70. $this->type_libelle = $obj->libelle;
  71. $this->amount = $obj->amount;
  72. $this->paye = $obj->paye;
  73. $this->periode = $this->db->jdate($obj->periode);
  74. return 1;
  75. }
  76. else
  77. {
  78. return 0;
  79. }
  80. $this->db->free($resql);
  81. }
  82. else
  83. {
  84. $this->error=$this->db->error();
  85. return -1;
  86. }
  87. }
  88. /**
  89. * \brief Create a social contribution in database
  90. * \param user User making creation
  91. * \return int <0 if KO, id if OK
  92. */
  93. function create($user)
  94. {
  95. // Nettoyage parametres
  96. $newamount=price2num($this->amount,'MT');
  97. // Validation parametres
  98. if (! $newamount > 0)
  99. {
  100. $this->error="ErrorBadParameter";
  101. return -2;
  102. }
  103. $this->db->begin();
  104. $sql = "INSERT INTO ".MAIN_DB_PREFIX."chargesociales (fk_type, libelle, date_ech, periode, amount)";
  105. $sql.= " VALUES (".$this->type.",'".$this->db->escape($this->lib)."',";
  106. $sql.= " '".$this->db->idate($this->date_ech)."','".$this->db->idate($this->periode)."',";
  107. $sql.= " ".price2num($newamount);
  108. $sql.= ")";
  109. dol_syslog("ChargesSociales::create sql=".$sql);
  110. $resql=$this->db->query($sql);
  111. if ($resql)
  112. {
  113. $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."chargesociales");
  114. //dol_syslog("ChargesSociales::create this->id=".$this->id);
  115. $this->db->commit();
  116. return $this->id;
  117. }
  118. else
  119. {
  120. $this->error=$this->db->error();
  121. $this->db->rollback();
  122. return -1;
  123. }
  124. }
  125. /**
  126. * Delete a social contribution
  127. * @param user Object user making delete
  128. * @return int <0 if KO, >0 if OK
  129. */
  130. function delete($user)
  131. {
  132. $error=0;
  133. $this->db->begin();
  134. // Get bank transaction lines for this social contributions
  135. include_once(DOL_DOCUMENT_ROOT."/compta/bank/class/account.class.php");
  136. $account=new Account($this->db);
  137. $lines_url=$account->get_url('',$this->id,'sc');
  138. // Delete bank urls
  139. foreach ($lines_url as $line_url)
  140. {
  141. if (! $error)
  142. {
  143. $accountline=new AccountLine($this->db);
  144. $accountline->fetch($line_url['fk_bank']);
  145. $result=$accountline->delete_urls($user);
  146. if ($result < 0)
  147. {
  148. $error++;
  149. }
  150. }
  151. }
  152. // Delete payments
  153. if (! $error)
  154. {
  155. $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge where fk_charge='".$this->id."'";
  156. dol_syslog(get_class($this)."::delete sql=".$sql);
  157. $resql=$this->db->query($sql);
  158. if (! $resql)
  159. {
  160. $error++;
  161. $this->error=$this->db->lasterror();
  162. }
  163. }
  164. if (! $error)
  165. {
  166. $sql = "DELETE FROM ".MAIN_DB_PREFIX."chargesociales where rowid='".$this->id."'";
  167. dol_syslog(get_class($this)."::delete sql=".$sql);
  168. $resql=$this->db->query($sql);
  169. if (! $resql)
  170. {
  171. $error++;
  172. $this->error=$this->db->lasterror();
  173. }
  174. }
  175. if (! $error)
  176. {
  177. $this->db->commit();
  178. return 1;
  179. }
  180. else
  181. {
  182. $this->db->rollback();
  183. return -1;
  184. }
  185. }
  186. /**
  187. * Met a jour une charge sociale
  188. * @param user Utilisateur qui modifie
  189. * @return int <0 si erreur, >0 si ok
  190. */
  191. function update($user)
  192. {
  193. $this->db->begin();
  194. $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales";
  195. $sql.= " SET libelle='".$this->db->escape($this->lib)."',";
  196. $sql.= " date_ech='".$this->db->idate($this->date_ech)."',";
  197. $sql.= " periode='".$this->db->idate($this->periode)."'";
  198. $sql.= " WHERE rowid=".$this->id;
  199. dol_syslog("ChargesSociales::update sql=".$sql);
  200. $resql=$this->db->query($sql);
  201. if ($resql)
  202. {
  203. $this->db->commit();
  204. return 1;
  205. }
  206. else
  207. {
  208. $this->error=$this->db->error();
  209. $this->db->rollback();
  210. return -1;
  211. }
  212. }
  213. function solde($year = 0)
  214. {
  215. $sql = "SELECT sum(f.amount) as amount";
  216. $sql .= " FROM ".MAIN_DB_PREFIX."chargesociales as f WHERE paye = 0";
  217. if ($year) {
  218. $sql .= " AND f.datev >= '$y-01-01' AND f.datev <= '$y-12-31' ";
  219. }
  220. $result = $this->db->query($sql);
  221. if ($result) {
  222. if ($this->db->num_rows($result)) {
  223. $obj = $this->db->fetch_object($result);
  224. return $obj->amount;
  225. } else {
  226. return 0;
  227. }
  228. $this->db->free();
  229. } else {
  230. print $this->db->error();
  231. return -1;
  232. }
  233. }
  234. /**
  235. * Tag social contribution as payed completely
  236. * @param user Object user making change
  237. */
  238. function set_paid($user)
  239. {
  240. $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales";
  241. $sql.= " set paye=1";
  242. $sql.= " WHERE rowid = ".$this->id;
  243. $return = $this->db->query($sql);
  244. if ($return) return 1;
  245. else return -1;
  246. }
  247. /**
  248. * \brief Retourne le libelle du statut d'une charge (impaye, payee)
  249. * \param mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long
  250. * \return string Libelle
  251. */
  252. function getLibStatut($mode=0)
  253. {
  254. return $this->LibStatut($this->paye,$mode);
  255. }
  256. /**
  257. * \brief Renvoi le libelle d'un statut donne
  258. * \param statut Id statut
  259. * \param mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  260. * \return string Libelle du statut
  261. */
  262. function LibStatut($statut,$mode=0)
  263. {
  264. global $langs;
  265. $langs->load('customers');
  266. if ($mode == 0)
  267. {
  268. if ($statut == 0) return $langs->trans("Unpaid");
  269. if ($statut == 1) return $langs->trans("Paid");
  270. }
  271. if ($mode == 1)
  272. {
  273. if ($statut == 0) return $langs->trans("Unpaid");
  274. if ($statut == 1) return $langs->trans("Paid");
  275. }
  276. if ($mode == 2)
  277. {
  278. if ($statut == 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
  279. if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
  280. }
  281. if ($mode == 3)
  282. {
  283. if ($statut == 0) return img_picto($langs->trans("Unpaid"), 'statut1');
  284. if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6');
  285. }
  286. if ($mode == 4)
  287. {
  288. if ($statut == 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
  289. if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
  290. }
  291. if ($mode == 5)
  292. {
  293. if ($statut == 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
  294. if ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
  295. }
  296. return "Error, mode/status not found";
  297. }
  298. /**
  299. * \brief Renvoie nom clicable (avec eventuellement le picto)
  300. * \param withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  301. * \param maxlen Longueur max libelle
  302. * \return string Chaine avec URL
  303. */
  304. function getNomUrl($withpicto=0,$maxlen=0)
  305. {
  306. global $langs;
  307. $result='';
  308. if (empty($this->ref)) $this->ref=$this->lib;
  309. $lien = '<a href="'.DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$this->id.'">';
  310. $lienfin='</a>';
  311. if ($withpicto) $result.=($lien.img_object($langs->trans("ShowSocialContribution").': '.$this->lib,'bill').$lienfin.' ');
  312. if ($withpicto && $withpicto != 2) $result.=' ';
  313. if ($withpicto != 2) $result.=$lien.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$lienfin;
  314. return $result;
  315. }
  316. /**
  317. * Return amount of payments already done
  318. * @return int Amount of payment already done, <0 if KO
  319. */
  320. function getSommePaiement()
  321. {
  322. $table='paiementcharge';
  323. $field='fk_charge';
  324. $sql = 'SELECT sum(amount) as amount';
  325. $sql.= ' FROM '.MAIN_DB_PREFIX.$table;
  326. $sql.= ' WHERE '.$field.' = '.$this->id;
  327. dol_syslog("ChargeSociales::getSommePaiement sql=".$sql, LOG_DEBUG);
  328. $resql=$this->db->query($sql);
  329. if ($resql)
  330. {
  331. $amount=0;
  332. $obj = $this->db->fetch_object($resql);
  333. if ($obj) $amount=$obj->amount?$obj->amount:0;
  334. $this->db->free($resql);
  335. return $amount;
  336. }
  337. else
  338. {
  339. return -1;
  340. }
  341. }
  342. /**
  343. * Initialise an instance with random values.
  344. * Used to build previews or test instances.
  345. * id must be 0 if object instance is a specimen.
  346. *
  347. * @return void
  348. */
  349. function initAsSpecimen()
  350. {
  351. global $user,$langs,$conf;
  352. // Initialize parameters
  353. $this->id=0;
  354. $this->ref = 'SPECIMEN';
  355. $this->specimen=1;
  356. $this->paye = 0;
  357. $this->date = time();
  358. $this->date_ech=$this->date+3600*24*30;
  359. $this->period=$this->date+3600*24*30;
  360. $this->amount=100;
  361. $this->lib = 0;
  362. $this->type = 1;
  363. $this->type_libelle = 'Social contribution label';
  364. }
  365. }
  366. ?>