dolibarr  9.0.0
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 <http://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 $picto='payment';
49 
50  public $datec; // Date creation
51  public $datem; // Date modification
52  public $dateh; // Subscription start date (date subscription)
53  public $datef; // Subscription end date
54 
58  public $fk_adherent;
59 
60  public $amount;
61 
65  public $fk_bank;
66 
67 
73  function __construct($db)
74  {
75  $this->db = $db;
76  }
77 
78 
86  function create($user, $notrigger = false)
87  {
88  global $langs;
89 
90  $error = 0;
91 
92  $now=dol_now();
93 
94  // Check parameters
95  if ($this->datef <= $this->dateh)
96  {
97  $this->error=$langs->trans("ErrorBadValueForDate");
98  return -1;
99  }
100  if (empty($this->datec)) $this->datec = $now;
101 
102 
103  $this->db->begin();
104 
105  $sql = "INSERT INTO ".MAIN_DB_PREFIX."subscription (fk_adherent, datec, dateadh, datef, subscription, note)";
106  $sql.= " VALUES (".$this->fk_adherent.", '".$this->db->idate($this->datec)."',";
107  $sql.= " '".$this->db->idate($this->dateh)."',";
108  $sql.= " '".$this->db->idate($this->datef)."',";
109  $sql.= " ".$this->amount.",";
110  $sql.= " '".$this->db->escape($this->note_public?$this->note_public:$this->note)."')";
111 
112  $resql = $this->db->query($sql);
113  if (! $resql) {
114  $error++;
115  $this->errors[] = $this->db->lasterror();
116  }
117 
118  if (! $error)
119  {
120  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
121  }
122 
123  if (! $error && ! $notrigger)
124  {
125  // Call triggers
126  $result=$this->call_trigger('MEMBER_SUBSCRIPTION_CREATE',$user);
127  if ($result < 0) { $error++; }
128  // End call triggers
129  }
130 
131  // Commit or rollback
132  if ($error) {
133  $this->db->rollback();
134  return -1;
135  } else {
136  $this->db->commit();
137  return $this->id;
138  }
139  }
140 
141 
148  function fetch($rowid)
149  {
150  $sql ="SELECT rowid, fk_adherent, datec,";
151  $sql.=" tms,";
152  $sql.=" dateadh as dateh,";
153  $sql.=" datef,";
154  $sql.=" subscription, note, fk_bank";
155  $sql.=" FROM ".MAIN_DB_PREFIX."subscription";
156  $sql.=" WHERE rowid=".$rowid;
157 
158  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
159  $resql=$this->db->query($sql);
160  if ($resql)
161  {
162  if ($this->db->num_rows($resql))
163  {
164  $obj = $this->db->fetch_object($resql);
165 
166  $this->id = $obj->rowid;
167  $this->ref = $obj->rowid;
168 
169  $this->fk_adherent = $obj->fk_adherent;
170  $this->datec = $this->db->jdate($obj->datec);
171  $this->datem = $this->db->jdate($obj->tms);
172  $this->dateh = $this->db->jdate($obj->dateh);
173  $this->datef = $this->db->jdate($obj->datef);
174  $this->amount = $obj->subscription;
175  $this->note = $obj->note;
176  $this->fk_bank = $obj->fk_bank;
177  return 1;
178  }
179  else
180  {
181  return 0;
182  }
183  }
184  else
185  {
186  $this->error=$this->db->lasterror();
187  return -1;
188  }
189  }
190 
191 
199  function update($user, $notrigger=0)
200  {
201  $error = 0;
202 
203  $this->db->begin();
204 
205  $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET ";
206  $sql .= " fk_adherent = ".$this->fk_adherent.",";
207  $sql .= " note=".($this->note ? "'".$this->db->escape($this->note)."'" : 'null').",";
208  $sql .= " subscription = '".price2num($this->amount)."',";
209  $sql .= " dateadh='".$this->db->idate($this->dateh)."',";
210  $sql .= " datef='".$this->db->idate($this->datef)."',";
211  $sql .= " datec='".$this->db->idate($this->datec)."',";
212  $sql .= " fk_bank = ".($this->fk_bank ? $this->fk_bank : 'null');
213  $sql .= " WHERE rowid = ".$this->id;
214 
215  dol_syslog(get_class($this)."::update", LOG_DEBUG);
216  $resql = $this->db->query($sql);
217  if ($resql)
218  {
219  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
220  $member=new Adherent($this->db);
221  $result=$member->fetch($this->fk_adherent);
222  $result=$member->update_end_date($user);
223 
224  if (! $error && ! $notrigger) {
225  // Call triggers
226  $result=$this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY',$user);
227  if ($result < 0) { $error++; } //Do also here what you must do to rollback action if trigger fail
228  // End call triggers
229  }
230  }
231  else
232  {
233  $error++;
234  $this->error=$this->db->lasterror();
235  }
236 
237  // Commit or rollback
238  if ($error) {
239  $this->db->rollback();
240  return -1;
241  } else {
242  $this->db->commit();
243  return $this->id;
244  }
245  }
246 
254  function delete($user, $notrigger=false)
255  {
256  $error = 0;
257 
258  // It subscription is linked to a bank transaction, we get it
259  if ($this->fk_bank > 0)
260  {
261  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
262  $accountline=new AccountLine($this->db);
263  $result=$accountline->fetch($this->fk_bank);
264  }
265 
266  $this->db->begin();
267 
268  if (! $error) {
269  if (! $notrigger) {
270  // Call triggers
271  $result=$this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user);
272  if ($result < 0) { $error++; } // Do also here what you must do to rollback action if trigger fail
273  // End call triggers
274  }
275  }
276 
277  if (! $error)
278  {
279  $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE rowid = ".$this->id;
280  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
281  $resql=$this->db->query($sql);
282  if ($resql)
283  {
284  $num=$this->db->affected_rows($resql);
285  if ($num)
286  {
287  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
288  $member=new Adherent($this->db);
289  $result=$member->fetch($this->fk_adherent);
290  $result=$member->update_end_date($user);
291 
292  if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) // If we found bank account line (this means this->fk_bank defined)
293  {
294  $result=$accountline->delete($user); // Return false if refused because line is conciliated
295  if ($result > 0)
296  {
297  $this->db->commit();
298  return 1;
299  }
300  else
301  {
302  $this->error=$accountline->error;
303  $this->db->rollback();
304  return -1;
305  }
306  }
307  else
308  {
309  $this->db->commit();
310  return 1;
311  }
312  }
313  else
314  {
315  $this->db->commit();
316  return 0;
317  }
318  }
319  else
320  {
321  $error++;
322  $this->error=$this->db->lasterror();
323  }
324  }
325 
326  // Commit or rollback
327  if ($error) {
328  $this->db->rollback();
329  return -1;
330  } else {
331  $this->db->commit();
332  return 1;
333  }
334  }
335 
336 
344  function getNomUrl($withpicto=0, $notooltip=0)
345  {
346  global $langs;
347 
348  $result='';
349 
350  $langs->load("members");
351  $label=$langs->trans("ShowSubscription").': '.$this->ref;
352 
353  $linkstart = '<a href="'.DOL_URL_ROOT.'/adherents/subscription/card.php?rowid='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
354  $linkend='</a>';
355 
356  $picto='payment';
357 
358  $result .= $linkstart;
359  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);
360  if ($withpicto != 2) $result.= $this->ref;
361  $result .= $linkend;
362 
363  return $result;
364  }
365 
366 
373  function getLibStatut($mode=0)
374  {
375  return '';
376  }
377 
378  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
385  function LibStatut($statut)
386  {
387  // phpcs:enable
388  global $langs;
389  $langs->load("members");
390  return '';
391  }
392 
399  function info($id)
400  {
401  $sql = 'SELECT c.rowid, c.datec,';
402  $sql.= ' c.tms as datem';
403  $sql.= ' FROM '.MAIN_DB_PREFIX.'subscription as c';
404  $sql.= ' WHERE c.rowid = '.$id;
405 
406  $result=$this->db->query($sql);
407  if ($result)
408  {
409  if ($this->db->num_rows($result))
410  {
411  $obj = $this->db->fetch_object($result);
412  $this->id = $obj->rowid;
413 
414  $this->date_creation = $this->db->jdate($obj->datec);
415  $this->date_modification = $this->db->jdate($obj->datem);
416  }
417 
418  $this->db->free($result);
419  }
420  else
421  {
422  dol_print_error($this->db);
423  }
424  }
425 }
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;une adhesion.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
info($id)
Load information of the subscription object.
if(! empty($search_group)) natural_search(array("g.nom" g note
Definition: list.php:123
fetch($rowid)
Method to load a subscription.
Class to manage bank transaction lines.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage members of a foundation.
dol_now($mode='gmt')
Return date for now.
Class to manage subscriptions of foundation members.
create($user, $notrigger=false)
Function who permitted cretaion of the subscription.
__construct($db)
Constructor.
update($user, $notrigger=0)
Update subscription.
getNomUrl($withpicto=0, $notooltip=0)
Return clicable name (with picto eventually)
call_trigger($trigger_name, $user)
Call trigger based on this instance.
LibStatut($statut)
Renvoi le libelle d&#39;un statut donne.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)