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