dolibarr 19.0.4
subscription.class.php
Go to the documentation of this file.
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
25//namespace DolibarrMember;
26
27require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
28
29
34{
38 public $element = 'subscription';
39
43 public $table_element = 'subscription';
44
48 public $ismultientitymanaged = 'fk_adherent@adherent';
49
53 public $picto = 'payment';
54
60 public $datec;
61
67 public $datem;
68
74 public $dateh;
75
81 public $datef;
82
86 public $fk_type;
87
91 public $fk_adherent;
92
96 public $amount;
97
101 public $fk_bank;
102
106 public $fields = array(
107 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
108 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>15),
109 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>20),
110 'fk_adherent' =>array('type'=>'integer', 'label'=>'Member', 'enabled'=>1, 'visible'=>-1, 'position'=>25),
111 'dateadh' =>array('type'=>'datetime', 'label'=>'DateSubscription', 'enabled'=>1, 'visible'=>-1, 'position'=>30),
112 'datef' =>array('type'=>'datetime', 'label'=>'DateEndSubscription', 'enabled'=>1, 'visible'=>-1, 'position'=>35),
113 'subscription' =>array('type'=>'double(24,8)', 'label'=>'Amount', 'enabled'=>1, 'visible'=>-1, 'position'=>40, 'isameasure'=>1),
114 'fk_bank' =>array('type'=>'integer', 'label'=>'BankId', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
115 'note' =>array('type'=>'html', 'label'=>'Note', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
116 'fk_type' =>array('type'=>'integer', 'label'=>'MemberType', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
117 'fk_user_creat' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'position'=>60),
118 'fk_user_valid' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
119 );
120
121
127 public function __construct($db)
128 {
129 $this->db = $db;
130 }
131
132
140 public function create($user, $notrigger = false)
141 {
142 global $langs;
143
144 $error = 0;
145
146 $now = dol_now();
147
148 // Check parameters
149 if ($this->datef <= $this->dateh) {
150 $this->error = $langs->trans("ErrorBadValueForDate");
151 return -1;
152 }
153 if (empty($this->datec)) {
154 $this->datec = $now;
155 }
156
157 $this->db->begin();
158
159 $sql = "INSERT INTO ".MAIN_DB_PREFIX."subscription (fk_adherent, fk_type, datec, dateadh, datef, subscription, note)";
160
161 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
162 $member = new Adherent($this->db);
163 $result = $member->fetch($this->fk_adherent);
164
165 if ($this->fk_type == null) { // If type not defined, we use the type of member
166 $type = $member->typeid;
167 } else {
168 $type = $this->fk_type;
169 }
170 $sql .= " VALUES (".((int) $this->fk_adherent).", '".$this->db->escape($type)."', '".$this->db->idate($now)."',";
171 $sql .= " '".$this->db->idate($this->dateh)."',";
172 $sql .= " '".$this->db->idate($this->datef)."',";
173 $sql .= " ".((float) $this->amount).",";
174 $sql .= " '".$this->db->escape($this->note_public ? $this->note_public : $this->note)."')";
175
176 $resql = $this->db->query($sql);
177 if (!$resql) {
178 $error++;
179 $this->errors[] = $this->db->lasterror();
180 }
181
182 if (!$error) {
183 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
184 $this->fk_type = $type;
185 }
186
187 if (!$error && !$notrigger) {
188 $this->context = array('member' => $member);
189 // Call triggers
190 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_CREATE', $user);
191 if ($result < 0) {
192 $error++;
193 }
194 // End call triggers
195 }
196
197 // Commit or rollback
198 if ($error) {
199 $this->db->rollback();
200 return -1;
201 } else {
202 $this->db->commit();
203 return $this->id;
204 }
205 }
206
207
214 public function fetch($rowid)
215 {
216 $sql = "SELECT rowid, fk_type, fk_adherent, datec,";
217 $sql .= " tms,";
218 $sql .= " dateadh as dateh,";
219 $sql .= " datef,";
220 $sql .= " subscription, note as note_public, fk_bank";
221 $sql .= " FROM ".MAIN_DB_PREFIX."subscription";
222 $sql .= " WHERE rowid = ".((int) $rowid);
223
224 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
225 $resql = $this->db->query($sql);
226 if ($resql) {
227 if ($this->db->num_rows($resql)) {
228 $obj = $this->db->fetch_object($resql);
229
230 $this->id = $obj->rowid;
231 $this->ref = $obj->rowid;
232
233 $this->fk_type = $obj->fk_type;
234 $this->fk_adherent = $obj->fk_adherent;
235 $this->datec = $this->db->jdate($obj->datec);
236 $this->datem = $this->db->jdate($obj->tms);
237 $this->dateh = $this->db->jdate($obj->dateh);
238 $this->datef = $this->db->jdate($obj->datef);
239 $this->amount = $obj->subscription;
240 $this->note = $obj->note_public; // deprecated
241 $this->note_public = $obj->note_public;
242 $this->fk_bank = $obj->fk_bank;
243 return 1;
244 } else {
245 return 0;
246 }
247 } else {
248 $this->error = $this->db->lasterror();
249 return -1;
250 }
251 }
252
253
261 public function update($user, $notrigger = 0)
262 {
263 $error = 0;
264
265 $this->db->begin();
266
267 if (!is_numeric($this->amount)) {
268 $this->error = 'BadValueForParameterAmount';
269 return -1;
270 }
271
272 if (empty($this->note_public) && !empty($this->note)) { // For backward compatibility
273 $this->note_public = $this->note;
274 }
275
276 $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET ";
277 $sql .= " fk_type = ".((int) $this->fk_type).",";
278 $sql .= " fk_adherent = ".((int) $this->fk_adherent).",";
279 $sql .= " note = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : 'null').",";
280 $sql .= " subscription = ".price2num($this->amount).",";
281 $sql .= " dateadh = '".$this->db->idate($this->dateh)."',";
282 $sql .= " datef = '".$this->db->idate($this->datef)."',";
283 $sql .= " datec = '".$this->db->idate($this->datec)."',";
284 $sql .= " fk_bank = ".($this->fk_bank ? ((int) $this->fk_bank) : 'null');
285 $sql .= " WHERE rowid = ".((int) $this->id);
286
287 dol_syslog(get_class($this)."::update", LOG_DEBUG);
288 $resql = $this->db->query($sql);
289 if ($resql) {
290 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
291 $member = new Adherent($this->db);
292 $result = $member->fetch($this->fk_adherent);
293 $result = $member->update_end_date($user);
294
295 if (!$error && !$notrigger) {
296 $this->context = array('member'=>$member);
297 // Call triggers
298 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY', $user);
299 if ($result < 0) {
300 $error++;
301 } //Do also here what you must do to rollback action if trigger fail
302 // End call triggers
303 }
304 } else {
305 $error++;
306 $this->error = $this->db->lasterror();
307 }
308
309 // Commit or rollback
310 if ($error) {
311 $this->db->rollback();
312 return -1;
313 } else {
314 $this->db->commit();
315 return $this->id;
316 }
317 }
318
326 public function delete($user, $notrigger = false)
327 {
328 $error = 0;
329
330 // It subscription is linked to a bank transaction, we get it
331 if ($this->fk_bank > 0) {
332 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
333 $accountline = new AccountLine($this->db);
334 $result = $accountline->fetch($this->fk_bank);
335 }
336
337 $this->db->begin();
338
339 if (!$error) {
340 if (!$notrigger) {
341 // Call triggers
342 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user);
343 if ($result < 0) {
344 $error++;
345 } // Do also here what you must do to rollback action if trigger fail
346 // End call triggers
347 }
348 }
349
350 if (!$error) {
351 $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE rowid = ".((int) $this->id);
352 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
353 $resql = $this->db->query($sql);
354 if ($resql) {
355 $num = $this->db->affected_rows($resql);
356 if ($num) {
357 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
358 $member = new Adherent($this->db);
359 $result = $member->fetch($this->fk_adherent);
360 $result = $member->update_end_date($user);
361
362 if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) { // If we found bank account line (this means this->fk_bank defined)
363 $result = $accountline->delete($user); // Return false if refused because line is reconciled
364 if ($result > 0) {
365 $this->db->commit();
366 return 1;
367 } else {
368 $this->error = $accountline->error;
369 $this->db->rollback();
370 return -1;
371 }
372 } else {
373 $this->db->commit();
374 return 1;
375 }
376 } else {
377 $this->db->commit();
378 return 0;
379 }
380 } else {
381 $error++;
382 $this->error = $this->db->lasterror();
383 }
384 }
385
386 // Commit or rollback
387 if ($error) {
388 $this->db->rollback();
389 return -1;
390 } else {
391 $this->db->commit();
392 return 1;
393 }
394 }
395
396
407 public function getNomUrl($withpicto = 0, $notooltip = 0, $option = '', $morecss = '', $save_lastsearch_value = -1)
408 {
409 global $langs;
410
411 $result = '';
412
413 $langs->load("members");
414
415 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Subscription").'</u>';
416 /*if (isset($this->statut)) {
417 $label .= ' '.$this->getLibStatut(5);
418 }*/
419 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
420 if (!empty($this->dateh)) {
421 $label .= '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->dateh, 'day');
422 }
423 if (!empty($this->datef)) {
424 $label .= '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->datef, 'day');
425 }
426
427 $url = DOL_URL_ROOT.'/adherents/subscription/card.php?rowid='.((int) $this->id);
428
429 if ($option != 'nolink') {
430 // Add param to save lastsearch_values or not
431 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
432 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
433 $add_save_lastsearch_values = 1;
434 }
435 if ($add_save_lastsearch_values) {
436 $url .= '&save_lastsearch_values=1';
437 }
438 }
439
440 $linkstart = '<a href="'.$url.'" class="classfortooltip" title="'.dol_escape_htmltag($label, 1).'">';
441 $linkend = '</a>';
442
443 $result .= $linkstart;
444 if ($withpicto) {
445 $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);
446 }
447 if ($withpicto != 2) {
448 $result .= $this->ref;
449 }
450 $result .= $linkend;
451
452 return $result;
453 }
454
455
462 public function getLibStatut($mode = 0)
463 {
464 return '';
465 }
466
467 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
475 public function LibStatut($status, $mode = 0)
476 {
477 // phpcs:enable
478 global $langs;
479
480 //$langs->load("members");
481
482 return '';
483 }
484
491 public function info($id)
492 {
493 $sql = 'SELECT c.rowid, c.datec,';
494 $sql .= ' c.tms as datem';
495 $sql .= ' FROM '.MAIN_DB_PREFIX.'subscription as c';
496 $sql .= ' WHERE c.rowid = '.((int) $id);
497
498 $resql = $this->db->query($sql);
499 if ($resql) {
500 if ($this->db->num_rows($resql)) {
501 $obj = $this->db->fetch_object($resql);
502 $this->id = $obj->rowid;
503
504 $this->date_creation = $this->db->jdate($obj->datec);
505 $this->date_modification = $this->db->jdate($obj->datem);
506 }
507
508 $this->db->free($resql);
509 } else {
510 dol_print_error($this->db);
511 }
512 }
513
521 public function getKanbanView($option = '', $arraydata = null)
522 {
523 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
524
525 $return = '<div class="box-flex-item box-flex-grow-zero">';
526 $return .= '<div class="info-box info-box-sm">';
527 $return .= '<span class="info-box-icon bg-infobox-action">';
528 $return .= img_picto('', $this->picto);
529 $return .= '</span>';
530
531 $return .= '<div class="info-box-content">';
532 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">';
533 $return .= $this->getNomUrl(-1);
534 $return .= '</span>';
535 if ($selected >= 0) {
536 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
537 }
538 if (property_exists($this, 'dateh') || property_exists($this, 'datef')) {
539 $return .= '<br><span class="info-box-status opacitymedium small">'.dol_print_date($this->dateh, 'day').' - '.dol_print_date($this->datef, 'day').'</span>';
540 }
541
542 if (!empty($arraydata['member']) && is_object($arraydata['member'])) {
543 $return .= '<br><span class="inline-block">'.$arraydata['member']->getNomUrl(-4).'</span>';
544 }
545
546 if (property_exists($this, 'amount')) {
547 $return .= '<br><span class="margintoponly amount inline-block">'.price($this->amount).'</span>';
548 if (!empty($arraydata['bank'])) {
549 $return .= ' &nbsp; <span class="info-box-label ">'.$arraydata['bank']->getNomUrl(-1).'</span>';
550 }
551 }
552 $return .= '</div>';
553 $return .= '</div>';
554 $return .= '</div>';
555 return $return;
556 }
557}
$object ref
Definition info.php:79
Class to manage bank transaction lines.
Class to manage members of a foundation.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage subscriptions of foundation members.
fetch($rowid)
Method to load a subscription.
getLibStatut($mode=0)
Return the label of the status.
info($id)
Load information of the subscription object.
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
__construct($db)
Constructor.
getNomUrl($withpicto=0, $notooltip=0, $option='', $morecss='', $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
LibStatut($status, $mode=0)
Return the label of a given status.
create($user, $notrigger=false)
Function who permitted creation of the subscription.
update($user, $notrigger=0)
Update subscription.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...