dolibarr  13.0.2
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  public $frstrecur;
40  public $rum;
41  public $date_rum;
42 
48  public $datec;
49 
55  public $datem;
56 
57 
63  public function __construct(DoliDB $db)
64  {
65  $this->db = $db;
66 
67  $this->socid = 0;
68  $this->solde = 0;
69  $this->error_number = 0;
70  $this->default_rib = 0;
71  }
72 
73 
81  public function create(User $user = null, $notrigger = 0)
82  {
83  $now = dol_now();
84  $error = 0;
85  // Correct default_rib to be sure to have always one default
86  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib where fk_soc = ".$this->socid." AND default_rib = 1 AND type = 'ban'";
87  $result = $this->db->query($sql);
88  if ($result)
89  {
90  $numrows = $this->db->num_rows($result);
91  if ($this->default_rib && $numrows > 0) $this->default_rib = 0;
92  if (empty($this->default_rib) && $numrows == 0) $this->default_rib = 1;
93  }
94 
95  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_rib (fk_soc, type, datec)";
96  $sql .= " VALUES (".$this->socid.", 'ban', '".$this->db->idate($now)."')";
97  $resql = $this->db->query($sql);
98  if ($resql)
99  {
100  if ($this->db->affected_rows($resql))
101  {
102  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe_rib");
103 
104  if (!$notrigger)
105  {
106  // Call trigger
107  $result = $this->call_trigger('COMPANY_RIB_CREATE', $user);
108  if ($result < 0) $error++;
109  // End call triggers
110 
111  if (!$error)
112  {
113  return 1;
114  } else {
115  return 0;
116  }
117  } else {
118  return 1;
119  }
120  }
121  } else {
122  print $this->db->error();
123  return 0;
124  }
125  }
126 
134  public function update(User $user = null, $notrigger = 0)
135  {
136  global $conf;
137  $error = 0;
138 
139  if (!$this->id) return -1;
140 
141  if (dol_strlen($this->domiciliation) > 255) $this->domiciliation = dol_trunc($this->domiciliation, 254, 'right', 'UTF-8', 1);
142  if (dol_strlen($this->owner_address) > 255) $this->owner_address = dol_trunc($this->owner_address, 254, 'right', 'UTF-8', 1);
143 
144  $sql = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET";
145  $sql .= " bank = '".$this->db->escape($this->bank)."'";
146  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
147  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
148  $sql .= ",number='".$this->db->escape($this->number)."'";
149  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
150  $sql .= ",bic='".$this->db->escape($this->bic)."'";
151  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
152  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
153  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
154  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
155  $sql .= ",default_rib = ".$this->default_rib;
156  if ($conf->prelevement->enabled)
157  {
158  $sql .= ",frstrecur = '".$this->db->escape($this->frstrecur)."'";
159  $sql .= ",rum = '".$this->db->escape($this->rum)."'";
160  $sql .= ",date_rum = ".($this->date_rum ? "'".$this->db->idate($this->date_rum)."'" : "null");
161  }
162  if (trim($this->label) != '')
163  $sql .= ",label = '".$this->db->escape($this->label)."'";
164  else $sql .= ",label = NULL";
165  $sql .= " WHERE rowid = ".$this->id;
166 
167  $result = $this->db->query($sql);
168  if ($result)
169  {
170  if (!$notrigger)
171  {
172  // Call trigger
173  $result = $this->call_trigger('COMPANY_RIB_MODIFY', $user);
174  if ($result < 0) $error++;
175  // End call triggers
176  if (!$error)
177  {
178  return 1;
179  } else {
180  return -1;
181  }
182  } else {
183  return 1;
184  }
185  } else {
186  dol_print_error($this->db);
187  return -1;
188  }
189  }
190 
200  public function fetch($id, $socid = 0, $default = 1, $type = 'ban')
201  {
202  if (empty($id) && empty($socid)) return -1;
203 
204  $sql = "SELECT rowid, type, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
205  $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur, date_rum";
206  $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
207  if ($id) $sql .= " WHERE rowid = ".$id;
208  if ($socid)
209  {
210  $sql .= " WHERE fk_soc = ".$socid;
211  if ($default > -1) $sql .= " AND default_rib = ".$this->db->escape($default);
212  if ($type) $sql .= " AND type ='".$this->db->escape($type)."'";
213  }
214 
215  $resql = $this->db->query($sql);
216  if ($resql)
217  {
218  if ($this->db->num_rows($resql))
219  {
220  $obj = $this->db->fetch_object($resql);
221 
222  $this->ref = $obj->fk_soc.'-'.$obj->label; // Generate an artificial ref
223 
224  $this->id = $obj->rowid;
225  $this->type = $obj->type;
226  $this->socid = $obj->fk_soc;
227  $this->bank = $obj->bank;
228  $this->code_banque = $obj->code_banque;
229  $this->code_guichet = $obj->code_guichet;
230  $this->number = $obj->number;
231  $this->cle_rib = $obj->cle_rib;
232  $this->bic = $obj->bic;
233  $this->iban = $obj->iban;
234  $this->domiciliation = $obj->domiciliation;
235  $this->proprio = $obj->proprio;
236  $this->owner_address = $obj->owner_address;
237  $this->label = $obj->label;
238  $this->default_rib = $obj->default_rib;
239  $this->datec = $this->db->jdate($obj->datec);
240  $this->datem = $this->db->jdate($obj->datem);
241  $this->rum = $obj->rum;
242  $this->frstrecur = $obj->frstrecur;
243  $this->date_rum = $this->db->jdate($obj->date_rum);
244  }
245  $this->db->free($resql);
246 
247  return 1;
248  } else {
249  dol_print_error($this->db);
250  return -1;
251  }
252  }
253 
261  public function delete(User $user = null, $notrigger = 0)
262  {
263  global $conf;
264 
265  $error = 0;
266 
267  dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
268 
269  $this->db->begin();
270 
271  if (!$error && !$notrigger)
272  {
273  // Call trigger
274  $result = $this->call_trigger('COMPANY_RIB_DELETE', $user);
275  if ($result < 0) $error++;
276  // End call triggers
277  }
278 
279  if (!$error)
280  {
281  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_rib";
282  $sql .= " WHERE rowid = ".$this->id;
283 
284  if (!$this->db->query($sql))
285  {
286  $error++;
287  $this->errors[] = $this->db->lasterror();
288  }
289  }
290 
291  if (!$error)
292  {
293  $this->db->commit();
294  return 1;
295  } else {
296  $this->db->rollback();
297  return -1 * $error;
298  }
299  }
300 
307  public function getRibLabel($displayriblabel = true)
308  {
309  $rib = '';
310 
311  if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib || $this->iban || $this->bic) {
312  if ($this->label && $displayriblabel) {
313  $rib = $this->label." : ";
314  }
315 
316  $rib .= (string) $this;
317  }
318 
319  return $rib;
320  }
321 
328  public function setAsDefault($rib = 0)
329  {
330  $sql1 = "SELECT rowid as id, fk_soc FROM ".MAIN_DB_PREFIX."societe_rib";
331  $sql1 .= " WHERE rowid = ".($rib ? $rib : $this->id);
332 
333  dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
334  $result1 = $this->db->query($sql1);
335  if ($result1)
336  {
337  if ($this->db->num_rows($result1) == 0)
338  {
339  return 0;
340  } else {
341  $obj = $this->db->fetch_object($result1);
342 
343  $this->db->begin();
344 
345  $sql2 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 0";
346  $sql2 .= " WHERE type = 'ban' AND fk_soc = ".$obj->fk_soc;
347  dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
348  $result2 = $this->db->query($sql2);
349 
350  $sql3 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 1";
351  $sql3 .= " WHERE rowid = ".$obj->id;
352  dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
353  $result3 = $this->db->query($sql3);
354 
355  if (!$result2 || !$result3)
356  {
357  dol_print_error($this->db);
358  $this->db->rollback();
359  return -1;
360  } else {
361  $this->db->commit();
362  return 1;
363  }
364  }
365  } else {
366  dol_print_error($this->db);
367  return -1;
368  }
369  }
370 
378  public function initAsSpecimen()
379  {
380  $this->specimen = 1;
381  $this->ref = 'CBA';
382  $this->label = 'CustomerCorp Bank account';
383  $this->bank = 'CustomerCorp Bank';
384  $this->courant = Account::TYPE_CURRENT;
385  $this->clos = Account::STATUS_OPEN;
386  $this->code_banque = '123';
387  $this->code_guichet = '456';
388  $this->number = 'CUST12345';
389  $this->cle_rib = 50;
390  $this->bic = 'CC12';
391  $this->iban = 'FR999999999';
392  $this->domiciliation = 'Bank address of customer corp';
393  $this->proprio = 'Owner';
394  $this->owner_address = 'Owner address';
395  $this->country_id = 1;
396 
397  $this->rum = 'UMR-CU1212-0007-5-1475405262';
398  $this->date_rum = dol_now() - 10000;
399  $this->frstrecur = 'FRST';
400 
401  $this->socid = 0;
402  }
403 }
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.
const TYPE_CURRENT
Current account.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage bank accounts.
update(User $user=null, $notrigger=0)
Update bank account.
setAsDefault($rib=0)
Set a BAN as Default.
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.
getRibLabel($displayriblabel=true)
Return RIB.
print
Draft customers invoices.
Definition: index.php:89
call_trigger($triggerName, $user)
Call trigger based on this instance.
solde($option=0)
Return current sold.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
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:105