subscription.class.php 16 KB

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