subscription.class.php 15 KB

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