dolibarr  17.0.4
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->error_number = 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  }
105  } else {
106  print $this->db->error();
107  return 0;
108  }
109  }
110 
118  public function update(User $user = null, $notrigger = 0)
119  {
120  if (!$this->id) {
121  $this->create();
122  }
123 
124  $sql = "UPDATE ".$this->db->prefix()."user_rib SET";
125  $sql .= " bank = '".$this->db->escape($this->bank)."'";
126  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
127  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
128  $sql .= ",number='".$this->db->escape($this->number)."'";
129  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
130  $sql .= ",bic='".$this->db->escape($this->bic)."'";
131  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
132  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
133  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
134  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
135  $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
136  $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
137  $sql .= ",fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
138 
139  if (trim($this->label) != '') {
140  $sql .= ",label = '".$this->db->escape($this->label)."'";
141  } else {
142  $sql .= ",label = NULL";
143  }
144  $sql .= " WHERE rowid = ".((int) $this->id);
145 
146  $result = $this->db->query($sql);
147  if ($result) {
148  return 1;
149  } else {
150  dol_print_error($this->db);
151  return 0;
152  }
153  }
154 
163  public function fetch($id, $ref = '', $userid = 0)
164  {
165  if (empty($id) && empty($ref) && empty($userid)) {
166  return -1;
167  }
168 
169  $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";
170  $sql .= ", ur.owner_address, ur.label, ur.datec, ur.tms as datem";
171  $sql .= ', ur.currency_code, ur.state_id, ur.fk_country as country_id';
172  $sql .= ', c.code as country_code, c.label as country';
173  $sql .= ', d.code_departement as state_code, d.nom as state';
174  $sql .= " FROM ".$this->db->prefix()."user_rib as ur";
175  $sql .= ' LEFT JOIN '.$this->db->prefix().'c_country as c ON ur.fk_country=c.rowid';
176  $sql .= ' LEFT JOIN '.$this->db->prefix().'c_departements as d ON ur.state_id=d.rowid';
177 
178  if ($id) {
179  $sql .= " WHERE ur.rowid = ".((int) $id);
180  }
181  if ($ref) {
182  $sql .= " WHERE ur.label = '".$this->db->escape($ref)."'";
183  }
184  if ($userid) {
185  $sql .= " WHERE ur.fk_user = ".((int) $userid);
186  }
187 
188  $resql = $this->db->query($sql);
189  if ($resql) {
190  if ($this->db->num_rows($resql)) {
191  $obj = $this->db->fetch_object($resql);
192 
193  $this->id = $obj->rowid;
194  $this->userid = $obj->fk_user;
195  $this->bank = $obj->bank;
196  $this->code_banque = $obj->code_banque;
197  $this->code_guichet = $obj->code_guichet;
198  $this->number = $obj->number;
199  $this->cle_rib = $obj->cle_rib;
200  $this->bic = $obj->bic;
201  $this->iban = $obj->iban;
202  $this->domiciliation = $obj->domiciliation;
203  $this->proprio = $obj->proprio;
204  $this->owner_address = $obj->owner_address;
205  $this->label = $obj->label;
206  $this->datec = $this->db->jdate($obj->datec);
207  $this->datem = $this->db->jdate($obj->datem);
208  $this->currency_code = $obj->currency_code;
209 
210  $this->state_id = $obj->state_id;
211  $this->state_code = $obj->state_code;
212  $this->state = $obj->state;
213 
214  $this->country_id = $obj->country_id;
215  $this->country_code = $obj->country_code;
216  $this->country = $obj->country;
217  }
218  $this->db->free($resql);
219 
220  return 1;
221  } else {
222  dol_print_error($this->db);
223  return -1;
224  }
225  }
226 
233  public function delete(User $user = null)
234  {
235  $error = 0;
236 
237  $this->db->begin();
238 
239  // Delete link between tag and bank account
240  /*
241  if (!$error) {
242  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
243  $sql .= " WHERE fk_account = ".((int) $this->id);
244 
245  $resql = $this->db->query($sql);
246  if (!$resql) {
247  $error++;
248  $this->error = "Error ".$this->db->lasterror();
249  }
250  }
251  */
252 
253  if (!$error) {
254  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
255  $sql .= " WHERE rowid = ".((int) $this->id);
256 
257  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
258  $result = $this->db->query($sql);
259  if ($result) {
260  // Remove extrafields
261  /*
262  if (!$error) {
263  $result = $this->deleteExtraFields();
264  if ($result < 0) {
265  $error++;
266  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
267  }
268  }*/
269  } else {
270  $error++;
271  $this->error = "Error ".$this->db->lasterror();
272  }
273  }
274 
275  if (!$error) {
276  $this->db->commit();
277  return 1;
278  } else {
279  $this->db->rollback();
280  return -1;
281  }
282  }
283 
290  public function getRibLabel($displayriblabel = true)
291  {
292  $rib = '';
293 
294  if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib) {
295  if ($this->label && $displayriblabel) {
296  $rib = $this->label." : ";
297  }
298 
299  $rib .= $this->iban;
300  }
301 
302  return $rib;
303  }
304 }
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:47
if(isModEnabled('facture') &&!empty($user->rights->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') &&!empty($user->rights->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)) $resql
Social contributions to pay.
Definition: index.php:745
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.
$conf db
API class for accounts.
Definition: inc.php:41