dolibarr  17.0.0
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 
247  public $ics;
248 
253  public $ics_transfer;
254 
255 
256 
281  // BEGIN MODULEBUILDER PROPERTIES
285  public $fields = array(
286  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
287  'ref' =>array('type'=>'varchar(12)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>25),
288  'label' =>array('type'=>'varchar(30)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>30),
289  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>35, 'index'=>1),
290  'bank' =>array('type'=>'varchar(60)', 'label'=>'Bank', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
291  'code_banque' =>array('type'=>'varchar(128)', 'label'=>'Code banque', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
292  'code_guichet' =>array('type'=>'varchar(6)', 'label'=>'Code guichet', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
293  'number' =>array('type'=>'varchar(255)', 'label'=>'Number', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
294  'cle_rib' =>array('type'=>'varchar(5)', 'label'=>'Cle rib', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
295  'bic' =>array('type'=>'varchar(11)', 'label'=>'Bic', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
296  'iban_prefix' =>array('type'=>'varchar(34)', 'label'=>'Iban prefix', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
297  'country_iban' =>array('type'=>'varchar(2)', 'label'=>'Country iban', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
298  'cle_iban' =>array('type'=>'varchar(2)', 'label'=>'Cle iban', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
299  'domiciliation' =>array('type'=>'varchar(255)', 'label'=>'Domiciliation', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
300  'state_id' =>array('type'=>'integer', 'label'=>'State id', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
301  'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>95),
302  'proprio' =>array('type'=>'varchar(60)', 'label'=>'Proprio', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
303  'owner_address' =>array('type'=>'text', 'label'=>'Owner address', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
304  'courant' =>array('type'=>'smallint(6)', 'label'=>'Courant', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>110),
305  'clos' =>array('type'=>'smallint(6)', 'label'=>'Clos', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>115),
306  'rappro' =>array('type'=>'smallint(6)', 'label'=>'Rappro', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
307  'url' =>array('type'=>'varchar(128)', 'label'=>'Url', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
308  'account_number' =>array('type'=>'varchar(32)', 'label'=>'Account number', 'enabled'=>1, 'visible'=>-1, 'position'=>130),
309  'fk_accountancy_journal' =>array('type'=>'integer', 'label'=>'Accountancy journal ID', 'enabled'=>1, 'visible'=>-1, 'position'=>132),
310  'accountancy_journal' =>array('type'=>'varchar(20)', 'label'=>'Accountancy journal', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
311  'currency_code' =>array('type'=>'varchar(3)', 'label'=>'Currency code', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>140),
312  'min_allowed' =>array('type'=>'integer', 'label'=>'Min allowed', 'enabled'=>1, 'visible'=>-1, 'position'=>145),
313  'min_desired' =>array('type'=>'integer', 'label'=>'Min desired', 'enabled'=>1, 'visible'=>-1, 'position'=>150),
314  'comment' =>array('type'=>'text', 'label'=>'Comment', 'enabled'=>1, 'visible'=>-1, 'position'=>155),
315  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>156),
316  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>157),
317  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>160),
318  'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>165),
319  'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>170),
320  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>175),
321  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>180),
322  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
323  );
324  // END MODULEBUILDER PROPERTIES
325 
329  const TYPE_CURRENT = 1;
333  const TYPE_CASH = 2;
337  const TYPE_SAVINGS = 0;
338 
339 
340  const STATUS_OPEN = 0;
341  const STATUS_CLOSED = 1;
342 
343 
349  public function __construct(DoliDB $db)
350  {
351  global $langs;
352 
353  $this->db = $db;
354 
355  $this->solde = 0;
356 
357  $this->type_lib = array(
358  self::TYPE_SAVINGS => $langs->trans("BankType0"),
359  self::TYPE_CURRENT => $langs->trans("BankType1"),
360  self::TYPE_CASH => $langs->trans("BankType2"),
361  );
362 
363  $this->status = array(
364  self::STATUS_OPEN => $langs->trans("StatusAccountOpened"),
365  self::STATUS_CLOSED => $langs->trans("StatusAccountClosed")
366  );
367  }
368 
374  public function __toString()
375  {
376  $string = '';
377  foreach ($this->getFieldsToShow() as $val) {
378  if ($val == 'BankCode') {
379  $string .= $this->code_banque.' ';
380  } elseif ($val == 'BankAccountNumber') {
381  $string .= $this->number.' ';
382  } elseif ($val == 'DeskCode') {
383  $string .= $this->code_guichet.' ';
384  } elseif ($val == 'BankAccountNumberKey') {
385  $string .= $this->cle_rib.' ';
386  } elseif ($val == 'BIC') {
387  $string .= $this->bic.' ';
388  } elseif ($val == 'IBAN') {
389  $string .= $this->iban.' ';
390  }
391  }
392 
393  return trim($string);
394  }
395 
396 
402  public function canBeConciliated()
403  {
404  global $conf;
405 
406  if (empty($this->rappro)) {
407  return -1;
408  }
409  if ($this->courant == Account::TYPE_CASH && empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
410  return -2;
411  }
412  if ($this->clos) {
413  return -3;
414  }
415  return 1;
416  }
417 
418 
419  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
430  public function add_url_line($line_id, $url_id, $url, $label, $type)
431  {
432  // phpcs:enable
433  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
434  $sql .= "fk_bank";
435  $sql .= ", url_id";
436  $sql .= ", url"; // deprecated
437  $sql .= ", label";
438  $sql .= ", type";
439  $sql .= ") VALUES (";
440  $sql .= " ".((int) $line_id);
441  $sql .= ", ".((int) $url_id);
442  $sql .= ", '".$this->db->escape($url)."'"; // dperecated
443  $sql .= ", '".$this->db->escape($label)."'";
444  $sql .= ", '".$this->db->escape($type)."'";
445  $sql .= ")";
446 
447  dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
448  if ($this->db->query($sql)) {
449  $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
450  return $rowid;
451  } else {
452  $this->error = $this->db->lasterror();
453  return -1;
454  }
455  }
456 
457  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
467  public function get_url($fk_bank = '', $url_id = '', $type = '')
468  {
469  // phpcs:enable
470  $lines = array();
471 
472  // Check parameters
473  if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) {
474  $this->error = "ErrorBadParameter";
475  return -1;
476  }
477 
478  $sql = "SELECT fk_bank, url_id, url, label, type";
479  $sql .= " FROM ".MAIN_DB_PREFIX."bank_url";
480  if ($fk_bank > 0) {
481  $sql .= " WHERE fk_bank = ".((int) $fk_bank);
482  } else {
483  $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'";
484  }
485  $sql .= " ORDER BY type, label";
486 
487  dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
488  $result = $this->db->query($sql);
489  if ($result) {
490  $i = 0;
491  $num = $this->db->num_rows($result);
492  while ($i < $num) {
493  $obj = $this->db->fetch_object($result);
494  // Anciens liens (pour compatibilite)
495  $lines[$i][0] = $obj->url;
496  $lines[$i][1] = $obj->url_id;
497  $lines[$i][2] = $obj->label;
498  $lines[$i][3] = $obj->type;
499  // Nouveaux liens
500  $lines[$i]['url'] = $obj->url;
501  $lines[$i]['url_id'] = $obj->url_id;
502  $lines[$i]['label'] = $obj->label;
503  $lines[$i]['type'] = $obj->type;
504  $lines[$i]['fk_bank'] = $obj->fk_bank;
505  $i++;
506  }
507  } else {
508  dol_print_error($this->db);
509  }
510 
511  return $lines;
512  }
513 
532  public function addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur = '', $banque = '', $accountancycode = '', $datev = null, $num_releve = '', $amount_main_currency = null)
533  {
534  // Deprecation warning
535  if (is_numeric($oper)) {
536  dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING);
537  }
538 
539  if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
540  $this->id = $this->rowid;
541  }
542 
543  // Clean parameters
544  $emetteur = trim($emetteur);
545  $banque = trim($banque);
546  $label = trim($label);
547 
548  $now = dol_now();
549 
550  if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid
551  $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
552  $sql .= " WHERE id = ".((int) $oper);
553  $sql .= " AND entity IN (".getEntity('c_paiement').")";
554  $resql = $this->db->query($sql);
555  if ($resql) {
556  $obj = $this->db->fetch_object($resql);
557  $oper = $obj->code;
558  } else {
559  dol_print_error($this->db, 'Failed to get payment type code');
560  return -1;
561  }
562  }
563 
564  // Check parameters
565  if (!$oper) {
566  $this->error = "oper not defined";
567  return -1;
568  }
569  if (!$this->id) {
570  $this->error = "this->id not defined";
571  return -2;
572  }
573  if ($this->courant == Account::TYPE_CASH && $oper != 'LIQ') {
574  $this->error = "ErrorCashAccountAcceptsOnlyCashMoney";
575  return -3;
576  }
577 
578  $this->db->begin();
579 
580  if (is_null($datev) || empty($datev)) {
581  $datev = $date;
582  }
583 
584  $accline = new AccountLine($this->db);
585  $accline->datec = $now;
586  $accline->dateo = $date;
587  $accline->datev = $datev;
588  $accline->label = $label;
589  $accline->amount = $amount;
590  $accline->amount_main_currency = $amount_main_currency;
591  $accline->fk_user_author = $user->id;
592  $accline->fk_account = $this->id;
593  $accline->fk_type = $oper;
594  $accline->numero_compte = $accountancycode;
595  $accline->num_releve = $num_releve;
596 
597  if ($num_chq) {
598  $accline->num_chq = $num_chq;
599  }
600 
601  if ($emetteur) {
602  $accline->emetteur = $emetteur;
603  }
604 
605  if ($banque) {
606  $accline->bank_chq = $banque;
607  }
608 
609  if ($accline->insert() > 0) {
610  if ($categorie > 0) {
611  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class(";
612  $sql .= "lineid, fk_categ";
613  $sql .= ") VALUES (";
614  $sql .= ((int) $accline->id).", '".$this->db->escape($categorie)."'";
615  $sql .= ")";
616 
617  $result = $this->db->query($sql);
618  if (!$result) {
619  $this->error = $this->db->lasterror();
620  $this->db->rollback();
621 
622  return -4;
623  }
624  }
625 
626  $this->db->commit();
627 
628  return $accline->id;
629  } else {
630  $this->error = $accline->error;
631  $this->errors = $accline->errors;
632  $this->db->rollback();
633 
634  return -5;
635  }
636  }
637 
645  public function create(User $user, $notrigger = 0)
646  {
647  global $langs, $conf;
648 
649  $error = 0;
650 
651  // Clean parameters
652  if (!$this->min_allowed) {
653  $this->min_allowed = 0;
654  }
655  if (!$this->min_desired) {
656  $this->min_desired = 0;
657  }
658 
659  // Check parameters
660  if (empty($this->country_id)) {
661  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
662  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
663  return -1;
664  }
665  if (empty($this->ref)) {
666  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
667  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
668  return -1;
669  }
670  if (empty($this->date_solde)) {
671  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance"));
672  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
673  return -1;
674  }
675 
676  // Chargement librairie pour acces fonction controle RIB
677  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
678 
679  $now = dol_now();
680 
681  $this->db->begin();
682 
683  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
684  $sql .= "datec";
685  $sql .= ", ref";
686  $sql .= ", label";
687  $sql .= ", entity";
688  $sql .= ", account_number";
689  $sql .= ", fk_accountancy_journal";
690  $sql .= ", bank";
691  $sql .= ", code_banque";
692  $sql .= ", code_guichet";
693  $sql .= ", number";
694  $sql .= ", cle_rib";
695  $sql .= ", bic";
696  $sql .= ", iban_prefix";
697  $sql .= ", domiciliation";
698  $sql .= ", pti_in_ctti";
699  $sql .= ", proprio";
700  $sql .= ", owner_address";
701  $sql .= ", currency_code";
702  $sql .= ", rappro";
703  $sql .= ", min_allowed";
704  $sql .= ", min_desired";
705  $sql .= ", comment";
706  $sql .= ", state_id";
707  $sql .= ", fk_pays";
708  $sql .= ", ics";
709  $sql .= ", ics_transfer";
710  $sql .= ") VALUES (";
711  $sql .= "'".$this->db->idate($now)."'";
712  $sql .= ", '".$this->db->escape($this->ref)."'";
713  $sql .= ", '".$this->db->escape($this->label)."'";
714  $sql .= ", ".((int) $conf->entity);
715  $sql .= ", '".$this->db->escape($this->account_number)."'";
716  $sql .= ", ".($this->fk_accountancy_journal > 0 ? $this->db->escape($this->fk_accountancy_journal) : "null");
717  $sql .= ", '".$this->db->escape($this->bank)."'";
718  $sql .= ", '".$this->db->escape($this->code_banque)."'";
719  $sql .= ", '".$this->db->escape($this->code_guichet)."'";
720  $sql .= ", '".$this->db->escape($this->number)."'";
721  $sql .= ", '".$this->db->escape($this->cle_rib)."'";
722  $sql .= ", '".$this->db->escape($this->bic)."'";
723  $sql .= ", '".$this->db->escape($this->iban)."'";
724  $sql .= ", '".$this->db->escape($this->domiciliation)."'";
725  $sql .= ", ".((int) $this->pti_in_ctti);
726  $sql .= ", '".$this->db->escape($this->proprio)."'";
727  $sql .= ", '".$this->db->escape($this->owner_address)."'";
728  $sql .= ", '".$this->db->escape($this->currency_code)."'";
729  $sql .= ", ".((int) $this->rappro);
730  $sql .= ", ".price2num($this->min_allowed, 'MT');
731  $sql .= ", ".price2num($this->min_desired, 'MT');
732  $sql .= ", '".$this->db->escape($this->comment)."'";
733  $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
734  $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
735  $sql .= ", '".$this->db->escape($this->ics)."'";
736  $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
737  $sql .= ")";
738 
739  dol_syslog(get_class($this)."::create", LOG_DEBUG);
740  $resql = $this->db->query($sql);
741  if ($resql) {
742  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
743 
744  $result = $this->update($user, 1);
745  if ($result > 0) {
746  $accline = new AccountLine($this->db);
747  $accline->datec = $this->db->idate($now);
748  $accline->label = '('.$langs->trans("InitialBankBalance").')';
749  $accline->amount = price2num($this->solde);
750  $accline->fk_user_author = $user->id;
751  $accline->fk_account = $this->id;
752  $accline->datev = $this->db->idate($this->date_solde);
753  $accline->dateo = $this->db->idate($this->date_solde);
754  $accline->fk_type = 'SOLD';
755 
756  if ($accline->insert() < 0) {
757  $error++;
758  $this->error = $accline->error;
759  $this->errors = $accline->errors;
760  }
761 
762  if (!$error) {
763  $result = $this->insertExtraFields();
764  if ($result < 0) {
765  $error++;
766  }
767  }
768 
769  if (!$error && !$notrigger) {
770  // Call trigger
771  $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
772  if ($result < 0) {
773  $error++;
774  }
775  // End call triggers
776  }
777  } else {
778  $error++;
779  }
780  } else {
781  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
782  $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
783  $error++;
784  } else {
785  $this->error = $this->db->error()." sql=".$sql;
786  $error++;
787  }
788  }
789 
790  if (!$error) {
791  $this->db->commit();
792  return $this->id;
793  } else {
794  $this->db->rollback();
795  return -1 * $error;
796  }
797  }
798 
806  public function update(User $user, $notrigger = 0)
807  {
808  global $langs, $conf;
809 
810  $error = 0;
811 
812  $this->db->begin();
813 
814  // Check parameters
815  if (empty($this->country_id)) {
816  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
817  dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
818  return -1;
819  }
820  if (empty($this->ref)) {
821  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
822  dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
823  return -1;
824  }
825  if (!$this->label) {
826  $this->label = "???";
827  }
828 
829  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
830 
831  $sql .= " ref = '".$this->db->escape($this->ref)."'";
832  $sql .= ",label = '".$this->db->escape($this->label)."'";
833 
834  $sql .= ",courant = ".((int) $this->courant);
835  $sql .= ",clos = ".((int) $this->clos);
836  $sql .= ",rappro = ".((int) $this->rappro);
837  $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
838  $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
839  $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? $this->db->escape($this->fk_accountancy_journal) : "null");
840  $sql .= ",bank = '".$this->db->escape($this->bank)."'";
841  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
842  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
843  $sql .= ",number='".$this->db->escape($this->number)."'";
844  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
845  $sql .= ",bic='".$this->db->escape($this->bic)."'";
846  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
847  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
848  $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
849  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
850  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
851 
852  $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
853 
854  $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
855  $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
856  $sql .= ",comment = '".$this->db->escape($this->comment)."'";
857 
858  $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
859  $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
860  $sql .= ",ics = '".$this->db->escape($this->ics)."'";
861  $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
862 
863  $sql .= " WHERE rowid = ".((int) $this->id);
864 
865  dol_syslog(get_class($this)."::update", LOG_DEBUG);
866  $result = $this->db->query($sql);
867  if ($result) {
868  // Actions on extra fields (by external module or standard code)
869  if (!$error) {
870  $result = $this->insertExtraFields();
871  if ($result < 0) {
872  $error++;
873  }
874  }
875 
876  if (!$error && !$notrigger) {
877  // Call trigger
878  $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
879  if ($result < 0) {
880  $error++;
881  }
882  // End call triggers
883  }
884  } else {
885  $error++;
886  $this->error = $this->db->lasterror();
887  dol_print_error($this->db);
888  }
889 
890  if (!$error) {
891  $this->db->commit();
892  return $this->id;
893  } else {
894  $this->db->rollback();
895  return -1 * $error;
896  }
897  }
898 
899 
900  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
907  public function update_bban(User $user = null)
908  {
909  // phpcs:enable
910  global $conf, $langs;
911 
912  // Load library to get BAN control function
913  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
914 
915  dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
916 
917  // Check parameters
918  if (!$this->ref) {
919  $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
920  return -2;
921  }
922 
923  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
924  $sql .= " bank = '".$this->db->escape($this->bank)."'";
925  $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
926  $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
927  $sql .= ",number='".$this->db->escape($this->number)."'";
928  $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
929  $sql .= ",bic='".$this->db->escape($this->bic)."'";
930  $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
931  $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
932  $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
933  $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
934  $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null");
935  $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null");
936  $sql .= " WHERE rowid = ".((int) $this->id);
937  $sql .= " AND entity = ".((int) $conf->entity);
938 
939  dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
940 
941  $result = $this->db->query($sql);
942  if ($result) {
943  return 1;
944  } else {
945  $this->error = $this->db->lasterror();
946  dol_print_error($this->db);
947  return -1;
948  }
949  }
950 
951 
959  public function fetch($id, $ref = '')
960  {
961  global $conf;
962 
963  if (empty($id) && empty($ref)) {
964  $this->error = "ErrorBadParameters";
965  return -1;
966  }
967 
968  $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,";
969  $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
970  $sql .= " ba.domiciliation, ba.pti_in_ctti, ba.proprio, ba.owner_address, ba.state_id, ba.fk_pays as country_id,";
971  $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
972  $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
973  $sql .= " ba.datec as date_creation, ba.tms as date_update, ba.ics, ba.ics_transfer,";
974  $sql .= ' c.code as country_code, c.label as country,';
975  $sql .= ' d.code_departement as state_code, d.nom as state';
976  $sql .= ' , aj.code as accountancy_journal';
977  $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
978  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
979  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
980  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal';
981  $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
982  if ($id) {
983  $sql .= " AND ba.rowid = ".((int) $id);
984  }
985  if ($ref) {
986  $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
987  }
988 
989  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
990  $result = $this->db->query($sql);
991  if ($result) {
992  if ($this->db->num_rows($result)) {
993  $obj = $this->db->fetch_object($result);
994 
995  $this->id = $obj->rowid;
996  $this->rowid = $obj->rowid;
997  $this->ref = $obj->ref;
998  $this->label = $obj->label;
999  $this->type = $obj->courant;
1000  $this->courant = $obj->courant;
1001  $this->bank = $obj->bank;
1002  $this->clos = $obj->clos;
1003  $this->rappro = $obj->rappro;
1004  $this->url = $obj->url;
1005 
1006  $this->code_banque = $obj->code_banque;
1007  $this->code_guichet = $obj->code_guichet;
1008  $this->number = $obj->number;
1009  $this->cle_rib = $obj->cle_rib;
1010  $this->bic = $obj->bic;
1011  $this->iban = $obj->iban;
1012  $this->domiciliation = $obj->domiciliation;
1013  $this->pti_in_ctti = $obj->pti_in_ctti;
1014  $this->proprio = $obj->proprio;
1015  $this->owner_address = $obj->owner_address;
1016 
1017  $this->state_id = $obj->state_id;
1018  $this->state_code = $obj->state_code;
1019  $this->state = $obj->state;
1020 
1021  $this->country_id = $obj->country_id;
1022  $this->country_code = $obj->country_code;
1023  $this->country = $obj->country;
1024 
1025  $this->account_number = $obj->account_number;
1026  $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
1027  $this->accountancy_journal = $obj->accountancy_journal;
1028 
1029  $this->currency_code = $obj->currency_code;
1030  $this->account_currency_code = $obj->currency_code;
1031  $this->min_allowed = $obj->min_allowed;
1032  $this->min_desired = $obj->min_desired;
1033  $this->comment = $obj->comment;
1034 
1035  $this->date_creation = $this->db->jdate($obj->date_creation);
1036  $this->date_update = $this->db->jdate($obj->date_update);
1037 
1038  $this->ics = $obj->ics;
1039  $this->ics_transfer = $obj->ics_transfer;
1040 
1041  // Retrieve all extrafield
1042  // fetch optionals attributes and labels
1043  $this->fetch_optionals();
1044 
1045  return 1;
1046  } else {
1047  return 0;
1048  }
1049  } else {
1050  $this->error = $this->db->lasterror;
1051  $this->errors[] = $this->error;
1052  return -1;
1053  }
1054  }
1055 
1066  public function setCategories($categories)
1067  {
1068  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1069  return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
1070  }
1071 
1078  public function delete(User $user = null)
1079  {
1080  $error = 0;
1081 
1082  $this->db->begin();
1083 
1084  // Delete link between tag and bank account
1085  if (!$error) {
1086  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1087  $sql .= " WHERE fk_account = ".((int) $this->id);
1088 
1089  $resql = $this->db->query($sql);
1090  if (!$resql) {
1091  $error++;
1092  $this->error = "Error ".$this->db->lasterror();
1093  }
1094  }
1095 
1096  if (!$error) {
1097  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
1098  $sql .= " WHERE rowid = ".((int) $this->id);
1099 
1100  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1101  $result = $this->db->query($sql);
1102  if ($result) {
1103  // Remove extrafields
1104  if (!$error) {
1105  $result = $this->deleteExtraFields();
1106  if ($result < 0) {
1107  $error++;
1108  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1109  }
1110  }
1111  } else {
1112  $error++;
1113  $this->error = "Error ".$this->db->lasterror();
1114  }
1115  }
1116 
1117  if (!$error) {
1118  $this->db->commit();
1119  return 1;
1120  } else {
1121  $this->db->rollback();
1122  return -1;
1123  }
1124  }
1125 
1126 
1133  public function getLibStatut($mode = 0)
1134  {
1135  return $this->LibStatut($this->clos, $mode);
1136  }
1137 
1138  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1146  public function LibStatut($status, $mode = 0)
1147  {
1148  // phpcs:enable
1149  global $langs;
1150  $langs->load('banks');
1151 
1152  if ($status == self::STATUS_OPEN) {
1153  $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1154  $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1155  $statusType = 'status4';
1156  } else {
1157  $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1158  $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1159  $statusType = 'status5';
1160  }
1161 
1162  return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1163  }
1164 
1165 
1166  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1172  public function can_be_deleted()
1173  {
1174  // phpcs:enable
1175  $can_be_deleted = false;
1176 
1177  $sql = "SELECT COUNT(rowid) as nb";
1178  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1179  $sql .= " WHERE fk_account = ".((int) $this->id);
1180 
1181  $resql = $this->db->query($sql);
1182  if ($resql) {
1183  $obj = $this->db->fetch_object($resql);
1184  if ($obj->nb <= 1) {
1185  $can_be_deleted = true; // Juste le solde
1186  }
1187  } else {
1188  dol_print_error($this->db);
1189  }
1190  return $can_be_deleted;
1191  }
1192 
1193 
1199  public function error()
1200  {
1201  return $this->error;
1202  }
1203 
1212  public function solde($option = 0, $date_end = '', $field = 'dateo')
1213  {
1214  $solde = 0;
1215 
1216  $sql = "SELECT sum(amount) as amount";
1217  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1218  $sql .= " WHERE fk_account = ".((int) $this->id);
1219  if ($option == 1) {
1220  $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1221  }
1222 
1223  $resql = $this->db->query($sql);
1224  if ($resql) {
1225  if ($this->db->num_rows($resql)) {
1226  $obj = $this->db->fetch_object($resql);
1227  $solde = $obj->amount;
1228  }
1229  $this->db->free($resql);
1230  } else {
1231  $this->errors[] = $this->db->lasterror;
1232  return -1;
1233  }
1234 
1235  return price2num($solde, 'MU');
1236  }
1237 
1238  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1246  public function load_board(User $user, $filteraccountid = 0)
1247  {
1248  // phpcs:enable
1249  global $conf, $langs;
1250 
1251  if ($user->socid) {
1252  return -1; // protection pour eviter appel par utilisateur externe
1253  }
1254 
1255  $sql = "SELECT b.rowid, b.datev as datefin";
1256  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1257  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1258  $sql .= " WHERE b.rappro=0";
1259  $sql .= " AND b.fk_account = ba.rowid";
1260  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1261  $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1262  $sql .= " AND clos = 0";
1263  if ($filteraccountid) {
1264  $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1265  }
1266 
1267  $resql = $this->db->query($sql);
1268  if ($resql) {
1269  $langs->load("banks");
1270  $now = dol_now();
1271 
1272  require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1273 
1274  $response = new WorkboardResponse();
1275  $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1276  $response->label = $langs->trans("TransactionsToConciliate");
1277  $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1278  $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1279  $response->img = img_object('', "payment");
1280 
1281  while ($obj = $this->db->fetch_object($resql)) {
1282  $response->nbtodo++;
1283  if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1284  $response->nbtodolate++;
1285  }
1286  }
1287 
1288  return $response;
1289  } else {
1290  dol_print_error($this->db);
1291  $this->error = $this->db->error();
1292  return -1;
1293  }
1294  }
1295 
1296  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1302  public function load_state_board($filteraccountid = 0)
1303  {
1304  // phpcs:enable
1305  global $user;
1306 
1307  if ($user->socid) {
1308  return -1; // protection pour eviter appel par utilisateur externe
1309  }
1310 
1311  $sql = "SELECT count(b.rowid) as nb";
1312  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1313  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1314  $sql .= " WHERE b.fk_account = ba.rowid";
1315  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1316  $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1317  $sql .= " AND clos = 0";
1318  if ($filteraccountid) {
1319  $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1320  }
1321 
1322  $resql = $this->db->query($sql);
1323  if ($resql) {
1324  while ($obj = $this->db->fetch_object($resql)) {
1325  $this->nb["banklines"] = $obj->nb;
1326  }
1327  $this->db->free($resql);
1328  } else {
1329  dol_print_error($this->db);
1330  $this->error = $this->db->error();
1331  return -1;
1332  }
1333  }
1334 
1335 
1341  public function countAccountToReconcile()
1342  {
1343  global $db, $conf, $user;
1344 
1345  //Protection against external users
1346  if ($user->socid) {
1347  return 0;
1348  }
1349 
1350  $nb = 0;
1351 
1352  $sql = "SELECT COUNT(ba.rowid) as nb";
1353  $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1354  $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1355  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1356  if (empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
1357  $sql .= " AND ba.courant != 2";
1358  }
1359  $resql = $this->db->query($sql);
1360  if ($resql) {
1361  $obj = $this->db->fetch_object($resql);
1362  $nb = $obj->nb;
1363  } else {
1364  dol_print_error($this->db);
1365  }
1366 
1367  return $nb;
1368  }
1369 
1380  public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0)
1381  {
1382  global $conf, $langs, $user;
1383  include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1384 
1385  $result = '';
1386  $label = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
1387  if (isset($this->status)) {
1388  $label .= ' '.$this->getLibStatut(5);
1389  }
1390  $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1391  $label .= '<br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
1392  $label .= '<br><b>'.$langs->trans('IBAN').':</b> '.getIbanHumanReadable($this);
1393  $label .= '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
1394  $label .= '<br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
1395 
1396  if (empty($user->rights->banque->lire) || !empty($user->socid)) {
1397  $option = 'nolink';
1398  }
1399 
1400  if (isModEnabled('accounting')) {
1401  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1402  $langs->load("accountancy");
1403  $label .= '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
1404  $label .= '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
1405  }
1406 
1407  $linkclose = '" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1408 
1409  $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1410  if ($mode == 'transactions') {
1411  $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1412  } elseif ($mode == 'receipts') {
1413  $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1414  }
1415 
1416  if ($option != 'nolink') {
1417  // Add param to save lastsearch_values or not
1418  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1419  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1420  $add_save_lastsearch_values = 1;
1421  }
1422  if ($add_save_lastsearch_values) {
1423  $url .= '&save_lastsearch_values=1';
1424  }
1425  }
1426 
1427  $linkstart = '<a href="'.$url.$linkclose;
1428  $linkend = '</a>';
1429 
1430  if ($option == 'nolink') {
1431  $linkstart = '';
1432  $linkend = '';
1433  }
1434 
1435  $result .= $linkstart;
1436  if ($withpicto) {
1437  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1438  }
1439  if ($withpicto != 2) {
1440  $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1441  }
1442  $result .= $linkend;
1443 
1444  return $result;
1445  }
1446 
1447 
1448  // Method after here are common to Account and CompanyBankAccount
1449 
1450 
1456  public function verif()
1457  {
1458  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1459 
1460  $this->error_number = 0;
1461 
1462  // Call function to check BAN
1463 
1464  if (!checkIbanForAccount($this)) {
1465  $this->error_number = 12;
1466  $this->error_message = 'IBANNotValid';
1467  }
1468  if (!checkSwiftForAccount($this)) {
1469  $this->error_number = 12;
1470  $this->error_message = 'SwiftNotValid';
1471  }
1472  /*if (! checkBanForAccount($this))
1473  {
1474  $this->error_number = 12;
1475  $this->error_message = 'BANControlError';
1476  }*/
1477 
1478  if ($this->error_number == 0) {
1479  return 1;
1480  } else {
1481  return 0;
1482  }
1483  }
1484 
1490  public function getCountryCode()
1491  {
1492  global $mysoc;
1493 
1494  // We return country code of bank account
1495  if (!empty($this->country_code)) {
1496  return $this->country_code;
1497  }
1498 
1499  // For backward compatibility, we try to guess country from other information
1500  if (!empty($this->iban)) {
1501  // If IBAN defined, we can know country of account from it
1502  $reg = array();
1503  if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
1504  return $reg[1];
1505  }
1506  }
1507 
1508  // If this class is linked to a third party
1509  if (!empty($this->socid)) {
1510  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
1511  $company = new Societe($this->db);
1512  $result = $company->fetch($this->socid);
1513  if (!empty($company->country_code)) {
1514  return $company->country_code;
1515  }
1516  }
1517 
1518  // We return country code of managed company
1519  if (!empty($mysoc->country_code)) {
1520  return $mysoc->country_code;
1521  }
1522 
1523  return '';
1524  }
1525 
1534  public function useDetailedBBAN()
1535  {
1536  $country_code = $this->getCountryCode();
1537 
1538  if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
1539  return 1; // France, Spain, Gabon, ... - Not valid for CH
1540  }
1541  if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
1542  return 2; // Australia, England...
1543  }
1544  return 0;
1545  }
1546 
1552  public function needIBAN()
1553  {
1554  global $conf;
1555 
1556  if (!empty($conf->global->MAIN_IBAN_IS_NEVER_MANDATORY)) {
1557  return 0;
1558  }
1559 
1560  $country_code = $this->getCountryCode();
1561 
1562  $country_code_in_EEC = array(
1563  'AT', // Austria
1564  'BE', // Belgium
1565  'BG', // Bulgaria
1566  'CY', // Cyprus
1567  'CZ', // Czech republic
1568  'DE', // Germany
1569  'DK', // Danemark
1570  'EE', // Estonia
1571  'ES', // Spain
1572  'FI', // Finland
1573  'FR', // France
1574  'GB', // United Kingdom
1575  'GR', // Greece
1576  'HR', // Croatia
1577  'NL', // Holland
1578  'HU', // Hungary
1579  'IE', // Ireland
1580  'IM', // Isle of Man - Included in UK
1581  'IT', // Italy
1582  'LT', // Lithuania
1583  'LU', // Luxembourg
1584  'LV', // Latvia
1585  'MC', // Monaco - Included in France
1586  'MT', // Malta
1587  //'NO', // Norway
1588  'PL', // Poland
1589  'PT', // Portugal
1590  'RO', // Romania
1591  'SE', // Sweden
1592  'SK', // Slovakia
1593  'SI', // Slovenia
1594  'UK', // United Kingdom
1595  //'CH', // Switzerland - No. Swizerland in not in EEC
1596  );
1597 
1598  if (in_array($country_code, $country_code_in_EEC)) {
1599  return 1; // France, Spain, ...
1600  }
1601  return 0;
1602  }
1603 
1610  public function info($id)
1611  {
1612  }
1613 
1628  public function getFieldsToShow($includeibanbic = 0)
1629  {
1630  //Get the required properties depending on the country
1631  $detailedBBAN = $this->useDetailedBBAN();
1632 
1633  if ($detailedBBAN == 0) {
1634  $fieldarray = array(
1635  'BankAccountNumber'
1636  );
1637  } elseif ($detailedBBAN == 2) {
1638  $fieldarray = array(
1639  'BankCode',
1640  'BankAccountNumber'
1641  );
1642  } else {
1643  $fieldarray = self::getAccountNumberOrder();
1644  }
1645 
1646  //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1647  if ($includeibanbic) {
1648  $fieldarray[] = 'IBAN';
1649  $fieldarray[] = 'BIC';
1650  }
1651  //}
1652 
1653  //Get the order the properties are shown
1654  return $fieldarray;
1655  }
1656 
1667  public static function getAccountNumberOrder()
1668  {
1669  global $conf;
1670 
1671  $fieldlists = array(
1672  'BankCode',
1673  'DeskCode',
1674  'BankAccountNumber',
1675  'BankAccountNumberKey'
1676  );
1677 
1678  if (!empty($conf->global->BANK_SHOW_ORDER_OPTION)) {
1679  if (is_numeric($conf->global->BANK_SHOW_ORDER_OPTION)) {
1680  if ($conf->global->BANK_SHOW_ORDER_OPTION == '1') {
1681  $fieldlists = array(
1682  'BankCode',
1683  'DeskCode',
1684  'BankAccountNumberKey',
1685  'BankAccountNumber'
1686  );
1687  }
1688  } else {
1689  //Replace the old AccountNumber key with the new BankAccountNumber key
1690  $fieldlists = explode(
1691  ' ',
1692  preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
1693  );
1694  }
1695  }
1696 
1697  return $fieldlists;
1698  }
1699 
1700 
1708  public function initAsSpecimen()
1709  {
1710  // Example of IBAN FR7630001007941234567890185
1711  $this->specimen = 1;
1712  $this->ref = 'MBA';
1713  $this->label = 'My Big Company Bank account';
1714  $this->bank = 'MyBank';
1715  $this->courant = Account::TYPE_CURRENT;
1716  $this->clos = Account::STATUS_OPEN;
1717  $this->code_banque = '30001';
1718  $this->code_guichet = '00794';
1719  $this->number = '12345678901';
1720  $this->cle_rib = '85';
1721  $this->bic = 'AA12';
1722  $this->iban = 'FR7630001007941234567890185';
1723  $this->domiciliation = 'Banque de France';
1724  $this->proprio = 'Owner';
1725  $this->owner_address = 'Owner address';
1726  $this->country_id = 1;
1727  }
1728 
1737  public static function replaceThirdparty($dbs, $origin_id, $dest_id)
1738  {
1739  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'";
1740 
1741  if ($dbs->query($sql)) {
1742  return true;
1743  } else {
1744  //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.
1745  //$this->errors = $dbs->lasterror();
1746  return false;
1747  }
1748  }
1749 }
1750 
1751 
1756 {
1760  public $error = '';
1761 
1765  public $db;
1766 
1770  public $element = 'bank';
1771 
1775  public $table_element = 'bank';
1776 
1780  public $picto = 'accountline';
1781 
1785  public $id;
1786 
1790  public $ref;
1791 
1797  public $datec;
1798 
1804  public $dateo;
1805 
1811  public $datev;
1812 
1813  public $amount; /* Amount of payment in the bank account currency */
1814  public $amount_main_currency; /* Amount in the currency of company if bank account use another currency */
1815 
1819  public $fk_user_author;
1820 
1824  public $fk_user_rappro;
1825 
1829  public $fk_type;
1830 
1834  public $fk_bordereau;
1835 
1839  public $fk_account;
1840 
1844  public $bank_account_ref;
1845 
1849  public $bank_account_label;
1850 
1854  public $numero_compte;
1855 
1859  public $emetteur;
1860 
1861  public $rappro; // Is it conciliated
1862  public $num_releve; // If conciliated, what is bank statement
1863  public $num_chq; // Num of cheque
1864  public $bank_chq; // Bank of cheque
1865 
1869  public $label;
1870 
1871  public $note;
1872 
1873 
1874 
1880  public function __construct(DoliDB $db)
1881  {
1882  $this->db = $db;
1883  }
1884 
1893  public function fetch($rowid, $ref = '', $num = '')
1894  {
1895  global $conf;
1896 
1897  // Check parameters
1898  if (empty($rowid) && empty($ref) && empty($num)) {
1899  return -1;
1900  }
1901 
1902  $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
1903  $sql .= " b.fk_user_author, b.fk_user_rappro,";
1904  $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
1905  $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
1906  //$sql.= " b.author"; // Is this used ?
1907  $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
1908  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1909  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1910  $sql .= " WHERE b.fk_account = ba.rowid";
1911  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1912  if ($num) {
1913  $sql .= " AND b.num_chq='".$this->db->escape($num)."'";
1914  } elseif ($ref) {
1915  $sql .= " AND b.rowid='".$this->db->escape($ref)."'";
1916  } else {
1917  $sql .= " AND b.rowid = ".((int) $rowid);
1918  }
1919 
1920  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1921  $result = $this->db->query($sql);
1922  if ($result) {
1923  $ret = 0;
1924 
1925  $obj = $this->db->fetch_object($result);
1926  if ($obj) {
1927  $this->id = $obj->rowid;
1928  $this->rowid = $obj->rowid;
1929  $this->ref = $obj->rowid;
1930 
1931  $this->datec = $obj->datec;
1932  $this->datev = $obj->datev;
1933  $this->dateo = $obj->dateo;
1934  $this->amount = $obj->amount;
1935  $this->label = $obj->label;
1936  $this->note = $obj->note;
1937 
1938  $this->fk_user_author = $obj->fk_user_author;
1939  $this->fk_user_rappro = $obj->fk_user_rappro;
1940 
1941  $this->fk_type = $obj->fk_type; // Type of transaction
1942  $this->rappro = $obj->rappro;
1943  $this->num_releve = $obj->num_releve;
1944 
1945  $this->num_chq = $obj->num_chq;
1946  $this->bank_chq = $obj->banque;
1947  $this->fk_bordereau = $obj->fk_bordereau;
1948 
1949  $this->fk_account = $obj->fk_account;
1950  $this->bank_account_ref = $obj->bank_account_ref;
1951  $this->bank_account_label = $obj->bank_account_label;
1952 
1953  // Retrieve all extrafield
1954  // fetch optionals attributes and labels
1955  $this->fetch_optionals();
1956 
1957  $ret = 1;
1958  }
1959  $this->db->free($result);
1960  return $ret;
1961  } else {
1962  return -1;
1963  }
1964  }
1965 
1971  public function insert()
1972  {
1973  $error = 0;
1974 
1975  $this->db->begin();
1976 
1977  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
1978  $sql .= "datec";
1979  $sql .= ", dateo";
1980  $sql .= ", datev";
1981  $sql .= ", label";
1982  $sql .= ", amount";
1983  $sql .= ", amount_main_currency";
1984  $sql .= ", fk_user_author";
1985  $sql .= ", num_chq";
1986  $sql .= ", fk_account";
1987  $sql .= ", fk_type";
1988  $sql .= ", emetteur,banque";
1989  $sql .= ", rappro";
1990  $sql .= ", numero_compte";
1991  $sql .= ", num_releve";
1992  $sql .= ") VALUES (";
1993  $sql .= "'".$this->db->idate($this->datec)."'";
1994  $sql .= ", '".$this->db->idate($this->dateo)."'";
1995  $sql .= ", '".$this->db->idate($this->datev)."'";
1996  $sql .= ", '".$this->db->escape($this->label)."'";
1997  $sql .= ", ".price2num($this->amount);
1998  $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
1999  $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
2000  $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
2001  $sql .= ", '".$this->db->escape($this->fk_account)."'";
2002  $sql .= ", '".$this->db->escape($this->fk_type)."'";
2003  $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
2004  $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
2005  $sql .= ", ".(int) $this->rappro;
2006  $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2007  $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2008  $sql .= ")";
2009 
2010  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2011  $resql = $this->db->query($sql);
2012  if ($resql) {
2013  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2014  // Actions on extra fields (by external module or standard code)
2015  $result = $this->insertExtraFields();
2016  if ($result < 0) {
2017  $error++;
2018  }
2019  } else {
2020  $error++;
2021  $this->error = $this->db->lasterror();
2022  dol_print_error($this->db);
2023  }
2024 
2025  if (!$error) {
2026  $this->db->commit();
2027  return $this->id;
2028  } else {
2029  $this->db->rollback();
2030  return -1 * $error;
2031  }
2032  }
2033 
2040  public function delete(User $user = null)
2041  {
2042  global $conf;
2043 
2044  $nbko = 0;
2045 
2046  if ($this->rappro) {
2047  // Protection to avoid any delete of consolidated lines
2048  $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2049  return -1;
2050  }
2051 
2052  $this->db->begin();
2053 
2054  // Protection to avoid any delete of accounted lines. Protection on by default
2055  if (empty($conf->global->BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING)) {
2056  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2057  $resql = $this->db->query($sql);
2058  if ($resql) {
2059  $obj = $this->db->fetch_object($resql);
2060  if ($obj && $obj->nb) {
2061  $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2062  $this->db->rollback();
2063  return -1;
2064  }
2065  } else {
2066  $this->error = $this->db->lasterror();
2067  $this->db->rollback();
2068  return -1;
2069  }
2070  }
2071 
2072  // Delete urls
2073  $result = $this->delete_urls($user);
2074  if ($result < 0) {
2075  $nbko++;
2076  }
2077 
2078  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".(int) $this->rowid;
2079  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2080  $result = $this->db->query($sql);
2081  if (!$result) {
2082  $nbko++;
2083  }
2084 
2085  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_extrafields WHERE fk_object=".(int) $this->rowid;
2086  $result = $this->db->query($sql);
2087  if (!$result) {
2088  $nbko++;
2089  }
2090 
2091  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2092  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2093  $result = $this->db->query($sql);
2094  if (!$result) {
2095  $nbko++;
2096  }
2097 
2098  if (!$nbko) {
2099  $this->db->commit();
2100  return 1;
2101  } else {
2102  $this->db->rollback();
2103  return -$nbko;
2104  }
2105  }
2106 
2107 
2108  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2115  public function delete_urls(User $user = null)
2116  {
2117  // phpcs:enable
2118  $nbko = 0;
2119 
2120  if ($this->rappro) {
2121  // Protection to avoid any delete of consolidated lines
2122  $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2123  return -1;
2124  }
2125 
2126  $this->db->begin();
2127 
2128  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2129  dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
2130  $result = $this->db->query($sql);
2131  if (!$result) {
2132  $nbko++;
2133  }
2134 
2135  if (!$nbko) {
2136  $this->db->commit();
2137  return 1;
2138  } else {
2139  $this->db->rollback();
2140  return -$nbko;
2141  }
2142  }
2143 
2144 
2152  public function update(User $user, $notrigger = 0)
2153  {
2154  $this->db->begin();
2155 
2156  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2157  $sql .= " amount = ".price2num($this->amount).",";
2158  $sql .= " datev='".$this->db->idate($this->datev)."',";
2159  $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2160  $sql .= " WHERE rowid = ".((int) $this->rowid);
2161 
2162  dol_syslog(get_class($this)."::update", LOG_DEBUG);
2163  $resql = $this->db->query($sql);
2164  if ($resql) {
2165  $this->db->commit();
2166  return 1;
2167  } else {
2168  $this->db->rollback();
2169  $this->error = $this->db->error();
2170  return -1;
2171  }
2172  }
2173 
2174 
2175  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2184  public function update_conciliation(User $user, $cat, $conciliated = 1)
2185  {
2186  // phpcs:enable
2187  global $conf, $langs;
2188 
2189  $this->db->begin();
2190 
2191  // Check statement field
2192  if (!empty($conf->global->BANK_STATEMENT_REGEX_RULE)) {
2193  if (!preg_match('/'.$conf->global->BANK_STATEMENT_REGEX_RULE.'/', $this->num_releve)) {
2194  $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", $conf->global->BANK_STATEMENT_REGEX_RULE);
2195  return -1;
2196  }
2197  }
2198 
2199  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2200  $sql .= " rappro = ".((int) $conciliated);
2201  $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2202  if ($conciliated) {
2203  $sql .= ", fk_user_rappro = ".$user->id;
2204  }
2205  $sql .= " WHERE rowid = ".((int) $this->id);
2206 
2207  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2208  $resql = $this->db->query($sql);
2209  if ($resql) {
2210  if (!empty($cat)) {
2211  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
2212  $sql .= "lineid";
2213  $sql .= ", fk_categ";
2214  $sql .= ") VALUES (";
2215  $sql .= $this->id;
2216  $sql .= ", ".((int) $cat);
2217  $sql .= ")";
2218 
2219  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2220  $this->db->query($sql);
2221 
2222  // No error check. Can fail if category already affected
2223  }
2224 
2225  $this->rappro = 1;
2226 
2227  $this->db->commit();
2228  return 1;
2229  } else {
2230  $this->db->rollback();
2231  return -1;
2232  }
2233  }
2234 
2235 
2236  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2244  public function datev_change($rowid, $sign = 1)
2245  {
2246  // phpcs:enable
2247  $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2248  $resql = $this->db->query($sql);
2249  if ($resql) {
2250  $obj = $this->db->fetch_object($resql);
2251  $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2252 
2253  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2254  $sql .= " datev = '".$this->db->idate($newdate)."'";
2255  $sql .= " WHERE rowid = ".((int) $rowid);
2256 
2257  $result = $this->db->query($sql);
2258  if ($result) {
2259  if ($this->db->affected_rows($result)) {
2260  return 1;
2261  }
2262  } else {
2263  dol_print_error($this->db);
2264  return 0;
2265  }
2266  } else {
2267  dol_print_error($this->db);
2268  }
2269  return 0;
2270  }
2271 
2272  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2279  public function datev_next($id)
2280  {
2281  // phpcs:enable
2282  return $this->datev_change($id, 1);
2283  }
2284 
2285  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2292  public function datev_previous($id)
2293  {
2294  // phpcs:enable
2295  return $this->datev_change($id, -1);
2296  }
2297 
2298 
2299  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2307  public function dateo_change($rowid, $sign = 1)
2308  {
2309  // phpcs:enable
2310  $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2311  $resql = $this->db->query($sql);
2312  if ($resql) {
2313  $obj = $this->db->fetch_object($resql);
2314  $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2315 
2316  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2317  $sql .= " dateo = '".$this->db->idate($newdate)."'";
2318  $sql .= " WHERE rowid = ".((int) $rowid);
2319 
2320  $result = $this->db->query($sql);
2321  if ($result) {
2322  if ($this->db->affected_rows($result)) {
2323  return 1;
2324  }
2325  } else {
2326  dol_print_error($this->db);
2327  return 0;
2328  }
2329  } else {
2330  dol_print_error($this->db);
2331  }
2332  return 0;
2333  }
2334 
2335  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2342  public function dateo_next($id)
2343  {
2344  // phpcs:enable
2345  return $this->dateo_change($id, 1);
2346  }
2347 
2348  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2355  public function dateo_previous($id)
2356  {
2357  // phpcs:enable
2358  return $this->dateo_change($id, -1);
2359  }
2360 
2361 
2368  public function info($id)
2369  {
2370  $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2371  $sql .= ' b.fk_user_author, b.fk_user_rappro';
2372  $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2373  $sql .= ' WHERE b.rowid = '.((int) $id);
2374 
2375  $result = $this->db->query($sql);
2376  if ($result) {
2377  if ($this->db->num_rows($result)) {
2378  $obj = $this->db->fetch_object($result);
2379  $this->id = $obj->rowid;
2380 
2381  if ($obj->fk_user_author) {
2382  $cuser = new User($this->db);
2383  $cuser->fetch($obj->fk_user_author);
2384  $this->user_creation = $cuser;
2385  }
2386  if ($obj->fk_user_rappro) {
2387  $ruser = new User($this->db);
2388  $ruser->fetch($obj->fk_user_rappro);
2389  $this->user_rappro = $ruser;
2390  }
2391 
2392  $this->date_creation = $this->db->jdate($obj->datec);
2393  $this->date_modification = $this->db->jdate($obj->datem);
2394  //$this->date_rappro = $obj->daterappro; // Not yet managed
2395  }
2396  $this->db->free($result);
2397  } else {
2398  dol_print_error($this->db);
2399  }
2400  }
2401 
2402 
2412  public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2413  {
2414  global $langs;
2415 
2416  $result = '';
2417 
2418  $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2419  $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2420 
2421  $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">';
2422  $linkend = '</a>';
2423 
2424  $result .= $linkstart;
2425  if ($withpicto) {
2426  $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);
2427  }
2428  if ($withpicto != 2) {
2429  $result .= ($this->ref ? $this->ref : $this->id);
2430  }
2431  $result .= $linkend;
2432 
2433  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2434  $result .= ' <span class="opacitymedium">(';
2435  }
2436  if ($option == 'showall') {
2437  $result .= $langs->trans("BankAccount").': ';
2438  $accountstatic = new Account($this->db);
2439  $accountstatic->id = $this->fk_account;
2440  $accountstatic->ref = $this->bank_account_ref;
2441  $accountstatic->label = $this->bank_account_label;
2442  $result .= $accountstatic->getNomUrl(0).', ';
2443  }
2444  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2445  $result .= $langs->trans("BankLineConciliated").': ';
2446  $result .= yn($this->rappro);
2447  }
2448  if ($option == 'showall' || $option == 'showconciliatedandaccounted') {
2449  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2450  $resql = $this->db->query($sql);
2451  if ($resql) {
2452  $obj = $this->db->fetch_object($resql);
2453  if ($obj && $obj->nb) {
2454  $result .= ' - '.$langs->trans("Accounted").': '.yn(1);
2455  } else {
2456  $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2457  }
2458  }
2459  }
2460  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2461  $result .= ')</span>';
2462  }
2463 
2464  return $result;
2465  }
2466 
2467 
2474  public function getLibStatut($mode = 0)
2475  {
2476  return $this->LibStatut($this->status, $mode);
2477  }
2478 
2479  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2487  public function LibStatut($status, $mode = 0)
2488  {
2489  // phpcs:enable
2490  global $langs;
2491  //$langs->load('companies');
2492  /*
2493  if ($mode == 0)
2494  {
2495  if ($status==0) return $langs->trans("ActivityCeased");
2496  if ($status==1) return $langs->trans("InActivity");
2497  }
2498  if ($mode == 1)
2499  {
2500  if ($status==0) return $langs->trans("ActivityCeased");
2501  if ($status==1) return $langs->trans("InActivity");
2502  }
2503  if ($mode == 2)
2504  {
2505  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2506  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2507  }
2508  if ($mode == 3)
2509  {
2510  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2511  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2512  }
2513  if ($mode == 4)
2514  {
2515  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2516  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2517  }
2518  if ($mode == 5)
2519  {
2520  if ($status==0) return $langs->trans("ActivityCeased").' '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2521  if ($status==1) return $langs->trans("InActivity").' '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2522  }*/
2523  }
2524 
2525 
2531  public function getVentilExportCompta()
2532  {
2533  $alreadydispatched = 0;
2534 
2535  $type = 'bank';
2536 
2537  $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);
2538  $resql = $this->db->query($sql);
2539  if ($resql) {
2540  $obj = $this->db->fetch_object($resql);
2541  if ($obj) {
2542  $alreadydispatched = $obj->nb;
2543  }
2544  } else {
2545  $this->error = $this->db->lasterror();
2546  return -1;
2547  }
2548 
2549  if ($alreadydispatched) {
2550  return 1;
2551  }
2552  return 0;
2553  }
2554 }
Account\getCountryCode
getCountryCode()
Return account country code.
Definition: account.class.php:1490
Account\TYPE_CURRENT
const TYPE_CURRENT
Current account.
Definition: account.class.php:329
Account\load_state_board
load_state_board($filteraccountid=0)
Charge indicateurs this->nb de tableau de bord.
Definition: account.class.php:1302
AccountLine\fetch
fetch($rowid, $ref='', $num='')
Load into memory content of a bank transaction line.
Definition: account.class.php:1893
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:49
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:6675
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:1493
Account\verif
verif()
Return if an account has valid information for Direct debit payment.
Definition: account.class.php:1456
AccountLine\getLibStatut
getLibStatut($mode=0)
Return label of status (activity, closed)
Definition: account.class.php:2474
Account\update
update(User $user, $notrigger=0)
Update bank account card.
Definition: account.class.php:806
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:1341
AccountLine\update_conciliation
update_conciliation(User $user, $cat, $conciliated=1)
Update conciliation field.
Definition: account.class.php:2184
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:4993
ref
$object ref
Definition: info.php:78
checkIbanForAccount
checkIbanForAccount(Account $account)
Check IBAN number informations for a bank account.
Definition: bank.lib.php:295
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:467
checkSwiftForAccount
checkSwiftForAccount($account)
Check SWIFT informations for a bank account.
Definition: bank.lib.php:279
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:1610
Account\setCategories
setCategories($categories)
Sets object to supplied categories.
Definition: account.class.php:1066
Account\load_board
load_board(User $user, $filteraccountid=0)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
Definition: account.class.php:1246
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:2307
Account\can_be_deleted
can_be_deleted()
Renvoi si un compte peut etre supprimer ou non (sans mouvements)
Definition: account.class.php:1172
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5823
AccountLine\datev_change
datev_change($rowid, $sign=1)
Increase/decrease value date of a rowid.
Definition: account.class.php:2244
WorkboardResponse
Definition: workboardresponse.class.php:24
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:4024
Account\TYPE_CASH
const TYPE_CASH
Cash account.
Definition: account.class.php:333
AccountLine\dateo_next
dateo_next($id)
Increase operation date of a rowid.
Definition: account.class.php:2342
Account\LibStatut
LibStatut($status, $mode=0)
Return label of given object status.
Definition: account.class.php:1146
Account\canBeConciliated
canBeConciliated()
Return if a bank account need to be conciliated.
Definition: account.class.php:402
Account\getNomUrl
getNomUrl($withpicto=0, $mode='', $option='', $save_lastsearch_value=-1, $notooltip=0)
Return clicable name (with picto eventually)
Definition: account.class.php:1380
AccountLine\datev_previous
datev_previous($id)
Decrease value date of a rowid.
Definition: account.class.php:2292
AccountLine\__construct
__construct(DoliDB $db)
Constructor.
Definition: account.class.php:1880
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:1628
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:532
Account\__construct
__construct(DoliDB $db)
Constructor.
Definition: account.class.php:349
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6165
Account\getAccountNumberOrder
static getAccountNumberOrder()
Returns the components of the bank account in order.
Definition: account.class.php:1667
$resql
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
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:1199
Account\TYPE_SAVINGS
const TYPE_SAVINGS
Savings account.
Definition: account.class.php:337
AccountLine\getNomUrl
getNomUrl($withpicto=0, $maxlen=0, $option='', $notooltip=0)
Return clickable name (with picto eventually)
Definition: account.class.php:2412
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
AccountLine\dateo_previous
dateo_previous($id)
Decrease operation date of a rowid.
Definition: account.class.php:2355
AccountLine\datev_next
datev_next($id)
Increase value date of a rowid.
Definition: account.class.php:2279
AccountLine\getVentilExportCompta
getVentilExportCompta()
Return if a bank line was dispatched into bookkeeping.
Definition: account.class.php:2531
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:6016
Account\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: account.class.php:1708
Account\__toString
__toString()
Shows the account number in the appropriate format.
Definition: account.class.php:374
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:137
AccountLine\insert
insert()
Inserts a transaction to a bank account.
Definition: account.class.php:1971
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:959
Account\solde
solde($option=0, $date_end='', $field='dateo')
Return current sold.
Definition: account.class.php:1212
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6125
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10709
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:4360
AccountLine
Class to manage bank transaction lines.
Definition: account.class.php:1755
Account\replaceThirdparty
static replaceThirdparty($dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: account.class.php:1737
Account\create
create(User $user, $notrigger=0)
Create bank account into database.
Definition: account.class.php:645
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2951
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5800
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:1534
AccountLine\LibStatut
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
Definition: account.class.php:2487
AccountLine\delete_urls
delete_urls(User $user=null)
Delete bank line records.
Definition: account.class.php:2115
Account\getLibStatut
getLibStatut($mode=0)
Return label of object status.
Definition: account.class.php:1133
Account
Class to manage bank accounts.
Definition: account.class.php:39
getIbanHumanReadable
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition: bank.lib.php:317
Account\needIBAN
needIBAN()
Return 1 if IBAN / BIC is mandatory (otherwise option)
Definition: account.class.php:1552
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:430
AccountLine\update
update(User $user, $notrigger=0)
Update bank account record in database.
Definition: account.class.php:2152
Account\update_bban
update_bban(User $user=null)
Update BBAN (RIB) account fields.
Definition: account.class.php:907
AccountLine\info
info($id)
Load miscellaneous information for tab "Info".
Definition: account.class.php:2368