dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
20  */
21 
28 require_once DOL_DOCUMENT_ROOT .'/compta/bank/class/account.class.php';
29 
30 
35 {
36  var $socid;
37 
38  var $default_rib;
39  var $frstrecur;
40  var $rum;
41  var $date_rum;
42 
43  var $datec;
44  var $datem;
45 
46 
52  public function __construct(DoliDB $db)
53  {
54  $this->db = $db;
55 
56  $this->socid = 0;
57  $this->solde = 0;
58  $this->error_number = 0;
59  $this->default_rib = 0;
60  }
61 
62 
70  function create(User $user = null, $notrigger=0)
71  {
72  $now = dol_now();
73  $error = 0;
74  // Correct default_rib to be sure to have always one default
75  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib where fk_soc = ".$this->socid." AND default_rib = 1 AND type = 'ban'";
76  $result = $this->db->query($sql);
77  if ($result)
78  {
79  $numrows=$this->db->num_rows($result);
80  if ($this->default_rib && $numrows > 0) $this->default_rib = 0;
81  if (empty($this->default_rib) && $numrows == 0) $this->default_rib = 1;
82  }
83 
84  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_rib (fk_soc, type, datec)";
85  $sql.= " VALUES (".$this->socid.", 'ban', '".$this->db->idate($now)."')";
86  $resql=$this->db->query($sql);
87  if ($resql)
88  {
89  if ($this->db->affected_rows($resql))
90  {
91  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe_rib");
92 
93  if (! $notrigger)
94  {
95  // Call trigger
96  $result=$this->call_trigger('COMPANY_RIB_CREATE',$user);
97  if ($result < 0) $error++;
98  // End call triggers
99 
100  if (! $error)
101  {
102  return 1;
103  }
104  else
105  {
106  return 0;
107  }
108  }
109  else
110  {
111  return 1;
112  }
113  }
114  }
115  else
116  {
117  print $this->db->error();
118  return 0;
119  }
120  }
121 
129  function update(User $user = null, $notrigger = 0)
130  {
131  global $conf;
132  $error = 0;
133 
134  if (! $this->id) return -1;
135 
136  if (dol_strlen($this->domiciliation) > 255) $this->domiciliation = dol_trunc($this->domiciliation, 254, 'right', 'UTF-8', 1);
137  if (dol_strlen($this->owner_address) > 255) $this->owner_address = dol_trunc($this->owner_address, 254, 'right', 'UTF-8', 1);
138 
139  $sql = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET";
140  $sql.= " bank = '" .$this->db->escape($this->bank)."'";
141  $sql.= ",code_banque='".$this->db->escape($this->code_banque)."'";
142  $sql.= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
143  $sql.= ",number='".$this->db->escape($this->number)."'";
144  $sql.= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
145  $sql.= ",bic='".$this->db->escape($this->bic)."'";
146  $sql.= ",iban_prefix = '".$this->db->escape($this->iban)."'";
147  $sql.= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
148  $sql.= ",proprio = '".$this->db->escape($this->proprio)."'";
149  $sql.= ",owner_address = '".$this->db->escape($this->owner_address)."'";
150  $sql.= ",default_rib = ".$this->default_rib;
151  if ($conf->prelevement->enabled)
152  {
153  $sql.= ",frstrecur = '".$this->db->escape($this->frstrecur)."'";
154  $sql.= ",rum = '".$this->db->escape($this->rum)."'";
155  $sql.= ",date_rum = ".($this->date_rum ? "'".$this->db->idate($this->date_rum)."'" : "null");
156  }
157  if (trim($this->label) != '')
158  $sql.= ",label = '".$this->db->escape($this->label)."'";
159  else
160  $sql.= ",label = NULL";
161  $sql.= " WHERE rowid = ".$this->id;
162 
163  $result = $this->db->query($sql);
164  if ($result)
165  {
166 
167 
168  if (! $notrigger)
169  {
170  // Call trigger
171  $result=$this->call_trigger('COMPANY_RIB_MODIFY',$user);
172  if ($result < 0) $error++;
173  // End call triggers
174  if(! $error )
175  {
176  return 1;
177  }
178  else
179  {
180  return -1;
181  }
182  }
183  else
184  {
185  return 1;
186  }
187  }
188  else
189  {
190  dol_print_error($this->db);
191  return -1;
192  }
193  }
194 
204  function fetch($id, $socid=0, $default=1, $type='ban')
205  {
206  if (empty($id) && empty($socid)) return -1;
207 
208  $sql = "SELECT rowid, type, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
209  $sql.= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
210  $sql.= " FROM ".MAIN_DB_PREFIX."societe_rib";
211  if ($id) $sql.= " WHERE rowid = ".$id;
212  if ($socid)
213  {
214  $sql.= " WHERE fk_soc = ".$socid;
215  if ($default > -1) $sql.=" AND default_rib = ".$this->db->escape($default);
216  if ($type) $sql.= " AND type ='".$this->db->escape($type)."'";
217  }
218 
219  $resql = $this->db->query($sql);
220  if ($resql)
221  {
222  if ($this->db->num_rows($resql))
223  {
224  $obj = $this->db->fetch_object($resql);
225 
226  $this->ref = $obj->fk_soc.'-'.$obj->label; // Generate an artificial ref
227 
228  $this->id = $obj->rowid;
229  $this->type = $obj->type;
230  $this->socid = $obj->fk_soc;
231  $this->bank = $obj->bank;
232  $this->code_banque = $obj->code_banque;
233  $this->code_guichet = $obj->code_guichet;
234  $this->number = $obj->number;
235  $this->cle_rib = $obj->cle_rib;
236  $this->bic = $obj->bic;
237  $this->iban = $obj->iban;
238  $this->domiciliation = $obj->domiciliation;
239  $this->proprio = $obj->proprio;
240  $this->owner_address = $obj->owner_address;
241  $this->label = $obj->label;
242  $this->default_rib = $obj->default_rib;
243  $this->datec = $this->db->jdate($obj->datec);
244  $this->datem = $this->db->jdate($obj->datem);
245  $this->rum = $obj->rum;
246  $this->frstrecur = $obj->frstrecur;
247  }
248  $this->db->free($resql);
249 
250  return 1;
251  }
252  else
253  {
254  dol_print_error($this->db);
255  return -1;
256  }
257  }
258 
266  function delete(User $user = null, $notrigger=0)
267  {
268  global $conf;
269 
270  $error = 0;
271 
272  dol_syslog(get_class($this) . "::delete ".$this->id, LOG_DEBUG);
273 
274  $this->db->begin();
275 
276  if (! $error && ! $notrigger)
277  {
278  // Call trigger
279  $result=$this->call_trigger('COMPANY_RIB_DELETE',$user);
280  if ($result < 0) $error++;
281  // End call triggers
282  }
283 
284  if (! $error)
285  {
286  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "societe_rib";
287  $sql .= " WHERE rowid = " . $this->id;
288 
289  if (! $this->db->query($sql))
290  {
291  $error++;
292  $this->errors[]=$this->db->lasterror();
293  }
294  }
295 
296  if (! $error)
297  {
298  $this->db->commit();
299  return 1;
300  }
301  else
302  {
303  $this->db->rollback();
304  return -1*$error;
305  }
306  }
307 
314  public function getRibLabel($displayriblabel = true)
315  {
316  $rib = '';
317 
318  if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib || $this->iban || $this->bic ) {
319 
320  if ($this->label && $displayriblabel) {
321  $rib = $this->label." : ";
322  }
323 
324  $rib .= (string) $this;
325  }
326 
327  return $rib;
328  }
329 
336  function setAsDefault($rib=0)
337  {
338  $sql1 = "SELECT rowid as id, fk_soc FROM ".MAIN_DB_PREFIX."societe_rib";
339  $sql1.= " WHERE rowid = ".($rib?$rib:$this->id);
340 
341  dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
342  $result1 = $this->db->query($sql1);
343  if ($result1)
344  {
345  if ($this->db->num_rows($result1) == 0)
346  {
347  return 0;
348  }
349  else
350  {
351  $obj = $this->db->fetch_object($result1);
352 
353  $this->db->begin();
354 
355  $sql2 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 0";
356  $sql2.= " WHERE type = 'ban' AND fk_soc = ".$obj->fk_soc;
357  dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
358  $result2 = $this->db->query($sql2);
359 
360  $sql3 = "UPDATE ".MAIN_DB_PREFIX."societe_rib SET default_rib = 1";
361  $sql3.= " WHERE rowid = ".$obj->id;
362  dol_syslog(get_class($this).'::setAsDefault', LOG_DEBUG);
363  $result3 = $this->db->query($sql3);
364 
365  if (!$result2 || !$result3)
366  {
367  dol_print_error($this->db);
368  $this->db->rollback();
369  return -1;
370  }
371  else
372  {
373  $this->db->commit();
374  return 1;
375  }
376  }
377  }
378  else
379  {
380  dol_print_error($this->db);
381  return -1;
382  }
383  }
384 
392  function initAsSpecimen()
393  {
394  $this->specimen = 1;
395  $this->ref = 'CBA';
396  $this->label = 'CustomerCorp Bank account';
397  $this->bank = 'CustomerCorp Bank';
398  $this->courant = Account::TYPE_CURRENT;
399  $this->clos = Account::STATUS_OPEN;
400  $this->code_banque = '123';
401  $this->code_guichet = '456';
402  $this->number = 'CUST12345';
403  $this->cle_rib = 50;
404  $this->bic = 'CC12';
405  $this->iban = 'FR999999999';
406  $this->domiciliation = 'Bank address of customer corp';
407  $this->proprio = 'Owner';
408  $this->owner_address = 'Owner address';
409  $this->country_id = 1;
410 
411  $this->rum = 'UMR-CU1212-0007-5-1475405262';
412  $this->date_rum =dol_now() - 10000;
413  $this->frstrecur = 'FRST';
414 
415  $this->socid = 0;
416  }
417 }
418 
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
print
Draft customers invoices.
Definition: index.php:91
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.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
fetch($id, $socid=0, $default=1, $type='ban')
Load record from database.
Class to manage Dolibarr users.
Definition: user.class.php:41
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.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class to manage bank accounts.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
type
Definition: viewcat.php:284
update(User $user=null, $notrigger=0)
Update bank account.
setAsDefault($rib=0)
Set a BAN as Default.
__construct(DoliDB $db)
Constructor.
dol_now($mode='gmt')
Return date for now.
getRibLabel($displayriblabel=true)
Return RIB.
solde($option=0)
Return current sold.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
create(User $user=null, $notrigger=0)
Create bank information record.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.