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 
248  public $solde;
249 
254  public $balance;
255 
260  public $ics;
261 
266  public $ics_transfer;
267 
268 
269 
294  // BEGIN MODULEBUILDER PROPERTIES
298  public $fields = array(
299  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
300  'ref' =>array('type'=>'varchar(12)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>25),
301  'label' =>array('type'=>'varchar(30)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>30),
302  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>35, 'index'=>1),
303  'bank' =>array('type'=>'varchar(60)', 'label'=>'Bank', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
304  'code_banque' =>array('type'=>'varchar(128)', 'label'=>'Code banque', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
305  'code_guichet' =>array('type'=>'varchar(6)', 'label'=>'Code guichet', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
306  'number' =>array('type'=>'varchar(255)', 'label'=>'Number', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
307  'cle_rib' =>array('type'=>'varchar(5)', 'label'=>'Cle rib', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
308  'bic' =>array('type'=>'varchar(11)', 'label'=>'Bic', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
309  'iban_prefix' =>array('type'=>'varchar(34)', 'label'=>'Iban prefix', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
310  'country_iban' =>array('type'=>'varchar(2)', 'label'=>'Country iban', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
311  'cle_iban' =>array('type'=>'varchar(2)', 'label'=>'Cle iban', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
312  'domiciliation' =>array('type'=>'varchar(255)', 'label'=>'Domiciliation', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
313  'state_id' =>array('type'=>'integer', 'label'=>'State id', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
314  'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>95),
315  'proprio' =>array('type'=>'varchar(60)', 'label'=>'Proprio', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
316  'owner_address' =>array('type'=>'text', 'label'=>'Owner address', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
317  'courant' =>array('type'=>'smallint(6)', 'label'=>'Courant', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>110),
318  'clos' =>array('type'=>'smallint(6)', 'label'=>'Clos', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>115),
319  'rappro' =>array('type'=>'smallint(6)', 'label'=>'Rappro', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
320  'url' =>array('type'=>'varchar(128)', 'label'=>'Url', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
321  'account_number' =>array('type'=>'varchar(32)', 'label'=>'Account number', 'enabled'=>1, 'visible'=>-1, 'position'=>130),
322  'fk_accountancy_journal' =>array('type'=>'integer', 'label'=>'Accountancy journal ID', 'enabled'=>1, 'visible'=>-1, 'position'=>132),
323  'accountancy_journal' =>array('type'=>'varchar(20)', 'label'=>'Accountancy journal', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
324  'currency_code' =>array('type'=>'varchar(3)', 'label'=>'Currency code', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>140),
325  'min_allowed' =>array('type'=>'integer', 'label'=>'Min allowed', 'enabled'=>1, 'visible'=>-1, 'position'=>145),
326  'min_desired' =>array('type'=>'integer', 'label'=>'Min desired', 'enabled'=>1, 'visible'=>-1, 'position'=>150),
327  'comment' =>array('type'=>'text', 'label'=>'Comment', 'enabled'=>1, 'visible'=>-1, 'position'=>155),
328  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>156),
329  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>157),
330  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>160),
331  'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>165),
332  'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>170),
333  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>175),
334  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>180),
335  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
336  );
337  // END MODULEBUILDER PROPERTIES
338 
342  const TYPE_CURRENT = 1;
346  const TYPE_CASH = 2;
350  const TYPE_SAVINGS = 0;
351 
352 
353  const STATUS_OPEN = 0;
354  const STATUS_CLOSED = 1;
355 
356 
362  public function __construct(DoliDB $db)
363  {
364  global $langs;
365 
366  $this->db = $db;
367 
368  $this->solde = 0;
369 
370  $this->type_lib = array(
371  self::TYPE_SAVINGS => $langs->trans("BankType0"),
372  self::TYPE_CURRENT => $langs->trans("BankType1"),
373  self::TYPE_CASH => $langs->trans("BankType2"),
374  );
375 
376  $this->status = array(
377  self::STATUS_OPEN => $langs->trans("StatusAccountOpened"),
378  self::STATUS_CLOSED => $langs->trans("StatusAccountClosed")
379  );
380  }
381 
387  public function __toString()
388  {
389  $string = '';
390  foreach ($this->getFieldsToShow() as $val) {
391  if ($val == 'BankCode') {
392  $string .= $this->code_banque.' ';
393  } elseif ($val == 'BankAccountNumber') {
394  $string .= $this->number.' ';
395  } elseif ($val == 'DeskCode') {
396  $string .= $this->code_guichet.' ';
397  } elseif ($val == 'BankAccountNumberKey') {
398  $string .= $this->cle_rib.' ';
399  } elseif ($val == 'BIC') {
400  $string .= $this->bic.' ';
401  } elseif ($val == 'IBAN') {
402  $string .= $this->iban.' ';
403  }
404  }
405 
406  return trim($string);
407  }
408 
409 
415  public function canBeConciliated()
416  {
417  global $conf;
418 
419  if (empty($this->rappro)) {
420  return -1;
421  }
422  if ($this->courant == Account::TYPE_CASH && empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
423  return -2;
424  }
425  if ($this->clos) {
426  return -3;
427  }
428  return 1;
429  }
430 
431 
432  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
443  public function add_url_line($line_id, $url_id, $url, $label, $type)
444  {
445  // phpcs:enable
446  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
447  $sql .= "fk_bank";
448  $sql .= ", url_id";
449  $sql .= ", url"; // deprecated
450  $sql .= ", label";
451  $sql .= ", type";
452  $sql .= ") VALUES (";
453  $sql .= " ".((int) $line_id);
454  $sql .= ", ".((int) $url_id);
455  $sql .= ", '".$this->db->escape($url)."'"; // dperecated
456  $sql .= ", '".$this->db->escape($label)."'";
457  $sql .= ", '".$this->db->escape($type)."'";
458  $sql .= ")";
459 
460  dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
461  if ($this->db->query($sql)) {
462  $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
463  return $rowid;
464  } else {
465  $this->error = $this->db->lasterror();
466  return -1;
467  }
468  }
469 
470  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
480  public function get_url($fk_bank = '', $url_id = '', $type = '')
481  {
482  // phpcs:enable
483  $lines = array();
484 
485  // Check parameters
486  if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) {
487  $this->error = "ErrorBadParameter";
488  return -1;
489  }
490 
491  $sql = "SELECT fk_bank, url_id, url, label, type";
492  $sql .= " FROM ".MAIN_DB_PREFIX."bank_url";
493  if ($fk_bank > 0) {
494  $sql .= " WHERE fk_bank = ".((int) $fk_bank);
495  } else {
496  $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'";
497  }
498  $sql .= " ORDER BY type, label";
499 
500  dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
501  $result = $this->db->query($sql);
502  if ($result) {
503  $i = 0;
504  $num = $this->db->num_rows($result);
505  while ($i < $num) {
506  $obj = $this->db->fetch_object($result);
507  // Anciens liens (pour compatibilite)
508  $lines[$i][0] = $obj->url;
509  $lines[$i][1] = $obj->url_id;
510  $lines[$i][2] = $obj->label;
511  $lines[$i][3] = $obj->type;
512  // Nouveaux liens
513  $lines[$i]['url'] = $obj->url;
514  $lines[$i]['url_id'] = $obj->url_id;
515  $lines[$i]['label'] = $obj->label;
516  $lines[$i]['type'] = $obj->type;
517  $lines[$i]['fk_bank'] = $obj->fk_bank;
518  $i++;
519  }
520  } else {
521  dol_print_error($this->db);
522  }
523 
524  return $lines;
525  }
526 
545  public function addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur = '', $banque = '', $accountancycode = '', $datev = null, $num_releve = '', $amount_main_currency = null)
546  {
547  // Deprecation warning
548  if (is_numeric($oper)) {
549  dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING);
550  }
551 
552  if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
553  $this->id = $this->rowid;
554  }
555 
556  // Clean parameters
557  $emetteur = trim($emetteur);
558  $banque = trim($banque);
559  $label = trim($label);
560 
561  $now = dol_now();
562 
563  if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid
564  $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
565  $sql .= " WHERE id = ".((int) $oper);
566  $sql .= " AND entity IN (".getEntity('c_paiement').")";
567  $resql = $this->db->query($sql);
568  if ($resql) {
569  $obj = $this->db->fetch_object($resql);
570  $oper = $obj->code;
571  } else {
572  dol_print_error($this->db, 'Failed to get payment type code');
573  return -1;
574  }
575  }
576 
577  // Check parameters
578  if (!$oper) {
579  $this->error = "oper not defined";
580  return -1;
581  }
582  if (!$this->id) {
583  $this->error = "this->id not defined";
584  return -2;
585  }
586  if ($this->courant == Account::TYPE_CASH && $oper != 'LIQ') {
587  $this->error = "ErrorCashAccountAcceptsOnlyCashMoney";
588  return -3;
589  }
590 
591  $this->db->begin();
592 
593  if (is_null($datev) || empty($datev)) {
594  $datev = $date;
595  }
596 
597  $accline = new AccountLine($this->db);
598  $accline->datec = $now;
599  $accline->dateo = $date;
600  $accline->datev = $datev;
601  $accline->label = $label;
602  $accline->amount = $amount;
603  $accline->amount_main_currency = $amount_main_currency;
604  $accline->fk_user_author = $user->id;
605  $accline->fk_account = $this->id;
606  $accline->fk_type = $oper;
607  $accline->numero_compte = $accountancycode;
608  $accline->num_releve = $num_releve;
609 
610  if ($num_chq) {
611  $accline->num_chq = $num_chq;
612  }
613 
614  if ($emetteur) {
615  $accline->emetteur = $emetteur;
616  }
617 
618  if ($banque) {
619  $accline->bank_chq = $banque;
620  }
621 
622  if ($accline->insert() > 0) {
623  if ($categorie > 0) {
624  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class(";
625  $sql .= "lineid, fk_categ";
626  $sql .= ") VALUES (";
627  $sql .= ((int) $accline->id).", '".$this->db->escape($categorie)."'";
628  $sql .= ")";
629 
630  $result = $this->db->query($sql);
631  if (!$result) {
632  $this->error = $this->db->lasterror();
633  $this->db->rollback();
634 
635  return -4;
636  }
637  }
638 
639  $this->db->commit();
640 
641  return $accline->id;
642  } else {
643  $this->error = $accline->error;
644  $this->errors = $accline->errors;
645  $this->db->rollback();
646 
647  return -5;
648  }
649  }
650 
658  public function create(User $user, $notrigger = 0)
659  {
660  global $langs, $conf;
661 
662  $error = 0;
663 
664  // Clean parameters
665  if (!$this->min_allowed) {
666  $this->min_allowed = 0;
667  }
668  if (!$this->min_desired) {
669  $this->min_desired = 0;
670  }
671 
672  // Check parameters
673  if (empty($this->country_id)) {
674  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
675  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
676  return -1;
677  }
678  if (empty($this->ref)) {
679  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
680  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
681  return -1;
682  }
683  if (empty($this->date_solde)) {
684  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance"));
685  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
686  return -1;
687  }
688 
689  $balance = $this->balance;
690  if (empty($balance) && !empty($this->solde)) {
691  $balance = $this->solde;
692  }
693  if (empty($balance)) {
694  $balance = 0;
695  }
696 
697  // Chargement librairie pour acces fonction controle RIB
698  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
699 
700  $now = dol_now();
701 
702  $this->db->begin();
703 
704  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
705  $sql .= "datec";
706  $sql .= ", ref";
707  $sql .= ", label";
708  $sql .= ", entity";
709  $sql .= ", account_number";
710  $sql .= ", fk_accountancy_journal";
711  $sql .= ", bank";
712  $sql .= ", code_banque";
713  $sql .= ", code_guichet";
714  $sql .= ", number";
715  $sql .= ", cle_rib";
716  $sql .= ", bic";
717  $sql .= ", iban_prefix";
718  $sql .= ", domiciliation";
719  $sql .= ", pti_in_ctti";
720  $sql .= ", proprio";
721  $sql .= ", owner_address";
722  $sql .= ", currency_code";
723  $sql .= ", rappro";
724  $sql .= ", min_allowed";
725  $sql .= ", min_desired";
726  $sql .= ", comment";
727  $sql .= ", state_id";
728  $sql .= ", fk_pays";
729  $sql .= ", ics";
730  $sql .= ", ics_transfer";
731  $sql .= ") VALUES (";
732  $sql .= "'".$this->db->idate($now)."'";
733  $sql .= ", '".$this->db->escape($this->ref)."'";
734  $sql .= ", '".$this->db->escape($this->label)."'";
735  $sql .= ", ".((int) $conf->entity);
736  $sql .= ", '".$this->db->escape($this->account_number)."'";
737  $sql .= ", ".($this->fk_accountancy_journal > 0 ? $this->db->escape($this->fk_accountancy_journal) : "null");
738  $sql .= ", '".$this->db->escape($this->bank)."'";
739  $sql .= ", '".$this->db->escape($this->code_banque)."'";
740  $sql .= ", '".$this->db->escape($this->code_guichet)."'";
741  $sql .= ", '".$this->db->escape($this->number)."'";
742  $sql .= ", '".$this->db->escape($this->cle_rib)."'";
743  $sql .= ", '".$this->db->escape($this->bic)."'";
744  $sql .= ", '".$this->db->escape($this->iban)."'";
745  $sql .= ", '".$this->db->escape($this->domiciliation)."'";
746  $sql .= ", ".((int) $this->pti_in_ctti);
747  $sql .= ", '".$this->db->escape($this->proprio)."'";
748  $sql .= ", '".$this->db->escape($this->owner_address)."'";
749  $sql .= ", '".$this->db->escape($this->currency_code)."'";
750  $sql .= ", ".((int) $this->rappro);
751  $sql .= ", ".price2num($this->min_allowed, 'MT');
752  $sql .= ", ".price2num($this->min_desired, 'MT');
753  $sql .= ", '".$this->db->escape($this->comment)."'";
754  $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
755  $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
756  $sql .= ", '".$this->db->escape($this->ics)."'";
757  $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
758  $sql .= ")";
759 
760  dol_syslog(get_class($this)."::create", LOG_DEBUG);
761  $resql = $this->db->query($sql);
762  if ($resql) {
763  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
764 
765  $result = $this->update($user, 1);
766  if ($result > 0) {
767  $accline = new AccountLine($this->db);
768  $accline->datec = $this->db->idate($now);
769  $accline->label = '('.$langs->trans("InitialBankBalance").')';
770  $accline->amount = price2num($balance);
771  $accline->fk_user_author = $user->id;
772  $accline->fk_account = $this->id;
773  $accline->datev = $this->db->idate($this->date_solde);
774  $accline->dateo = $this->db->idate($this->date_solde);
775  $accline->fk_type = 'SOLD';
776 
777  if ($accline->insert() < 0) {
778  $error++;
779  $this->error = $accline->error;
780  $this->errors = $accline->errors;
781  }
782 
783  if (!$error) {
784  $result = $this->insertExtraFields();
785  if ($result < 0) {
786  $error++;
787  }
788  }
789 
790  if (!$error && !$notrigger) {
791  // Call trigger
792  $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
793  if ($result < 0) {
794  $error++;
795  }
796  // End call triggers
797  }
798  } else {
799  $error++;
800  }
801  } else {
802  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
803  $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
804  $error++;
805  } else {
806  $this->error = $this->db->error()." sql=".$sql;
807  $error++;
808  }
809  }
810 
811  if (!$error) {
812  $this->db->commit();
813  return $this->id;
814  } else {
815  $this->db->rollback();
816  return -1 * $error;
817  }
818  }
819 
827  public function update(User $user, $notrigger = 0)
828  {
829  global $langs, $conf;
830 
831  $error = 0;
832 
833  $this->db->begin();
834 
835  // Check parameters
836  if (empty($this->country_id)) {
837  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
838  dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
839  return -1;
840  }
841  if (empty($this->ref)) {
842  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
843  dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
844  return -1;
845  }
846  if (!$this->label) {
847  $this->label = "???";
848  }
849 
850  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
851 
852  $sql .= " ref = '".$this->db->escape($this->ref)."'";
853  $sql .= ",label = '".$this->db->escape($this->label)."'";
854 
855  $sql .= ",courant = ".((int) $this->courant);
856  $sql .= ",clos = ".((int) $this->clos);
857  $sql .= ",rappro = ".((int) $this->rappro);
858  $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
859  $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
860  $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? $this->db->escape($this->fk_accountancy_journal) : "null");
861  $sql .= ",bank = '".$this->db->escape($this->bank)."'";
862  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
863  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
864  $sql .= ",number='".$this->db->escape($this->number)."'";
865  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
866  $sql .= ",bic='".$this->db->escape($this->bic)."'";
867  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
868  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
869  $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
870  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
871  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
872 
873  $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
874 
875  $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
876  $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
877  $sql .= ",comment = '".$this->db->escape($this->comment)."'";
878 
879  $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
880  $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
881  $sql .= ",ics = '".$this->db->escape($this->ics)."'";
882  $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
883 
884  $sql .= " WHERE rowid = ".((int) $this->id);
885 
886  dol_syslog(get_class($this)."::update", LOG_DEBUG);
887  $result = $this->db->query($sql);
888  if ($result) {
889  // Actions on extra fields (by external module or standard code)
890  if (!$error) {
891  $result = $this->insertExtraFields();
892  if ($result < 0) {
893  $error++;
894  }
895  }
896 
897  if (!$error && !$notrigger) {
898  // Call trigger
899  $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
900  if ($result < 0) {
901  $error++;
902  }
903  // End call triggers
904  }
905  } else {
906  $error++;
907  $this->error = $this->db->lasterror();
908  dol_print_error($this->db);
909  }
910 
911  if (!$error) {
912  $this->db->commit();
913  return $this->id;
914  } else {
915  $this->db->rollback();
916  return -1 * $error;
917  }
918  }
919 
920 
921  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
928  public function update_bban(User $user = null)
929  {
930  // phpcs:enable
931  global $conf, $langs;
932 
933  // Load library to get BAN control function
934  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
935 
936  dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
937 
938  // Check parameters
939  if (!$this->ref) {
940  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
941  return -2;
942  }
943 
944  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
945  $sql .= " bank = '".$this->db->escape($this->bank)."'";
946  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
947  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
948  $sql .= ",number='".$this->db->escape($this->number)."'";
949  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
950  $sql .= ",bic='".$this->db->escape($this->bic)."'";
951  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
952  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
953  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
954  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
955  $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null");
956  $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null");
957  $sql .= " WHERE rowid = ".((int) $this->id);
958  $sql .= " AND entity = ".((int) $conf->entity);
959 
960  dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
961 
962  $result = $this->db->query($sql);
963  if ($result) {
964  return 1;
965  } else {
966  $this->error = $this->db->lasterror();
967  dol_print_error($this->db);
968  return -1;
969  }
970  }
971 
972 
980  public function fetch($id, $ref = '')
981  {
982  global $conf;
983 
984  if (empty($id) && empty($ref)) {
985  $this->error = "ErrorBadParameters";
986  return -1;
987  }
988 
989  $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,";
990  $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
991  $sql .= " ba.domiciliation, ba.pti_in_ctti, ba.proprio, ba.owner_address, ba.state_id, ba.fk_pays as country_id,";
992  $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
993  $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
994  $sql .= " ba.datec as date_creation, ba.tms as date_update, ba.ics, ba.ics_transfer,";
995  $sql .= ' c.code as country_code, c.label as country,';
996  $sql .= ' d.code_departement as state_code, d.nom as state';
997  $sql .= ' , aj.code as accountancy_journal';
998  $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
999  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
1000  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
1001  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal';
1002  $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
1003  if ($id) {
1004  $sql .= " AND ba.rowid = ".((int) $id);
1005  }
1006  if ($ref) {
1007  $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
1008  }
1009 
1010  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1011  $result = $this->db->query($sql);
1012  if ($result) {
1013  if ($this->db->num_rows($result)) {
1014  $obj = $this->db->fetch_object($result);
1015 
1016  $this->id = $obj->rowid;
1017  $this->rowid = $obj->rowid;
1018  $this->ref = $obj->ref;
1019  $this->label = $obj->label;
1020  $this->type = $obj->courant;
1021  $this->courant = $obj->courant;
1022  $this->bank = $obj->bank;
1023  $this->clos = $obj->clos;
1024  $this->rappro = $obj->rappro;
1025  $this->url = $obj->url;
1026 
1027  $this->code_banque = $obj->code_banque;
1028  $this->code_guichet = $obj->code_guichet;
1029  $this->number = $obj->number;
1030  $this->cle_rib = $obj->cle_rib;
1031  $this->bic = $obj->bic;
1032  $this->iban = $obj->iban;
1033  $this->domiciliation = $obj->domiciliation;
1034  $this->pti_in_ctti = $obj->pti_in_ctti;
1035  $this->proprio = $obj->proprio;
1036  $this->owner_address = $obj->owner_address;
1037 
1038  $this->state_id = $obj->state_id;
1039  $this->state_code = $obj->state_code;
1040  $this->state = $obj->state;
1041 
1042  $this->country_id = $obj->country_id;
1043  $this->country_code = $obj->country_code;
1044  $this->country = $obj->country;
1045 
1046  $this->account_number = $obj->account_number;
1047  $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
1048  $this->accountancy_journal = $obj->accountancy_journal;
1049 
1050  $this->currency_code = $obj->currency_code;
1051  $this->account_currency_code = $obj->currency_code;
1052  $this->min_allowed = $obj->min_allowed;
1053  $this->min_desired = $obj->min_desired;
1054  $this->comment = $obj->comment;
1055 
1056  $this->date_creation = $this->db->jdate($obj->date_creation);
1057  $this->date_update = $this->db->jdate($obj->date_update);
1058 
1059  $this->ics = $obj->ics;
1060  $this->ics_transfer = $obj->ics_transfer;
1061 
1062  // Retrieve all extrafield
1063  // fetch optionals attributes and labels
1064  $this->fetch_optionals();
1065 
1066  return 1;
1067  } else {
1068  return 0;
1069  }
1070  } else {
1071  $this->error = $this->db->lasterror;
1072  $this->errors[] = $this->error;
1073  return -1;
1074  }
1075  }
1076 
1087  public function setCategories($categories)
1088  {
1089  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1090  return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
1091  }
1092 
1099  public function delete(User $user = null)
1100  {
1101  global $conf;
1102 
1103  $error = 0;
1104 
1105  $this->db->begin();
1106 
1107  // @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
1108 
1109  // Delete link between tag and bank account
1110  if (!$error) {
1111  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1112  $sql .= " WHERE fk_account = ".((int) $this->id);
1113 
1114  $resql = $this->db->query($sql);
1115  if (!$resql) {
1116  $error++;
1117  $this->error = "Error ".$this->db->lasterror();
1118  }
1119  }
1120 
1121  if (!$error) {
1122  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account";
1123  $sql .= " WHERE rowid = ".((int) $this->rowid);
1124 
1125  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1126  $result = $this->db->query($sql);
1127  if ($result) {
1128  // Remove extrafields
1129  if (!$error) {
1130  $result = $this->deleteExtraFields();
1131  if ($result < 0) {
1132  $error++;
1133  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1134  }
1135  }
1136  } else {
1137  $error++;
1138  $this->error = "Error ".$this->db->lasterror();
1139  }
1140  }
1141 
1142  if (!$error) {
1143  $this->db->commit();
1144  return 1;
1145  } else {
1146  $this->db->rollback();
1147  return -1;
1148  }
1149  }
1150 
1151 
1158  public function getLibStatut($mode = 0)
1159  {
1160  return $this->LibStatut($this->clos, $mode);
1161  }
1162 
1163  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1171  public function LibStatut($status, $mode = 0)
1172  {
1173  // phpcs:enable
1174  global $langs;
1175  $langs->load('banks');
1176 
1177  if ($status == self::STATUS_OPEN) {
1178  $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1179  $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1180  $statusType = 'status4';
1181  } else {
1182  $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1183  $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1184  $statusType = 'status5';
1185  }
1186 
1187  return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1188  }
1189 
1190 
1191  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1197  public function can_be_deleted()
1198  {
1199  // phpcs:enable
1200  $can_be_deleted = false;
1201 
1202  $sql = "SELECT COUNT(rowid) as nb";
1203  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1204  $sql .= " WHERE fk_account = ".((int) $this->id);
1205 
1206  $resql = $this->db->query($sql);
1207  if ($resql) {
1208  $obj = $this->db->fetch_object($resql);
1209  if ($obj->nb <= 1) {
1210  $can_be_deleted = true; // Juste le solde
1211  }
1212  } else {
1213  dol_print_error($this->db);
1214  }
1215  return $can_be_deleted;
1216  }
1217 
1218 
1224  public function error()
1225  {
1226  return $this->error;
1227  }
1228 
1237  public function solde($option = 0, $date_end = '', $field = 'dateo')
1238  {
1239  $solde = 0;
1240 
1241  $sql = "SELECT sum(amount) as amount";
1242  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1243  $sql .= " WHERE fk_account = ".((int) $this->id);
1244  if ($option == 1) {
1245  $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1246  }
1247 
1248  $resql = $this->db->query($sql);
1249  if ($resql) {
1250  if ($this->db->num_rows($resql)) {
1251  $obj = $this->db->fetch_object($resql);
1252  $solde = $obj->amount;
1253  }
1254  $this->db->free($resql);
1255  } else {
1256  $this->errors[] = $this->db->lasterror;
1257  return -1;
1258  }
1259 
1260  return price2num($solde, 'MU');
1261  }
1262 
1263  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1271  public function load_board(User $user, $filteraccountid = 0)
1272  {
1273  // phpcs:enable
1274  global $conf, $langs;
1275 
1276  if ($user->socid) {
1277  return -1; // protection pour eviter appel par utilisateur externe
1278  }
1279 
1280  $sql = "SELECT b.rowid, b.datev as datefin";
1281  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1282  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1283  $sql .= " WHERE b.rappro=0";
1284  $sql .= " AND b.fk_account = ba.rowid";
1285  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1286  $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1287  $sql .= " AND clos = 0";
1288  if ($filteraccountid) {
1289  $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1290  }
1291 
1292  $resql = $this->db->query($sql);
1293  if ($resql) {
1294  $langs->load("banks");
1295  $now = dol_now();
1296 
1297  require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1298 
1299  $response = new WorkboardResponse();
1300  $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1301  $response->label = $langs->trans("TransactionsToConciliate");
1302  $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1303  $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1304  $response->img = img_object('', "payment");
1305 
1306  while ($obj = $this->db->fetch_object($resql)) {
1307  $response->nbtodo++;
1308  if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1309  $response->nbtodolate++;
1310  }
1311  }
1312 
1313  return $response;
1314  } else {
1315  dol_print_error($this->db);
1316  $this->error = $this->db->error();
1317  return -1;
1318  }
1319  }
1320 
1321  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1327  public function load_state_board($filteraccountid = 0)
1328  {
1329  // phpcs:enable
1330  global $user;
1331 
1332  if ($user->socid) {
1333  return -1; // protection pour eviter appel par utilisateur externe
1334  }
1335 
1336  $sql = "SELECT count(b.rowid) as nb";
1337  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1338  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1339  $sql .= " WHERE b.fk_account = ba.rowid";
1340  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1341  $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1342  $sql .= " AND clos = 0";
1343  if ($filteraccountid) {
1344  $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1345  }
1346 
1347  $resql = $this->db->query($sql);
1348  if ($resql) {
1349  while ($obj = $this->db->fetch_object($resql)) {
1350  $this->nb["banklines"] = $obj->nb;
1351  }
1352  $this->db->free($resql);
1353  } else {
1354  dol_print_error($this->db);
1355  $this->error = $this->db->error();
1356  return -1;
1357  }
1358  }
1359 
1360 
1366  public function countAccountToReconcile()
1367  {
1368  global $db, $conf, $user;
1369 
1370  //Protection against external users
1371  if ($user->socid) {
1372  return 0;
1373  }
1374 
1375  $nb = 0;
1376 
1377  $sql = "SELECT COUNT(ba.rowid) as nb";
1378  $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1379  $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1380  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1381  if (empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
1382  $sql .= " AND ba.courant != 2";
1383  }
1384  $resql = $this->db->query($sql);
1385  if ($resql) {
1386  $obj = $this->db->fetch_object($resql);
1387  $nb = $obj->nb;
1388  } else {
1389  dol_print_error($this->db);
1390  }
1391 
1392  return $nb;
1393  }
1394 
1405  public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0)
1406  {
1407  global $conf, $langs, $user;
1408 
1409  $result = '';
1410  $label = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
1411  if (isset($this->status)) {
1412  $label .= ' '.$this->getLibStatut(5);
1413  }
1414  $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1415  $label .= '<br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
1416  $label .= '<br><b>'.$langs->trans('IBAN').':</b> '.$this->iban;
1417  $label .= '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
1418  $label .= '<br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
1419 
1420  if (empty($user->rights->banque->lire) || !empty($user->socid)) {
1421  $option = 'nolink';
1422  }
1423 
1424  if (isModEnabled('accounting')) {
1425  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1426  $langs->load("accountancy");
1427  $label .= '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
1428  $label .= '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
1429  }
1430 
1431  $linkclose = '" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1432 
1433  $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1434  if ($mode == 'transactions') {
1435  $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1436  } elseif ($mode == 'receipts') {
1437  $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1438  }
1439 
1440  if ($option != 'nolink') {
1441  // Add param to save lastsearch_values or not
1442  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1443  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1444  $add_save_lastsearch_values = 1;
1445  }
1446  if ($add_save_lastsearch_values) {
1447  $url .= '&save_lastsearch_values=1';
1448  }
1449  }
1450 
1451  $linkstart = '<a href="'.$url.$linkclose;
1452  $linkend = '</a>';
1453 
1454  if ($option == 'nolink') {
1455  $linkstart = '';
1456  $linkend = '';
1457  }
1458 
1459  $result .= $linkstart;
1460  if ($withpicto) {
1461  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1462  }
1463  if ($withpicto != 2) {
1464  $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1465  }
1466  $result .= $linkend;
1467 
1468  return $result;
1469  }
1470 
1471 
1472  // Method after here are common to Account and CompanyBankAccount
1473 
1474 
1480  public function verif()
1481  {
1482  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1483 
1484  $this->error_number = 0;
1485 
1486  // Call function to check BAN
1487 
1488  if (!checkIbanForAccount($this)) {
1489  $this->error_number = 12;
1490  $this->error_message = 'IBANNotValid';
1491  }
1492  if (!checkSwiftForAccount($this)) {
1493  $this->error_number = 12;
1494  $this->error_message = 'SwiftNotValid';
1495  }
1496  /*if (! checkBanForAccount($this))
1497  {
1498  $this->error_number = 12;
1499  $this->error_message = 'BANControlError';
1500  }*/
1501 
1502  if ($this->error_number == 0) {
1503  return 1;
1504  } else {
1505  return 0;
1506  }
1507  }
1508 
1514  public function getCountryCode()
1515  {
1516  global $mysoc;
1517 
1518  // We return country code of bank account
1519  if (!empty($this->country_code)) {
1520  return $this->country_code;
1521  }
1522 
1523  // For backward compatibility, we try to guess country from other information
1524  if (!empty($this->iban)) {
1525  // If IBAN defined, we can know country of account from it
1526  $reg = array();
1527  if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
1528  return $reg[1];
1529  }
1530  }
1531 
1532  // If this class is linked to a third party
1533  if (!empty($this->socid)) {
1534  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
1535  $company = new Societe($this->db);
1536  $result = $company->fetch($this->socid);
1537  if (!empty($company->country_code)) {
1538  return $company->country_code;
1539  }
1540  }
1541 
1542  // We return country code of managed company
1543  if (!empty($mysoc->country_code)) {
1544  return $mysoc->country_code;
1545  }
1546 
1547  return '';
1548  }
1549 
1558  public function useDetailedBBAN()
1559  {
1560  $country_code = $this->getCountryCode();
1561 
1562  if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
1563  return 1; // France, Spain, Gabon, ... - Not valid for CH
1564  }
1565  if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
1566  return 2; // Australia, England...
1567  }
1568  return 0;
1569  }
1570 
1576  public function needIBAN()
1577  {
1578  global $conf;
1579 
1580  if (!empty($conf->global->MAIN_IBAN_IS_NEVER_MANDATORY)) {
1581  return 0;
1582  }
1583 
1584  $country_code = $this->getCountryCode();
1585 
1586  $country_code_in_EEC = array(
1587  'AT', // Austria
1588  'BE', // Belgium
1589  'BG', // Bulgaria
1590  'CY', // Cyprus
1591  'CZ', // Czech republic
1592  'DE', // Germany
1593  'DK', // Danemark
1594  'EE', // Estonia
1595  'ES', // Spain
1596  'FI', // Finland
1597  'FR', // France
1598  'GB', // United Kingdom
1599  'GR', // Greece
1600  'HR', // Croatia
1601  'NL', // Holland
1602  'HU', // Hungary
1603  'IE', // Ireland
1604  'IM', // Isle of Man - Included in UK
1605  'IT', // Italy
1606  'LT', // Lithuania
1607  'LU', // Luxembourg
1608  'LV', // Latvia
1609  'MC', // Monaco - Included in France
1610  'MT', // Malta
1611  //'NO', // Norway
1612  'PL', // Poland
1613  'PT', // Portugal
1614  'RO', // Romania
1615  'SE', // Sweden
1616  'SK', // Slovakia
1617  'SI', // Slovenia
1618  'UK', // United Kingdom
1619  //'CH', // Switzerland - No. Swizerland in not in EEC
1620  );
1621 
1622  if (in_array($country_code, $country_code_in_EEC)) {
1623  return 1; // France, Spain, ...
1624  }
1625  return 0;
1626  }
1627 
1634  public function info($id)
1635  {
1636  }
1637 
1652  public function getFieldsToShow($includeibanbic = 0)
1653  {
1654  //Get the required properties depending on the country
1655  $detailedBBAN = $this->useDetailedBBAN();
1656 
1657  if ($detailedBBAN == 0) {
1658  $fieldarray = array(
1659  'BankAccountNumber'
1660  );
1661  } elseif ($detailedBBAN == 2) {
1662  $fieldarray = array(
1663  'BankCode',
1664  'BankAccountNumber'
1665  );
1666  } else {
1667  $fieldarray = self::getAccountNumberOrder();
1668  }
1669 
1670  //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1671  if ($includeibanbic) {
1672  $fieldarray[] = 'IBAN';
1673  $fieldarray[] = 'BIC';
1674  }
1675  //}
1676 
1677  //Get the order the properties are shown
1678  return $fieldarray;
1679  }
1680 
1691  public static function getAccountNumberOrder()
1692  {
1693  global $conf;
1694 
1695  $fieldlists = array(
1696  'BankCode',
1697  'DeskCode',
1698  'BankAccountNumber',
1699  'BankAccountNumberKey'
1700  );
1701 
1702  if (!empty($conf->global->BANK_SHOW_ORDER_OPTION)) {
1703  if (is_numeric($conf->global->BANK_SHOW_ORDER_OPTION)) {
1704  if ($conf->global->BANK_SHOW_ORDER_OPTION == '1') {
1705  $fieldlists = array(
1706  'BankCode',
1707  'DeskCode',
1708  'BankAccountNumberKey',
1709  'BankAccountNumber'
1710  );
1711  }
1712  } else {
1713  //Replace the old AccountNumber key with the new BankAccountNumber key
1714  $fieldlists = explode(
1715  ' ',
1716  preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
1717  );
1718  }
1719  }
1720 
1721  return $fieldlists;
1722  }
1723 
1724 
1732  public function initAsSpecimen()
1733  {
1734  // Example of IBAN FR7630001007941234567890185
1735  $this->specimen = 1;
1736  $this->ref = 'MBA';
1737  $this->label = 'My Big Company Bank account';
1738  $this->bank = 'MyBank';
1739  $this->courant = Account::TYPE_CURRENT;
1740  $this->clos = Account::STATUS_OPEN;
1741  $this->code_banque = '30001';
1742  $this->code_guichet = '00794';
1743  $this->number = '12345678901';
1744  $this->cle_rib = '85';
1745  $this->bic = 'AA12';
1746  $this->iban = 'FR7630001007941234567890185';
1747  $this->domiciliation = 'Banque de France';
1748  $this->proprio = 'Owner';
1749  $this->owner_address = 'Owner address';
1750  $this->country_id = 1;
1751  }
1752 
1761  public static function replaceThirdparty($dbs, $origin_id, $dest_id)
1762  {
1763  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'";
1764 
1765  if ($dbs->query($sql)) {
1766  return true;
1767  } else {
1768  //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.
1769  //$this->errors = $dbs->lasterror();
1770  return false;
1771  }
1772  }
1773 }
1774 
1775 
1780 {
1784  public $error = '';
1785 
1789  public $db;
1790 
1794  public $element = 'bank';
1795 
1799  public $table_element = 'bank';
1800 
1804  public $picto = 'accountline';
1805 
1809  public $id;
1810 
1814  public $ref;
1815 
1821  public $datec;
1822 
1828  public $dateo;
1829 
1835  public $datev;
1836 
1837  public $amount; /* Amount of payment in the bank account currency */
1838  public $amount_main_currency; /* Amount in the currency of company if bank account use another currency */
1839 
1843  public $fk_user_author;
1844 
1848  public $fk_user_rappro;
1849 
1853  public $fk_type;
1854 
1858  public $fk_bordereau;
1859 
1863  public $fk_account;
1864 
1868  public $bank_account_ref;
1869 
1873  public $bank_account_label;
1874 
1878  public $numero_compte;
1879 
1883  public $emetteur;
1884 
1885  public $rappro; // Is it conciliated
1886  public $num_releve; // If conciliated, what is bank statement
1887  public $num_chq; // Num of cheque
1888  public $bank_chq; // Bank of cheque
1889 
1893  public $label;
1894 
1895  public $note;
1896 
1897 
1898 
1904  public function __construct(DoliDB $db)
1905  {
1906  $this->db = $db;
1907  }
1908 
1917  public function fetch($rowid, $ref = '', $num = '')
1918  {
1919  global $conf;
1920 
1921  // Check parameters
1922  if (empty($rowid) && empty($ref) && empty($num)) {
1923  return -1;
1924  }
1925 
1926  $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
1927  $sql .= " b.fk_user_author, b.fk_user_rappro,";
1928  $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
1929  $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
1930  $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
1931  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1932  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1933  $sql .= " WHERE b.fk_account = ba.rowid";
1934  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1935  if ($num) {
1936  $sql .= " AND b.num_chq='".$this->db->escape($num)."'";
1937  } elseif ($ref) {
1938  $sql .= " AND b.rowid='".$this->db->escape($ref)."'";
1939  } else {
1940  $sql .= " AND b.rowid = ".((int) $rowid);
1941  }
1942 
1943  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1944  $result = $this->db->query($sql);
1945  if ($result) {
1946  $ret = 0;
1947 
1948  $obj = $this->db->fetch_object($result);
1949  if ($obj) {
1950  $this->id = $obj->rowid;
1951  $this->rowid = $obj->rowid;
1952  $this->ref = $obj->rowid;
1953 
1954  $this->datec = $obj->datec;
1955  $this->datev = $obj->datev;
1956  $this->dateo = $obj->dateo;
1957  $this->amount = $obj->amount;
1958  $this->label = $obj->label;
1959  $this->note = $obj->note;
1960 
1961  $this->fk_user_author = $obj->fk_user_author;
1962  $this->fk_user_rappro = $obj->fk_user_rappro;
1963 
1964  $this->fk_type = $obj->fk_type; // Type of transaction
1965  $this->rappro = $obj->rappro;
1966  $this->num_releve = $obj->num_releve;
1967 
1968  $this->num_chq = $obj->num_chq;
1969  $this->bank_chq = $obj->banque;
1970  $this->fk_bordereau = $obj->fk_bordereau;
1971 
1972  $this->fk_account = $obj->fk_account;
1973  $this->bank_account_ref = $obj->bank_account_ref;
1974  $this->bank_account_label = $obj->bank_account_label;
1975 
1976  $ret = 1;
1977  }
1978  $this->db->free($result);
1979  return $ret;
1980  } else {
1981  return -1;
1982  }
1983  }
1984 
1990  public function insert()
1991  {
1992  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
1993  $sql .= "datec";
1994  $sql .= ", dateo";
1995  $sql .= ", datev";
1996  $sql .= ", label";
1997  $sql .= ", amount";
1998  $sql .= ", amount_main_currency";
1999  $sql .= ", fk_user_author";
2000  $sql .= ", num_chq";
2001  $sql .= ", fk_account";
2002  $sql .= ", fk_type";
2003  $sql .= ", emetteur,banque";
2004  $sql .= ", rappro";
2005  $sql .= ", numero_compte";
2006  $sql .= ", num_releve";
2007  $sql .= ") VALUES (";
2008  $sql .= "'".$this->db->idate($this->datec)."'";
2009  $sql .= ", '".$this->db->idate($this->dateo)."'";
2010  $sql .= ", '".$this->db->idate($this->datev)."'";
2011  $sql .= ", '".$this->db->escape($this->label)."'";
2012  $sql .= ", ".price2num($this->amount);
2013  $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
2014  $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
2015  $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
2016  $sql .= ", '".$this->db->escape($this->fk_account)."'";
2017  $sql .= ", '".$this->db->escape($this->fk_type)."'";
2018  $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
2019  $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
2020  $sql .= ", ".(int) $this->rappro;
2021  $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2022  $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2023  $sql .= ")";
2024 
2025  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2026  $resql = $this->db->query($sql);
2027 
2028  if (!$resql) {
2029  $this->error = $this->db->lasterror();
2030  return -1;
2031  }
2032 
2033  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2034 
2035  return $this->id;
2036  }
2037 
2044  public function delete(User $user = null)
2045  {
2046  global $conf;
2047 
2048  $nbko = 0;
2049 
2050  if ($this->rappro) {
2051  // Protection to avoid any delete of consolidated lines
2052  $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2053  return -1;
2054  }
2055 
2056  $this->db->begin();
2057 
2058  // Protection to avoid any delete of accounted lines. Protection on by default
2059  if (empty($conf->global->BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING)) {
2060  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2061  $resql = $this->db->query($sql);
2062  if ($resql) {
2063  $obj = $this->db->fetch_object($resql);
2064  if ($obj && $obj->nb) {
2065  $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2066  $this->db->rollback();
2067  return -1;
2068  }
2069  } else {
2070  $this->error = $this->db->lasterror();
2071  $this->db->rollback();
2072  return -1;
2073  }
2074  }
2075 
2076  // Delete urls
2077  $result = $this->delete_urls($user);
2078  if ($result < 0) {
2079  $nbko++;
2080  }
2081 
2082  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".(int) $this->rowid;
2083  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2084  $result = $this->db->query($sql);
2085  if (!$result) {
2086  $nbko++;
2087  }
2088 
2089  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2090  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2091  $result = $this->db->query($sql);
2092  if (!$result) {
2093  $nbko++;
2094  }
2095 
2096  if (!$nbko) {
2097  $this->db->commit();
2098  return 1;
2099  } else {
2100  $this->db->rollback();
2101  return -$nbko;
2102  }
2103  }
2104 
2105 
2106  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2113  public function delete_urls(User $user = null)
2114  {
2115  // phpcs:enable
2116  $nbko = 0;
2117 
2118  if ($this->rappro) {
2119  // Protection to avoid any delete of consolidated lines
2120  $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2121  return -1;
2122  }
2123 
2124  $this->db->begin();
2125 
2126  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2127  dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
2128  $result = $this->db->query($sql);
2129  if (!$result) {
2130  $nbko++;
2131  }
2132 
2133  if (!$nbko) {
2134  $this->db->commit();
2135  return 1;
2136  } else {
2137  $this->db->rollback();
2138  return -$nbko;
2139  }
2140  }
2141 
2142 
2150  public function update(User $user, $notrigger = 0)
2151  {
2152  $this->db->begin();
2153 
2154  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2155  $sql .= " amount = ".price2num($this->amount).",";
2156  $sql .= " datev='".$this->db->idate($this->datev)."',";
2157  $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2158  $sql .= " WHERE rowid = ".((int) $this->rowid);
2159 
2160  dol_syslog(get_class($this)."::update", LOG_DEBUG);
2161  $resql = $this->db->query($sql);
2162  if ($resql) {
2163  $this->db->commit();
2164  return 1;
2165  } else {
2166  $this->db->rollback();
2167  $this->error = $this->db->error();
2168  return -1;
2169  }
2170  }
2171 
2172 
2173  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2182  public function update_conciliation(User $user, $cat, $conciliated = 1)
2183  {
2184  // phpcs:enable
2185  global $conf, $langs;
2186 
2187  $this->db->begin();
2188 
2189  // Check statement field
2190  if (!empty($conf->global->BANK_STATEMENT_REGEX_RULE)) {
2191  if (!preg_match('/'.$conf->global->BANK_STATEMENT_REGEX_RULE.'/', $this->num_releve)) {
2192  $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", $conf->global->BANK_STATEMENT_REGEX_RULE);
2193  return -1;
2194  }
2195  }
2196 
2197  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2198  $sql .= " rappro = ".((int) $conciliated);
2199  $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2200  if ($conciliated) {
2201  $sql .= ", fk_user_rappro = ".$user->id;
2202  }
2203  $sql .= " WHERE rowid = ".((int) $this->id);
2204 
2205  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2206  $resql = $this->db->query($sql);
2207  if ($resql) {
2208  if (!empty($cat)) {
2209  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
2210  $sql .= "lineid";
2211  $sql .= ", fk_categ";
2212  $sql .= ") VALUES (";
2213  $sql .= $this->id;
2214  $sql .= ", ".((int) $cat);
2215  $sql .= ")";
2216 
2217  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2218  $this->db->query($sql);
2219 
2220  // No error check. Can fail if category already affected
2221  }
2222 
2223  $this->rappro = 1;
2224 
2225  $this->db->commit();
2226  return 1;
2227  } else {
2228  $this->db->rollback();
2229  return -1;
2230  }
2231  }
2232 
2233 
2234  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2242  public function datev_change($rowid, $sign = 1)
2243  {
2244  // phpcs:enable
2245  $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2246  $resql = $this->db->query($sql);
2247  if ($resql) {
2248  $obj = $this->db->fetch_object($resql);
2249  $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2250 
2251  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2252  $sql .= " datev = '".$this->db->idate($newdate)."'";
2253  $sql .= " WHERE rowid = ".((int) $rowid);
2254 
2255  $result = $this->db->query($sql);
2256  if ($result) {
2257  if ($this->db->affected_rows($result)) {
2258  return 1;
2259  }
2260  } else {
2261  dol_print_error($this->db);
2262  return 0;
2263  }
2264  } else {
2265  dol_print_error($this->db);
2266  }
2267  return 0;
2268  }
2269 
2270  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2277  public function datev_next($id)
2278  {
2279  // phpcs:enable
2280  return $this->datev_change($id, 1);
2281  }
2282 
2283  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2290  public function datev_previous($id)
2291  {
2292  // phpcs:enable
2293  return $this->datev_change($id, -1);
2294  }
2295 
2296 
2297  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2305  public function dateo_change($rowid, $sign = 1)
2306  {
2307  // phpcs:enable
2308  $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2309  $resql = $this->db->query($sql);
2310  if ($resql) {
2311  $obj = $this->db->fetch_object($resql);
2312  $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2313 
2314  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2315  $sql .= " dateo = '".$this->db->idate($newdate)."'";
2316  $sql .= " WHERE rowid = ".((int) $rowid);
2317 
2318  $result = $this->db->query($sql);
2319  if ($result) {
2320  if ($this->db->affected_rows($result)) {
2321  return 1;
2322  }
2323  } else {
2324  dol_print_error($this->db);
2325  return 0;
2326  }
2327  } else {
2328  dol_print_error($this->db);
2329  }
2330  return 0;
2331  }
2332 
2333  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2340  public function dateo_next($id)
2341  {
2342  // phpcs:enable
2343  return $this->dateo_change($id, 1);
2344  }
2345 
2346  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2353  public function dateo_previous($id)
2354  {
2355  // phpcs:enable
2356  return $this->dateo_change($id, -1);
2357  }
2358 
2359 
2366  public function info($id)
2367  {
2368  $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2369  $sql .= ' b.fk_user_author, b.fk_user_rappro';
2370  $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2371  $sql .= ' WHERE b.rowid = '.((int) $id);
2372 
2373  $result = $this->db->query($sql);
2374  if ($result) {
2375  if ($this->db->num_rows($result)) {
2376  $obj = $this->db->fetch_object($result);
2377  $this->id = $obj->rowid;
2378 
2379  if ($obj->fk_user_author) {
2380  $cuser = new User($this->db);
2381  $cuser->fetch($obj->fk_user_author);
2382  $this->user_creation = $cuser;
2383  }
2384  if ($obj->fk_user_rappro) {
2385  $ruser = new User($this->db);
2386  $ruser->fetch($obj->fk_user_rappro);
2387  $this->user_rappro = $ruser;
2388  }
2389 
2390  $this->date_creation = $this->db->jdate($obj->datec);
2391  $this->date_modification = $this->db->jdate($obj->datem);
2392  //$this->date_rappro = $obj->daterappro; // Not yet managed
2393  }
2394  $this->db->free($result);
2395  } else {
2396  dol_print_error($this->db);
2397  }
2398  }
2399 
2400 
2410  public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2411  {
2412  global $langs;
2413 
2414  $result = '';
2415 
2416  $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2417  $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2418 
2419  $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">';
2420  $linkend = '</a>';
2421 
2422  $result .= $linkstart;
2423  if ($withpicto) {
2424  $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);
2425  }
2426  if ($withpicto != 2) {
2427  $result .= ($this->ref ? $this->ref : $this->id);
2428  }
2429  $result .= $linkend;
2430 
2431  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2432  $result .= ' <span class="opacitymedium">(';
2433  }
2434  if ($option == 'showall') {
2435  $result .= $langs->trans("BankAccount").': ';
2436  $accountstatic = new Account($this->db);
2437  $accountstatic->id = $this->fk_account;
2438  $accountstatic->ref = $this->bank_account_ref;
2439  $accountstatic->label = $this->bank_account_label;
2440  $result .= $accountstatic->getNomUrl(0).', ';
2441  }
2442  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2443  $result .= $langs->trans("BankLineConciliated").': ';
2444  $result .= yn($this->rappro);
2445  }
2446  if ($option == 'showall' || $option == 'showconciliatedandaccounted') {
2447  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2448  $resql = $this->db->query($sql);
2449  if ($resql) {
2450  $obj = $this->db->fetch_object($resql);
2451  if ($obj && $obj->nb) {
2452  $result .= ' - '.$langs->trans("Accounted").': '.yn(1);
2453  } else {
2454  $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2455  }
2456  }
2457  }
2458  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2459  $result .= ')</span>';
2460  }
2461 
2462  return $result;
2463  }
2464 
2465 
2472  public function getLibStatut($mode = 0)
2473  {
2474  return $this->LibStatut($this->status, $mode);
2475  }
2476 
2477  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2485  public function LibStatut($status, $mode = 0)
2486  {
2487  // phpcs:enable
2488  global $langs;
2489  //$langs->load('companies');
2490  /*
2491  if ($mode == 0)
2492  {
2493  if ($status==0) return $langs->trans("ActivityCeased");
2494  if ($status==1) return $langs->trans("InActivity");
2495  }
2496  if ($mode == 1)
2497  {
2498  if ($status==0) return $langs->trans("ActivityCeased");
2499  if ($status==1) return $langs->trans("InActivity");
2500  }
2501  if ($mode == 2)
2502  {
2503  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2504  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2505  }
2506  if ($mode == 3)
2507  {
2508  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2509  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2510  }
2511  if ($mode == 4)
2512  {
2513  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2514  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2515  }
2516  if ($mode == 5)
2517  {
2518  if ($status==0) return $langs->trans("ActivityCeased").' '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2519  if ($status==1) return $langs->trans("InActivity").' '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2520  }*/
2521  }
2522 
2523 
2529  public function getVentilExportCompta()
2530  {
2531  $alreadydispatched = 0;
2532 
2533  $type = 'bank';
2534 
2535  $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);
2536  $resql = $this->db->query($sql);
2537  if ($resql) {
2538  $obj = $this->db->fetch_object($resql);
2539  if ($obj) {
2540  $alreadydispatched = $obj->nb;
2541  }
2542  } else {
2543  $this->error = $this->db->lasterror();
2544  return -1;
2545  }
2546 
2547  if ($alreadydispatched) {
2548  return 1;
2549  }
2550  return 0;
2551  }
2552 }
Account\getCountryCode
getCountryCode()
Return account country code.
Definition: account.class.php:1514
Account\TYPE_CURRENT
const TYPE_CURRENT
Current account.
Definition: account.class.php:342
Account\load_state_board
load_state_board($filteraccountid=0)
Charge indicateurs this->nb de tableau de bord.
Definition: account.class.php:1327
AccountLine\fetch
fetch($rowid, $ref='', $num='')
Load into memory content of a bank transaction line.
Definition: account.class.php:1917
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:6491
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:1468
Account\verif
verif()
Return if an account has valid information for Direct debit payment.
Definition: account.class.php:1480
AccountLine\getLibStatut
getLibStatut($mode=0)
Return label of status (activity, closed)
Definition: account.class.php:2472
Account\update
update(User $user, $notrigger=0)
Update bank account card.
Definition: account.class.php:827
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:1366
AccountLine\update_conciliation
update_conciliation(User $user, $cat, $conciliated=1)
Update conciliation field.
Definition: account.class.php:2182
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:4844
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:480
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:1634
Account\setCategories
setCategories($categories)
Sets object to supplied categories.
Definition: account.class.php:1087
Account\load_board
load_board(User $user, $filteraccountid=0)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
Definition: account.class.php:1271
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:2305
Account\can_be_deleted
can_be_deleted()
Renvoi si un compte peut etre supprimer ou non (sans mouvements)
Definition: account.class.php:1197
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
AccountLine\datev_change
datev_change($rowid, $sign=1)
Increase/decrease value date of a rowid.
Definition: account.class.php:2242
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:3880
Account\TYPE_CASH
const TYPE_CASH
Cash account.
Definition: account.class.php:346
AccountLine\dateo_next
dateo_next($id)
Increase operation date of a rowid.
Definition: account.class.php:2340
Account\LibStatut
LibStatut($status, $mode=0)
Return label of given object status.
Definition: account.class.php:1171
Account\canBeConciliated
canBeConciliated()
Return if a bank account need to be conciliated.
Definition: account.class.php:415
Account\getNomUrl
getNomUrl($withpicto=0, $mode='', $option='', $save_lastsearch_value=-1, $notooltip=0)
Return clicable name (with picto eventually)
Definition: account.class.php:1405
AccountLine\datev_previous
datev_previous($id)
Decrease value date of a rowid.
Definition: account.class.php:2290
AccountLine\__construct
__construct(DoliDB $db)
Constructor.
Definition: account.class.php:1904
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:1652
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:545
Account\__construct
__construct(DoliDB $db)
Constructor.
Definition: account.class.php:362
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6156
Account\getAccountNumberOrder
static getAccountNumberOrder()
Returns the components of the bank account in order.
Definition: account.class.php:1691
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:1224
Account\TYPE_SAVINGS
const TYPE_SAVINGS
Savings account.
Definition: account.class.php:350
AccountLine\getNomUrl
getNomUrl($withpicto=0, $maxlen=0, $option='', $notooltip=0)
Return clickable name (with picto eventually)
Definition: account.class.php:2410
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
AccountLine\dateo_previous
dateo_previous($id)
Decrease operation date of a rowid.
Definition: account.class.php:2353
AccountLine\datev_next
datev_next($id)
Increase value date of a rowid.
Definition: account.class.php:2277
AccountLine\getVentilExportCompta
getVentilExportCompta()
Return if a bank line was dispatched into bookkeeping.
Definition: account.class.php:2529
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:6007
Account\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: account.class.php:1732
Account\__toString
__toString()
Shows the account number in the appropriate format.
Definition: account.class.php:387
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:1990
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:980
Account\solde
solde($option=0, $date_end='', $field='dateo')
Return current sold.
Definition: account.class.php:1237
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6116
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10338
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:4211
AccountLine
Class to manage bank transaction lines.
Definition: account.class.php:1779
Account\replaceThirdparty
static replaceThirdparty($dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: account.class.php:1761
Account\create
create(User $user, $notrigger=0)
Create bank account into database.
Definition: account.class.php:658
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$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:5791
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:1558
AccountLine\LibStatut
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
Definition: account.class.php:2485
AccountLine\delete_urls
delete_urls(User $user=null)
Delete bank line records.
Definition: account.class.php:2113
Account\getLibStatut
getLibStatut($mode=0)
Return label of object status.
Definition: account.class.php:1158
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:1576
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:443
AccountLine\update
update(User $user, $notrigger=0)
Update bank account record in database.
Definition: account.class.php:2150
Account\update_bban
update_bban(User $user=null)
Update BBAN (RIB) account fields.
Definition: account.class.php:928
AccountLine\info
info($id)
Load miscellaneous information for tab "Info".
Definition: account.class.php:2366