dolibarr  x.y.z
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  global $conf;
1081 
1082  $error = 0;
1083 
1084  $this->db->begin();
1085 
1086  // Delete link between tag and bank account
1087  if (!$error) {
1088  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1089  $sql .= " WHERE fk_account = ".((int) $this->id);
1090 
1091  $resql = $this->db->query($sql);
1092  if (!$resql) {
1093  $error++;
1094  $this->error = "Error ".$this->db->lasterror();
1095  }
1096  }
1097 
1098  if (!$error) {
1099  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account";
1100  $sql .= " WHERE rowid = ".((int) $this->rowid);
1101 
1102  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1103  $result = $this->db->query($sql);
1104  if ($result) {
1105  // Remove extrafields
1106  if (!$error) {
1107  $result = $this->deleteExtraFields();
1108  if ($result < 0) {
1109  $error++;
1110  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1111  }
1112  }
1113  } else {
1114  $error++;
1115  $this->error = "Error ".$this->db->lasterror();
1116  }
1117  }
1118 
1119  if (!$error) {
1120  $this->db->commit();
1121  return 1;
1122  } else {
1123  $this->db->rollback();
1124  return -1;
1125  }
1126  }
1127 
1128 
1135  public function getLibStatut($mode = 0)
1136  {
1137  return $this->LibStatut($this->clos, $mode);
1138  }
1139 
1140  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1148  public function LibStatut($status, $mode = 0)
1149  {
1150  // phpcs:enable
1151  global $langs;
1152  $langs->load('banks');
1153 
1154  if ($status == self::STATUS_OPEN) {
1155  $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1156  $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1157  $statusType = 'status4';
1158  } else {
1159  $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1160  $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1161  $statusType = 'status5';
1162  }
1163 
1164  return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1165  }
1166 
1167 
1168  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1174  public function can_be_deleted()
1175  {
1176  // phpcs:enable
1177  $can_be_deleted = false;
1178 
1179  $sql = "SELECT COUNT(rowid) as nb";
1180  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1181  $sql .= " WHERE fk_account = ".((int) $this->id);
1182 
1183  $resql = $this->db->query($sql);
1184  if ($resql) {
1185  $obj = $this->db->fetch_object($resql);
1186  if ($obj->nb <= 1) {
1187  $can_be_deleted = true; // Juste le solde
1188  }
1189  } else {
1190  dol_print_error($this->db);
1191  }
1192  return $can_be_deleted;
1193  }
1194 
1195 
1201  public function error()
1202  {
1203  return $this->error;
1204  }
1205 
1214  public function solde($option = 0, $date_end = '', $field = 'dateo')
1215  {
1216  $solde = 0;
1217 
1218  $sql = "SELECT sum(amount) as amount";
1219  $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1220  $sql .= " WHERE fk_account = ".((int) $this->id);
1221  if ($option == 1) {
1222  $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1223  }
1224 
1225  $resql = $this->db->query($sql);
1226  if ($resql) {
1227  if ($this->db->num_rows($resql)) {
1228  $obj = $this->db->fetch_object($resql);
1229  $solde = $obj->amount;
1230  }
1231  $this->db->free($resql);
1232  } else {
1233  $this->errors[] = $this->db->lasterror;
1234  return -1;
1235  }
1236 
1237  return price2num($solde, 'MU');
1238  }
1239 
1240  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1248  public function load_board(User $user, $filteraccountid = 0)
1249  {
1250  // phpcs:enable
1251  global $conf, $langs;
1252 
1253  if ($user->socid) {
1254  return -1; // protection pour eviter appel par utilisateur externe
1255  }
1256 
1257  $sql = "SELECT b.rowid, b.datev as datefin";
1258  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1259  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1260  $sql .= " WHERE b.rappro=0";
1261  $sql .= " AND b.fk_account = ba.rowid";
1262  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1263  $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1264  $sql .= " AND clos = 0";
1265  if ($filteraccountid) {
1266  $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1267  }
1268 
1269  $resql = $this->db->query($sql);
1270  if ($resql) {
1271  $langs->load("banks");
1272  $now = dol_now();
1273 
1274  require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1275 
1276  $response = new WorkboardResponse();
1277  $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1278  $response->label = $langs->trans("TransactionsToConciliate");
1279  $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1280  $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1281  $response->img = img_object('', "payment");
1282 
1283  while ($obj = $this->db->fetch_object($resql)) {
1284  $response->nbtodo++;
1285  if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1286  $response->nbtodolate++;
1287  }
1288  }
1289 
1290  return $response;
1291  } else {
1292  dol_print_error($this->db);
1293  $this->error = $this->db->error();
1294  return -1;
1295  }
1296  }
1297 
1298  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1304  public function load_state_board($filteraccountid = 0)
1305  {
1306  // phpcs:enable
1307  global $user;
1308 
1309  if ($user->socid) {
1310  return -1; // protection pour eviter appel par utilisateur externe
1311  }
1312 
1313  $sql = "SELECT count(b.rowid) as nb";
1314  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1315  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1316  $sql .= " WHERE b.fk_account = ba.rowid";
1317  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1318  $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1319  $sql .= " AND clos = 0";
1320  if ($filteraccountid) {
1321  $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1322  }
1323 
1324  $resql = $this->db->query($sql);
1325  if ($resql) {
1326  while ($obj = $this->db->fetch_object($resql)) {
1327  $this->nb["banklines"] = $obj->nb;
1328  }
1329  $this->db->free($resql);
1330  } else {
1331  dol_print_error($this->db);
1332  $this->error = $this->db->error();
1333  return -1;
1334  }
1335  }
1336 
1337 
1343  public function countAccountToReconcile()
1344  {
1345  global $db, $conf, $user;
1346 
1347  //Protection against external users
1348  if ($user->socid) {
1349  return 0;
1350  }
1351 
1352  $nb = 0;
1353 
1354  $sql = "SELECT COUNT(ba.rowid) as nb";
1355  $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1356  $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1357  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1358  if (empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
1359  $sql .= " AND ba.courant != 2";
1360  }
1361  $resql = $this->db->query($sql);
1362  if ($resql) {
1363  $obj = $this->db->fetch_object($resql);
1364  $nb = $obj->nb;
1365  } else {
1366  dol_print_error($this->db);
1367  }
1368 
1369  return $nb;
1370  }
1371 
1382  public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0)
1383  {
1384  global $conf, $langs, $user;
1385  include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1386 
1387  $result = '';
1388  $label = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
1389  if (isset($this->status)) {
1390  $label .= ' '.$this->getLibStatut(5);
1391  }
1392  $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1393  $label .= '<br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
1394  $label .= '<br><b>'.$langs->trans('IBAN').':</b> '.getIbanHumanReadable($this);
1395  $label .= '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
1396  $label .= '<br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
1397 
1398  if (empty($user->rights->banque->lire) || !empty($user->socid)) {
1399  $option = 'nolink';
1400  }
1401 
1402  if (isModEnabled('accounting')) {
1403  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1404  $langs->load("accountancy");
1405  $label .= '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
1406  $label .= '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
1407  }
1408 
1409  $linkclose = '" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1410 
1411  $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1412  if ($mode == 'transactions') {
1413  $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1414  } elseif ($mode == 'receipts') {
1415  $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1416  }
1417 
1418  if ($option != 'nolink') {
1419  // Add param to save lastsearch_values or not
1420  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1421  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1422  $add_save_lastsearch_values = 1;
1423  }
1424  if ($add_save_lastsearch_values) {
1425  $url .= '&save_lastsearch_values=1';
1426  }
1427  }
1428 
1429  $linkstart = '<a href="'.$url.$linkclose;
1430  $linkend = '</a>';
1431 
1432  if ($option == 'nolink') {
1433  $linkstart = '';
1434  $linkend = '';
1435  }
1436 
1437  $result .= $linkstart;
1438  if ($withpicto) {
1439  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1440  }
1441  if ($withpicto != 2) {
1442  $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1443  }
1444  $result .= $linkend;
1445 
1446  return $result;
1447  }
1448 
1449 
1450  // Method after here are common to Account and CompanyBankAccount
1451 
1452 
1458  public function verif()
1459  {
1460  require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1461 
1462  $this->error_number = 0;
1463 
1464  // Call function to check BAN
1465 
1466  if (!checkIbanForAccount($this)) {
1467  $this->error_number = 12;
1468  $this->error_message = 'IBANNotValid';
1469  }
1470  if (!checkSwiftForAccount($this)) {
1471  $this->error_number = 12;
1472  $this->error_message = 'SwiftNotValid';
1473  }
1474  /*if (! checkBanForAccount($this))
1475  {
1476  $this->error_number = 12;
1477  $this->error_message = 'BANControlError';
1478  }*/
1479 
1480  if ($this->error_number == 0) {
1481  return 1;
1482  } else {
1483  return 0;
1484  }
1485  }
1486 
1492  public function getCountryCode()
1493  {
1494  global $mysoc;
1495 
1496  // We return country code of bank account
1497  if (!empty($this->country_code)) {
1498  return $this->country_code;
1499  }
1500 
1501  // For backward compatibility, we try to guess country from other information
1502  if (!empty($this->iban)) {
1503  // If IBAN defined, we can know country of account from it
1504  $reg = array();
1505  if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
1506  return $reg[1];
1507  }
1508  }
1509 
1510  // If this class is linked to a third party
1511  if (!empty($this->socid)) {
1512  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
1513  $company = new Societe($this->db);
1514  $result = $company->fetch($this->socid);
1515  if (!empty($company->country_code)) {
1516  return $company->country_code;
1517  }
1518  }
1519 
1520  // We return country code of managed company
1521  if (!empty($mysoc->country_code)) {
1522  return $mysoc->country_code;
1523  }
1524 
1525  return '';
1526  }
1527 
1536  public function useDetailedBBAN()
1537  {
1538  $country_code = $this->getCountryCode();
1539 
1540  if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
1541  return 1; // France, Spain, Gabon, ... - Not valid for CH
1542  }
1543  if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
1544  return 2; // Australia, England...
1545  }
1546  return 0;
1547  }
1548 
1554  public function needIBAN()
1555  {
1556  global $conf;
1557 
1558  if (!empty($conf->global->MAIN_IBAN_IS_NEVER_MANDATORY)) {
1559  return 0;
1560  }
1561 
1562  $country_code = $this->getCountryCode();
1563 
1564  $country_code_in_EEC = array(
1565  'AT', // Austria
1566  'BE', // Belgium
1567  'BG', // Bulgaria
1568  'CY', // Cyprus
1569  'CZ', // Czech republic
1570  'DE', // Germany
1571  'DK', // Danemark
1572  'EE', // Estonia
1573  'ES', // Spain
1574  'FI', // Finland
1575  'FR', // France
1576  'GB', // United Kingdom
1577  'GR', // Greece
1578  'HR', // Croatia
1579  'NL', // Holland
1580  'HU', // Hungary
1581  'IE', // Ireland
1582  'IM', // Isle of Man - Included in UK
1583  'IT', // Italy
1584  'LT', // Lithuania
1585  'LU', // Luxembourg
1586  'LV', // Latvia
1587  'MC', // Monaco - Included in France
1588  'MT', // Malta
1589  //'NO', // Norway
1590  'PL', // Poland
1591  'PT', // Portugal
1592  'RO', // Romania
1593  'SE', // Sweden
1594  'SK', // Slovakia
1595  'SI', // Slovenia
1596  'UK', // United Kingdom
1597  //'CH', // Switzerland - No. Swizerland in not in EEC
1598  );
1599 
1600  if (in_array($country_code, $country_code_in_EEC)) {
1601  return 1; // France, Spain, ...
1602  }
1603  return 0;
1604  }
1605 
1612  public function info($id)
1613  {
1614  }
1615 
1630  public function getFieldsToShow($includeibanbic = 0)
1631  {
1632  //Get the required properties depending on the country
1633  $detailedBBAN = $this->useDetailedBBAN();
1634 
1635  if ($detailedBBAN == 0) {
1636  $fieldarray = array(
1637  'BankAccountNumber'
1638  );
1639  } elseif ($detailedBBAN == 2) {
1640  $fieldarray = array(
1641  'BankCode',
1642  'BankAccountNumber'
1643  );
1644  } else {
1645  $fieldarray = self::getAccountNumberOrder();
1646  }
1647 
1648  //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1649  if ($includeibanbic) {
1650  $fieldarray[] = 'IBAN';
1651  $fieldarray[] = 'BIC';
1652  }
1653  //}
1654 
1655  //Get the order the properties are shown
1656  return $fieldarray;
1657  }
1658 
1669  public static function getAccountNumberOrder()
1670  {
1671  global $conf;
1672 
1673  $fieldlists = array(
1674  'BankCode',
1675  'DeskCode',
1676  'BankAccountNumber',
1677  'BankAccountNumberKey'
1678  );
1679 
1680  if (!empty($conf->global->BANK_SHOW_ORDER_OPTION)) {
1681  if (is_numeric($conf->global->BANK_SHOW_ORDER_OPTION)) {
1682  if ($conf->global->BANK_SHOW_ORDER_OPTION == '1') {
1683  $fieldlists = array(
1684  'BankCode',
1685  'DeskCode',
1686  'BankAccountNumberKey',
1687  'BankAccountNumber'
1688  );
1689  }
1690  } else {
1691  //Replace the old AccountNumber key with the new BankAccountNumber key
1692  $fieldlists = explode(
1693  ' ',
1694  preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
1695  );
1696  }
1697  }
1698 
1699  return $fieldlists;
1700  }
1701 
1702 
1710  public function initAsSpecimen()
1711  {
1712  // Example of IBAN FR7630001007941234567890185
1713  $this->specimen = 1;
1714  $this->ref = 'MBA';
1715  $this->label = 'My Big Company Bank account';
1716  $this->bank = 'MyBank';
1717  $this->courant = Account::TYPE_CURRENT;
1718  $this->clos = Account::STATUS_OPEN;
1719  $this->code_banque = '30001';
1720  $this->code_guichet = '00794';
1721  $this->number = '12345678901';
1722  $this->cle_rib = '85';
1723  $this->bic = 'AA12';
1724  $this->iban = 'FR7630001007941234567890185';
1725  $this->domiciliation = 'Banque de France';
1726  $this->proprio = 'Owner';
1727  $this->owner_address = 'Owner address';
1728  $this->country_id = 1;
1729  }
1730 
1739  public static function replaceThirdparty($dbs, $origin_id, $dest_id)
1740  {
1741  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'";
1742 
1743  if ($dbs->query($sql)) {
1744  return true;
1745  } else {
1746  //if ($ignoreerrors) return true; // TODO Not enough. If there is A-B on kept thirdparty and B-C on old one, we must get A-B-C after merge. Not A-B.
1747  //$this->errors = $dbs->lasterror();
1748  return false;
1749  }
1750  }
1751 }
1752 
1753 
1758 {
1762  public $error = '';
1763 
1767  public $db;
1768 
1772  public $element = 'bank';
1773 
1777  public $table_element = 'bank';
1778 
1782  public $picto = 'accountline';
1783 
1787  public $id;
1788 
1792  public $ref;
1793 
1799  public $datec;
1800 
1806  public $dateo;
1807 
1813  public $datev;
1814 
1815  public $amount; /* Amount of payment in the bank account currency */
1816  public $amount_main_currency; /* Amount in the currency of company if bank account use another currency */
1817 
1821  public $fk_user_author;
1822 
1826  public $fk_user_rappro;
1827 
1831  public $fk_type;
1832 
1836  public $fk_bordereau;
1837 
1841  public $fk_account;
1842 
1846  public $bank_account_ref;
1847 
1851  public $bank_account_label;
1852 
1856  public $numero_compte;
1857 
1861  public $emetteur;
1862 
1863  public $rappro; // Is it conciliated
1864  public $num_releve; // If conciliated, what is bank statement
1865  public $num_chq; // Num of cheque
1866  public $bank_chq; // Bank of cheque
1867 
1871  public $label;
1872 
1873  public $note;
1874 
1875 
1876 
1882  public function __construct(DoliDB $db)
1883  {
1884  $this->db = $db;
1885  }
1886 
1895  public function fetch($rowid, $ref = '', $num = '')
1896  {
1897  global $conf;
1898 
1899  // Check parameters
1900  if (empty($rowid) && empty($ref) && empty($num)) {
1901  return -1;
1902  }
1903 
1904  $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
1905  $sql .= " b.fk_user_author, b.fk_user_rappro,";
1906  $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
1907  $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
1908  //$sql.= " b.author"; // Is this used ?
1909  $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
1910  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1911  $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1912  $sql .= " WHERE b.fk_account = ba.rowid";
1913  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1914  if ($num) {
1915  $sql .= " AND b.num_chq='".$this->db->escape($num)."'";
1916  } elseif ($ref) {
1917  $sql .= " AND b.rowid='".$this->db->escape($ref)."'";
1918  } else {
1919  $sql .= " AND b.rowid = ".((int) $rowid);
1920  }
1921 
1922  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1923  $result = $this->db->query($sql);
1924  if ($result) {
1925  $ret = 0;
1926 
1927  $obj = $this->db->fetch_object($result);
1928  if ($obj) {
1929  $this->id = $obj->rowid;
1930  $this->rowid = $obj->rowid;
1931  $this->ref = $obj->rowid;
1932 
1933  $this->datec = $obj->datec;
1934  $this->datev = $obj->datev;
1935  $this->dateo = $obj->dateo;
1936  $this->amount = $obj->amount;
1937  $this->label = $obj->label;
1938  $this->note = $obj->note;
1939 
1940  $this->fk_user_author = $obj->fk_user_author;
1941  $this->fk_user_rappro = $obj->fk_user_rappro;
1942 
1943  $this->fk_type = $obj->fk_type; // Type of transaction
1944  $this->rappro = $obj->rappro;
1945  $this->num_releve = $obj->num_releve;
1946 
1947  $this->num_chq = $obj->num_chq;
1948  $this->bank_chq = $obj->banque;
1949  $this->fk_bordereau = $obj->fk_bordereau;
1950 
1951  $this->fk_account = $obj->fk_account;
1952  $this->bank_account_ref = $obj->bank_account_ref;
1953  $this->bank_account_label = $obj->bank_account_label;
1954 
1955  // Retrieve all extrafield
1956  // fetch optionals attributes and labels
1957  $this->fetch_optionals();
1958 
1959  $ret = 1;
1960  }
1961  $this->db->free($result);
1962  return $ret;
1963  } else {
1964  return -1;
1965  }
1966  }
1967 
1973  public function insert()
1974  {
1975  $error = 0;
1976 
1977  $this->db->begin();
1978 
1979  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
1980  $sql .= "datec";
1981  $sql .= ", dateo";
1982  $sql .= ", datev";
1983  $sql .= ", label";
1984  $sql .= ", amount";
1985  $sql .= ", amount_main_currency";
1986  $sql .= ", fk_user_author";
1987  $sql .= ", num_chq";
1988  $sql .= ", fk_account";
1989  $sql .= ", fk_type";
1990  $sql .= ", emetteur,banque";
1991  $sql .= ", rappro";
1992  $sql .= ", numero_compte";
1993  $sql .= ", num_releve";
1994  $sql .= ") VALUES (";
1995  $sql .= "'".$this->db->idate($this->datec)."'";
1996  $sql .= ", '".$this->db->idate($this->dateo)."'";
1997  $sql .= ", '".$this->db->idate($this->datev)."'";
1998  $sql .= ", '".$this->db->escape($this->label)."'";
1999  $sql .= ", ".price2num($this->amount);
2000  $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
2001  $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
2002  $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
2003  $sql .= ", '".$this->db->escape($this->fk_account)."'";
2004  $sql .= ", '".$this->db->escape($this->fk_type)."'";
2005  $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
2006  $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
2007  $sql .= ", ".(int) $this->rappro;
2008  $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2009  $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2010  $sql .= ")";
2011 
2012  dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2013  $resql = $this->db->query($sql);
2014  if ($resql) {
2015  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2016  // Actions on extra fields (by external module or standard code)
2017  $result = $this->insertExtraFields();
2018  if ($result < 0) {
2019  $error++;
2020  }
2021  } else {
2022  $error++;
2023  $this->error = $this->db->lasterror();
2024  dol_print_error($this->db);
2025  }
2026 
2027  if (!$error) {
2028  $this->db->commit();
2029  return $this->id;
2030  } else {
2031  $this->db->rollback();
2032  return -1 * $error;
2033  }
2034  }
2035 
2042  public function delete(User $user = null)
2043  {
2044  global $conf;
2045 
2046  $nbko = 0;
2047 
2048  if ($this->rappro) {
2049  // Protection to avoid any delete of consolidated lines
2050  $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2051  return -1;
2052  }
2053 
2054  $this->db->begin();
2055 
2056  // Protection to avoid any delete of accounted lines. Protection on by default
2057  if (empty($conf->global->BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING)) {
2058  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2059  $resql = $this->db->query($sql);
2060  if ($resql) {
2061  $obj = $this->db->fetch_object($resql);
2062  if ($obj && $obj->nb) {
2063  $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2064  $this->db->rollback();
2065  return -1;
2066  }
2067  } else {
2068  $this->error = $this->db->lasterror();
2069  $this->db->rollback();
2070  return -1;
2071  }
2072  }
2073 
2074  // Delete urls
2075  $result = $this->delete_urls($user);
2076  if ($result < 0) {
2077  $nbko++;
2078  }
2079 
2080  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".(int) $this->rowid;
2081  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2082  $result = $this->db->query($sql);
2083  if (!$result) {
2084  $nbko++;
2085  }
2086 
2087  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_extrafields WHERE fk_object=".(int) $this->rowid;
2088  $result = $this->db->query($sql);
2089  if (!$result) {
2090  $nbko++;
2091  }
2092 
2093  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2094  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2095  $result = $this->db->query($sql);
2096  if (!$result) {
2097  $nbko++;
2098  }
2099 
2100  if (!$nbko) {
2101  $this->db->commit();
2102  return 1;
2103  } else {
2104  $this->db->rollback();
2105  return -$nbko;
2106  }
2107  }
2108 
2109 
2110  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2117  public function delete_urls(User $user = null)
2118  {
2119  // phpcs:enable
2120  $nbko = 0;
2121 
2122  if ($this->rappro) {
2123  // Protection to avoid any delete of consolidated lines
2124  $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2125  return -1;
2126  }
2127 
2128  $this->db->begin();
2129 
2130  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2131  dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
2132  $result = $this->db->query($sql);
2133  if (!$result) {
2134  $nbko++;
2135  }
2136 
2137  if (!$nbko) {
2138  $this->db->commit();
2139  return 1;
2140  } else {
2141  $this->db->rollback();
2142  return -$nbko;
2143  }
2144  }
2145 
2146 
2154  public function update(User $user, $notrigger = 0)
2155  {
2156  $this->db->begin();
2157 
2158  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2159  $sql .= " amount = ".price2num($this->amount).",";
2160  $sql .= " datev='".$this->db->idate($this->datev)."',";
2161  $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2162  $sql .= " WHERE rowid = ".((int) $this->rowid);
2163 
2164  dol_syslog(get_class($this)."::update", LOG_DEBUG);
2165  $resql = $this->db->query($sql);
2166  if ($resql) {
2167  $this->db->commit();
2168  return 1;
2169  } else {
2170  $this->db->rollback();
2171  $this->error = $this->db->error();
2172  return -1;
2173  }
2174  }
2175 
2176 
2177  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2186  public function update_conciliation(User $user, $cat, $conciliated = 1)
2187  {
2188  // phpcs:enable
2189  global $conf, $langs;
2190 
2191  $this->db->begin();
2192 
2193  // Check statement field
2194  if (!empty($conf->global->BANK_STATEMENT_REGEX_RULE)) {
2195  if (!preg_match('/'.$conf->global->BANK_STATEMENT_REGEX_RULE.'/', $this->num_releve)) {
2196  $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", $conf->global->BANK_STATEMENT_REGEX_RULE);
2197  return -1;
2198  }
2199  }
2200 
2201  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2202  $sql .= " rappro = ".((int) $conciliated);
2203  $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2204  if ($conciliated) {
2205  $sql .= ", fk_user_rappro = ".$user->id;
2206  }
2207  $sql .= " WHERE rowid = ".((int) $this->id);
2208 
2209  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2210  $resql = $this->db->query($sql);
2211  if ($resql) {
2212  if (!empty($cat)) {
2213  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
2214  $sql .= "lineid";
2215  $sql .= ", fk_categ";
2216  $sql .= ") VALUES (";
2217  $sql .= $this->id;
2218  $sql .= ", ".((int) $cat);
2219  $sql .= ")";
2220 
2221  dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2222  $this->db->query($sql);
2223 
2224  // No error check. Can fail if category already affected
2225  }
2226 
2227  $this->rappro = 1;
2228 
2229  $this->db->commit();
2230  return 1;
2231  } else {
2232  $this->db->rollback();
2233  return -1;
2234  }
2235  }
2236 
2237 
2238  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2246  public function datev_change($rowid, $sign = 1)
2247  {
2248  // phpcs:enable
2249  $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2250  $resql = $this->db->query($sql);
2251  if ($resql) {
2252  $obj = $this->db->fetch_object($resql);
2253  $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2254 
2255  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2256  $sql .= " datev = '".$this->db->idate($newdate)."'";
2257  $sql .= " WHERE rowid = ".((int) $rowid);
2258 
2259  $result = $this->db->query($sql);
2260  if ($result) {
2261  if ($this->db->affected_rows($result)) {
2262  return 1;
2263  }
2264  } else {
2265  dol_print_error($this->db);
2266  return 0;
2267  }
2268  } else {
2269  dol_print_error($this->db);
2270  }
2271  return 0;
2272  }
2273 
2274  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2281  public function datev_next($id)
2282  {
2283  // phpcs:enable
2284  return $this->datev_change($id, 1);
2285  }
2286 
2287  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2294  public function datev_previous($id)
2295  {
2296  // phpcs:enable
2297  return $this->datev_change($id, -1);
2298  }
2299 
2300 
2301  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2309  public function dateo_change($rowid, $sign = 1)
2310  {
2311  // phpcs:enable
2312  $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2313  $resql = $this->db->query($sql);
2314  if ($resql) {
2315  $obj = $this->db->fetch_object($resql);
2316  $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2317 
2318  $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2319  $sql .= " dateo = '".$this->db->idate($newdate)."'";
2320  $sql .= " WHERE rowid = ".((int) $rowid);
2321 
2322  $result = $this->db->query($sql);
2323  if ($result) {
2324  if ($this->db->affected_rows($result)) {
2325  return 1;
2326  }
2327  } else {
2328  dol_print_error($this->db);
2329  return 0;
2330  }
2331  } else {
2332  dol_print_error($this->db);
2333  }
2334  return 0;
2335  }
2336 
2337  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2344  public function dateo_next($id)
2345  {
2346  // phpcs:enable
2347  return $this->dateo_change($id, 1);
2348  }
2349 
2350  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2357  public function dateo_previous($id)
2358  {
2359  // phpcs:enable
2360  return $this->dateo_change($id, -1);
2361  }
2362 
2363 
2370  public function info($id)
2371  {
2372  $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2373  $sql .= ' b.fk_user_author, b.fk_user_rappro';
2374  $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2375  $sql .= ' WHERE b.rowid = '.((int) $id);
2376 
2377  $result = $this->db->query($sql);
2378  if ($result) {
2379  if ($this->db->num_rows($result)) {
2380  $obj = $this->db->fetch_object($result);
2381  $this->id = $obj->rowid;
2382 
2383  if ($obj->fk_user_author) {
2384  $cuser = new User($this->db);
2385  $cuser->fetch($obj->fk_user_author);
2386  $this->user_creation = $cuser;
2387  }
2388  if ($obj->fk_user_rappro) {
2389  $ruser = new User($this->db);
2390  $ruser->fetch($obj->fk_user_rappro);
2391  $this->user_rappro = $ruser;
2392  }
2393 
2394  $this->date_creation = $this->db->jdate($obj->datec);
2395  $this->date_modification = $this->db->jdate($obj->datem);
2396  //$this->date_rappro = $obj->daterappro; // Not yet managed
2397  }
2398  $this->db->free($result);
2399  } else {
2400  dol_print_error($this->db);
2401  }
2402  }
2403 
2404 
2414  public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2415  {
2416  global $langs;
2417 
2418  $result = '';
2419 
2420  $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2421  $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2422 
2423  $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">';
2424  $linkend = '</a>';
2425 
2426  $result .= $linkstart;
2427  if ($withpicto) {
2428  $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);
2429  }
2430  if ($withpicto != 2) {
2431  $result .= ($this->ref ? $this->ref : $this->id);
2432  }
2433  $result .= $linkend;
2434 
2435  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2436  $result .= ' <span class="opacitymedium">(';
2437  }
2438  if ($option == 'showall') {
2439  $result .= $langs->trans("BankAccount").': ';
2440  $accountstatic = new Account($this->db);
2441  $accountstatic->id = $this->fk_account;
2442  $accountstatic->ref = $this->bank_account_ref;
2443  $accountstatic->label = $this->bank_account_label;
2444  $result .= $accountstatic->getNomUrl(0).', ';
2445  }
2446  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2447  $result .= $langs->trans("BankLineConciliated").': ';
2448  $result .= yn($this->rappro);
2449  }
2450  if ($option == 'showall' || $option == 'showconciliatedandaccounted') {
2451  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2452  $resql = $this->db->query($sql);
2453  if ($resql) {
2454  $obj = $this->db->fetch_object($resql);
2455  if ($obj && $obj->nb) {
2456  $result .= ' - '.$langs->trans("Accounted").': '.yn(1);
2457  } else {
2458  $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2459  }
2460  }
2461  }
2462  if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2463  $result .= ')</span>';
2464  }
2465 
2466  return $result;
2467  }
2468 
2469 
2476  public function getLibStatut($mode = 0)
2477  {
2478  return $this->LibStatut($this->status, $mode);
2479  }
2480 
2481  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2489  public function LibStatut($status, $mode = 0)
2490  {
2491  // phpcs:enable
2492  global $langs;
2493  //$langs->load('companies');
2494  /*
2495  if ($mode == 0)
2496  {
2497  if ($status==0) return $langs->trans("ActivityCeased");
2498  if ($status==1) return $langs->trans("InActivity");
2499  }
2500  if ($mode == 1)
2501  {
2502  if ($status==0) return $langs->trans("ActivityCeased");
2503  if ($status==1) return $langs->trans("InActivity");
2504  }
2505  if ($mode == 2)
2506  {
2507  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2508  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2509  }
2510  if ($mode == 3)
2511  {
2512  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2513  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2514  }
2515  if ($mode == 4)
2516  {
2517  if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2518  if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2519  }
2520  if ($mode == 5)
2521  {
2522  if ($status==0) return $langs->trans("ActivityCeased").' '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2523  if ($status==1) return $langs->trans("InActivity").' '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2524  }*/
2525  }
2526 
2527 
2533  public function getVentilExportCompta()
2534  {
2535  $alreadydispatched = 0;
2536 
2537  $type = 'bank';
2538 
2539  $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);
2540  $resql = $this->db->query($sql);
2541  if ($resql) {
2542  $obj = $this->db->fetch_object($resql);
2543  if ($obj) {
2544  $alreadydispatched = $obj->nb;
2545  }
2546  } else {
2547  $this->error = $this->db->lasterror();
2548  return -1;
2549  }
2550 
2551  if ($alreadydispatched) {
2552  return 1;
2553  }
2554  return 0;
2555  }
2556 }
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:45
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