dolibarr  19.0.0-dev
userbankaccount.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2010-2013 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2013 Peter Fontaine <contact@peterfontaine.fr>
6  * Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
7  * Copyright (C) 2016 Marcos GarcĂ­a <marcosgdf@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
29 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
30 
31 
35 class UserBankAccount extends Account
36 {
40  public $element = 'user_bank_account';
41 
45  public $table_element = 'user_rib';
46 
47 
53  public $datec;
54 
60  public $datem;
61 
67  public $userid;
68 
69 
75  public function __construct(DoliDB $db)
76  {
77  $this->db = $db;
78 
79  $this->userid = 0;
80  $this->solde = 0;
81  $this->balance = 0;
82  }
83 
84 
92  public function create(User $user = null, $notrigger = 0)
93  {
94  $now = dol_now();
95 
96  $sql = "INSERT INTO ".$this->db->prefix()."user_rib (fk_user, datec)";
97  $sql .= " VALUES (".$this->userid.", '".$this->db->idate($now)."')";
98  $resql = $this->db->query($sql);
99  if ($resql) {
100  if ($this->db->affected_rows($resql)) {
101  $this->id = $this->db->last_insert_id($this->db->prefix()."user_rib");
102 
103  return $this->update($user);
104  } else {
105  return 0;
106  }
107  } else {
108  print $this->db->error();
109  return -1;
110  }
111  }
112 
120  public function update(User $user = null, $notrigger = 0)
121  {
122  if (!$this->id) {
123  $this->create();
124  }
125 
126  $sql = "UPDATE ".$this->db->prefix()."user_rib SET";
127  $sql .= " bank = '".$this->db->escape($this->bank)."'";
128  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
129  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
130  $sql .= ",number='".$this->db->escape($this->number)."'";
131  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
132  $sql .= ",bic='".$this->db->escape($this->bic)."'";
133  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
134  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
135  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
136  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
137  $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
138  $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
139  $sql .= ",fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
140 
141  if (trim($this->label) != '') {
142  $sql .= ",label = '".$this->db->escape($this->label)."'";
143  } else {
144  $sql .= ",label = NULL";
145  }
146  $sql .= " WHERE rowid = ".((int) $this->id);
147 
148  $result = $this->db->query($sql);
149  if ($result) {
150  return 1;
151  } else {
152  dol_print_error($this->db);
153  return 0;
154  }
155  }
156 
165  public function fetch($id, $ref = '', $userid = 0)
166  {
167  if (empty($id) && empty($ref) && empty($userid)) {
168  return -1;
169  }
170 
171  $sql = "SELECT ur.rowid, ur.fk_user, ur.entity, ur.bank, ur.number, ur.code_banque, ur.code_guichet, ur.cle_rib, ur.bic, ur.iban_prefix as iban, ur.domiciliation, ur.proprio";
172  $sql .= ", ur.owner_address, ur.label, ur.datec, ur.tms as datem";
173  $sql .= ', ur.currency_code, ur.state_id, ur.fk_country as country_id';
174  $sql .= ', c.code as country_code, c.label as country';
175  $sql .= ', d.code_departement as state_code, d.nom as state';
176  $sql .= " FROM ".$this->db->prefix()."user_rib as ur";
177  $sql .= ' LEFT JOIN '.$this->db->prefix().'c_country as c ON ur.fk_country=c.rowid';
178  $sql .= ' LEFT JOIN '.$this->db->prefix().'c_departements as d ON ur.state_id=d.rowid';
179 
180  if ($id) {
181  $sql .= " WHERE ur.rowid = ".((int) $id);
182  }
183  if ($ref) {
184  $sql .= " WHERE ur.label = '".$this->db->escape($ref)."'";
185  }
186  if ($userid) {
187  $sql .= " WHERE ur.fk_user = ".((int) $userid);
188  }
189 
190  $resql = $this->db->query($sql);
191  if ($resql) {
192  if ($this->db->num_rows($resql)) {
193  $obj = $this->db->fetch_object($resql);
194 
195  $this->id = $obj->rowid;
196  $this->userid = $obj->fk_user;
197  $this->bank = $obj->bank;
198  $this->code_banque = $obj->code_banque;
199  $this->code_guichet = $obj->code_guichet;
200  $this->number = $obj->number;
201  $this->cle_rib = $obj->cle_rib;
202  $this->bic = $obj->bic;
203  $this->iban = $obj->iban;
204  $this->domiciliation = $obj->domiciliation;
205  $this->proprio = $obj->proprio;
206  $this->owner_address = $obj->owner_address;
207  $this->label = $obj->label;
208  $this->datec = $this->db->jdate($obj->datec);
209  $this->datem = $this->db->jdate($obj->datem);
210  $this->currency_code = $obj->currency_code;
211 
212  $this->state_id = $obj->state_id;
213  $this->state_code = $obj->state_code;
214  $this->state = $obj->state;
215 
216  $this->country_id = $obj->country_id;
217  $this->country_code = $obj->country_code;
218  $this->country = $obj->country;
219  }
220  $this->db->free($resql);
221 
222  return 1;
223  } else {
224  dol_print_error($this->db);
225  return -1;
226  }
227  }
228 
235  public function delete(User $user = null)
236  {
237  $error = 0;
238 
239  $this->db->begin();
240 
241  // Delete link between tag and bank account
242  /*
243  if (!$error) {
244  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
245  $sql .= " WHERE fk_account = ".((int) $this->id);
246 
247  $resql = $this->db->query($sql);
248  if (!$resql) {
249  $error++;
250  $this->error = "Error ".$this->db->lasterror();
251  }
252  }
253  */
254 
255  if (!$error) {
256  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
257  $sql .= " WHERE rowid = ".((int) $this->id);
258 
259  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
260  $result = $this->db->query($sql);
261  if ($result) {
262  // Remove extrafields
263  /*
264  if (!$error) {
265  $result = $this->deleteExtraFields();
266  if ($result < 0) {
267  $error++;
268  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
269  }
270  }*/
271  } else {
272  $error++;
273  $this->error = "Error ".$this->db->lasterror();
274  }
275  }
276 
277  if (!$error) {
278  $this->db->commit();
279  return 1;
280  } else {
281  $this->db->rollback();
282  return -1;
283  }
284  }
285 
292  public function getRibLabel($displayriblabel = true)
293  {
294  $rib = '';
295 
296  if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib) {
297  if ($this->label && $displayriblabel) {
298  $rib = $this->label." : ";
299  }
300 
301  $rib .= $this->iban;
302  }
303 
304  return $rib;
305  }
306 }
Class to manage bank accounts.
solde($option=0, $date_end='', $field='dateo')
Return current sold.
error()
Return error.
Class to manage Dolibarr database access.
Class to manage bank accounts description of users.
update(User $user=null, $notrigger=0)
Update bank account.
create(User $user=null, $notrigger=0)
Create bank information record.
getRibLabel($displayriblabel=true)
Return RIB.
__construct(DoliDB $db)
Constructor.
fetch($id, $ref='', $userid=0)
Load record from database.
Class to manage Dolibarr users.
Definition: user.class.php:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.