dolibarr  17.0.4
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 
41  public $element = 'societe_rib';
42 
46  public $table_element = 'societe_rib';
47 
49  public $default_rib;
50 
56  public $frstrecur;
57 
58  public $rum;
59  public $date_rum;
60 
61  public $stripe_card_ref; // ID of BAN into an external payment system
62  public $stripe_account; // Account of the external payment system
63 
69  public $datec;
70 
76  public $datem;
77 
83  const TRIGGER_PREFIX = 'COMPANY_RIB';
84 
85 
91  public function __construct(DoliDB $db)
92  {
93  $this->db = $db;
94 
95  $this->socid = 0;
96  $this->solde = 0;
97  $this->error_number = 0;
98  $this->default_rib = 0;
99  }
100 
101 
109  public function create(User $user = null, $notrigger = 0)
110  {
111  $now = dol_now();
112 
113  $error = 0;
114 
115  // Check paramaters
116  if (empty($this->socid)) {
117  $this->error = 'BadValueForParameter';
118  $this->errors[] = $this->error;
119  return -1;
120  }
121 
122  // 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'.
123  // If we really want the new bank account to be the default, we must set it by calling setDefault() after creation.
124  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib where fk_soc = ".((int) $this->socid)." AND default_rib = 1 AND type = 'ban'";
125  $result = $this->db->query($sql);
126  if ($result) {
127  $numrows = $this->db->num_rows($result);
128  if ($this->default_rib && $numrows > 0) {
129  $this->default_rib = 0;
130  }
131  if (empty($this->default_rib) && $numrows == 0) {
132  $this->default_rib = 1;
133  }
134  }
135 
136 
137  $this->db->begin();
138 
139  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_rib (fk_soc, type, datec)";
140  $sql .= " VALUES (".((int) $this->socid).", 'ban', '".$this->db->idate($now)."')";
141  $resql = $this->db->query($sql);
142  if ($resql) {
143  if ($this->db->affected_rows($resql)) {
144  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe_rib");
145 
146  if (!$notrigger) {
147  // Call trigger
148  $result = $this->call_trigger('COMPANY_RIB_CREATE', $user);
149  if ($result < 0) {
150  $error++;
151  }
152  // End call triggers
153  }
154  }
155  } else {
156  $error++;
157  $this->error = $this->db->lasterror();
158  $this->errors[] = $this->error;
159  }
160 
161  if (!$error) {
162  $this->db->commit();
163  return $this->id;
164  } else {
165  $this->db->rollback();
166  return -1;
167  }
168  }
169 
177  public function update(User $user = null, $notrigger = 0)
178  {
179  global $conf, $langs;
180 
181  $error = 0;
182 
183  if (!$this->id) {
184  return -1;
185  }
186 
187  if (dol_strlen($this->domiciliation) > 255) {
188  $this->domiciliation = dol_trunc($this->domiciliation, 254, 'right', 'UTF-8', 1);
189  }
190  if (dol_strlen($this->owner_address) > 255) {
191  $this->owner_address = dol_trunc($this->owner_address, 254, 'right', 'UTF-8', 1);
192  }
193 
194  $this->db->begin();
195 
196  $sql = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET";
197  $sql .= " bank = '".$this->db->escape($this->bank)."'";
198  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
199  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
200  $sql .= ",number='".$this->db->escape($this->number)."'";
201  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
202  $sql .= ",bic='".$this->db->escape($this->bic)."'";
203  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
204  $sql .= ",domiciliation = '".$this->db->escape($this->domiciliation)."'";
205  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
206  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
207  $sql .= ",default_rib = ".((int) $this->default_rib);
208  if (!empty($conf->prelevement->enabled)) {
209  $sql .= ",frstrecur = '".$this->db->escape($this->frstrecur)."'";
210  $sql .= ",rum = '".$this->db->escape($this->rum)."'";
211  $sql .= ",date_rum = ".($this->date_rum ? "'".$this->db->idate($this->date_rum)."'" : "null");
212  }
213  if (trim($this->label) != '') {
214  $sql .= ",label = '".$this->db->escape($this->label)."'";
215  } else {
216  $sql .= ",label = NULL";
217  }
218  $sql .= ",stripe_card_ref = '".$this->db->escape($this->stripe_card_ref)."'";
219  $sql .= ",stripe_account = '".$this->db->escape($this->stripe_account)."'";
220  $sql .= " WHERE rowid = ".((int) $this->id);
221 
222  $result = $this->db->query($sql);
223  if ($result) {
224  if (!$notrigger) {
225  // Call trigger
226  $result = $this->call_trigger('COMPANY_RIB_MODIFY', $user);
227  if ($result < 0) {
228  $error++;
229  }
230  // End call triggers
231  }
232  } else {
233  $error++;
234  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
235  $this->error = $langs->trans('ErrorDuplicateField');
236  } else {
237  $this->error = $this->db->lasterror();
238  }
239  $this->errors[] = $this->error;
240  }
241 
242  if (!$error) {
243  $this->db->commit();
244  return 1;
245  } else {
246  $this->db->rollback();
247  return -1;
248  }
249  }
250 
260  public function fetch($id, $socid = 0, $default = 1, $type = 'ban')
261  {
262  if (empty($id) && empty($socid)) {
263  return -1;
264  }
265 
266  $sql = "SELECT rowid, type, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
267  $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur, date_rum,";
268  $sql .= " stripe_card_ref, stripe_account";
269  $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
270  if ($id) {
271  $sql .= " WHERE rowid = ".((int) $id);
272  } elseif ($socid > 0) {
273  $sql .= " WHERE fk_soc = ".((int) $socid);
274  if ($default > -1) {
275  $sql .= " AND default_rib = ".((int) $default);
276  }
277  if ($type) {
278  $sql .= " AND type = '".$this->db->escape($type)."'";
279  }
280  }
281 
282  $resql = $this->db->query($sql);
283  if ($resql) {
284  if ($this->db->num_rows($resql)) {
285  $obj = $this->db->fetch_object($resql);
286 
287  $this->ref = $obj->fk_soc.'-'.$obj->label; // Generate an artificial ref
288 
289  $this->id = $obj->rowid;
290  $this->type = $obj->type;
291  $this->socid = $obj->fk_soc;
292  $this->bank = $obj->bank;
293  $this->code_banque = $obj->code_banque;
294  $this->code_guichet = $obj->code_guichet;
295  $this->number = $obj->number;
296  $this->cle_rib = $obj->cle_rib;
297  $this->bic = $obj->bic;
298  $this->iban = $obj->iban;
299  $this->domiciliation = $obj->domiciliation;
300  $this->proprio = $obj->proprio;
301  $this->owner_address = $obj->owner_address;
302  $this->label = $obj->label;
303  $this->default_rib = $obj->default_rib;
304  $this->datec = $this->db->jdate($obj->datec);
305  $this->datem = $this->db->jdate($obj->datem);
306  $this->rum = $obj->rum;
307  $this->frstrecur = $obj->frstrecur;
308  $this->date_rum = $this->db->jdate($obj->date_rum);
309  $this->stripe_card_ref = $obj->stripe_card_ref;
310  $this->stripe_account = $obj->stripe_account;
311  }
312  $this->db->free($resql);
313 
314  return 1;
315  } else {
316  dol_print_error($this->db);
317  return -1;
318  }
319  }
320 
328  public function delete(User $user = null, $notrigger = 0)
329  {
330  $error = 0;
331 
332  dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
333 
334  $this->db->begin();
335 
336  if (!$error && !$notrigger) {
337  // Call trigger
338  $result = $this->call_trigger('COMPANY_RIB_DELETE', $user);
339  if ($result < 0) {
340  $error++;
341  }
342  // End call triggers
343  }
344 
345  if (!$error) {
346  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_rib";
347  $sql .= " WHERE rowid = ".((int) $this->id);
348 
349  if (!$this->db->query($sql)) {
350  $error++;
351  $this->errors[] = $this->db->lasterror();
352  }
353  }
354 
355  if (!$error) {
356  $this->db->commit();
357  return 1;
358  } else {
359  $this->db->rollback();
360  return -1 * $error;
361  }
362  }
363 
370  public function getRibLabel($displayriblabel = true)
371  {
372  $rib = '';
373 
374  if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib || $this->iban || $this->bic) {
375  if ($this->label && $displayriblabel) {
376  $rib = $this->label." : ";
377  }
378 
379  $rib .= $this->iban;
380  }
381 
382  return $rib;
383  }
384 
392  public function setAsDefault($rib = 0, $resetolddefaultfor = 'ban')
393  {
394  $sql1 = "SELECT rowid as id, fk_soc FROM ".MAIN_DB_PREFIX."societe_rib";
395  $sql1 .= " WHERE rowid = ".($rib ? $rib : $this->id);
396 
397  dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
398  $result1 = $this->db->query($sql1);
399  if ($result1) {
400  if ($this->db->num_rows($result1) == 0) {
401  return 0;
402  } else {
403  $obj = $this->db->fetch_object($result1);
404 
405  $this->db->begin();
406 
407  $sql2 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 0";
408  $sql2 .= " WHERE fk_soc = ".((int) $obj->fk_soc);
409  if ($resetolddefaultfor) {
410  $sql2 .= " AND type = '".$this->db->escape($resetolddefaultfor)."'";
411  }
412  $result2 = $this->db->query($sql2);
413 
414  $sql3 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 1";
415  $sql3 .= " WHERE rowid = ".((int) $obj->id);
416  $result3 = $this->db->query($sql3);
417 
418  if (!$result2 || !$result3) {
419  dol_print_error($this->db);
420  $this->db->rollback();
421  return -1;
422  } else {
423  $this->db->commit();
424  return 1;
425  }
426  }
427  } else {
428  dol_print_error($this->db);
429  return -1;
430  }
431  }
432 
440  public function initAsSpecimen()
441  {
442  $this->specimen = 1;
443  $this->ref = 'CBA';
444  $this->label = 'CustomerCorp Bank account';
445  $this->bank = 'CustomerCorp Bank';
446  $this->courant = Account::TYPE_CURRENT;
447  $this->clos = Account::STATUS_OPEN;
448  $this->code_banque = '123';
449  $this->code_guichet = '456';
450  $this->number = 'CUST12345';
451  $this->cle_rib = 50;
452  $this->bic = 'CC12';
453  $this->iban = 'FR999999999';
454  $this->domiciliation = 'Bank address of customer corp';
455  $this->proprio = 'Owner';
456  $this->owner_address = 'Owner address';
457  $this->country_id = 1;
458 
459  $this->rum = 'UMR-CU1212-0007-5-1475405262';
460  $this->date_rum = dol_now() - 10000;
461  $this->frstrecur = 'FRST';
462 
463  $this->socid = 1;
464  }
465 }
$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:47
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