cotisation.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2006-2008 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/adherents/class/cotisation.class.php
  20. * \ingroup member
  21. * \brief File of class to manage subscriptions of foundation members
  22. */
  23. require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
  24. /**
  25. * \class Cotisation
  26. * \brief Class to manage subscriptions of foundation members
  27. */
  28. class Cotisation extends CommonObject
  29. {
  30. var $id;
  31. var $db;
  32. var $error;
  33. var $errors;
  34. var $element='subscription';
  35. var $table_element='cotisation';
  36. var $datec;
  37. var $datem;
  38. var $dateh; // Subscription start date
  39. var $datef; // Subscription end date
  40. var $fk_adherent;
  41. var $amount;
  42. var $note;
  43. var $fk_bank;
  44. /**
  45. * \brief Constructor
  46. * \param DB Handler base de donnees
  47. */
  48. function Cotisation($DB)
  49. {
  50. $this->db = $DB;
  51. }
  52. /**
  53. * \brief Fonction qui permet de creer la cotisation
  54. * \param userid userid de celui qui insere
  55. * \return int <0 si KO, Id cotisation cree si OK
  56. */
  57. function create($userid)
  58. {
  59. global $langs;
  60. // Check parameters
  61. if ($this->datef <= $this->dateh)
  62. {
  63. $this->error=$langs->trans("ErrorBadValueForDate");
  64. return -1;
  65. }
  66. $sql = "INSERT INTO ".MAIN_DB_PREFIX."cotisation (fk_adherent, datec, dateadh, datef, cotisation, note)";
  67. $sql.= " VALUES (".$this->fk_adherent.", ".$this->db->idate(mktime()).",";
  68. $sql.= " ".$this->db->idate($this->dateh).",";
  69. $sql.= " ".$this->db->idate($this->datef).",";
  70. $sql.= " ".$this->amount.",'".$this->db->escape($this->note)."')";
  71. dol_syslog("Cotisation::create sql=".$sql);
  72. $resql = $this->db->query($sql);
  73. if ($resql)
  74. {
  75. return $this->db->last_insert_id(MAIN_DB_PREFIX."cotisation");
  76. }
  77. else
  78. {
  79. $this->error=$this->db->error();
  80. dol_syslog($this->error, LOG_ERR);
  81. return -1;
  82. }
  83. }
  84. /**
  85. * \brief Fonction qui permet de recuperer une cotisation
  86. * \param rowid Id cotisation
  87. * \return int <0 si KO, =0 si OK mais non trouve, >0 si OK
  88. */
  89. function fetch($rowid)
  90. {
  91. $sql ="SELECT rowid, fk_adherent, datec,";
  92. $sql.=" tms,";
  93. $sql.=" dateadh,";
  94. $sql.=" datef,";
  95. $sql.=" cotisation, note, fk_bank";
  96. $sql.=" FROM ".MAIN_DB_PREFIX."cotisation";
  97. $sql.=" WHERE rowid=".$rowid;
  98. dol_syslog("Cotisation::fetch sql=".$sql);
  99. $resql=$this->db->query($sql);
  100. if ($resql)
  101. {
  102. if ($this->db->num_rows($resql))
  103. {
  104. $obj = $this->db->fetch_object($resql);
  105. $this->id = $obj->rowid;
  106. $this->ref = $obj->rowid;
  107. $this->fk_adherent = $obj->fk_adherent;
  108. $this->datec = $this->db->jdate($obj->datec);
  109. $this->datem = $this->db->jdate($obj->tms);
  110. $this->dateh = $this->db->jdate($obj->dateadh);
  111. $this->datef = $this->db->jdate($obj->datef);
  112. $this->amount = $obj->cotisation;
  113. $this->note = $obj->note;
  114. $this->fk_bank = $obj->fk_bank;
  115. return 1;
  116. }
  117. else
  118. {
  119. return 0;
  120. }
  121. }
  122. else
  123. {
  124. $this->error=$this->db->error();
  125. return -1;
  126. }
  127. }
  128. /**
  129. * \brief Met a jour en base la cotisation
  130. * \param user Objet user qui met a jour
  131. * \param notrigger 0=Desactive les triggers
  132. * \param int <0 if KO, >0 if OK
  133. */
  134. function update($user,$notrigger=0)
  135. {
  136. $this->db->begin();
  137. $sql = "UPDATE ".MAIN_DB_PREFIX."cotisation SET ";
  138. $sql .= " fk_adherent = ".$this->fk_adherent.",";
  139. $sql .= " note=".($this->note ? "'".$this->db->escape($this->note)."'" : 'null').",";
  140. $sql .= " cotisation = '".price2num($this->amount)."',";
  141. $sql .= " dateadh='".$this->db->idate($this->dateh)."',";
  142. $sql .= " datef='".$this->db->idate($this->datef)."',";
  143. $sql .= " datec='".$this->db->idate($this->datec)."',";
  144. $sql .= " fk_bank = ".($this->fk_bank ? $this->fk_bank : 'null');
  145. $sql .= " WHERE rowid = ".$this->id;
  146. dol_syslog("Cotisation::update sql=".$sql);
  147. $resql = $this->db->query($sql);
  148. if ($resql)
  149. {
  150. $member=new Adherent($this->db);
  151. $result=$member->fetch($this->fk_adherent);
  152. $result=$member->update_end_date($user);
  153. $this->db->commit();
  154. return 1;
  155. }
  156. else
  157. {
  158. $this->db->rollback();
  159. $this->error=$this->db->error();
  160. dol_syslog("Cotisation::update ".$this->error, LOG_ERR);
  161. return -1;
  162. }
  163. }
  164. /**
  165. * \brief Delete a subscription
  166. * \param rowid Id cotisation
  167. * \return int <0 si KO, 0 si OK mais non trouve, >0 si OK
  168. */
  169. function delete($user)
  170. {
  171. // It subscription is linked to a bank transaction, we get it
  172. if ($this->fk_bank)
  173. {
  174. require_once(DOL_DOCUMENT_ROOT."/compta/bank/class/account.class.php");
  175. $accountline=new AccountLine($this->db);
  176. $result=$accountline->fetch($this->fk_bank);
  177. }
  178. $this->db->begin();
  179. $sql = "DELETE FROM ".MAIN_DB_PREFIX."cotisation WHERE rowid = ".$this->id;
  180. dol_syslog("Cotisation::delete sql=".$sql);
  181. $resql=$this->db->query($sql);
  182. if ($resql)
  183. {
  184. $num=$this->db->affected_rows($resql);
  185. if ($num)
  186. {
  187. require_once(DOL_DOCUMENT_ROOT."/adherents/class/adherent.class.php");
  188. $member=new Adherent($this->db);
  189. $result=$member->fetch($this->fk_adherent);
  190. $result=$member->update_end_date($user);
  191. if ($this->fk_bank)
  192. {
  193. $result=$accountline->delete($user); // Return false if refused because line is conciliated
  194. if ($result > 0)
  195. {
  196. $this->db->commit();
  197. return 1;
  198. }
  199. else
  200. {
  201. $this->error=$accountline->error;
  202. $this->db->rollback();
  203. return -1;
  204. }
  205. }
  206. else
  207. {
  208. $this->db->commit();
  209. return 1;
  210. }
  211. }
  212. else
  213. {
  214. $this->db->commit();
  215. return 0;
  216. }
  217. }
  218. else
  219. {
  220. $this->error=$this->db->lasterror();
  221. $this->db->rollback();
  222. return -1;
  223. }
  224. }
  225. /**
  226. * \brief Renvoie nom clicable (avec eventuellement le picto)
  227. * \param withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  228. * \return string Chaine avec URL
  229. */
  230. function getNomUrl($withpicto=0)
  231. {
  232. global $langs;
  233. $result='';
  234. $lien = '<a href="'.DOL_URL_ROOT.'/adherents/fiche_subscription.php?rowid='.$this->id.'">';
  235. $lienfin='</a>';
  236. $picto='payment';
  237. $label=$langs->trans("ShowSubscription");
  238. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  239. if ($withpicto && $withpicto != 2) $result.=' ';
  240. $result.=$lien.$this->ref.$lienfin;
  241. return $result;
  242. }
  243. /**
  244. * \brief Charge les informations d'ordre info dans l'objet cotisation
  245. * \param id Id adhesion a charger
  246. */
  247. function info($id)
  248. {
  249. $sql = 'SELECT c.rowid, c.datec,';
  250. $sql.= ' c.tms as datem';
  251. $sql.= ' FROM '.MAIN_DB_PREFIX.'cotisation as c';
  252. $sql.= ' WHERE c.rowid = '.$id;
  253. $result=$this->db->query($sql);
  254. if ($result)
  255. {
  256. if ($this->db->num_rows($result))
  257. {
  258. $obj = $this->db->fetch_object($result);
  259. $this->id = $obj->rowid;
  260. $this->date_creation = $this->db->jdate($obj->datec);
  261. $this->date_modification = $this->db->jdate($obj->datem);
  262. }
  263. $this->db->free($result);
  264. }
  265. else
  266. {
  267. dol_print_error($this->db);
  268. }
  269. }
  270. }
  271. ?>