dolibarr  16.0.5
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@inodbox.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@open-dsi.fr>
10  * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
11  * Copyright (C) 2019 JC Prieto <jcprieto@virtual20.com><prietojc@gmail.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <https://www.gnu.org/licenses/>.
25  */
26 
32 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
33 
34 
38 class Account extends CommonObject
39 {
43  public $element = 'bank_account';
44 
48  public $table_element = 'bank_account';
49 
53  public $picto = 'account';
54 
60  public $rowid;
61 
66  public $label;
67 
72  public $courant;
73 
78  public $type;
79 
84  public $bank;
85 
90  public $clos = self::STATUS_OPEN;
91 
96  public $rappro = 1;
97 
102  public $url;
103 
108  public $code_banque;
109 
114  public $code_guichet;
115 
120  public $number;
121 
126  public $cle_rib;
127 
132  public $bic;
133 
138  public $iban;
139 
146  public $iban_prefix;
147 
152  public $domiciliation;
153 
158  public $pti_in_ctti = 0;
159 
164  public $proprio;
165 
170  public $owner_address;
171 
172  public $state_id;
173  public $state_code;
174  public $state;
175 
181  public $type_lib = array();
182 
188  public $status = array();
189 
194  public $account_number;
195 
199  public $fk_accountancy_journal;
203  public $accountancy_journal;
204 
209  public $currency_code;
210 
216  public $account_currency_code;
217 
222  public $min_allowed;
223 
228  public $min_desired;
229 
234  public $comment;
235 
240  public $date_solde;
241 
246  public $ics;
247 
252  public $ics_transfer;
253 
254 
255 
280  // BEGIN MODULEBUILDER PROPERTIES
284  public $fields = array(
285  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
286  'ref' =>array('type'=>'varchar(12)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>25),
287  'label' =>array('type'=>'varchar(30)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>30),
288  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>35, 'index'=>1),
289  'bank' =>array('type'=>'varchar(60)', 'label'=>'Bank', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
290  'code_banque' =>array('type'=>'varchar(128)', 'label'=>'Code banque', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
291  'code_guichet' =>array('type'=>'varchar(6)', 'label'=>'Code guichet', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
292  'number' =>array('type'=>'varchar(255)', 'label'=>'Number', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
293  'cle_rib' =>array('type'=>'varchar(5)', 'label'=>'Cle rib', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
294  'bic' =>array('type'=>'varchar(11)', 'label'=>'Bic', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
295  'iban_prefix' =>array('type'=>'varchar(34)', 'label'=>'Iban prefix', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
296  'country_iban' =>array('type'=>'varchar(2)', 'label'=>'Country iban', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
297  'cle_iban' =>array('type'=>'varchar(2)', 'label'=>'Cle iban', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
298  'domiciliation' =>array('type'=>'varchar(255)', 'label'=>'Domiciliation', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
299  'state_id' =>array('type'=>'integer', 'label'=>'State id', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
300  'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>95),
301  'proprio' =>array('type'=>'varchar(60)', 'label'=>'Proprio', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
302  'owner_address' =>array('type'=>'text', 'label'=>'Owner address', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
303  'courant' =>array('type'=>'smallint(6)', 'label'=>'Courant', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>110),
304  'clos' =>array('type'=>'smallint(6)', 'label'=>'Clos', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>115),
305  'rappro' =>array('type'=>'smallint(6)', 'label'=>'Rappro', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
306  'url' =>array('type'=>'varchar(128)', 'label'=>'Url', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
307  'account_number' =>array('type'=>'varchar(32)', 'label'=>'Account number', 'enabled'=>1, 'visible'=>-1, 'position'=>130),
308  'fk_accountancy_journal' =>array('type'=>'integer', 'label'=>'Accountancy journal ID', 'enabled'=>1, 'visible'=>-1, 'position'=>132),
309  'accountancy_journal' =>array('type'=>'varchar(20)', 'label'=>'Accountancy journal', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
310  'currency_code' =>array('type'=>'varchar(3)', 'label'=>'Currency code', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>140),
311  'min_allowed' =>array('type'=>'integer', 'label'=>'Min allowed', 'enabled'=>1, 'visible'=>-1, 'position'=>145),
312  'min_desired' =>array('type'=>'integer', 'label'=>'Min desired', 'enabled'=>1, 'visible'=>-1, 'position'=>150),
313  'comment' =>array('type'=>'text', 'label'=>'Comment', 'enabled'=>1, 'visible'=>-1, 'position'=>155),
314  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>156),
315  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>157),
316  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>160),
317  'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>165),
318  'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>170),
319  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>175),
320  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>180),
321  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
322  );
323  // END MODULEBUILDER PROPERTIES
324 
328  const TYPE_CURRENT = 1;
332  const TYPE_CASH = 2;
336  const TYPE_SAVINGS = 0;
337 
338 
339  const STATUS_OPEN = 0;
340  const STATUS_CLOSED = 1;
341 
342 
348  public function __construct(DoliDB $db)
349  {
350  global $langs;
351 
352  $this->db = $db;
353 
354  $this->solde = 0;
355 
356  $this->type_lib = array(
357  self::TYPE_SAVINGS => $langs->trans("BankType0"),
358  self::TYPE_CURRENT => $langs->trans("BankType1"),
359  self::TYPE_CASH => $langs->trans("BankType2"),
360  );
361 
362  $this->status = array(
363  self::STATUS_OPEN => $langs->trans("StatusAccountOpened"),
364  self::STATUS_CLOSED => $langs->trans("StatusAccountClosed")
365  );
366  }
367 
373  public function __toString()
374  {
375  $string = '';
376  foreach ($this->getFieldsToShow() as $val) {
377  if ($val == 'BankCode') {
378  $string .= $this->code_banque.' ';
379  } elseif ($val == 'BankAccountNumber') {
380  $string .= $this->number.' ';
381  } elseif ($val == 'DeskCode') {
382  $string .= $this->code_guichet.' ';
383  } elseif ($val == 'BankAccountNumberKey') {
384  $string .= $this->cle_rib.' ';
385  } elseif ($val == 'BIC') {
386  $string .= $this->bic.' ';
387  } elseif ($val == 'IBAN') {
388  $string .= $this->iban.' ';
389  }
390  }
391 
392  return trim($string);
393  }
394 
395 
401  public function canBeConciliated()
402  {
403  global $conf;
404 
405  if (empty($this->rappro)) {
406  return -1;
407  }
408  if ($this->courant == Account::TYPE_CASH && empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
409  return -2;
410  }
411  if ($this->clos) {
412  return -3;
413  }
414  return 1;
415  }
416 
417 
418  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
429  public function add_url_line($line_id, $url_id, $url, $label, $type)
430  {
431  // phpcs:enable
432  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
433  $sql .= "fk_bank";
434  $sql .= ", url_id";
435  $sql .= ", url"; // deprecated
436  $sql .= ", label";
437  $sql .= ", type";
438  $sql .= ") VALUES (";
439  $sql .= " ".((int) $line_id);
440  $sql .= ", ".((int) $url_id);
441  $sql .= ", '".$this->db->escape($url)."'"; // dperecated
442  $sql .= ", '".$this->db->escape($label)."'";
443  $sql .= ", '".$this->db->escape($type)."'";
444  $sql .= ")";
445 
446  dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
447  if ($this->db->query($sql)) {
448  $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
449  return $rowid;
450  } else {
451  $this->error = $this->db->lasterror();
452  return -1;
453  }
454  }
455 
456  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
466  public function get_url($fk_bank = '', $url_id = '', $type = '')
467  {
468  // phpcs:enable
469  $lines = array();
470 
471  // Check parameters
472  if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) {
473  $this->error = "ErrorBadParameter";
474  return -1;
475  }
476 
477  $sql = "SELECT fk_bank, url_id, url, label, type";
478  $sql .= " FROM ".MAIN_DB_PREFIX."bank_url";
479  if ($fk_bank > 0) {
480  $sql .= " WHERE fk_bank = ".((int) $fk_bank);
481  } else {
482  $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'";
483  }
484  $sql .= " ORDER BY type, label";
485 
486  dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
487  $result = $this->db->query($sql);
488  if ($result) {
489  $i = 0;
490  $num = $this->db->num_rows($result);
491  while ($i < $num) {
492  $obj = $this->db->fetch_object($result);
493  // Anciens liens (pour compatibilite)
494  $lines[$i][0] = $obj->url;
495  $lines[$i][1] = $obj->url_id;
496  $lines[$i][2] = $obj->label;
497  $lines[$i][3] = $obj->type;
498  // Nouveaux liens
499  $lines[$i]['url'] = $obj->url;
500  $lines[$i]['url_id'] = $obj->url_id;
501  $lines[$i]['label'] = $obj->label;
502  $lines[$i]['type'] = $obj->type;
503  $lines[$i]['fk_bank'] = $obj->fk_bank;
504  $i++;
505  }
506  } else {
507  dol_print_error($this->db);
508  }
509 
510  return $lines;
511  }
512 
531  public function addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur = '', $banque = '', $accountancycode = '', $datev = null, $num_releve = '', $amount_main_currency = null)
532  {
533  // Deprecation warning
534  if (is_numeric($oper)) {
535  dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING);
536  }
537 
538  if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
539  $this->id = $this->rowid;
540  }
541 
542  // Clean parameters
543  $emetteur = trim($emetteur);
544  $banque = trim($banque);
545  $label = trim($label);
546 
547  $now = dol_now();
548 
549  if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid
550  $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
551  $sql .= " WHERE id = ".((int) $oper);
552  $sql .= " AND entity IN (".getEntity('c_paiement').")";
553  $resql = $this->db->query($sql);
554  if ($resql) {
555  $obj = $this->db->fetch_object($resql);
556  $oper = $obj->code;
557  } else {
558  dol_print_error($this->db, 'Failed to get payment type code');
559  return -1;
560  }
561  }
562 
563  // Check parameters
564  if (!$oper) {
565  $this->error = "oper not defined";
566  return -1;
567  }
568  if (!$this->id) {
569  $this->error = "this->id not defined";
570  return -2;
571  }
572  if ($this->courant == Account::TYPE_CASH && $oper != 'LIQ') {
573  $this->error = "ErrorCashAccountAcceptsOnlyCashMoney";
574  return -3;
575  }
576 
577  $this->db->begin();
578 
579  if (is_null($datev) || empty($datev)) {
580  $datev = $date;
581  }
582 
583  $accline = new AccountLine($this->db);
584  $accline->datec = $now;
585  $accline->dateo = $date;
586  $accline->datev = $datev;
587  $accline->label = $label;
588  $accline->amount = $amount;
589  $accline->amount_main_currency = $amount_main_currency;
590  $accline->fk_user_author = $user->id;
591  $accline->fk_account = $this->id;
592  $accline->fk_type = $oper;
593  $accline->numero_compte = $accountancycode;
594  $accline->num_releve = $num_releve;
595 
596  if ($num_chq) {
597  $accline->num_chq = $num_chq;
598  }
599 
600  if ($emetteur) {
601  $accline->emetteur = $emetteur;
602  }
603 
604  if ($banque) {
605  $accline->bank_chq = $banque;
606  }
607 
608  if ($accline->insert() > 0) {
609  if ($categorie > 0) {
610  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class(";
611  $sql .= "lineid, fk_categ";
612  $sql .= ") VALUES (";
613  $sql .= ((int) $accline->id).", '".$this->db->escape($categorie)."'";
614  $sql .= ")";
615 
616  $result = $this->db->query($sql);
617  if (!$result) {
618  $this->error = $this->db->lasterror();
619  $this->db->rollback();
620 
621  return -4;
622  }
623  }
624 
625  $this->db->commit();
626 
627  return $accline->id;
628  } else {
629  $this->error = $accline->error;
630  $this->errors = $accline->errors;
631  $this->db->rollback();
632 
633  return -5;
634  }
635  }
636 
644  public function create(User $user, $notrigger = 0)
645  {
646  global $langs, $conf;
647 
648  $error = 0;
649 
650  // Clean parameters
651  if (!$this->min_allowed) {
652  $this->min_allowed = 0;
653  }
654  if (!$this->min_desired) {
655  $this->min_desired = 0;
656  }
657 
658  // Check parameters
659  if (empty($this->country_id)) {
660  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
661  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
662  return -1;
663  }
664  if (empty($this->ref)) {
665  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
666  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
667  return -1;
668  }
669  if (empty($this->date_solde)) {
670  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance"));
671  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
672  return -1;
673  }
674 
675  // Chargement librairie pour acces fonction controle RIB
676  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
677 
678  $now = dol_now();
679 
680  $this->db->begin();
681 
682  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
683  $sql .= "datec";
684  $sql .= ", ref";
685  $sql .= ", label";
686  $sql .= ", entity";
687  $sql .= ", account_number";
688  $sql .= ", fk_accountancy_journal";
689  $sql .= ", bank";
690  $sql .= ", code_banque";
691  $sql .= ", code_guichet";
692  $sql .= ", number";
693  $sql .= ", cle_rib";
694  $sql .= ", bic";
695  $sql .= ", iban_prefix";
696  $sql .= ", domiciliation";
697  $sql .= ", pti_in_ctti";
698  $sql .= ", proprio";
699  $sql .= ", owner_address";
700  $sql .= ", currency_code";
701  $sql .= ", rappro";
702  $sql .= ", min_allowed";
703  $sql .= ", min_desired";
704  $sql .= ", comment";
705  $sql .= ", state_id";
706  $sql .= ", fk_pays";
707  $sql .= ", ics";
708  $sql .= ", ics_transfer";
709  $sql .= ") VALUES (";
710  $sql .= "'".$this->db->idate($now)."'";
711  $sql .= ", '".$this->db->escape($this->ref)."'";
712  $sql .= ", '".$this->db->escape($this->label)."'";
713  $sql .= ", ".((int) $conf->entity);
714  $sql .= ", '".$this->db->escape($this->account_number)."'";
715  $sql .= ", ".($this->fk_accountancy_journal > 0 ? $this->db->escape($this->fk_accountancy_journal) : "null");
716  $sql .= ", '".$this->db->escape($this->bank)."'";
717  $sql .= ", '".$this->db->escape($this->code_banque)."'";
718  $sql .= ", '".$this->db->escape($this->code_guichet)."'";
719  $sql .= ", '".$this->db->escape($this->number)."'";
720  $sql .= ", '".$this->db->escape($this->cle_rib)."'";
721  $sql .= ", '".$this->db->escape($this->bic)."'";
722  $sql .= ", '".$this->db->escape($this->iban)."'";
723  $sql .= ", '".$this->db->escape($this->domiciliation)."'";
724  $sql .= ", ".((int) $this->pti_in_ctti);
725  $sql .= ", '".$this->db->escape($this->proprio)."'";
726  $sql .= ", '".$this->db->escape($this->owner_address)."'";
727  $sql .= ", '".$this->db->escape($this->currency_code)."'";
728  $sql .= ", ".((int) $this->rappro);
729  $sql .= ", ".price2num($this->min_allowed, 'MT');
730  $sql .= ", ".price2num($this->min_desired, 'MT');
731  $sql .= ", '".$this->db->escape($this->comment)."'";
732  $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
733  $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
734  $sql .= ", '".$this->db->escape($this->ics)."'";
735  $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
736  $sql .= ")";
737 
738  dol_syslog(get_class($this)."::create", LOG_DEBUG);
739  $resql = $this->db->query($sql);
740  if ($resql) {
741  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
742 
743  $result = $this->update($user, 1);
744  if ($result > 0) {
745  $accline = new AccountLine($this->db);
746  $accline->datec = $this->db->idate($now);
747  $accline->label = '('.$langs->trans("InitialBankBalance").')';
748  $accline->amount = price2num($this->solde);
749  $accline->fk_user_author = $user->id;
750  $accline->fk_account = $this->id;
751  $accline->datev = $this->db->idate($this->date_solde);
752  $accline->dateo = $this->db->idate($this->date_solde);
753  $accline->fk_type = 'SOLD';
754 
755  if ($accline->insert() < 0) {
756  $error++;
757  $this->error = $accline->error;
758  $this->errors = $accline->errors;
759  }
760 
761  if (!$error) {
762  $result = $this->insertExtraFields();
763  if ($result < 0) {
764  $error++;
765  }
766  }
767 
768  if (!$error && !$notrigger) {
769  // Call trigger
770  $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
771  if ($result < 0) {
772  $error++;
773  }
774  // End call triggers
775  }
776  } else {
777  $error++;
778  }
779  } else {
780  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
781  $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
782  $error++;
783  } else {
784  $this->error = $this->db->error()." sql=".$sql;
785  $error++;
786  }
787  }
788 
789  if (!$error) {
790  $this->db->commit();
791  return $this->id;
792  } else {
793  $this->db->rollback();
794  return -1 * $error;
795  }
796  }
797 
805  public function update(User $user, $notrigger = 0)
806  {
807  global $langs, $conf;
808 
809  $error = 0;
810 
811  $this->db->begin();
812 
813  // Check parameters
814  if (empty($this->country_id)) {
815  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
816  dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
817  return -1;
818  }
819  if (empty($this->ref)) {
820  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
821  dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
822  return -1;
823  }
824  if (!$this->label) {
825  $this->label = "???";
826  }
827 
828  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
829 
830  $sql .= " ref = '".$this->db->escape($this->ref)."'";
831  $sql .= ",label = '".$this->db->escape($this->label)."'";
832 
833  $sql .= ",courant = ".((int) $this->courant);
834  $sql .= ",clos = ".((int) $this->clos);
835  $sql .= ",rappro = ".((int) $this->rappro);
836  $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
837  $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
838  $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? $this->db->escape($this->fk_accountancy_journal) : "null");
839  $sql .= ",bank = '".$this->db->escape($this->bank)."'";
840  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
841  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
842  $sql .= ",number='".$this->db->escape($this->number)."'";
843  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
844  $sql .= ",bic='".$this->db->escape($this->bic)."'";
845  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
846  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
847  $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
848  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
849  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
850 
851  $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
852 
853  $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
854  $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
855  $sql .= ",comment = '".$this->db->escape($this->comment)."'";
856 
857  $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
858  $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
859  $sql .= ",ics = '".$this->db->escape($this->ics)."'";
860  $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
861 
862  $sql .= " WHERE rowid = ".((int) $this->id);
863 
864  dol_syslog(get_class($this)."::update", LOG_DEBUG);
865  $result = $this->db->query($sql);
866  if ($result) {
867  // Actions on extra fields (by external module or standard code)
868  if (!$error) {
869  $result = $this->insertExtraFields();
870  if ($result < 0) {
871  $error++;
872  }
873  }
874 
875  if (!$error && !$notrigger) {
876  // Call trigger
877  $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
878  if ($result < 0) {
879  $error++;
880  }
881  // End call triggers
882  }
883  } else {
884  $error++;
885  $this->error = $this->db->lasterror();
886  dol_print_error($this->db);
887  }
888 
889  if (!$error) {
890  $this->db->commit();
891  return $this->id;
892  } else {
893  $this->db->rollback();
894  return -1 * $error;
895  }
896  }
897 
898 
899  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
906  public function update_bban(User $user = null)
907  {
908  // phpcs:enable
909  global $conf, $langs;
910 
911  // Load library to get BAN control function
912  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
913 
914  dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
915 
916  // Check parameters
917  if (!$this->ref) {
918  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
919  return -2;
920  }
921 
922  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
923  $sql .= " bank = '".$this->db->escape($this->bank)."'";
924  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
925  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
926  $sql .= ",number='".$this->db->escape($this->number)."'";
927  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
928  $sql .= ",bic='".$this->db->escape($this->bic)."'";
929  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
930  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
931  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
932  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
933  $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null");
934  $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null");
935  $sql .= " WHERE rowid = ".((int) $this->id);
936  $sql .= " AND entity = ".((int) $conf->entity);
937 
938  dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
939 
940  $result = $this->db->query($sql);
941  if ($result) {
942  return 1;
943  } else {
944  $this->error = $this->db->lasterror();
945  dol_print_error($this->db);
946  return -1;
947  }
948  }
949 
950 
958  public function fetch($id, $ref = '')
959  {
960  global $conf;
961 
962  if (empty($id) && empty($ref)) {
963  $this->error = "ErrorBadParameters";
964  return -1;
965  }
966 
967  $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,";
968  $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
969  $sql .= " ba.domiciliation, ba.pti_in_ctti, ba.proprio, ba.owner_address, ba.state_id, ba.fk_pays as country_id,";
970  $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
971  $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
972  $sql .= " ba.datec as date_creation, ba.tms as date_update, ba.ics, ba.ics_transfer,";
973  $sql .= ' c.code as country_code, c.label as country,';
974  $sql .= ' d.code_departement as state_code, d.nom as state';
975  $sql .= ' , aj.code as accountancy_journal';
976  $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
977  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
978  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
979  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal';
980  $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
981  if ($id) {
982  $sql .= " AND ba.rowid = ".((int) $id);
983  }
984  if ($ref) {
985  $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
986  }
987 
988  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
989  $result = $this->db->query($sql);
990  if ($result) {
991  if ($this->db->num_rows($result)) {
992  $obj = $this->db->fetch_object($result);
993 
994  $this->id = $obj->rowid;
995  $this->rowid = $obj->rowid;
996  $this->ref = $obj->ref;
997  $this->label = $obj->label;
998  $this->type = $obj->courant;
999  $this->courant = $obj->courant;
1000  $this->bank = $obj->bank;
1001  $this->clos = $obj->clos;
1002  $this->rappro = $obj->rappro;
1003  $this->url = $obj->url;
1004 
1005  $this->code_banque = $obj->code_banque;
1006  $this->code_guichet = $obj->code_guichet;
1007  $this->number = $obj->number;
1008  $this->cle_rib = $obj->cle_rib;
1009  $this->bic = $obj->bic;
1010  $this->iban = $obj->iban;
1011  $this->domiciliation = $obj->domiciliation;
1012  $this->pti_in_ctti = $obj->pti_in_ctti;
1013  $this->proprio = $obj->proprio;
1014  $this->owner_address = $obj->owner_address;
1015 
1016  $this->state_id = $obj->state_id;
1017  $this->state_code = $obj->state_code;
1018  $this->state = $obj->state;
1019 
1020  $this->country_id = $obj->country_id;
1021  $this->country_code = $obj->country_code;
1022  $this->country = $obj->country;
1023 
1024  $this->account_number = $obj->account_number;
1025  $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
1026  $this->accountancy_journal = $obj->accountancy_journal;
1027 
1028  $this->currency_code = $obj->currency_code;
1029  $this->account_currency_code = $obj->currency_code;
1030  $this->min_allowed = $obj->min_allowed;
1031  $this->min_desired = $obj->min_desired;
1032  $this->comment = $obj->comment;
1033 
1034  $this->date_creation = $this->db->jdate($obj->date_creation);
1035  $this->date_update = $this->db->jdate($obj->date_update);
1036 
1037  $this->ics = $obj->ics;
1038  $this->ics_transfer = $obj->ics_transfer;
1039 
1040  // Retrieve all extrafield
1041  // fetch optionals attributes and labels
1042  $this->fetch_optionals();
1043 
1044  return 1;
1045  } else {
1046  return 0;
1047  }
1048  } else {
1049  $this->error = $this->db->lasterror;
1050  $this->errors[] = $this->error;
1051  return -1;
1052  }
1053  }
1054 
1065  public function setCategories($categories)
1066  {
1067  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1068  return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
1069  }
1070 
1077  public function delete(User $user = null)
1078  {
1079  global $conf;
1080 
1081  $error = 0;
1082 
1083  $this->db->begin();
1084 
1085  // @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
1086 
1087  // Delete link between tag and bank account
1088  if (!$error) {
1089  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1090  $sql .= " WHERE fk_account = ".((int) $this->id);
1091 
1092  $resql = $this->db->query($sql);
1093  if (!$resql) {
1094  $error++;
1095  $this->error = "Error ".$this->db->lasterror();
1096  }
1097  }
1098 
1099  if (!$error) {
1100  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account";
1101  $sql .= " WHERE rowid = ".((int) $this->rowid);
1102 
1103  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1104  $result = $this->db->query($sql);
1105  if ($result) {
1106  // Remove extrafields
1107  if (!$error) {
1108  $result = $this->deleteExtraFields();
1109  if ($result < 0) {
1110  $error++;
1111  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1112  }
1113  }
1114  } else {
1115  $error++;
1116  $this->error = "Error ".$this->db->lasterror();
1117  }
1118  }
1119 
1120  if (!$error) {
1121  $this->db->commit();
1122  return 1;
1123  } else {
1124  $this->db->rollback();
1125  return -1;
1126  }
1127  }
1128 
1129 
1136  public function getLibStatut($mode = 0)
1137  {
1138  return $this->LibStatut($this->clos, $mode);
1139  }
1140 
1141  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1149  public function LibStatut($status, $mode = 0)
1150  {
1151  // phpcs:enable
1152  global $langs;
1153  $langs->load('banks');
1154 
1155  if ($status == self::STATUS_OPEN) {
1156  $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1157  $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1158  $statusType = 'status4';
1159  } else {
1160  $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1161  $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1162  $statusType = 'status5';
1163  }
1164 
1165  return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1166  }
1167 
1168 
1169  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1175  public function can_be_deleted()
1176  {
1177  // phpcs:enable
1178  $can_be_deleted = false;
1179 
1180  $sql = "SELECT COUNT(rowid) as nb";
1181  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1182  $sql .= " WHERE fk_account = ".((int) $this->id);
1183 
1184  $resql = $this->db->query($sql);
1185  if ($resql) {
1186  $obj = $this->db->fetch_object($resql);
1187  if ($obj->nb <= 1) {
1188  $can_be_deleted = true; // Juste le solde
1189  }
1190  } else {
1191  dol_print_error($this->db);
1192  }
1193  return $can_be_deleted;
1194  }
1195 
1196 
1202  public function error()
1203  {
1204  return $this->error;
1205  }
1206 
1215  public function solde($option = 0, $date_end = '', $field = 'dateo')
1216  {
1217  $solde = 0;
1218 
1219  $sql = "SELECT sum(amount) as amount";
1220  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1221  $sql .= " WHERE fk_account = ".((int) $this->id);
1222  if ($option == 1) {
1223  $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1224  }
1225 
1226  $resql = $this->db->query($sql);
1227  if ($resql) {
1228  if ($this->db->num_rows($resql)) {
1229  $obj = $this->db->fetch_object($resql);
1230  $solde = $obj->amount;
1231  }
1232  $this->db->free($resql);
1233  } else {
1234  $this->errors[] = $this->db->lasterror;
1235  return -1;
1236  }
1237 
1238  return price2num($solde, 'MU');
1239  }
1240 
1241  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1249  public function load_board(User $user, $filteraccountid = 0)
1250  {
1251  // phpcs:enable
1252  global $conf, $langs;
1253 
1254  if ($user->socid) {
1255  return -1; // protection pour eviter appel par utilisateur externe
1256  }
1257 
1258  $sql = "SELECT b.rowid, b.datev as datefin";
1259  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1260  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1261  $sql .= " WHERE b.rappro=0";
1262  $sql .= " AND b.fk_account = ba.rowid";
1263  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1264  $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1265  $sql .= " AND clos = 0";
1266  if ($filteraccountid) {
1267  $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1268  }
1269 
1270  $resql = $this->db->query($sql);
1271  if ($resql) {
1272  $langs->load("banks");
1273  $now = dol_now();
1274 
1275  require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1276 
1277  $response = new WorkboardResponse();
1278  $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1279  $response->label = $langs->trans("TransactionsToConciliate");
1280  $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1281  $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1282  $response->img = img_object('', "payment");
1283 
1284  while ($obj = $this->db->fetch_object($resql)) {
1285  $response->nbtodo++;
1286  if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1287  $response->nbtodolate++;
1288  }
1289  }
1290 
1291  return $response;
1292  } else {
1293  dol_print_error($this->db);
1294  $this->error = $this->db->error();
1295  return -1;
1296  }
1297  }
1298 
1299  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1305  public function load_state_board($filteraccountid = 0)
1306  {
1307  // phpcs:enable
1308  global $user;
1309 
1310  if ($user->socid) {
1311  return -1; // protection pour eviter appel par utilisateur externe
1312  }
1313 
1314  $sql = "SELECT count(b.rowid) as nb";
1315  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1316  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1317  $sql .= " WHERE b.fk_account = ba.rowid";
1318  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1319  $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1320  $sql .= " AND clos = 0";
1321  if ($filteraccountid) {
1322  $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1323  }
1324 
1325  $resql = $this->db->query($sql);
1326  if ($resql) {
1327  while ($obj = $this->db->fetch_object($resql)) {
1328  $this->nb["banklines"] = $obj->nb;
1329  }
1330  $this->db->free($resql);
1331  } else {
1332  dol_print_error($this->db);
1333  $this->error = $this->db->error();
1334  return -1;
1335  }
1336  }
1337 
1338 
1344  public function countAccountToReconcile()
1345  {
1346  global $db, $conf, $user;
1347 
1348  //Protection against external users
1349  if ($user->socid) {
1350  return 0;
1351  }
1352 
1353  $nb = 0;
1354 
1355  $sql = "SELECT COUNT(ba.rowid) as nb";
1356  $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1357  $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1358  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1359  if (empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
1360  $sql .= " AND ba.courant != 2";
1361  }
1362  $resql = $this->db->query($sql);
1363  if ($resql) {
1364  $obj = $this->db->fetch_object($resql);
1365  $nb = $obj->nb;
1366  } else {
1367  dol_print_error($this->db);
1368  }
1369 
1370  return $nb;
1371  }
1372 
1383  public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0)
1384  {
1385  global $conf, $langs, $user;
1386 
1387  $result = '';
1388  $label = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
1389  if (isset($this->status)) {
1390  $label .= ' '.$this->getLibStatut(5);
1391  }
1392  $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1393  $label .= '<br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
1394  $label .= '<br><b>'.$langs->trans('IBAN').':</b> '.$this->iban;
1395  $label .= '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
1396  $label .= '<br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
1397 
1398  if (empty($user->rights->banque->lire) || !empty($user->socid)) {
1399  $option = 'nolink';
1400  }
1401 
1402  if (isModEnabled('accounting')) {
1403  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1404  $langs->load("accountancy");
1405  $label .= '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
1406  $label .= '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
1407  }
1408 
1409  $linkclose = '" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1410 
1411  $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1412  if ($mode == 'transactions') {
1413  $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1414  } elseif ($mode == 'receipts') {
1415  $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1416  }
1417 
1418  if ($option != 'nolink') {
1419  // Add param to save lastsearch_values or not
1420  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1421  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1422  $add_save_lastsearch_values = 1;
1423  }
1424  if ($add_save_lastsearch_values) {
1425  $url .= '&save_lastsearch_values=1';
1426  }
1427  }
1428 
1429  $linkstart = '<a href="'.$url.$linkclose;
1430  $linkend = '</a>';
1431 
1432  if ($option == 'nolink') {
1433  $linkstart = '';
1434  $linkend = '';
1435  }
1436 
1437  $result .= $linkstart;
1438  if ($withpicto) {
1439  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1440  }
1441  if ($withpicto != 2) {
1442  $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1443  }
1444  $result .= $linkend;
1445 
1446  return $result;
1447  }
1448 
1449 
1450  // Method after here are common to Account and CompanyBankAccount
1451 
1452 
1458  public function verif()
1459  {
1460  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1461 
1462  $this->error_number = 0;
1463 
1464  // Call function to check BAN
1465 
1466  if (!checkIbanForAccount($this)) {
1467  $this->error_number = 12;
1468  $this->error_message = 'IBANNotValid';
1469  }
1470  if (!checkSwiftForAccount($this)) {
1471  $this->error_number = 12;
1472  $this->error_message = 'SwiftNotValid';
1473  }
1474  /*if (! checkBanForAccount($this))
1475  {
1476  $this->error_number = 12;
1477  $this->error_message = 'BANControlError';
1478  }*/
1479 
1480  if ($this->error_number == 0) {
1481  return 1;
1482  } else {
1483  return 0;
1484  }
1485  }
1486 
1492  public function getCountryCode()
1493  {
1494  global $mysoc;
1495 
1496  // We return country code of bank account
1497  if (!empty($this->country_code)) {
1498  return $this->country_code;
1499  }
1500 
1501  // For backward compatibility, we try to guess country from other information
1502  if (!empty($this->iban)) {
1503  // If IBAN defined, we can know country of account from it
1504  $reg = array();
1505  if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
1506  return $reg[1];
1507  }
1508  }
1509 
1510  // If this class is linked to a third party
1511  if (!empty($this->socid)) {
1512  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
1513  $company = new Societe($this->db);
1514  $result = $company->fetch($this->socid);
1515  if (!empty($company->country_code)) {
1516  return $company->country_code;
1517  }
1518  }
1519 
1520  // We return country code of managed company
1521  if (!empty($mysoc->country_code)) {
1522  return $mysoc->country_code;
1523  }
1524 
1525  return '';
1526  }
1527 
1536  public function useDetailedBBAN()
1537  {
1538  $country_code = $this->getCountryCode();
1539 
1540  if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
1541  return 1; // France, Spain, Gabon, ... - Not valid for CH
1542  }
1543  if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
1544  return 2; // Australia, England...
1545  }
1546  return 0;
1547  }
1548 
1554  public function needIBAN()
1555  {
1556  global $conf;
1557 
1558  if (!empty($conf->global->MAIN_IBAN_IS_NEVER_MANDATORY)) {
1559  return 0;
1560  }
1561 
1562  $country_code = $this->getCountryCode();
1563 
1564  $country_code_in_EEC = array(
1565  'AT', // Austria
1566  'BE', // Belgium
1567  'BG', // Bulgaria
1568  'CY', // Cyprus
1569  'CZ', // Czech republic
1570  'DE', // Germany
1571  'DK', // Danemark
1572  'EE', // Estonia
1573  'ES', // Spain
1574  'FI', // Finland
1575  'FR', // France
1576  'GB', // United Kingdom
1577  'GR', // Greece
1578  'HR', // Croatia
1579  'NL', // Holland
1580  'HU', // Hungary
1581  'IE', // Ireland
1582  'IM', // Isle of Man - Included in UK
1583  'IT', // Italy
1584  'LT', // Lithuania
1585  'LU', // Luxembourg
1586  'LV', // Latvia
1587  'MC', // Monaco - Included in France
1588  'MT', // Malta
1589  //'NO', // Norway
1590  'PL', // Poland
1591  'PT', // Portugal
1592  'RO', // Romania
1593  'SE', // Sweden
1594  'SK', // Slovakia
1595  'SI', // Slovenia
1596  'UK', // United Kingdom
1597  //'CH', // Switzerland - No. Swizerland in not in EEC
1598  );
1599 
1600  if (in_array($country_code, $country_code_in_EEC)) {
1601  return 1; // France, Spain, ...
1602  }
1603  return 0;
1604  }
1605 
1612  public function info($id)
1613  {
1614  }
1615 
1630  public function getFieldsToShow($includeibanbic = 0)
1631  {
1632  //Get the required properties depending on the country
1633  $detailedBBAN = $this->useDetailedBBAN();
1634 
1635  if ($detailedBBAN == 0) {
1636  $fieldarray = array(
1637  'BankAccountNumber'
1638  );
1639  } elseif ($detailedBBAN == 2) {
1640  $fieldarray = array(
1641  'BankCode',
1642  'BankAccountNumber'
1643  );
1644  } else {
1645  $fieldarray = self::getAccountNumberOrder();
1646  }
1647 
1648  //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1649  if ($includeibanbic) {
1650  $fieldarray[] = 'IBAN';
1651  $fieldarray[] = 'BIC';
1652  }
1653  //}
1654 
1655  //Get the order the properties are shown
1656  return $fieldarray;
1657  }
1658 
1669  public static function getAccountNumberOrder()
1670  {
1671  global $conf;
1672 
1673  $fieldlists = array(
1674  'BankCode',
1675  'DeskCode',
1676  'BankAccountNumber',
1677  'BankAccountNumberKey'
1678  );
1679 
1680  if (!empty($conf->global->BANK_SHOW_ORDER_OPTION)) {
1681  if (is_numeric($conf->global->BANK_SHOW_ORDER_OPTION)) {
1682  if ($conf->global->BANK_SHOW_ORDER_OPTION == '1') {
1683  $fieldlists = array(
1684  'BankCode',
1685  'DeskCode',
1686  'BankAccountNumberKey',
1687  'BankAccountNumber'
1688  );
1689  }
1690  } else {
1691  //Replace the old AccountNumber key with the new BankAccountNumber key
1692  $fieldlists = explode(
1693  ' ',
1694  preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
1695  );
1696  }
1697  }
1698 
1699  return $fieldlists;
1700  }
1701 
1702 
1710  public function initAsSpecimen()
1711  {
1712  // Example of IBAN FR7630001007941234567890185
1713  $this->specimen = 1;
1714  $this->ref = 'MBA';
1715  $this->label = 'My Big Company Bank account';
1716  $this->bank = 'MyBank';
1717  $this->courant = Account::TYPE_CURRENT;
1718  $this->clos = Account::STATUS_OPEN;
1719  $this->code_banque = '30001';
1720  $this->code_guichet = '00794';
1721  $this->number = '12345678901';
1722  $this->cle_rib = '85';
1723  $this->bic = 'AA12';
1724  $this->iban = 'FR7630001007941234567890185';
1725  $this->domiciliation = 'Banque de France';
1726  $this->proprio = 'Owner';
1727  $this->owner_address = 'Owner address';
1728  $this->country_id = 1;
1729  }
1730 
1739  public static function replaceThirdparty($dbs, $origin_id, $dest_id)
1740  {
1741  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'";
1742 
1743  if ($dbs->query($sql)) {
1744  return true;
1745  } else {
1746  //if ($ignoreerrors) return true; // TODO Not enough. If there is A-B on kept thirdparty and B-C on old one, we must get A-B-C after merge. Not A-B.
1747  //$this->errors = $dbs->lasterror();
1748  return false;
1749  }
1750  }
1751 }
1752 
1753 
1758 {
1762  public $error = '';
1763 
1767  public $db;
1768 
1772  public $element = 'bank';
1773 
1777  public $table_element = 'bank';
1778 
1782  public $picto = 'accountline';
1783 
1787  public $id;
1788 
1792  public $ref;
1793 
1799  public $datec;
1800 
1806  public $dateo;
1807 
1813  public $datev;
1814 
1815  public $amount; /* Amount of payment in the bank account currency */
1816  public $amount_main_currency; /* Amount in the currency of company if bank account use another currency */
1817 
1821  public $fk_user_author;
1822 
1826  public $fk_user_rappro;
1827 
1831  public $fk_type;
1832 
1836  public $fk_bordereau;
1837 
1841  public $fk_account;
1842 
1846  public $bank_account_ref;
1847 
1851  public $bank_account_label;
1852 
1856  public $numero_compte;
1857 
1861  public $emetteur;
1862 
1863  public $rappro; // Is it conciliated
1864  public $num_releve; // If conciliated, what is bank statement
1865  public $num_chq; // Num of cheque
1866  public $bank_chq; // Bank of cheque
1867 
1871  public $label;
1872 
1873  public $note;
1874 
1875 
1876 
1882  public function __construct(DoliDB $db)
1883  {
1884  $this->db = $db;
1885  }
1886 
1895  public function fetch($rowid, $ref = '', $num = '')
1896  {
1897  global $conf;
1898 
1899  // Check parameters
1900  if (empty($rowid) && empty($ref) && empty($num)) {
1901  return -1;
1902  }
1903 
1904  $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
1905  $sql .= " b.fk_user_author, b.fk_user_rappro,";
1906  $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
1907  $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
1908  $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
1909  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1910  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1911  $sql .= " WHERE b.fk_account = ba.rowid";
1912  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1913  if ($num) {
1914  $sql .= " AND b.num_chq='".$this->db->escape($num)."'";
1915  } elseif ($ref) {
1916  $sql .= " AND b.rowid='".$this->db->escape($ref)."'";
1917  } else {
1918  $sql .= " AND b.rowid = ".((int) $rowid);
1919  }
1920 
1921  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1922  $result = $this->db->query($sql);
1923  if ($result) {
1924  $ret = 0;
1925 
1926  $obj = $this->db->fetch_object($result);
1927  if ($obj) {
1928  $this->id = $obj->rowid;
1929  $this->rowid = $obj->rowid;
1930  $this->ref = $obj->rowid;
1931 
1932  $this->datec = $obj->datec;
1933  $this->datev = $obj->datev;
1934  $this->dateo = $obj->dateo;
1935  $this->amount = $obj->amount;
1936  $this->label = $obj->label;
1937  $this->note = $obj->note;
1938 
1939  $this->fk_user_author = $obj->fk_user_author;
1940  $this->fk_user_rappro = $obj->fk_user_rappro;
1941 
1942  $this->fk_type = $obj->fk_type; // Type of transaction
1943  $this->rappro = $obj->rappro;
1944  $this->num_releve = $obj->num_releve;
1945 
1946  $this->num_chq = $obj->num_chq;
1947  $this->bank_chq = $obj->banque;
1948  $this->fk_bordereau = $obj->fk_bordereau;
1949 
1950  $this->fk_account = $obj->fk_account;
1951  $this->bank_account_ref = $obj->bank_account_ref;
1952  $this->bank_account_label = $obj->bank_account_label;
1953 
1954  $ret = 1;
1955  }
1956  $this->db->free($result);
1957  return $ret;
1958  } else {
1959  return -1;
1960  }
1961  }
1962 
1968  public function insert()
1969  {
1970  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
1971  $sql .= "datec";
1972  $sql .= ", dateo";
1973  $sql .= ", datev";
1974  $sql .= ", label";
1975  $sql .= ", amount";
1976  $sql .= ", amount_main_currency";
1977  $sql .= ", fk_user_author";
1978  $sql .= ", num_chq";
1979  $sql .= ", fk_account";
1980  $sql .= ", fk_type";
1981  $sql .= ", emetteur,banque";
1982  $sql .= ", rappro";
1983  $sql .= ", numero_compte";
1984  $sql .= ", num_releve";
1985  $sql .= ") VALUES (";
1986  $sql .= "'".$this->db->idate($this->datec)."'";
1987  $sql .= ", '".$this->db->idate($this->dateo)."'";
1988  $sql .= ", '".$this->db->idate($this->datev)."'";
1989  $sql .= ", '".$this->db->escape($this->label)."'";
1990  $sql .= ", ".price2num($this->amount);
1991  $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
1992  $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
1993  $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
1994  $sql .= ", '".$this->db->escape($this->fk_account)."'";
1995  $sql .= ", '".$this->db->escape($this->fk_type)."'";
1996  $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
1997  $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
1998  $sql .= ", ".(int) $this->rappro;
1999  $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2000  $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2001  $sql .= ")";
2002 
2003  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2004  $resql = $this->db->query($sql);
2005 
2006  if (!$resql) {
2007  $this->error = $this->db->lasterror();
2008  return -1;
2009  }
2010 
2011  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2012 
2013  return $this->id;
2014  }
2015 
2022  public function delete(User $user = null)
2023  {
2024  global $conf;
2025 
2026  $nbko = 0;
2027 
2028  if ($this->rappro) {
2029  // Protection to avoid any delete of consolidated lines
2030  $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2031  return -1;
2032  }
2033 
2034  $this->db->begin();
2035 
2036  // Protection to avoid any delete of accounted lines. Protection on by default
2037  if (empty($conf->global->BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING)) {
2038  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2039  $resql = $this->db->query($sql);
2040  if ($resql) {
2041  $obj = $this->db->fetch_object($resql);
2042  if ($obj && $obj->nb) {
2043  $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2044  $this->db->rollback();
2045  return -1;
2046  }
2047  } else {
2048  $this->error = $this->db->lasterror();
2049  $this->db->rollback();
2050  return -1;
2051  }
2052  }
2053 
2054  // Delete urls
2055  $result = $this->delete_urls($user);
2056  if ($result < 0) {
2057  $nbko++;
2058  }
2059 
2060  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".(int) $this->rowid;
2061  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2062  $result = $this->db->query($sql);
2063  if (!$result) {
2064  $nbko++;
2065  }
2066 
2067  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2068  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2069  $result = $this->db->query($sql);
2070  if (!$result) {
2071  $nbko++;
2072  }
2073 
2074  if (!$nbko) {
2075  $this->db->commit();
2076  return 1;
2077  } else {
2078  $this->db->rollback();
2079  return -$nbko;
2080  }
2081  }
2082 
2083 
2084  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2091  public function delete_urls(User $user = null)
2092  {
2093  // phpcs:enable
2094  $nbko = 0;
2095 
2096  if ($this->rappro) {
2097  // Protection to avoid any delete of consolidated lines
2098  $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2099  return -1;
2100  }
2101 
2102  $this->db->begin();
2103 
2104  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2105  dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
2106  $result = $this->db->query($sql);
2107  if (!$result) {
2108  $nbko++;
2109  }
2110 
2111  if (!$nbko) {
2112  $this->db->commit();
2113  return 1;
2114  } else {
2115  $this->db->rollback();
2116  return -$nbko;
2117  }
2118  }
2119 
2120 
2128  public function update(User $user, $notrigger = 0)
2129  {
2130  $this->db->begin();
2131 
2132  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2133  $sql .= " amount = ".price2num($this->amount).",";
2134  $sql .= " datev='".$this->db->idate($this->datev)."',";
2135  $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2136  $sql .= " WHERE rowid = ".((int) $this->rowid);
2137 
2138  dol_syslog(get_class($this)."::update", LOG_DEBUG);
2139  $resql = $this->db->query($sql);
2140  if ($resql) {
2141  $this->db->commit();
2142  return 1;
2143  } else {
2144  $this->db->rollback();
2145  $this->error = $this->db->error();
2146  return -1;
2147  }
2148  }
2149 
2150 
2151  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2160  public function update_conciliation(User $user, $cat, $conciliated = 1)
2161  {
2162  // phpcs:enable
2163  global $conf, $langs;
2164 
2165  $this->db->begin();
2166 
2167  // Check statement field
2168  if (!empty($conf->global->BANK_STATEMENT_REGEX_RULE)) {
2169  if (!preg_match('/'.$conf->global->BANK_STATEMENT_REGEX_RULE.'/', $this->num_releve)) {
2170  $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", $conf->global->BANK_STATEMENT_REGEX_RULE);
2171  return -1;
2172  }
2173  }
2174 
2175  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2176  $sql .= " rappro = ".((int) $conciliated);
2177  $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2178  if ($conciliated) {
2179  $sql .= ", fk_user_rappro = ".$user->id;
2180  }
2181  $sql .= " WHERE rowid = ".((int) $this->id);
2182 
2183  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2184  $resql = $this->db->query($sql);
2185  if ($resql) {
2186  if (!empty($cat)) {
2187  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
2188  $sql .= "lineid";
2189  $sql .= ", fk_categ";
2190  $sql .= ") VALUES (";
2191  $sql .= $this->id;
2192  $sql .= ", ".((int) $cat);
2193  $sql .= ")";
2194 
2195  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2196  $this->db->query($sql);
2197 
2198  // No error check. Can fail if category already affected
2199  }
2200 
2201  $this->rappro = 1;
2202 
2203  $this->db->commit();
2204  return 1;
2205  } else {
2206  $this->db->rollback();
2207  return -1;
2208  }
2209  }
2210 
2211 
2212  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2220  public function datev_change($rowid, $sign = 1)
2221  {
2222  // phpcs:enable
2223  $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2224  $resql = $this->db->query($sql);
2225  if ($resql) {
2226  $obj = $this->db->fetch_object($resql);
2227  $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2228 
2229  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2230  $sql .= " datev = '".$this->db->idate($newdate)."'";
2231  $sql .= " WHERE rowid = ".((int) $rowid);
2232 
2233  $result = $this->db->query($sql);
2234  if ($result) {
2235  if ($this->db->affected_rows($result)) {
2236  return 1;
2237  }
2238  } else {
2239  dol_print_error($this->db);
2240  return 0;
2241  }
2242  } else {
2243  dol_print_error($this->db);
2244  }
2245  return 0;
2246  }
2247 
2248  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2255  public function datev_next($id)
2256  {
2257  // phpcs:enable
2258  return $this->datev_change($id, 1);
2259  }
2260 
2261  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2268  public function datev_previous($id)
2269  {
2270  // phpcs:enable
2271  return $this->datev_change($id, -1);
2272  }
2273 
2274 
2275  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2283  public function dateo_change($rowid, $sign = 1)
2284  {
2285  // phpcs:enable
2286  $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2287  $resql = $this->db->query($sql);
2288  if ($resql) {
2289  $obj = $this->db->fetch_object($resql);
2290  $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2291 
2292  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2293  $sql .= " dateo = '".$this->db->idate($newdate)."'";
2294  $sql .= " WHERE rowid = ".((int) $rowid);
2295 
2296  $result = $this->db->query($sql);
2297  if ($result) {
2298  if ($this->db->affected_rows($result)) {
2299  return 1;
2300  }
2301  } else {
2302  dol_print_error($this->db);
2303  return 0;
2304  }
2305  } else {
2306  dol_print_error($this->db);
2307  }
2308  return 0;
2309  }
2310 
2311  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2318  public function dateo_next($id)
2319  {
2320  // phpcs:enable
2321  return $this->dateo_change($id, 1);
2322  }
2323 
2324  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2331  public function dateo_previous($id)
2332  {
2333  // phpcs:enable
2334  return $this->dateo_change($id, -1);
2335  }
2336 
2337 
2344  public function info($id)
2345  {
2346  $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2347  $sql .= ' b.fk_user_author, b.fk_user_rappro';
2348  $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2349  $sql .= ' WHERE b.rowid = '.((int) $id);
2350 
2351  $result = $this->db->query($sql);
2352  if ($result) {
2353  if ($this->db->num_rows($result)) {
2354  $obj = $this->db->fetch_object($result);
2355  $this->id = $obj->rowid;
2356 
2357  if ($obj->fk_user_author) {
2358  $cuser = new User($this->db);
2359  $cuser->fetch($obj->fk_user_author);
2360  $this->user_creation = $cuser;
2361  }
2362  if ($obj->fk_user_rappro) {
2363  $ruser = new User($this->db);
2364  $ruser->fetch($obj->fk_user_rappro);
2365  $this->user_rappro = $ruser;
2366  }
2367 
2368  $this->date_creation = $this->db->jdate($obj->datec);
2369  $this->date_modification = $this->db->jdate($obj->datem);
2370  //$this->date_rappro = $obj->daterappro; // Not yet managed
2371  }
2372  $this->db->free($result);
2373  } else {
2374  dol_print_error($this->db);
2375  }
2376  }
2377 
2378 
2388  public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2389  {
2390  global $langs;
2391 
2392  $result = '';
2393 
2394  $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2395  $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2396 
2397  $linkstart = '<a href="'.DOL_URL_ROOT.'/compta/bank/line.php?rowid='.((int) $this->id).'&save_lastsearch_values=1" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
2398  $linkend = '</a>';
2399 
2400  $result .= $linkstart;
2401  if ($withpicto) {
2402  $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);
2403  }
2404  if ($withpicto != 2) {
2405  $result .= ($this->ref ? $this->ref : $this->id);
2406  }
2407  $result .= $linkend;
2408 
2409  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2410  $result .= ' <span class="opacitymedium">(';
2411  }
2412  if ($option == 'showall') {
2413  $result .= $langs->trans("BankAccount").': ';
2414  $accountstatic = new Account($this->db);
2415  $accountstatic->id = $this->fk_account;
2416  $accountstatic->ref = $this->bank_account_ref;
2417  $accountstatic->label = $this->bank_account_label;
2418  $result .= $accountstatic->getNomUrl(0).', ';
2419  }
2420  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2421  $result .= $langs->trans("BankLineConciliated").': ';
2422  $result .= yn($this->rappro);
2423  }
2424  if ($option == 'showall' || $option == 'showconciliatedandaccounted') {
2425  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2426  $resql = $this->db->query($sql);
2427  if ($resql) {
2428  $obj = $this->db->fetch_object($resql);
2429  if ($obj && $obj->nb) {
2430  $result .= ' - '.$langs->trans("Accounted").': '.yn(1);
2431  } else {
2432  $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2433  }
2434  }
2435  }
2436  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2437  $result .= ')</span>';
2438  }
2439 
2440  return $result;
2441  }
2442 
2443 
2450  public function getLibStatut($mode = 0)
2451  {
2452  return $this->LibStatut($this->status, $mode);
2453  }
2454 
2455  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2463  public function LibStatut($status, $mode = 0)
2464  {
2465  // phpcs:enable
2466  global $langs;
2467  //$langs->load('companies');
2468  /*
2469  if ($mode == 0)
2470  {
2471  if ($status==0) return $langs->trans("ActivityCeased");
2472  if ($status==1) return $langs->trans("InActivity");
2473  }
2474  if ($mode == 1)
2475  {
2476  if ($status==0) return $langs->trans("ActivityCeased");
2477  if ($status==1) return $langs->trans("InActivity");
2478  }
2479  if ($mode == 2)
2480  {
2481  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2482  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2483  }
2484  if ($mode == 3)
2485  {
2486  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2487  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2488  }
2489  if ($mode == 4)
2490  {
2491  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2492  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2493  }
2494  if ($mode == 5)
2495  {
2496  if ($status==0) return $langs->trans("ActivityCeased").' '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2497  if ($status==1) return $langs->trans("InActivity").' '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2498  }*/
2499  }
2500 
2501 
2507  public function getVentilExportCompta()
2508  {
2509  $alreadydispatched = 0;
2510 
2511  $type = 'bank';
2512 
2513  $sql = " SELECT COUNT(ab.rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type='".$this->db->escape($type)."' AND ab.fk_doc = ".((int) $this->id);
2514  $resql = $this->db->query($sql);
2515  if ($resql) {
2516  $obj = $this->db->fetch_object($resql);
2517  if ($obj) {
2518  $alreadydispatched = $obj->nb;
2519  }
2520  } else {
2521  $this->error = $this->db->lasterror();
2522  return -1;
2523  }
2524 
2525  if ($alreadydispatched) {
2526  return 1;
2527  }
2528  return 0;
2529  }
2530 }
Account\getCountryCode
getCountryCode()
Return account country code.
Definition: account.class.php:1492
Account\TYPE_CURRENT
const TYPE_CURRENT
Current account.
Definition: account.class.php:328
Account\load_state_board
load_state_board($filteraccountid=0)
Charge indicateurs this->nb de tableau de bord.
Definition: account.class.php:1305
AccountLine\fetch
fetch($rowid, $ref='', $num='')
Load into memory content of a bank transaction line.
Definition: account.class.php:1895
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
db
$conf db
API class for accounts.
Definition: inc.php:41
yn
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
Definition: functions.lib.php:6477
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1454
Account\verif
verif()
Return if an account has valid information for Direct debit payment.
Definition: account.class.php:1458
AccountLine\getLibStatut
getLibStatut($mode=0)
Return label of status (activity, closed)
Definition: account.class.php:2450
Account\update
update(User $user, $notrigger=0)
Update bank account card.
Definition: account.class.php:805
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Account\countAccountToReconcile
countAccountToReconcile()
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
Definition: account.class.php:1344
AccountLine\update_conciliation
update_conciliation(User $user, $cat, $conciliated=1)
Update conciliation field.
Definition: account.class.php:2160
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4830
checkIbanForAccount
checkIbanForAccount(Account $account)
Check IBAN number informations for a bank account.
Definition: bank.lib.php:277
Account\get_url
get_url($fk_bank='', $url_id='', $type='')
TODO Move this into AccountLine Return array with links from llx_bank_url.
Definition: account.class.php:466
checkSwiftForAccount
checkSwiftForAccount($account)
Check SWIFT informations for a bank account.
Definition: bank.lib.php:261
ref
$object ref
Definition: info.php:77
rowid
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
Definition: email_expire_services_to_representatives.php:79
Account\info
info($id)
Load miscellaneous information for tab "Info".
Definition: account.class.php:1612
Account\setCategories
setCategories($categories)
Sets object to supplied categories.
Definition: account.class.php:1065
Account\load_board
load_board(User $user, $filteraccountid=0)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
Definition: account.class.php:1249
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
AccountLine\dateo_change
dateo_change($rowid, $sign=1)
Increase/decrease operation date of a rowid.
Definition: account.class.php:2283
Account\can_be_deleted
can_be_deleted()
Renvoi si un compte peut etre supprimer ou non (sans mouvements)
Definition: account.class.php:1175
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5647
AccountLine\datev_change
datev_change($rowid, $sign=1)
Increase/decrease value date of a rowid.
Definition: account.class.php:2220
WorkboardResponse
Definition: workboardresponse.class.php:25
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3866
Account\TYPE_CASH
const TYPE_CASH
Cash account.
Definition: account.class.php:332
AccountLine\dateo_next
dateo_next($id)
Increase operation date of a rowid.
Definition: account.class.php:2318
Account\LibStatut
LibStatut($status, $mode=0)
Return label of given object status.
Definition: account.class.php:1149
Account\canBeConciliated
canBeConciliated()
Return if a bank account need to be conciliated.
Definition: account.class.php:401
Account\getNomUrl
getNomUrl($withpicto=0, $mode='', $option='', $save_lastsearch_value=-1, $notooltip=0)
Return clicable name (with picto eventually)
Definition: account.class.php:1383
AccountLine\datev_previous
datev_previous($id)
Decrease value date of a rowid.
Definition: account.class.php:2268
AccountLine\__construct
__construct(DoliDB $db)
Constructor.
Definition: account.class.php:1882
Account\getFieldsToShow
getFieldsToShow($includeibanbic=0)
Returns the fields in order that this bank account should show to the user Will return an array with ...
Definition: account.class.php:1630
Account\addline
addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur='', $banque='', $accountancycode='', $datev=null, $num_releve='', $amount_main_currency=null)
Add an entry into table ".MAIN_DB_PREFIX."bank.
Definition: account.class.php:531
Account\__construct
__construct(DoliDB $db)
Constructor.
Definition: account.class.php:348
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6143
Account\getAccountNumberOrder
static getAccountNumberOrder()
Returns the components of the bank account in order.
Definition: account.class.php:1669
length_accountg
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
Definition: accounting.lib.php:94
Account\error
error()
Return error.
Definition: account.class.php:1202
Account\TYPE_SAVINGS
const TYPE_SAVINGS
Savings account.
Definition: account.class.php:336
AccountLine\getNomUrl
getNomUrl($withpicto=0, $maxlen=0, $option='', $notooltip=0)
Return clickable name (with picto eventually)
Definition: account.class.php:2388
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1589
AccountLine\dateo_previous
dateo_previous($id)
Decrease operation date of a rowid.
Definition: account.class.php:2331
AccountLine\datev_next
datev_next($id)
Increase value date of a rowid.
Definition: account.class.php:2255
AccountLine\getVentilExportCompta
getVentilExportCompta()
Return if a bank line was dispatched into bookkeeping.
Definition: account.class.php:2507
CommonObject\fetch_optionals
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...
Definition: commonobject.class.php:5994
Account\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: account.class.php:1710
Account\__toString
__toString()
Shows the account number in the appropriate format.
Definition: account.class.php:373
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
AccountLine\insert
insert()
Inserts a transaction to a bank account.
Definition: account.class.php:1968
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Account\fetch
fetch($id, $ref='')
Load a bank account into memory from database.
Definition: account.class.php:958
Account\solde
solde($option=0, $date_end='', $field='dateo')
Return current sold.
Definition: account.class.php:1215
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6103
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10324
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4197
AccountLine
Class to manage bank transaction lines.
Definition: account.class.php:1757
Account\replaceThirdparty
static replaceThirdparty($dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: account.class.php:1739
Account\create
create(User $user, $notrigger=0)
Create bank account into database.
Definition: account.class.php:644
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2831
$resql
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
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5778
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
Account\useDetailedBBAN
useDetailedBBAN()
Return if a bank account is defined with detailed information (bank code, desk code,...
Definition: account.class.php:1536
AccountLine\LibStatut
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
Definition: account.class.php:2463
AccountLine\delete_urls
delete_urls(User $user=null)
Delete bank line records.
Definition: account.class.php:2091
Account\getLibStatut
getLibStatut($mode=0)
Return label of object status.
Definition: account.class.php:1136
Account
Class to manage bank accounts.
Definition: account.class.php:38
Account\needIBAN
needIBAN()
Return 1 if IBAN / BIC is mandatory (otherwise option)
Definition: account.class.php:1554
Account\add_url_line
add_url_line($line_id, $url_id, $url, $label, $type)
Add a link between bank line record and its source.
Definition: account.class.php:429
AccountLine\update
update(User $user, $notrigger=0)
Update bank account record in database.
Definition: account.class.php:2128
Account\update_bban
update_bban(User $user=null)
Update BBAN (RIB) account fields.
Definition: account.class.php:906
AccountLine\info
info($id)
Load miscellaneous information for tab "Info".
Definition: account.class.php:2344