dolibarr  7.0.0-beta
account.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
4  * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
6  * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@capnetworks.com>
7  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8  * Copyright (C) 2015-2016 Marcos GarcĂ­a <marcosgdf@gmail.com>
9  * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@zendsi.com>
10  * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
31 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
32 
33 
37 class Account extends CommonObject
38 {
39  public $element = 'bank_account';
40  public $table_element = 'bank_account';
41  public $picto = 'account';
42 
48  public $rowid;
49 
54  public $label;
55 
60  public $courant;
61 
66  public $type;
67 
72  public $bank;
73 
78  public $clos = self::STATUS_OPEN;
79 
84  public $rappro=1;
85 
90  public $url;
91 
96  public $code_banque;
97 
102  public $code_guichet;
103 
108  public $number;
109 
114  public $cle_rib;
115 
120  public $bic;
121 
126  public $iban;
127 
132  public $proprio;
133 
138  public $owner_address;
139 
140  public $state_id;
141  public $state_code;
142  public $state;
143 
149  public $type_lib = array();
150 
156  public $status = array();
157 
162  public $account_number;
163  public $fk_accountancy_journal;
164 
169  public $currency_code;
170 
176  public $account_currency_code;
177 
182  public $min_allowed;
183 
188  public $min_desired;
189 
194  public $comment;
195 
200  public $date_solde;
201 
205  const TYPE_CURRENT = 1;
209  const TYPE_CASH = 2;
213  const TYPE_SAVINGS = 0;
214 
215  const STATUS_OPEN = 0;
216  const STATUS_CLOSED = 1;
217 
223  function __construct(DoliDB $db)
224  {
225  global $langs;
226 
227  $this->db = $db;
228 
229  $this->solde = 0;
230 
231  $this->type_lib = array(
232  self::TYPE_SAVINGS => $langs->trans("BankType0"),
233  self::TYPE_CURRENT => $langs->trans("BankType1"),
234  self::TYPE_CASH => $langs->trans("BankType2"),
235  );
236 
237  $this->status = array(
238  self::STATUS_OPEN => $langs->trans("StatusAccountOpened"),
239  self::STATUS_CLOSED => $langs->trans("StatusAccountClosed")
240  );
241  }
242 
248  public function __toString()
249  {
250  $string = '';
251 
252  foreach ($this->getFieldsToShow() as $val) {
253 
254  if ($val == 'BankCode') {
255  $string .= $this->code_banque.' ';
256  } elseif ($val == 'BankAccountNumber') {
257  $string .= $this->number.' ';
258  } elseif ($val == 'DeskCode') {
259  $string .= $this->code_guichet.' ';
260  } elseif ($val == 'BankAccountNumberKey') {
261  $string .= $this->cle_rib.' ';
262  }elseif ($val == 'BIC') {
263  $string .= $this->bic.' ';
264  }elseif ($val == 'IBAN') {
265  $string .= $this->iban.' ';
266  }
267  }
268 
269  return trim($string);
270  }
271 
272 
278  function canBeConciliated()
279  {
280  global $conf;
281 
282  if (empty($this->rappro)) return -1;
283  if ($this->courant == Account::TYPE_CASH && empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) return -2;
284  if ($this->clos) return -3;
285  return 1;
286  }
287 
288 
299  function add_url_line($line_id, $url_id, $url, $label, $type)
300  {
301  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
302  $sql.= "fk_bank";
303  $sql.= ", url_id";
304  $sql.= ", url";
305  $sql.= ", label";
306  $sql.= ", type";
307  $sql.= ") VALUES (";
308  $sql.= "'".$line_id."'";
309  $sql.= ", '".$url_id."'";
310  $sql.= ", '".$url."'";
311  $sql.= ", '".$this->db->escape($label)."'";
312  $sql.= ", '".$type."'";
313  $sql.= ")";
314 
315  dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
316  if ($this->db->query($sql))
317  {
318  $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
319  return $rowid;
320  }
321  else
322  {
323  $this->error=$this->db->lasterror();
324  return -1;
325  }
326  }
327 
337  function get_url($fk_bank='', $url_id='', $type='')
338  {
339  $lines = array();
340 
341  // Check parameters
342  if (! empty($fk_bank) && (! empty($url_id) || ! empty($type)))
343  {
344  $this->error="ErrorBadParameter";
345  return -1;
346  }
347 
348  $sql = "SELECT fk_bank, url_id, url, label, type";
349  $sql.= " FROM ".MAIN_DB_PREFIX."bank_url";
350  if ($fk_bank > 0) {
351  $sql.= " WHERE fk_bank = ".$fk_bank;
352  }
353  else { $sql.= " WHERE url_id = ".$url_id." AND type = '".$type."'";
354  }
355  $sql.= " ORDER BY type, label";
356 
357  dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
358  $result = $this->db->query($sql);
359  if ($result)
360  {
361  $i = 0;
362  $num = $this->db->num_rows($result);
363  while ($i < $num)
364  {
365  $obj = $this->db->fetch_object($result);
366  // Anciens liens (pour compatibilite)
367  $lines[$i][0] = $obj->url;
368  $lines[$i][1] = $obj->url_id;
369  $lines[$i][2] = $obj->label;
370  $lines[$i][3] = $obj->type;
371  // Nouveaux liens
372  $lines[$i]['url'] = $obj->url;
373  $lines[$i]['url_id'] = $obj->url_id;
374  $lines[$i]['label'] = $obj->label;
375  $lines[$i]['type'] = $obj->type;
376  $lines[$i]['fk_bank'] = $obj->fk_bank;
377  $i++;
378  }
379  }
380  else dol_print_error($this->db);
381 
382  return $lines;
383  }
384 
400  function addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur='',$banque='', $accountancycode='')
401  {
402  // DeprecatĂ®on warning
403  if (is_numeric($oper)) {
404  dol_syslog(__METHOD__ . ": using numeric operations is deprecated", LOG_WARNING);
405  }
406 
407  // Clean parameters
408  $emetteur=trim($emetteur);
409  $banque=trim($banque);
410 
411  $now=dol_now();
412 
413  if (is_numeric($oper)) // Clean oper to have a code instead of a rowid
414  {
415  $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
416  $sql.= " WHERE id=".$oper;
417  $sql.= " AND entity IN (".getEntity('c_paiement').")";
418  $resql=$this->db->query($sql);
419  if ($resql)
420  {
421  $obj=$this->db->fetch_object($resql);
422  $oper=$obj->code;
423  }
424  else
425  {
426  dol_print_error($this->db,'Failed to get payment type code');
427  return -1;
428  }
429  }
430 
431  // Check parameters
432  if (! $oper)
433  {
434  $this->error="oper not defined";
435  return -1;
436  }
437  if (! $this->rowid)
438  {
439  $this->error="this->rowid not defined";
440  return -2;
441  }
442  if ($this->courant == Account::TYPE_CASH && $oper != 'LIQ')
443  {
444  $this->error="ErrorCashAccountAcceptsOnlyCashMoney";
445  return -3;
446  }
447 
448  $this->db->begin();
449 
450  $datev = $date;
451 
452  $accline = new AccountLine($this->db);
453  $accline->datec = $now;
454  $accline->dateo = $date;
455  $accline->datev = $datev;
456  $accline->label = $label;
457  $accline->amount = $amount;
458  $accline->fk_user_author = $user->id;
459  $accline->fk_account = $this->rowid;
460  $accline->fk_type = $oper;
461  $accline->numero_compte = $accountancycode;
462 
463  if ($num_chq) {
464  $accline->num_chq = $num_chq;
465  }
466 
467  if ($emetteur) {
468  $accline->emetteur = $emetteur;
469  }
470 
471  if ($banque) {
472  $accline->bank_chq = $banque;
473  }
474 
475  if ($accline->insert() > 0) {
476 
477  if ($categorie>0) {
478  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
479  $sql .= "lineid, fk_categ";
480  $sql .= ") VALUES (";
481  $sql .= $accline->id.", ".$categorie;
482  $sql .= ")";
483 
484  $result = $this->db->query($sql);
485  if (!$result) {
486  $this->error = $this->db->lasterror();
487  $this->db->rollback();
488  return -3;
489  }
490  }
491 
492  $this->db->commit();
493  return $accline->id;
494  } else {
495  $this->error = $this->db->lasterror();
496  $this->db->rollback();
497  return -2;
498  }
499  }
500 
508  function create(User $user = null, $notrigger=0)
509  {
510  global $langs,$conf, $hookmanager;
511 
512  // Clean parameters
513  if (! $this->min_allowed) $this->min_allowed=0;
514  if (! $this->min_desired) $this->min_desired=0;
515  $this->state_id = ($this->state_id?$this->state_id:$this->state_id);
516  $this->country_id = ($this->country_id?$this->country_id:$this->country_id);
517 
518  // Check parameters
519  if (empty($this->country_id))
520  {
521  $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Country"));
522  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
523  return -1;
524  }
525  if (empty($this->ref))
526  {
527  $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Ref"));
528  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
529  return -1;
530  }
531  if (empty($this->date_solde))
532  {
533  $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("DateInitialBalance"));
534  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
535  return -1;
536  }
537 
538  // Chargement librairie pour acces fonction controle RIB
539  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
540 
541  $now=dol_now();
542 
543  $this->db->begin();
544 
545  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
546  $sql.= "datec";
547  $sql.= ", ref";
548  $sql.= ", label";
549  $sql.= ", entity";
550  $sql.= ", account_number";
551  $sql.= ", fk_accountancy_journal";
552  $sql.= ", bank";
553  $sql.= ", code_banque";
554  $sql.= ", code_guichet";
555  $sql.= ", number";
556  $sql.= ", cle_rib";
557  $sql.= ", bic";
558  $sql.= ", iban_prefix";
559  $sql.= ", domiciliation";
560  $sql.= ", proprio";
561  $sql.= ", owner_address";
562  $sql.= ", currency_code";
563  $sql.= ", rappro";
564  $sql.= ", min_allowed";
565  $sql.= ", min_desired";
566  $sql.= ", comment";
567  $sql.= ", state_id";
568  $sql.= ", fk_pays";
569  $sql.= ") VALUES (";
570  $sql.= "'".$this->db->idate($now)."'";
571  $sql.= ", '".$this->db->escape($this->ref)."'";
572  $sql.= ", '".$this->db->escape($this->label)."'";
573  $sql.= ", ".$conf->entity;
574  $sql.= ", '".$this->db->escape($this->account_number)."'";
575  $sql.= ", ".($this->fk_accountancy_journal > 0 ? $this->db->escape($this->fk_accountancy_journal) : "null");
576  $sql.= ", '".$this->db->escape($this->bank)."'";
577  $sql.= ", '".$this->db->escape($this->code_banque)."'";
578  $sql.= ", '".$this->db->escape($this->code_guichet)."'";
579  $sql.= ", '".$this->db->escape($this->number)."'";
580  $sql.= ", '".$this->db->escape($this->cle_rib)."'";
581  $sql.= ", '".$this->db->escape($this->bic)."'";
582  $sql.= ", '".$this->db->escape($this->iban)."'";
583  $sql.= ", '".$this->db->escape($this->domiciliation)."'";
584  $sql.= ", '".$this->db->escape($this->proprio)."'";
585  $sql.= ", '".$this->db->escape($this->owner_address)."'";
586  $sql.= ", '".$this->db->escape($this->currency_code)."'";
587  $sql.= ", ".$this->rappro;
588  $sql.= ", ".price2num($this->min_allowed);
589  $sql.= ", ".price2num($this->min_desired);
590  $sql.= ", '".$this->db->escape($this->comment)."'";
591  $sql.= ", ".($this->state_id>0?$this->state_id:"null");
592  $sql.= ", ".$this->country_id;
593  $sql.= ")";
594 
595  dol_syslog(get_class($this)."::create", LOG_DEBUG);
596  $resql=$this->db->query($sql);
597  if ($resql)
598  {
599  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
600 
601  $result=$this->update($user, 1);
602  if ($result > 0)
603  {
604  $accline = new AccountLine($this->db);
605  $accline->datec = $this->db->idate($now);
606  $accline->label = '('.$langs->trans("InitialBankBalance").')';
607  $accline->amount = price2num($this->solde);
608  $accline->fk_user_author = $user->id;
609  $accline->fk_account = $this->id;
610  $accline->datev = $this->db->idate($this->date_solde);
611  $accline->dateo = $this->db->idate($this->date_solde);
612  $accline->fk_type = 'SOLD';
613 
614  if ($accline->insert() < 0) {
615  $error++;
616  $this->error = $accline->error;
617  $this->errors = $accline->errors;
618  }
619 
620  if (! $error)
621  {
622  $result=$this->insertExtraFields();
623  if ($result < 0) $error++;
624  }
625 
626  if (! $error && ! $notrigger)
627  {
628  // Call trigger
629  $result=$this->call_trigger('BANKACCOUNT_CREATE',$user);
630  if ($result < 0) $error++;
631  // End call triggers
632  }
633  }
634  else
635  {
636  $error++;
637  }
638  }
639  else
640  {
641  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
642  {
643  $this->error=$langs->trans("ErrorBankLabelAlreadyExists");
644  $error++;
645  }
646  else {
647  $this->error=$this->db->error()." sql=".$sql;
648  $error++;
649  }
650  }
651 
652  if (! $error)
653  {
654  $this->db->commit();
655  return $this->id;
656  }
657  else
658  {
659  $this->db->rollback();
660  return -1*$error;
661  }
662  }
663 
671  function update(User $user = null, $notrigger = 0)
672  {
673  global $langs,$conf, $hookmanager;
674 
675  $error=0;
676 
677  $this->db->begin();
678 
679  // Clean parameters
680  $this->state_id = ($this->state_id?$this->state_id:$this->state_id);
681  $this->country_id = ($this->country_id?$this->country_id:$this->country_id);
682 
683  // Check parameters
684  if (empty($this->country_id))
685  {
686  $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Country"));
687  dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
688  return -1;
689  }
690  if (empty($this->ref))
691  {
692  $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Ref"));
693  dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
694  return -1;
695  }
696  if (! $this->label) $this->label = "???";
697 
698  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
699 
700  $sql.= " ref = '".$this->db->escape($this->ref)."'";
701  $sql.= ",label = '".$this->db->escape($this->label)."'";
702 
703  $sql.= ",courant = ".$this->courant;
704  $sql.= ",clos = ".$this->clos;
705  $sql.= ",rappro = ".$this->rappro;
706  $sql.= ",url = ".($this->url?"'".$this->db->escape($this->url)."'":"null");
707  $sql.= ",account_number = '".$this->db->escape($this->account_number)."'";
708  $sql.= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? $this->db->escape($this->fk_accountancy_journal) : "null");
709  $sql.= ",bank = '".$this->db->escape($this->bank)."'";
710  $sql.= ",code_banque='".$this->db->escape($this->code_banque)."'";
711  $sql.= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
712  $sql.= ",number='".$this->db->escape($this->number)."'";
713  $sql.= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
714  $sql.= ",bic='".$this->db->escape($this->bic)."'";
715  $sql.= ",iban_prefix = '".$this->db->escape($this->iban)."'";
716  $sql.= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
717  $sql.= ",proprio = '".$this->db->escape($this->proprio)."'";
718  $sql.= ",owner_address = '".$this->db->escape($this->owner_address)."'";
719 
720  $sql.= ",currency_code = '".$this->db->escape($this->currency_code)."'";
721 
722  $sql.= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
723  $sql.= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
724  $sql.= ",comment = '".$this->db->escape($this->comment)."'";
725 
726  $sql.= ",state_id = ".($this->state_id>0?$this->state_id:"null");
727  $sql.= ",fk_pays = ".$this->country_id;
728 
729  $sql.= " WHERE rowid = ".$this->id;
730  $sql.= " AND entity = ".$conf->entity;
731 
732  dol_syslog(get_class($this)."::update", LOG_DEBUG);
733  $result = $this->db->query($sql);
734  if ($result)
735  {
736  // Actions on extra fields (by external module or standard code)
737  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
738  {
739  if (! $error)
740  {
741  $result=$this->insertExtraFields();
742  if ($result < 0) $error++;
743  }
744  }
745 
746  if (! $error && ! $notrigger)
747  {
748  // Call trigger
749  $result=$this->call_trigger('BANKACCOUNT_UPDATE',$user);
750  if ($result < 0) $error++;
751  // End call triggers
752  }
753  }
754  else
755  {
756  $error++;
757  $this->error=$this->db->lasterror();
758  dol_print_error($this->db);
759  }
760 
761  if (! $error)
762  {
763  $this->db->commit();
764  return $this->id;
765  }
766  else
767  {
768  $this->db->rollback();
769  return -1*$error;
770  }
771  }
772 
773 
780  function update_bban(User $user = null)
781  {
782  global $conf,$langs;
783 
784  // Clean parameters
785  $this->state_id = ($this->state_id?$this->state_id:$this->state_id);
786  $this->country_id = ($this->country_id?$this->country_id:$this->country_id);
787 
788  // Chargement librairie pour acces fonction controle RIB
789  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
790 
791  dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
792 
793  // Check parameters
794  if (! $this->ref)
795  {
796  $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->trans("Ref"));
797  return -2;
798  }
799 
800  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
801  $sql.= " bank = '".$this->db->escape($this->bank)."'";
802  $sql.= ",code_banque='".$this->db->escape($this->code_banque)."'";
803  $sql.= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
804  $sql.= ",number='".$this->db->escape($this->number)."'";
805  $sql.= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
806  $sql.= ",bic='".$this->db->escape($this->bic)."'";
807  $sql.= ",iban_prefix = '".$this->db->escape($this->iban)."'";
808  $sql.= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
809  $sql.= ",proprio = '".$this->db->escape($this->proprio)."'";
810  $sql.= ",owner_address = '".$this->db->escape($this->owner_address)."'";
811  $sql.= ",state_id = ".($this->state_id>0?$this->state_id:"null");
812  $sql.= ",fk_pays = ".$this->country_id;
813  $sql.= " WHERE rowid = ".$this->id;
814  $sql.= " AND entity = ".$conf->entity;
815 
816  dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
817 
818  $result = $this->db->query($sql);
819  if ($result)
820  {
821  return 1;
822  }
823  else
824  {
825  $this->error=$this->db->lasterror();
826  dol_print_error($this->db);
827  return -1;
828  }
829  }
830 
831 
839  function fetch($id, $ref='')
840  {
841  global $conf;
842 
843  if (empty($id) && empty($ref))
844  {
845  $this->error="ErrorBadParameters";
846  return -1;
847  }
848 
849  $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,";
850  $sql.= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
851  $sql.= " ba.domiciliation, ba.proprio, ba.owner_address, ba.state_id, ba.fk_pays as country_id,";
852  $sql.= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
853  $sql.= " ba.min_allowed, ba.min_desired, ba.comment,";
854  $sql.= " ba.datec as date_creation, ba.tms as date_update,";
855  $sql.= ' c.code as country_code, c.label as country,';
856  $sql.= ' d.code_departement as state_code, d.nom as state';
857  $sql.= ' , aj.code as accountancy_journal';
858  $sql.= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
859  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
860  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
861  $sql.= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal';
862  $sql.= " WHERE ba.entity IN (".getEntity($this->element).")";
863  if ($id) $sql.= " AND ba.rowid = ".$id;
864  if ($ref) $sql.= " AND ba.ref = '".$this->db->escape($ref)."'";
865 
866  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
867  $result = $this->db->query($sql);
868  if ($result)
869  {
870  if ($this->db->num_rows($result))
871  {
872  $obj = $this->db->fetch_object($result);
873 
874  $this->id = $obj->rowid;
875  $this->rowid = $obj->rowid;
876  $this->ref = $obj->ref;
877  $this->label = $obj->label;
878  $this->type = $obj->courant;
879  $this->courant = $obj->courant;
880  $this->bank = $obj->bank;
881  $this->clos = $obj->clos;
882  $this->rappro = $obj->rappro;
883  $this->url = $obj->url;
884 
885  $this->code_banque = $obj->code_banque;
886  $this->code_guichet = $obj->code_guichet;
887  $this->number = $obj->number;
888  $this->cle_rib = $obj->cle_rib;
889  $this->bic = $obj->bic;
890  $this->iban = $obj->iban;
891  $this->domiciliation = $obj->domiciliation;
892  $this->proprio = $obj->proprio;
893  $this->owner_address = $obj->owner_address;
894 
895  $this->state_id = $obj->state_id;
896  $this->state_code = $obj->state_code;
897  $this->state = $obj->state;
898 
899  $this->country_id = $obj->country_id;
900  $this->country_code = $obj->country_code;
901  $this->country = $obj->country;
902 
903  $this->account_number = $obj->account_number;
904  $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
905  $this->accountancy_journal = $obj->accountancy_journal;
906 
907  $this->currency_code = $obj->currency_code;
908  $this->account_currency_code = $obj->currency_code;
909  $this->min_allowed = $obj->min_allowed;
910  $this->min_desired = $obj->min_desired;
911  $this->comment = $obj->comment;
912 
913  $this->date_creation = $this->db->jdate($obj->date_creation);
914  $this->date_update = $this->db->jdate($obj->date_update);
915 
916  // Retreive all extrafield for thirdparty
917  // fetch optionals attributes and labels
918  require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
919  $extrafields=new ExtraFields($this->db);
920  $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
921  $this->fetch_optionals($this->id,$extralabels);
922 
923  return 1;
924  }
925  else
926  {
927  return 0;
928  }
929  }
930  else
931  {
932  $this->error=$this->db->lasterror;
933  $this->errors[]=$this->error;
934  return -1;
935  }
936  }
937 
947  public function setCategories($categories) {
948  // Handle single category
949  if (! is_array($categories)) {
950  $categories = array($categories);
951  }
952 
953  // Get current categories
954  require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
955  $c = new Categorie($this->db);
956  $existing = $c->containing($this->id, Categorie::TYPE_ACCOUNT, 'id');
957 
958  // Diff
959  if (is_array($existing)) {
960  $to_del = array_diff($existing, $categories);
961  $to_add = array_diff($categories, $existing);
962  } else {
963  $to_del = array(); // Nothing to delete
964  $to_add = $categories;
965  }
966 
967  // Process
968  foreach($to_del as $del) {
969  if ($c->fetch($del) > 0) {
970  $c->del_type($this, 'account');
971  }
972  }
973  foreach ($to_add as $add) {
974  if ($c->fetch($add) > 0) {
975  $c->add_type($this, 'account');
976  }
977  }
978 
979  return;
980  }
981 
988  function delete(User $user = null)
989  {
990  global $conf;
991 
992  $error=0;
993 
994  $this->db->begin();
995 
996  // Delete link between tag and bank account
997  if (! $error)
998  {
999  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1000  $sql.= " WHERE fk_account = ".$this->id;
1001 
1002  $resql = $this->db->query($sql);
1003  if (!$resql)
1004  {
1005  $error++;
1006  $this->error = "Error ".$this->db->lasterror();
1007  }
1008  }
1009 
1010  if (! $error)
1011  {
1012  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account";
1013  $sql.= " WHERE rowid = ".$this->rowid;
1014 
1015  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1016  $result = $this->db->query($sql);
1017  if ($result)
1018  {
1019  // Remove extrafields
1020  if ((empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used
1021  {
1022  $result=$this->deleteExtraFields();
1023  if ($result < 0)
1024  {
1025  $error++;
1026  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1027  }
1028  }
1029  }
1030  else
1031  {
1032  $error++;
1033  $this->error = "Error ".$this->db->lasterror();
1034  }
1035  }
1036 
1037  if (! $error)
1038  {
1039  $this->db->commit();
1040  return 1;
1041  }
1042  else
1043  {
1044  $this->db->rollback();
1045  return -1;
1046  }
1047  }
1048 
1049 
1056  function getLibStatut($mode=0)
1057  {
1058  return $this->LibStatut($this->clos,$mode);
1059  }
1060 
1068  function LibStatut($statut, $mode = 0)
1069  {
1070  global $langs;
1071  $langs->load('banks');
1072 
1073  if ($statut == self::STATUS_OPEN) {
1074  $label = $langs->trans("StatusAccountOpened");
1075  $picto = img_picto($label, 'statut4');
1076  } else {
1077  $label = $langs->trans("StatusAccountClosed");
1078  $picto = img_picto($label, 'statut5');
1079  }
1080 
1081  if ($mode == 2) {
1082  return $picto.' '.$label;
1083  } elseif ($mode == 3) {
1084  return $picto;
1085  } elseif ($mode == 4) {
1086  return $picto.' '.$label;
1087  } elseif ($mode == 5) {
1088  return $label.' '.$picto;
1089  } elseif ($mode == 6) {
1090  return $label.' '.$picto;
1091  }
1092 
1093  //There is no short mode for this label
1094  return $label;
1095  }
1096 
1097 
1103  function can_be_deleted()
1104  {
1105  $can_be_deleted=false;
1106 
1107  $sql = "SELECT COUNT(rowid) as nb";
1108  $sql.= " FROM ".MAIN_DB_PREFIX."bank";
1109  $sql.= " WHERE fk_account=".$this->id;
1110 
1111  $resql = $this->db->query($sql);
1112  if ($resql) {
1113  $obj=$this->db->fetch_object($resql);
1114  if ($obj->nb <= 1) $can_be_deleted=true; // Juste le solde
1115  }
1116  else {
1117  dol_print_error($this->db);
1118  }
1119  return $can_be_deleted;
1120  }
1121 
1122 
1128  function error()
1129  {
1130  return $this->error;
1131  }
1132 
1139  function solde($option=0)
1140  {
1141  $sql = "SELECT sum(amount) as amount";
1142  $sql.= " FROM ".MAIN_DB_PREFIX."bank";
1143  $sql.= " WHERE fk_account = ".$this->id;
1144  if ($option == 1) $sql.= " AND dateo <= '".$this->db->idate(dol_now())."'";
1145 
1146  $resql = $this->db->query($sql);
1147  if ($resql)
1148  {
1149  if ($this->db->num_rows($resql))
1150  {
1151  $obj=$this->db->fetch_object($resql);
1152  $solde = $obj->amount;
1153  }
1154  $this->db->free($resql);
1155  return $solde;
1156  }
1157  }
1158 
1166  function load_board(User $user, $filteraccountid = 0)
1167  {
1168  global $conf, $langs;
1169 
1170  if ($user->societe_id) return -1; // protection pour eviter appel par utilisateur externe
1171 
1172  $sql = "SELECT b.rowid, b.datev as datefin";
1173  $sql.= " FROM ".MAIN_DB_PREFIX."bank as b,";
1174  $sql.= " ".MAIN_DB_PREFIX."bank_account as ba";
1175  $sql.= " WHERE b.rappro=0";
1176  $sql.= " AND b.fk_account = ba.rowid";
1177  $sql.= " AND ba.entity IN (".getEntity('bank_account').")";
1178  $sql.= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1179  $sql.= " AND clos = 0";
1180  if ($filteraccountid) $sql.=" AND ba.rowid = ".$filteraccountid;
1181 
1182  $resql=$this->db->query($sql);
1183  if ($resql)
1184  {
1185  $langs->load("banks");
1186  $now=dol_now();
1187 
1188  require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1189 
1190  $response = new WorkboardResponse();
1191  $response->warning_delay=$conf->bank->rappro->warning_delay/60/60/24;
1192  $response->label=$langs->trans("TransactionsToConciliate");
1193  $response->url=DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1194  $response->img=img_object('',"payment");
1195 
1196  while ($obj=$this->db->fetch_object($resql))
1197  {
1198  $response->nbtodo++;
1199  if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1200  $response->nbtodolate++;
1201  }
1202  }
1203 
1204  return $response;
1205  }
1206  else
1207  {
1208  dol_print_error($this->db);
1209  $this->error=$this->db->error();
1210  return -1;
1211  }
1212  }
1213 
1219  function load_state_board($filteraccountid = 0)
1220  {
1221  global $user;
1222 
1223  if ($user->societe_id) return -1; // protection pour eviter appel par utilisateur externe
1224 
1225  $sql = "SELECT count(b.rowid) as nb";
1226  $sql.= " FROM ".MAIN_DB_PREFIX."bank as b,";
1227  $sql.= " ".MAIN_DB_PREFIX."bank_account as ba";
1228  $sql.= " WHERE b.fk_account = ba.rowid";
1229  $sql.= " AND ba.entity IN (".getEntity('bank_account').")";
1230  $sql.= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1231  $sql.= " AND clos = 0";
1232  if ($filteraccountid) $sql.=" AND ba.rowid = ".$filteraccountid;
1233 
1234  $resql=$this->db->query($sql);
1235  if ($resql)
1236  {
1237  while ($obj=$this->db->fetch_object($resql))
1238  {
1239  $this->nb["banklines"]=$obj->nb;
1240  }
1241  $this->db->free($resql);
1242  return 1;
1243  }
1244  else
1245  {
1246  dol_print_error($this->db);
1247  $this->error=$this->db->error();
1248  return -1;
1249  }
1250  }
1251 
1252 
1258  public static function countAccountToReconcile()
1259  {
1260  global $db, $conf, $user;
1261 
1262  //Protection against external users
1263  if ($user->societe_id) {
1264  return 0;
1265  }
1266 
1267  $nb=0;
1268 
1269  $sql = "SELECT COUNT(ba.rowid) as nb";
1270  $sql.= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1271  $sql.= " WHERE ba.rappro > 0 and ba.clos = 0";
1272  $sql.= " AND ba.entity IN (".getEntity('bank_account').")";
1273  if (empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) $sql.= " AND ba.courant != 2";
1274  $resql=$db->query($sql);
1275  if ($resql)
1276  {
1277  $obj = $db->fetch_object($resql);
1278  $nb = $obj->nb;
1279  }
1280  else dol_print_error($db);
1281 
1282  return $nb;
1283  }
1284 
1295  function getNomUrl($withpicto=0, $mode='', $option='', $save_lastsearch_value=-1, $notooltip=0)
1296  {
1297  global $conf, $langs;
1298 
1299  $result='';
1300  $label = '<u>' . $langs->trans("ShowAccount") . '</u>';
1301  $label .= '<br><b>' . $langs->trans('BankAccount') . ':</b> ' . $this->label;
1302  $label .= '<br><b>' . $langs->trans('AccountNumber') . ':</b> ' . $this->number;
1303  $label .= '<br><b>' . $langs->trans("AccountCurrency") . ':</b> ' . $this->currency_code;
1304  if (! empty($conf->accounting->enabled))
1305  {
1306  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1307  $langs->load("accountancy");
1308  $label .= '<br><b>' . $langs->trans('AccountAccounting') . ':</b> ' . length_accountg($this->account_number);
1309  $label .= '<br><b>' . $langs->trans('AccountancyJournal') . ':</b> ' . $this->accountancy_journal;
1310  }
1311  $linkclose = '" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1312 
1313  $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1314  if ($mode == 'transactions')
1315  {
1316  $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1317  }
1318  else if ($mode == 'receipts')
1319  {
1320  $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1321  }
1322 
1323  if ($option != 'nolink')
1324  {
1325  // Add param to save lastsearch_values or not
1326  $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
1327  if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
1328  if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
1329  }
1330 
1331  $linkstart = '<a href="'.$url.$linkclose;
1332  $linkend = '</a>';
1333 
1334  $result .= $linkstart;
1335  if ($withpicto) $result.=img_object(($notooltip?'':$label), $this->picto, ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
1336  if ($withpicto != 2) $result.= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1337  $result .= $linkend;
1338 
1339  return $result;
1340  }
1341 
1342 
1343  // Method after here are common to Account and CompanyBankAccount
1344 
1345 
1351  function verif()
1352  {
1353  require_once DOL_DOCUMENT_ROOT . '/core/lib/bank.lib.php';
1354 
1355  $this->error_number = 0;
1356 
1357  // Call function to check BAN
1358 
1359  if (! checkIbanForAccount($this) || ! checkSwiftForAccount($this))
1360  {
1361  $this->error_number = 12;
1362  $this->error_message = 'IBANSWIFTControlError';
1363  }
1364  /*if (! checkBanForAccount($this))
1365  {
1366  $this->error_number = 12;
1367  $this->error_message = 'BANControlError';
1368  }*/
1369 
1370  if ($this->error_number == 0)
1371  {
1372  return 1;
1373  }
1374  else
1375  {
1376  return 0;
1377  }
1378  }
1379 
1385  function getCountryCode()
1386  {
1387  global $mysoc;
1388 
1389  // We return country code of bank account
1390  if (! empty($this->country_code)) return $this->country_code;
1391 
1392  // For backward compatibility, we try to guess country from other information
1393  if (! empty($this->iban))
1394  {
1395  // If IBAN defined, we can know country of account from it
1396  if (preg_match("/^([a-zA-Z][a-zA-Z])/i",$this->iban,$reg)) return $reg[1];
1397  }
1398 
1399  // If this class is linked to a third party
1400  if (! empty($this->socid))
1401  {
1402  require_once DOL_DOCUMENT_ROOT .'/societe/class/societe.class.php';
1403  $company=new Societe($this->db);
1404  $result=$company->fetch($this->socid);
1405  if (! empty($company->country_code)) return $company->country_code;
1406  }
1407 
1408  // We return country code of managed company
1409  if (! empty($mysoc->country_code)) return $mysoc->country_code;
1410 
1411  return '';
1412  }
1413 
1422  function useDetailedBBAN()
1423  {
1424  $country_code=$this->getCountryCode();
1425 
1426  if (in_array($country_code,array('CH','FR','ES','GA','IT','NC'))) return 1; // France, Spain, Gabon, ...
1427  if (in_array($country_code,array('AU','BE','CA','DE','DK','GR','GB','ID','IE','IR','KR','NL','NZ','UK','US'))) return 2; // Australia, England...
1428  return 0;
1429  }
1430 
1436  function needIBAN()
1437  {
1438  $country_code=$this->getCountryCode();
1439 
1440  $country_code_in_EEC=array(
1441  'AT', // Austria
1442  'BE', // Belgium
1443  'BG', // Bulgaria
1444  'CY', // Cyprus
1445  'CZ', // Czech republic
1446  'DE', // Germany
1447  'DK', // Danemark
1448  'EE', // Estonia
1449  'ES', // Spain
1450  'FI', // Finland
1451  'FR', // France
1452  'GB', // United Kingdom
1453  'GR', // Greece
1454  'HR', // Croatia
1455  'NL', // Holland
1456  'HU', // Hungary
1457  'IE', // Ireland
1458  'IM', // Isle of Man - Included in UK
1459  'IT', // Italy
1460  'LT', // Lithuania
1461  'LU', // Luxembourg
1462  'LV', // Latvia
1463  'MC', // Monaco - Included in France
1464  'MT', // Malta
1465  //'NO', // Norway
1466  'PL', // Poland
1467  'PT', // Portugal
1468  'RO', // Romania
1469  'SE', // Sweden
1470  'SK', // Slovakia
1471  'SI', // Slovenia
1472  'UK', // United Kingdom
1473  //'CH', // Switzerland - No. Swizerland in not in EEC
1474  );
1475 
1476  if (in_array($country_code,$country_code_in_EEC)) return 1; // France, Spain, ...
1477  return 0;
1478  }
1479 
1486  function info($id)
1487  {
1488 
1489  }
1490 
1505  public function getFieldsToShow($includeibanbic=0)
1506  {
1507  //Get the required properties depending on the country
1508  $detailedBBAN = $this->useDetailedBBAN();
1509 
1510  if ($detailedBBAN == 0) {
1511  $fieldarray= array(
1512  'BankAccountNumber'
1513  );
1514  } elseif ($detailedBBAN == 2) {
1515  $fieldarray= array(
1516  'BankCode',
1517  'BankAccountNumber'
1518  );
1519  } else {
1520  $fieldarray=self::getAccountNumberOrder();
1521  }
1522 
1523  //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1524  if ($includeibanbic)
1525  {
1526  $fieldarray[]='IBAN';
1527  $fieldarray[]='BIC';
1528  }
1529  //}
1530 
1531  //Get the order the properties are shown
1532  return $fieldarray;
1533 
1534  }
1535 
1546  public static function getAccountNumberOrder()
1547  {
1548  global $conf;
1549 
1550  $fieldlists = array(
1551  'BankCode',
1552  'DeskCode',
1553  'BankAccountNumber',
1554  'BankAccountNumberKey'
1555  );
1556 
1557  if (!empty($conf->global->BANK_SHOW_ORDER_OPTION)) {
1558  if (is_numeric($conf->global->BANK_SHOW_ORDER_OPTION)) {
1559  if ($conf->global->BANK_SHOW_ORDER_OPTION == '1') {
1560  $fieldlists = array(
1561  'BankCode',
1562  'DeskCode',
1563  'BankAccountNumberKey',
1564  'BankAccountNumber'
1565  );
1566  }
1567  } else {
1568  //Replace the old AccountNumber key with the new BankAccountNumber key
1569  $fieldlists = explode(
1570  ' ',
1571  preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber',
1572  $conf->global->BANK_SHOW_ORDER_OPTION)
1573  );
1574  }
1575  }
1576 
1577  return $fieldlists;
1578  }
1579 
1580 
1588  function initAsSpecimen()
1589  {
1590  $this->specimen = 1;
1591  $this->ref = 'MBA';
1592  $this->label = 'My Big Company Bank account';
1593  $this->bank = 'MyBank';
1594  $this->courant = Account::TYPE_CURRENT;
1595  $this->clos = Account::STATUS_OPEN;
1596  $this->code_banque = '123';
1597  $this->code_guichet = '456';
1598  $this->number = 'ABC12345';
1599  $this->cle_rib = 50;
1600  $this->bic = 'AA12';
1601  $this->iban = 'FR999999999';
1602  $this->domiciliation = 'My bank address';
1603  $this->proprio = 'Owner';
1604  $this->owner_address = 'Owner address';
1605  $this->country_id = 1;
1606  }
1607 
1608 }
1609 
1610 
1615 {
1616  var $error;
1617  var $db;
1618  var $element='bank';
1619  var $table_element='bank';
1620  var $picto = 'generic';
1621 
1622  var $id;
1623  var $ref;
1624  var $datec;
1625  var $dateo;
1626 
1630  var $datev;
1631  var $amount;
1632  var $label;
1633  var $note;
1634  var $fk_user_author;
1635  var $fk_user_rappro;
1636  var $fk_type;
1637  var $rappro; // Is it conciliated
1638  var $num_releve; // If conciliated, what is bank statement
1639  var $num_chq; // Num of cheque
1640  var $bank_chq; // Bank of cheque
1641  var $fk_bordereau; // Id of cheque receipt
1642 
1643  var $fk_account; // Id of bank account
1644  var $bank_account_label; // Label of bank account
1645 
1646  public $emetteur;
1647 
1653  function __construct(DoliDB $db)
1654  {
1655  $this->db = $db;
1656  }
1657 
1666  function fetch($rowid,$ref='',$num='')
1667  {
1668  global $conf;
1669 
1670  // Check parameters
1671  if (empty($rowid) && empty($ref) && empty($num)) return -1;
1672 
1673  $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
1674  $sql.= " b.fk_user_author, b.fk_user_rappro,";
1675  $sql.= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
1676  $sql.= " b.fk_bordereau, b.banque, b.emetteur,";
1677  //$sql.= " b.author"; // Is this used ?
1678  $sql.= " ba.ref as bank_account_ref, ba.label as bank_account_label";
1679  $sql.= " FROM ".MAIN_DB_PREFIX."bank as b,";
1680  $sql.= " ".MAIN_DB_PREFIX."bank_account as ba";
1681  $sql.= " WHERE b.fk_account = ba.rowid";
1682  $sql.= " AND ba.entity IN (".getEntity('bank_account').")";
1683  if ($num) $sql.= " AND b.num_chq='".$this->db->escape($num)."'";
1684  else if ($ref) $sql.= " AND b.rowid='".$this->db->escape($ref)."'";
1685  else $sql.= " AND b.rowid=".$rowid;
1686 
1687  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1688  $result = $this->db->query($sql);
1689  if ($result)
1690  {
1691  $ret=0;
1692 
1693  $obj = $this->db->fetch_object($result);
1694  if ($obj)
1695  {
1696  $this->id = $obj->rowid;
1697  $this->rowid = $obj->rowid;
1698  $this->ref = $obj->rowid;
1699 
1700  $this->datec = $obj->datec;
1701  $this->datev = $obj->datev;
1702  $this->dateo = $obj->dateo;
1703  $this->amount = $obj->amount;
1704  $this->label = $obj->label;
1705  $this->note = $obj->note;
1706 
1707  $this->fk_user_author = $obj->fk_user_author;
1708  $this->fk_user_rappro = $obj->fk_user_rappro;
1709 
1710  $this->fk_type = $obj->fk_type; // Type of transaction
1711  $this->rappro = $obj->rappro;
1712  $this->num_releve = $obj->num_releve;
1713 
1714  $this->num_chq = $obj->num_chq;
1715  $this->bank_chq = $obj->banque;
1716  $this->fk_bordereau = $obj->fk_bordereau;
1717 
1718  $this->fk_account = $obj->fk_account;
1719  $this->bank_account_ref = $obj->bank_account_ref;
1720  $this->bank_account_label = $obj->bank_account_label;
1721 
1722  $ret=1;
1723  }
1724  $this->db->free($result);
1725  return $ret;
1726  }
1727  else
1728  {
1729  return -1;
1730  }
1731  }
1732 
1738  public function insert()
1739  {
1740  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
1741  $sql .= "datec";
1742  $sql .= ", dateo";
1743  $sql .= ", datev";
1744  $sql .= ", label";
1745  $sql .= ", amount";
1746  $sql .= ", fk_user_author";
1747  $sql .= ", num_chq";
1748  $sql .= ", fk_account";
1749  $sql .= ", fk_type";
1750  $sql .= ", emetteur,banque";
1751  $sql .= ", rappro";
1752  $sql .= ", numero_compte";
1753  $sql .= ") VALUES (";
1754  $sql .= "'".$this->db->idate($this->datec)."'";
1755  $sql .= ", '".$this->db->idate($this->dateo)."'";
1756  $sql .= ", '".$this->db->idate($this->datev)."'";
1757  $sql .= ", '".$this->db->escape($this->label)."'";
1758  $sql .= ", ".price2num($this->amount);
1759  $sql .= ", ".($this->fk_user_author > 0 ? $this->fk_user_author :"null");
1760  $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
1761  $sql .= ", '".$this->db->escape($this->fk_account)."'";
1762  $sql .= ", '".$this->db->escape($this->fk_type)."'";
1763  $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
1764  $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
1765  $sql .= ", ".(int) $this->rappro;
1766  $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
1767  $sql .= ")";
1768 
1769  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
1770  $resql = $this->db->query($sql);
1771 
1772  if (!$resql) {
1773  $this->error = $this->db->lasterror();
1774  return -1;
1775  }
1776 
1777  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
1778 
1779  return $this->id;
1780  }
1781 
1788  function delete(User $user = null)
1789  {
1790  $nbko=0;
1791 
1792  if ($this->rappro)
1793  {
1794  // Protection to avoid any delete of consolidated lines
1795  $this->error="ErrorDeleteNotPossibleLineIsConsolidated";
1796  return -1;
1797  }
1798 
1799  $this->db->begin();
1800 
1801  // Delete urls
1802  $result=$this->delete_urls($user);
1803  if ($result < 0)
1804  {
1805  $nbko++;
1806  }
1807 
1808  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".(int) $this->rowid;
1809  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1810  $result = $this->db->query($sql);
1811  if (! $result) $nbko++;
1812 
1813  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
1814  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1815  $result = $this->db->query($sql);
1816  if (! $result) $nbko++;
1817 
1818  if (! $nbko)
1819  {
1820  $this->db->commit();
1821  return 1;
1822  }
1823  else
1824  {
1825  $this->db->rollback();
1826  return -$nbko;
1827  }
1828  }
1829 
1830 
1837  function delete_urls(User $user = null)
1838  {
1839  $nbko=0;
1840 
1841  if ($this->rappro)
1842  {
1843  // Protection to avoid any delete of consolidated lines
1844  $this->error="ErrorDeleteNotPossibleLineIsConsolidated";
1845  return -1;
1846  }
1847 
1848  $this->db->begin();
1849 
1850  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
1851  dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
1852  $result = $this->db->query($sql);
1853  if (! $result) $nbko++;
1854 
1855  if (! $nbko)
1856  {
1857  $this->db->commit();
1858  return 1;
1859  }
1860  else
1861  {
1862  $this->db->rollback();
1863  return -$nbko;
1864  }
1865  }
1866 
1867 
1875  function update(User $user, $notrigger = 0)
1876  {
1877  $this->db->begin();
1878 
1879  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
1880  $sql.= " amount = ".price2num($this->amount).",";
1881  $sql.= " datev='".$this->db->idate($this->datev)."',";
1882  $sql.= " dateo='".$this->db->idate($this->dateo)."'";
1883  $sql.= " WHERE rowid = ".$this->rowid;
1884 
1885  dol_syslog(get_class($this)."::update", LOG_DEBUG);
1886  $resql = $this->db->query($sql);
1887  if ($resql)
1888  {
1889  $this->db->commit();
1890  return 1;
1891  }
1892  else
1893  {
1894  $this->db->rollback();
1895  $this->error=$this->db->error();
1896  return -1;
1897  }
1898  }
1899 
1900 
1908  function update_conciliation(User $user, $cat)
1909  {
1910  global $conf;
1911 
1912  $this->db->begin();
1913 
1914  // Check statement field
1915  if (! empty($conf->global->BANK_STATEMENT_REGEX_RULE))
1916  {
1917  if (! preg_match('/'.$conf->global->BANK_STATEMENT_REGEX_RULE.'/', $this->num_releve))
1918  {
1919  $this->errors[]=$langs->trans("ErrorBankStatementNameMustFollowRegex", $conf->global->BANK_STATEMENT_REGEX_RULE);
1920  return -1;
1921  }
1922  }
1923 
1924  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
1925  $sql.= " rappro = 1";
1926  $sql.= ", num_releve = '".$this->db->escape($this->num_releve)."'";
1927  $sql.= ", fk_user_rappro = ".$user->id;
1928  $sql.= " WHERE rowid = ".$this->id;
1929 
1930  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
1931  $resql = $this->db->query($sql);
1932  if ($resql)
1933  {
1934  if (! empty($cat))
1935  {
1936  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
1937  $sql.= "lineid";
1938  $sql.= ", fk_categ";
1939  $sql.= ") VALUES (";
1940  $sql.= $this->id;
1941  $sql.= ", ".$cat;
1942  $sql.= ")";
1943 
1944  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
1945  $this->db->query($sql);
1946 
1947  // No error check. Can fail if category already affected
1948  }
1949 
1950  $this->rappro=1;
1951 
1952  $this->db->commit();
1953  return 1;
1954  }
1955  else
1956  {
1957  $this->db->rollback();
1958  return -1;
1959  }
1960  }
1961 
1962 
1970  function datev_change($rowid,$sign=1)
1971  {
1972  $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".$rowid;
1973  $resql = $this->db->query($sql);
1974  if ($resql)
1975  {
1976  $obj=$this->db->fetch_object($resql);
1977  $newdate=$this->db->jdate($obj->datev)+(3600*24*$sign);
1978 
1979  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
1980  $sql.= " datev = '".$this->db->idate($newdate)."'";
1981  $sql.= " WHERE rowid = ".$rowid;
1982 
1983  $result = $this->db->query($sql);
1984  if ($result)
1985  {
1986  if ($this->db->affected_rows($result))
1987  {
1988  return 1;
1989  }
1990  }
1991  else
1992  {
1993  dol_print_error($this->db);
1994  return 0;
1995  }
1996  }
1997  else dol_print_error($this->db);
1998  return 0;
1999  }
2000 
2007  function datev_next($id)
2008  {
2009  return $this->datev_change($id,1);
2010  }
2011 
2018  function datev_previous($id)
2019  {
2020  return $this->datev_change($id,-1);
2021  }
2022 
2023 
2031  function dateo_change($rowid,$sign=1)
2032  {
2033  $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".$rowid;
2034  $resql = $this->db->query($sql);
2035  if ($resql)
2036  {
2037  $obj=$this->db->fetch_object($resql);
2038  $newdate=$this->db->jdate($obj->dateo)+(3600*24*$sign);
2039 
2040  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2041  $sql.= " dateo = '".$this->db->idate($newdate)."'";
2042  $sql.= " WHERE rowid = ".$rowid;
2043 
2044  $result = $this->db->query($sql);
2045  if ($result)
2046  {
2047  if ($this->db->affected_rows($result))
2048  {
2049  return 1;
2050  }
2051  }
2052  else
2053  {
2054  dol_print_error($this->db);
2055  return 0;
2056  }
2057  }
2058  else dol_print_error($this->db);
2059  return 0;
2060  }
2061 
2068  function dateo_next($id)
2069  {
2070  return $this->dateo_change($id,1);
2071  }
2072 
2079  function dateo_previous($id)
2080  {
2081  return $this->dateo_change($id,-1);
2082  }
2083 
2084 
2091  function info($id)
2092  {
2093  $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2094  $sql.= ' b.fk_user_author, b.fk_user_rappro';
2095  $sql.= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2096  $sql.= ' WHERE b.rowid = '.$id;
2097 
2098  $result=$this->db->query($sql);
2099  if ($result)
2100  {
2101  if ($this->db->num_rows($result))
2102  {
2103  $obj = $this->db->fetch_object($result);
2104  $this->id = $obj->rowid;
2105 
2106  if ($obj->fk_user_author)
2107  {
2108  $cuser = new User($this->db);
2109  $cuser->fetch($obj->fk_user_author);
2110  $this->user_creation = $cuser;
2111  }
2112  if ($obj->fk_user_rappro)
2113  {
2114  $ruser = new User($this->db);
2115  $ruser->fetch($obj->fk_user_rappro);
2116  $this->user_rappro = $ruser;
2117  }
2118 
2119  $this->date_creation = $this->db->jdate($obj->datec);
2120  $this->date_modification = $this->db->jdate($obj->datem);
2121  //$this->date_rappro = $obj->daterappro; // Not yet managed
2122  }
2123  $this->db->free($result);
2124  }
2125  else
2126  {
2127  dol_print_error($this->db);
2128  }
2129  }
2130 
2131 
2140  function getNomUrl($withpicto=0,$maxlen=0,$option='')
2141  {
2142  global $langs;
2143 
2144  $result='';
2145  $label=$langs->trans("ShowTransaction").': '.$this->rowid;
2146  $linkstart = '<a href="'.DOL_URL_ROOT.'/compta/bank/ligne.php?rowid='.$this->rowid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
2147  $linkend='</a>';
2148 
2149  $result .= $linkstart;
2150  if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'account'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
2151  if ($withpicto != 2) $result.=($this->ref?$this->ref:$this->rowid);
2152  $result .= $linkend;
2153 
2154  if ($option == 'showall' || $option == 'showconciliated') $result.=' (';
2155  if ($option == 'showall')
2156  {
2157  $result.=$langs->trans("BankAccount").': ';
2158  $accountstatic=new Account($this->db);
2159  $accountstatic->id=$this->fk_account;
2160  $accountstatic->ref=$this->bank_account_ref;
2161  $accountstatic->label=$this->bank_account_label;
2162  $result.=$accountstatic->getNomUrl(0).', ';
2163  }
2164  if ($option == 'showall' || $option == 'showconciliated')
2165  {
2166  $result.=$langs->trans("BankLineConciliated").': ';
2167  $result.=yn($this->rappro);
2168  }
2169  if ($option == 'showall' || $option == 'showconciliated') $result.=')';
2170 
2171  return $result;
2172  }
2173 
2174 
2181  function getLibStatut($mode=0)
2182  {
2183  return $this->LibStatut($this->status,$mode);
2184  }
2185 
2193  function LibStatut($statut,$mode=0)
2194  {
2195  global $langs;
2196  //$langs->load('companies');
2197  /*
2198  if ($mode == 0)
2199  {
2200  if ($statut==0) return $langs->trans("ActivityCeased");
2201  if ($statut==1) return $langs->trans("InActivity");
2202  }
2203  if ($mode == 1)
2204  {
2205  if ($statut==0) return $langs->trans("ActivityCeased");
2206  if ($statut==1) return $langs->trans("InActivity");
2207  }
2208  if ($mode == 2)
2209  {
2210  if ($statut==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2211  if ($statut==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2212  }
2213  if ($mode == 3)
2214  {
2215  if ($statut==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2216  if ($statut==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2217  }
2218  if ($mode == 4)
2219  {
2220  if ($statut==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2221  if ($statut==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2222  }
2223  if ($mode == 5)
2224  {
2225  if ($statut==0) return $langs->trans("ActivityCeased").' '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2226  if ($statut==1) return $langs->trans("InActivity").' '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2227  }*/
2228  }
2229 
2230 }
2231 
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
update_conciliation(User $user, $cat)
Update conciliation field.
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
getNomUrl($withpicto=0, $maxlen=0, $option='')
Return clicable name (with picto eventually)
static getAccountNumberOrder()
Returns the components of the bank account in order.
can_be_deleted()
Renvoi si un compte peut etre supprimer ou non (sans mouvements)
addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur='', $banque='', $accountancycode='')
Add an entry into table ".MAIN_DB_PREFIX."bank.
update_bban(User $user=null)
Update BBAN (RIB) account fields.
update(User $user=null, $notrigger=0)
Update bank account card.
Class to manage Dolibarr users.
Definition: user.class.php:39
useDetailedBBAN()
Return if a bank account is defined with detailed information (bank code, desk code, number and key).
load_board(User $user, $filteraccountid=0)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
Class to manage Dolibarr database access.
datev_next($id)
Increase value date of a rowid.
Class to manage bank transaction lines.
error()
Return error.
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...
checkSwiftForAccount($account)
Check SWIFT informations for a bank account.
Definition: bank.lib.php:209
canBeConciliated()
Return if a bank account need to be conciliated.
__construct(DoliDB $db)
Constructor.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
Class to manage bank accounts.
getLibStatut($mode=0)
Return label of object status.
LibStatut($statut, $mode=0)
Renvoi le libelle d'un statut donne.
insert()
Inserts a transaction to a bank account.
dateo_change($rowid, $sign=1)
Increase/decrease operation date of a rowid.
Class to manage standard extra fields.
get_url($fk_bank='', $url_id='', $type='')
TODO Move this into AccountLine Return array with links from llx_bank_url.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Class to manage categories.
needIBAN()
Return 1 if IBAN / BIC is mandatory (otherwise option)
dateo_previous($id)
Decrease operation date of a rowid.
setCategories($categories)
Sets object to supplied categories.
getCountryCode()
Return account country code.
getLibStatut($mode=0)
Return label of status (activity, closed)
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
deleteExtraFields()
Delete all extra fields values for the current object.
delete_urls(User $user=null)
Delete bank line records.
datev_change($rowid, $sign=1)
Increase/decrease value date of a rowid.
verif()
Return if an account has valid information for Direct debit payment.
dol_now($mode='gmt')
Return date for now.
update(User $user, $notrigger=0)
Update bank account record in database.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
getNomUrl($withpicto=0, $mode='', $option='', $save_lastsearch_value=-1, $notooltip=0)
Return clicable name (with picto eventually)
const TYPE_CASH
Cash account.
create(User $user=null, $notrigger=0)
Create bank account into database.
add_url_line($line_id, $url_id, $url, $label, $type)
Add a link between bank line record and its source.
__toString()
Shows the account number in the appropiate format.
solde($option=0)
Return current sold.
checkIbanForAccount($account)
Check IBAN number informations for a bank account.
Definition: bank.lib.php:226
fetch($id, $ref='')
Load a bank account into memory from database.
load_state_board($filteraccountid=0)
Charge indicateurs this->nb de tableau de bord.
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:1013
getFieldsToShow($includeibanbic=0)
Returns the fields in order that this bank account should show to the user Will return an array with ...
const TYPE_SAVINGS
Savings account.
info($id)
Load miscellaneous information for tab "Info".
static countAccountToReconcile()
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
info($id)
Load miscellaneous information for tab "Info".
call_trigger($trigger_name, $user)
Call trigger based on this instance.
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...
fetch($rowid, $ref='', $num='')
Load into memory content of a bank transaction line.
type
Definition: viewcat.php:283
dateo_next($id)
Increase operation date of a rowid.
__construct(DoliDB $db)
Constructor.
initAsSpecimen()
Initialise an instance with random values.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...
LibStatut($statut, $mode=0)
Return label of given object status.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
datev_previous($id)
Decrease value date of a rowid.
$datev
Value date.