dolibarr  16.0.1
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  }
236  if ($socid) {
237  $sql .= " WHERE fk_soc = ".((int) $socid);
238  if ($default > -1) {
239  $sql .= " AND default_rib = ".((int) $default);
240  }
241  if ($type) {
242  $sql .= " AND type = '".$this->db->escape($type)."'";
243  }
244  }
245 
246  $resql = $this->db->query($sql);
247  if ($resql) {
248  if ($this->db->num_rows($resql)) {
249  $obj = $this->db->fetch_object($resql);
250 
251  $this->ref = $obj->fk_soc.'-'.$obj->label; // Generate an artificial ref
252 
253  $this->id = $obj->rowid;
254  $this->type = $obj->type;
255  $this->socid = $obj->fk_soc;
256  $this->bank = $obj->bank;
257  $this->code_banque = $obj->code_banque;
258  $this->code_guichet = $obj->code_guichet;
259  $this->number = $obj->number;
260  $this->cle_rib = $obj->cle_rib;
261  $this->bic = $obj->bic;
262  $this->iban = $obj->iban;
263  $this->domiciliation = $obj->domiciliation;
264  $this->proprio = $obj->proprio;
265  $this->owner_address = $obj->owner_address;
266  $this->label = $obj->label;
267  $this->default_rib = $obj->default_rib;
268  $this->datec = $this->db->jdate($obj->datec);
269  $this->datem = $this->db->jdate($obj->datem);
270  $this->rum = $obj->rum;
271  $this->frstrecur = $obj->frstrecur;
272  $this->date_rum = $this->db->jdate($obj->date_rum);
273  }
274  $this->db->free($resql);
275 
276  return 1;
277  } else {
278  dol_print_error($this->db);
279  return -1;
280  }
281  }
282 
290  public function delete(User $user = null, $notrigger = 0)
291  {
292  $error = 0;
293 
294  dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
295 
296  $this->db->begin();
297 
298  if (!$error && !$notrigger) {
299  // Call trigger
300  $result = $this->call_trigger('COMPANY_RIB_DELETE', $user);
301  if ($result < 0) {
302  $error++;
303  }
304  // End call triggers
305  }
306 
307  if (!$error) {
308  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_rib";
309  $sql .= " WHERE rowid = ".((int) $this->id);
310 
311  if (!$this->db->query($sql)) {
312  $error++;
313  $this->errors[] = $this->db->lasterror();
314  }
315  }
316 
317  if (!$error) {
318  $this->db->commit();
319  return 1;
320  } else {
321  $this->db->rollback();
322  return -1 * $error;
323  }
324  }
325 
332  public function getRibLabel($displayriblabel = true)
333  {
334  $rib = '';
335 
336  if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib || $this->iban || $this->bic) {
337  if ($this->label && $displayriblabel) {
338  $rib = $this->label." : ";
339  }
340 
341  $rib .= (string) $this;
342  }
343 
344  return $rib;
345  }
346 
354  public function setAsDefault($rib = 0, $resetolddefaultfor = 'ban')
355  {
356  $sql1 = "SELECT rowid as id, fk_soc FROM ".MAIN_DB_PREFIX."societe_rib";
357  $sql1 .= " WHERE rowid = ".($rib ? $rib : $this->id);
358 
359  dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
360  $result1 = $this->db->query($sql1);
361  if ($result1) {
362  if ($this->db->num_rows($result1) == 0) {
363  return 0;
364  } else {
365  $obj = $this->db->fetch_object($result1);
366 
367  $this->db->begin();
368 
369  $sql2 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 0";
370  $sql2 .= " WHERE fk_soc = ".((int) $obj->fk_soc);
371  if ($resetolddefaultfor) {
372  $sql2 .= " AND type = '".$this->db->escape($resetolddefaultfor)."'";
373  }
374  $result2 = $this->db->query($sql2);
375 
376  $sql3 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 1";
377  $sql3 .= " WHERE rowid = ".((int) $obj->id);
378  $result3 = $this->db->query($sql3);
379 
380  if (!$result2 || !$result3) {
381  dol_print_error($this->db);
382  $this->db->rollback();
383  return -1;
384  } else {
385  $this->db->commit();
386  return 1;
387  }
388  }
389  } else {
390  dol_print_error($this->db);
391  return -1;
392  }
393  }
394 
402  public function initAsSpecimen()
403  {
404  $this->specimen = 1;
405  $this->ref = 'CBA';
406  $this->label = 'CustomerCorp Bank account';
407  $this->bank = 'CustomerCorp Bank';
408  $this->courant = Account::TYPE_CURRENT;
409  $this->clos = Account::STATUS_OPEN;
410  $this->code_banque = '123';
411  $this->code_guichet = '456';
412  $this->number = 'CUST12345';
413  $this->cle_rib = 50;
414  $this->bic = 'CC12';
415  $this->iban = 'FR999999999';
416  $this->domiciliation = 'Bank address of customer corp';
417  $this->proprio = 'Owner';
418  $this->owner_address = 'Owner address';
419  $this->country_id = 1;
420 
421  $this->rum = 'UMR-CU1212-0007-5-1475405262';
422  $this->date_rum = dol_now() - 10000;
423  $this->frstrecur = 'FRST';
424 
425  $this->socid = 1;
426  }
427 }
$conf db
API class for accounts.
Definition: inc.php:41
setAsDefault($rib=0, $resetolddefaultfor= 'ban')
Set a BAN as Default.
solde($option=0, $date_end= '', $field= 'dateo')
Return current sold.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
Class to manage bank accounts description of third parties.
initAsSpecimen()
Initialise an instance with random values.
error()
Return error.
const TYPE_CURRENT
Current account.
Class to manage bank accounts.
update(User $user=null, $notrigger=0)
Update bank account.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
fetch($id, $socid=0, $default=1, $type= 'ban')
Load record from database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
__construct(DoliDB $db)
Constructor.
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
getRibLabel($displayriblabel=true)
Return RIB.
call_trigger($triggerName, $user)
Call trigger based on this instance.
$object ref
Definition: info.php:77
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;…&#39; if string larger than length. ...
create(User $user=null, $notrigger=0)
Create bank information record.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119