subscription.class.php 17 KB

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