subscription.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/adherents/class/subscription.class.php
  20. * \ingroup member
  21. * \brief File of class to manage subscriptions of foundation members
  22. */
  23. //namespace DolibarrMember;
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  25. /**
  26. * Class to manage subscriptions of foundation members
  27. */
  28. class Subscription extends CommonObject
  29. {
  30. /**
  31. * @var string ID to identify managed object
  32. */
  33. public $element='subscription';
  34. /**
  35. * @var string Name of table without prefix where object is stored
  36. */
  37. public $table_element='subscription';
  38. /**
  39. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  40. */
  41. public $picto='payment';
  42. /**
  43. * Date creation record (datec)
  44. *
  45. * @var integer
  46. */
  47. public $datec;
  48. /**
  49. * Date modification record (tms)
  50. *
  51. * @var integer
  52. */
  53. public $datem;
  54. /**
  55. * Subscription start date (date subscription)
  56. *
  57. * @var integer
  58. */
  59. public $dateh;
  60. /**
  61. * Subscription end date
  62. *
  63. * @var integer
  64. */
  65. public $datef;
  66. /**
  67. * @var int ID
  68. */
  69. public $fk_type;
  70. public $fk_adherent;
  71. public $amount;
  72. /**
  73. * @var int ID
  74. */
  75. public $fk_bank;
  76. /**
  77. * Constructor
  78. *
  79. * @param DoliDB $db Database handler
  80. */
  81. public function __construct($db)
  82. {
  83. $this->db = $db;
  84. }
  85. /**
  86. * Function who permitted cretaion of the subscription
  87. *
  88. * @param User $user User that create
  89. * @param bool $notrigger false=launch triggers after, true=disable triggers
  90. * @return int <0 if KO, Id subscription created if OK
  91. */
  92. public function create($user, $notrigger = false)
  93. {
  94. global $langs;
  95. $error = 0;
  96. $now=dol_now();
  97. // Check parameters
  98. if ($this->datef <= $this->dateh)
  99. {
  100. $this->error=$langs->trans("ErrorBadValueForDate");
  101. return -1;
  102. }
  103. if (empty($this->datec)) $this->datec = $now;
  104. $this->db->begin();
  105. $sql = "INSERT INTO ".MAIN_DB_PREFIX."subscription (fk_adherent, fk_type, datec, dateadh, datef, subscription, note)";
  106. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  107. $member=new Adherent($this->db);
  108. $result=$member->fetch($this->fk_adherent);
  109. if ($this->fk_type == null) { // If type not defined, we use the type of member
  110. $type=$member->typeid;
  111. } else {
  112. $type=$this->fk_type;
  113. }
  114. $sql.= " VALUES (".$this->fk_adherent.", '".$type."', '".$this->db->idate($now)."',";
  115. $sql.= " '".$this->db->idate($this->dateh)."',";
  116. $sql.= " '".$this->db->idate($this->datef)."',";
  117. $sql.= " ".$this->amount.",";
  118. $sql.= " '".$this->db->escape($this->note_public?$this->note_public:$this->note)."')";
  119. $resql = $this->db->query($sql);
  120. if (! $resql) {
  121. $error++;
  122. $this->errors[] = $this->db->lasterror();
  123. }
  124. if (! $error)
  125. {
  126. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
  127. $this->fk_type = $type;
  128. }
  129. if (! $error && ! $notrigger)
  130. {
  131. $this->context = array('member'=>$member);
  132. // Call triggers
  133. $result=$this->call_trigger('MEMBER_SUBSCRIPTION_CREATE', $user);
  134. if ($result < 0) { $error++; }
  135. // End call triggers
  136. }
  137. // Commit or rollback
  138. if ($error) {
  139. $this->db->rollback();
  140. return -1;
  141. } else {
  142. $this->db->commit();
  143. return $this->id;
  144. }
  145. }
  146. /**
  147. * Method to load a subscription
  148. *
  149. * @param int $rowid Id subscription
  150. * @return int <0 if KO, =0 if not found, >0 if OK
  151. */
  152. public function fetch($rowid)
  153. {
  154. $sql ="SELECT rowid, fk_type, fk_adherent, datec,";
  155. $sql.=" tms,";
  156. $sql.=" dateadh as dateh,";
  157. $sql.=" datef,";
  158. $sql.=" subscription, note, fk_bank";
  159. $sql.=" FROM ".MAIN_DB_PREFIX."subscription";
  160. $sql.=" WHERE rowid=".$rowid;
  161. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  162. $resql=$this->db->query($sql);
  163. if ($resql)
  164. {
  165. if ($this->db->num_rows($resql))
  166. {
  167. $obj = $this->db->fetch_object($resql);
  168. $this->id = $obj->rowid;
  169. $this->ref = $obj->rowid;
  170. $this->fk_type = $obj->fk_type;
  171. $this->fk_adherent = $obj->fk_adherent;
  172. $this->datec = $this->db->jdate($obj->datec);
  173. $this->datem = $this->db->jdate($obj->tms);
  174. $this->dateh = $this->db->jdate($obj->dateh);
  175. $this->datef = $this->db->jdate($obj->datef);
  176. $this->amount = $obj->subscription;
  177. $this->note = $obj->note;
  178. $this->fk_bank = $obj->fk_bank;
  179. return 1;
  180. }
  181. else
  182. {
  183. return 0;
  184. }
  185. }
  186. else
  187. {
  188. $this->error=$this->db->lasterror();
  189. return -1;
  190. }
  191. }
  192. /**
  193. * Update subscription
  194. *
  195. * @param User $user User who updated
  196. * @param int $notrigger 0=Disable triggers
  197. * @return int <0 if KO, >0 if OK
  198. */
  199. public function update($user, $notrigger = 0)
  200. {
  201. $error = 0;
  202. $this->db->begin();
  203. $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET ";
  204. $sql .= " fk_type = ".$this->fk_type.",";
  205. $sql .= " fk_adherent = ".$this->fk_adherent.",";
  206. $sql .= " note=".($this->note ? "'".$this->db->escape($this->note)."'" : 'null').",";
  207. $sql .= " subscription = '".price2num($this->amount)."',";
  208. $sql .= " dateadh='".$this->db->idate($this->dateh)."',";
  209. $sql .= " datef='".$this->db->idate($this->datef)."',";
  210. $sql .= " datec='".$this->db->idate($this->datec)."',";
  211. $sql .= " fk_bank = ".($this->fk_bank ? $this->fk_bank : 'null');
  212. $sql .= " WHERE rowid = ".$this->id;
  213. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  214. $resql = $this->db->query($sql);
  215. if ($resql)
  216. {
  217. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  218. $member=new Adherent($this->db);
  219. $result=$member->fetch($this->fk_adherent);
  220. $result=$member->update_end_date($user);
  221. if (! $error && ! $notrigger) {
  222. $this->context = array('member'=>$member);
  223. // Call triggers
  224. $result=$this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY', $user);
  225. if ($result < 0) { $error++; } //Do also here what you must do to rollback action if trigger fail
  226. // End call triggers
  227. }
  228. }
  229. else
  230. {
  231. $error++;
  232. $this->error=$this->db->lasterror();
  233. }
  234. // Commit or rollback
  235. if ($error) {
  236. $this->db->rollback();
  237. return -1;
  238. } else {
  239. $this->db->commit();
  240. return $this->id;
  241. }
  242. }
  243. /**
  244. * Delete a subscription
  245. *
  246. * @param User $user User that delete
  247. * @param bool $notrigger false=launch triggers after, true=disable triggers
  248. * @return int <0 if KO, 0 if not found, >0 if OK
  249. */
  250. public function delete($user, $notrigger = false)
  251. {
  252. $error = 0;
  253. // It subscription is linked to a bank transaction, we get it
  254. if ($this->fk_bank > 0)
  255. {
  256. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  257. $accountline=new AccountLine($this->db);
  258. $result=$accountline->fetch($this->fk_bank);
  259. }
  260. $this->db->begin();
  261. if (! $error) {
  262. if (! $notrigger) {
  263. // Call triggers
  264. $result=$this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user);
  265. if ($result < 0) { $error++; } // Do also here what you must do to rollback action if trigger fail
  266. // End call triggers
  267. }
  268. }
  269. if (! $error)
  270. {
  271. $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE rowid = ".$this->id;
  272. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  273. $resql=$this->db->query($sql);
  274. if ($resql)
  275. {
  276. $num=$this->db->affected_rows($resql);
  277. if ($num)
  278. {
  279. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  280. $member=new Adherent($this->db);
  281. $result=$member->fetch($this->fk_adherent);
  282. $result=$member->update_end_date($user);
  283. if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) // If we found bank account line (this means this->fk_bank defined)
  284. {
  285. $result=$accountline->delete($user); // Return false if refused because line is conciliated
  286. if ($result > 0)
  287. {
  288. $this->db->commit();
  289. return 1;
  290. }
  291. else
  292. {
  293. $this->error=$accountline->error;
  294. $this->db->rollback();
  295. return -1;
  296. }
  297. }
  298. else
  299. {
  300. $this->db->commit();
  301. return 1;
  302. }
  303. }
  304. else
  305. {
  306. $this->db->commit();
  307. return 0;
  308. }
  309. }
  310. else
  311. {
  312. $error++;
  313. $this->error=$this->db->lasterror();
  314. }
  315. }
  316. // Commit or rollback
  317. if ($error) {
  318. $this->db->rollback();
  319. return -1;
  320. } else {
  321. $this->db->commit();
  322. return 1;
  323. }
  324. }
  325. /**
  326. * Return clicable name (with picto eventually)
  327. *
  328. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  329. * @param int $notooltip 1=Disable tooltip
  330. * @param string $option Page for link ('', 'nolink', ...)
  331. * @param string $morecss Add more css on link
  332. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  333. * @return string Chaine avec URL
  334. */
  335. public function getNomUrl($withpicto = 0, $notooltip = 0, $option = '', $morecss = '', $save_lastsearch_value = -1)
  336. {
  337. global $langs;
  338. $result='';
  339. $langs->load("members");
  340. $label=$langs->trans("ShowSubscription").': '.$this->ref;
  341. $url = DOL_URL_ROOT.'/adherents/subscription/card.php?rowid='.$this->id;
  342. if ($option != 'nolink')
  343. {
  344. // Add param to save lastsearch_values or not
  345. $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
  346. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
  347. if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
  348. }
  349. $linkstart = '<a href="'.$url.'" class="classfortooltip" title="'.dol_escape_htmltag($label, 1).'">';
  350. $linkend = '</a>';
  351. $picto='payment';
  352. $result .= $linkstart;
  353. 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);
  354. if ($withpicto != 2) $result.= $this->ref;
  355. $result .= $linkend;
  356. return $result;
  357. }
  358. /**
  359. * Retourne le libelle du statut d'une adhesion
  360. *
  361. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  362. * @return string Label
  363. */
  364. public function getLibStatut($mode = 0)
  365. {
  366. return '';
  367. }
  368. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  369. /**
  370. * Renvoi le libelle d'un statut donne
  371. *
  372. * @param int $statut Id statut
  373. * @return string Label
  374. */
  375. public function LibStatut($statut)
  376. {
  377. // phpcs:enable
  378. global $langs;
  379. $langs->load("members");
  380. return '';
  381. }
  382. /**
  383. * Load information of the subscription object
  384. *
  385. * @param int $id Id subscription
  386. * @return void
  387. */
  388. public function info($id)
  389. {
  390. $sql = 'SELECT c.rowid, c.datec,';
  391. $sql.= ' c.tms as datem';
  392. $sql.= ' FROM '.MAIN_DB_PREFIX.'subscription as c';
  393. $sql.= ' WHERE c.rowid = '.$id;
  394. $result=$this->db->query($sql);
  395. if ($result)
  396. {
  397. if ($this->db->num_rows($result))
  398. {
  399. $obj = $this->db->fetch_object($result);
  400. $this->id = $obj->rowid;
  401. $this->date_creation = $this->db->jdate($obj->datec);
  402. $this->date_modification = $this->db->jdate($obj->datem);
  403. }
  404. $this->db->free($result);
  405. }
  406. else
  407. {
  408. dol_print_error($this->db);
  409. }
  410. }
  411. }