dolibarr  17.0.4
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  * Copyright (C) 2022 Frédéric France <frederic.france@netlogic.fr>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <https://www.gnu.org/licenses/>.
26  */
27 
33 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
34 
35 
39 class Account extends CommonObject
40 {
44  public $element = 'bank_account';
45 
49  public $table_element = 'bank_account';
50 
54  public $picto = 'account';
55 
61  public $rowid;
62 
67  public $label;
68 
73  public $courant;
74 
79  public $type;
80 
85  public $bank;
86 
91  public $clos = self::STATUS_OPEN;
92 
97  public $rappro = 1;
98 
103  public $url;
104 
109  public $code_banque;
110 
115  public $code_guichet;
116 
121  public $number;
122 
127  public $cle_rib;
128 
133  public $bic;
134 
139  public $iban;
140 
147  public $iban_prefix;
148 
153  public $domiciliation;
154 
159  public $pti_in_ctti = 0;
160 
165  public $proprio;
166 
171  public $owner_address;
172 
173  public $state_id;
174  public $state_code;
175  public $state;
176 
182  public $type_lib = array();
183 
189  public $status = array();
190 
195  public $account_number;
196 
200  public $fk_accountancy_journal;
204  public $accountancy_journal;
205 
210  public $currency_code;
211 
217  public $account_currency_code;
218 
223  public $min_allowed;
224 
229  public $min_desired;
230 
235  public $comment;
236 
241  public $date_solde;
242 
249  public $solde;
250 
255  public $balance;
256 
261  public $ics;
262 
267  public $ics_transfer;
268 
269 
270 
295  // BEGIN MODULEBUILDER PROPERTIES
299  public $fields = array(
300  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
301  'ref' =>array('type'=>'varchar(12)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>25),
302  'label' =>array('type'=>'varchar(30)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>30),
303  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>35, 'index'=>1),
304  'bank' =>array('type'=>'varchar(60)', 'label'=>'Bank', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
305  'code_banque' =>array('type'=>'varchar(128)', 'label'=>'Code banque', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
306  'code_guichet' =>array('type'=>'varchar(6)', 'label'=>'Code guichet', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
307  'number' =>array('type'=>'varchar(255)', 'label'=>'Number', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
308  'cle_rib' =>array('type'=>'varchar(5)', 'label'=>'Cle rib', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
309  'bic' =>array('type'=>'varchar(11)', 'label'=>'Bic', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
310  'iban_prefix' =>array('type'=>'varchar(34)', 'label'=>'Iban prefix', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
311  'country_iban' =>array('type'=>'varchar(2)', 'label'=>'Country iban', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
312  'cle_iban' =>array('type'=>'varchar(2)', 'label'=>'Cle iban', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
313  'domiciliation' =>array('type'=>'varchar(255)', 'label'=>'Domiciliation', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
314  'state_id' =>array('type'=>'integer', 'label'=>'State id', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
315  'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>95),
316  'proprio' =>array('type'=>'varchar(60)', 'label'=>'Proprio', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
317  'owner_address' =>array('type'=>'text', 'label'=>'Owner address', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
318  'courant' =>array('type'=>'smallint(6)', 'label'=>'Courant', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>110),
319  'clos' =>array('type'=>'smallint(6)', 'label'=>'Clos', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>115),
320  'rappro' =>array('type'=>'smallint(6)', 'label'=>'Rappro', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
321  'url' =>array('type'=>'varchar(128)', 'label'=>'Url', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
322  'account_number' =>array('type'=>'varchar(32)', 'label'=>'Account number', 'enabled'=>1, 'visible'=>-1, 'position'=>130),
323  'fk_accountancy_journal' =>array('type'=>'integer', 'label'=>'Accountancy journal ID', 'enabled'=>1, 'visible'=>-1, 'position'=>132),
324  'accountancy_journal' =>array('type'=>'varchar(20)', 'label'=>'Accountancy journal', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
325  'currency_code' =>array('type'=>'varchar(3)', 'label'=>'Currency code', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>140),
326  'min_allowed' =>array('type'=>'integer', 'label'=>'Min allowed', 'enabled'=>1, 'visible'=>-1, 'position'=>145),
327  'min_desired' =>array('type'=>'integer', 'label'=>'Min desired', 'enabled'=>1, 'visible'=>-1, 'position'=>150),
328  'comment' =>array('type'=>'text', 'label'=>'Comment', 'enabled'=>1, 'visible'=>-1, 'position'=>155),
329  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>156),
330  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>157),
331  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>160),
332  'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>165),
333  'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>170),
334  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>175),
335  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>180),
336  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
337  );
338  // END MODULEBUILDER PROPERTIES
339 
343  const TYPE_CURRENT = 1;
347  const TYPE_CASH = 2;
351  const TYPE_SAVINGS = 0;
352 
353 
354  const STATUS_OPEN = 0;
355  const STATUS_CLOSED = 1;
356 
357 
363  public function __construct(DoliDB $db)
364  {
365  global $langs;
366 
367  $this->db = $db;
368 
369  $this->solde = 0;
370 
371  $this->type_lib = array(
372  self::TYPE_SAVINGS => $langs->trans("BankType0"),
373  self::TYPE_CURRENT => $langs->trans("BankType1"),
374  self::TYPE_CASH => $langs->trans("BankType2"),
375  );
376 
377  $this->status = array(
378  self::STATUS_OPEN => $langs->trans("StatusAccountOpened"),
379  self::STATUS_CLOSED => $langs->trans("StatusAccountClosed")
380  );
381  }
382 
388  public function __toString()
389  {
390  $string = '';
391  foreach ($this->getFieldsToShow() as $val) {
392  if ($val == 'BankCode') {
393  $string .= $this->code_banque.' ';
394  } elseif ($val == 'BankAccountNumber') {
395  $string .= $this->number.' ';
396  } elseif ($val == 'DeskCode') {
397  $string .= $this->code_guichet.' ';
398  } elseif ($val == 'BankAccountNumberKey') {
399  $string .= $this->cle_rib.' ';
400  } elseif ($val == 'BIC') {
401  $string .= $this->bic.' ';
402  } elseif ($val == 'IBAN') {
403  $string .= $this->iban.' ';
404  }
405  }
406 
407  return trim($string);
408  }
409 
410 
416  public function canBeConciliated()
417  {
418  global $conf;
419 
420  if (empty($this->rappro)) {
421  return -1;
422  }
423  if ($this->courant == Account::TYPE_CASH && empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
424  return -2;
425  }
426  if ($this->clos) {
427  return -3;
428  }
429  return 1;
430  }
431 
432 
433  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
444  public function add_url_line($line_id, $url_id, $url, $label, $type)
445  {
446  // phpcs:enable
447  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
448  $sql .= "fk_bank";
449  $sql .= ", url_id";
450  $sql .= ", url"; // deprecated
451  $sql .= ", label";
452  $sql .= ", type";
453  $sql .= ") VALUES (";
454  $sql .= " ".((int) $line_id);
455  $sql .= ", ".((int) $url_id);
456  $sql .= ", '".$this->db->escape($url)."'"; // dperecated
457  $sql .= ", '".$this->db->escape($label)."'";
458  $sql .= ", '".$this->db->escape($type)."'";
459  $sql .= ")";
460 
461  dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
462  if ($this->db->query($sql)) {
463  $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
464  return $rowid;
465  } else {
466  $this->error = $this->db->lasterror();
467  return -1;
468  }
469  }
470 
471  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
481  public function get_url($fk_bank = '', $url_id = '', $type = '')
482  {
483  // phpcs:enable
484  $lines = array();
485 
486  // Check parameters
487  if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) {
488  $this->error = "ErrorBadParameter";
489  return -1;
490  }
491 
492  $sql = "SELECT fk_bank, url_id, url, label, type";
493  $sql .= " FROM ".MAIN_DB_PREFIX."bank_url";
494  if ($fk_bank > 0) {
495  $sql .= " WHERE fk_bank = ".((int) $fk_bank);
496  } else {
497  $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'";
498  }
499  $sql .= " ORDER BY type, label";
500 
501  dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
502  $result = $this->db->query($sql);
503  if ($result) {
504  $i = 0;
505  $num = $this->db->num_rows($result);
506  while ($i < $num) {
507  $obj = $this->db->fetch_object($result);
508  // Anciens liens (pour compatibilite)
509  $lines[$i][0] = $obj->url;
510  $lines[$i][1] = $obj->url_id;
511  $lines[$i][2] = $obj->label;
512  $lines[$i][3] = $obj->type;
513  // Nouveaux liens
514  $lines[$i]['url'] = $obj->url;
515  $lines[$i]['url_id'] = $obj->url_id;
516  $lines[$i]['label'] = $obj->label;
517  $lines[$i]['type'] = $obj->type;
518  $lines[$i]['fk_bank'] = $obj->fk_bank;
519  $i++;
520  }
521  } else {
522  dol_print_error($this->db);
523  }
524 
525  return $lines;
526  }
527 
546  public function addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur = '', $banque = '', $accountancycode = '', $datev = null, $num_releve = '', $amount_main_currency = null)
547  {
548  global $langs;
549 
550  // Deprecation warning
551  if (is_numeric($oper)) {
552  dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING);
553  }
554 
555  if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
556  $this->id = $this->rowid;
557  }
558 
559  // Clean parameters
560  $emetteur = trim($emetteur);
561  $banque = trim($banque);
562  $label = trim($label);
563 
564  $now = dol_now();
565 
566  if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid
567  $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
568  $sql .= " WHERE id = ".((int) $oper);
569  $sql .= " AND entity IN (".getEntity('c_paiement').")";
570  $resql = $this->db->query($sql);
571  if ($resql) {
572  $obj = $this->db->fetch_object($resql);
573  $oper = $obj->code;
574  } else {
575  dol_print_error($this->db, 'Failed to get payment type code');
576  return -1;
577  }
578  }
579 
580  // Check parameters
581  if (!$oper) {
582  $this->error = $langs->trans("OperNotDefined");
583  return -1;
584  }
585  if (!$this->id) {
586  $this->error = $langs->trans("ThisIdNotDefined");
587  return -2;
588  }
589  if ($this->courant == Account::TYPE_CASH && $oper != 'LIQ') {
590  $this->error = "ErrorCashAccountAcceptsOnlyCashMoney";
591  return -3;
592  }
593 
594  $this->db->begin();
595 
596  if (is_null($datev) || empty($datev)) {
597  $datev = $date;
598  }
599 
600  $accline = new AccountLine($this->db);
601  $accline->datec = $now;
602  $accline->dateo = $date;
603  $accline->datev = $datev;
604  $accline->label = $label;
605  $accline->amount = $amount;
606  $accline->amount_main_currency = $amount_main_currency;
607  $accline->fk_user_author = $user->id;
608  $accline->fk_account = $this->id;
609  $accline->fk_type = $oper;
610  $accline->numero_compte = $accountancycode;
611  $accline->num_releve = $num_releve;
612 
613  if ($num_chq) {
614  $accline->num_chq = $num_chq;
615  }
616 
617  if ($emetteur) {
618  $accline->emetteur = $emetteur;
619  }
620 
621  if ($banque) {
622  $accline->bank_chq = $banque;
623  }
624 
625  if ($accline->insert() > 0) {
626  if ($categorie > 0) {
627  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class(";
628  $sql .= "lineid, fk_categ";
629  $sql .= ") VALUES (";
630  $sql .= ((int) $accline->id).", '".$this->db->escape($categorie)."'";
631  $sql .= ")";
632 
633  $result = $this->db->query($sql);
634  if (!$result) {
635  $this->error = $this->db->lasterror();
636  $this->db->rollback();
637 
638  return -4;
639  }
640  }
641 
642  $this->db->commit();
643 
644  return $accline->id;
645  } else {
646  $this->error = $accline->error;
647  $this->errors = $accline->errors;
648  $this->db->rollback();
649 
650  return -5;
651  }
652  }
653 
661  public function create(User $user, $notrigger = 0)
662  {
663  global $langs, $conf;
664 
665  $error = 0;
666 
667  // Clean parameters
668  if (!$this->min_allowed) {
669  $this->min_allowed = 0;
670  }
671  if (!$this->min_desired) {
672  $this->min_desired = 0;
673  }
674 
675  // Check parameters
676  if (empty($this->country_id)) {
677  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
678  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
679  return -1;
680  }
681  if (empty($this->ref)) {
682  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
683  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
684  return -1;
685  }
686  if (empty($this->date_solde)) {
687  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance"));
688  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
689  return -1;
690  }
691 
692  $balance = $this->balance;
693  if (empty($balance) && !empty($this->solde)) {
694  $balance = $this->solde;
695  }
696  if (empty($balance)) {
697  $balance = 0;
698  }
699 
700  // Chargement librairie pour acces fonction controle RIB
701  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
702 
703  $now = dol_now();
704 
705  $this->db->begin();
706 
707  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
708  $sql .= "datec";
709  $sql .= ", ref";
710  $sql .= ", label";
711  $sql .= ", entity";
712  $sql .= ", account_number";
713  $sql .= ", fk_accountancy_journal";
714  $sql .= ", bank";
715  $sql .= ", code_banque";
716  $sql .= ", code_guichet";
717  $sql .= ", number";
718  $sql .= ", cle_rib";
719  $sql .= ", bic";
720  $sql .= ", iban_prefix";
721  $sql .= ", domiciliation";
722  $sql .= ", pti_in_ctti";
723  $sql .= ", proprio";
724  $sql .= ", owner_address";
725  $sql .= ", currency_code";
726  $sql .= ", rappro";
727  $sql .= ", min_allowed";
728  $sql .= ", min_desired";
729  $sql .= ", comment";
730  $sql .= ", state_id";
731  $sql .= ", fk_pays";
732  $sql .= ", ics";
733  $sql .= ", ics_transfer";
734  $sql .= ") VALUES (";
735  $sql .= "'".$this->db->idate($now)."'";
736  $sql .= ", '".$this->db->escape($this->ref)."'";
737  $sql .= ", '".$this->db->escape($this->label)."'";
738  $sql .= ", ".((int) $conf->entity);
739  $sql .= ", '".$this->db->escape($this->account_number)."'";
740  $sql .= ", ".($this->fk_accountancy_journal > 0 ? $this->db->escape($this->fk_accountancy_journal) : "null");
741  $sql .= ", '".$this->db->escape($this->bank)."'";
742  $sql .= ", '".$this->db->escape($this->code_banque)."'";
743  $sql .= ", '".$this->db->escape($this->code_guichet)."'";
744  $sql .= ", '".$this->db->escape($this->number)."'";
745  $sql .= ", '".$this->db->escape($this->cle_rib)."'";
746  $sql .= ", '".$this->db->escape($this->bic)."'";
747  $sql .= ", '".$this->db->escape($this->iban)."'";
748  $sql .= ", '".$this->db->escape($this->domiciliation)."'";
749  $sql .= ", ".((int) $this->pti_in_ctti);
750  $sql .= ", '".$this->db->escape($this->proprio)."'";
751  $sql .= ", '".$this->db->escape($this->owner_address)."'";
752  $sql .= ", '".$this->db->escape($this->currency_code)."'";
753  $sql .= ", ".((int) $this->rappro);
754  $sql .= ", ".price2num($this->min_allowed, 'MT');
755  $sql .= ", ".price2num($this->min_desired, 'MT');
756  $sql .= ", '".$this->db->escape($this->comment)."'";
757  $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
758  $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
759  $sql .= ", '".$this->db->escape($this->ics)."'";
760  $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
761  $sql .= ")";
762 
763  dol_syslog(get_class($this)."::create", LOG_DEBUG);
764  $resql = $this->db->query($sql);
765  if ($resql) {
766  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
767 
768  $result = $this->update($user, 1);
769  if ($result > 0) {
770  $accline = new AccountLine($this->db);
771  $accline->datec = $this->db->idate($now);
772  $accline->label = '('.$langs->trans("InitialBankBalance").')';
773  $accline->amount = price2num($balance);
774  $accline->fk_user_author = $user->id;
775  $accline->fk_account = $this->id;
776  $accline->datev = $this->db->idate($this->date_solde);
777  $accline->dateo = $this->db->idate($this->date_solde);
778  $accline->fk_type = 'SOLD';
779 
780  if ($accline->insert() < 0) {
781  $error++;
782  $this->error = $accline->error;
783  $this->errors = $accline->errors;
784  }
785 
786  if (!$error) {
787  $result = $this->insertExtraFields();
788  if ($result < 0) {
789  $error++;
790  }
791  }
792 
793  if (!$error && !$notrigger) {
794  // Call trigger
795  $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
796  if ($result < 0) {
797  $error++;
798  }
799  // End call triggers
800  }
801  } else {
802  $error++;
803  }
804  } else {
805  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
806  $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
807  $error++;
808  } else {
809  $this->error = $this->db->error()." sql=".$sql;
810  $error++;
811  }
812  }
813 
814  if (!$error) {
815  $this->db->commit();
816  return $this->id;
817  } else {
818  $this->db->rollback();
819  return -1 * $error;
820  }
821  }
822 
830  public function update(User $user, $notrigger = 0)
831  {
832  global $langs, $conf;
833 
834  $error = 0;
835 
836  $this->db->begin();
837 
838  // Check parameters
839  if (empty($this->country_id)) {
840  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
841  dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
842  return -1;
843  }
844  if (empty($this->ref)) {
845  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
846  dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
847  return -1;
848  }
849  if (!$this->label) {
850  $this->label = "???";
851  }
852 
853  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
854 
855  $sql .= " ref = '".$this->db->escape($this->ref)."'";
856  $sql .= ",label = '".$this->db->escape($this->label)."'";
857 
858  $sql .= ",courant = ".((int) $this->courant);
859  $sql .= ",clos = ".((int) $this->clos);
860  $sql .= ",rappro = ".((int) $this->rappro);
861  $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
862  $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
863  $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? $this->db->escape($this->fk_accountancy_journal) : "null");
864  $sql .= ",bank = '".$this->db->escape($this->bank)."'";
865  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
866  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
867  $sql .= ",number='".$this->db->escape($this->number)."'";
868  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
869  $sql .= ",bic='".$this->db->escape($this->bic)."'";
870  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
871  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
872  $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
873  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
874  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
875 
876  $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
877 
878  $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
879  $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
880  $sql .= ",comment = '".$this->db->escape($this->comment)."'";
881 
882  $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
883  $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
884  $sql .= ",ics = '".$this->db->escape($this->ics)."'";
885  $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
886 
887  $sql .= " WHERE rowid = ".((int) $this->id);
888 
889  dol_syslog(get_class($this)."::update", LOG_DEBUG);
890  $result = $this->db->query($sql);
891  if ($result) {
892  // Actions on extra fields (by external module or standard code)
893  if (!$error) {
894  $result = $this->insertExtraFields();
895  if ($result < 0) {
896  $error++;
897  }
898  }
899 
900  if (!$error && !$notrigger) {
901  // Call trigger
902  $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
903  if ($result < 0) {
904  $error++;
905  }
906  // End call triggers
907  }
908  } else {
909  $error++;
910  $this->error = $this->db->lasterror();
911  dol_print_error($this->db);
912  }
913 
914  if (!$error) {
915  $this->db->commit();
916  return $this->id;
917  } else {
918  $this->db->rollback();
919  return -1 * $error;
920  }
921  }
922 
923 
924  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
931  public function update_bban(User $user = null)
932  {
933  // phpcs:enable
934  global $conf, $langs;
935 
936  // Load library to get BAN control function
937  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
938 
939  dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
940 
941  // Check parameters
942  if (!$this->ref) {
943  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
944  return -2;
945  }
946 
947  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
948  $sql .= " bank = '".$this->db->escape($this->bank)."'";
949  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
950  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
951  $sql .= ",number='".$this->db->escape($this->number)."'";
952  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
953  $sql .= ",bic='".$this->db->escape($this->bic)."'";
954  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
955  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
956  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
957  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
958  $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null");
959  $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null");
960  $sql .= " WHERE rowid = ".((int) $this->id);
961  $sql .= " AND entity = ".((int) $conf->entity);
962 
963  dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
964 
965  $result = $this->db->query($sql);
966  if ($result) {
967  return 1;
968  } else {
969  $this->error = $this->db->lasterror();
970  dol_print_error($this->db);
971  return -1;
972  }
973  }
974 
975 
983  public function fetch($id, $ref = '')
984  {
985  if (empty($id) && empty($ref)) {
986  $this->error = "ErrorBadParameters";
987  return -1;
988  }
989 
990  $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,";
991  $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
992  $sql .= " ba.domiciliation, ba.pti_in_ctti, ba.proprio, ba.owner_address, ba.state_id, ba.fk_pays as country_id,";
993  $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
994  $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
995  $sql .= " ba.datec as date_creation, ba.tms as date_update, ba.ics, ba.ics_transfer,";
996  $sql .= ' c.code as country_code, c.label as country,';
997  $sql .= ' d.code_departement as state_code, d.nom as state';
998  $sql .= ' , aj.code as accountancy_journal';
999  $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1000  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
1001  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
1002  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal';
1003  $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
1004  if ($id) {
1005  $sql .= " AND ba.rowid = ".((int) $id);
1006  }
1007  if ($ref) {
1008  $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
1009  }
1010 
1011  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1012  $result = $this->db->query($sql);
1013  if ($result) {
1014  if ($this->db->num_rows($result)) {
1015  $obj = $this->db->fetch_object($result);
1016 
1017  $this->id = $obj->rowid;
1018  $this->rowid = $obj->rowid;
1019  $this->ref = $obj->ref;
1020  $this->label = $obj->label;
1021  $this->type = $obj->courant;
1022  $this->courant = $obj->courant;
1023  $this->bank = $obj->bank;
1024  $this->clos = $obj->clos;
1025  $this->rappro = $obj->rappro;
1026  $this->url = $obj->url;
1027 
1028  $this->code_banque = $obj->code_banque;
1029  $this->code_guichet = $obj->code_guichet;
1030  $this->number = $obj->number;
1031  $this->cle_rib = $obj->cle_rib;
1032  $this->bic = $obj->bic;
1033  $this->iban = $obj->iban;
1034  $this->domiciliation = $obj->domiciliation;
1035  $this->pti_in_ctti = $obj->pti_in_ctti;
1036  $this->proprio = $obj->proprio;
1037  $this->owner_address = $obj->owner_address;
1038 
1039  $this->state_id = $obj->state_id;
1040  $this->state_code = $obj->state_code;
1041  $this->state = $obj->state;
1042 
1043  $this->country_id = $obj->country_id;
1044  $this->country_code = $obj->country_code;
1045  $this->country = $obj->country;
1046 
1047  $this->account_number = $obj->account_number;
1048  $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
1049  $this->accountancy_journal = $obj->accountancy_journal;
1050 
1051  $this->currency_code = $obj->currency_code;
1052  $this->account_currency_code = $obj->currency_code;
1053  $this->min_allowed = $obj->min_allowed;
1054  $this->min_desired = $obj->min_desired;
1055  $this->comment = $obj->comment;
1056 
1057  $this->date_creation = $this->db->jdate($obj->date_creation);
1058  $this->date_update = $this->db->jdate($obj->date_update);
1059 
1060  $this->ics = $obj->ics;
1061  $this->ics_transfer = $obj->ics_transfer;
1062 
1063  // Retrieve all extrafield
1064  // fetch optionals attributes and labels
1065  $this->fetch_optionals();
1066 
1067  return 1;
1068  } else {
1069  return 0;
1070  }
1071  } else {
1072  $this->error = $this->db->lasterror;
1073  $this->errors[] = $this->error;
1074  return -1;
1075  }
1076  }
1077 
1088  public function setCategories($categories)
1089  {
1090  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1091  return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
1092  }
1093 
1100  public function delete(User $user = null)
1101  {
1102  $error = 0;
1103 
1104  $this->db->begin();
1105 
1106  // @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
1107 
1108  // Delete link between tag and bank account
1109  if (!$error) {
1110  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1111  $sql .= " WHERE fk_account = ".((int) $this->id);
1112 
1113  $resql = $this->db->query($sql);
1114  if (!$resql) {
1115  $error++;
1116  $this->error = "Error ".$this->db->lasterror();
1117  }
1118  }
1119 
1120  if (!$error) {
1121  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
1122  $sql .= " WHERE rowid = ".((int) $this->id);
1123 
1124  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1125  $result = $this->db->query($sql);
1126  if ($result) {
1127  // Remove extrafields
1128  if (!$error) {
1129  $result = $this->deleteExtraFields();
1130  if ($result < 0) {
1131  $error++;
1132  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1133  }
1134  }
1135  } else {
1136  $error++;
1137  $this->error = "Error ".$this->db->lasterror();
1138  }
1139  }
1140 
1141  if (!$error) {
1142  $this->db->commit();
1143  return 1;
1144  } else {
1145  $this->db->rollback();
1146  return -1;
1147  }
1148  }
1149 
1150 
1157  public function getLibStatut($mode = 0)
1158  {
1159  return $this->LibStatut($this->clos, $mode);
1160  }
1161 
1162  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1170  public function LibStatut($status, $mode = 0)
1171  {
1172  // phpcs:enable
1173  global $langs;
1174  $langs->load('banks');
1175 
1176  if ($status == self::STATUS_OPEN) {
1177  $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1178  $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1179  $statusType = 'status4';
1180  } else {
1181  $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1182  $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1183  $statusType = 'status5';
1184  }
1185 
1186  return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1187  }
1188 
1189 
1190  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1196  public function can_be_deleted()
1197  {
1198  // phpcs:enable
1199  $can_be_deleted = false;
1200 
1201  $sql = "SELECT COUNT(rowid) as nb";
1202  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1203  $sql .= " WHERE fk_account = ".((int) $this->id);
1204 
1205  $resql = $this->db->query($sql);
1206  if ($resql) {
1207  $obj = $this->db->fetch_object($resql);
1208  if ($obj->nb <= 1) {
1209  $can_be_deleted = true; // Juste le solde
1210  }
1211  } else {
1212  dol_print_error($this->db);
1213  }
1214  return $can_be_deleted;
1215  }
1216 
1217 
1223  public function error()
1224  {
1225  return $this->error;
1226  }
1227 
1236  public function solde($option = 0, $date_end = '', $field = 'dateo')
1237  {
1238  $solde = 0;
1239 
1240  $sql = "SELECT sum(amount) as amount";
1241  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1242  $sql .= " WHERE fk_account = ".((int) $this->id);
1243  if ($option == 1) {
1244  $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1245  }
1246 
1247  $resql = $this->db->query($sql);
1248  if ($resql) {
1249  if ($this->db->num_rows($resql)) {
1250  $obj = $this->db->fetch_object($resql);
1251  $solde = $obj->amount;
1252  }
1253  $this->db->free($resql);
1254  } else {
1255  $this->errors[] = $this->db->lasterror;
1256  return -1;
1257  }
1258 
1259  return price2num($solde, 'MU');
1260  }
1261 
1262  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1270  public function load_board(User $user, $filteraccountid = 0)
1271  {
1272  // phpcs:enable
1273  global $conf, $langs;
1274 
1275  if ($user->socid) {
1276  return -1; // protection pour eviter appel par utilisateur externe
1277  }
1278 
1279  $sql = "SELECT b.rowid, b.datev as datefin";
1280  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1281  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1282  $sql .= " WHERE b.rappro=0";
1283  $sql .= " AND b.fk_account = ba.rowid";
1284  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1285  $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1286  $sql .= " AND clos = 0";
1287  if ($filteraccountid) {
1288  $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1289  }
1290 
1291  $resql = $this->db->query($sql);
1292  if ($resql) {
1293  $langs->load("banks");
1294  $now = dol_now();
1295 
1296  require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1297 
1298  $response = new WorkboardResponse();
1299  $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1300  $response->label = $langs->trans("TransactionsToConciliate");
1301  $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1302  $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1303  $response->img = img_object('', "payment");
1304 
1305  while ($obj = $this->db->fetch_object($resql)) {
1306  $response->nbtodo++;
1307  if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1308  $response->nbtodolate++;
1309  }
1310  }
1311 
1312  return $response;
1313  } else {
1314  dol_print_error($this->db);
1315  $this->error = $this->db->error();
1316  return -1;
1317  }
1318  }
1319 
1320  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1326  public function load_state_board($filteraccountid = 0)
1327  {
1328  // phpcs:enable
1329  global $user;
1330 
1331  if ($user->socid) {
1332  return -1; // protection pour eviter appel par utilisateur externe
1333  }
1334 
1335  $sql = "SELECT count(b.rowid) as nb";
1336  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1337  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1338  $sql .= " WHERE b.fk_account = ba.rowid";
1339  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1340  $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1341  $sql .= " AND clos = 0";
1342  if ($filteraccountid) {
1343  $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1344  }
1345 
1346  $resql = $this->db->query($sql);
1347  if ($resql) {
1348  while ($obj = $this->db->fetch_object($resql)) {
1349  $this->nb["banklines"] = $obj->nb;
1350  }
1351  $this->db->free($resql);
1352  } else {
1353  dol_print_error($this->db);
1354  $this->error = $this->db->error();
1355  return -1;
1356  }
1357  }
1358 
1359 
1365  public function countAccountToReconcile()
1366  {
1367  global $db, $conf, $user;
1368 
1369  //Protection against external users
1370  if ($user->socid) {
1371  return 0;
1372  }
1373 
1374  $nb = 0;
1375 
1376  $sql = "SELECT COUNT(ba.rowid) as nb";
1377  $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1378  $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1379  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1380  if (empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
1381  $sql .= " AND ba.courant != 2";
1382  }
1383  $resql = $this->db->query($sql);
1384  if ($resql) {
1385  $obj = $this->db->fetch_object($resql);
1386  $nb = $obj->nb;
1387  } else {
1388  dol_print_error($this->db);
1389  }
1390 
1391  return $nb;
1392  }
1393 
1404  public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0)
1405  {
1406  global $conf, $langs, $user;
1407  include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
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> '.getIbanHumanReadable($this);
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  // Retrieve all extrafield
1977  // fetch optionals attributes and labels
1978  $this->fetch_optionals();
1979 
1980  $ret = 1;
1981  }
1982  $this->db->free($result);
1983  return $ret;
1984  } else {
1985  return -1;
1986  }
1987  }
1988 
1994  public function insert()
1995  {
1996  $error = 0;
1997 
1998  $this->db->begin();
1999 
2000  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
2001  $sql .= "datec";
2002  $sql .= ", dateo";
2003  $sql .= ", datev";
2004  $sql .= ", label";
2005  $sql .= ", amount";
2006  $sql .= ", amount_main_currency";
2007  $sql .= ", fk_user_author";
2008  $sql .= ", num_chq";
2009  $sql .= ", fk_account";
2010  $sql .= ", fk_type";
2011  $sql .= ", emetteur,banque";
2012  $sql .= ", rappro";
2013  $sql .= ", numero_compte";
2014  $sql .= ", num_releve";
2015  $sql .= ") VALUES (";
2016  $sql .= "'".$this->db->idate($this->datec)."'";
2017  $sql .= ", '".$this->db->idate($this->dateo)."'";
2018  $sql .= ", '".$this->db->idate($this->datev)."'";
2019  $sql .= ", '".$this->db->escape($this->label)."'";
2020  $sql .= ", ".price2num($this->amount);
2021  $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
2022  $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
2023  $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
2024  $sql .= ", '".$this->db->escape($this->fk_account)."'";
2025  $sql .= ", '".$this->db->escape($this->fk_type)."'";
2026  $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
2027  $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
2028  $sql .= ", ".(int) $this->rappro;
2029  $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2030  $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2031  $sql .= ")";
2032 
2033  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2034  $resql = $this->db->query($sql);
2035  if ($resql) {
2036  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2037  // Actions on extra fields (by external module or standard code)
2038  $result = $this->insertExtraFields();
2039  if ($result < 0) {
2040  $error++;
2041  }
2042  } else {
2043  $error++;
2044  $this->error = $this->db->lasterror();
2045  dol_print_error($this->db);
2046  }
2047 
2048  if (!$error) {
2049  $this->db->commit();
2050  return $this->id;
2051  } else {
2052  $this->db->rollback();
2053  return -1 * $error;
2054  }
2055  }
2056 
2063  public function delete(User $user = null)
2064  {
2065  global $conf;
2066 
2067  $nbko = 0;
2068 
2069  if ($this->rappro) {
2070  // Protection to avoid any delete of consolidated lines
2071  $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2072  return -1;
2073  }
2074 
2075  $this->db->begin();
2076 
2077  // Protection to avoid any delete of accounted lines. Protection on by default
2078  if (empty($conf->global->BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING)) {
2079  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2080  $resql = $this->db->query($sql);
2081  if ($resql) {
2082  $obj = $this->db->fetch_object($resql);
2083  if ($obj && $obj->nb) {
2084  $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2085  $this->db->rollback();
2086  return -1;
2087  }
2088  } else {
2089  $this->error = $this->db->lasterror();
2090  $this->db->rollback();
2091  return -1;
2092  }
2093  }
2094 
2095  // Delete urls
2096  $result = $this->delete_urls($user);
2097  if ($result < 0) {
2098  $nbko++;
2099  }
2100 
2101  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".(int) $this->rowid;
2102  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2103  $result = $this->db->query($sql);
2104  if (!$result) {
2105  $nbko++;
2106  }
2107 
2108  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_extrafields WHERE fk_object=".(int) $this->rowid;
2109  $result = $this->db->query($sql);
2110  if (!$result) {
2111  $nbko++;
2112  }
2113 
2114  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2115  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2116  $result = $this->db->query($sql);
2117  if (!$result) {
2118  $nbko++;
2119  }
2120 
2121  if (!$nbko) {
2122  $this->db->commit();
2123  return 1;
2124  } else {
2125  $this->db->rollback();
2126  return -$nbko;
2127  }
2128  }
2129 
2130 
2131  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2138  public function delete_urls(User $user = null)
2139  {
2140  // phpcs:enable
2141  $nbko = 0;
2142 
2143  if ($this->rappro) {
2144  // Protection to avoid any delete of consolidated lines
2145  $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2146  return -1;
2147  }
2148 
2149  $this->db->begin();
2150 
2151  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2152  dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
2153  $result = $this->db->query($sql);
2154  if (!$result) {
2155  $nbko++;
2156  }
2157 
2158  if (!$nbko) {
2159  $this->db->commit();
2160  return 1;
2161  } else {
2162  $this->db->rollback();
2163  return -$nbko;
2164  }
2165  }
2166 
2167 
2175  public function update(User $user, $notrigger = 0)
2176  {
2177  $this->db->begin();
2178 
2179  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2180  $sql .= " amount = ".price2num($this->amount).",";
2181  $sql .= " datev='".$this->db->idate($this->datev)."',";
2182  $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2183  $sql .= " WHERE rowid = ".((int) $this->rowid);
2184 
2185  dol_syslog(get_class($this)."::update", LOG_DEBUG);
2186  $resql = $this->db->query($sql);
2187  if ($resql) {
2188  $this->db->commit();
2189  return 1;
2190  } else {
2191  $this->db->rollback();
2192  $this->error = $this->db->error();
2193  return -1;
2194  }
2195  }
2196 
2197 
2198  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2207  public function update_conciliation(User $user, $cat, $conciliated = 1)
2208  {
2209  // phpcs:enable
2210  global $conf, $langs;
2211 
2212  $this->db->begin();
2213 
2214  // Check statement field
2215  if (!empty($conf->global->BANK_STATEMENT_REGEX_RULE)) {
2216  if (!preg_match('/'.$conf->global->BANK_STATEMENT_REGEX_RULE.'/', $this->num_releve)) {
2217  $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", $conf->global->BANK_STATEMENT_REGEX_RULE);
2218  return -1;
2219  }
2220  }
2221 
2222  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2223  $sql .= " rappro = ".((int) $conciliated);
2224  $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2225  if ($conciliated) {
2226  $sql .= ", fk_user_rappro = ".$user->id;
2227  }
2228  $sql .= " WHERE rowid = ".((int) $this->id);
2229 
2230  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2231  $resql = $this->db->query($sql);
2232  if ($resql) {
2233  if (!empty($cat)) {
2234  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
2235  $sql .= "lineid";
2236  $sql .= ", fk_categ";
2237  $sql .= ") VALUES (";
2238  $sql .= $this->id;
2239  $sql .= ", ".((int) $cat);
2240  $sql .= ")";
2241 
2242  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2243  $this->db->query($sql);
2244 
2245  // No error check. Can fail if category already affected
2246  }
2247 
2248  $this->rappro = 1;
2249 
2250  $this->db->commit();
2251  return 1;
2252  } else {
2253  $this->db->rollback();
2254  return -1;
2255  }
2256  }
2257 
2258 
2259  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2267  public function datev_change($rowid, $sign = 1)
2268  {
2269  // phpcs:enable
2270  $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2271  $resql = $this->db->query($sql);
2272  if ($resql) {
2273  $obj = $this->db->fetch_object($resql);
2274  $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2275 
2276  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2277  $sql .= " datev = '".$this->db->idate($newdate)."'";
2278  $sql .= " WHERE rowid = ".((int) $rowid);
2279 
2280  $result = $this->db->query($sql);
2281  if ($result) {
2282  if ($this->db->affected_rows($result)) {
2283  return 1;
2284  }
2285  } else {
2286  dol_print_error($this->db);
2287  return 0;
2288  }
2289  } else {
2290  dol_print_error($this->db);
2291  }
2292  return 0;
2293  }
2294 
2295  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2302  public function datev_next($id)
2303  {
2304  // phpcs:enable
2305  return $this->datev_change($id, 1);
2306  }
2307 
2308  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2315  public function datev_previous($id)
2316  {
2317  // phpcs:enable
2318  return $this->datev_change($id, -1);
2319  }
2320 
2321 
2322  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2330  public function dateo_change($rowid, $sign = 1)
2331  {
2332  // phpcs:enable
2333  $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2334  $resql = $this->db->query($sql);
2335  if ($resql) {
2336  $obj = $this->db->fetch_object($resql);
2337  $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2338 
2339  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2340  $sql .= " dateo = '".$this->db->idate($newdate)."'";
2341  $sql .= " WHERE rowid = ".((int) $rowid);
2342 
2343  $result = $this->db->query($sql);
2344  if ($result) {
2345  if ($this->db->affected_rows($result)) {
2346  return 1;
2347  }
2348  } else {
2349  dol_print_error($this->db);
2350  return 0;
2351  }
2352  } else {
2353  dol_print_error($this->db);
2354  }
2355  return 0;
2356  }
2357 
2358  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2365  public function dateo_next($id)
2366  {
2367  // phpcs:enable
2368  return $this->dateo_change($id, 1);
2369  }
2370 
2371  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2378  public function dateo_previous($id)
2379  {
2380  // phpcs:enable
2381  return $this->dateo_change($id, -1);
2382  }
2383 
2384 
2391  public function info($id)
2392  {
2393  $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2394  $sql .= ' b.fk_user_author, b.fk_user_rappro';
2395  $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2396  $sql .= ' WHERE b.rowid = '.((int) $id);
2397 
2398  $result = $this->db->query($sql);
2399  if ($result) {
2400  if ($this->db->num_rows($result)) {
2401  $obj = $this->db->fetch_object($result);
2402  $this->id = $obj->rowid;
2403 
2404  if ($obj->fk_user_author) {
2405  $cuser = new User($this->db);
2406  $cuser->fetch($obj->fk_user_author);
2407  $this->user_creation = $cuser;
2408  }
2409  if ($obj->fk_user_rappro) {
2410  $ruser = new User($this->db);
2411  $ruser->fetch($obj->fk_user_rappro);
2412  $this->user_rappro = $ruser;
2413  }
2414 
2415  $this->date_creation = $this->db->jdate($obj->datec);
2416  $this->date_modification = $this->db->jdate($obj->datem);
2417  //$this->date_rappro = $obj->daterappro; // Not yet managed
2418  }
2419  $this->db->free($result);
2420  } else {
2421  dol_print_error($this->db);
2422  }
2423  }
2424 
2425 
2435  public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2436  {
2437  global $langs;
2438 
2439  $result = '';
2440 
2441  $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2442  $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2443 
2444  $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">';
2445  $linkend = '</a>';
2446 
2447  $result .= $linkstart;
2448  if ($withpicto) {
2449  $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);
2450  }
2451  if ($withpicto != 2) {
2452  $result .= ($this->ref ? $this->ref : $this->id);
2453  }
2454  $result .= $linkend;
2455 
2456  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2457  $result .= ' <span class="opacitymedium">(';
2458  }
2459  if ($option == 'showall') {
2460  $result .= $langs->trans("BankAccount").': ';
2461  $accountstatic = new Account($this->db);
2462  $accountstatic->id = $this->fk_account;
2463  $accountstatic->ref = $this->bank_account_ref;
2464  $accountstatic->label = $this->bank_account_label;
2465  $result .= $accountstatic->getNomUrl(0).', ';
2466  }
2467  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2468  $result .= $langs->trans("BankLineConciliated").': ';
2469  $result .= yn($this->rappro);
2470  }
2471  if ($option == 'showall' || $option == 'showconciliatedandaccounted') {
2472  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2473  $resql = $this->db->query($sql);
2474  if ($resql) {
2475  $obj = $this->db->fetch_object($resql);
2476  if ($obj && $obj->nb) {
2477  $result .= ' - '.$langs->trans("Accounted").': '.yn(1);
2478  } else {
2479  $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2480  }
2481  }
2482  }
2483  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2484  $result .= ')</span>';
2485  }
2486 
2487  return $result;
2488  }
2489 
2490 
2497  public function getLibStatut($mode = 0)
2498  {
2499  return $this->LibStatut($this->status, $mode);
2500  }
2501 
2502  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2510  public function LibStatut($status, $mode = 0)
2511  {
2512  // phpcs:enable
2513  global $langs;
2514  //$langs->load('companies');
2515  /*
2516  if ($mode == 0)
2517  {
2518  if ($status==0) return $langs->trans("ActivityCeased");
2519  if ($status==1) return $langs->trans("InActivity");
2520  }
2521  if ($mode == 1)
2522  {
2523  if ($status==0) return $langs->trans("ActivityCeased");
2524  if ($status==1) return $langs->trans("InActivity");
2525  }
2526  if ($mode == 2)
2527  {
2528  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2529  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2530  }
2531  if ($mode == 3)
2532  {
2533  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2534  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2535  }
2536  if ($mode == 4)
2537  {
2538  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2539  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2540  }
2541  if ($mode == 5)
2542  {
2543  if ($status==0) return $langs->trans("ActivityCeased").' '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2544  if ($status==1) return $langs->trans("InActivity").' '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2545  }*/
2546  }
2547 
2548 
2554  public function getVentilExportCompta()
2555  {
2556  $alreadydispatched = 0;
2557 
2558  $type = 'bank';
2559 
2560  $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);
2561  $resql = $this->db->query($sql);
2562  if ($resql) {
2563  $obj = $this->db->fetch_object($resql);
2564  if ($obj) {
2565  $alreadydispatched = $obj->nb;
2566  }
2567  } else {
2568  $this->error = $this->db->lasterror();
2569  return -1;
2570  }
2571 
2572  if ($alreadydispatched) {
2573  return 1;
2574  }
2575  return 0;
2576  }
2577 }
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
checkIbanForAccount(Account $account)
Check IBAN number informations for a bank account.
Definition: bank.lib.php:295
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition: bank.lib.php:317
checkSwiftForAccount($account)
Check SWIFT informations for a bank account.
Definition: bank.lib.php:279
$object ref
Definition: info.php:78
Class to manage bank accounts.
solde($option=0, $date_end='', $field='dateo')
Return current sold.
fetch($id, $ref='')
Load a bank account into memory from database.
__construct(DoliDB $db)
Constructor.
add_url_line($line_id, $url_id, $url, $label, $type)
Add a link between bank line record and its source.
static replaceThirdparty($dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
update(User $user, $notrigger=0)
Update bank account card.
getCountryCode()
Return account country code.
needIBAN()
Return 1 if IBAN / BIC is mandatory (otherwise option)
getLibStatut($mode=0)
Return label of object status.
error()
Return error.
info($id)
Load miscellaneous information for tab "Info".
const TYPE_CASH
Cash account.
__toString()
Shows the account number in the appropriate format.
const TYPE_SAVINGS
Savings account.
useDetailedBBAN()
Return if a bank account is defined with detailed information (bank code, desk code,...
static getAccountNumberOrder()
Returns the components of the bank account in order.
load_state_board($filteraccountid=0)
Charge indicateurs this->nb de tableau de bord.
canBeConciliated()
Return if a bank account need to be conciliated.
create(User $user, $notrigger=0)
Create bank account into database.
can_be_deleted()
Renvoi si un compte peut etre supprimer ou non (sans mouvements)
update_bban(User $user=null)
Update BBAN (RIB) account fields.
const TYPE_CURRENT
Current account.
setCategories($categories)
Sets object to supplied categories.
getNomUrl($withpicto=0, $mode='', $option='', $save_lastsearch_value=-1, $notooltip=0)
Return clicable name (with picto eventually)
countAccountToReconcile()
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
verif()
Return if an account has valid information for Direct debit payment.
initAsSpecimen()
Initialise an instance with random values.
get_url($fk_bank='', $url_id='', $type='')
TODO Move this into AccountLine Return array with links from llx_bank_url.
getFieldsToShow($includeibanbic=0)
Returns the fields in order that this bank account should show to the user Will return an array with ...
load_board(User $user, $filteraccountid=0)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
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.
LibStatut($status, $mode=0)
Return label of given object status.
Class to manage bank transaction lines.
info($id)
Load miscellaneous information for tab "Info".
__construct(DoliDB $db)
Constructor.
update_conciliation(User $user, $cat, $conciliated=1)
Update conciliation field.
getLibStatut($mode=0)
Return label of status (activity, closed)
fetch($rowid, $ref='', $num='')
Load into memory content of a bank transaction line.
dateo_change($rowid, $sign=1)
Increase/decrease operation date of a rowid.
datev_next($id)
Increase value date of a rowid.
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
update(User $user, $notrigger=0)
Update bank account record in database.
insert()
Inserts a transaction to a bank account.
getNomUrl($withpicto=0, $maxlen=0, $option='', $notooltip=0)
Return clickable name (with picto eventually)
delete_urls(User $user=null)
Delete bank line records.
getVentilExportCompta()
Return if a bank line was dispatched into bookkeeping.
datev_change($rowid, $sign=1)
Increase/decrease value date of a rowid.
dateo_previous($id)
Decrease operation date of a rowid.
dateo_next($id)
Increase operation date of a rowid.
datev_previous($id)
Decrease value date of a rowid.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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...
deleteExtraFields()
Delete all extra fields values for the current object.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
Definition: user.class.php:47
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("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->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
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.
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
$conf db
API class for accounts.
Definition: inc.php:41