dolibarr 24.0.0-beta
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 $amount;
121
125 public $fk_bank;
126
130 public $fields = array(
131 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
132 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 15),
133 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 20),
134 'fk_adherent' => array('type' => 'integer', 'label' => 'Member', 'enabled' => 1, 'visible' => -1, 'position' => 25),
135 'dateadh' => array('type' => 'datetime', 'label' => 'DateSubscription', 'enabled' => 1, 'visible' => -1, 'position' => 30),
136 'datef' => array('type' => 'datetime', 'label' => 'DateEndSubscription', 'enabled' => 1, 'visible' => -1, 'position' => 35),
137 'subscription' => array('type' => 'double(24,8)', 'label' => 'Amount', 'enabled' => 1, 'visible' => -1, 'position' => 40, 'isameasure' => 1),
138 'fk_bank' => array('type' => 'integer', 'label' => 'BankId', 'enabled' => 1, 'visible' => -1, 'position' => 45),
139 'note' => array('type' => 'html', 'label' => 'Note', 'enabled' => 1, 'visible' => -1, 'position' => 50),
140 'fk_type' => array('type' => 'integer', 'label' => 'MemberType', 'enabled' => 1, 'visible' => -1, 'position' => 55),
141 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -2, 'position' => 60),
142 'fk_user_valid' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserValidation', 'enabled' => 1, 'visible' => -1, 'position' => 65),
143 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 805),
144 );
145
146
152 public function __construct($db)
153 {
154 $this->db = $db;
155
156 $this->ismultientitymanaged = 'fk_adherent@adherent';
157 }
158
159
167 public function create($user, $notrigger = 0)
168 {
169 global $langs;
170
171 $error = 0;
172
173 $now = dol_now();
174
175 // Clean parameters
176 if (isset($this->import_key)) {
177 $this->import_key = trim($this->import_key);
178 }
179
180 // Check parameters
181 if ($this->datef <= $this->dateh) {
182 $this->error = $langs->trans("ErrorBadValueForDate");
183 return -1;
184 }
185 if (empty($this->datec)) {
186 $this->datec = $now;
187 }
188
189 $this->db->begin();
190
191 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
192 $member = new Adherent($this->db);
193 $result = $member->fetch($this->fk_adherent);
194
195 if ($this->fk_type == null) { // If type not defined, we use the type of member
196 $type = $member->typeid;
197 } else {
198 $type = $this->fk_type;
199 }
200
201 $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)";
202 $sql .= " VALUES (".((int) $this->fk_adherent).", '".$this->db->escape((string) $type)."', '".$this->db->idate($now)."',";
203 $sql .= " '".$this->db->idate($this->dateh)."',";
204 $sql .= " '".$this->db->idate($this->datef)."',";
205 $sql .= " ".((float) $this->amount).",";
206 $sql .= " '".$this->db->escape($this->note_public ? $this->note_public : $this->note)."',";
207 $sql .= " '".$this->db->escape($this->note_private)."',";
208 $sql .= " ".(empty($this->ref_ext) ? "null" : "'".$this->db->escape($this->ref_ext)."'").",";
209 $sql .= " ".((int) ($this->user_creation_id > 0 ? $this->user_creation_id : $user->id));
210 $sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
211 $sql .= ")";
212
213 $resql = $this->db->query($sql);
214 if (!$resql) {
215 $error++;
216 $this->errors[] = $this->db->lasterror();
217 }
218
219 if (!$error) {
220 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
221 $this->fk_type = $type;
222 }
223
224 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
225 $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
226 }
227
228 // Add object linked
229 if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
230 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
231 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, ...))
232 foreach ($tmp_origin_id as $origin_id) {
233 $ret = $this->add_object_linked($origin, $origin_id);
234 if (!$ret) {
235 $this->error = $this->db->lasterror();
236 $error++;
237 }
238 }
239 } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
240 $origin_id = $tmp_origin_id;
241 $ret = $this->add_object_linked($origin, $origin_id);
242 if (!$ret) {
243 $this->error = $this->db->lasterror();
244 $error++;
245 }
246 }
247 }
248 }
249
250 if (!$error && !$notrigger) {
251 $this->context = array('member' => $member);
252 // Call triggers
253 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_CREATE', $user);
254 if ($result < 0) {
255 $error++;
256 }
257 // End call triggers
258 }
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
277 public function fetch($rowid)
278 {
279 $sql = "SELECT rowid, fk_type, fk_adherent, datec,";
280 $sql .= " tms,";
281 $sql .= " dateadh as dateh,";
282 $sql .= " datef,";
283 $sql .= " subscription, note as note_public, fk_bank";
284 $sql .= " FROM ".MAIN_DB_PREFIX."subscription";
285 $sql .= " WHERE rowid = ".((int) $rowid);
286
287 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
288 $resql = $this->db->query($sql);
289 if ($resql) {
290 if ($this->db->num_rows($resql)) {
291 $obj = $this->db->fetch_object($resql);
292
293 $this->id = $obj->rowid;
294 $this->ref = $obj->rowid;
295
296 $this->fk_type = $obj->fk_type;
297 $this->fk_adherent = $obj->fk_adherent;
298 $this->datec = $this->db->jdate($obj->datec);
299 $this->datem = $this->db->jdate($obj->tms);
300 $this->dateh = $this->db->jdate($obj->dateh);
301 $this->datef = $this->db->jdate($obj->datef);
302 $this->amount = $obj->subscription;
303 $this->note = $obj->note_public; // deprecated
304 $this->note_public = $obj->note_public;
305 $this->fk_bank = $obj->fk_bank;
306 return 1;
307 } else {
308 return 0;
309 }
310 } else {
311 $this->error = $this->db->lasterror();
312 return -1;
313 }
314 }
315
316
324 public function update($user, $notrigger = 0)
325 {
326 $error = 0;
327
328 $this->db->begin();
329
330 if (!is_numeric($this->amount)) {
331 $this->error = 'BadValueForParameterAmount';
332 return -1;
333 }
334
335 if (empty($this->note_public) && !empty($this->note)) { // For backward compatibility
336 $this->note_public = $this->note;
337 }
338
339 $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET ";
340 $sql .= " fk_type = ".((int) $this->fk_type).",";
341 $sql .= " fk_adherent = ".((int) $this->fk_adherent).",";
342 $sql .= " note = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : 'null').",";
343 $sql .= " subscription = ".(float) price2num($this->amount).",";
344 $sql .= " dateadh = '".$this->db->idate($this->dateh)."',";
345 $sql .= " datef = '".$this->db->idate($this->datef)."',";
346 $sql .= " datec = '".$this->db->idate($this->datec)."',";
347 $sql .= " fk_bank = ".($this->fk_bank ? ((int) $this->fk_bank) : 'null');
348 $sql .= " WHERE rowid = ".((int) $this->id);
349
350 dol_syslog(get_class($this)."::update", LOG_DEBUG);
351 $resql = $this->db->query($sql);
352 if ($resql) {
353 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
354 $member = new Adherent($this->db);
355 $result = $member->fetch($this->fk_adherent);
356 $result = $member->update_end_date($user);
357
358 if (!$notrigger) {
359 $this->context = array('member' => $member);
360 // Call triggers
361 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY', $user);
362 if ($result < 0) {
363 $error++;
364 } //Do also here what you must do to rollback action if trigger fail
365 // End call triggers
366 }
367 } else {
368 $error++;
369 $this->error = $this->db->lasterror();
370 }
371
372 // Commit or rollback
373 if ($error) {
374 $this->db->rollback();
375 return -1;
376 } else {
377 $this->db->commit();
378 return $this->id;
379 }
380 }
381
389 public function delete($user, $notrigger = 0)
390 {
391 $error = 0;
392
393 // It subscription is linked to a bank transaction, we get it
394 if ($this->fk_bank > 0) {
395 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
396 $accountline = new AccountLine($this->db);
397 $result = $accountline->fetch($this->fk_bank);
398 } else {
399 $accountline = null;
400 }
401
402 $this->db->begin();
403
404 if (!$notrigger) {
405 // Call triggers
406 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user);
407 if ($result < 0) {
408 $error++;
409 } // Do also here what you must do to rollback action if trigger fail
410 // End call triggers
411 }
412
413 if (!$error) {
414 $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE rowid = ".((int) $this->id);
415 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
416 $resql = $this->db->query($sql);
417 if ($resql) {
418 $num = $this->db->affected_rows($resql);
419 if ($num) {
420 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
421 $member = new Adherent($this->db);
422 $result = $member->fetch($this->fk_adherent);
423 $result = $member->update_end_date($user);
424
425 if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) { // If we found bank account line (this means this->fk_bank defined)
426 $result = $accountline->delete($user); // Return false if refused because line is reconciled
427 if ($result <= 0) {
428 $this->setErrorsFromObject($accountline);
429 $error++;
430 }
431 }
432 } else {
433 $this->db->commit();
434 return 0;
435 }
436 } else {
437 $error++;
438 $this->error = $this->db->lasterror();
439 }
440 }
441
442 // Commit or rollback
443 if ($error) {
444 $this->db->rollback();
445 return -1;
446 } else {
447 $this->db->commit();
448 return 1;
449 }
450 }
451
452
463 public function getNomUrl($withpicto = 0, $notooltip = 0, $option = '', $morecss = '', $save_lastsearch_value = -1)
464 {
465 global $langs;
466
467 $result = '';
468
469 $langs->load("members");
470 $langs->load("main");
471
472 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Subscription").'</u>';
473 /*if (isset($this->statut)) {
474 $label .= ' '.$this->getLibStatut(5);
475 }*/
476 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
477 $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->note_public;
478 if (!empty($this->dateh)) {
479 $label .= '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->dateh, 'day');
480 }
481 if (!empty($this->datef)) {
482 $label .= '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->datef, 'day');
483 }
484 $baseurl = DOL_URL_ROOT . '/adherents/subscription/card.php';
485 $query = ['rowid' => $this->id];
486 if ($option != 'nolink') {
487 // Add param to save lastsearch_values or not
488 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
489 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
490 $add_save_lastsearch_values = 1;
491 }
492 if ($add_save_lastsearch_values) {
493 $query = array_merge($query, ['save_lastsearch_values' => 1]);
494 }
495 }
496 $url = dolBuildUrl($baseurl, $query);
497
498 $linkstart = '<a href="'.$url.'" class="classfortooltip" title="'.dol_escape_htmltag($label, 1).'">';
499 $linkend = '</a>';
500
501 $result .= $linkstart;
502 if ($withpicto) {
503 $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);
504 }
505 if ($withpicto != 2) {
506 $result .= $this->ref;
507 }
508 $result .= $linkend;
509
510 return $result;
511 }
512
513
520 public function getLibStatut($mode = 0)
521 {
522 return '';
523 }
524
525 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
533 public function LibStatut($status, $mode = 0)
534 {
535 // phpcs:enable
536
537 //$langs->load("members");
538
539 return '';
540 }
541
548 public function info($id)
549 {
550 $sql = 'SELECT c.rowid, c.datec, c.tms as datem, c.fk_user_creat';
551 $sql .= ' FROM '.MAIN_DB_PREFIX.'subscription as c';
552 $sql .= ' WHERE c.rowid = '.((int) $id);
553
554 $resql = $this->db->query($sql);
555 if ($resql) {
556 if ($this->db->num_rows($resql)) {
557 $obj = $this->db->fetch_object($resql);
558 $this->id = $obj->rowid;
559
560 $this->date_creation = $this->db->jdate($obj->datec);
561 $this->date_modification = $this->db->jdate($obj->datem);
562
563 $this->user_creation_id = $obj->fk_user_creat;
564 }
565
566 $this->db->free($resql);
567 } else {
568 dol_print_error($this->db);
569 }
570 }
571
579 public function getKanbanView($option = '', $arraydata = null)
580 {
581 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
582
583 $return = '<div class="box-flex-item box-flex-grow-zero">';
584 $return .= '<div class="info-box info-box-sm">';
585 $return .= '<span class="info-box-icon bg-infobox-action">';
586 $return .= img_picto('', $this->picto);
587 $return .= '</span>';
588
589 $return .= '<div class="info-box-content">';
590 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">';
591 $return .= $this->getNomUrl(0);
592 $return .= '</span>';
593 if ($selected >= 0) {
594 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
595 }
596 if (!empty($this->dateh) || !empty($this->datef)) {
597 $return .= '<br><span class="info-box-status opacitymedium small">'.dol_print_date($this->dateh, 'day').' - '.dol_print_date($this->datef, 'day').'</span>';
598 }
599
600 if (!empty($arraydata['member']) && is_object($arraydata['member'])) {
601 $return .= '<br><div class="inline-block tdoverflowmax150">'.$arraydata['member']->getNomUrl(-4).'</div>';
602 }
603
604 $return .= '<br><span class="amount inline-block">'.price($this->amount).'</span>';
605 if (!empty($arraydata['bank'])) {
606 $return .= ' &nbsp; <span class="info-box-label ">'.$arraydata['bank']->getNomUrl(-1).'</span>';
607 }
608 $return .= '</div>';
609 $return .= '</div>';
610 $return .= '</div>';
611
612 return $return;
613 }
614}
$object ref
Definition info.php:90
Class to manage bank transaction lines.
Class to manage members of a foundation.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
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_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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...
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:489