cotisation.class.php 7.8 KB

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