dolibarr 23.0.3
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-2025 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
83 public $fk_type;
84
88 public $fk_adherent;
89
93 public $amount;
94
98 public $fk_bank;
99
103 public $fields = array(
104 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
105 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 15),
106 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 20),
107 'fk_adherent' => array('type' => 'integer', 'label' => 'Member', 'enabled' => 1, 'visible' => -1, 'position' => 25),
108 'dateadh' => array('type' => 'datetime', 'label' => 'DateSubscription', 'enabled' => 1, 'visible' => -1, 'position' => 30),
109 'datef' => array('type' => 'datetime', 'label' => 'DateEndSubscription', 'enabled' => 1, 'visible' => -1, 'position' => 35),
110 'subscription' => array('type' => 'double(24,8)', 'label' => 'Amount', 'enabled' => 1, 'visible' => -1, 'position' => 40, 'isameasure' => 1),
111 'fk_bank' => array('type' => 'integer', 'label' => 'BankId', 'enabled' => 1, 'visible' => -1, 'position' => 45),
112 'note' => array('type' => 'html', 'label' => 'Note', 'enabled' => 1, 'visible' => -1, 'position' => 50),
113 'fk_type' => array('type' => 'integer', 'label' => 'MemberType', 'enabled' => 1, 'visible' => -1, 'position' => 55),
114 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -2, 'position' => 60),
115 'fk_user_valid' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserValidation', 'enabled' => 1, 'visible' => -1, 'position' => 65),
116 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 805),
117 );
118
119
125 public function __construct($db)
126 {
127 $this->db = $db;
128
129 $this->ismultientitymanaged = 'fk_adherent@adherent';
130 }
131
132
140 public function create($user, $notrigger = 0)
141 {
142 global $langs;
143
144 $error = 0;
145
146 $now = dol_now();
147
148 // Clean parameters
149 if (isset($this->import_key)) {
150 $this->import_key = trim($this->import_key);
151 }
152
153 // Check parameters
154 if ($this->datef <= $this->dateh) {
155 $this->error = $langs->trans("ErrorBadValueForDate");
156 return -1;
157 }
158 if (empty($this->datec)) {
159 $this->datec = $now;
160 }
161
162 $this->db->begin();
163
164 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
165 $member = new Adherent($this->db);
166 $result = $member->fetch($this->fk_adherent);
167
168 if ($this->fk_type == null) { // If type not defined, we use the type of member
169 $type = $member->typeid;
170 } else {
171 $type = $this->fk_type;
172 }
173
174 $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)";
175 $sql .= " VALUES (".((int) $this->fk_adherent).", '".$this->db->escape((string) $type)."', '".$this->db->idate($now)."',";
176 $sql .= " '".$this->db->idate($this->dateh)."',";
177 $sql .= " '".$this->db->idate($this->datef)."',";
178 $sql .= " ".((float) $this->amount).",";
179 $sql .= " '".$this->db->escape($this->note_public ? $this->note_public : $this->note)."',";
180 $sql .= " '".$this->db->escape($this->note_private)."',";
181 $sql .= " ".(empty($this->ref_ext) ? "null" : "'".$this->db->escape($this->ref_ext)."'").",";
182 $sql .= " ".((int) ($this->user_creation_id > 0 ? $this->user_creation_id : $user->id));
183 $sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
184 $sql .= ")";
185
186 $resql = $this->db->query($sql);
187 if (!$resql) {
188 $error++;
189 $this->errors[] = $this->db->lasterror();
190 }
191
192 if (!$error) {
193 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
194 $this->fk_type = $type;
195 }
196
197 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
198 $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
199 }
200
201 // Add object linked
202 if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
203 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
204 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, ...))
205 foreach ($tmp_origin_id as $origin_id) {
206 $ret = $this->add_object_linked($origin, $origin_id);
207 if (!$ret) {
208 $this->error = $this->db->lasterror();
209 $error++;
210 }
211 }
212 } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
213 $origin_id = $tmp_origin_id;
214 $ret = $this->add_object_linked($origin, $origin_id);
215 if (!$ret) {
216 $this->error = $this->db->lasterror();
217 $error++;
218 }
219 }
220 }
221 }
222
223 if (!$error && !$notrigger) {
224 $this->context = array('member' => $member);
225 // Call triggers
226 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_CREATE', $user);
227 if ($result < 0) {
228 $error++;
229 }
230 // End call triggers
231 }
232
233 // Commit or rollback
234 if ($error) {
235 $this->db->rollback();
236 return -1;
237 } else {
238 $this->db->commit();
239 return $this->id;
240 }
241 }
242
243
250 public function fetch($rowid)
251 {
252 $sql = "SELECT rowid, fk_type, fk_adherent, datec,";
253 $sql .= " tms,";
254 $sql .= " dateadh as dateh,";
255 $sql .= " datef,";
256 $sql .= " subscription, note as note_public, fk_bank";
257 $sql .= " FROM ".MAIN_DB_PREFIX."subscription";
258 $sql .= " WHERE rowid = ".((int) $rowid);
259
260 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
261 $resql = $this->db->query($sql);
262 if ($resql) {
263 if ($this->db->num_rows($resql)) {
264 $obj = $this->db->fetch_object($resql);
265
266 $this->id = $obj->rowid;
267 $this->ref = $obj->rowid;
268
269 $this->fk_type = $obj->fk_type;
270 $this->fk_adherent = $obj->fk_adherent;
271 $this->datec = $this->db->jdate($obj->datec);
272 $this->datem = $this->db->jdate($obj->tms);
273 $this->dateh = $this->db->jdate($obj->dateh);
274 $this->datef = $this->db->jdate($obj->datef);
275 $this->amount = $obj->subscription;
276 $this->note = $obj->note_public; // deprecated
277 $this->note_public = $obj->note_public;
278 $this->fk_bank = $obj->fk_bank;
279 return 1;
280 } else {
281 return 0;
282 }
283 } else {
284 $this->error = $this->db->lasterror();
285 return -1;
286 }
287 }
288
289
297 public function update($user, $notrigger = 0)
298 {
299 $error = 0;
300
301 $this->db->begin();
302
303 if (!is_numeric($this->amount)) {
304 $this->error = 'BadValueForParameterAmount';
305 return -1;
306 }
307
308 if (empty($this->note_public) && !empty($this->note)) { // For backward compatibility
309 $this->note_public = $this->note;
310 }
311
312 $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET ";
313 $sql .= " fk_type = ".((int) $this->fk_type).",";
314 $sql .= " fk_adherent = ".((int) $this->fk_adherent).",";
315 $sql .= " note = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : 'null').",";
316 $sql .= " subscription = ".(float) price2num($this->amount).",";
317 $sql .= " dateadh = '".$this->db->idate($this->dateh)."',";
318 $sql .= " datef = '".$this->db->idate($this->datef)."',";
319 $sql .= " datec = '".$this->db->idate($this->datec)."',";
320 $sql .= " fk_bank = ".($this->fk_bank ? ((int) $this->fk_bank) : 'null');
321 $sql .= " WHERE rowid = ".((int) $this->id);
322
323 dol_syslog(get_class($this)."::update", LOG_DEBUG);
324 $resql = $this->db->query($sql);
325 if ($resql) {
326 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
327 $member = new Adherent($this->db);
328 $result = $member->fetch($this->fk_adherent);
329 $result = $member->update_end_date($user);
330
331 if (!$notrigger) {
332 $this->context = array('member' => $member);
333 // Call triggers
334 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY', $user);
335 if ($result < 0) {
336 $error++;
337 } //Do also here what you must do to rollback action if trigger fail
338 // End call triggers
339 }
340 } else {
341 $error++;
342 $this->error = $this->db->lasterror();
343 }
344
345 // Commit or rollback
346 if ($error) {
347 $this->db->rollback();
348 return -1;
349 } else {
350 $this->db->commit();
351 return $this->id;
352 }
353 }
354
362 public function delete($user, $notrigger = 0)
363 {
364 $error = 0;
365
366 // It subscription is linked to a bank transaction, we get it
367 if ($this->fk_bank > 0) {
368 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
369 $accountline = new AccountLine($this->db);
370 $result = $accountline->fetch($this->fk_bank);
371 } else {
372 $accountline = null;
373 }
374
375 $this->db->begin();
376
377 if (!$notrigger) {
378 // Call triggers
379 $result = $this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user);
380 if ($result < 0) {
381 $error++;
382 } // Do also here what you must do to rollback action if trigger fail
383 // End call triggers
384 }
385
386 if (!$error) {
387 $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE rowid = ".((int) $this->id);
388 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
389 $resql = $this->db->query($sql);
390 if ($resql) {
391 $num = $this->db->affected_rows($resql);
392 if ($num) {
393 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
394 $member = new Adherent($this->db);
395 $result = $member->fetch($this->fk_adherent);
396 $result = $member->update_end_date($user);
397
398 if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) { // If we found bank account line (this means this->fk_bank defined)
399 $result = $accountline->delete($user); // Return false if refused because line is reconciled
400 if ($result <= 0) {
401 $this->setErrorsFromObject($accountline);
402 $error++;
403 }
404 }
405 } else {
406 $this->db->commit();
407 return 0;
408 }
409 } else {
410 $error++;
411 $this->error = $this->db->lasterror();
412 }
413 }
414
415 // Commit or rollback
416 if ($error) {
417 $this->db->rollback();
418 return -1;
419 } else {
420 $this->db->commit();
421 return 1;
422 }
423 }
424
425
436 public function getNomUrl($withpicto = 0, $notooltip = 0, $option = '', $morecss = '', $save_lastsearch_value = -1)
437 {
438 global $langs;
439
440 $result = '';
441
442 $langs->load("members");
443
444 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Subscription").'</u>';
445 /*if (isset($this->statut)) {
446 $label .= ' '.$this->getLibStatut(5);
447 }*/
448 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
449 if (!empty($this->dateh)) {
450 $label .= '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->dateh, 'day');
451 }
452 if (!empty($this->datef)) {
453 $label .= '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->datef, 'day');
454 }
455 $baseurl = DOL_URL_ROOT . '/adherents/subscription/card.php';
456 $query = ['rowid' => $this->id];
457 if ($option != 'nolink') {
458 // Add param to save lastsearch_values or not
459 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
460 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
461 $add_save_lastsearch_values = 1;
462 }
463 if ($add_save_lastsearch_values) {
464 $query = array_merge($query, ['save_lastsearch_values' => 1]);
465 }
466 }
467 $url = dolBuildUrl($baseurl, $query);
468
469 $linkstart = '<a href="'.$url.'" class="classfortooltip" title="'.dol_escape_htmltag($label, 1).'">';
470 $linkend = '</a>';
471
472 $result .= $linkstart;
473 if ($withpicto) {
474 $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);
475 }
476 if ($withpicto != 2) {
477 $result .= $this->ref;
478 }
479 $result .= $linkend;
480
481 return $result;
482 }
483
484
491 public function getLibStatut($mode = 0)
492 {
493 return '';
494 }
495
496 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
504 public function LibStatut($status, $mode = 0)
505 {
506 // phpcs:enable
507
508 //$langs->load("members");
509
510 return '';
511 }
512
519 public function info($id)
520 {
521 $sql = 'SELECT c.rowid, c.datec, c.tms as datem, c.fk_user_creat';
522 $sql .= ' FROM '.MAIN_DB_PREFIX.'subscription as c';
523 $sql .= ' WHERE c.rowid = '.((int) $id);
524
525 $resql = $this->db->query($sql);
526 if ($resql) {
527 if ($this->db->num_rows($resql)) {
528 $obj = $this->db->fetch_object($resql);
529 $this->id = $obj->rowid;
530
531 $this->date_creation = $this->db->jdate($obj->datec);
532 $this->date_modification = $this->db->jdate($obj->datem);
533
534 $this->user_creation_id = $obj->fk_user_creat;
535 }
536
537 $this->db->free($resql);
538 } else {
539 dol_print_error($this->db);
540 }
541 }
542
550 public function getKanbanView($option = '', $arraydata = null)
551 {
552 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
553
554 $return = '<div class="box-flex-item box-flex-grow-zero">';
555 $return .= '<div class="info-box info-box-sm">';
556 $return .= '<span class="info-box-icon bg-infobox-action">';
557 $return .= img_picto('', $this->picto);
558 $return .= '</span>';
559
560 $return .= '<div class="info-box-content">';
561 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">';
562 $return .= $this->getNomUrl(0);
563 $return .= '</span>';
564 if ($selected >= 0) {
565 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
566 }
567 if (!empty($this->dateh) || !empty($this->datef)) {
568 $return .= '<br><span class="info-box-status opacitymedium small">'.dol_print_date($this->dateh, 'day').' - '.dol_print_date($this->datef, 'day').'</span>';
569 }
570
571 if (!empty($arraydata['member']) && is_object($arraydata['member'])) {
572 $return .= '<br><div class="inline-block tdoverflowmax150">'.$arraydata['member']->getNomUrl(-4).'</div>';
573 }
574
575 $return .= '<br><span class="amount inline-block">'.price($this->amount).'</span>';
576 if (!empty($arraydata['bank'])) {
577 $return .= ' &nbsp; <span class="info-box-label ">'.$arraydata['bank']->getNomUrl(-1).'</span>';
578 }
579 $return .= '</div>';
580 $return .= '</div>';
581 $return .= '</div>';
582
583 return $return;
584 }
585}
$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)
dolBuildUrl($url, $params=[], $addtoken=false)
Return path of url.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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...
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left 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 PaymentTypeShortLIQ right SELECT p pos_change as p datep as p p num_paiement as f pf amount as amount
Definition receipt.php:466