dolibarr  16.0.5
companybankaccount.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) 2016 Marcos García <marcosgdf@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
28 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
29 
30 
35 {
36  public $socid;
37 
38  public $default_rib;
39 
45  public $frstrecur;
46 
47  public $rum;
48  public $date_rum;
49 
55  public $datec;
56 
62  public $datem;
63 
64 
70  public function __construct(DoliDB $db)
71  {
72  $this->db = $db;
73 
74  $this->socid = 0;
75  $this->solde = 0;
76  $this->error_number = 0;
77  $this->default_rib = 0;
78  }
79 
80 
88  public function create(User $user = null, $notrigger = 0)
89  {
90  $now = dol_now();
91 
92  $error = 0;
93 
94  // Check paramaters
95  if (empty($this->socid)) {
96  $this->error = 'BadValueForParameter';
97  return -1;
98  }
99 
100  // Correct ->default_rib to not set the new account as default, if there is already 1. We want to be sure to have always 1 default for type = 'ban'.
101  // If we really want the new bank account to be the default, we must set it by calling setDefault() after creation.
102  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib where fk_soc = ".((int) $this->socid)." AND default_rib = 1 AND type = 'ban'";
103  $result = $this->db->query($sql);
104  if ($result) {
105  $numrows = $this->db->num_rows($result);
106  if ($this->default_rib && $numrows > 0) {
107  $this->default_rib = 0;
108  }
109  if (empty($this->default_rib) && $numrows == 0) {
110  $this->default_rib = 1;
111  }
112  }
113 
114  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_rib (fk_soc, type, datec)";
115  $sql .= " VALUES (".((int) $this->socid).", 'ban', '".$this->db->idate($now)."')";
116  $resql = $this->db->query($sql);
117  if ($resql) {
118  if ($this->db->affected_rows($resql)) {
119  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe_rib");
120 
121  if (!$notrigger) {
122  // Call trigger
123  $result = $this->call_trigger('COMPANY_RIB_CREATE', $user);
124  if ($result < 0) {
125  $error++;
126  }
127  // End call triggers
128 
129  if (!$error) {
130  return 1;
131  } else {
132  return 0;
133  }
134  } else {
135  return 1;
136  }
137  }
138  } else {
139  print $this->db->error();
140  return 0;
141  }
142  }
143 
151  public function update(User $user = null, $notrigger = 0)
152  {
153  global $conf;
154 
155  $error = 0;
156 
157  if (!$this->id) {
158  return -1;
159  }
160 
161  if (dol_strlen($this->domiciliation) > 255) {
162  $this->domiciliation = dol_trunc($this->domiciliation, 254, 'right', 'UTF-8', 1);
163  }
164  if (dol_strlen($this->owner_address) > 255) {
165  $this->owner_address = dol_trunc($this->owner_address, 254, 'right', 'UTF-8', 1);
166  }
167 
168  $sql = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET";
169  $sql .= " bank = '".$this->db->escape($this->bank)."'";
170  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
171  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
172  $sql .= ",number='".$this->db->escape($this->number)."'";
173  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
174  $sql .= ",bic='".$this->db->escape($this->bic)."'";
175  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
176  $sql .= ",domiciliation = '".$this->db->escape($this->domiciliation)."'";
177  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
178  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
179  $sql .= ",default_rib = ".((int) $this->default_rib);
180  if (!empty($conf->prelevement->enabled)) {
181  $sql .= ",frstrecur = '".$this->db->escape($this->frstrecur)."'";
182  $sql .= ",rum = '".$this->db->escape($this->rum)."'";
183  $sql .= ",date_rum = ".($this->date_rum ? "'".$this->db->idate($this->date_rum)."'" : "null");
184  }
185  if (trim($this->label) != '') {
186  $sql .= ",label = '".$this->db->escape($this->label)."'";
187  } else {
188  $sql .= ",label = NULL";
189  }
190  $sql .= " WHERE rowid = ".((int) $this->id);
191 
192  $result = $this->db->query($sql);
193  if ($result) {
194  if (!$notrigger) {
195  // Call trigger
196  $result = $this->call_trigger('COMPANY_RIB_MODIFY', $user);
197  if ($result < 0) {
198  $error++;
199  }
200  // End call triggers
201  if (!$error) {
202  return 1;
203  } else {
204  return -1;
205  }
206  } else {
207  return 1;
208  }
209  } else {
210  $this->error = $this->db->lasterror();
211  return -1;
212  }
213  }
214 
224  public function fetch($id, $socid = 0, $default = 1, $type = 'ban')
225  {
226  if (empty($id) && empty($socid)) {
227  return -1;
228  }
229 
230  $sql = "SELECT rowid, type, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
231  $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur, date_rum";
232  $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
233  if ($id) {
234  $sql .= " WHERE rowid = ".((int) $id);
235  } elseif ($socid > 0) {
236  $sql .= " WHERE fk_soc = ".((int) $socid);
237  if ($default > -1) {
238  $sql .= " AND default_rib = ".((int) $default);
239  }
240  if ($type) {
241  $sql .= " AND type = '".$this->db->escape($type)."'";
242  }
243  }
244 
245  $resql = $this->db->query($sql);
246  if ($resql) {
247  if ($this->db->num_rows($resql)) {
248  $obj = $this->db->fetch_object($resql);
249 
250  $this->ref = $obj->fk_soc.'-'.$obj->label; // Generate an artificial ref
251 
252  $this->id = $obj->rowid;
253  $this->type = $obj->type;
254  $this->socid = $obj->fk_soc;
255  $this->bank = $obj->bank;
256  $this->code_banque = $obj->code_banque;
257  $this->code_guichet = $obj->code_guichet;
258  $this->number = $obj->number;
259  $this->cle_rib = $obj->cle_rib;
260  $this->bic = $obj->bic;
261  $this->iban = $obj->iban;
262  $this->domiciliation = $obj->domiciliation;
263  $this->proprio = $obj->proprio;
264  $this->owner_address = $obj->owner_address;
265  $this->label = $obj->label;
266  $this->default_rib = $obj->default_rib;
267  $this->datec = $this->db->jdate($obj->datec);
268  $this->datem = $this->db->jdate($obj->datem);
269  $this->rum = $obj->rum;
270  $this->frstrecur = $obj->frstrecur;
271  $this->date_rum = $this->db->jdate($obj->date_rum);
272  }
273  $this->db->free($resql);
274 
275  return 1;
276  } else {
277  dol_print_error($this->db);
278  return -1;
279  }
280  }
281 
289  public function delete(User $user = null, $notrigger = 0)
290  {
291  $error = 0;
292 
293  dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
294 
295  $this->db->begin();
296 
297  if (!$error && !$notrigger) {
298  // Call trigger
299  $result = $this->call_trigger('COMPANY_RIB_DELETE', $user);
300  if ($result < 0) {
301  $error++;
302  }
303  // End call triggers
304  }
305 
306  if (!$error) {
307  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_rib";
308  $sql .= " WHERE rowid = ".((int) $this->id);
309 
310  if (!$this->db->query($sql)) {
311  $error++;
312  $this->errors[] = $this->db->lasterror();
313  }
314  }
315 
316  if (!$error) {
317  $this->db->commit();
318  return 1;
319  } else {
320  $this->db->rollback();
321  return -1 * $error;
322  }
323  }
324 
331  public function getRibLabel($displayriblabel = true)
332  {
333  $rib = '';
334 
335  if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib || $this->iban || $this->bic) {
336  if ($this->label && $displayriblabel) {
337  $rib = $this->label." : ";
338  }
339 
340  $rib .= (string) $this;
341  }
342 
343  return $rib;
344  }
345 
353  public function setAsDefault($rib = 0, $resetolddefaultfor = 'ban')
354  {
355  $sql1 = "SELECT rowid as id, fk_soc FROM ".MAIN_DB_PREFIX."societe_rib";
356  $sql1 .= " WHERE rowid = ".($rib ? $rib : $this->id);
357 
358  dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
359  $result1 = $this->db->query($sql1);
360  if ($result1) {
361  if ($this->db->num_rows($result1) == 0) {
362  return 0;
363  } else {
364  $obj = $this->db->fetch_object($result1);
365 
366  $this->db->begin();
367 
368  $sql2 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 0";
369  $sql2 .= " WHERE fk_soc = ".((int) $obj->fk_soc);
370  if ($resetolddefaultfor) {
371  $sql2 .= " AND type = '".$this->db->escape($resetolddefaultfor)."'";
372  }
373  $result2 = $this->db->query($sql2);
374 
375  $sql3 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 1";
376  $sql3 .= " WHERE rowid = ".((int) $obj->id);
377  $result3 = $this->db->query($sql3);
378 
379  if (!$result2 || !$result3) {
380  dol_print_error($this->db);
381  $this->db->rollback();
382  return -1;
383  } else {
384  $this->db->commit();
385  return 1;
386  }
387  }
388  } else {
389  dol_print_error($this->db);
390  return -1;
391  }
392  }
393 
401  public function initAsSpecimen()
402  {
403  $this->specimen = 1;
404  $this->ref = 'CBA';
405  $this->label = 'CustomerCorp Bank account';
406  $this->bank = 'CustomerCorp Bank';
407  $this->courant = Account::TYPE_CURRENT;
408  $this->clos = Account::STATUS_OPEN;
409  $this->code_banque = '123';
410  $this->code_guichet = '456';
411  $this->number = 'CUST12345';
412  $this->cle_rib = 50;
413  $this->bic = 'CC12';
414  $this->iban = 'FR999999999';
415  $this->domiciliation = 'Bank address of customer corp';
416  $this->proprio = 'Owner';
417  $this->owner_address = 'Owner address';
418  $this->country_id = 1;
419 
420  $this->rum = 'UMR-CU1212-0007-5-1475405262';
421  $this->date_rum = dol_now() - 10000;
422  $this->frstrecur = 'FRST';
423 
424  $this->socid = 1;
425  }
426 }
Account\TYPE_CURRENT
const TYPE_CURRENT
Current account.
Definition: account.class.php:342
db
$conf db
API class for accounts.
Definition: inc.php:41
CompanyBankAccount\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: companybankaccount.class.php:401
dol_trunc
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
Definition: functions.lib.php:3805
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
ref
$object ref
Definition: info.php:77
CompanyBankAccount\create
create(User $user=null, $notrigger=0)
Create bank information record.
Definition: companybankaccount.class.php:88
CompanyBankAccount
Class to manage bank accounts description of third parties.
Definition: companybankaccount.class.php:34
CompanyBankAccount\setAsDefault
setAsDefault($rib=0, $resetolddefaultfor='ban')
Set a BAN as Default.
Definition: companybankaccount.class.php:353
CompanyBankAccount\update
update(User $user=null, $notrigger=0)
Update bank account.
Definition: companybankaccount.class.php:151
CompanyBankAccount\__construct
__construct(DoliDB $db)
Constructor.
Definition: companybankaccount.class.php:70
Account\error
error()
Return error.
Definition: account.class.php:1224
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
CompanyBankAccount\fetch
fetch($id, $socid=0, $default=1, $type='ban')
Load record from database.
Definition: companybankaccount.class.php:224
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Account\solde
solde($option=0, $date_end='', $field='dateo')
Return current sold.
Definition: account.class.php:1237
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
Account
Class to manage bank accounts.
Definition: account.class.php:38
CompanyBankAccount\getRibLabel
getRibLabel($displayriblabel=true)
Return RIB.
Definition: companybankaccount.class.php:331