dolibarr 24.0.3
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-2025 Frédéric France <frederic.france@free.fr>
13 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <https://www.gnu.org/licenses/>.
27 */
28
35require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
36
37
41class Account extends CommonObject
42{
46 public $element = 'bank_account';
47
51 public $table_element = 'bank_account';
52
56 public $picto = 'account';
57
63 public $ref;
64
70 public $rowid;
71
76 public $label;
77
84 public $courant;
85
90 public $type;
91
96 public $bank;
97
105 public $clos = self::STATUS_OPEN;
106
111 public $rappro = 1;
112
117 public $url;
118
123 public $code_banque;
124
129 public $code_guichet;
130
135 public $number;
136
141 public $cle_rib;
142
147 public $bic;
148
153 public $iban;
154
161 public $iban_prefix;
162
167 public $pti_in_ctti = 0;
168
175 public $proprio;
176
181 public $owner_name;
182
187 public $owner_address;
188
193 public $owner_zip;
194
199 public $owner_town;
203 public $owner_country_id;
207 public $owner_country_code;
208
215 public $domiciliation;
216
221 public $address;
225 public $state_id;
229 public $state_code;
233 public $state;
237 public $country_id;
238
244 public $type_lib = array();
245
250 public $account_number;
251
255 public $fk_accountancy_journal;
256
260 public $accountancy_journal;
261
266 public $currency_code;
267
274 public $account_currency_code;
275
280 public $min_allowed;
281
286 public $min_desired;
287
292 public $comment;
293
298 public $date_solde;
299
306 public $solde;
307
312 public $balance;
313
318 public $ics;
319
324 public $ics_transfer;
325
329 public $oldref;
330
331
356 // BEGIN MODULEBUILDER PROPERTIES
360 public $fields = array(
361 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
362 'ref' => array('type' => 'varchar(12)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'showoncombobox' => 1, 'position' => 25),
363 'label' => array('type' => 'varchar(30)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30),
364 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 35, 'index' => 1),
365 'bank' => array('type' => 'varchar(60)', 'label' => 'Bank', 'enabled' => 1, 'visible' => -1, 'position' => 40),
366 'code_banque' => array('type' => 'varchar(128)', 'label' => 'Code banque', 'enabled' => 1, 'visible' => -1, 'position' => 45),
367 'code_guichet' => array('type' => 'varchar(6)', 'label' => 'Code guichet', 'enabled' => 1, 'visible' => -1, 'position' => 50),
368 'number' => array('type' => 'varchar(255)', 'label' => 'Number', 'enabled' => 1, 'visible' => -1, 'position' => 55),
369 'cle_rib' => array('type' => 'varchar(5)', 'label' => 'Cle rib', 'enabled' => 1, 'visible' => -1, 'position' => 60),
370 'bic' => array('type' => 'varchar(11)', 'label' => 'Bic', 'enabled' => 1, 'visible' => -1, 'position' => 65),
371 'iban_prefix' => array('type' => 'varchar(34)', 'label' => 'Iban prefix', 'enabled' => 1, 'visible' => -1, 'position' => 70),
372 'country_iban' => array('type' => 'varchar(2)', 'label' => 'Country iban', 'enabled' => 1, 'visible' => -1, 'position' => 75),
373 'cle_iban' => array('type' => 'varchar(2)', 'label' => 'Cle iban', 'enabled' => 1, 'visible' => -1, 'position' => 80),
374 'domiciliation' => array('type' => 'varchar(255)', 'label' => 'Domiciliation', 'enabled' => 1, 'visible' => -1, 'position' => 85),
375 'state_id' => array('type' => 'integer', 'label' => 'StateId', 'enabled' => 1, 'visible' => -1, 'position' => 90),
376 'fk_pays' => array('type' => 'integer', 'label' => 'Country', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 95),
377 'proprio' => array('type' => 'varchar(60)', 'label' => 'Proprio', 'enabled' => 1, 'visible' => -1, 'position' => 100),
378 'owner_address' => array('type' => 'varchar(255)', 'label' => 'Owner address', 'enabled' => 1, 'visible' => -1, 'position' => 105),
379 'owner_zip' => array('type' => 'varchar(25)', 'label' => 'Owner zip', 'enabled' => 1, 'visible' => -1, 'position' => 106),
380 'owner_town' => array('type' => 'varchar(50)', 'label' => 'Owner town', 'enabled' => 1, 'visible' => -1, 'position' => 107),
381 'owner_country_id' => array('type' => 'integer', 'label' => 'Owner country', 'enabled' => 1, 'visible' => -1, 'position' => 108),
382 'courant' => array('type' => 'smallint(6)', 'label' => 'Courant', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 110),
383 'clos' => array('type' => 'smallint(6)', 'label' => 'Clos', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 115),
384 'rappro' => array('type' => 'smallint(6)', 'label' => 'Rappro', 'enabled' => 1, 'visible' => -1, 'position' => 120),
385 'url' => array('type' => 'varchar(128)', 'label' => 'Url', 'enabled' => 1, 'visible' => -1, 'position' => 125),
386 'account_number' => array('type' => 'varchar(32)', 'label' => 'Account number', 'enabled' => 1, 'visible' => -1, 'position' => 130),
387 'fk_accountancy_journal' => array('type' => 'integer', 'label' => 'Accountancy journal ID', 'enabled' => 1, 'visible' => -1, 'position' => 132),
388 'accountancy_journal' => array('type' => 'varchar(20)', 'label' => 'Accountancy journal', 'enabled' => 1, 'visible' => -1, 'position' => 135),
389 'currency_code' => array('type' => 'varchar(3)', 'label' => 'Currency code', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 140),
390 'min_allowed' => array('type' => 'integer', 'label' => 'Min allowed', 'enabled' => 1, 'visible' => -1, 'position' => 145),
391 'min_desired' => array('type' => 'integer', 'label' => 'Min desired', 'enabled' => 1, 'visible' => -1, 'position' => 150),
392 'comment' => array('type' => 'text', 'label' => 'Comment', 'enabled' => 1, 'visible' => -1, 'position' => 155),
393 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 156),
394 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 157),
395 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'position' => 160),
396 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 165),
397 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 170),
398 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 175),
399 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 180),
400 'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 185),
401 );
402 // END MODULEBUILDER PROPERTIES
403
407 const TYPE_CURRENT = 1;
411 const TYPE_CASH = 2;
415 const TYPE_SAVINGS = 0;
416
417
418 const STATUS_OPEN = 0;
419 const STATUS_CLOSED = 1;
420
421
427 public function __construct(DoliDB $db)
428 {
429 global $langs;
430
431 $this->db = $db;
432
433 $this->ismultientitymanaged = 1;
434
435 $this->balance = 0;
436
437 $this->type_lib = array(
438 self::TYPE_SAVINGS => $langs->transnoentitiesnoconv("BankType0"),
439 self::TYPE_CURRENT => $langs->transnoentitiesnoconv("BankType1"),
440 self::TYPE_CASH => $langs->transnoentitiesnoconv("BankType2"),
441 );
442
443 $this->labelStatus = array(
444 self::STATUS_OPEN => $langs->transnoentitiesnoconv("StatusAccountOpened"),
445 self::STATUS_CLOSED => $langs->transnoentitiesnoconv("StatusAccountClosed")
446 );
447 }
448
454 public function __toString()
455 {
456 $string = '';
457 foreach ($this->getFieldsToShow() as $val) {
458 if ($val == 'BankCode') {
459 $string .= $this->code_banque.' ';
460 } elseif ($val == 'BankAccountNumber') {
461 $string .= $this->number.' ';
462 } elseif ($val == 'DeskCode') {
463 $string .= $this->code_guichet.' ';
464 } elseif ($val == 'BankAccountNumberKey') {
465 $string .= $this->cle_rib.' ';
466 } elseif ($val == 'BIC') {
467 $string .= $this->bic.' ';
468 } elseif ($val == 'IBAN') {
469 $string .= $this->iban.' ';
470 }
471 }
472
473 return trim($string);
474 }
475
476
482 public function canBeConciliated()
483 {
484 if (empty($this->rappro)) {
485 return -1;
486 }
487 if ($this->type == Account::TYPE_CASH && !getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
488 return -2;
489 }
490 if ($this->status) {
491 return -3;
492 }
493 return 1;
494 }
495
496
497 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
508 public function add_url_line($line_id, $url_id, $url, $label, $type)
509 {
510 // phpcs:enable
511 // Avoid uk_bank_url collision when the same target (line_id, url_id, type) is linked twice
512 // e.g. two distinct credit transfers for the same employee dispatched on the same bank line.
513 $sqlcheck = "SELECT rowid FROM ".MAIN_DB_PREFIX."bank_url";
514 $sqlcheck .= " WHERE fk_bank = ".((int) $line_id);
515 $sqlcheck .= " AND url_id = ".((int) $url_id);
516 $sqlcheck .= " AND type = '".$this->db->escape($type)."'";
517 $resqlcheck = $this->db->query($sqlcheck);
518 if ($resqlcheck) {
519 $obj = $this->db->fetch_object($resqlcheck);
520 if ($obj) {
521 $this->db->free($resqlcheck);
522 return (int) $obj->rowid;
523 }
524 $this->db->free($resqlcheck);
525 }
526
527 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
528 $sql .= "fk_bank";
529 $sql .= ", url_id";
530 $sql .= ", url"; // deprecated
531 $sql .= ", label";
532 $sql .= ", type";
533 $sql .= ") VALUES (";
534 $sql .= ((int) $line_id);
535 $sql .= ", ".((int) $url_id);
536 $sql .= ", '".$this->db->escape($url)."'"; // deprecated
537 $sql .= ", '".$this->db->escape($label)."'";
538 $sql .= ", '".$this->db->escape($type)."'";
539 $sql .= ")";
540
541 dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
542 if ($this->db->query($sql)) {
543 $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
544 return $rowid;
545 } else {
546 $this->error = $this->db->lasterror();
547 return -1;
548 }
549 }
550
551 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
561 public function get_url($fk_bank = 0, $url_id = 0, $type = '')
562 {
563 // phpcs:enable
564 $lines = array();
565
566 // Check parameters
567 if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) {
568 $this->error = "ErrorBadParameter";
569 return -1;
570 }
571
572 $sql = "SELECT fk_bank, url_id, url, label, type";
573 $sql .= " FROM ".MAIN_DB_PREFIX."bank_url";
574 if ($fk_bank > 0) {
575 $sql .= " WHERE fk_bank = ".((int) $fk_bank);
576 } else {
577 $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'";
578 }
579 $sql .= " ORDER BY type, label";
580
581 dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
582 $result = $this->db->query($sql);
583 if ($result) {
584 $i = 0;
585 $num = $this->db->num_rows($result);
586 while ($i < $num) {
587 $obj = $this->db->fetch_object($result);
588 // Old links (for compatibility)
589 $lines[$i][0] = $obj->url;
590 $lines[$i][1] = $obj->url_id;
591 $lines[$i][2] = $obj->label;
592 $lines[$i][3] = $obj->type;
593 // New links
594 $lines[$i]['url'] = $obj->url;
595 $lines[$i]['url_id'] = $obj->url_id;
596 $lines[$i]['label'] = $obj->label;
597 $lines[$i]['type'] = $obj->type;
598 $lines[$i]['fk_bank'] = $obj->fk_bank;
599 $i++;
600 }
601 } else {
602 dol_print_error($this->db);
603 }
604
605 return $lines;
606 }
607
627 public function addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur = '', $banque = '', $accountancycode = '', $datev = null, $num_releve = '', $amount_main_currency = null, $note_private = '')
628 {
629 global $langs;
630
631 // Deprecation warning
632 if (is_numeric($oper)) {
633 dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING);
634 }
635
636 if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
637 $this->id = $this->rowid;
638 }
639
640 // Clean parameters
641 $emetteur = trim($emetteur);
642 $banque = trim($banque);
643 $label = trim($label);
644
645 $now = dol_now();
646
647 if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid
648 $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
649 $sql .= " WHERE id = ".((int) $oper);
650 $sql .= " AND entity IN (".getEntity('c_paiement').")";
651 $resql = $this->db->query($sql);
652 if ($resql) {
653 $obj = $this->db->fetch_object($resql);
654 $oper = $obj->code;
655 } else {
656 dol_print_error($this->db, 'Failed to get payment type code');
657 return -1;
658 }
659 }
660
661 // Check parameters
662 if (!$oper) {
663 $this->error = $langs->trans("OperNotDefined");
664 return -1;
665 }
666 if (!$this->id) {
667 $this->error = $langs->trans("ThisIdNotDefined");
668 return -2;
669 }
670 if ($this->type == Account::TYPE_CASH && $oper != 'LIQ') {
671 $this->error = "ErrorCashAccountAcceptsOnlyCashMoney";
672 return -3;
673 }
674
675 $this->db->begin();
676
677 if (is_null($datev) || empty($datev)) {
678 $datev = $date;
679 }
680
681 $accline = new AccountLine($this->db);
682 $accline->datec = $now;
683 $accline->dateo = $date;
684 $accline->datev = $datev;
685 $accline->label = $label;
686 $accline->amount = $amount;
687 $accline->amount_main_currency = $amount_main_currency;
688 $accline->fk_user_author = $user->id;
689 $accline->fk_account = $this->id;
690 $accline->fk_type = $oper;
691 $accline->numero_compte = $accountancycode;
692 $accline->num_releve = $num_releve;
693 $accline->note_private = $note_private;
694
695 if ($num_chq) {
696 $accline->num_chq = $num_chq;
697 }
698
699 if ($emetteur) {
700 $accline->emetteur = $emetteur;
701 }
702
703 if ($banque) {
704 $accline->bank_chq = $banque;
705 }
706
707 if ($accline->insert() > 0) {
708 if ($categorie > 0) {
709 $sql = "INSERT INTO ".MAIN_DB_PREFIX."category_bankline(";
710 $sql .= "lineid, fk_categ";
711 $sql .= ") VALUES (";
712 $sql .= ((int) $accline->id).", ".((int) $categorie);
713 $sql .= ")";
714
715 $result = $this->db->query($sql);
716 if (!$result) {
717 $this->error = $this->db->lasterror();
718 $this->db->rollback();
719
720 return -4;
721 }
722 }
723
724 $this->db->commit();
725
726 return $accline->id;
727 } else {
728 $this->setErrorsFromObject($accline);
729 $this->db->rollback();
730
731 return -5;
732 }
733 }
734
742 public function create($user, $notrigger = 0)
743 {
744 global $langs, $conf;
745
746 $error = 0;
747
748 // Clean parameters
749 if (!$this->min_allowed) {
750 $this->min_allowed = 0;
751 }
752 if (!$this->min_desired) {
753 $this->min_desired = 0;
754 }
755
756 // Check parameters
757 if (empty($this->country_id)) {
758 $langs->load('errors');
759 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
760 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
761 return -1;
762 }
763 if (empty($this->ref)) {
764 $langs->load('errors');
765 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
766 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
767 return -1;
768 }
769 if (empty($this->date_solde)) {
770 $langs->load('errors');
771 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance"));
772 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
773 return -1;
774 }
775
776 // Load libraries to check BAN
777 $balance = $this->balance;
778 if (empty($balance) && !empty($this->solde)) {
779 dol_syslog(get_class($this)."::create solde is deprecated use balance", LOG_NOTICE);
780 $balance = $this->solde;
781 }
782 if (empty($balance)) {
783 $balance = 0;
784 }
785 if (empty($this->status && !empty($this->clos))) {
786 dol_syslog(get_class($this)."::create clos is deprecated use status", LOG_NOTICE);
787 $this->status = $this->clos;
788 }
789
790 if (empty($this->country_id) && !empty($this->country_code)) {
791 $country_id = getCountry($this->country_code, '3');
792 $this->country_id = is_int($country_id) ? $country_id : 0;
793 }
794
795 // Load the library to validate/check a BAN account
796 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
797
798 $now = dol_now();
799
800 $this->db->begin();
801
802 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
803 $sql .= "datec";
804 $sql .= ", ref";
805 $sql .= ", label";
806 $sql .= ", entity";
807 $sql .= ", account_number";
808 $sql .= ", fk_accountancy_journal";
809 $sql .= ", bank";
810 $sql .= ", code_banque";
811 $sql .= ", code_guichet";
812 $sql .= ", number";
813 $sql .= ", cle_rib";
814 $sql .= ", bic";
815 $sql .= ", iban_prefix";
816 $sql .= ", domiciliation";
817 $sql .= ", pti_in_ctti";
818 $sql .= ", proprio";
819 $sql .= ", owner_address";
820 $sql .= ", owner_zip";
821 $sql .= ", owner_town";
822 $sql .= ", owner_country_id";
823 $sql .= ", currency_code";
824 $sql .= ", rappro";
825 $sql .= ", min_allowed";
826 $sql .= ", min_desired";
827 $sql .= ", comment";
828 $sql .= ", state_id";
829 $sql .= ", fk_pays";
830 $sql .= ", ics";
831 $sql .= ", ics_transfer";
832 $sql .= ") VALUES (";
833 $sql .= "'".$this->db->idate($now)."'";
834 $sql .= ", '".$this->db->escape($this->ref)."'";
835 $sql .= ", '".$this->db->escape($this->label)."'";
836 $sql .= ", ".((int) $conf->entity);
837 $sql .= ", '".$this->db->escape($this->account_number)."'";
838 $sql .= ", ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
839 $sql .= ", '".$this->db->escape($this->bank)."'";
840 $sql .= ", '".$this->db->escape($this->code_banque)."'";
841 $sql .= ", '".$this->db->escape($this->code_guichet)."'";
842 $sql .= ", '".$this->db->escape($this->number)."'";
843 $sql .= ", '".$this->db->escape($this->cle_rib)."'";
844 $sql .= ", '".$this->db->escape($this->bic)."'";
845 $sql .= ", '".$this->db->escape($this->iban)."'";
846 $sql .= ", '".$this->db->escape($this->address)."'";
847 $sql .= ", ".((int) $this->pti_in_ctti);
848 $sql .= ", '".$this->db->escape($this->owner_name)."'";
849 $sql .= ", '".$this->db->escape($this->owner_address)."'";
850 $sql .= ", '".$this->db->escape($this->owner_zip)."'";
851 $sql .= ", '".$this->db->escape($this->owner_town)."'";
852 $sql .= ", ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
853 $sql .= ", '".$this->db->escape($this->currency_code)."'";
854 $sql .= ", ".((int) $this->rappro);
855 $sql .= ", ".price2num($this->min_allowed, 'MT');
856 $sql .= ", ".price2num($this->min_desired, 'MT');
857 $sql .= ", '".$this->db->escape($this->comment)."'";
858 $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
859 $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
860 $sql .= ", '".$this->db->escape($this->ics)."'";
861 $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
862 $sql .= ")";
863
864 dol_syslog(get_class($this)."::create", LOG_DEBUG);
865 $resql = $this->db->query($sql);
866 if ($resql) {
867 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
868
869 $result = $this->update($user, 1);
870 if ($result > 0) {
871 $accline = new AccountLine($this->db);
872 $accline->datec = $now;
873 $accline->label = '('.$langs->trans("InitialBankBalance").')';
874 $accline->amount = (float) price2num($balance);
875 $accline->fk_user_author = $user->id;
876 $accline->fk_account = $this->id;
877 $accline->datev = $this->date_solde;
878 $accline->dateo = $this->date_solde;
879 $accline->fk_type = 'SOLD';
880
881 if ($accline->insert() < 0) {
882 $error++;
883 $this->setErrorsFromObject($accline);
884 }
885
886 if (!$error) {
887 $result = $this->insertExtraFields();
888 if ($result < 0) {
889 $error++;
890 }
891 }
892
893 if (!$error && !$notrigger) {
894 // Call trigger
895 $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
896 if ($result < 0) {
897 $error++;
898 }
899 // End call triggers
900 }
901 } else {
902 $error++;
903 }
904 } else {
905 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
906 $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
907 $error++;
908 } else {
909 $this->error = $this->db->error()." sql=".$sql;
910 $error++;
911 }
912 }
913
914 if (!$error) {
915 $this->db->commit();
916 return $this->id;
917 } else {
918 $this->db->rollback();
919 return -1 * $error;
920 }
921 }
922
930 public function update($user, $notrigger = 0)
931 {
932 global $langs, $conf;
933
934 if (empty($this->country_id) && !empty($this->country_code)) {
935 $country_id = getCountry($this->country_code, '3');
936 $this->country_id = is_int($country_id) ? $country_id : 0;
937 }
938
939 $error = 0;
940
941 $this->db->begin();
942
943 // Check parameters
944 if (empty($this->country_id)) {
945 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
946 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
947 return -1;
948 }
949 if (empty($this->ref)) {
950 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
951 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
952 return -1;
953 }
954 if (!$this->label) {
955 $this->label = "???";
956 }
957
958 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET";
959 $sql .= " ref = '".$this->db->escape($this->ref)."'";
960 $sql .= ",label = '".$this->db->escape($this->label)."'";
961 $sql .= ",courant = ".((int) $this->type);
962 $sql .= ",clos = ".((int) $this->status);
963 $sql .= ",rappro = ".((int) $this->rappro);
964 $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
965 $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
966 $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
967 $sql .= ",bank = '".$this->db->escape($this->bank)."'";
968 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
969 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
970 $sql .= ",number='".$this->db->escape($this->number)."'";
971 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
972 $sql .= ",bic='".$this->db->escape($this->bic)."'";
973 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
974 $sql .= ",domiciliation='".$this->db->escape($this->address)."'";
975 $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
976 $sql .= ",proprio = '".$this->db->escape($this->owner_name)."'";
977 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
978 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
979 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
980 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
981
982 $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
983
984 $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
985 $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
986 $sql .= ",comment = '".$this->db->escape($this->comment)."'";
987
988 $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
989 $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
990 $sql .= ",ics = '".$this->db->escape($this->ics)."'";
991 $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
992
993 $sql .= " WHERE rowid = ".((int) $this->id);
994
995 dol_syslog(get_class($this)."::update", LOG_DEBUG);
996 $result = $this->db->query($sql);
997 if ($result) {
998 // Actions on extra fields (by external module or standard code)
999 if (!$error) {
1000 $result = $this->insertExtraFields();
1001 if ($result < 0) {
1002 $error++;
1003 }
1004 }
1005
1006 if (!$error && !empty($this->oldref) && $this->oldref !== $this->ref) {
1007 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'bank/".$this->db->escape($this->ref)."'";
1008 $sql .= " WHERE filepath = 'bank/".$this->db->escape($this->oldref)."' and src_object_type = 'bank_account' and entity = ".((int) $conf->entity);
1009 $resql = $this->db->query($sql);
1010 if (!$resql) {
1011 $error++;
1012 $this->error = $this->db->lasterror();
1013 }
1014
1015 // We rename directory in order not to lose the attachments
1016 $oldref = dol_sanitizeFileName($this->oldref);
1017 $newref = dol_sanitizeFileName($this->ref);
1018 $dirsource = $conf->bank->dir_output.'/'.$oldref;
1019 $dirdest = $conf->bank->dir_output.'/'.$newref;
1020 if (file_exists($dirsource)) {
1021 dol_syslog(get_class($this)."::update rename dir ".$dirsource." into ".$dirdest, LOG_DEBUG);
1022 if (@rename($dirsource, $dirdest)) {
1023 dol_syslog("Rename ok", LOG_DEBUG);
1024 }
1025 }
1026 }
1027
1028 if (!$error && !$notrigger) {
1029 // Call trigger
1030 $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
1031 if ($result < 0) {
1032 $error++;
1033 }
1034 // End call triggers
1035 }
1036 } else {
1037 $error++;
1038 $this->error = $this->db->lasterror();
1039 dol_print_error($this->db);
1040 }
1041
1042 if (!$error) {
1043 $this->db->commit();
1044 return $this->id;
1045 } else {
1046 $this->db->rollback();
1047 return -1 * $error;
1048 }
1049 }
1050
1051
1052 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1059 public function update_bban($user = null)
1060 {
1061 // phpcs:enable
1062 global $conf, $langs;
1063
1064 // Load library to get BAN control function
1065 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1066
1067 dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
1068
1069 // Check parameters
1070 if (!$this->ref) {
1071 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
1072 return -2;
1073 }
1074
1075 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET";
1076 $sql .= " bank = '".$this->db->escape($this->bank)."'";
1077 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
1078 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
1079 $sql .= ",number='".$this->db->escape($this->number)."'";
1080 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
1081 $sql .= ",bic='".$this->db->escape($this->bic)."'";
1082 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
1083 $sql .= ",domiciliation='".$this->db->escape($this->address)."'";
1084 $sql .= ",proprio = '".$this->db->escape($this->owner_name)."'";
1085 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
1086 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
1087 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
1088 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
1089 $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
1090 $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
1091 $sql .= " WHERE rowid = ".((int) $this->id);
1092 $sql .= " AND entity = ".((int) $conf->entity);
1093
1094 dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
1095
1096 $result = $this->db->query($sql);
1097 if ($result) {
1098 return 1;
1099 } else {
1100 $this->error = $this->db->lasterror();
1101 dol_print_error($this->db);
1102 return -1;
1103 }
1104 }
1105
1106
1114 public function fetch($id, $ref = '')
1115 {
1116 if (empty($id) && empty($ref)) {
1117 $this->error = "ErrorBadParameters";
1118 return -1;
1119 }
1120
1121 $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant as type, ba.clos as status, ba.rappro, ba.url, ba.entity,";
1122 $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
1123 $sql .= " ba.domiciliation as address, ba.pti_in_ctti, ba.proprio as owner_name, ba.owner_address, ba.owner_zip, ba.owner_town, ba.owner_country_id, ba.state_id, ba.fk_pays as country_id,";
1124 $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
1125 $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
1126 $sql .= " ba.datec as date_creation, ba.tms as date_modification, ba.ics, ba.ics_transfer,";
1127 $sql .= ' c.code as country_code, c.label as country,';
1128 $sql .= ' d.code_departement as state_code, d.nom as state,';
1129 $sql .= ' aj.code as accountancy_journal';
1130 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1131 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
1132 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
1133 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid = ba.fk_accountancy_journal';
1134 $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
1135 if ($id) {
1136 $sql .= " AND ba.rowid = ".((int) $id);
1137 }
1138 if ($ref) {
1139 $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
1140 }
1141
1142 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1143 $result = $this->db->query($sql);
1144 if ($result) {
1145 if ($this->db->num_rows($result)) {
1146 $obj = $this->db->fetch_object($result);
1147
1148 $this->id = $obj->rowid;
1149 $this->rowid = $obj->rowid;
1150 $this->entity = $obj->entity;
1151 $this->ref = $obj->ref;
1152 $this->label = $obj->label;
1153 $this->type = $obj->type;
1154 $this->courant = $obj->type;
1155 $this->bank = $obj->bank;
1156 $this->clos = $obj->status;
1157 $this->status = $obj->status;
1158 $this->rappro = $obj->rappro;
1159 $this->url = $obj->url;
1160
1161 $this->code_banque = $obj->code_banque;
1162 $this->code_guichet = $obj->code_guichet;
1163 $this->number = $obj->number;
1164 $this->cle_rib = $obj->cle_rib;
1165 $this->bic = $obj->bic;
1166 $this->iban = $obj->iban;
1167 $this->address = $obj->address;
1168
1169 $this->owner_name = $obj->owner_name;
1170 $this->proprio = $this->owner_name;
1171 $this->owner_address = $obj->owner_address;
1172 $this->owner_zip = $obj->owner_zip;
1173 $this->owner_town = $obj->owner_town;
1174 $this->owner_country_id = $obj->owner_country_id;
1175
1176 $this->pti_in_ctti = $obj->pti_in_ctti;
1177
1178 $this->state_id = $obj->state_id;
1179 $this->state_code = $obj->state_code;
1180 $this->state = $obj->state;
1181
1182 $this->country_id = $obj->country_id;
1183 $this->country_code = $obj->country_code;
1184 $this->country = $obj->country;
1185
1186 $this->account_number = $obj->account_number;
1187 $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
1188 $this->accountancy_journal = $obj->accountancy_journal;
1189
1190 $this->currency_code = $obj->currency_code;
1191 $this->account_currency_code = $obj->currency_code;
1192 $this->min_allowed = $obj->min_allowed;
1193 $this->min_desired = $obj->min_desired;
1194 $this->comment = $obj->comment;
1195
1196 $this->date_creation = $this->db->jdate($obj->date_creation);
1197 $this->date_modification = $this->db->jdate($obj->date_modification);
1198
1199 $this->ics = $obj->ics;
1200 $this->ics_transfer = $obj->ics_transfer;
1201
1202 // Retrieve all extrafield
1203 // fetch optionals attributes and labels
1204 $this->fetch_optionals();
1205
1206 return 1;
1207 } else {
1208 return 0;
1209 }
1210 } else {
1211 $this->error = $this->db->lasterror();
1212 $this->errors[] = $this->error;
1213 return -1;
1214 }
1215 }
1216
1227 public function setCategories($categories)
1228 {
1229 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1230 return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
1231 }
1232
1240 public function delete($user, $notrigger = 0)
1241 {
1242 $error = 0;
1243
1244 $this->db->begin();
1245
1246 // @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
1247
1248 // Delete link between tag and bank account
1249 if (!$error) {
1250 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1251 $sql .= " WHERE fk_account = ".((int) $this->id);
1252
1253 $resql = $this->db->query($sql);
1254 if (!$resql) {
1255 $error++;
1256 $this->error = "Error ".$this->db->lasterror();
1257 }
1258 }
1259
1260 if (!$error) {
1261 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
1262 $sql .= " WHERE rowid = ".((int) $this->id);
1263
1264 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1265 $result = $this->db->query($sql);
1266 if ($result) {
1267 // Remove extrafields
1268 if (!$error) {
1269 $result = $this->deleteExtraFields();
1270 if ($result < 0) {
1271 $error++;
1272 dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1273 }
1274 }
1275 } else {
1276 $error++;
1277 $this->error = "Error ".$this->db->lasterror();
1278 }
1279 }
1280
1281 if (!$error) {
1282 $this->db->commit();
1283 return 1;
1284 } else {
1285 $this->db->rollback();
1286 return -1;
1287 }
1288 }
1289
1290
1297 public function getLibStatut($mode = 0)
1298 {
1299 return $this->LibStatut($this->status, $mode);
1300 }
1301
1302 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1310 public function LibStatut($status, $mode = 0)
1311 {
1312 // phpcs:enable
1313 global $langs;
1314 $langs->load('banks');
1315
1316 if ($status == self::STATUS_OPEN) {
1317 $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1318 $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1319 $statusType = 'status4';
1320 } else {
1321 $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1322 $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1323 $statusType = 'status5';
1324 }
1325
1326 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1327 }
1328
1329
1330 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1336 public function can_be_deleted()
1337 {
1338 // phpcs:enable
1339 $can_be_deleted = false;
1340
1341 $sql = "SELECT COUNT(rowid) as nb";
1342 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1343 $sql .= " WHERE fk_account = ".((int) $this->id);
1344
1345 $resql = $this->db->query($sql);
1346 if ($resql) {
1347 $obj = $this->db->fetch_object($resql);
1348 if ($obj->nb <= 1) {
1349 $can_be_deleted = true; // Juste le solde
1350 }
1351 } else {
1352 dol_print_error($this->db);
1353 }
1354 return $can_be_deleted;
1355 }
1356
1357
1363 public function error()
1364 {
1365 return $this->error;
1366 }
1367
1376 public function solde($option = 0, $date_end = '', $field = 'dateo')
1377 {
1378 $solde = 0;
1379
1380 $sql = "SELECT sum(amount) as amount";
1381 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1382 $sql .= " WHERE fk_account = ".((int) $this->id);
1383 if ($option == 1) {
1384 $sql .= " AND ".$this->db->sanitize($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1385 }
1386
1387 $resql = $this->db->query($sql);
1388 if ($resql) {
1389 if ($this->db->num_rows($resql)) {
1390 $obj = $this->db->fetch_object($resql);
1391 $solde = $obj->amount;
1392 }
1393 $this->db->free($resql);
1394 } else {
1395 $this->errors[] = $this->db->lasterror;
1396 return -1;
1397 }
1398
1399 return (float) price2num($solde, 'MU');
1400 }
1401
1402 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1410 public function load_board(User $user, $filteraccountid = 0)
1411 {
1412 // phpcs:enable
1413 global $conf, $langs;
1414
1415 if ($user->socid) {
1416 return -1; // Protection to prevent calls by external users
1417 }
1418
1419 $sql = "SELECT b.rowid, b.datev as datefin";
1420 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1421 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1422 $sql .= " WHERE b.rappro=0";
1423 $sql .= " AND b.fk_account = ba.rowid";
1424 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1425 $sql .= " AND (ba.rappro = 1 AND ba.courant != " . Account::TYPE_CASH . ")"; // Compte rapprochable
1426 $sql .= " AND clos = 0";
1427 if ($filteraccountid) {
1428 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1429 }
1430 $resql = $this->db->query($sql);
1431 if ($resql) {
1432 $langs->load("banks");
1433 $now = dol_now();
1434
1435 require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1436
1437 $response = new WorkboardResponse();
1438 $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1439 $response->label = $langs->trans("TransactionsToConciliate");
1440 $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1441 $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1442 $response->img = img_object('', "payment");
1443
1444 while ($obj = $this->db->fetch_object($resql)) {
1445 $response->nbtodo++;
1446 if ((int) $this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1447 $response->nbtodolate++;
1448 }
1449 }
1450 return $response;
1451 } else {
1452 dol_print_error($this->db);
1453 $this->error = $this->db->error();
1454 return -1;
1455 }
1456 }
1457
1464 public function loadStateBoard($filteraccountid = 0)
1465 {
1466 global $user;
1467
1468 if ($user->socid) {
1469 return -1; // Protection to prevent calls by external users
1470 }
1471
1472 $sql = "SELECT count(b.rowid) as nb";
1473 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1474 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1475 $sql .= " WHERE b.fk_account = ba.rowid";
1476 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1477 $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1478 $sql .= " AND clos = 0";
1479 if ($filteraccountid) {
1480 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1481 }
1482
1483 $resql = $this->db->query($sql);
1484 if ($resql) {
1485 while ($obj = $this->db->fetch_object($resql)) {
1486 $this->nb["banklines"] = $obj->nb;
1487 }
1488 $this->db->free($resql);
1489 return 1;
1490 } else {
1491 dol_print_error($this->db);
1492 $this->error = $this->db->error();
1493 return -1;
1494 }
1495 }
1496
1497
1503 public function countAccountToReconcile()
1504 {
1505 global $user;
1506
1507 //Protection against external users
1508 if ($user->socid) {
1509 return 0;
1510 }
1511
1512 $nb = 0;
1513
1514 $sql = "SELECT COUNT(ba.rowid) as nb";
1515 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1516 $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1517 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1518 if (!getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
1519 $sql .= " AND ba.courant != " . Account::TYPE_CASH;
1520 }
1521
1522 $resql = $this->db->query($sql);
1523 if ($resql) {
1524 $obj = $this->db->fetch_object($resql);
1525 $nb = (int) $obj->nb;
1526 } else {
1527 dol_print_error($this->db);
1528 }
1529
1530 return $nb;
1531 }
1532
1541 public function getTooltipContentArray($params)
1542 {
1543 global $langs;
1544 $langs->loadLangs(['banks', 'compta']);
1545 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1546
1547 $datas = array();
1548
1549 $nofetch = !empty($params['nofetch']);
1550 $pictos = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
1551 if (isset($this->status)) {
1552 $pictos .= ' '.$this->getLibStatut(5);
1553 }
1554 $datas['picto'] = $pictos;
1555 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1556 $datas['label'] = '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1557 $datas['accountnumber'] = '<br><br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
1558 $datas['iban'] = '<br><b>'.$langs->trans('IBAN').':</b> '.getIbanHumanReadable($this);
1559 $datas['bic'] = '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
1560 $datas['accountcurrency'] = '<br><br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
1561
1562 if (isModEnabled('accounting')) {
1563 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1564 $langs->load("accountancy");
1565 $datas['accountaccounting'] = '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
1566 $datas['accountancyjournal'] = '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
1567 }
1568 // show categories for this record only in ajax to not overload lists
1569 if (isModEnabled('category') && !$nofetch) {
1570 require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
1571 $form = new Form($this->db);
1572 $datas['categories'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_ACCOUNT, 1);
1573 }
1574
1575 return $datas;
1576 }
1577
1589 public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1590 {
1591 global $conf, $langs;
1592
1593 if (!empty($conf->dol_no_mouse_hover)) {
1594 $notooltip = 1; // Force disable tooltips
1595 }
1596
1597 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1598
1599 $result = '';
1600 $classfortooltip = 'classfortooltip';
1601 $dataparams = '';
1602 $params = [
1603 'id' => $this->id,
1604 'objecttype' => $this->element,
1605 'option' => $option,
1606 'nofetch' => 1,
1607 ];
1608 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1609 $classfortooltip = 'classforajaxtooltip';
1610 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1611 $label = '';
1612 } else {
1613 $label = implode($this->getTooltipContentArray($params));
1614 }
1615
1616 $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1617 if ($mode == 'transactions') {
1618 $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1619 } elseif ($mode == 'receipts') {
1620 $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1621 }
1622
1623 if ($option != 'nolink') {
1624 // Add param to save lastsearch_values or not
1625 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1626 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1627 $add_save_lastsearch_values = 1;
1628 }
1629 if ($add_save_lastsearch_values) {
1630 $url .= '&save_lastsearch_values=1';
1631 }
1632 }
1633
1634 $linkclose = '';
1635 if (empty($notooltip)) {
1636 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1637 $label = $langs->trans("BankAccount");
1638 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1639 }
1640 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1641 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1642 } else {
1643 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1644 }
1645
1646 if ($option == 'nolink' || empty($url)) {
1647 $linkstart = '<span';
1648 } else {
1649 $linkstart = '<a href="'.$url.'"';
1650 }
1651 $linkstart .= $linkclose.'>';
1652 if ($option == 'nolink' || empty($url)) {
1653 $linkend = '</span>';
1654 } else {
1655 $linkend = '</a>';
1656 }
1657
1658 $result .= $linkstart;
1659
1660 if ($withpicto) {
1661 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1662 }
1663 if ($withpicto != 2) {
1664 $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1665 }
1666 $result .= $linkend;
1667
1668 return $result;
1669 }
1670
1671
1672 // Method after here are common to Account and CompanyBankAccount
1673
1674
1680 public function verif()
1681 {
1682 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1683
1684 $error = 0;
1685
1686 // Call functions to check BAN
1687 if (!checkIbanForAccount($this)) {
1688 $error++;
1689 $this->error = 'IBANNotValid';
1690 }
1691 // Call function to check Swift/BIC.
1692 // A non valid BIC/Swift is a problem if: it is not empty or always a problem if WITHDRAWAL_WITHOUT_BIC is not set).
1693 if (!checkSwiftForAccount($this) && (!empty($this->bic) || !getDolGlobalInt('WITHDRAWAL_WITHOUT_BIC'))) {
1694 $error++;
1695 $this->error = 'SwiftNotValid';
1696 }
1697
1698 if (! $error) {
1699 return 1;
1700 } else {
1701 return 0;
1702 }
1703 }
1704
1710 public function getCountryCode()
1711 {
1712 global $mysoc;
1713
1714 // We return country code of bank account
1715 if (!empty($this->country_code)) {
1716 return $this->country_code;
1717 }
1718
1719 // For backward compatibility, we try to guess country from other information
1720 if (!empty($this->iban)) {
1721 // If IBAN defined, we can know country of account from it
1722 $reg = array();
1723 if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
1724 return $reg[1];
1725 }
1726 }
1727
1728 // If this class is linked to a third party
1729 if (!empty($this->socid)) {
1730 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
1731 $company = new Societe($this->db);
1732 $result = $company->fetch($this->socid);
1733 if (!empty($company->country_code)) {
1734 return $company->country_code;
1735 }
1736 }
1737
1738 // We return country code of managed company
1739 if (!empty($mysoc->country_code)) {
1740 return $mysoc->country_code;
1741 }
1742
1743 return '';
1744 }
1745
1753 public function getBannerAddress($htmlkey, $object)
1754 {
1755 global $conf, $langs;
1756
1757 $out = '';
1758
1759 $outdone = 0;
1760 $coords = $this->getFullAddress(1, ', ', getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT'));
1761 if ($coords) {
1762 if (!empty($conf->use_javascript_ajax)) {
1763 // hideonsmatphone because copyToClipboard call jquery dialog that does not work with jmobile
1764 $out .= '<a href="#" class="hideonsmartphone" onclick="return copyToClipboard(\''.dol_escape_js($coords).'\',\''.dol_escape_js($langs->trans("HelpCopyToClipboard")).'\');">';
1765 $out .= img_picto($langs->trans("Address"), 'map-marker-alt');
1766 $out .= '</a> ';
1767 }
1768 $address = dol_print_address($coords, 'address_'.$htmlkey.'_'.$this->id, $this->element, $this->id, 1, ', ');
1769 if ($address) {
1770 $out .= $address;
1771 $outdone++;
1772 }
1773 $outdone++;
1774 }
1775
1776 return $out;
1777 }
1778
1785 public function getBankFieldLabel($field)
1786 {
1787 $country_code = $this->getCountryCode();
1788
1789 if ($country_code == 'CA' && $field == 'BankCode') {
1790 return 'InstitutionNumber';
1791 }
1792 if ($country_code == 'CA' && $field == 'DeskCode') {
1793 return 'TransitNumber';
1794 }
1795 if ($country_code == 'US' && $field == 'BankCode') {
1796 return 'ABARoutingNumber';
1797 }
1798
1799 return $field;
1800 }
1801
1811 public function useDetailedBBAN()
1812 {
1813 $country_code = $this->getCountryCode();
1814
1815 if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
1816 return 1; // France, Spain, Gabon, ... - Not valid for CH
1817 }
1818 if ($country_code == 'CA') {
1819 return 3;
1820 }
1821 if (in_array($country_code, array('AD', 'AU', 'BE', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
1822 return 2; // Australia, England...
1823 }
1824 return 0;
1825 }
1826
1832 public function needIBAN()
1833 {
1834 global $conf;
1835
1836 if (getDolGlobalString('MAIN_IBAN_IS_NEVER_MANDATORY')) {
1837 return 0;
1838 }
1839
1840 $country_code = $this->getCountryCode();
1841
1842 $country_code_in_EEC = array(
1843 'AT', // Austria
1844 'BE', // Belgium
1845 'BG', // Bulgaria
1846 'CY', // Cyprus
1847 'CZ', // Czech republic
1848 'DE', // Germany
1849 'DK', // Danemark
1850 'EE', // Estonia
1851 'ES', // Spain
1852 'FI', // Finland
1853 'FR', // France
1854 'GB', // United Kingdom
1855 'GR', // Greece
1856 'HR', // Croatia
1857 'NL', // Holland
1858 'HU', // Hungary
1859 'IE', // Ireland
1860 'IM', // Isle of Man - Included in UK
1861 'IT', // Italy
1862 'LT', // Lithuania
1863 'LU', // Luxembourg
1864 'LV', // Latvia
1865 'MC', // Monaco - Included in France
1866 'MT', // Malta
1867 //'NO', // Norway
1868 'PL', // Poland
1869 'PT', // Portugal
1870 'RO', // Romania
1871 'SE', // Sweden
1872 'SK', // Slovakia
1873 'SI', // Slovenia
1874 'UK', // United Kingdom
1875 //'CH', // Switzerland - No. Swizerland in not in EEC
1876 );
1877
1878 if (in_array($country_code, $country_code_in_EEC)) {
1879 return 1; // France, Spain, ...
1880 }
1881 return 0;
1882 }
1883
1889 public function needBIC()
1890 {
1891 if (getDolGlobalString('MAIN_IBAN_IS_NEVER_MANDATORY')) {
1892 return 0;
1893 }
1894
1895 $country_code = $this->getCountryCode();
1896
1897 $country_code_in_EEC = array(
1898 'AD', // Andorra
1899 'BH', // Bahrein
1900 'DK', // Denmark
1901 'FR', // France
1902 'GH', // Ghana
1903 'HU', // Hungary
1904 'JP', // Japan
1905 'LV', // Latvia
1906 'SE', // Sweden
1907 );
1908
1909 if (in_array($country_code, $country_code_in_EEC)) {
1910 return 1; // Andorra, Bahrein, ...
1911 }
1912 return 0;
1913 }
1914
1921 public function info($id)
1922 {
1923 }
1924
1939 public function getFieldsToShow($includeibanbic = 0)
1940 {
1941 //Get the required properties depending on the country
1942 $detailedBBAN = $this->useDetailedBBAN();
1943
1944 if ($detailedBBAN == 0) {
1945 $fieldarray = array(
1946 'BankAccountNumber'
1947 );
1948 } elseif ($detailedBBAN == 2) {
1949 $fieldarray = array(
1950 'BankCode',
1951 'BankAccountNumber'
1952 );
1953 } elseif ($detailedBBAN == 3) {
1954 $fieldarray = array(
1955 'BankCode',
1956 'DeskCode',
1957 'BankAccountNumber'
1958 );
1959 } else {
1960 $fieldarray = self::getAccountNumberOrder();
1961 }
1962
1963 //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1964 if ($includeibanbic) {
1965 $fieldarray[] = 'IBAN';
1966 $fieldarray[] = 'BIC';
1967 }
1968 //}
1969
1970 //Get the order the properties are shown
1971 return $fieldarray;
1972 }
1973
1984 public static function getAccountNumberOrder()
1985 {
1986 global $conf;
1987
1988 $fieldlists = array(
1989 'BankCode',
1990 'DeskCode',
1991 'BankAccountNumber',
1992 'BankAccountNumberKey'
1993 );
1994
1995 if (getDolGlobalString('BANK_SHOW_ORDER_OPTION')) {
1996 if (is_numeric(getDolGlobalString('BANK_SHOW_ORDER_OPTION'))) {
1997 if (getDolGlobalString('BANK_SHOW_ORDER_OPTION') == '1') {
1998 $fieldlists = array(
1999 'BankCode',
2000 'DeskCode',
2001 'BankAccountNumberKey',
2002 'BankAccountNumber'
2003 );
2004 }
2005 } else {
2006 //Replace the old AccountNumber key with the new BankAccountNumber key
2007 $fieldlists = explode(
2008 ' ',
2009 preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
2010 );
2011 }
2012 }
2013
2014 return $fieldlists;
2015 }
2016
2017
2025 public function initAsSpecimen()
2026 {
2027 // Example of IBAN FR7630001007941234567890185
2028 $this->specimen = 1;
2029 $this->ref = 'MBA';
2030 $this->label = 'My Big Company Bank account';
2031 $this->clos = Account::STATUS_OPEN;
2032 $this->type = Account::TYPE_CURRENT;
2033 $this->status = Account::STATUS_OPEN;
2034 $this->code_banque = '30001';
2035 $this->code_guichet = '00794';
2036 $this->number = '12345678901';
2037 $this->cle_rib = '85';
2038 $this->bic = 'AA12';
2039 $this->iban = 'FR7630001007941234567890185';
2040
2041 $this->bank = 'MyBank';
2042 $this->address = 'Rue de Paris';
2043 $this->owner_name = 'Owner';
2044 $this->owner_address = 'Owner address';
2045 $this->owner_zip = 'Owner zip';
2046 $this->owner_town = 'Owner town';
2047 $this->owner_country_id = 1;
2048 $this->country_id = 1;
2049
2050 return 1;
2051 }
2052
2061 public static function replaceThirdparty($dbs, $origin_id, $dest_id)
2062 {
2063 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type = 'company'";
2064
2065 if ($dbs->query($sql)) {
2066 return true;
2067 } else {
2068 //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.
2069 //$this->errors = $dbs->lasterror();
2070 return false;
2071 }
2072 }
2073
2081 public function getKanbanView($option = '', $arraydata = null)
2082 {
2083 global $langs;
2084
2085 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2086
2087 $return = '<div class="box-flex-item box-flex-grow-zero">';
2088 $return .= '<div class="info-box info-box-sm">';
2089 $return .= '<span class="info-box-icon bg-infobox-action">';
2090 $return .= img_picto('', $this->picto);
2091 $return .= '</span>';
2092 $return .= '<div class="info-box-content">';
2093 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
2094 if ($selected >= 0) {
2095 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2096 }
2097 if (property_exists($this, 'type_lib')) {
2098 $return .= '<br><span class="info-box-label opacitymedium" title="'.$this->type_lib[$this->type].'">'.substr($this->type_lib[$this->type], 0, 24).'...</span>';
2099 }
2100 if (method_exists($this, 'solde')) {
2101 $return .= '<br><a href="'.DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id.'">';
2102 $return .= '<span class="opacitymedium">'.$langs->trans("Balance").'</span> : <span class="amount">'.price(price2num($this->solde(1), 'MT'), 0, $langs, 1, -1, -1, $this->currency_code).'</span>';
2103 }
2104 if (method_exists($this, 'getLibStatut')) {
2105 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2106 }
2107 $return .= '</div>';
2108 $return .= '</div>';
2109 $return .= '</div>';
2110 return $return;
2111 }
2112}
2113
2114
2115require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
2116
2120class AccountLine extends CommonObjectLine
2121{
2125 public $db;
2126
2130 public $element = 'bank';
2131
2135 public $table_element = 'bank';
2136
2140 public $picto = 'accountline';
2141
2145 public $id;
2146
2150 public $ref;
2151
2157 public $datec;
2158
2164 public $dateo;
2165
2171 public $datev;
2172
2176 public $amount;
2177
2181 public $amount_main_currency;
2182
2186 public $fk_user_author;
2187
2191 public $fk_user_rappro;
2192
2196 public $fk_type;
2197
2201 public $fk_bordereau;
2202
2206 public $fk_account;
2207
2211 public $bank_account_ref;
2212
2216 public $bank_account_label;
2217
2221 public $numero_compte;
2222
2226 public $emetteur;
2227
2231 public $rappro;
2232
2236 public $num_releve;
2237
2241 public $num_chq;
2242
2246 public $bank_chq;
2247
2251 public $label;
2252
2256 public $note;
2257
2263 public $user_rappro;
2264
2265
2271 public function __construct(DoliDB $db)
2272 {
2273 $this->db = $db;
2274 }
2275
2284 public function fetch($rowid, $ref = '', $num = '')
2285 {
2286 // Check parameters
2287 if (empty($rowid) && empty($ref) && empty($num)) {
2288 return -1;
2289 }
2290
2291 $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
2292 $sql .= " b.fk_user_author, b.fk_user_rappro,";
2293 $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
2294 $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
2295 $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
2296 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
2297 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
2298 $sql .= " WHERE b.fk_account = ba.rowid";
2299 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
2300 if ($num) {
2301 $sql .= " AND b.num_chq = '".$this->db->escape($num)."'";
2302 } elseif ($ref) {
2303 $sql .= " AND b.rowid = '".$this->db->escape($ref)."'";
2304 } else {
2305 $sql .= " AND b.rowid = ".((int) $rowid);
2306 }
2307
2308 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
2309 $result = $this->db->query($sql);
2310 if ($result) {
2311 $ret = 0;
2312
2313 $obj = $this->db->fetch_object($result);
2314 if ($obj) {
2315 $this->id = $obj->rowid;
2316 $this->rowid = $obj->rowid;
2317 $this->ref = $obj->rowid;
2318
2319 $this->datec = $this->db->jdate($obj->datec);
2320 $this->datev = $this->db->jdate($obj->datev);
2321 $this->dateo = $this->db->jdate($obj->dateo);
2322 $this->amount = $obj->amount;
2323 $this->label = $obj->label;
2324 $this->note = $obj->note;
2325
2326 $this->fk_user_author = $obj->fk_user_author;
2327 $this->fk_user_rappro = $obj->fk_user_rappro;
2328
2329 $this->fk_type = $obj->fk_type; // Type of transaction
2330 $this->rappro = (int) $obj->rappro;
2331 $this->num_releve = $obj->num_releve;
2332
2333 $this->num_chq = $obj->num_chq;
2334 $this->bank_chq = $obj->banque;
2335 $this->fk_bordereau = $obj->fk_bordereau;
2336
2337 $this->fk_account = $obj->fk_account;
2338 $this->bank_account_ref = $obj->bank_account_ref;
2339 $this->bank_account_label = $obj->bank_account_label;
2340
2341 // Retrieve all extrafield
2342 // fetch optionals attributes and labels
2343 $this->fetch_optionals();
2344
2345 $ret = 1;
2346 }
2347 $this->db->free($result);
2348 return $ret;
2349 } else {
2350 return -1;
2351 }
2352 }
2353
2359 public function insert()
2360 {
2361 $error = 0;
2362
2363 $this->db->begin();
2364
2365 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
2366 $sql .= "datec";
2367 $sql .= ", dateo";
2368 $sql .= ", datev";
2369 $sql .= ", label";
2370 $sql .= ", amount";
2371 $sql .= ", amount_main_currency";
2372 $sql .= ", fk_user_author";
2373 $sql .= ", num_chq";
2374 $sql .= ", fk_account";
2375 $sql .= ", fk_type";
2376 $sql .= ", emetteur,banque";
2377 $sql .= ", rappro";
2378 $sql .= ", numero_compte";
2379 $sql .= ", num_releve";
2380 $sql .= ", note";
2381 $sql .= ") VALUES (";
2382 $sql .= "'".$this->db->idate($this->datec)."'";
2383 $sql .= ", '".$this->db->idate($this->dateo)."'";
2384 $sql .= ", '".$this->db->idate($this->datev)."'";
2385 $sql .= ", '".$this->db->escape($this->label)."'";
2386 $sql .= ", ".price2num($this->amount);
2387 $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
2388 $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
2389 $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
2390 $sql .= ", '".$this->db->escape((string) $this->fk_account)."'";
2391 $sql .= ", '".$this->db->escape($this->fk_type)."'";
2392 $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
2393 $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
2394 $sql .= ", ".(int) $this->rappro;
2395 $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2396 $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2397 $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
2398 $sql .= ")";
2399
2400
2401 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2402 $resql = $this->db->query($sql);
2403 if ($resql) {
2404 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2405 // Actions on extra fields (by external module or standard code)
2406 $result = $this->insertExtraFields();
2407 if ($result < 0) {
2408 $error++;
2409 }
2410 } else {
2411 $error++;
2412 $this->error = $this->db->lasterror();
2413 dol_print_error($this->db);
2414 }
2415
2416 if (!$error) {
2417 $this->db->commit();
2418 return $this->id;
2419 } else {
2420 $this->db->rollback();
2421 return -1 * $error;
2422 }
2423 }
2424
2432 public function delete($user, $notrigger = 0)
2433 {
2434 $nbko = 0;
2435
2436 if ($this->rappro) {
2437 // Protection to avoid any delete of consolidated lines
2438 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2439 return -1;
2440 }
2441
2442 $this->db->begin();
2443
2444 if (!$notrigger) {
2445 // Call trigger
2446 $result = $this->call_trigger('BANKACCOUNTLINE_DELETE', $user);
2447 if ($result < 0) {
2448 $this->db->rollback();
2449 return -1;
2450 }
2451 // End call triggers
2452 }
2453
2454 // Protection to avoid any delete of accounted lines. Protection on by default
2455 if (!getDolGlobalString('BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING')) {
2456 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2457 $resql = $this->db->query($sql);
2458 if ($resql) {
2459 $obj = $this->db->fetch_object($resql);
2460 if ($obj && $obj->nb) {
2461 $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2462 $this->db->rollback();
2463 return -1;
2464 }
2465 } else {
2466 $this->error = $this->db->lasterror();
2467 $this->db->rollback();
2468 return -1;
2469 }
2470 }
2471
2472 // Delete urls
2473 $result = $this->delete_urls($user);
2474 if ($result < 0) {
2475 $nbko++;
2476 }
2477
2478 $sql = "DELETE FROM ".MAIN_DB_PREFIX."category_bankline WHERE lineid=".(int) $this->rowid;
2479 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2480 $result = $this->db->query($sql);
2481 if (!$result) {
2482 $nbko++;
2483 }
2484
2485 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_extrafields WHERE fk_object=".(int) $this->rowid;
2486 $result = $this->db->query($sql);
2487 if (!$result) {
2488 $nbko++;
2489 }
2490
2491 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2492 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2493 $result = $this->db->query($sql);
2494 if (!$result) {
2495 $nbko++;
2496 }
2497
2498 if (!$nbko) {
2499 $this->db->commit();
2500 return 1;
2501 } else {
2502 $this->db->rollback();
2503 return -$nbko;
2504 }
2505 }
2506
2507
2508 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2515 public function delete_urls($user = null)
2516 {
2517 // phpcs:enable
2518 $nbko = 0;
2519
2520 if ($this->rappro) {
2521 // Protection to avoid any delete of consolidated lines
2522 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2523 return -1;
2524 }
2525
2526 $this->db->begin();
2527
2528 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2529 dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
2530 $result = $this->db->query($sql);
2531 if (!$result) {
2532 $nbko++;
2533 }
2534
2535 if (!$nbko) {
2536 $this->db->commit();
2537 return 1;
2538 } else {
2539 $this->db->rollback();
2540 return -$nbko;
2541 }
2542 }
2543
2544
2552 public function update(User $user, $notrigger = 0)
2553 {
2554 $this->db->begin();
2555
2556 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2557 $sql .= " amount = ".price2num($this->amount).",";
2558 $sql .= " datev='".$this->db->idate($this->datev)."',";
2559 $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2560 $sql .= " WHERE rowid = ".((int) $this->rowid);
2561
2562 dol_syslog(get_class($this)."::update", LOG_DEBUG);
2563 $resql = $this->db->query($sql);
2564 if ($resql) {
2565 $this->db->commit();
2566 return 1;
2567 } else {
2568 $this->db->rollback();
2569 $this->error = $this->db->error();
2570 return -1;
2571 }
2572 }
2573
2574
2580 public function updateLabel()
2581 {
2582 $this->db->begin();
2583
2584 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2585 $sql .= " label = '".$this->db->escape($this->label)."'";
2586 $sql .= " WHERE rowid = ".((int) $this->rowid);
2587
2588 dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
2589 $resql = $this->db->query($sql);
2590 if ($resql) {
2591 $this->db->commit();
2592 return 1;
2593 } else {
2594 $this->db->rollback();
2595 $this->error = $this->db->error();
2596 return -1;
2597 }
2598 }
2599
2600
2601 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2610 public function update_conciliation(User $user, $cat, $conciliated = 1)
2611 {
2612 // phpcs:enable
2613 global $conf, $langs;
2614
2615 $this->db->begin();
2616
2617 // Check statement field
2618 if (getDolGlobalString('BANK_STATEMENT_REGEX_RULE')) {
2619 if (!preg_match('/' . getDolGlobalString('BANK_STATEMENT_REGEX_RULE').'/', $this->num_releve)) {
2620 $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", getDolGlobalString('BANK_STATEMENT_REGEX_RULE'));
2621 $this->db->rollback();
2622 return -1;
2623 }
2624 }
2625
2626 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2627 $sql .= " rappro = ".((int) $conciliated);
2628 $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2629 if ($conciliated) {
2630 $sql .= ", fk_user_rappro = ".((int) $user->id);
2631 }
2632 $sql .= " WHERE rowid = ".((int) $this->id);
2633
2634 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2635 $resql = $this->db->query($sql);
2636 if ($resql) {
2637 if (!empty($cat) && $cat > 0) {
2638 $sql = "INSERT INTO ".MAIN_DB_PREFIX."category_bankline (";
2639 $sql .= "lineid";
2640 $sql .= ", fk_categ";
2641 $sql .= ") VALUES (";
2642 $sql .= ((int) $this->id);
2643 $sql .= ", ".((int) $cat);
2644 $sql .= ")";
2645
2646 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2647 $this->db->query($sql);
2648
2649 // No error check. Can fail if category already affected
2650 // TODO Do no try the insert if link already exists
2651 }
2652
2653 $this->rappro = (int) $conciliated;
2654
2655 $this->db->commit();
2656 return 1;
2657 } else {
2658 $this->db->rollback();
2659 return -1;
2660 }
2661 }
2662
2663
2664 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2672 public function datev_change($rowid, $sign = 1)
2673 {
2674 // phpcs:enable
2675 $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2676 $resql = $this->db->query($sql);
2677 if ($resql) {
2678 $obj = $this->db->fetch_object($resql);
2679 $newdate = (int) $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2680
2681 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2682 $sql .= " datev = '".$this->db->idate($newdate)."'";
2683 $sql .= " WHERE rowid = ".((int) $rowid);
2684
2685 $result = $this->db->query($sql);
2686 if ($result) {
2687 if ($this->db->affected_rows($result)) {
2688 return 1;
2689 }
2690 } else {
2691 dol_print_error($this->db);
2692 return 0;
2693 }
2694 } else {
2695 dol_print_error($this->db);
2696 }
2697 return 0;
2698 }
2699
2700 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2707 public function datev_next($id)
2708 {
2709 // phpcs:enable
2710 return $this->datev_change($id, 1);
2711 }
2712
2713 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2720 public function datev_previous($id)
2721 {
2722 // phpcs:enable
2723 return $this->datev_change($id, -1);
2724 }
2725
2726
2727 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2735 public function dateo_change($rowid, $sign = 1)
2736 {
2737 // phpcs:enable
2738 $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2739 $resql = $this->db->query($sql);
2740 if ($resql) {
2741 $obj = $this->db->fetch_object($resql);
2742 $newdate = (int) $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2743
2744 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2745 $sql .= " dateo = '".$this->db->idate($newdate)."'";
2746 $sql .= " WHERE rowid = ".((int) $rowid);
2747
2748 $result = $this->db->query($sql);
2749 if ($result) {
2750 if ($this->db->affected_rows($result)) {
2751 return 1;
2752 }
2753 } else {
2754 dol_print_error($this->db);
2755 return 0;
2756 }
2757 } else {
2758 dol_print_error($this->db);
2759 }
2760 return 0;
2761 }
2762
2763 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2770 public function dateo_next($id)
2771 {
2772 // phpcs:enable
2773 return $this->dateo_change($id, 1);
2774 }
2775
2776 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2783 public function dateo_previous($id)
2784 {
2785 // phpcs:enable
2786 return $this->dateo_change($id, -1);
2787 }
2788
2789
2796 public function info($id)
2797 {
2798 $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2799 $sql .= ' b.fk_user_author, b.fk_user_rappro';
2800 $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2801 $sql .= ' WHERE b.rowid = '.((int) $id);
2802
2803 $result = $this->db->query($sql);
2804 if ($result) {
2805 if ($this->db->num_rows($result)) {
2806 $obj = $this->db->fetch_object($result);
2807
2808 $this->id = $obj->rowid;
2809
2810 $this->user_creation_id = $obj->fk_user_author;
2811 $this->user_rappro = $obj->fk_user_rappro;
2812 $this->date_creation = $this->db->jdate($obj->datec);
2813 $this->date_modification = $this->db->jdate($obj->datem);
2814 //$this->date_rappro = $obj->daterappro; // Not yet managed
2815 }
2816 $this->db->free($result);
2817 } else {
2818 dol_print_error($this->db);
2819 }
2820 }
2821
2822
2832 public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2833 {
2834 global $conf, $langs;
2835
2836 $result = '';
2837
2838 $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2839 $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2840 if ($this->amount) {
2841 $label .= '<br><strong>'.$langs->trans("Amount").':</strong> '.price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
2842 }
2843
2844 $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">';
2845 $linkend = '</a>';
2846
2847 $result .= $linkstart;
2848 if ($withpicto) {
2849 $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);
2850 }
2851 if ($withpicto != 2) {
2852 $result .= ($this->ref ? $this->ref : $this->id);
2853 }
2854
2855 $result .= $linkend;
2856
2857 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2858 $result .= ' <span class="opacitymedium">(';
2859 }
2860 if ($option == 'showall') {
2861 $result .= $langs->trans("BankAccount").': ';
2862 $accountstatic = new Account($this->db);
2863 $accountstatic->id = $this->fk_account;
2864 $accountstatic->ref = $this->bank_account_ref;
2865 $accountstatic->label = $this->bank_account_label;
2866 $result .= $accountstatic->getNomUrl(0).', ';
2867 }
2868 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2869 $result .= $langs->trans("BankLineConciliated").': ';
2870 $result .= yn($this->rappro);
2871 }
2872 if (isModEnabled('accounting') && ($option == 'showall' || $option == 'showconciliatedandaccounted')) {
2873 $sql = "SELECT COUNT(rowid) as nb, MAX(piece_num) as banktransactionid FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
2874 $sql .= " WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2875 $resql = $this->db->query($sql);
2876 if ($resql) {
2877 $obj = $this->db->fetch_object($resql);
2878 if ($obj && $obj->nb) {
2879 $result .= ' - '.$langs->trans("Accounted").': <span title="'.$langs->trans("TransactionNumShort").' '.$obj->banktransactionid.'">'.yn(1).'</span>';
2880 } else {
2881 $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2882 }
2883 }
2884 }
2885 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2886 $result .= ')</span>';
2887 }
2888
2889 return $result;
2890 }
2891
2892
2899 public function getLibStatut($mode = 0)
2900 {
2901 return $this->LibStatut($this->status, $mode);
2902 }
2903
2904 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2912 public function LibStatut($status, $mode = 0)
2913 {
2914 // phpcs:enable
2915 return '';
2916 }
2917
2924 public function getVentilExportCompta($mode = 0)
2925 {
2926 $alreadydispatched = 0;
2927
2928 $type = 'bank';
2929
2930 $sql = " SELECT ".($mode ? 'DISTINCT piece_num' : 'COUNT(ab.rowid)')." as nb";
2931 $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type = '".$this->db->escape($type)."' AND ab.fk_doc = ".((int) $this->id);
2932 $resql = $this->db->query($sql);
2933 if ($resql) {
2934 $obj = $this->db->fetch_object($resql);
2935 if ($obj) {
2936 $alreadydispatched = $obj->nb;
2937 }
2938 } else {
2939 $this->error = $this->db->lasterror();
2940 return -1;
2941 }
2942
2943 if ($alreadydispatched) {
2944 return $alreadydispatched;
2945 }
2946 return 0;
2947 }
2948}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
checkIbanForAccount($account=null, $ibantocheck=null)
Check IBAN number information for a bank account.
Definition bank.lib.php:390
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition bank.lib.php:415
checkSwiftForAccount($account=null, $swift=null)
Check SWIFT information for a bank account.
Definition bank.lib.php:369
$object ref
Definition info.php:90
Class to manage bank accounts.
solde($option=0, $date_end='', $field='dateo')
Return current balance.
fetch($id, $ref='')
Load a bank account into memory from database.
__construct(DoliDB $db)
Constructor.
getNomUrl($withpicto=0, $mode='', $option='', $save_lastsearch_value=-1, $notooltip=0, $morecss='')
Return clickable name (with picto eventually)
add_url_line($line_id, $url_id, $url, $label, $type)
Add a link between bank line record and its source.
loadStateBoard($filteraccountid=0)
Load the indicators this->nb for the state board.
getCountryCode()
Return account country code.
getLibStatut($mode=0)
Return label of object status.
update_bban($user=null)
Update BBAN (RIB) account fields.
create($user, $notrigger=0)
Create bank account into database.
addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur='', $banque='', $accountancycode='', $datev=null, $num_releve='', $amount_main_currency=null, $note_private='')
Add an entry into table llx_bank.
getBannerAddress($htmlkey, $object)
Return full address for banner.
error()
Return error.
info($id)
Load miscellaneous information for tab "Info".
const TYPE_CASH
Cash account.
update($user, $notrigger=0)
Update bank account card.
__toString()
Shows the account number in the appropriate format.
delete($user, $notrigger=0)
Delete bank account from database.
const TYPE_SAVINGS
Savings account.
getTooltipContentArray($params)
getTooltipContentArray
canBeConciliated()
Return if a bank account need to be conciliated.
can_be_deleted()
Indicates if an account can be deleted or not (without movements)
const TYPE_CURRENT
Current account.
setCategories($categories)
Sets object to supplied categories.
countAccountToReconcile()
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
verif()
Return if an account has valid information for Direct debit payment.
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)
get_url($fk_bank=0, $url_id=0, $type='')
TODO Move this into AccountLine Return array with links from llx_bank_url.
LibStatut($status, $mode=0)
Return label of given object status.
Class to manage bank transaction lines.
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...
setErrorsFromObject($object)
setErrorsFromObject
getFullAddress($withcountry=0, $sep="\n", $withregion=0, $extralangcode='')
Return full address of contact.
deleteExtraFields()
Delete all extra fields values for the current object.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Class to manage Dolibarr database access.
Class to manage generation of HTML components Only common components must be here.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
getCountry($searchkey, $withcode='', $dbtouse=null, $outputlangs=null, $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
global $mysoc
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
div refaddress div address
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as p p num_paiement as f pf amount as amount
Definition receipt.php:494
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:137