dolibarr  13.0.2
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 //namespace DolibarrMember;
26 
27 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
28 
29 
34 {
38  public $element = 'subscription';
39 
43  public $table_element = 'subscription';
44 
48  public $ismultientitymanaged = 'fk_adherent@adherent';
49 
53  public $picto = 'payment';
54 
60  public $datec;
61 
67  public $datem;
68 
74  public $dateh;
75 
81  public $datef;
82 
86  public $fk_type;
87  public $fk_adherent;
88 
89  public $amount;
90 
94  public $fk_bank;
95 
96  public $fields = array(
97  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
98  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>15),
99  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>20),
100  'fk_adherent' =>array('type'=>'integer', 'label'=>'Member', 'enabled'=>1, 'visible'=>-1, 'position'=>25),
101  'dateadh' =>array('type'=>'datetime', 'label'=>'DateSubscription', 'enabled'=>1, 'visible'=>-1, 'position'=>30),
102  'datef' =>array('type'=>'datetime', 'label'=>'DateEndSubscription', 'enabled'=>1, 'visible'=>-1, 'position'=>35),
103  'subscription' =>array('type'=>'double(24,8)', 'label'=>'Amount', 'enabled'=>1, 'visible'=>-1, 'position'=>40, 'isameasure'=>1),
104  'fk_bank' =>array('type'=>'integer', 'label'=>'BankId', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
105  'note' =>array('type'=>'text', 'label'=>'Note', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
106  'fk_type' =>array('type'=>'integer', 'label'=>'MemberType', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
107  'fk_user_creat' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'position'=>60),
108  'fk_user_valid' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
109  );
110 
111 
117  public function __construct($db)
118  {
119  $this->db = $db;
120  }
121 
122 
130  public function create($user, $notrigger = false)
131  {
132  global $langs;
133 
134  $error = 0;
135 
136  $now = dol_now();
137 
138  // Check parameters
139  if ($this->datef <= $this->dateh) {
140  $this->error = $langs->trans("ErrorBadValueForDate");
141  return -1;
142  }
143  if (empty($this->datec)) $this->datec = $now;
144 
145 
146  $this->db->begin();
147 
148  $sql = "INSERT INTO ".MAIN_DB_PREFIX."subscription (fk_adherent, fk_type, datec, dateadh, datef, subscription, note)";
149 
150  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
151  $member = new Adherent($this->db);
152  $result = $member->fetch($this->fk_adherent);
153 
154  if ($this->fk_type == null) { // If type not defined, we use the type of member
155  $type = $member->typeid;
156  } else {
157  $type = $this->fk_type;
158  }
159  $sql .= " VALUES (".$this->fk_adherent.", '".$this->db->escape($type)."', '".$this->db->idate($now)."',";
160  $sql .= " '".$this->db->idate($this->dateh)."',";
161  $sql .= " '".$this->db->idate($this->datef)."',";
162  $sql .= " ".$this->amount.",";
163  $sql .= " '".$this->db->escape($this->note_public ? $this->note_public : $this->note)."')";
164 
165  $resql = $this->db->query($sql);
166  if (!$resql) {
167  $error++;
168  $this->errors[] = $this->db->lasterror();
169  }
170 
171  if (!$error) {
172  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
173  $this->fk_type = $type;
174  }
175 
176  if (!$error && !$notrigger) {
177  $this->context = array('member' => $member);
178  // Call triggers
179  $result = $this->call_trigger('MEMBER_SUBSCRIPTION_CREATE', $user);
180  if ($result < 0) { $error++; }
181  // End call triggers
182  }
183 
184  // Commit or rollback
185  if ($error) {
186  $this->db->rollback();
187  return -1;
188  } else {
189  $this->db->commit();
190  return $this->id;
191  }
192  }
193 
194 
201  public function fetch($rowid)
202  {
203  $sql = "SELECT rowid, fk_type, fk_adherent, datec,";
204  $sql .= " tms,";
205  $sql .= " dateadh as dateh,";
206  $sql .= " datef,";
207  $sql .= " subscription, note, fk_bank";
208  $sql .= " FROM ".MAIN_DB_PREFIX."subscription";
209  $sql .= " WHERE rowid=".$rowid;
210 
211  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
212  $resql = $this->db->query($sql);
213  if ($resql) {
214  if ($this->db->num_rows($resql)) {
215  $obj = $this->db->fetch_object($resql);
216 
217  $this->id = $obj->rowid;
218  $this->ref = $obj->rowid;
219 
220  $this->fk_type = $obj->fk_type;
221  $this->fk_adherent = $obj->fk_adherent;
222  $this->datec = $this->db->jdate($obj->datec);
223  $this->datem = $this->db->jdate($obj->tms);
224  $this->dateh = $this->db->jdate($obj->dateh);
225  $this->datef = $this->db->jdate($obj->datef);
226  $this->amount = $obj->subscription;
227  $this->note = $obj->note;
228  $this->fk_bank = $obj->fk_bank;
229  return 1;
230  } else {
231  return 0;
232  }
233  } else {
234  $this->error = $this->db->lasterror();
235  return -1;
236  }
237  }
238 
239 
247  public function update($user, $notrigger = 0)
248  {
249  $error = 0;
250 
251  $this->db->begin();
252 
253  if (!is_numeric($this->amount)) {
254  $this->error = 'BadValueForParameterAmount';
255  return -1;
256  }
257 
258  $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET ";
259  $sql .= " fk_type = ".$this->fk_type.",";
260  $sql .= " fk_adherent = ".$this->fk_adherent.",";
261  $sql .= " note=".($this->note ? "'".$this->db->escape($this->note)."'" : 'null').",";
262  $sql .= " subscription = ".price2num($this->amount).",";
263  $sql .= " dateadh='".$this->db->idate($this->dateh)."',";
264  $sql .= " datef='".$this->db->idate($this->datef)."',";
265  $sql .= " datec='".$this->db->idate($this->datec)."',";
266  $sql .= " fk_bank = ".($this->fk_bank ? $this->fk_bank : 'null');
267  $sql .= " WHERE rowid = ".$this->id;
268 
269  dol_syslog(get_class($this)."::update", LOG_DEBUG);
270  $resql = $this->db->query($sql);
271  if ($resql) {
272  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
273  $member = new Adherent($this->db);
274  $result = $member->fetch($this->fk_adherent);
275  $result = $member->update_end_date($user);
276 
277  if (!$error && !$notrigger) {
278  $this->context = array('member'=>$member);
279  // Call triggers
280  $result = $this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY', $user);
281  if ($result < 0) { $error++; } //Do also here what you must do to rollback action if trigger fail
282  // End call triggers
283  }
284  } else {
285  $error++;
286  $this->error = $this->db->lasterror();
287  }
288 
289  // Commit or rollback
290  if ($error) {
291  $this->db->rollback();
292  return -1;
293  } else {
294  $this->db->commit();
295  return $this->id;
296  }
297  }
298 
306  public function delete($user, $notrigger = false)
307  {
308  $error = 0;
309 
310  // It subscription is linked to a bank transaction, we get it
311  if ($this->fk_bank > 0) {
312  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
313  $accountline = new AccountLine($this->db);
314  $result = $accountline->fetch($this->fk_bank);
315  }
316 
317  $this->db->begin();
318 
319  if (!$error) {
320  if (!$notrigger) {
321  // Call triggers
322  $result = $this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user);
323  if ($result < 0) { $error++; } // Do also here what you must do to rollback action if trigger fail
324  // End call triggers
325  }
326  }
327 
328  if (!$error) {
329  $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE rowid = ".$this->id;
330  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
331  $resql = $this->db->query($sql);
332  if ($resql) {
333  $num = $this->db->affected_rows($resql);
334  if ($num) {
335  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
336  $member = new Adherent($this->db);
337  $result = $member->fetch($this->fk_adherent);
338  $result = $member->update_end_date($user);
339 
340  if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) { // If we found bank account line (this means this->fk_bank defined)
341  $result = $accountline->delete($user); // Return false if refused because line is conciliated
342  if ($result > 0) {
343  $this->db->commit();
344  return 1;
345  } else {
346  $this->error = $accountline->error;
347  $this->db->rollback();
348  return -1;
349  }
350  } else {
351  $this->db->commit();
352  return 1;
353  }
354  } else {
355  $this->db->commit();
356  return 0;
357  }
358  } else {
359  $error++;
360  $this->error = $this->db->lasterror();
361  }
362  }
363 
364  // Commit or rollback
365  if ($error) {
366  $this->db->rollback();
367  return -1;
368  } else {
369  $this->db->commit();
370  return 1;
371  }
372  }
373 
374 
385  public function getNomUrl($withpicto = 0, $notooltip = 0, $option = '', $morecss = '', $save_lastsearch_value = -1)
386  {
387  global $langs;
388 
389  $result = '';
390 
391  $langs->load("members");
392 
393  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Subscription").'</u>';
394  /*if (isset($this->statut)) {
395  $label .= ' '.$this->getLibStatut(5);
396  }*/
397  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
398  if (!empty($this->dateh)) {
399  $label .= '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->dateh, 'day');
400  }
401  if (!empty($this->datef)) {
402  $label .= '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->datef, 'day');
403  }
404 
405  $url = DOL_URL_ROOT.'/adherents/subscription/card.php?rowid='.$this->id;
406 
407  if ($option != 'nolink') {
408  // Add param to save lastsearch_values or not
409  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
410  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
411  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
412  }
413 
414  $linkstart = '<a href="'.$url.'" class="classfortooltip" title="'.dol_escape_htmltag($label, 1).'">';
415  $linkend = '</a>';
416 
417  $result .= $linkstart;
418  if ($withpicto) $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);
419  if ($withpicto != 2) $result .= $this->ref;
420  $result .= $linkend;
421 
422  return $result;
423  }
424 
425 
432  public function getLibStatut($mode = 0)
433  {
434  return '';
435  }
436 
437  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
444  public function LibStatut($status)
445  {
446  // phpcs:enable
447  global $langs;
448  $langs->load("members");
449  return '';
450  }
451 
458  public function info($id)
459  {
460  $sql = 'SELECT c.rowid, c.datec,';
461  $sql .= ' c.tms as datem';
462  $sql .= ' FROM '.MAIN_DB_PREFIX.'subscription as c';
463  $sql .= ' WHERE c.rowid = '.$id;
464 
465  $result = $this->db->query($sql);
466  if ($result) {
467  if ($this->db->num_rows($result)) {
468  $obj = $this->db->fetch_object($result);
469  $this->id = $obj->rowid;
470 
471  $this->date_creation = $this->db->jdate($obj->datec);
472  $this->date_modification = $this->db->jdate($obj->datem);
473  }
474 
475  $this->db->free($result);
476  } else {
477  dol_print_error($this->db);
478  }
479  }
480 }
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;une adhesion.
info($id)
Load information of the subscription object.
fetch($rowid)
Method to load a subscription.
dol_now($mode= 'auto')
Return date for now.
Class to manage bank transaction lines.
$conf db
API class for accounts.
Definition: inc.php:54
getNomUrl($withpicto=0, $notooltip=0, $option= '', $morecss= '', $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage members of a foundation.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Class to manage subscriptions of foundation members.
LibStatut($status)
Renvoi le libelle d&#39;un statut donne.
print $_SERVER["PHP_SELF"]
Edit parameters.
create($user, $notrigger=false)
Function who permitted cretaion of the subscription.
__construct($db)
Constructor.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
update($user, $notrigger=0)
Update subscription.
call_trigger($triggerName, $user)
Call trigger based on this instance.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
if(!empty($search_group)) natural_search(array("g.nom"g note
Definition: list.php:122
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...