dolibarr 25.0.0-alpha
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 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27//namespace DolibarrMember;
28
29require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
30
31
36{
40 public $element = 'subscription';
41
45 public $table_element = 'subscription';
46
50 public $picto = 'payment';
51
57 public $datec;
58
64 public $datem;
65
71 public $dateh;
72
78 public $datef;
79
88 public $date_start;
89
96 public $date_end;
97
105 public $note_public;
106
110 public $fk_type;
111
115 public $fk_adherent;
116
120 public $member_firstname;
121
125 public $member_lastname;
126
130 public $member_company;
131
135 public $amount;
136
140 public $fk_bank;
141
145 public $fields = array(
146 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
147 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 15),
148 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 20),
149 'fk_adherent' => array('type' => 'integer', 'label' => 'Member', 'enabled' => 1, 'visible' => -1, 'position' => 25),
150 'dateadh' => array('type' => 'datetime', 'label' => 'DateSubscription', 'enabled' => 1, 'visible' => -1, 'position' => 30),
151 'datef' => array('type' => 'datetime', 'label' => 'DateEndSubscription', 'enabled' => 1, 'visible' => -1, 'position' => 35),
152 'subscription' => array('type' => 'double(24,8)', 'label' => 'Amount', 'enabled' => 1, 'visible' => -1, 'position' => 40, 'isameasure' => 1),
153 'fk_bank' => array('type' => 'integer', 'label' => 'BankId', 'enabled' => 1, 'visible' => -1, 'position' => 45),
154 'note' => array('type' => 'html', 'label' => 'Note', 'enabled' => 1, 'visible' => -1, 'position' => 50),
155 'fk_type' => array('type' => 'integer', 'label' => 'MemberType', 'enabled' => 1, 'visible' => -1, 'position' => 55),
156 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -2, 'position' => 60),
157 'fk_user_valid' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserValidation', 'enabled' => 1, 'visible' => -1, 'position' => 65),
158 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 805),
159 );
160
161
167 public function __construct($db)
168 {
169 $this->db = $db;
170
171 $this->ismultientitymanaged = 'fk_adherent@adherent';
172 }
173
174
182 public function create($user, $notrigger = 0)
183 {
184 global $langs;
185 dol_syslog(__METHOD__.'::begin', LOG_DEBUG);
186
187 $error = 0;
188
189 $now = dol_now();
190
191 // Clean parameters
192 if (isset($this->import_key)) {
193 $this->import_key = trim($this->import_key);
194 }
195
196 // Check parameters
197 if ($this->datef <= $this->dateh) {
198 $this->error = $langs->trans("ErrorBadValueForDate");
199 return -1;
200 }
201 if (empty($this->datec)) {
202 $this->datec = $now;
203 }
204
205 $this->db->begin();
206
207 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
208 $member = new Adherent($this->db);
209 $result = $member->fetch($this->fk_adherent);
210
211 // Check if member status allows subscription creation
212 $allowedStatusesStr = getDolGlobalString('MEMBER_SUBSCRIPTION_ALLOWED_FOR_STATUS');
213 if ($allowedStatusesStr === '') {
215 }
216 $allowedStatuses = array_map('intval', explode(',', $allowedStatusesStr));
217 if (!in_array((int) $member->statut, $allowedStatuses, true)) {
218 $this->error = $langs->trans('ErrorSubscriptionNotAllowed', $member->getLibStatut(1));
219 dol_syslog(get_class($this).'::create User='.$user->id.' tried to create subscription for member='.$member->id.' with status='.$member->statut.', allowed='.$allowedStatusesStr, LOG_WARNING);
220 $this->db->rollback();
221 return -1;
222 }
223
224 if ($this->fk_type == null) { // If type not defined, we use the type of member
225 $type = $member->typeid;
226 } else {
227 $type = $this->fk_type;
228 }
229
230 $sql = "INSERT INTO ".MAIN_DB_PREFIX."subscription (fk_adherent, fk_type, datec, dateadh, datef, subscription, note, note_private, ref_ext, fk_user_creat, import_key)";
231 $sql .= " VALUES (".((int) $this->fk_adherent).", '".$this->db->escape((string) $type)."', '".$this->db->idate($now)."',";
232 $sql .= " '".$this->db->idate($this->dateh)."',";
233 $sql .= " '".$this->db->idate($this->datef)."',";
234 $sql .= " ".((float) $this->amount).",";
235 $sql .= " '".$this->db->escape($this->note_public ? $this->note_public : $this->note)."',";
236 $sql .= " '".$this->db->escape((string) $this->note_private)."',";
237 $sql .= " ".(empty($this->ref_ext) ? "null" : "'".$this->db->escape($this->ref_ext)."'").",";
238 $sql .= " ".((int) ($this->user_creation_id > 0 ? $this->user_creation_id : ((int) $user->id)));
239 $sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
240 $sql .= ")";
241
242 $resql = $this->db->query($sql);
243 if (!$resql) {
244 $error++;
245 $this->errors[] = $this->db->lasterror();
246 }
247
248 if (!$error) {
249 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
250 $this->fk_type = $type;
251 }
252
253 // Update the denormalized end date of subscription of the member, like update() and delete() do
254 if (!$error) {
255 $result = $member->update_end_date($user);
256 if ($result < 0) {
257 $error++;
258 $this->error = $member->error;
259 $this->errors[] = $this->error;
260 }
261 }
262
263 // Recording a subscription means the person became a member: validate a draft member
264 if (!$error && (int) $member->statut == Adherent::STATUS_DRAFT) {
265 $result = $member->validate($user);
266 if ($result < 0) {
267 $error++;
268 $this->error = $langs->trans('ErrorValidationFailed', $member->error);
269 dol_syslog(get_class($this).': ErrorValidationFailed for member='.$member->id, LOG_ERR);
270 } else {
271 dol_syslog('Auto-validated member '.$member->id.' upon subscription creation', LOG_INFO);
272 }
273 }
274
275 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
276 $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
277 }
278
279 // Add object linked
280 if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
281 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
282 if (is_array($tmp_origin_id)) { // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...))
283 foreach ($tmp_origin_id as $origin_id) {
284 $ret = $this->add_object_linked($origin, $origin_id);
285 if (!$ret) {
286 $this->error = $this->db->lasterror();
287 $error++;
288 }
289 }
290 } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
291 $origin_id = $tmp_origin_id;
292 $ret = $this->add_object_linked($origin, $origin_id);
293 if (!$ret) {
294 $this->error = $this->db->lasterror();
295 $error++;
296 }
297 }
298 }
299 }
300
301 if (!$error && !$notrigger) {
302 $this->context = array('member' => $member);
303 // Call triggers
304 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_CREATE', $user);
305 if ($result < 0) {
306 $error++;
307 }
308 // End call triggers
309 }
310
311 // Commit or rollback
312 if ($error) {
313 $this->db->rollback();
314 return -1;
315 } else {
316 $this->db->commit();
317 return $this->id;
318 }
319 }
320
321
328 public function fetch($rowid)
329 {
330 $sql = "SELECT s.rowid, s.fk_type, s.fk_adherent, s.datec,";
331 $sql .= " s.tms,";
332 $sql .= " s.dateadh as dateh,";
333 $sql .= " s.datef,";
334 $sql .= " s.subscription, s.note as note_public, s.fk_bank,";
335 $sql .= " a.firstname as member_firstname, a.lastname as member_lastname, a.societe as member_company";
336 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as s";
337 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent as a ON a.rowid = s.fk_adherent";
338 $sql .= " WHERE s.rowid = ".((int) $rowid);
339
340 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
341 $resql = $this->db->query($sql);
342 if ($resql) {
343 if ($this->db->num_rows($resql)) {
344 $obj = $this->db->fetch_object($resql);
345
346 $this->id = $obj->rowid;
347 $this->ref = $obj->rowid;
348
349 $this->fk_type = $obj->fk_type;
350 $this->fk_adherent = $obj->fk_adherent;
351 $this->member_firstname = $obj->member_firstname;
352 $this->member_lastname = $obj->member_lastname;
353 $this->member_company = $obj->member_company;
354 $this->datec = $this->db->jdate($obj->datec);
355 $this->datem = $this->db->jdate($obj->tms);
356 $this->dateh = $this->db->jdate($obj->dateh);
357 $this->datef = $this->db->jdate($obj->datef);
358 $this->amount = $obj->subscription;
359 $this->note = $obj->note_public; // deprecated
360 $this->note_public = $obj->note_public;
361 $this->fk_bank = $obj->fk_bank;
362 return 1;
363 } else {
364 return 0;
365 }
366 } else {
367 $this->error = $this->db->lasterror();
368 return -1;
369 }
370 }
371
372
380 public function update($user, $notrigger = 0)
381 {
382 $error = 0;
383
384 $this->db->begin();
385
386 if (!is_numeric($this->amount)) {
387 $this->error = 'BadValueForParameterAmount';
388 return -1;
389 }
390
391 if (empty($this->note_public) && !empty($this->note)) { // For backward compatibility
392 $this->note_public = $this->note;
393 }
394
395 $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET ";
396 $sql .= " fk_type = ".((int) $this->fk_type).",";
397 $sql .= " fk_adherent = ".((int) $this->fk_adherent).",";
398 $sql .= " note = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : 'null').",";
399 $sql .= " subscription = ".(float) price2num($this->amount).",";
400 $sql .= " dateadh = '".$this->db->idate($this->dateh)."',";
401 $sql .= " datef = '".$this->db->idate($this->datef)."',";
402 $sql .= " datec = '".$this->db->idate($this->datec)."',";
403 $sql .= " fk_bank = ".($this->fk_bank ? ((int) $this->fk_bank) : 'null');
404 $sql .= " WHERE rowid = ".((int) $this->id);
405
406 dol_syslog(get_class($this)."::update", LOG_DEBUG);
407 $resql = $this->db->query($sql);
408 if ($resql) {
409 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
410 $member = new Adherent($this->db);
411 $result = $member->fetch($this->fk_adherent);
412 $result = $member->update_end_date($user);
413
414 if (!$notrigger) {
415 $this->context = array('member' => $member);
416 // Call triggers
417 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY', $user);
418 if ($result < 0) {
419 $error++;
420 } //Do also here what you must do to rollback action if trigger fail
421 // End call triggers
422 }
423 } else {
424 $error++;
425 $this->error = $this->db->lasterror();
426 }
427
428 // Commit or rollback
429 if ($error) {
430 $this->db->rollback();
431 return -1;
432 } else {
433 $this->db->commit();
434 return $this->id;
435 }
436 }
437
445 public function delete($user, $notrigger = 0)
446 {
447 $error = 0;
448
449 // It subscription is linked to a bank transaction, we get it
450 if ($this->fk_bank > 0) {
451 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
452 $accountline = new AccountLine($this->db);
453 $result = $accountline->fetch($this->fk_bank);
454 } else {
455 $accountline = null;
456 }
457
458 $this->db->begin();
459
460 if (!$notrigger) {
461 // Call triggers
462 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user);
463 if ($result < 0) {
464 $error++;
465 } // Do also here what you must do to rollback action if trigger fail
466 // End call triggers
467 }
468
469 if (!$error) {
470 $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE rowid = ".((int) $this->id);
471 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
472 $resql = $this->db->query($sql);
473 if ($resql) {
474 $num = $this->db->affected_rows($resql);
475 if ($num) {
476 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
477 $member = new Adherent($this->db);
478 $result = $member->fetch($this->fk_adherent);
479 $result = $member->update_end_date($user);
480
481 if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) { // If we found bank account line (this means this->fk_bank defined)
482 $result = $accountline->delete($user); // Return false if refused because line is reconciled
483 if ($result <= 0) {
484 $this->setErrorsFromObject($accountline);
485 $error++;
486 }
487 }
488 } else {
489 $this->db->commit();
490 return 0;
491 }
492 } else {
493 $error++;
494 $this->error = $this->db->lasterror();
495 }
496 }
497
498 // Commit or rollback
499 if ($error) {
500 $this->db->rollback();
501 return -1;
502 } else {
503 $this->db->commit();
504 return 1;
505 }
506 }
507
508
519 public function getNomUrl($withpicto = 0, $notooltip = 0, $option = '', $morecss = '', $save_lastsearch_value = -1)
520 {
521 global $langs;
522
523 $result = '';
524
525 $langs->load("members");
526 $langs->load("main");
527
528 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Subscription").'</u>';
529 /*if (isset($this->statut)) {
530 $label .= ' '.$this->getLibStatut(5);
531 }*/
532 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
533 $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->note_public;
534 $memberName = dolGetFirstLastname($this->member_firstname, $this->member_lastname);
535 if (empty($memberName)) {
536 $memberName = $this->member_company;
537 }
538 if (!empty($memberName)) {
539 $label .= '<br><b>'.$langs->trans('Member').':</b> '.$memberName;
540 }
541 if (!empty($this->dateh)) {
542 $label .= '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->dateh, 'day');
543 }
544 if (!empty($this->datef)) {
545 $label .= '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->datef, 'day');
546 }
547 $baseurl = DOL_URL_ROOT . '/adherents/subscription/card.php';
548 $query = ['rowid' => $this->id];
549 if ($option != 'nolink') {
550 // Add param to save lastsearch_values or not
551 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
552 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
553 $add_save_lastsearch_values = 1;
554 }
555 if ($add_save_lastsearch_values) {
556 $query = array_merge($query, ['save_lastsearch_values' => 1]);
557 }
558 }
559 $url = dolBuildUrl($baseurl, $query);
560
561 $linkstart = '<a href="'.$url.'" class="classfortooltip" title="'.dol_escape_htmltag($label, 1).'">';
562 $linkend = '</a>';
563
564 $result .= $linkstart;
565 if ($withpicto) {
566 $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);
567 }
568 if ($withpicto != 2) {
569 $result .= $this->ref;
570 }
571 $result .= $linkend;
572
573 global $action, $hookmanager;
574 $hookmanager->initHooks(array($this->element . 'dao'));
575 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
576 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
577 if ($reshook > 0) {
578 $result = $hookmanager->resPrint;
579 } else {
580 $result .= $hookmanager->resPrint;
581 }
582
583 return $result;
584 }
585
586
593 public function getLibStatut($mode = 0)
594 {
595 return '';
596 }
597
598 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
606 public function LibStatut($status, $mode = 0)
607 {
608 // phpcs:enable
609
610 //$langs->load("members");
611
612 return '';
613 }
614
621 public function info($id)
622 {
623 $sql = "SELECT c.rowid, c.datec, c.tms as datem, c.fk_user_creat";
624 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as c";
625 $sql .= " WHERE c.rowid = ".((int) $id);
626
627 $resql = $this->db->query($sql);
628 if ($resql) {
629 if ($this->db->num_rows($resql)) {
630 $obj = $this->db->fetch_object($resql);
631 $this->id = $obj->rowid;
632
633 $this->date_creation = $this->db->jdate($obj->datec);
634 $this->date_modification = $this->db->jdate($obj->datem);
635
636 $this->user_creation_id = $obj->fk_user_creat;
637 }
638
639 $this->db->free($resql);
640 } else {
641 dol_print_error($this->db);
642 }
643 }
644
652 public function getKanbanView($option = '', $arraydata = null)
653 {
654 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
655
656 $return = '<div class="box-flex-item box-flex-grow-zero">';
657 $return .= '<div class="info-box info-box-sm">';
658 $return .= '<span class="info-box-icon bg-infobox-action">';
659 $return .= img_picto('', $this->picto);
660 $return .= '</span>';
661
662 $return .= '<div class="info-box-content">';
663 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">';
664 $return .= $this->getNomUrl(0);
665 $return .= '</span>';
666 if ($selected >= 0) {
667 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
668 }
669 if (!empty($this->dateh) || !empty($this->datef)) {
670 $return .= '<br><span class="info-box-status opacitymedium small">'.dol_print_date($this->dateh, 'day').' - '.dol_print_date($this->datef, 'day').'</span>';
671 }
672
673 if (!empty($arraydata['member']) && is_object($arraydata['member'])) {
674 $return .= '<br><div class="inline-block tdoverflowmax150">'.$arraydata['member']->getNomUrl(-4).'</div>';
675 }
676
677 $return .= '<br><span class="amount inline-block">'.price($this->amount).'</span>';
678 if (!empty($arraydata['bank'])) {
679 $return .= ' &nbsp; <span class="info-box-label ">'.$arraydata['bank']->getNomUrl(-1).'</span>';
680 }
681 $return .= '</div>';
682 $return .= '</div>';
683 $return .= '</div>';
684
685 return $return;
686 }
687}
$object ref
Definition info.php:90
Class to manage bank transaction lines.
Class to manage members of a foundation.
const STATUS_DRAFT
Draft status.
const STATUS_RESILIATED
Resiliated (membership end and was not renew)
const STATUS_VALIDATED
Validated status.
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
setErrorsFromObject($object)
setErrorsFromObject
Class to manage subscriptions of foundation members.
fetch($rowid)
Method to load a subscription.
getLibStatut($mode=0)
Return the label of the status.
create($user, $notrigger=0)
Function who permitted creation of the subscription.
info($id)
Load information of the subscription object.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
__construct($db)
Constructor.
getNomUrl($withpicto=0, $notooltip=0, $option='', $morecss='', $save_lastsearch_value=-1)
Return clickable name (with picto eventually)
LibStatut($status, $mode=0)
Return the label of a given status.
update($user, $notrigger=0)
Update subscription.
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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)
Definition html.lib.php:183
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as p p num_paiement as f pf amount as amount
Definition receipt.php:494