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
186 $error = 0;
187
188 $now = dol_now();
189
190 // Clean parameters
191 if (isset($this->import_key)) {
192 $this->import_key = trim($this->import_key);
193 }
194
195 // Check parameters
196 if ($this->datef <= $this->dateh) {
197 $this->error = $langs->trans("ErrorBadValueForDate");
198 return -1;
199 }
200 if (empty($this->datec)) {
201 $this->datec = $now;
202 }
203
204 $this->db->begin();
205
206 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
207 $member = new Adherent($this->db);
208 $result = $member->fetch($this->fk_adherent);
209
210 if ($this->fk_type == null) { // If type not defined, we use the type of member
211 $type = $member->typeid;
212 } else {
213 $type = $this->fk_type;
214 }
215
216 $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)";
217 $sql .= " VALUES (".((int) $this->fk_adherent).", '".$this->db->escape((string) $type)."', '".$this->db->idate($now)."',";
218 $sql .= " '".$this->db->idate($this->dateh)."',";
219 $sql .= " '".$this->db->idate($this->datef)."',";
220 $sql .= " ".((float) $this->amount).",";
221 $sql .= " '".$this->db->escape($this->note_public ? $this->note_public : $this->note)."',";
222 $sql .= " '".$this->db->escape($this->note_private)."',";
223 $sql .= " ".(empty($this->ref_ext) ? "null" : "'".$this->db->escape($this->ref_ext)."'").",";
224 $sql .= " ".((int) ($this->user_creation_id > 0 ? $this->user_creation_id : ((int) $user->id)));
225 $sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
226 $sql .= ")";
227
228 $resql = $this->db->query($sql);
229 if (!$resql) {
230 $error++;
231 $this->errors[] = $this->db->lasterror();
232 }
233
234 if (!$error) {
235 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
236 $this->fk_type = $type;
237 }
238
239 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
240 $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
241 }
242
243 // Add object linked
244 if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
245 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
246 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, ...))
247 foreach ($tmp_origin_id as $origin_id) {
248 $ret = $this->add_object_linked($origin, $origin_id);
249 if (!$ret) {
250 $this->error = $this->db->lasterror();
251 $error++;
252 }
253 }
254 } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
255 $origin_id = $tmp_origin_id;
256 $ret = $this->add_object_linked($origin, $origin_id);
257 if (!$ret) {
258 $this->error = $this->db->lasterror();
259 $error++;
260 }
261 }
262 }
263 }
264
265 if (!$error && !$notrigger) {
266 $this->context = array('member' => $member);
267 // Call triggers
268 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_CREATE', $user);
269 if ($result < 0) {
270 $error++;
271 }
272 // End call triggers
273 }
274
275 // Commit or rollback
276 if ($error) {
277 $this->db->rollback();
278 return -1;
279 } else {
280 $this->db->commit();
281 return $this->id;
282 }
283 }
284
285
292 public function fetch($rowid)
293 {
294 $sql = "SELECT s.rowid, s.fk_type, s.fk_adherent, s.datec,";
295 $sql .= " s.tms,";
296 $sql .= " s.dateadh as dateh,";
297 $sql .= " s.datef,";
298 $sql .= " s.subscription, s.note as note_public, s.fk_bank,";
299 $sql .= " a.firstname as member_firstname, a.lastname as member_lastname, a.societe as member_company";
300 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as s";
301 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent as a ON a.rowid = s.fk_adherent";
302 $sql .= " WHERE s.rowid = ".((int) $rowid);
303
304 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
305 $resql = $this->db->query($sql);
306 if ($resql) {
307 if ($this->db->num_rows($resql)) {
308 $obj = $this->db->fetch_object($resql);
309
310 $this->id = $obj->rowid;
311 $this->ref = $obj->rowid;
312
313 $this->fk_type = $obj->fk_type;
314 $this->fk_adherent = $obj->fk_adherent;
315 $this->member_firstname = $obj->member_firstname;
316 $this->member_lastname = $obj->member_lastname;
317 $this->member_company = $obj->member_company;
318 $this->datec = $this->db->jdate($obj->datec);
319 $this->datem = $this->db->jdate($obj->tms);
320 $this->dateh = $this->db->jdate($obj->dateh);
321 $this->datef = $this->db->jdate($obj->datef);
322 $this->amount = $obj->subscription;
323 $this->note = $obj->note_public; // deprecated
324 $this->note_public = $obj->note_public;
325 $this->fk_bank = $obj->fk_bank;
326 return 1;
327 } else {
328 return 0;
329 }
330 } else {
331 $this->error = $this->db->lasterror();
332 return -1;
333 }
334 }
335
336
344 public function update($user, $notrigger = 0)
345 {
346 $error = 0;
347
348 $this->db->begin();
349
350 if (!is_numeric($this->amount)) {
351 $this->error = 'BadValueForParameterAmount';
352 return -1;
353 }
354
355 if (empty($this->note_public) && !empty($this->note)) { // For backward compatibility
356 $this->note_public = $this->note;
357 }
358
359 $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET ";
360 $sql .= " fk_type = ".((int) $this->fk_type).",";
361 $sql .= " fk_adherent = ".((int) $this->fk_adherent).",";
362 $sql .= " note = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : 'null').",";
363 $sql .= " subscription = ".(float) price2num($this->amount).",";
364 $sql .= " dateadh = '".$this->db->idate($this->dateh)."',";
365 $sql .= " datef = '".$this->db->idate($this->datef)."',";
366 $sql .= " datec = '".$this->db->idate($this->datec)."',";
367 $sql .= " fk_bank = ".($this->fk_bank ? ((int) $this->fk_bank) : 'null');
368 $sql .= " WHERE rowid = ".((int) $this->id);
369
370 dol_syslog(get_class($this)."::update", LOG_DEBUG);
371 $resql = $this->db->query($sql);
372 if ($resql) {
373 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
374 $member = new Adherent($this->db);
375 $result = $member->fetch($this->fk_adherent);
376 $result = $member->update_end_date($user);
377
378 if (!$notrigger) {
379 $this->context = array('member' => $member);
380 // Call triggers
381 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY', $user);
382 if ($result < 0) {
383 $error++;
384 } //Do also here what you must do to rollback action if trigger fail
385 // End call triggers
386 }
387 } else {
388 $error++;
389 $this->error = $this->db->lasterror();
390 }
391
392 // Commit or rollback
393 if ($error) {
394 $this->db->rollback();
395 return -1;
396 } else {
397 $this->db->commit();
398 return $this->id;
399 }
400 }
401
409 public function delete($user, $notrigger = 0)
410 {
411 $error = 0;
412
413 // It subscription is linked to a bank transaction, we get it
414 if ($this->fk_bank > 0) {
415 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
416 $accountline = new AccountLine($this->db);
417 $result = $accountline->fetch($this->fk_bank);
418 } else {
419 $accountline = null;
420 }
421
422 $this->db->begin();
423
424 if (!$notrigger) {
425 // Call triggers
426 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user);
427 if ($result < 0) {
428 $error++;
429 } // Do also here what you must do to rollback action if trigger fail
430 // End call triggers
431 }
432
433 if (!$error) {
434 $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE rowid = ".((int) $this->id);
435 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
436 $resql = $this->db->query($sql);
437 if ($resql) {
438 $num = $this->db->affected_rows($resql);
439 if ($num) {
440 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
441 $member = new Adherent($this->db);
442 $result = $member->fetch($this->fk_adherent);
443 $result = $member->update_end_date($user);
444
445 if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) { // If we found bank account line (this means this->fk_bank defined)
446 $result = $accountline->delete($user); // Return false if refused because line is reconciled
447 if ($result <= 0) {
448 $this->setErrorsFromObject($accountline);
449 $error++;
450 }
451 }
452 } else {
453 $this->db->commit();
454 return 0;
455 }
456 } else {
457 $error++;
458 $this->error = $this->db->lasterror();
459 }
460 }
461
462 // Commit or rollback
463 if ($error) {
464 $this->db->rollback();
465 return -1;
466 } else {
467 $this->db->commit();
468 return 1;
469 }
470 }
471
472
483 public function getNomUrl($withpicto = 0, $notooltip = 0, $option = '', $morecss = '', $save_lastsearch_value = -1)
484 {
485 global $langs;
486
487 $result = '';
488
489 $langs->load("members");
490 $langs->load("main");
491
492 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Subscription").'</u>';
493 /*if (isset($this->statut)) {
494 $label .= ' '.$this->getLibStatut(5);
495 }*/
496 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
497 $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->note_public;
498 $memberName = dolGetFirstLastname($this->member_firstname, $this->member_lastname);
499 if (empty($memberName)) {
500 $memberName = $this->member_company;
501 }
502 if (!empty($memberName)) {
503 $label .= '<br><b>'.$langs->trans('Member').':</b> '.$memberName;
504 }
505 if (!empty($this->dateh)) {
506 $label .= '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->dateh, 'day');
507 }
508 if (!empty($this->datef)) {
509 $label .= '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->datef, 'day');
510 }
511 $baseurl = DOL_URL_ROOT . '/adherents/subscription/card.php';
512 $query = ['rowid' => $this->id];
513 if ($option != 'nolink') {
514 // Add param to save lastsearch_values or not
515 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
516 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
517 $add_save_lastsearch_values = 1;
518 }
519 if ($add_save_lastsearch_values) {
520 $query = array_merge($query, ['save_lastsearch_values' => 1]);
521 }
522 }
523 $url = dolBuildUrl($baseurl, $query);
524
525 $linkstart = '<a href="'.$url.'" class="classfortooltip" title="'.dol_escape_htmltag($label, 1).'">';
526 $linkend = '</a>';
527
528 $result .= $linkstart;
529 if ($withpicto) {
530 $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);
531 }
532 if ($withpicto != 2) {
533 $result .= $this->ref;
534 }
535 $result .= $linkend;
536
537 global $action, $hookmanager;
538 $hookmanager->initHooks(array($this->element . 'dao'));
539 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
540 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
541 if ($reshook > 0) {
542 $result = $hookmanager->resPrint;
543 } else {
544 $result .= $hookmanager->resPrint;
545 }
546
547 return $result;
548 }
549
550
557 public function getLibStatut($mode = 0)
558 {
559 return '';
560 }
561
562 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
570 public function LibStatut($status, $mode = 0)
571 {
572 // phpcs:enable
573
574 //$langs->load("members");
575
576 return '';
577 }
578
585 public function info($id)
586 {
587 $sql = "SELECT c.rowid, c.datec, c.tms as datem, c.fk_user_creat";
588 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as c";
589 $sql .= " WHERE c.rowid = ".((int) $id);
590
591 $resql = $this->db->query($sql);
592 if ($resql) {
593 if ($this->db->num_rows($resql)) {
594 $obj = $this->db->fetch_object($resql);
595 $this->id = $obj->rowid;
596
597 $this->date_creation = $this->db->jdate($obj->datec);
598 $this->date_modification = $this->db->jdate($obj->datem);
599
600 $this->user_creation_id = $obj->fk_user_creat;
601 }
602
603 $this->db->free($resql);
604 } else {
605 dol_print_error($this->db);
606 }
607 }
608
616 public function getKanbanView($option = '', $arraydata = null)
617 {
618 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
619
620 $return = '<div class="box-flex-item box-flex-grow-zero">';
621 $return .= '<div class="info-box info-box-sm">';
622 $return .= '<span class="info-box-icon bg-infobox-action">';
623 $return .= img_picto('', $this->picto);
624 $return .= '</span>';
625
626 $return .= '<div class="info-box-content">';
627 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">';
628 $return .= $this->getNomUrl(0);
629 $return .= '</span>';
630 if ($selected >= 0) {
631 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
632 }
633 if (!empty($this->dateh) || !empty($this->datef)) {
634 $return .= '<br><span class="info-box-status opacitymedium small">'.dol_print_date($this->dateh, 'day').' - '.dol_print_date($this->datef, 'day').'</span>';
635 }
636
637 if (!empty($arraydata['member']) && is_object($arraydata['member'])) {
638 $return .= '<br><div class="inline-block tdoverflowmax150">'.$arraydata['member']->getNomUrl(-4).'</div>';
639 }
640
641 $return .= '<br><span class="amount inline-block">'.price($this->amount).'</span>';
642 if (!empty($arraydata['bank'])) {
643 $return .= ' &nbsp; <span class="info-box-label ">'.$arraydata['bank']->getNomUrl(-1).'</span>';
644 }
645 $return .= '</div>';
646 $return .= '</div>';
647 $return .= '</div>';
648
649 return $return;
650 }
651}
$object ref
Definition info.php:90
Class to manage bank transaction lines.
Class to manage members of a foundation.
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)
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).
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:181
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