dolibarr  x.y.z
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 
50  public $stripe_card_ref; // ID of BAN into an external payment system
51  public $stripe_account; // Account of the external payment system
52 
58  public $datec;
59 
65  public $datem;
66 
67 
73  public function __construct(DoliDB $db)
74  {
75  $this->db = $db;
76 
77  $this->socid = 0;
78  $this->solde = 0;
79  $this->error_number = 0;
80  $this->default_rib = 0;
81  }
82 
83 
91  public function create(User $user = null, $notrigger = 0)
92  {
93  $now = dol_now();
94 
95  $error = 0;
96 
97  // Check paramaters
98  if (empty($this->socid)) {
99  $this->error = 'BadValueForParameter';
100  return -1;
101  }
102 
103  // 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'.
104  // If we really want the new bank account to be the default, we must set it by calling setDefault() after creation.
105  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib where fk_soc = ".((int) $this->socid)." AND default_rib = 1 AND type = 'ban'";
106  $result = $this->db->query($sql);
107  if ($result) {
108  $numrows = $this->db->num_rows($result);
109  if ($this->default_rib && $numrows > 0) {
110  $this->default_rib = 0;
111  }
112  if (empty($this->default_rib) && $numrows == 0) {
113  $this->default_rib = 1;
114  }
115  }
116 
117  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_rib (fk_soc, type, datec)";
118  $sql .= " VALUES (".((int) $this->socid).", 'ban', '".$this->db->idate($now)."')";
119  $resql = $this->db->query($sql);
120  if ($resql) {
121  if ($this->db->affected_rows($resql)) {
122  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe_rib");
123 
124  if (!$notrigger) {
125  // Call trigger
126  $result = $this->call_trigger('COMPANY_RIB_CREATE', $user);
127  if ($result < 0) {
128  $error++;
129  }
130  // End call triggers
131 
132  if (!$error) {
133  return $this->id;
134  } else {
135  return 0;
136  }
137  } else {
138  return 1;
139  }
140  }
141  } else {
142  $this->error = $this->db->lasterror();
143  return 0;
144  }
145  }
146 
154  public function update(User $user = null, $notrigger = 0)
155  {
156  global $conf, $langs;
157 
158  $error = 0;
159 
160  if (!$this->id) {
161  return -1;
162  }
163 
164  if (dol_strlen($this->domiciliation) > 255) {
165  $this->domiciliation = dol_trunc($this->domiciliation, 254, 'right', 'UTF-8', 1);
166  }
167  if (dol_strlen($this->owner_address) > 255) {
168  $this->owner_address = dol_trunc($this->owner_address, 254, 'right', 'UTF-8', 1);
169  }
170 
171  $sql = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET";
172  $sql .= " bank = '".$this->db->escape($this->bank)."'";
173  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
174  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
175  $sql .= ",number='".$this->db->escape($this->number)."'";
176  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
177  $sql .= ",bic='".$this->db->escape($this->bic)."'";
178  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
179  $sql .= ",domiciliation = '".$this->db->escape($this->domiciliation)."'";
180  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
181  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
182  $sql .= ",default_rib = ".((int) $this->default_rib);
183  if (!empty($conf->prelevement->enabled)) {
184  $sql .= ",frstrecur = '".$this->db->escape($this->frstrecur)."'";
185  $sql .= ",rum = '".$this->db->escape($this->rum)."'";
186  $sql .= ",date_rum = ".($this->date_rum ? "'".$this->db->idate($this->date_rum)."'" : "null");
187  }
188  if (trim($this->label) != '') {
189  $sql .= ",label = '".$this->db->escape($this->label)."'";
190  } else {
191  $sql .= ",label = NULL";
192  }
193  $sql .= ",stripe_card_ref = '".$this->db->escape($this->stripe_card_ref)."'";
194  $sql .= ",stripe_account = '".$this->db->escape($this->stripe_account)."'";
195  $sql .= " WHERE rowid = ".((int) $this->id);
196 
197  $result = $this->db->query($sql);
198  if ($result) {
199  if (!$notrigger) {
200  // Call trigger
201  $result = $this->call_trigger('COMPANY_RIB_MODIFY', $user);
202  if ($result < 0) {
203  $error++;
204  }
205  // End call triggers
206  if (!$error) {
207  return 1;
208  } else {
209  return -1;
210  }
211  } else {
212  return 1;
213  }
214  } else {
215  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
216  $this->error = $langs->trans('ErrorDuplicateField');
217  } else {
218  $this->error = $this->db->lasterror();
219  }
220  return -1;
221  }
222  }
223 
233  public function fetch($id, $socid = 0, $default = 1, $type = 'ban')
234  {
235  if (empty($id) && empty($socid)) {
236  return -1;
237  }
238 
239  $sql = "SELECT rowid, type, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
240  $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur, date_rum,";
241  $sql .= " stripe_card_ref, stripe_account";
242  $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
243  if ($id) {
244  $sql .= " WHERE rowid = ".((int) $id);
245  } elseif ($socid > 0) {
246  $sql .= " WHERE fk_soc = ".((int) $socid);
247  if ($default > -1) {
248  $sql .= " AND default_rib = ".((int) $default);
249  }
250  if ($type) {
251  $sql .= " AND type = '".$this->db->escape($type)."'";
252  }
253  }
254 
255  $resql = $this->db->query($sql);
256  if ($resql) {
257  if ($this->db->num_rows($resql)) {
258  $obj = $this->db->fetch_object($resql);
259 
260  $this->ref = $obj->fk_soc.'-'.$obj->label; // Generate an artificial ref
261 
262  $this->id = $obj->rowid;
263  $this->type = $obj->type;
264  $this->socid = $obj->fk_soc;
265  $this->bank = $obj->bank;
266  $this->code_banque = $obj->code_banque;
267  $this->code_guichet = $obj->code_guichet;
268  $this->number = $obj->number;
269  $this->cle_rib = $obj->cle_rib;
270  $this->bic = $obj->bic;
271  $this->iban = $obj->iban;
272  $this->domiciliation = $obj->domiciliation;
273  $this->proprio = $obj->proprio;
274  $this->owner_address = $obj->owner_address;
275  $this->label = $obj->label;
276  $this->default_rib = $obj->default_rib;
277  $this->datec = $this->db->jdate($obj->datec);
278  $this->datem = $this->db->jdate($obj->datem);
279  $this->rum = $obj->rum;
280  $this->frstrecur = $obj->frstrecur;
281  $this->date_rum = $this->db->jdate($obj->date_rum);
282  $this->stripe_card_ref = $obj->stripe_card_ref;
283  $this->stripe_account = $obj->stripe_account;
284  }
285  $this->db->free($resql);
286 
287  return 1;
288  } else {
289  dol_print_error($this->db);
290  return -1;
291  }
292  }
293 
301  public function delete(User $user = null, $notrigger = 0)
302  {
303  $error = 0;
304 
305  dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
306 
307  $this->db->begin();
308 
309  if (!$error && !$notrigger) {
310  // Call trigger
311  $result = $this->call_trigger('COMPANY_RIB_DELETE', $user);
312  if ($result < 0) {
313  $error++;
314  }
315  // End call triggers
316  }
317 
318  if (!$error) {
319  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_rib";
320  $sql .= " WHERE rowid = ".((int) $this->id);
321 
322  if (!$this->db->query($sql)) {
323  $error++;
324  $this->errors[] = $this->db->lasterror();
325  }
326  }
327 
328  if (!$error) {
329  $this->db->commit();
330  return 1;
331  } else {
332  $this->db->rollback();
333  return -1 * $error;
334  }
335  }
336 
343  public function getRibLabel($displayriblabel = true)
344  {
345  $rib = '';
346 
347  if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib || $this->iban || $this->bic) {
348  if ($this->label && $displayriblabel) {
349  $rib = $this->label." : ";
350  }
351 
352  $rib .= $this->iban;
353  }
354 
355  return $rib;
356  }
357 
365  public function setAsDefault($rib = 0, $resetolddefaultfor = 'ban')
366  {
367  $sql1 = "SELECT rowid as id, fk_soc FROM ".MAIN_DB_PREFIX."societe_rib";
368  $sql1 .= " WHERE rowid = ".($rib ? $rib : $this->id);
369 
370  dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
371  $result1 = $this->db->query($sql1);
372  if ($result1) {
373  if ($this->db->num_rows($result1) == 0) {
374  return 0;
375  } else {
376  $obj = $this->db->fetch_object($result1);
377 
378  $this->db->begin();
379 
380  $sql2 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 0";
381  $sql2 .= " WHERE fk_soc = ".((int) $obj->fk_soc);
382  if ($resetolddefaultfor) {
383  $sql2 .= " AND type = '".$this->db->escape($resetolddefaultfor)."'";
384  }
385  $result2 = $this->db->query($sql2);
386 
387  $sql3 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 1";
388  $sql3 .= " WHERE rowid = ".((int) $obj->id);
389  $result3 = $this->db->query($sql3);
390 
391  if (!$result2 || !$result3) {
392  dol_print_error($this->db);
393  $this->db->rollback();
394  return -1;
395  } else {
396  $this->db->commit();
397  return 1;
398  }
399  }
400  } else {
401  dol_print_error($this->db);
402  return -1;
403  }
404  }
405 
413  public function initAsSpecimen()
414  {
415  $this->specimen = 1;
416  $this->ref = 'CBA';
417  $this->label = 'CustomerCorp Bank account';
418  $this->bank = 'CustomerCorp Bank';
419  $this->courant = Account::TYPE_CURRENT;
420  $this->clos = Account::STATUS_OPEN;
421  $this->code_banque = '123';
422  $this->code_guichet = '456';
423  $this->number = 'CUST12345';
424  $this->cle_rib = 50;
425  $this->bic = 'CC12';
426  $this->iban = 'FR999999999';
427  $this->domiciliation = 'Bank address of customer corp';
428  $this->proprio = 'Owner';
429  $this->owner_address = 'Owner address';
430  $this->country_id = 1;
431 
432  $this->rum = 'UMR-CU1212-0007-5-1475405262';
433  $this->date_rum = dol_now() - 10000;
434  $this->frstrecur = 'FRST';
435 
436  $this->socid = 1;
437  }
438 }
$object ref
Definition: info.php:78
Class to manage bank accounts.
solde($option=0, $date_end='', $field='dateo')
Return current sold.
error()
Return error.
const TYPE_CURRENT
Current account.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage bank accounts description of third parties.
setAsDefault($rib=0, $resetolddefaultfor='ban')
Set a BAN as Default.
getRibLabel($displayriblabel=true)
Return RIB.
update(User $user=null, $notrigger=0)
Update bank account.
initAsSpecimen()
Initialise an instance with random values.
create(User $user=null, $notrigger=0)
Create bank information record.
__construct(DoliDB $db)
Constructor.
fetch($id, $socid=0, $default=1, $type='ban')
Load record from database.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
Definition: user.class.php:45
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_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
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.
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:119
$conf db
API class for accounts.
Definition: inc.php:41