dolibarr 22.0.5
account.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
4 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
6 * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@inodbox.com>
7 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8 * Copyright (C) 2015-2016 Marcos García <marcosgdf@gmail.com>
9 * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
10 * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
11 * Copyright (C) 2019 JC Prieto <jcprieto@virtual20.com><prietojc@gmail.com>
12 * Copyright (C) 2022-2025 Frédéric France <frederic.france@free.fr>
13 * Copyright (C) 2024-2025 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 $rowid;
64
69 public $label;
70
77 public $courant;
78
83 public $type;
84
89 public $bank;
90
98 public $clos = self::STATUS_OPEN;
99
104 public $rappro = 1;
105
110 public $url;
111
116 public $code_banque;
117
122 public $code_guichet;
123
128 public $number;
129
134 public $cle_rib;
135
140 public $bic;
141
146 public $iban;
147
154 public $iban_prefix;
155
160 public $pti_in_ctti = 0;
161
168 public $proprio;
169
174 public $owner_name;
175
180 public $owner_address;
181
186 public $owner_zip;
187
192 public $owner_town;
196 public $owner_country_id;
200 public $owner_country_code;
201
208 public $domiciliation;
209
214 public $address;
218 public $state_id;
222 public $state_code;
226 public $state;
230 public $country_id;
231
237 public $type_lib = array();
238
243 public $account_number;
244
248 public $fk_accountancy_journal;
249
253 public $accountancy_journal;
254
259 public $currency_code;
260
267 public $account_currency_code;
268
273 public $min_allowed;
274
279 public $min_desired;
280
285 public $comment;
286
291 public $date_solde;
292
299 public $solde;
300
305 public $balance;
306
311 public $ics;
312
317 public $ics_transfer;
318
322 public $oldref;
323
324
349 // BEGIN MODULEBUILDER PROPERTIES
353 public $fields = array(
354 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
355 'ref' => array('type' => 'varchar(12)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'showoncombobox' => 1, 'position' => 25),
356 'label' => array('type' => 'varchar(30)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30),
357 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 35, 'index' => 1),
358 'bank' => array('type' => 'varchar(60)', 'label' => 'Bank', 'enabled' => 1, 'visible' => -1, 'position' => 40),
359 'code_banque' => array('type' => 'varchar(128)', 'label' => 'Code banque', 'enabled' => 1, 'visible' => -1, 'position' => 45),
360 'code_guichet' => array('type' => 'varchar(6)', 'label' => 'Code guichet', 'enabled' => 1, 'visible' => -1, 'position' => 50),
361 'number' => array('type' => 'varchar(255)', 'label' => 'Number', 'enabled' => 1, 'visible' => -1, 'position' => 55),
362 'cle_rib' => array('type' => 'varchar(5)', 'label' => 'Cle rib', 'enabled' => 1, 'visible' => -1, 'position' => 60),
363 'bic' => array('type' => 'varchar(11)', 'label' => 'Bic', 'enabled' => 1, 'visible' => -1, 'position' => 65),
364 'iban_prefix' => array('type' => 'varchar(34)', 'label' => 'Iban prefix', 'enabled' => 1, 'visible' => -1, 'position' => 70),
365 'country_iban' => array('type' => 'varchar(2)', 'label' => 'Country iban', 'enabled' => 1, 'visible' => -1, 'position' => 75),
366 'cle_iban' => array('type' => 'varchar(2)', 'label' => 'Cle iban', 'enabled' => 1, 'visible' => -1, 'position' => 80),
367 'domiciliation' => array('type' => 'varchar(255)', 'label' => 'Domiciliation', 'enabled' => 1, 'visible' => -1, 'position' => 85),
368 'state_id' => array('type' => 'integer', 'label' => 'StateId', 'enabled' => 1, 'visible' => -1, 'position' => 90),
369 'fk_pays' => array('type' => 'integer', 'label' => 'Country', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 95),
370 'proprio' => array('type' => 'varchar(60)', 'label' => 'Proprio', 'enabled' => 1, 'visible' => -1, 'position' => 100),
371 'owner_address' => array('type' => 'varchar(255)', 'label' => 'Owner address', 'enabled' => 1, 'visible' => -1, 'position' => 105),
372 'owner_zip' => array('type' => 'varchar(25)', 'label' => 'Owner zip', 'enabled' => 1, 'visible' => -1, 'position' => 106),
373 'owner_town' => array('type' => 'varchar(50)', 'label' => 'Owner town', 'enabled' => 1, 'visible' => -1, 'position' => 107),
374 'owner_country_id' => array('type' => 'integer', 'label' => 'Owner country', 'enabled' => 1, 'visible' => -1, 'position' => 108),
375 'courant' => array('type' => 'smallint(6)', 'label' => 'Courant', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 110),
376 'clos' => array('type' => 'smallint(6)', 'label' => 'Clos', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 115),
377 'rappro' => array('type' => 'smallint(6)', 'label' => 'Rappro', 'enabled' => 1, 'visible' => -1, 'position' => 120),
378 'url' => array('type' => 'varchar(128)', 'label' => 'Url', 'enabled' => 1, 'visible' => -1, 'position' => 125),
379 'account_number' => array('type' => 'varchar(32)', 'label' => 'Account number', 'enabled' => 1, 'visible' => -1, 'position' => 130),
380 'fk_accountancy_journal' => array('type' => 'integer', 'label' => 'Accountancy journal ID', 'enabled' => 1, 'visible' => -1, 'position' => 132),
381 'accountancy_journal' => array('type' => 'varchar(20)', 'label' => 'Accountancy journal', 'enabled' => 1, 'visible' => -1, 'position' => 135),
382 'currency_code' => array('type' => 'varchar(3)', 'label' => 'Currency code', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 140),
383 'min_allowed' => array('type' => 'integer', 'label' => 'Min allowed', 'enabled' => 1, 'visible' => -1, 'position' => 145),
384 'min_desired' => array('type' => 'integer', 'label' => 'Min desired', 'enabled' => 1, 'visible' => -1, 'position' => 150),
385 'comment' => array('type' => 'text', 'label' => 'Comment', 'enabled' => 1, 'visible' => -1, 'position' => 155),
386 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 156),
387 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 157),
388 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'position' => 160),
389 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 165),
390 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 170),
391 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 175),
392 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 180),
393 'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 185),
394 );
395 // END MODULEBUILDER PROPERTIES
396
400 const TYPE_CURRENT = 1;
404 const TYPE_CASH = 2;
408 const TYPE_SAVINGS = 0;
409
410
411 const STATUS_OPEN = 0;
412 const STATUS_CLOSED = 1;
413
414
420 public function __construct(DoliDB $db)
421 {
422 global $langs;
423
424 $this->db = $db;
425
426 $this->ismultientitymanaged = 1;
427
428 $this->balance = 0;
429
430 $this->type_lib = array(
431 self::TYPE_SAVINGS => $langs->transnoentitiesnoconv("BankType0"),
432 self::TYPE_CURRENT => $langs->transnoentitiesnoconv("BankType1"),
433 self::TYPE_CASH => $langs->transnoentitiesnoconv("BankType2"),
434 );
435
436 $this->labelStatus = array(
437 self::STATUS_OPEN => $langs->transnoentitiesnoconv("StatusAccountOpened"),
438 self::STATUS_CLOSED => $langs->transnoentitiesnoconv("StatusAccountClosed")
439 );
440 }
441
447 public function __toString()
448 {
449 $string = '';
450 foreach ($this->getFieldsToShow() as $val) {
451 if ($val == 'BankCode') {
452 $string .= $this->code_banque.' ';
453 } elseif ($val == 'BankAccountNumber') {
454 $string .= $this->number.' ';
455 } elseif ($val == 'DeskCode') {
456 $string .= $this->code_guichet.' ';
457 } elseif ($val == 'BankAccountNumberKey') {
458 $string .= $this->cle_rib.' ';
459 } elseif ($val == 'BIC') {
460 $string .= $this->bic.' ';
461 } elseif ($val == 'IBAN') {
462 $string .= $this->iban.' ';
463 }
464 }
465
466 return trim($string);
467 }
468
469
475 public function canBeConciliated()
476 {
477 global $conf;
478
479 if (empty($this->rappro)) {
480 return -1;
481 }
482 if ($this->type == Account::TYPE_CASH && !getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
483 return -2;
484 }
485 if ($this->status) {
486 return -3;
487 }
488 return 1;
489 }
490
491
492 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
503 public function add_url_line($line_id, $url_id, $url, $label, $type)
504 {
505 // phpcs:enable
506 // Avoid uk_bank_url collision when the same target (line_id, url_id, type) is linked twice
507 // e.g. two distinct credit transfers for the same employee dispatched on the same bank line.
508 $sqlcheck = "SELECT rowid FROM ".MAIN_DB_PREFIX."bank_url";
509 $sqlcheck .= " WHERE fk_bank = ".((int) $line_id);
510 $sqlcheck .= " AND url_id = ".((int) $url_id);
511 $sqlcheck .= " AND type = '".$this->db->escape($type)."'";
512 $resqlcheck = $this->db->query($sqlcheck);
513 if ($resqlcheck) {
514 $obj = $this->db->fetch_object($resqlcheck);
515 if ($obj) {
516 $this->db->free($resqlcheck);
517 return (int) $obj->rowid;
518 }
519 $this->db->free($resqlcheck);
520 }
521
522 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
523 $sql .= "fk_bank";
524 $sql .= ", url_id";
525 $sql .= ", url"; // deprecated
526 $sql .= ", label";
527 $sql .= ", type";
528 $sql .= ") VALUES (";
529 $sql .= ((int) $line_id);
530 $sql .= ", ".((int) $url_id);
531 $sql .= ", '".$this->db->escape($url)."'"; // deprecated
532 $sql .= ", '".$this->db->escape($label)."'";
533 $sql .= ", '".$this->db->escape($type)."'";
534 $sql .= ")";
535
536 dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
537 if ($this->db->query($sql)) {
538 $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
539 return $rowid;
540 } else {
541 $this->error = $this->db->lasterror();
542 return -1;
543 }
544 }
545
546 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
556 public function get_url($fk_bank = 0, $url_id = 0, $type = '')
557 {
558 // phpcs:enable
559 $lines = array();
560
561 // Check parameters
562 if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) {
563 $this->error = "ErrorBadParameter";
564 return -1;
565 }
566
567 $sql = "SELECT fk_bank, url_id, url, label, type";
568 $sql .= " FROM ".MAIN_DB_PREFIX."bank_url";
569 if ($fk_bank > 0) {
570 $sql .= " WHERE fk_bank = ".((int) $fk_bank);
571 } else {
572 $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'";
573 }
574 $sql .= " ORDER BY type, label";
575
576 dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
577 $result = $this->db->query($sql);
578 if ($result) {
579 $i = 0;
580 $num = $this->db->num_rows($result);
581 while ($i < $num) {
582 $obj = $this->db->fetch_object($result);
583 // Old links (for compatibility)
584 $lines[$i][0] = $obj->url;
585 $lines[$i][1] = $obj->url_id;
586 $lines[$i][2] = $obj->label;
587 $lines[$i][3] = $obj->type;
588 // New links
589 $lines[$i]['url'] = $obj->url;
590 $lines[$i]['url_id'] = $obj->url_id;
591 $lines[$i]['label'] = $obj->label;
592 $lines[$i]['type'] = $obj->type;
593 $lines[$i]['fk_bank'] = $obj->fk_bank;
594 $i++;
595 }
596 } else {
597 dol_print_error($this->db);
598 }
599
600 return $lines;
601 }
602
622 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 = '')
623 {
624 global $langs;
625
626 // Deprecation warning
627 if (is_numeric($oper)) {
628 dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING);
629 }
630
631 if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
632 $this->id = $this->rowid;
633 }
634
635 // Clean parameters
636 $emetteur = trim($emetteur);
637 $banque = trim($banque);
638 $label = trim($label);
639
640 $now = dol_now();
641
642 if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid
643 $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
644 $sql .= " WHERE id = ".((int) $oper);
645 $sql .= " AND entity IN (".getEntity('c_paiement').")";
646 $resql = $this->db->query($sql);
647 if ($resql) {
648 $obj = $this->db->fetch_object($resql);
649 $oper = $obj->code;
650 } else {
651 dol_print_error($this->db, 'Failed to get payment type code');
652 return -1;
653 }
654 }
655
656 // Check parameters
657 if (!$oper) {
658 $this->error = $langs->trans("OperNotDefined");
659 return -1;
660 }
661 if (!$this->id) {
662 $this->error = $langs->trans("ThisIdNotDefined");
663 return -2;
664 }
665 if ($this->type == Account::TYPE_CASH && $oper != 'LIQ') {
666 $this->error = "ErrorCashAccountAcceptsOnlyCashMoney";
667 return -3;
668 }
669
670 $this->db->begin();
671
672 if (is_null($datev) || empty($datev)) {
673 $datev = $date;
674 }
675
676 $accline = new AccountLine($this->db);
677 $accline->datec = $now;
678 $accline->dateo = $date;
679 $accline->datev = $datev;
680 $accline->label = $label;
681 $accline->amount = $amount;
682 $accline->amount_main_currency = $amount_main_currency;
683 $accline->fk_user_author = $user->id;
684 $accline->fk_account = $this->id;
685 $accline->fk_type = $oper;
686 $accline->numero_compte = $accountancycode;
687 $accline->num_releve = $num_releve;
688 $accline->note_private = $note_private;
689
690 if ($num_chq) {
691 $accline->num_chq = $num_chq;
692 }
693
694 if ($emetteur) {
695 $accline->emetteur = $emetteur;
696 }
697
698 if ($banque) {
699 $accline->bank_chq = $banque;
700 }
701
702 if ($accline->insert() > 0) {
703 if ($categorie > 0) {
704 $sql = "INSERT INTO ".MAIN_DB_PREFIX."category_bankline(";
705 $sql .= "lineid, fk_categ";
706 $sql .= ") VALUES (";
707 $sql .= ((int) $accline->id).", ".((int) $categorie);
708 $sql .= ")";
709
710 $result = $this->db->query($sql);
711 if (!$result) {
712 $this->error = $this->db->lasterror();
713 $this->db->rollback();
714
715 return -4;
716 }
717 }
718
719 $this->db->commit();
720
721 return $accline->id;
722 } else {
723 $this->setErrorsFromObject($accline);
724 $this->db->rollback();
725
726 return -5;
727 }
728 }
729
737 public function create($user, $notrigger = 0)
738 {
739 global $langs, $conf;
740
741 $error = 0;
742
743 // Clean parameters
744 if (!$this->min_allowed) {
745 $this->min_allowed = 0;
746 }
747 if (!$this->min_desired) {
748 $this->min_desired = 0;
749 }
750
751 // Check parameters
752 if (empty($this->country_id)) {
753 $langs->load('errors');
754 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
755 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
756 return -1;
757 }
758 if (empty($this->ref)) {
759 $langs->load('errors');
760 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
761 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
762 return -1;
763 }
764 if (empty($this->date_solde)) {
765 $langs->load('errors');
766 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance"));
767 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
768 return -1;
769 }
770
771 // Load libraries to check BAN
772 $balance = $this->balance;
773 if (empty($balance) && !empty($this->solde)) {
774 dol_syslog(get_class($this)."::create solde is deprecated use balance", LOG_NOTICE);
775 $balance = $this->solde;
776 }
777 if (empty($balance)) {
778 $balance = 0;
779 }
780 if (empty($this->status && !empty($this->clos))) {
781 dol_syslog(get_class($this)."::create clos is deprecated use status", LOG_NOTICE);
782 $this->status = $this->clos;
783 }
784
785 if (empty($this->country_id) && !empty($this->country_code)) {
786 $country_id = getCountry($this->country_code, '3');
787 $this->country_id = is_int($country_id) ? $country_id : 0;
788 }
789
790 // Load the library to validate/check a BAN account
791 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
792
793 $now = dol_now();
794
795 $this->db->begin();
796
797 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
798 $sql .= "datec";
799 $sql .= ", ref";
800 $sql .= ", label";
801 $sql .= ", entity";
802 $sql .= ", account_number";
803 $sql .= ", fk_accountancy_journal";
804 $sql .= ", bank";
805 $sql .= ", code_banque";
806 $sql .= ", code_guichet";
807 $sql .= ", number";
808 $sql .= ", cle_rib";
809 $sql .= ", bic";
810 $sql .= ", iban_prefix";
811 $sql .= ", domiciliation";
812 $sql .= ", pti_in_ctti";
813 $sql .= ", proprio";
814 $sql .= ", owner_address";
815 $sql .= ", owner_zip";
816 $sql .= ", owner_town";
817 $sql .= ", owner_country_id";
818 $sql .= ", currency_code";
819 $sql .= ", rappro";
820 $sql .= ", min_allowed";
821 $sql .= ", min_desired";
822 $sql .= ", comment";
823 $sql .= ", state_id";
824 $sql .= ", fk_pays";
825 $sql .= ", ics";
826 $sql .= ", ics_transfer";
827 $sql .= ") VALUES (";
828 $sql .= "'".$this->db->idate($now)."'";
829 $sql .= ", '".$this->db->escape($this->ref)."'";
830 $sql .= ", '".$this->db->escape($this->label)."'";
831 $sql .= ", ".((int) $conf->entity);
832 $sql .= ", '".$this->db->escape($this->account_number)."'";
833 $sql .= ", ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
834 $sql .= ", '".$this->db->escape($this->bank)."'";
835 $sql .= ", '".$this->db->escape($this->code_banque)."'";
836 $sql .= ", '".$this->db->escape($this->code_guichet)."'";
837 $sql .= ", '".$this->db->escape($this->number)."'";
838 $sql .= ", '".$this->db->escape($this->cle_rib)."'";
839 $sql .= ", '".$this->db->escape($this->bic)."'";
840 $sql .= ", '".$this->db->escape($this->iban)."'";
841 $sql .= ", '".$this->db->escape($this->address)."'";
842 $sql .= ", ".((int) $this->pti_in_ctti);
843 $sql .= ", '".$this->db->escape($this->owner_name)."'";
844 $sql .= ", '".$this->db->escape($this->owner_address)."'";
845 $sql .= ", '".$this->db->escape($this->owner_zip)."'";
846 $sql .= ", '".$this->db->escape($this->owner_town)."'";
847 $sql .= ", ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
848 $sql .= ", '".$this->db->escape($this->currency_code)."'";
849 $sql .= ", ".((int) $this->rappro);
850 $sql .= ", ".price2num($this->min_allowed, 'MT');
851 $sql .= ", ".price2num($this->min_desired, 'MT');
852 $sql .= ", '".$this->db->escape($this->comment)."'";
853 $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
854 $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
855 $sql .= ", '".$this->db->escape($this->ics)."'";
856 $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
857 $sql .= ")";
858
859 dol_syslog(get_class($this)."::create", LOG_DEBUG);
860 $resql = $this->db->query($sql);
861 if ($resql) {
862 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
863
864 $result = $this->update($user, 1);
865 if ($result > 0) {
866 $accline = new AccountLine($this->db);
867 $accline->datec = $now;
868 $accline->label = '('.$langs->trans("InitialBankBalance").')';
869 $accline->amount = (float) price2num($balance);
870 $accline->fk_user_author = $user->id;
871 $accline->fk_account = $this->id;
872 $accline->datev = $this->date_solde;
873 $accline->dateo = $this->date_solde;
874 $accline->fk_type = 'SOLD';
875
876 if ($accline->insert() < 0) {
877 $error++;
878 $this->setErrorsFromObject($accline);
879 }
880
881 if (!$error) {
882 $result = $this->insertExtraFields();
883 if ($result < 0) {
884 $error++;
885 }
886 }
887
888 if (!$error && !$notrigger) {
889 // Call trigger
890 $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
891 if ($result < 0) {
892 $error++;
893 }
894 // End call triggers
895 }
896 } else {
897 $error++;
898 }
899 } else {
900 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
901 $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
902 $error++;
903 } else {
904 $this->error = $this->db->error()." sql=".$sql;
905 $error++;
906 }
907 }
908
909 if (!$error) {
910 $this->db->commit();
911 return $this->id;
912 } else {
913 $this->db->rollback();
914 return -1 * $error;
915 }
916 }
917
925 public function update($user, $notrigger = 0)
926 {
927 global $langs, $conf;
928
929 if (empty($this->country_id) && !empty($this->country_code)) {
930 $country_id = getCountry($this->country_code, '3');
931 $this->country_id = is_int($country_id) ? $country_id : 0;
932 }
933
934 $error = 0;
935
936 $this->db->begin();
937
938 // Check parameters
939 if (empty($this->country_id)) {
940 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
941 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
942 return -1;
943 }
944 if (empty($this->ref)) {
945 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
946 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
947 return -1;
948 }
949 if (!$this->label) {
950 $this->label = "???";
951 }
952
953 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET";
954 $sql .= " ref = '".$this->db->escape($this->ref)."'";
955 $sql .= ",label = '".$this->db->escape($this->label)."'";
956 $sql .= ",courant = ".((int) $this->type);
957 $sql .= ",clos = ".((int) $this->status);
958 $sql .= ",rappro = ".((int) $this->rappro);
959 $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
960 $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
961 $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
962 $sql .= ",bank = '".$this->db->escape($this->bank)."'";
963 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
964 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
965 $sql .= ",number='".$this->db->escape($this->number)."'";
966 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
967 $sql .= ",bic='".$this->db->escape($this->bic)."'";
968 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
969 $sql .= ",domiciliation='".$this->db->escape($this->address)."'";
970 $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
971 $sql .= ",proprio = '".$this->db->escape($this->owner_name)."'";
972 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
973 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
974 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
975 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
976
977 $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
978
979 $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
980 $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
981 $sql .= ",comment = '".$this->db->escape($this->comment)."'";
982
983 $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
984 $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
985 $sql .= ",ics = '".$this->db->escape($this->ics)."'";
986 $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
987
988 $sql .= " WHERE rowid = ".((int) $this->id);
989
990 dol_syslog(get_class($this)."::update", LOG_DEBUG);
991 $result = $this->db->query($sql);
992 if ($result) {
993 // Actions on extra fields (by external module or standard code)
994 if (!$error) {
995 $result = $this->insertExtraFields();
996 if ($result < 0) {
997 $error++;
998 }
999 }
1000
1001 if (!$error && !empty($this->oldref) && $this->oldref !== $this->ref) {
1002 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'bank/".$this->db->escape($this->ref)."'";
1003 $sql .= " WHERE filepath = 'bank/".$this->db->escape($this->oldref)."' and src_object_type='bank_account' and entity = ".((int) $conf->entity);
1004 $resql = $this->db->query($sql);
1005 if (!$resql) {
1006 $error++;
1007 $this->error = $this->db->lasterror();
1008 }
1009
1010 // We rename directory in order not to lose the attachments
1011 $oldref = dol_sanitizeFileName($this->oldref);
1012 $newref = dol_sanitizeFileName($this->ref);
1013 $dirsource = $conf->bank->dir_output.'/'.$oldref;
1014 $dirdest = $conf->bank->dir_output.'/'.$newref;
1015 if (file_exists($dirsource)) {
1016 dol_syslog(get_class($this)."::update rename dir ".$dirsource." into ".$dirdest, LOG_DEBUG);
1017 if (@rename($dirsource, $dirdest)) {
1018 dol_syslog("Rename ok", LOG_DEBUG);
1019 }
1020 }
1021 }
1022
1023 if (!$error && !$notrigger) {
1024 // Call trigger
1025 $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
1026 if ($result < 0) {
1027 $error++;
1028 }
1029 // End call triggers
1030 }
1031 } else {
1032 $error++;
1033 $this->error = $this->db->lasterror();
1034 dol_print_error($this->db);
1035 }
1036
1037 if (!$error) {
1038 $this->db->commit();
1039 return $this->id;
1040 } else {
1041 $this->db->rollback();
1042 return -1 * $error;
1043 }
1044 }
1045
1046
1047 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1054 public function update_bban($user = null)
1055 {
1056 // phpcs:enable
1057 global $conf, $langs;
1058
1059 // Load library to get BAN control function
1060 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1061
1062 dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
1063
1064 // Check parameters
1065 if (!$this->ref) {
1066 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
1067 return -2;
1068 }
1069
1070 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET";
1071 $sql .= " bank = '".$this->db->escape($this->bank)."'";
1072 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
1073 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
1074 $sql .= ",number='".$this->db->escape($this->number)."'";
1075 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
1076 $sql .= ",bic='".$this->db->escape($this->bic)."'";
1077 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
1078 $sql .= ",domiciliation='".$this->db->escape($this->address)."'";
1079 $sql .= ",proprio = '".$this->db->escape($this->owner_name)."'";
1080 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
1081 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
1082 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
1083 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
1084 $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null");
1085 $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null");
1086 $sql .= " WHERE rowid = ".((int) $this->id);
1087 $sql .= " AND entity = ".((int) $conf->entity);
1088
1089 dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
1090
1091 $result = $this->db->query($sql);
1092 if ($result) {
1093 return 1;
1094 } else {
1095 $this->error = $this->db->lasterror();
1096 dol_print_error($this->db);
1097 return -1;
1098 }
1099 }
1100
1101
1109 public function fetch($id, $ref = '')
1110 {
1111 if (empty($id) && empty($ref)) {
1112 $this->error = "ErrorBadParameters";
1113 return -1;
1114 }
1115
1116 $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,";
1117 $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
1118 $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,";
1119 $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
1120 $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
1121 $sql .= " ba.datec as date_creation, ba.tms as date_modification, ba.ics, ba.ics_transfer,";
1122 $sql .= ' c.code as country_code, c.label as country,';
1123 $sql .= ' d.code_departement as state_code, d.nom as state,';
1124 $sql .= ' aj.code as accountancy_journal';
1125 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1126 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
1127 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
1128 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid = ba.fk_accountancy_journal';
1129 $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
1130 if ($id) {
1131 $sql .= " AND ba.rowid = ".((int) $id);
1132 }
1133 if ($ref) {
1134 $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
1135 }
1136
1137 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1138 $result = $this->db->query($sql);
1139 if ($result) {
1140 if ($this->db->num_rows($result)) {
1141 $obj = $this->db->fetch_object($result);
1142
1143 $this->id = $obj->rowid;
1144 $this->rowid = $obj->rowid;
1145 $this->entity = $obj->entity;
1146 $this->ref = $obj->ref;
1147 $this->label = $obj->label;
1148 $this->type = $obj->type;
1149 $this->courant = $obj->type;
1150 $this->bank = $obj->bank;
1151 $this->clos = $obj->status;
1152 $this->status = $obj->status;
1153 $this->rappro = $obj->rappro;
1154 $this->url = $obj->url;
1155
1156 $this->code_banque = $obj->code_banque;
1157 $this->code_guichet = $obj->code_guichet;
1158 $this->number = $obj->number;
1159 $this->cle_rib = $obj->cle_rib;
1160 $this->bic = $obj->bic;
1161 $this->iban = $obj->iban;
1162 $this->address = $obj->address;
1163
1164 $this->owner_name = $obj->owner_name;
1165 $this->proprio = $this->owner_name;
1166 $this->owner_address = $obj->owner_address;
1167 $this->owner_zip = $obj->owner_zip;
1168 $this->owner_town = $obj->owner_town;
1169 $this->owner_country_id = $obj->owner_country_id;
1170
1171 $this->pti_in_ctti = $obj->pti_in_ctti;
1172
1173 $this->state_id = $obj->state_id;
1174 $this->state_code = $obj->state_code;
1175 $this->state = $obj->state;
1176
1177 $this->country_id = $obj->country_id;
1178 $this->country_code = $obj->country_code;
1179 $this->country = $obj->country;
1180
1181 $this->account_number = $obj->account_number;
1182 $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
1183 $this->accountancy_journal = $obj->accountancy_journal;
1184
1185 $this->currency_code = $obj->currency_code;
1186 $this->account_currency_code = $obj->currency_code;
1187 $this->min_allowed = $obj->min_allowed;
1188 $this->min_desired = $obj->min_desired;
1189 $this->comment = $obj->comment;
1190
1191 $this->date_creation = $this->db->jdate($obj->date_creation);
1192 $this->date_modification = $this->db->jdate($obj->date_modification);
1193
1194 $this->ics = $obj->ics;
1195 $this->ics_transfer = $obj->ics_transfer;
1196
1197 // Retrieve all extrafield
1198 // fetch optionals attributes and labels
1199 $this->fetch_optionals();
1200
1201 return 1;
1202 } else {
1203 return 0;
1204 }
1205 } else {
1206 $this->error = $this->db->lasterror();
1207 $this->errors[] = $this->error;
1208 return -1;
1209 }
1210 }
1211
1222 public function setCategories($categories)
1223 {
1224 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1225 return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
1226 }
1227
1235 public function delete($user, $notrigger = 0)
1236 {
1237 $error = 0;
1238
1239 $this->db->begin();
1240
1241 // @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
1242
1243 // Delete link between tag and bank account
1244 if (!$error) {
1245 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1246 $sql .= " WHERE fk_account = ".((int) $this->id);
1247
1248 $resql = $this->db->query($sql);
1249 if (!$resql) {
1250 $error++;
1251 $this->error = "Error ".$this->db->lasterror();
1252 }
1253 }
1254
1255 if (!$error) {
1256 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
1257 $sql .= " WHERE rowid = ".((int) $this->id);
1258
1259 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1260 $result = $this->db->query($sql);
1261 if ($result) {
1262 // Remove extrafields
1263 if (!$error) {
1264 $result = $this->deleteExtraFields();
1265 if ($result < 0) {
1266 $error++;
1267 dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1268 }
1269 }
1270 } else {
1271 $error++;
1272 $this->error = "Error ".$this->db->lasterror();
1273 }
1274 }
1275
1276 if (!$error) {
1277 $this->db->commit();
1278 return 1;
1279 } else {
1280 $this->db->rollback();
1281 return -1;
1282 }
1283 }
1284
1285
1292 public function getLibStatut($mode = 0)
1293 {
1294 return $this->LibStatut($this->status, $mode);
1295 }
1296
1297 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1305 public function LibStatut($status, $mode = 0)
1306 {
1307 // phpcs:enable
1308 global $langs;
1309 $langs->load('banks');
1310
1311 if ($status == self::STATUS_OPEN) {
1312 $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1313 $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1314 $statusType = 'status4';
1315 } else {
1316 $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1317 $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1318 $statusType = 'status5';
1319 }
1320
1321 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1322 }
1323
1324
1325 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1331 public function can_be_deleted()
1332 {
1333 // phpcs:enable
1334 $can_be_deleted = false;
1335
1336 $sql = "SELECT COUNT(rowid) as nb";
1337 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1338 $sql .= " WHERE fk_account = ".((int) $this->id);
1339
1340 $resql = $this->db->query($sql);
1341 if ($resql) {
1342 $obj = $this->db->fetch_object($resql);
1343 if ($obj->nb <= 1) {
1344 $can_be_deleted = true; // Juste le solde
1345 }
1346 } else {
1347 dol_print_error($this->db);
1348 }
1349 return $can_be_deleted;
1350 }
1351
1352
1358 public function error()
1359 {
1360 return $this->error;
1361 }
1362
1371 public function solde($option = 0, $date_end = '', $field = 'dateo')
1372 {
1373 $solde = 0;
1374
1375 $sql = "SELECT sum(amount) as amount";
1376 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1377 $sql .= " WHERE fk_account = ".((int) $this->id);
1378 if ($option == 1) {
1379 $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1380 }
1381
1382 $resql = $this->db->query($sql);
1383 if ($resql) {
1384 if ($this->db->num_rows($resql)) {
1385 $obj = $this->db->fetch_object($resql);
1386 $solde = $obj->amount;
1387 }
1388 $this->db->free($resql);
1389 } else {
1390 $this->errors[] = $this->db->lasterror;
1391 return -1;
1392 }
1393
1394 return (float) price2num($solde, 'MU');
1395 }
1396
1397 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1405 public function load_board(User $user, $filteraccountid = 0)
1406 {
1407 // phpcs:enable
1408 global $conf, $langs;
1409
1410 if ($user->socid) {
1411 return -1; // protection pour eviter appel par utilisateur externe
1412 }
1413
1414 $sql = "SELECT b.rowid, b.datev as datefin";
1415 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1416 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1417 $sql .= " WHERE b.rappro=0";
1418 $sql .= " AND b.fk_account = ba.rowid";
1419 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1420 $sql .= " AND (ba.rappro = 1 AND ba.courant != " . Account::TYPE_CASH . ")"; // Compte rapprochable
1421 $sql .= " AND clos = 0";
1422 if ($filteraccountid) {
1423 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1424 }
1425
1426 $resql = $this->db->query($sql);
1427 if ($resql) {
1428 $langs->load("banks");
1429 $now = dol_now();
1430
1431 require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1432
1433 $response = new WorkboardResponse();
1434 $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1435 $response->label = $langs->trans("TransactionsToConciliate");
1436 $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1437 $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1438 $response->img = img_object('', "payment");
1439
1440 while ($obj = $this->db->fetch_object($resql)) {
1441 $response->nbtodo++;
1442 if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1443 $response->nbtodolate++;
1444 }
1445 }
1446
1447 return $response;
1448 } else {
1449 dol_print_error($this->db);
1450 $this->error = $this->db->error();
1451 return -1;
1452 }
1453 }
1454
1461 public function loadStateBoard($filteraccountid = 0)
1462 {
1463 global $user;
1464
1465 if ($user->socid) {
1466 return -1; // protection pour eviter appel par utilisateur externe
1467 }
1468
1469 $sql = "SELECT count(b.rowid) as nb";
1470 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1471 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1472 $sql .= " WHERE b.fk_account = ba.rowid";
1473 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1474 $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1475 $sql .= " AND clos = 0";
1476 if ($filteraccountid) {
1477 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1478 }
1479
1480 $resql = $this->db->query($sql);
1481 if ($resql) {
1482 while ($obj = $this->db->fetch_object($resql)) {
1483 $this->nb["banklines"] = $obj->nb;
1484 }
1485 $this->db->free($resql);
1486 return 1;
1487 } else {
1488 dol_print_error($this->db);
1489 $this->error = $this->db->error();
1490 return -1;
1491 }
1492 }
1493
1494
1500 public function countAccountToReconcile()
1501 {
1502 global $user;
1503
1504 //Protection against external users
1505 if ($user->socid) {
1506 return 0;
1507 }
1508
1509 $nb = 0;
1510
1511 $sql = "SELECT COUNT(ba.rowid) as nb";
1512 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1513 $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1514 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1515 if (!getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
1516 $sql .= " AND ba.courant != " . Account::TYPE_CASH;
1517 }
1518
1519 $resql = $this->db->query($sql);
1520 if ($resql) {
1521 $obj = $this->db->fetch_object($resql);
1522 $nb = $obj->nb;
1523 } else {
1524 dol_print_error($this->db);
1525 }
1526
1527 return $nb;
1528 }
1529
1538 public function getTooltipContentArray($params)
1539 {
1540 global $langs;
1541 $langs->loadLangs(['banks', 'compta']);
1542 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1543
1544 $datas = array();
1545
1546 $nofetch = !empty($params['nofetch']);
1547 $pictos = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
1548 if (isset($this->status)) {
1549 $pictos .= ' '.$this->getLibStatut(5);
1550 }
1551 $datas['picto'] = $pictos;
1552 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1553 $datas['label'] = '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1554 $datas['accountnumber'] = '<br><br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
1555 $datas['iban'] = '<br><b>'.$langs->trans('IBAN').':</b> '.getIbanHumanReadable($this);
1556 $datas['bic'] = '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
1557 $datas['accountcurrency'] = '<br><br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
1558
1559 if (isModEnabled('accounting')) {
1560 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1561 $langs->load("accountancy");
1562 $datas['accountaccounting'] = '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
1563 $datas['accountancyjournal'] = '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
1564 }
1565 // show categories for this record only in ajax to not overload lists
1566 if (isModEnabled('category') && !$nofetch) {
1567 require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
1568 $form = new Form($this->db);
1569 $datas['categories'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_ACCOUNT, 1);
1570 }
1571
1572 return $datas;
1573 }
1574
1586 public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1587 {
1588 global $conf, $langs;
1589
1590 if (!empty($conf->dol_no_mouse_hover)) {
1591 $notooltip = 1; // Force disable tooltips
1592 }
1593
1594 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1595
1596 $result = '';
1597 $classfortooltip = 'classfortooltip';
1598 $dataparams = '';
1599 $params = [
1600 'id' => $this->id,
1601 'objecttype' => $this->element,
1602 'option' => $option,
1603 'nofetch' => 1,
1604 ];
1605 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1606 $classfortooltip = 'classforajaxtooltip';
1607 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1608 $label = '';
1609 } else {
1610 $label = implode($this->getTooltipContentArray($params));
1611 }
1612
1613 $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1614 if ($mode == 'transactions') {
1615 $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1616 } elseif ($mode == 'receipts') {
1617 $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1618 }
1619
1620 if ($option != 'nolink') {
1621 // Add param to save lastsearch_values or not
1622 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1623 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1624 $add_save_lastsearch_values = 1;
1625 }
1626 if ($add_save_lastsearch_values) {
1627 $url .= '&save_lastsearch_values=1';
1628 }
1629 }
1630
1631 $linkclose = '';
1632 if (empty($notooltip)) {
1633 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1634 $label = $langs->trans("BankAccount");
1635 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1636 }
1637 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1638 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1639 } else {
1640 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1641 }
1642
1643 if ($option == 'nolink' || empty($url)) {
1644 $linkstart = '<span';
1645 } else {
1646 $linkstart = '<a href="'.$url.'"';
1647 }
1648 $linkstart .= $linkclose.'>';
1649 if ($option == 'nolink' || empty($url)) {
1650 $linkend = '</span>';
1651 } else {
1652 $linkend = '</a>';
1653 }
1654
1655 $result .= $linkstart;
1656
1657 if ($withpicto) {
1658 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1659 }
1660 if ($withpicto != 2) {
1661 $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1662 }
1663 $result .= $linkend;
1664
1665 return $result;
1666 }
1667
1668
1669 // Method after here are common to Account and CompanyBankAccount
1670
1671
1677 public function verif()
1678 {
1679 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1680
1681 $error = 0;
1682
1683 // Call functions to check BAN
1684 if (!checkIbanForAccount($this)) {
1685 $error++;
1686 $this->error = 'IBANNotValid';
1687 }
1688 // Call function to check Swift/BIC.
1689 // A non valid BIC/Swift is a problem if: it is not empty or always a problem if WITHDRAWAL_WITHOUT_BIC is not set).
1690 if (!checkSwiftForAccount($this) && (!empty($this->bic) || !getDolGlobalInt('WITHDRAWAL_WITHOUT_BIC'))) {
1691 $error++;
1692 $this->error = 'SwiftNotValid';
1693 }
1694
1695 if (! $error) {
1696 return 1;
1697 } else {
1698 return 0;
1699 }
1700 }
1701
1707 public function getCountryCode()
1708 {
1709 global $mysoc;
1710
1711 // We return country code of bank account
1712 if (!empty($this->country_code)) {
1713 return $this->country_code;
1714 }
1715
1716 // For backward compatibility, we try to guess country from other information
1717 if (!empty($this->iban)) {
1718 // If IBAN defined, we can know country of account from it
1719 $reg = array();
1720 if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
1721 return $reg[1];
1722 }
1723 }
1724
1725 // If this class is linked to a third party
1726 if (!empty($this->socid)) {
1727 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
1728 $company = new Societe($this->db);
1729 $result = $company->fetch($this->socid);
1730 if (!empty($company->country_code)) {
1731 return $company->country_code;
1732 }
1733 }
1734
1735 // We return country code of managed company
1736 if (!empty($mysoc->country_code)) {
1737 return $mysoc->country_code;
1738 }
1739
1740 return '';
1741 }
1742
1750 public function getBannerAddress($htmlkey, $object)
1751 {
1752 global $conf, $langs;
1753
1754 $out = '';
1755
1756 $outdone = 0;
1757 $coords = $this->getFullAddress(1, ', ', getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT'));
1758 if ($coords) {
1759 if (!empty($conf->use_javascript_ajax)) {
1760 // hideonsmatphone because copyToClipboard call jquery dialog that does not work with jmobile
1761 $out .= '<a href="#" class="hideonsmartphone" onclick="return copyToClipboard(\''.dol_escape_js($coords).'\',\''.dol_escape_js($langs->trans("HelpCopyToClipboard")).'\');">';
1762 $out .= img_picto($langs->trans("Address"), 'map-marker-alt');
1763 $out .= '</a> ';
1764 }
1765 $address = dol_print_address($coords, 'address_'.$htmlkey.'_'.$this->id, $this->element, $this->id, 1, ', ');
1766 if ($address) {
1767 $out .= $address;
1768 $outdone++;
1769 }
1770 $outdone++;
1771 }
1772
1773 return $out;
1774 }
1775
1776
1785 public function useDetailedBBAN()
1786 {
1787 $country_code = $this->getCountryCode();
1788
1789 if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
1790 return 1; // France, Spain, Gabon, ... - Not valid for CH
1791 }
1792 if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
1793 return 2; // Australia, England...
1794 }
1795 return 0;
1796 }
1797
1803 public function needIBAN()
1804 {
1805 global $conf;
1806
1807 if (getDolGlobalString('MAIN_IBAN_IS_NEVER_MANDATORY')) {
1808 return 0;
1809 }
1810
1811 $country_code = $this->getCountryCode();
1812
1813 $country_code_in_EEC = array(
1814 'AT', // Austria
1815 'BE', // Belgium
1816 'BG', // Bulgaria
1817 'CY', // Cyprus
1818 'CZ', // Czech republic
1819 'DE', // Germany
1820 'DK', // Danemark
1821 'EE', // Estonia
1822 'ES', // Spain
1823 'FI', // Finland
1824 'FR', // France
1825 'GB', // United Kingdom
1826 'GR', // Greece
1827 'HR', // Croatia
1828 'NL', // Holland
1829 'HU', // Hungary
1830 'IE', // Ireland
1831 'IM', // Isle of Man - Included in UK
1832 'IT', // Italy
1833 'LT', // Lithuania
1834 'LU', // Luxembourg
1835 'LV', // Latvia
1836 'MC', // Monaco - Included in France
1837 'MT', // Malta
1838 //'NO', // Norway
1839 'PL', // Poland
1840 'PT', // Portugal
1841 'RO', // Romania
1842 'SE', // Sweden
1843 'SK', // Slovakia
1844 'SI', // Slovenia
1845 'UK', // United Kingdom
1846 //'CH', // Switzerland - No. Swizerland in not in EEC
1847 );
1848
1849 if (in_array($country_code, $country_code_in_EEC)) {
1850 return 1; // France, Spain, ...
1851 }
1852 return 0;
1853 }
1854
1860 public function needBIC()
1861 {
1862 if (getDolGlobalString('MAIN_IBAN_IS_NEVER_MANDATORY')) {
1863 return 0;
1864 }
1865
1866 $country_code = $this->getCountryCode();
1867
1868 $country_code_in_EEC = array(
1869 'AD', // Andorra
1870 'BH', // Bahrein
1871 'DK', // Denmark
1872 'FR', // France
1873 'GH', // Ghana
1874 'HU', // Hungary
1875 'JP', // Japan
1876 'LV', // Latvia
1877 'SE', // Sweden
1878 );
1879
1880 if (in_array($country_code, $country_code_in_EEC)) {
1881 return 1; // Andorra, Bahrein, ...
1882 }
1883 return 0;
1884 }
1885
1892 public function info($id)
1893 {
1894 }
1895
1910 public function getFieldsToShow($includeibanbic = 0)
1911 {
1912 //Get the required properties depending on the country
1913 $detailedBBAN = $this->useDetailedBBAN();
1914
1915 if ($detailedBBAN == 0) {
1916 $fieldarray = array(
1917 'BankAccountNumber'
1918 );
1919 } elseif ($detailedBBAN == 2) {
1920 $fieldarray = array(
1921 'BankCode',
1922 'BankAccountNumber'
1923 );
1924 } else {
1925 $fieldarray = self::getAccountNumberOrder();
1926 }
1927
1928 //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1929 if ($includeibanbic) {
1930 $fieldarray[] = 'IBAN';
1931 $fieldarray[] = 'BIC';
1932 }
1933 //}
1934
1935 //Get the order the properties are shown
1936 return $fieldarray;
1937 }
1938
1949 public static function getAccountNumberOrder()
1950 {
1951 global $conf;
1952
1953 $fieldlists = array(
1954 'BankCode',
1955 'DeskCode',
1956 'BankAccountNumber',
1957 'BankAccountNumberKey'
1958 );
1959
1960 if (getDolGlobalString('BANK_SHOW_ORDER_OPTION')) {
1961 if (is_numeric(getDolGlobalString('BANK_SHOW_ORDER_OPTION'))) {
1962 if (getDolGlobalString('BANK_SHOW_ORDER_OPTION') == '1') {
1963 $fieldlists = array(
1964 'BankCode',
1965 'DeskCode',
1966 'BankAccountNumberKey',
1967 'BankAccountNumber'
1968 );
1969 }
1970 } else {
1971 //Replace the old AccountNumber key with the new BankAccountNumber key
1972 $fieldlists = explode(
1973 ' ',
1974 preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
1975 );
1976 }
1977 }
1978
1979 return $fieldlists;
1980 }
1981
1982
1990 public function initAsSpecimen()
1991 {
1992 // Example of IBAN FR7630001007941234567890185
1993 $this->specimen = 1;
1994 $this->ref = 'MBA';
1995 $this->label = 'My Big Company Bank account';
1996 $this->courant = Account::TYPE_CURRENT;
1997 $this->clos = Account::STATUS_OPEN;
1998 $this->type = Account::TYPE_CURRENT;
1999 $this->status = Account::STATUS_OPEN;
2000 $this->code_banque = '30001';
2001 $this->code_guichet = '00794';
2002 $this->number = '12345678901';
2003 $this->cle_rib = '85';
2004 $this->bic = 'AA12';
2005 $this->iban = 'FR7630001007941234567890185';
2006
2007 $this->bank = 'MyBank';
2008 $this->address = 'Rue de Paris';
2009 $this->owner_name = 'Owner';
2010 $this->owner_address = 'Owner address';
2011 $this->owner_zip = 'Owner zip';
2012 $this->owner_town = 'Owner town';
2013 $this->owner_country_id = 1;
2014 $this->country_id = 1;
2015
2016 return 1;
2017 }
2018
2027 public static function replaceThirdparty($dbs, $origin_id, $dest_id)
2028 {
2029 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'";
2030
2031 if ($dbs->query($sql)) {
2032 return true;
2033 } else {
2034 //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.
2035 //$this->errors = $dbs->lasterror();
2036 return false;
2037 }
2038 }
2039
2047 public function getKanbanView($option = '', $arraydata = null)
2048 {
2049 global $langs;
2050
2051 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2052
2053 $return = '<div class="box-flex-item box-flex-grow-zero">';
2054 $return .= '<div class="info-box info-box-sm">';
2055 $return .= '<span class="info-box-icon bg-infobox-action">';
2056 $return .= img_picto('', $this->picto);
2057 $return .= '</span>';
2058 $return .= '<div class="info-box-content">';
2059 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
2060 if ($selected >= 0) {
2061 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2062 }
2063 if (property_exists($this, 'type_lib')) {
2064 $return .= '<br><span class="info-box-label opacitymedium" title="'.$this->type_lib[$this->type].'">'.substr($this->type_lib[$this->type], 0, 24).'...</span>';
2065 }
2066 if (method_exists($this, 'solde')) {
2067 $return .= '<br><a href="'.DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id.'">';
2068 $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>';
2069 }
2070 if (method_exists($this, 'getLibStatut')) {
2071 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2072 }
2073 $return .= '</div>';
2074 $return .= '</div>';
2075 $return .= '</div>';
2076 return $return;
2077 }
2078}
2079
2080
2081require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
2082
2086class AccountLine extends CommonObjectLine
2087{
2091 public $db;
2092
2096 public $element = 'bank';
2097
2101 public $table_element = 'bank';
2102
2106 public $picto = 'accountline';
2107
2111 public $id;
2112
2116 public $ref;
2117
2123 public $datec;
2124
2130 public $dateo;
2131
2137 public $datev;
2138
2142 public $amount;
2143
2147 public $amount_main_currency;
2148
2152 public $fk_user_author;
2153
2157 public $fk_user_rappro;
2158
2162 public $fk_type;
2163
2167 public $fk_bordereau;
2168
2172 public $fk_account;
2173
2177 public $bank_account_ref;
2178
2182 public $bank_account_label;
2183
2187 public $numero_compte;
2188
2192 public $emetteur;
2193
2197 public $rappro;
2198
2202 public $num_releve;
2203
2207 public $num_chq;
2208
2212 public $bank_chq;
2213
2217 public $label;
2218
2222 public $note;
2223
2229 public $user_rappro;
2230
2231
2237 public function __construct(DoliDB $db)
2238 {
2239 $this->db = $db;
2240 }
2241
2250 public function fetch($rowid, $ref = '', $num = '')
2251 {
2252 // Check parameters
2253 if (empty($rowid) && empty($ref) && empty($num)) {
2254 return -1;
2255 }
2256
2257 $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
2258 $sql .= " b.fk_user_author, b.fk_user_rappro,";
2259 $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
2260 $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
2261 $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
2262 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
2263 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
2264 $sql .= " WHERE b.fk_account = ba.rowid";
2265 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
2266 if ($num) {
2267 $sql .= " AND b.num_chq = '".$this->db->escape($num)."'";
2268 } elseif ($ref) {
2269 $sql .= " AND b.rowid = '".$this->db->escape($ref)."'";
2270 } else {
2271 $sql .= " AND b.rowid = ".((int) $rowid);
2272 }
2273
2274 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
2275 $result = $this->db->query($sql);
2276 if ($result) {
2277 $ret = 0;
2278
2279 $obj = $this->db->fetch_object($result);
2280 if ($obj) {
2281 $this->id = $obj->rowid;
2282 $this->rowid = $obj->rowid;
2283 $this->ref = $obj->rowid;
2284
2285 $this->datec = $this->db->jdate($obj->datec);
2286 $this->datev = $this->db->jdate($obj->datev);
2287 $this->dateo = $this->db->jdate($obj->dateo);
2288 $this->amount = $obj->amount;
2289 $this->label = $obj->label;
2290 $this->note = $obj->note;
2291
2292 $this->fk_user_author = $obj->fk_user_author;
2293 $this->fk_user_rappro = $obj->fk_user_rappro;
2294
2295 $this->fk_type = $obj->fk_type; // Type of transaction
2296 $this->rappro = (int) $obj->rappro;
2297 $this->num_releve = $obj->num_releve;
2298
2299 $this->num_chq = $obj->num_chq;
2300 $this->bank_chq = $obj->banque;
2301 $this->fk_bordereau = $obj->fk_bordereau;
2302
2303 $this->fk_account = $obj->fk_account;
2304 $this->bank_account_ref = $obj->bank_account_ref;
2305 $this->bank_account_label = $obj->bank_account_label;
2306
2307 // Retrieve all extrafield
2308 // fetch optionals attributes and labels
2309 $this->fetch_optionals();
2310
2311 $ret = 1;
2312 }
2313 $this->db->free($result);
2314 return $ret;
2315 } else {
2316 return -1;
2317 }
2318 }
2319
2325 public function insert()
2326 {
2327 $error = 0;
2328
2329 $this->db->begin();
2330
2331 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
2332 $sql .= "datec";
2333 $sql .= ", dateo";
2334 $sql .= ", datev";
2335 $sql .= ", label";
2336 $sql .= ", amount";
2337 $sql .= ", amount_main_currency";
2338 $sql .= ", fk_user_author";
2339 $sql .= ", num_chq";
2340 $sql .= ", fk_account";
2341 $sql .= ", fk_type";
2342 $sql .= ", emetteur,banque";
2343 $sql .= ", rappro";
2344 $sql .= ", numero_compte";
2345 $sql .= ", num_releve";
2346 $sql .= ", note";
2347 $sql .= ") VALUES (";
2348 $sql .= "'".$this->db->idate($this->datec)."'";
2349 $sql .= ", '".$this->db->idate($this->dateo)."'";
2350 $sql .= ", '".$this->db->idate($this->datev)."'";
2351 $sql .= ", '".$this->db->escape($this->label)."'";
2352 $sql .= ", ".price2num($this->amount);
2353 $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
2354 $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
2355 $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
2356 $sql .= ", '".$this->db->escape((string) $this->fk_account)."'";
2357 $sql .= ", '".$this->db->escape($this->fk_type)."'";
2358 $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
2359 $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
2360 $sql .= ", ".(int) $this->rappro;
2361 $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2362 $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2363 $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
2364 $sql .= ")";
2365
2366
2367 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2368 $resql = $this->db->query($sql);
2369 if ($resql) {
2370 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2371 // Actions on extra fields (by external module or standard code)
2372 $result = $this->insertExtraFields();
2373 if ($result < 0) {
2374 $error++;
2375 }
2376 } else {
2377 $error++;
2378 $this->error = $this->db->lasterror();
2379 dol_print_error($this->db);
2380 }
2381
2382 if (!$error) {
2383 $this->db->commit();
2384 return $this->id;
2385 } else {
2386 $this->db->rollback();
2387 return -1 * $error;
2388 }
2389 }
2390
2398 public function delete($user, $notrigger = 0)
2399 {
2400 $nbko = 0;
2401
2402 if ($this->rappro) {
2403 // Protection to avoid any delete of consolidated lines
2404 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2405 return -1;
2406 }
2407
2408 $this->db->begin();
2409
2410 if (!$notrigger) {
2411 // Call trigger
2412 $result = $this->call_trigger('BANKACCOUNTLINE_DELETE', $user);
2413 if ($result < 0) {
2414 $this->db->rollback();
2415 return -1;
2416 }
2417 // End call triggers
2418 }
2419
2420 // Protection to avoid any delete of accounted lines. Protection on by default
2421 if (!getDolGlobalString('BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING')) {
2422 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2423 $resql = $this->db->query($sql);
2424 if ($resql) {
2425 $obj = $this->db->fetch_object($resql);
2426 if ($obj && $obj->nb) {
2427 $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2428 $this->db->rollback();
2429 return -1;
2430 }
2431 } else {
2432 $this->error = $this->db->lasterror();
2433 $this->db->rollback();
2434 return -1;
2435 }
2436 }
2437
2438 // Delete urls
2439 $result = $this->delete_urls($user);
2440 if ($result < 0) {
2441 $nbko++;
2442 }
2443
2444 $sql = "DELETE FROM ".MAIN_DB_PREFIX."category_bankline WHERE lineid=".(int) $this->rowid;
2445 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2446 $result = $this->db->query($sql);
2447 if (!$result) {
2448 $nbko++;
2449 }
2450
2451 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_extrafields WHERE fk_object=".(int) $this->rowid;
2452 $result = $this->db->query($sql);
2453 if (!$result) {
2454 $nbko++;
2455 }
2456
2457 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2458 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2459 $result = $this->db->query($sql);
2460 if (!$result) {
2461 $nbko++;
2462 }
2463
2464 if (!$nbko) {
2465 $this->db->commit();
2466 return 1;
2467 } else {
2468 $this->db->rollback();
2469 return -$nbko;
2470 }
2471 }
2472
2473
2474 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2481 public function delete_urls($user = null)
2482 {
2483 // phpcs:enable
2484 $nbko = 0;
2485
2486 if ($this->rappro) {
2487 // Protection to avoid any delete of consolidated lines
2488 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2489 return -1;
2490 }
2491
2492 $this->db->begin();
2493
2494 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2495 dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
2496 $result = $this->db->query($sql);
2497 if (!$result) {
2498 $nbko++;
2499 }
2500
2501 if (!$nbko) {
2502 $this->db->commit();
2503 return 1;
2504 } else {
2505 $this->db->rollback();
2506 return -$nbko;
2507 }
2508 }
2509
2510
2518 public function update(User $user, $notrigger = 0)
2519 {
2520 $this->db->begin();
2521
2522 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2523 $sql .= " amount = ".price2num($this->amount).",";
2524 $sql .= " datev='".$this->db->idate($this->datev)."',";
2525 $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2526 $sql .= " WHERE rowid = ".((int) $this->rowid);
2527
2528 dol_syslog(get_class($this)."::update", LOG_DEBUG);
2529 $resql = $this->db->query($sql);
2530 if ($resql) {
2531 $this->db->commit();
2532 return 1;
2533 } else {
2534 $this->db->rollback();
2535 $this->error = $this->db->error();
2536 return -1;
2537 }
2538 }
2539
2540
2546 public function updateLabel()
2547 {
2548 $this->db->begin();
2549
2550 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2551 $sql .= " label = '".$this->db->escape($this->label)."'";
2552 $sql .= " WHERE rowid = ".((int) $this->rowid);
2553
2554 dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
2555 $resql = $this->db->query($sql);
2556 if ($resql) {
2557 $this->db->commit();
2558 return 1;
2559 } else {
2560 $this->db->rollback();
2561 $this->error = $this->db->error();
2562 return -1;
2563 }
2564 }
2565
2566
2567 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2576 public function update_conciliation(User $user, $cat, $conciliated = 1)
2577 {
2578 // phpcs:enable
2579 global $conf, $langs;
2580
2581 $this->db->begin();
2582
2583 // Check statement field
2584 if (getDolGlobalString('BANK_STATEMENT_REGEX_RULE')) {
2585 if (!preg_match('/' . getDolGlobalString('BANK_STATEMENT_REGEX_RULE').'/', $this->num_releve)) {
2586 $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", getDolGlobalString('BANK_STATEMENT_REGEX_RULE'));
2587 return -1;
2588 }
2589 }
2590
2591 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2592 $sql .= " rappro = ".((int) $conciliated);
2593 $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2594 if ($conciliated) {
2595 $sql .= ", fk_user_rappro = ".((int) $user->id);
2596 }
2597 $sql .= " WHERE rowid = ".((int) $this->id);
2598
2599 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2600 $resql = $this->db->query($sql);
2601 if ($resql) {
2602 if (!empty($cat) && $cat > 0) {
2603 $sql = "INSERT INTO ".MAIN_DB_PREFIX."category_bankline (";
2604 $sql .= "lineid";
2605 $sql .= ", fk_categ";
2606 $sql .= ") VALUES (";
2607 $sql .= $this->id;
2608 $sql .= ", ".((int) $cat);
2609 $sql .= ")";
2610
2611 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2612 $this->db->query($sql);
2613
2614 // No error check. Can fail if category already affected
2615 // TODO Do no try the insert if link already exists
2616 }
2617
2618 $this->rappro = (int) $conciliated;
2619
2620 $this->db->commit();
2621 return 1;
2622 } else {
2623 $this->db->rollback();
2624 return -1;
2625 }
2626 }
2627
2628
2629 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2637 public function datev_change($rowid, $sign = 1)
2638 {
2639 // phpcs:enable
2640 $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2641 $resql = $this->db->query($sql);
2642 if ($resql) {
2643 $obj = $this->db->fetch_object($resql);
2644 $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2645
2646 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2647 $sql .= " datev = '".$this->db->idate($newdate)."'";
2648 $sql .= " WHERE rowid = ".((int) $rowid);
2649
2650 $result = $this->db->query($sql);
2651 if ($result) {
2652 if ($this->db->affected_rows($result)) {
2653 return 1;
2654 }
2655 } else {
2656 dol_print_error($this->db);
2657 return 0;
2658 }
2659 } else {
2660 dol_print_error($this->db);
2661 }
2662 return 0;
2663 }
2664
2665 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2672 public function datev_next($id)
2673 {
2674 // phpcs:enable
2675 return $this->datev_change($id, 1);
2676 }
2677
2678 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2685 public function datev_previous($id)
2686 {
2687 // phpcs:enable
2688 return $this->datev_change($id, -1);
2689 }
2690
2691
2692 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2700 public function dateo_change($rowid, $sign = 1)
2701 {
2702 // phpcs:enable
2703 $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2704 $resql = $this->db->query($sql);
2705 if ($resql) {
2706 $obj = $this->db->fetch_object($resql);
2707 $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2708
2709 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2710 $sql .= " dateo = '".$this->db->idate($newdate)."'";
2711 $sql .= " WHERE rowid = ".((int) $rowid);
2712
2713 $result = $this->db->query($sql);
2714 if ($result) {
2715 if ($this->db->affected_rows($result)) {
2716 return 1;
2717 }
2718 } else {
2719 dol_print_error($this->db);
2720 return 0;
2721 }
2722 } else {
2723 dol_print_error($this->db);
2724 }
2725 return 0;
2726 }
2727
2728 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2735 public function dateo_next($id)
2736 {
2737 // phpcs:enable
2738 return $this->dateo_change($id, 1);
2739 }
2740
2741 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2748 public function dateo_previous($id)
2749 {
2750 // phpcs:enable
2751 return $this->dateo_change($id, -1);
2752 }
2753
2754
2761 public function info($id)
2762 {
2763 $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2764 $sql .= ' b.fk_user_author, b.fk_user_rappro';
2765 $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2766 $sql .= ' WHERE b.rowid = '.((int) $id);
2767
2768 $result = $this->db->query($sql);
2769 if ($result) {
2770 if ($this->db->num_rows($result)) {
2771 $obj = $this->db->fetch_object($result);
2772
2773 $this->id = $obj->rowid;
2774
2775 $this->user_creation_id = $obj->fk_user_author;
2776 $this->user_rappro = $obj->fk_user_rappro;
2777 $this->date_creation = $this->db->jdate($obj->datec);
2778 $this->date_modification = $this->db->jdate($obj->datem);
2779 //$this->date_rappro = $obj->daterappro; // Not yet managed
2780 }
2781 $this->db->free($result);
2782 } else {
2783 dol_print_error($this->db);
2784 }
2785 }
2786
2787
2797 public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2798 {
2799 global $conf, $langs;
2800
2801 $result = '';
2802
2803 $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2804 $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2805 if ($this->amount) {
2806 $label .= '<br><strong>'.$langs->trans("Amount").':</strong> '.price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
2807 }
2808
2809 $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">';
2810 $linkend = '</a>';
2811
2812 $result .= $linkstart;
2813 if ($withpicto) {
2814 $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);
2815 }
2816 if ($withpicto != 2) {
2817 $result .= ($this->ref ? $this->ref : $this->id);
2818 }
2819
2820 $result .= $linkend;
2821
2822 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2823 $result .= ' <span class="opacitymedium">(';
2824 }
2825 if ($option == 'showall') {
2826 $result .= $langs->trans("BankAccount").': ';
2827 $accountstatic = new Account($this->db);
2828 $accountstatic->id = $this->fk_account;
2829 $accountstatic->ref = $this->bank_account_ref;
2830 $accountstatic->label = $this->bank_account_label;
2831 $result .= $accountstatic->getNomUrl(0).', ';
2832 }
2833 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2834 $result .= $langs->trans("BankLineConciliated").': ';
2835 $result .= yn($this->rappro);
2836 }
2837 if (isModEnabled('accounting') && ($option == 'showall' || $option == 'showconciliatedandaccounted')) {
2838 $sql = "SELECT COUNT(rowid) as nb, MAX(piece_num) as banktransactionid FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
2839 $sql .= " WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2840 $resql = $this->db->query($sql);
2841 if ($resql) {
2842 $obj = $this->db->fetch_object($resql);
2843 if ($obj && $obj->nb) {
2844 $result .= ' - '.$langs->trans("Accounted").': <span title="'.$langs->trans("TransactionNumShort").' '.$obj->banktransactionid.'">'.yn(1).'</span>';
2845 } else {
2846 $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2847 }
2848 }
2849 }
2850 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2851 $result .= ')</span>';
2852 }
2853
2854 return $result;
2855 }
2856
2857
2864 public function getLibStatut($mode = 0)
2865 {
2866 return $this->LibStatut($this->status, $mode);
2867 }
2868
2869 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2877 public function LibStatut($status, $mode = 0)
2878 {
2879 // phpcs:enable
2880 return '';
2881 }
2882
2889 public function getVentilExportCompta($mode = 0)
2890 {
2891 $alreadydispatched = 0;
2892
2893 $type = 'bank';
2894
2895 $sql = " SELECT ".($mode ? 'DISTINCT piece_num' : 'COUNT(ab.rowid)')." as nb";
2896 $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type = '".$this->db->escape($type)."' AND ab.fk_doc = ".((int) $this->id);
2897 $resql = $this->db->query($sql);
2898 if ($resql) {
2899 $obj = $this->db->fetch_object($resql);
2900 if ($obj) {
2901 $alreadydispatched = $obj->nb;
2902 }
2903 } else {
2904 $this->error = $this->db->lasterror();
2905 return -1;
2906 }
2907
2908 if ($alreadydispatched) {
2909 return $alreadydispatched;
2910 }
2911 return 0;
2912 }
2913}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
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:363
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition bank.lib.php:388
checkSwiftForAccount($account=null, $swift=null)
Check SWIFT information for a bank account.
Definition bank.lib.php:342
$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.
call_trigger($triggerName, $user)
Call trigger based on this instance.
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:171
getCountry($searchkey, $withcode='', $dbtouse=null, $outputlangs=null, $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
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_now($mode='auto')
Return date for now.
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...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
div refaddress div address
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:158