dolibarr  20.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 MDW <mdeweerd@users.noreply.github.com>
5  * Copyright (C) 2024 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 
29 require_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  );
117 
118 
124  public function __construct($db)
125  {
126  $this->db = $db;
127 
128  $this->ismultientitymanaged = 'fk_adherent@adherent';
129  }
130 
131 
139  public function create($user, $notrigger = 0)
140  {
141  global $langs;
142 
143  $error = 0;
144 
145  $now = dol_now();
146 
147  // Check parameters
148  if ($this->datef <= $this->dateh) {
149  $this->error = $langs->trans("ErrorBadValueForDate");
150  return -1;
151  }
152  if (empty($this->datec)) {
153  $this->datec = $now;
154  }
155 
156  $this->db->begin();
157 
158  $sql = "INSERT INTO ".MAIN_DB_PREFIX."subscription (fk_adherent, fk_type, datec, dateadh, datef, subscription, note)";
159 
160  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
161  $member = new Adherent($this->db);
162  $result = $member->fetch($this->fk_adherent);
163 
164  if ($this->fk_type == null) { // If type not defined, we use the type of member
165  $type = $member->typeid;
166  } else {
167  $type = $this->fk_type;
168  }
169  $sql .= " VALUES (".((int) $this->fk_adherent).", '".$this->db->escape($type)."', '".$this->db->idate($now)."',";
170  $sql .= " '".$this->db->idate($this->dateh)."',";
171  $sql .= " '".$this->db->idate($this->datef)."',";
172  $sql .= " ".((float) $this->amount).",";
173  $sql .= " '".$this->db->escape($this->note_public ? $this->note_public : $this->note)."')";
174 
175  $resql = $this->db->query($sql);
176  if (!$resql) {
177  $error++;
178  $this->errors[] = $this->db->lasterror();
179  }
180 
181  if (!$error) {
182  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
183  $this->fk_type = $type;
184  }
185 
186  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
187  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
188  }
189 
190  // Add object linked
191  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
192  foreach ($this->linked_objects as $origin => $tmp_origin_id) {
193  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, ...))
194  foreach ($tmp_origin_id as $origin_id) {
195  $ret = $this->add_object_linked($origin, $origin_id);
196  if (!$ret) {
197  $this->error = $this->db->lasterror();
198  $error++;
199  }
200  }
201  } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
202  $origin_id = $tmp_origin_id;
203  $ret = $this->add_object_linked($origin, $origin_id);
204  if (!$ret) {
205  $this->error = $this->db->lasterror();
206  $error++;
207  }
208  }
209  }
210  }
211 
212  if (!$error && !$notrigger) {
213  $this->context = array('member' => $member);
214  // Call triggers
215  $result = $this->call_trigger('MEMBER_SUBSCRIPTION_CREATE', $user);
216  if ($result < 0) {
217  $error++;
218  }
219  // End call triggers
220  }
221 
222  // Commit or rollback
223  if ($error) {
224  $this->db->rollback();
225  return -1;
226  } else {
227  $this->db->commit();
228  return $this->id;
229  }
230  }
231 
232 
239  public function fetch($rowid)
240  {
241  $sql = "SELECT rowid, fk_type, fk_adherent, datec,";
242  $sql .= " tms,";
243  $sql .= " dateadh as dateh,";
244  $sql .= " datef,";
245  $sql .= " subscription, note as note_public, fk_bank";
246  $sql .= " FROM ".MAIN_DB_PREFIX."subscription";
247  $sql .= " WHERE rowid = ".((int) $rowid);
248 
249  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
250  $resql = $this->db->query($sql);
251  if ($resql) {
252  if ($this->db->num_rows($resql)) {
253  $obj = $this->db->fetch_object($resql);
254 
255  $this->id = $obj->rowid;
256  $this->ref = $obj->rowid;
257 
258  $this->fk_type = $obj->fk_type;
259  $this->fk_adherent = $obj->fk_adherent;
260  $this->datec = $this->db->jdate($obj->datec);
261  $this->datem = $this->db->jdate($obj->tms);
262  $this->dateh = $this->db->jdate($obj->dateh);
263  $this->datef = $this->db->jdate($obj->datef);
264  $this->amount = $obj->subscription;
265  $this->note = $obj->note_public; // deprecated
266  $this->note_public = $obj->note_public;
267  $this->fk_bank = $obj->fk_bank;
268  return 1;
269  } else {
270  return 0;
271  }
272  } else {
273  $this->error = $this->db->lasterror();
274  return -1;
275  }
276  }
277 
278 
286  public function update($user, $notrigger = 0)
287  {
288  $error = 0;
289 
290  $this->db->begin();
291 
292  if (!is_numeric($this->amount)) {
293  $this->error = 'BadValueForParameterAmount';
294  return -1;
295  }
296 
297  if (empty($this->note_public) && !empty($this->note)) { // For backward compatibility
298  $this->note_public = $this->note;
299  }
300 
301  $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET ";
302  $sql .= " fk_type = ".((int) $this->fk_type).",";
303  $sql .= " fk_adherent = ".((int) $this->fk_adherent).",";
304  $sql .= " note = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : 'null').",";
305  $sql .= " subscription = ".(float) price2num($this->amount).",";
306  $sql .= " dateadh = '".$this->db->idate($this->dateh)."',";
307  $sql .= " datef = '".$this->db->idate($this->datef)."',";
308  $sql .= " datec = '".$this->db->idate($this->datec)."',";
309  $sql .= " fk_bank = ".($this->fk_bank ? ((int) $this->fk_bank) : 'null');
310  $sql .= " WHERE rowid = ".((int) $this->id);
311 
312  dol_syslog(get_class($this)."::update", LOG_DEBUG);
313  $resql = $this->db->query($sql);
314  if ($resql) {
315  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
316  $member = new Adherent($this->db);
317  $result = $member->fetch($this->fk_adherent);
318  $result = $member->update_end_date($user);
319 
320  if (!$error && !$notrigger) {
321  $this->context = array('member' => $member);
322  // Call triggers
323  $result = $this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY', $user);
324  if ($result < 0) {
325  $error++;
326  } //Do also here what you must do to rollback action if trigger fail
327  // End call triggers
328  }
329  } else {
330  $error++;
331  $this->error = $this->db->lasterror();
332  }
333 
334  // Commit or rollback
335  if ($error) {
336  $this->db->rollback();
337  return -1;
338  } else {
339  $this->db->commit();
340  return $this->id;
341  }
342  }
343 
351  public function delete($user, $notrigger = 0)
352  {
353  $error = 0;
354 
355  // It subscription is linked to a bank transaction, we get it
356  if ($this->fk_bank > 0) {
357  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
358  $accountline = new AccountLine($this->db);
359  $result = $accountline->fetch($this->fk_bank);
360  }
361 
362  $this->db->begin();
363 
364  if (!$error) {
365  if (!$notrigger) {
366  // Call triggers
367  $result = $this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user);
368  if ($result < 0) {
369  $error++;
370  } // Do also here what you must do to rollback action if trigger fail
371  // End call triggers
372  }
373  }
374 
375  if (!$error) {
376  $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE rowid = ".((int) $this->id);
377  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
378  $resql = $this->db->query($sql);
379  if ($resql) {
380  $num = $this->db->affected_rows($resql);
381  if ($num) {
382  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
383  $member = new Adherent($this->db);
384  $result = $member->fetch($this->fk_adherent);
385  $result = $member->update_end_date($user);
386 
387  if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) { // If we found bank account line (this means this->fk_bank defined)
388  $result = $accountline->delete($user); // Return false if refused because line is reconciled
389  if ($result > 0) {
390  $this->db->commit();
391  return 1;
392  } else {
393  $this->error = $accountline->error;
394  $this->db->rollback();
395  return -1;
396  }
397  } else {
398  $this->db->commit();
399  return 1;
400  }
401  } else {
402  $this->db->commit();
403  return 0;
404  }
405  } else {
406  $error++;
407  $this->error = $this->db->lasterror();
408  }
409  }
410 
411  // Commit or rollback
412  if ($error) {
413  $this->db->rollback();
414  return -1;
415  } else {
416  $this->db->commit();
417  return 1;
418  }
419  }
420 
421 
432  public function getNomUrl($withpicto = 0, $notooltip = 0, $option = '', $morecss = '', $save_lastsearch_value = -1)
433  {
434  global $langs;
435 
436  $result = '';
437 
438  $langs->load("members");
439 
440  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Subscription").'</u>';
441  /*if (isset($this->statut)) {
442  $label .= ' '.$this->getLibStatut(5);
443  }*/
444  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
445  if (!empty($this->dateh)) {
446  $label .= '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->dateh, 'day');
447  }
448  if (!empty($this->datef)) {
449  $label .= '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->datef, 'day');
450  }
451 
452  $url = DOL_URL_ROOT.'/adherents/subscription/card.php?rowid='.((int) $this->id);
453 
454  if ($option != 'nolink') {
455  // Add param to save lastsearch_values or not
456  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
457  if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
458  $add_save_lastsearch_values = 1;
459  }
460  if ($add_save_lastsearch_values) {
461  $url .= '&save_lastsearch_values=1';
462  }
463  }
464 
465  $linkstart = '<a href="'.$url.'" class="classfortooltip" title="'.dol_escape_htmltag($label, 1).'">';
466  $linkend = '</a>';
467 
468  $result .= $linkstart;
469  if ($withpicto) {
470  $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);
471  }
472  if ($withpicto != 2) {
473  $result .= $this->ref;
474  }
475  $result .= $linkend;
476 
477  return $result;
478  }
479 
480 
487  public function getLibStatut($mode = 0)
488  {
489  return '';
490  }
491 
492  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
500  public function LibStatut($status, $mode = 0)
501  {
502  // phpcs:enable
503 
504  //$langs->load("members");
505 
506  return '';
507  }
508 
515  public function info($id)
516  {
517  $sql = 'SELECT c.rowid, c.datec,';
518  $sql .= ' c.tms as datem';
519  $sql .= ' FROM '.MAIN_DB_PREFIX.'subscription as c';
520  $sql .= ' WHERE c.rowid = '.((int) $id);
521 
522  $resql = $this->db->query($sql);
523  if ($resql) {
524  if ($this->db->num_rows($resql)) {
525  $obj = $this->db->fetch_object($resql);
526  $this->id = $obj->rowid;
527 
528  $this->date_creation = $this->db->jdate($obj->datec);
529  $this->date_modification = $this->db->jdate($obj->datem);
530  }
531 
532  $this->db->free($resql);
533  } else {
534  dol_print_error($this->db);
535  }
536  }
537 
545  public function getKanbanView($option = '', $arraydata = null)
546  {
547  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
548 
549  $return = '<div class="box-flex-item box-flex-grow-zero">';
550  $return .= '<div class="info-box info-box-sm">';
551  $return .= '<span class="info-box-icon bg-infobox-action">';
552  $return .= img_picto('', $this->picto);
553  $return .= '</span>';
554 
555  $return .= '<div class="info-box-content">';
556  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">';
557  $return .= $this->getNomUrl(0);
558  $return .= '</span>';
559  if ($selected >= 0) {
560  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
561  }
562  if (property_exists($this, 'dateh') || property_exists($this, 'datef')) {
563  $return .= '<br><span class="info-box-status opacitymedium small">'.dol_print_date($this->dateh, 'day').' - '.dol_print_date($this->datef, 'day').'</span>';
564  }
565 
566  if (!empty($arraydata['member']) && is_object($arraydata['member'])) {
567  $return .= '<br><div class="inline-block tdoverflowmax150">'.$arraydata['member']->getNomUrl(-4).'</div>';
568  }
569 
570  if (property_exists($this, 'amount')) {
571  $return .= '<br><span class="amount inline-block">'.price($this->amount).'</span>';
572  if (!empty($arraydata['bank'])) {
573  $return .= ' &nbsp; <span class="info-box-label ">'.$arraydata['bank']->getNomUrl(-1).'</span>';
574  }
575  }
576  $return .= '</div>';
577  $return .= '</div>';
578  $return .= '</div>';
579  return $return;
580  }
581 }
$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,...
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
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.
create($user, $notrigger=0)
Function who permitted creation of the subscription.
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.
update($user, $notrigger=0)
Update subscription.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
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 '.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
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...
td amount
Definition: global.inc.php:554