dolibarr 21.0.0-beta
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
9 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
31require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
32
33
38{
42 public $element = 'user_bank_account';
43
47 public $table_element = 'user_rib';
48
49
55 public $datec;
56
62 public $datem;
63
69 public $userid;
70
71
77 public function __construct(DoliDB $db)
78 {
79 $this->db = $db;
80
81 $this->userid = 0;
82 $this->solde = 0;
83 $this->balance = 0;
84 }
85
86
94 public function create($user = null, $notrigger = 0)
95 {
96 $now = dol_now();
97
98 $sql = "INSERT INTO ".$this->db->prefix()."user_rib (fk_user, datec)";
99 $sql .= " VALUES (".$this->userid.", '".$this->db->idate($now)."')";
100 $resql = $this->db->query($sql);
101 if ($resql) {
102 if ($this->db->affected_rows($resql)) {
103 $this->id = $this->db->last_insert_id($this->db->prefix()."user_rib");
104
105 return $this->update($user);
106 } else {
107 return 0;
108 }
109 } else {
110 print $this->db->error();
111 return -1;
112 }
113 }
114
122 public function update($user = null, $notrigger = 0)
123 {
124 $error = 0;
125
126 $this->db->begin();
127
128 if (!$this->id) {
129 $this->create();
130 }
131
132 $sql = "UPDATE ".$this->db->prefix()."user_rib SET";
133 $sql .= " bank = '".$this->db->escape($this->bank)."'";
134 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
135 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
136 $sql .= ",number='".$this->db->escape($this->number)."'";
137 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
138 $sql .= ",bic='".$this->db->escape($this->bic)."'";
139 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
140 $sql .= ",domiciliation='".$this->db->escape($this->address)."'";
141 $sql .= ",proprio = '".$this->db->escape($this->owner_name)."'";
142 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
143 $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
144 $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
145 $sql .= ",fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
146
147 if (trim($this->label) != '') {
148 $sql .= ",label = '".$this->db->escape($this->label)."'";
149 } else {
150 $sql .= ",label = NULL";
151 }
152 $sql .= " WHERE rowid = ".((int) $this->id);
153
154 $result = $this->db->query($sql);
155 if (!$result) {
156 $error++;
157 $this->errors[] = $this->db->lasterror();
158 }
159
160 // Triggers
161 if (!$error && !$notrigger) {
162 // Call triggers
163 $result = $this->call_trigger(strtoupper(get_class($this)).'_MODIFY', $user);
164 if ($result < 0) {
165 $error++;
166 } //Do also here what you must do to rollback action if trigger fail
167 // End call triggers
168 }
169
170 // Commit or rollback
171 if ($error) {
172 $this->db->rollback();
173 return -1;
174 } else {
175 $this->db->commit();
176 return $this->id;
177 }
178 }
179
188 public function fetch($id, $ref = '', $userid = 0)
189 {
190 if (empty($id) && empty($ref) && empty($userid)) {
191 return -1;
192 }
193
194 $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 as address";
195 $sql .= ", ur.proprio as owner_name, ur.owner_address, ur.label, ur.datec, ur.tms as datem";
196 $sql .= ', ur.currency_code, ur.state_id, ur.fk_country as country_id';
197 $sql .= ', c.code as country_code, c.label as country';
198 $sql .= ', d.code_departement as state_code, d.nom as state';
199 $sql .= " FROM ".$this->db->prefix()."user_rib as ur";
200 $sql .= ' LEFT JOIN '.$this->db->prefix().'c_country as c ON ur.fk_country=c.rowid';
201 $sql .= ' LEFT JOIN '.$this->db->prefix().'c_departements as d ON ur.state_id=d.rowid';
202
203 if ($id) {
204 $sql .= " WHERE ur.rowid = ".((int) $id);
205 }
206 if ($ref) {
207 $sql .= " WHERE ur.label = '".$this->db->escape($ref)."'";
208 }
209 if ($userid) {
210 $sql .= " WHERE ur.fk_user = ".((int) $userid);
211 }
212
213 $resql = $this->db->query($sql);
214 if ($resql) {
215 if ($this->db->num_rows($resql)) {
216 $obj = $this->db->fetch_object($resql);
217
218 $this->id = $obj->rowid;
219 $this->userid = $obj->fk_user;
220 $this->bank = $obj->bank;
221 $this->code_banque = $obj->code_banque;
222 $this->code_guichet = $obj->code_guichet;
223 $this->number = $obj->number;
224 $this->cle_rib = $obj->cle_rib;
225 $this->bic = $obj->bic;
226 $this->iban = dolDecrypt($obj->iban);
227 $this->courant = self::TYPE_CURRENT;
228 $this->type = self::TYPE_CURRENT;
229
230 $this->address = $obj->address;
231
232 $this->owner_name = $obj->owner_name;
233 $this->proprio = $obj->owner_name;
234 $this->owner_address = $obj->owner_address;
235
236 $this->label = $obj->label;
237 $this->datec = $this->db->jdate($obj->datec);
238 $this->datem = $this->db->jdate($obj->datem);
239 $this->currency_code = $obj->currency_code;
240
241 $this->state_id = $obj->state_id;
242 $this->state_code = $obj->state_code;
243 $this->state = $obj->state;
244
245 $this->country_id = $obj->country_id;
246 $this->country_code = $obj->country_code;
247 $this->country = $obj->country;
248 }
249 $this->db->free($resql);
250
251 return 1;
252 } else {
253 dol_print_error($this->db);
254 return -1;
255 }
256 }
257
265 public function delete($user = null, $notrigger = 0)
266 {
267 $error = 0;
268
269 $this->db->begin();
270
271 // Delete link between tag and bank account
272 /*
273 if (!$error) {
274 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
275 $sql .= " WHERE fk_account = ".((int) $this->id);
276
277 $resql = $this->db->query($sql);
278 if (!$resql) {
279 $error++;
280 $this->error = "Error ".$this->db->lasterror();
281 }
282 }
283 */
284
285 if (!$error) {
286 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
287 $sql .= " WHERE rowid = ".((int) $this->id);
288
289 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
290 $result = $this->db->query($sql);
291 if ($result) {
292 // Remove extrafields
293 /*
294 if (!$error) {
295 $result = $this->deleteExtraFields();
296 if ($result < 0) {
297 $error++;
298 dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
299 }
300 }*/
301 } else {
302 $error++;
303 $this->error = "Error ".$this->db->lasterror();
304 }
305 }
306
307 if (!$error) {
308 $this->db->commit();
309 return 1;
310 } else {
311 $this->db->rollback();
312 return -1;
313 }
314 }
315
322 public function getRibLabel($displayriblabel = true)
323 {
324 $rib = '';
325
326 if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib) {
327 if ($this->label && $displayriblabel) {
328 $rib = $this->label." : ";
329 }
330
331 $rib .= $this->iban;
332 }
333
334 return $rib;
335 }
336
341 public function checkCountryBankAccount()
342 {
343 if (!empty($this->country_code)) {
344 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
345 $country_code_in_EEC = getCountriesInEEC();
346 return in_array($this->country_code, $country_code_in_EEC);
347 } else {
348 return false;
349 }
350 }
351}
Class to manage bank accounts.
solde($option=0, $date_end='', $field='dateo')
Return current balance.
error()
Return error.
const TYPE_CURRENT
Current account.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage bank accounts description of users.
getRibLabel($displayriblabel=true)
Return RIB.
update($user=null, $notrigger=0)
Update bank account.
create($user=null, $notrigger=0)
Create bank information record.
__construct(DoliDB $db)
Constructor.
checkCountryBankAccount()
Return if a country of userBank is inside the EEC (European Economic Community)
fetch($id, $ref='', $userid=0)
Load record from database.
getCountriesInEEC()
Return list of countries that are inside the EEC (European Economic Community) Note: Try to keep this...
dol_now($mode='auto')
Return date for now.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
div refaddress div address
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:149
dolDecrypt($chain, $key='')
Decode a string with a symmetric encryption.