dolibarr  7.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  *
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 {
35  public $element='subscription';
36  public $table_element='subscription';
37  public $picto='payment';
38 
39  var $datec; // Date creation
40  var $datem; // Date modification
41  var $dateh; // Subscription start date (date subscription)
42  var $datef; // Subscription end date
43  var $fk_adherent;
44  var $amount;
45  var $fk_bank;
46 
47 
53  function __construct($db)
54  {
55  $this->db = $db;
56  }
57 
58 
66  function create($user, $notrigger = false)
67  {
68  global $langs;
69 
70  $error = 0;
71 
72  $now=dol_now();
73 
74  // Check parameters
75  if ($this->datef <= $this->dateh)
76  {
77  $this->error=$langs->trans("ErrorBadValueForDate");
78  return -1;
79  }
80 
81  $this->db->begin();
82 
83  $sql = "INSERT INTO ".MAIN_DB_PREFIX."subscription (fk_adherent, datec, dateadh, datef, subscription, note)";
84  $sql.= " VALUES (".$this->fk_adherent.", '".$this->db->idate($now)."',";
85  $sql.= " '".$this->db->idate($this->dateh)."',";
86  $sql.= " '".$this->db->idate($this->datef)."',";
87  $sql.= " ".$this->amount.",";
88  $sql.= " '".$this->db->escape($this->note)."')";
89 
90  $resql = $this->db->query($sql);
91  if (! $resql) {
92  $error++;
93  $this->errors[] = $this->db->lasterror();
94  }
95 
96  if (! $error)
97  {
98  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
99  }
100 
101  if (! $error && ! $notrigger)
102  {
103  // Call triggers
104  $result=$this->call_trigger('MEMBER_SUBSCRIPTION_CREATE',$user);
105  if ($result < 0) { $error++; }
106  // End call triggers
107  }
108 
109  // Commit or rollback
110  if ($error) {
111  $this->db->rollback();
112  return -1;
113  } else {
114  $this->db->commit();
115  return $this->id;
116  }
117  }
118 
119 
126  function fetch($rowid)
127  {
128  $sql ="SELECT rowid, fk_adherent, datec,";
129  $sql.=" tms,";
130  $sql.=" dateadh as dateh,";
131  $sql.=" datef,";
132  $sql.=" subscription, note, fk_bank";
133  $sql.=" FROM ".MAIN_DB_PREFIX."subscription";
134  $sql.=" WHERE rowid=".$rowid;
135 
136  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
137  $resql=$this->db->query($sql);
138  if ($resql)
139  {
140  if ($this->db->num_rows($resql))
141  {
142  $obj = $this->db->fetch_object($resql);
143 
144  $this->id = $obj->rowid;
145  $this->ref = $obj->rowid;
146 
147  $this->fk_adherent = $obj->fk_adherent;
148  $this->datec = $this->db->jdate($obj->datec);
149  $this->datem = $this->db->jdate($obj->tms);
150  $this->dateh = $this->db->jdate($obj->dateh);
151  $this->datef = $this->db->jdate($obj->datef);
152  $this->amount = $obj->subscription;
153  $this->note = $obj->note;
154  $this->fk_bank = $obj->fk_bank;
155  return 1;
156  }
157  else
158  {
159  return 0;
160  }
161  }
162  else
163  {
164  $this->error=$this->db->lasterror();
165  return -1;
166  }
167  }
168 
169 
177  function update($user, $notrigger=0)
178  {
179  $error = 0;
180 
181  $this->db->begin();
182 
183  $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET ";
184  $sql .= " fk_adherent = ".$this->fk_adherent.",";
185  $sql .= " note=".($this->note ? "'".$this->db->escape($this->note)."'" : 'null').",";
186  $sql .= " subscription = '".price2num($this->amount)."',";
187  $sql .= " dateadh='".$this->db->idate($this->dateh)."',";
188  $sql .= " datef='".$this->db->idate($this->datef)."',";
189  $sql .= " datec='".$this->db->idate($this->datec)."',";
190  $sql .= " fk_bank = ".($this->fk_bank ? $this->fk_bank : 'null');
191  $sql .= " WHERE rowid = ".$this->id;
192 
193  dol_syslog(get_class($this)."::update", LOG_DEBUG);
194  $resql = $this->db->query($sql);
195  if ($resql)
196  {
197  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
198  $member=new Adherent($this->db);
199  $result=$member->fetch($this->fk_adherent);
200  $result=$member->update_end_date($user);
201 
202  if (! $error && ! $notrigger) {
203  // Call triggers
204  $result=$this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY',$user);
205  if ($result < 0) { $error++; } //Do also here what you must do to rollback action if trigger fail
206  // End call triggers
207  }
208  }
209  else
210  {
211  $error++;
212  $this->error=$this->db->lasterror();
213  }
214 
215  // Commit or rollback
216  if ($error) {
217  $this->db->rollback();
218  return -1;
219  } else {
220  $this->db->commit();
221  return $this->id;
222  }
223  }
224 
232  function delete($user, $notrigger=false)
233  {
234  $error = 0;
235 
236  // It subscription is linked to a bank transaction, we get it
237  if ($this->fk_bank > 0)
238  {
239  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
240  $accountline=new AccountLine($this->db);
241  $result=$accountline->fetch($this->fk_bank);
242  }
243 
244  $this->db->begin();
245 
246  if (! $error) {
247  if (! $notrigger) {
248  // Call triggers
249  $result=$this->call_trigger('MEMBER_SUBSCRIPTION_DELETE', $user);
250  if ($result < 0) { $error++; } // Do also here what you must do to rollback action if trigger fail
251  // End call triggers
252  }
253  }
254 
255  if (! $error)
256  {
257  $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE rowid = ".$this->id;
258  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
259  $resql=$this->db->query($sql);
260  if ($resql)
261  {
262  $num=$this->db->affected_rows($resql);
263  if ($num)
264  {
265  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
266  $member=new Adherent($this->db);
267  $result=$member->fetch($this->fk_adherent);
268  $result=$member->update_end_date($user);
269 
270  if ($this->fk_bank > 0 && is_object($accountline) && $accountline->id > 0) // If we found bank account line (this means this->fk_bank defined)
271  {
272  $result=$accountline->delete($user); // Return false if refused because line is conciliated
273  if ($result > 0)
274  {
275  $this->db->commit();
276  return 1;
277  }
278  else
279  {
280  $this->error=$accountline->error;
281  $this->db->rollback();
282  return -1;
283  }
284  }
285  else
286  {
287  $this->db->commit();
288  return 1;
289  }
290  }
291  else
292  {
293  $this->db->commit();
294  return 0;
295  }
296  }
297  else
298  {
299  $error++;
300  $this->error=$this->db->lasterror();
301  }
302  }
303 
304  // Commit or rollback
305  if ($error) {
306  $this->db->rollback();
307  return -1;
308  } else {
309  $this->db->commit();
310  return 1;
311  }
312  }
313 
314 
322  function getNomUrl($withpicto=0, $notooltip=0)
323  {
324  global $langs;
325 
326  $result='';
327  $label=$langs->trans("ShowSubscription").': '.$this->ref;
328 
329  $linkstart = '<a href="'.DOL_URL_ROOT.'/adherents/subscription/card.php?rowid='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
330  $linkend='</a>';
331 
332  $picto='payment';
333 
334  $result .= $linkstart;
335  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);
336  if ($withpicto != 2) $result.= $this->ref;
337  $result .= $linkend;
338 
339  return $result;
340  }
341 
342 
349  function getLibStatut($mode=0)
350  {
351  return '';
352  }
353 
360  function LibStatut($statut)
361  {
362  global $langs;
363  $langs->load("members");
364  return '';
365  }
366 
373  function info($id)
374  {
375  $sql = 'SELECT c.rowid, c.datec,';
376  $sql.= ' c.tms as datem';
377  $sql.= ' FROM '.MAIN_DB_PREFIX.'subscription as c';
378  $sql.= ' WHERE c.rowid = '.$id;
379 
380  $result=$this->db->query($sql);
381  if ($result)
382  {
383  if ($this->db->num_rows($result))
384  {
385  $obj = $this->db->fetch_object($result);
386  $this->id = $obj->rowid;
387 
388  $this->date_creation = $this->db->jdate($obj->datec);
389  $this->date_modification = $this->db->jdate($obj->datem);
390  }
391 
392  $this->db->free($result);
393 
394  }
395  else
396  {
397  dol_print_error($this->db);
398  }
399  }
400 }
getLibStatut($mode=0)
Retourne le libelle du statut d'une adhesion.
info($id)
Load information of the subscription object.
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.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
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.
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:1013
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'un statut donne.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)