dolibarr 25.0.0-alpha
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-2026 Frédéric France <frederic.france@free.fr>
9 * Copyright (C) 2024-2026 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, $notrigger = 0)
95 {
96 $now = dol_now();
97
98 $sql = "INSERT INTO ".$this->db->prefix()."user_rib (fk_user, datec)";
99 $sql .= " VALUES (".((int) $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 $result = $this->update($user, 1);
106 if ($result < 0) {
107 return $result;
108 }
109
110 if (!$notrigger) {
111 // Call trigger
112 $result = $this->call_trigger(strtoupper(get_class($this)).'_CREATE', $user);
113 if ($result < 0) {
114 return -1;
115 }
116 // End call triggers
117 }
118
119 return $this->id;
120 } else {
121 return 0;
122 }
123 } else {
124 print $this->db->error();
125 return -1;
126 }
127 }
128
136 public function update($user, $notrigger = 0)
137 {
138 $error = 0;
139
140 $this->db->begin();
141
142 if (!$this->id) {
143 $this->create($user, $notrigger);
144 }
145
146 $sql = "UPDATE ".$this->db->prefix()."user_rib SET";
147 $sql .= " bank = '".$this->db->escape($this->bank)."'";
148 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
149 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
150 $sql .= ",number='".$this->db->escape($this->number)."'";
151 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
152 $sql .= ",bic='".$this->db->escape($this->bic)."'";
153 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
154 $sql .= ",domiciliation='".$this->db->escape($this->address)."'";
155 $sql .= ",proprio = '".$this->db->escape($this->owner_name)."'";
156 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
157 $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
158 $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
159 $sql .= ",fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
160
161 if (trim($this->label) != '') {
162 $sql .= ",label = '".$this->db->escape($this->label)."'";
163 } else {
164 $sql .= ",label = NULL";
165 }
166 $sql .= " WHERE rowid = ".((int) $this->id);
167
168 $result = $this->db->query($sql);
169 if (!$result) {
170 $error++;
171 $this->errors[] = $this->db->lasterror();
172 }
173
174 // Triggers
175 if (!$error && !$notrigger) {
176 // Call triggers
177 $result = $this->call_trigger(strtoupper(get_class($this)).'_MODIFY', $user);
178 if ($result < 0) {
179 $error++;
180 } //Do also here what you must do to rollback action if trigger fail
181 // End call triggers
182 }
183
184 // Commit or rollback
185 if ($error) {
186 $this->db->rollback();
187 return -1;
188 } else {
189 $this->db->commit();
190 return $this->id;
191 }
192 }
193
202 public function fetch($id, $ref = '', $userid = 0)
203 {
204 if (empty($id) && empty($ref) && empty($userid)) {
205 return -1;
206 }
207
208 $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";
209 $sql .= ", ur.proprio as owner_name, ur.owner_address, ur.label, ur.datec, ur.tms as datem";
210 $sql .= ', ur.currency_code, ur.state_id, ur.fk_country as country_id';
211 $sql .= ', c.code as country_code, c.label as country';
212 $sql .= ', d.code_departement as state_code, d.nom as state';
213 $sql .= " FROM ".$this->db->prefix()."user_rib as ur";
214 $sql .= ' LEFT JOIN '.$this->db->prefix().'c_country as c ON ur.fk_country=c.rowid';
215 $sql .= ' LEFT JOIN '.$this->db->prefix().'c_departements as d ON ur.state_id=d.rowid';
216
217 if ($id) {
218 $sql .= " WHERE ur.rowid = ".((int) $id);
219 }
220 if ($ref) {
221 $sql .= " WHERE ur.label = '".$this->db->escape($ref)."'";
222 }
223 if ($userid) {
224 $sql .= " WHERE ur.fk_user = ".((int) $userid);
225 }
226
227 $resql = $this->db->query($sql);
228 if ($resql) {
229 if ($this->db->num_rows($resql)) {
230 $obj = $this->db->fetch_object($resql);
231
232 $this->id = $obj->rowid;
233 $this->userid = $obj->fk_user;
234 $this->bank = $obj->bank;
235 $this->code_banque = $obj->code_banque;
236 $this->code_guichet = $obj->code_guichet;
237 $this->number = $obj->number;
238 $this->cle_rib = $obj->cle_rib;
239 $this->bic = $obj->bic;
240 $this->iban = dolDecrypt($obj->iban);
241 $this->courant = self::TYPE_CURRENT;
242 $this->type = self::TYPE_CURRENT;
243
244 $this->address = $obj->address;
245
246 $this->owner_name = $obj->owner_name;
247 $this->proprio = $obj->owner_name;
248 $this->owner_address = $obj->owner_address;
249
250 $this->label = $obj->label;
251 $this->datec = $this->db->jdate($obj->datec);
252 $this->datem = $this->db->jdate($obj->datem);
253 $this->currency_code = $obj->currency_code;
254
255 $this->state_id = $obj->state_id;
256 $this->state_code = $obj->state_code;
257 $this->state = $obj->state;
258
259 $this->country_id = $obj->country_id;
260 $this->country_code = $obj->country_code;
261 $this->country = $obj->country;
262 }
263 $this->db->free($resql);
264
265 return 1;
266 } else {
267 dol_print_error($this->db);
268 return -1;
269 }
270 }
271
279 public function delete($user = null, $notrigger = 0)
280 {
281 $error = 0;
282
283 $this->db->begin();
284
285 // Delete link between tag and bank account
286 /*
287 if (!$error) {
288 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
289 $sql .= " WHERE fk_account = ".((int) $this->id);
290
291 $resql = $this->db->query($sql);
292 if (!$resql) {
293 $error++;
294 $this->error = "Error ".$this->db->lasterror();
295 }
296 }
297 */
298
299 if (!$error) {
300 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
301 $sql .= " WHERE rowid = ".((int) $this->id);
302
303 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
304 $result = $this->db->query($sql);
305 if ($result) {
306 // Remove extrafields
307 /*
308 if (!$error) {
309 $result = $this->deleteExtraFields();
310 if ($result < 0) {
311 $error++;
312 dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
313 }
314 }*/
315 } else {
316 $error++;
317 $this->error = "Error ".$this->db->lasterror();
318 }
319 }
320
321 // Triggers
322 if (!$error && !$notrigger) {
323 // Call triggers
324 $result = $this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user);
325 if ($result < 0) {
326 $error++;
327 } //Do also here what you must do to rollback action if trigger fail
328 // End call triggers
329 }
330
331 if (!$error) {
332 $this->db->commit();
333 return 1;
334 } else {
335 $this->db->rollback();
336 return -1;
337 }
338 }
339
346 public function getRibLabel($displayriblabel = true)
347 {
348 $rib = '';
349
350 if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib) {
351 if ($this->label && $displayriblabel) {
352 $rib = $this->label." : ";
353 }
354
355 $rib .= $this->iban;
356 }
357
358 return $rib;
359 }
360
365 public function checkCountryBankAccount()
366 {
367 if (!empty($this->country_code)) {
368 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
369 $country_code_in_EEC = getCountriesInEEC();
370 return in_array($this->country_code, $country_code_in_EEC);
371 } else {
372 return false;
373 }
374 }
375}
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition propal.php:280
Class to manage bank accounts.
solde($option=0, $date_end='', $field='dateo')
Return current balance.
error()
Return error.
const TYPE_CURRENT
Current account.
Class to manage Dolibarr database access.
Class to manage bank accounts description of users.
update($user, $notrigger=0)
Update bank account.
getRibLabel($displayriblabel=true)
Return RIB.
__construct(DoliDB $db)
Constructor.
checkCountryBankAccount()
Return if a country of userBank is inside the EEC (European Economic Community)
create($user, $notrigger=0)
Create bank information record.
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='gmt')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
div refaddress div address
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.