dolibarr 23.0.4
account.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
4 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
6 * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@inodbox.com>
7 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8 * Copyright (C) 2015-2016 Marcos García <marcosgdf@gmail.com>
9 * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
10 * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
11 * Copyright (C) 2019 JC Prieto <jcprieto@virtual20.com><prietojc@gmail.com>
12 * Copyright (C) 2022-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' => 'UserAuthor', '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 if (empty($this->rappro)) {
478 return -1;
479 }
480 if ($this->type == Account::TYPE_CASH && !getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
481 return -2;
482 }
483 if ($this->status) {
484 return -3;
485 }
486 return 1;
487 }
488
489
490 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
501 public function add_url_line($line_id, $url_id, $url, $label, $type)
502 {
503 // phpcs:enable
504 // Avoid uk_bank_url collision when the same target (line_id, url_id, type) is linked twice
505 // e.g. two distinct credit transfers for the same employee dispatched on the same bank line.
506 $sqlcheck = "SELECT rowid FROM ".MAIN_DB_PREFIX."bank_url";
507 $sqlcheck .= " WHERE fk_bank = ".((int) $line_id);
508 $sqlcheck .= " AND url_id = ".((int) $url_id);
509 $sqlcheck .= " AND type = '".$this->db->escape($type)."'";
510 $resqlcheck = $this->db->query($sqlcheck);
511 if ($resqlcheck) {
512 $obj = $this->db->fetch_object($resqlcheck);
513 if ($obj) {
514 $this->db->free($resqlcheck);
515 return (int) $obj->rowid;
516 }
517 $this->db->free($resqlcheck);
518 }
519
520 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
521 $sql .= "fk_bank";
522 $sql .= ", url_id";
523 $sql .= ", url"; // deprecated
524 $sql .= ", label";
525 $sql .= ", type";
526 $sql .= ") VALUES (";
527 $sql .= ((int) $line_id);
528 $sql .= ", ".((int) $url_id);
529 $sql .= ", '".$this->db->escape($url)."'"; // deprecated
530 $sql .= ", '".$this->db->escape($label)."'";
531 $sql .= ", '".$this->db->escape($type)."'";
532 $sql .= ")";
533
534 dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
535 if ($this->db->query($sql)) {
536 $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
537 return $rowid;
538 } else {
539 $this->error = $this->db->lasterror();
540 return -1;
541 }
542 }
543
544 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
554 public function get_url($fk_bank = 0, $url_id = 0, $type = '')
555 {
556 // phpcs:enable
557 $lines = array();
558
559 // Check parameters
560 if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) {
561 $this->error = "ErrorBadParameter";
562 return -1;
563 }
564
565 $sql = "SELECT fk_bank, url_id, url, label, type";
566 $sql .= " FROM ".MAIN_DB_PREFIX."bank_url";
567 if ($fk_bank > 0) {
568 $sql .= " WHERE fk_bank = ".((int) $fk_bank);
569 } else {
570 $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'";
571 }
572 $sql .= " ORDER BY type, label";
573
574 dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
575 $result = $this->db->query($sql);
576 if ($result) {
577 $i = 0;
578 $num = $this->db->num_rows($result);
579 while ($i < $num) {
580 $obj = $this->db->fetch_object($result);
581 // Old links (for compatibility)
582 $lines[$i][0] = $obj->url;
583 $lines[$i][1] = $obj->url_id;
584 $lines[$i][2] = $obj->label;
585 $lines[$i][3] = $obj->type;
586 // New links
587 $lines[$i]['url'] = $obj->url;
588 $lines[$i]['url_id'] = $obj->url_id;
589 $lines[$i]['label'] = $obj->label;
590 $lines[$i]['type'] = $obj->type;
591 $lines[$i]['fk_bank'] = $obj->fk_bank;
592 $i++;
593 }
594 } else {
595 dol_print_error($this->db);
596 }
597
598 return $lines;
599 }
600
620 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 = '')
621 {
622 global $langs;
623
624 // Deprecation warning
625 if (is_numeric($oper)) {
626 dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING);
627 }
628
629 if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
630 $this->id = $this->rowid;
631 }
632
633 // Clean parameters
634 $emetteur = trim($emetteur);
635 $banque = trim($banque);
636 $label = trim($label);
637
638 $now = dol_now();
639
640 if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid
641 $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
642 $sql .= " WHERE id = ".((int) $oper);
643 $sql .= " AND entity IN (".getEntity('c_paiement').")";
644 $resql = $this->db->query($sql);
645 if ($resql) {
646 $obj = $this->db->fetch_object($resql);
647 $oper = $obj->code;
648 } else {
649 dol_print_error($this->db, 'Failed to get payment type code');
650 return -1;
651 }
652 }
653
654 // Check parameters
655 if (!$oper) {
656 $this->error = $langs->trans("OperNotDefined");
657 return -1;
658 }
659 if (!$this->id) {
660 $this->error = $langs->trans("ThisIdNotDefined");
661 return -2;
662 }
663 if ($this->type == Account::TYPE_CASH && $oper != 'LIQ') {
664 $this->error = "ErrorCashAccountAcceptsOnlyCashMoney";
665 return -3;
666 }
667
668 $this->db->begin();
669
670 if (is_null($datev) || empty($datev)) {
671 $datev = $date;
672 }
673
674 $accline = new AccountLine($this->db);
675 $accline->datec = $now;
676 $accline->dateo = $date;
677 $accline->datev = $datev;
678 $accline->label = $label;
679 $accline->amount = $amount;
680 $accline->amount_main_currency = $amount_main_currency;
681 $accline->fk_user_author = $user->id;
682 $accline->fk_account = $this->id;
683 $accline->fk_type = $oper;
684 $accline->numero_compte = $accountancycode;
685 $accline->num_releve = $num_releve;
686 $accline->note_private = $note_private;
687
688 if ($num_chq) {
689 $accline->num_chq = $num_chq;
690 }
691
692 if ($emetteur) {
693 $accline->emetteur = $emetteur;
694 }
695
696 if ($banque) {
697 $accline->bank_chq = $banque;
698 }
699
700 if ($accline->insert() > 0) {
701 if ($categorie > 0) {
702 $sql = "INSERT INTO ".MAIN_DB_PREFIX."category_bankline(";
703 $sql .= "lineid, fk_categ";
704 $sql .= ") VALUES (";
705 $sql .= ((int) $accline->id).", ".((int) $categorie);
706 $sql .= ")";
707
708 $result = $this->db->query($sql);
709 if (!$result) {
710 $this->error = $this->db->lasterror();
711 $this->db->rollback();
712
713 return -4;
714 }
715 }
716
717 $this->db->commit();
718
719 return $accline->id;
720 } else {
721 $this->setErrorsFromObject($accline);
722 $this->db->rollback();
723
724 return -5;
725 }
726 }
727
735 public function create($user, $notrigger = 0)
736 {
737 global $langs, $conf;
738
739 $error = 0;
740
741 // Clean parameters
742 if (!$this->min_allowed) {
743 $this->min_allowed = 0;
744 }
745 if (!$this->min_desired) {
746 $this->min_desired = 0;
747 }
748
749 // Check parameters
750 if (empty($this->country_id)) {
751 $langs->load('errors');
752 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
753 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
754 return -1;
755 }
756 if (empty($this->ref)) {
757 $langs->load('errors');
758 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
759 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
760 return -1;
761 }
762 if (empty($this->date_solde)) {
763 $langs->load('errors');
764 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance"));
765 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
766 return -1;
767 }
768
769 // Load libraries to check BAN
770 $balance = $this->balance;
771 if (empty($balance) && !empty($this->solde)) {
772 dol_syslog(get_class($this)."::create solde is deprecated use balance", LOG_NOTICE);
773 $balance = $this->solde;
774 }
775 if (empty($balance)) {
776 $balance = 0;
777 }
778 if (empty($this->status && !empty($this->clos))) {
779 dol_syslog(get_class($this)."::create clos is deprecated use status", LOG_NOTICE);
780 $this->status = $this->clos;
781 }
782
783 if (empty($this->country_id) && !empty($this->country_code)) {
784 $country_id = getCountry($this->country_code, '3');
785 $this->country_id = is_int($country_id) ? $country_id : 0;
786 }
787
788 // Load the library to validate/check a BAN account
789 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
790
791 $now = dol_now();
792
793 $this->db->begin();
794
795 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
796 $sql .= "datec";
797 $sql .= ", ref";
798 $sql .= ", label";
799 $sql .= ", entity";
800 $sql .= ", account_number";
801 $sql .= ", fk_accountancy_journal";
802 $sql .= ", bank";
803 $sql .= ", code_banque";
804 $sql .= ", code_guichet";
805 $sql .= ", number";
806 $sql .= ", cle_rib";
807 $sql .= ", bic";
808 $sql .= ", iban_prefix";
809 $sql .= ", domiciliation";
810 $sql .= ", pti_in_ctti";
811 $sql .= ", proprio";
812 $sql .= ", owner_address";
813 $sql .= ", owner_zip";
814 $sql .= ", owner_town";
815 $sql .= ", owner_country_id";
816 $sql .= ", currency_code";
817 $sql .= ", rappro";
818 $sql .= ", min_allowed";
819 $sql .= ", min_desired";
820 $sql .= ", comment";
821 $sql .= ", state_id";
822 $sql .= ", fk_pays";
823 $sql .= ", ics";
824 $sql .= ", ics_transfer";
825 $sql .= ") VALUES (";
826 $sql .= "'".$this->db->idate($now)."'";
827 $sql .= ", '".$this->db->escape($this->ref)."'";
828 $sql .= ", '".$this->db->escape($this->label)."'";
829 $sql .= ", ".((int) $conf->entity);
830 $sql .= ", '".$this->db->escape($this->account_number)."'";
831 $sql .= ", ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
832 $sql .= ", '".$this->db->escape($this->bank)."'";
833 $sql .= ", '".$this->db->escape($this->code_banque)."'";
834 $sql .= ", '".$this->db->escape($this->code_guichet)."'";
835 $sql .= ", '".$this->db->escape($this->number)."'";
836 $sql .= ", '".$this->db->escape($this->cle_rib)."'";
837 $sql .= ", '".$this->db->escape($this->bic)."'";
838 $sql .= ", '".$this->db->escape($this->iban)."'";
839 $sql .= ", '".$this->db->escape($this->address)."'";
840 $sql .= ", ".((int) $this->pti_in_ctti);
841 $sql .= ", '".$this->db->escape($this->owner_name)."'";
842 $sql .= ", '".$this->db->escape($this->owner_address)."'";
843 $sql .= ", '".$this->db->escape($this->owner_zip)."'";
844 $sql .= ", '".$this->db->escape($this->owner_town)."'";
845 $sql .= ", ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
846 $sql .= ", '".$this->db->escape($this->currency_code)."'";
847 $sql .= ", ".((int) $this->rappro);
848 $sql .= ", ".price2num($this->min_allowed, 'MT');
849 $sql .= ", ".price2num($this->min_desired, 'MT');
850 $sql .= ", '".$this->db->escape($this->comment)."'";
851 $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
852 $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
853 $sql .= ", '".$this->db->escape($this->ics)."'";
854 $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
855 $sql .= ")";
856
857 dol_syslog(get_class($this)."::create", LOG_DEBUG);
858 $resql = $this->db->query($sql);
859 if ($resql) {
860 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
861
862 $result = $this->update($user, 1);
863 if ($result > 0) {
864 $accline = new AccountLine($this->db);
865 $accline->datec = $now;
866 $accline->label = '('.$langs->trans("InitialBankBalance").')';
867 $accline->amount = (float) price2num($balance);
868 $accline->fk_user_author = $user->id;
869 $accline->fk_account = $this->id;
870 $accline->datev = $this->date_solde;
871 $accline->dateo = $this->date_solde;
872 $accline->fk_type = 'SOLD';
873
874 if ($accline->insert() < 0) {
875 $error++;
876 $this->setErrorsFromObject($accline);
877 }
878
879 if (!$error) {
880 $result = $this->insertExtraFields();
881 if ($result < 0) {
882 $error++;
883 }
884 }
885
886 if (!$error && !$notrigger) {
887 // Call trigger
888 $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
889 if ($result < 0) {
890 $error++;
891 }
892 // End call triggers
893 }
894 } else {
895 $error++;
896 }
897 } else {
898 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
899 $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
900 $error++;
901 } else {
902 $this->error = $this->db->error()." sql=".$sql;
903 $error++;
904 }
905 }
906
907 if (!$error) {
908 $this->db->commit();
909 return $this->id;
910 } else {
911 $this->db->rollback();
912 return -1 * $error;
913 }
914 }
915
923 public function update($user, $notrigger = 0)
924 {
925 global $langs, $conf;
926
927 if (empty($this->country_id) && !empty($this->country_code)) {
928 $country_id = getCountry($this->country_code, '3');
929 $this->country_id = is_int($country_id) ? $country_id : 0;
930 }
931
932 $error = 0;
933
934 $this->db->begin();
935
936 // Check parameters
937 if (empty($this->country_id)) {
938 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
939 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
940 return -1;
941 }
942 if (empty($this->ref)) {
943 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
944 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
945 return -1;
946 }
947 if (!$this->label) {
948 $this->label = "???";
949 }
950
951 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET";
952 $sql .= " ref = '".$this->db->escape($this->ref)."'";
953 $sql .= ",label = '".$this->db->escape($this->label)."'";
954 $sql .= ",courant = ".((int) $this->type);
955 $sql .= ",clos = ".((int) $this->status);
956 $sql .= ",rappro = ".((int) $this->rappro);
957 $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
958 $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
959 $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
960 $sql .= ",bank = '".$this->db->escape($this->bank)."'";
961 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
962 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
963 $sql .= ",number='".$this->db->escape($this->number)."'";
964 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
965 $sql .= ",bic='".$this->db->escape($this->bic)."'";
966 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
967 $sql .= ",domiciliation='".$this->db->escape($this->address)."'";
968 $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
969 $sql .= ",proprio = '".$this->db->escape($this->owner_name)."'";
970 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
971 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
972 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
973 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
974
975 $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
976
977 $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
978 $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
979 $sql .= ",comment = '".$this->db->escape($this->comment)."'";
980
981 $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
982 $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
983 $sql .= ",ics = '".$this->db->escape($this->ics)."'";
984 $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
985
986 $sql .= " WHERE rowid = ".((int) $this->id);
987
988 dol_syslog(get_class($this)."::update", LOG_DEBUG);
989 $result = $this->db->query($sql);
990 if ($result) {
991 // Actions on extra fields (by external module or standard code)
992 if (!$error) {
993 $result = $this->insertExtraFields();
994 if ($result < 0) {
995 $error++;
996 }
997 }
998
999 if (!$error && !empty($this->oldref) && $this->oldref !== $this->ref) {
1000 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'bank/".$this->db->escape($this->ref)."'";
1001 $sql .= " WHERE filepath = 'bank/".$this->db->escape($this->oldref)."' and src_object_type = 'bank_account' and entity = ".((int) $conf->entity);
1002 $resql = $this->db->query($sql);
1003 if (!$resql) {
1004 $error++;
1005 $this->error = $this->db->lasterror();
1006 }
1007
1008 // We rename directory in order not to lose the attachments
1009 $oldref = dol_sanitizeFileName($this->oldref);
1010 $newref = dol_sanitizeFileName($this->ref);
1011 $dirsource = $conf->bank->dir_output.'/'.$oldref;
1012 $dirdest = $conf->bank->dir_output.'/'.$newref;
1013 if (file_exists($dirsource)) {
1014 dol_syslog(get_class($this)."::update rename dir ".$dirsource." into ".$dirdest, LOG_DEBUG);
1015 if (@rename($dirsource, $dirdest)) {
1016 dol_syslog("Rename ok", LOG_DEBUG);
1017 }
1018 }
1019 }
1020
1021 if (!$error && !$notrigger) {
1022 // Call trigger
1023 $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
1024 if ($result < 0) {
1025 $error++;
1026 }
1027 // End call triggers
1028 }
1029 } else {
1030 $error++;
1031 $this->error = $this->db->lasterror();
1032 dol_print_error($this->db);
1033 }
1034
1035 if (!$error) {
1036 $this->db->commit();
1037 return $this->id;
1038 } else {
1039 $this->db->rollback();
1040 return -1 * $error;
1041 }
1042 }
1043
1044
1045 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1052 public function update_bban($user = null)
1053 {
1054 // phpcs:enable
1055 global $conf, $langs;
1056
1057 // Load library to get BAN control function
1058 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1059
1060 dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
1061
1062 // Check parameters
1063 if (!$this->ref) {
1064 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
1065 return -2;
1066 }
1067
1068 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET";
1069 $sql .= " bank = '".$this->db->escape($this->bank)."'";
1070 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
1071 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
1072 $sql .= ",number='".$this->db->escape($this->number)."'";
1073 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
1074 $sql .= ",bic='".$this->db->escape($this->bic)."'";
1075 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
1076 $sql .= ",domiciliation='".$this->db->escape($this->address)."'";
1077 $sql .= ",proprio = '".$this->db->escape($this->owner_name)."'";
1078 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
1079 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
1080 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
1081 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
1082 $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null");
1083 $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null");
1084 $sql .= " WHERE rowid = ".((int) $this->id);
1085 $sql .= " AND entity = ".((int) $conf->entity);
1086
1087 dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
1088
1089 $result = $this->db->query($sql);
1090 if ($result) {
1091 return 1;
1092 } else {
1093 $this->error = $this->db->lasterror();
1094 dol_print_error($this->db);
1095 return -1;
1096 }
1097 }
1098
1099
1107 public function fetch($id, $ref = '')
1108 {
1109 if (empty($id) && empty($ref)) {
1110 $this->error = "ErrorBadParameters";
1111 return -1;
1112 }
1113
1114 $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,";
1115 $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
1116 $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,";
1117 $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
1118 $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
1119 $sql .= " ba.datec as date_creation, ba.tms as date_modification, ba.ics, ba.ics_transfer,";
1120 $sql .= ' c.code as country_code, c.label as country,';
1121 $sql .= ' d.code_departement as state_code, d.nom as state,';
1122 $sql .= ' aj.code as accountancy_journal';
1123 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1124 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
1125 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
1126 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid = ba.fk_accountancy_journal';
1127 $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
1128 if ($id) {
1129 $sql .= " AND ba.rowid = ".((int) $id);
1130 }
1131 if ($ref) {
1132 $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
1133 }
1134
1135 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1136 $result = $this->db->query($sql);
1137 if ($result) {
1138 if ($this->db->num_rows($result)) {
1139 $obj = $this->db->fetch_object($result);
1140
1141 $this->id = $obj->rowid;
1142 $this->rowid = $obj->rowid;
1143 $this->entity = $obj->entity;
1144 $this->ref = $obj->ref;
1145 $this->label = $obj->label;
1146 $this->type = $obj->type;
1147 $this->courant = $obj->type;
1148 $this->bank = $obj->bank;
1149 $this->clos = $obj->status;
1150 $this->status = $obj->status;
1151 $this->rappro = $obj->rappro;
1152 $this->url = $obj->url;
1153
1154 $this->code_banque = $obj->code_banque;
1155 $this->code_guichet = $obj->code_guichet;
1156 $this->number = $obj->number;
1157 $this->cle_rib = $obj->cle_rib;
1158 $this->bic = $obj->bic;
1159 $this->iban = $obj->iban;
1160 $this->address = $obj->address;
1161
1162 $this->owner_name = $obj->owner_name;
1163 $this->proprio = $this->owner_name;
1164 $this->owner_address = $obj->owner_address;
1165 $this->owner_zip = $obj->owner_zip;
1166 $this->owner_town = $obj->owner_town;
1167 $this->owner_country_id = $obj->owner_country_id;
1168
1169 $this->pti_in_ctti = $obj->pti_in_ctti;
1170
1171 $this->state_id = $obj->state_id;
1172 $this->state_code = $obj->state_code;
1173 $this->state = $obj->state;
1174
1175 $this->country_id = $obj->country_id;
1176 $this->country_code = $obj->country_code;
1177 $this->country = $obj->country;
1178
1179 $this->account_number = $obj->account_number;
1180 $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
1181 $this->accountancy_journal = $obj->accountancy_journal;
1182
1183 $this->currency_code = $obj->currency_code;
1184 $this->account_currency_code = $obj->currency_code;
1185 $this->min_allowed = $obj->min_allowed;
1186 $this->min_desired = $obj->min_desired;
1187 $this->comment = $obj->comment;
1188
1189 $this->date_creation = $this->db->jdate($obj->date_creation);
1190 $this->date_modification = $this->db->jdate($obj->date_modification);
1191
1192 $this->ics = $obj->ics;
1193 $this->ics_transfer = $obj->ics_transfer;
1194
1195 // Retrieve all extrafield
1196 // fetch optionals attributes and labels
1197 $this->fetch_optionals();
1198
1199 return 1;
1200 } else {
1201 return 0;
1202 }
1203 } else {
1204 $this->error = $this->db->lasterror();
1205 $this->errors[] = $this->error;
1206 return -1;
1207 }
1208 }
1209
1220 public function setCategories($categories)
1221 {
1222 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1223 return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
1224 }
1225
1233 public function delete($user, $notrigger = 0)
1234 {
1235 $error = 0;
1236
1237 $this->db->begin();
1238
1239 // @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
1240
1241 // Delete link between tag and bank account
1242 if (!$error) {
1243 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1244 $sql .= " WHERE fk_account = ".((int) $this->id);
1245
1246 $resql = $this->db->query($sql);
1247 if (!$resql) {
1248 $error++;
1249 $this->error = "Error ".$this->db->lasterror();
1250 }
1251 }
1252
1253 if (!$error) {
1254 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
1255 $sql .= " WHERE rowid = ".((int) $this->id);
1256
1257 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1258 $result = $this->db->query($sql);
1259 if ($result) {
1260 // Remove extrafields
1261 if (!$error) {
1262 $result = $this->deleteExtraFields();
1263 if ($result < 0) {
1264 $error++;
1265 dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1266 }
1267 }
1268 } else {
1269 $error++;
1270 $this->error = "Error ".$this->db->lasterror();
1271 }
1272 }
1273
1274 if (!$error) {
1275 $this->db->commit();
1276 return 1;
1277 } else {
1278 $this->db->rollback();
1279 return -1;
1280 }
1281 }
1282
1283
1290 public function getLibStatut($mode = 0)
1291 {
1292 return $this->LibStatut($this->status, $mode);
1293 }
1294
1295 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1303 public function LibStatut($status, $mode = 0)
1304 {
1305 // phpcs:enable
1306 global $langs;
1307 $langs->load('banks');
1308
1309 if ($status == self::STATUS_OPEN) {
1310 $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1311 $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1312 $statusType = 'status4';
1313 } else {
1314 $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1315 $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1316 $statusType = 'status5';
1317 }
1318
1319 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1320 }
1321
1322
1323 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1329 public function can_be_deleted()
1330 {
1331 // phpcs:enable
1332 $can_be_deleted = false;
1333
1334 $sql = "SELECT COUNT(rowid) as nb";
1335 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1336 $sql .= " WHERE fk_account = ".((int) $this->id);
1337
1338 $resql = $this->db->query($sql);
1339 if ($resql) {
1340 $obj = $this->db->fetch_object($resql);
1341 if ($obj->nb <= 1) {
1342 $can_be_deleted = true; // Juste le solde
1343 }
1344 } else {
1345 dol_print_error($this->db);
1346 }
1347 return $can_be_deleted;
1348 }
1349
1350
1356 public function error()
1357 {
1358 return $this->error;
1359 }
1360
1369 public function solde($option = 0, $date_end = '', $field = 'dateo')
1370 {
1371 $solde = 0;
1372
1373 $sql = "SELECT sum(amount) as amount";
1374 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1375 $sql .= " WHERE fk_account = ".((int) $this->id);
1376 if ($option == 1) {
1377 $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1378 }
1379
1380 $resql = $this->db->query($sql);
1381 if ($resql) {
1382 if ($this->db->num_rows($resql)) {
1383 $obj = $this->db->fetch_object($resql);
1384 $solde = $obj->amount;
1385 }
1386 $this->db->free($resql);
1387 } else {
1388 $this->errors[] = $this->db->lasterror;
1389 return -1;
1390 }
1391
1392 return (float) price2num($solde, 'MU');
1393 }
1394
1395 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1403 public function load_board(User $user, $filteraccountid = 0)
1404 {
1405 // phpcs:enable
1406 global $conf, $langs;
1407
1408 if ($user->socid) {
1409 return -1; // protection pour eviter appel par utilisateur externe
1410 }
1411
1412 $sql = "SELECT b.rowid, b.datev as datefin";
1413 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1414 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1415 $sql .= " WHERE b.rappro=0";
1416 $sql .= " AND b.fk_account = ba.rowid";
1417 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1418 $sql .= " AND (ba.rappro = 1 AND ba.courant != " . Account::TYPE_CASH . ")"; // Compte rapprochable
1419 $sql .= " AND clos = 0";
1420 if ($filteraccountid) {
1421 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1422 }
1423
1424 $resql = $this->db->query($sql);
1425 if ($resql) {
1426 $langs->load("banks");
1427 $now = dol_now();
1428
1429 require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1430
1431 $response = new WorkboardResponse();
1432 $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1433 $response->label = $langs->trans("TransactionsToConciliate");
1434 $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1435 $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1436 $response->img = img_object('', "payment");
1437
1438 while ($obj = $this->db->fetch_object($resql)) {
1439 $response->nbtodo++;
1440 if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1441 $response->nbtodolate++;
1442 }
1443 }
1444
1445 return $response;
1446 } else {
1447 dol_print_error($this->db);
1448 $this->error = $this->db->error();
1449 return -1;
1450 }
1451 }
1452
1459 public function loadStateBoard($filteraccountid = 0)
1460 {
1461 global $user;
1462
1463 if ($user->socid) {
1464 return -1; // protection pour eviter appel par utilisateur externe
1465 }
1466
1467 $sql = "SELECT count(b.rowid) as nb";
1468 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1469 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1470 $sql .= " WHERE b.fk_account = ba.rowid";
1471 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1472 $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1473 $sql .= " AND clos = 0";
1474 if ($filteraccountid) {
1475 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1476 }
1477
1478 $resql = $this->db->query($sql);
1479 if ($resql) {
1480 while ($obj = $this->db->fetch_object($resql)) {
1481 $this->nb["banklines"] = $obj->nb;
1482 }
1483 $this->db->free($resql);
1484 return 1;
1485 } else {
1486 dol_print_error($this->db);
1487 $this->error = $this->db->error();
1488 return -1;
1489 }
1490 }
1491
1492
1498 public function countAccountToReconcile()
1499 {
1500 global $user;
1501
1502 //Protection against external users
1503 if ($user->socid) {
1504 return 0;
1505 }
1506
1507 $nb = 0;
1508
1509 $sql = "SELECT COUNT(ba.rowid) as nb";
1510 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1511 $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1512 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1513 if (!getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
1514 $sql .= " AND ba.courant != " . Account::TYPE_CASH;
1515 }
1516
1517 $resql = $this->db->query($sql);
1518 if ($resql) {
1519 $obj = $this->db->fetch_object($resql);
1520 $nb = (int) $obj->nb;
1521 } else {
1522 dol_print_error($this->db);
1523 }
1524
1525 return $nb;
1526 }
1527
1536 public function getTooltipContentArray($params)
1537 {
1538 global $langs;
1539 $langs->loadLangs(['banks', 'compta']);
1540 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1541
1542 $datas = array();
1543
1544 $nofetch = !empty($params['nofetch']);
1545 $pictos = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
1546 if (isset($this->status)) {
1547 $pictos .= ' '.$this->getLibStatut(5);
1548 }
1549 $datas['picto'] = $pictos;
1550 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1551 $datas['label'] = '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1552 $datas['accountnumber'] = '<br><br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
1553 $datas['iban'] = '<br><b>'.$langs->trans('IBAN').':</b> '.getIbanHumanReadable($this);
1554 $datas['bic'] = '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
1555 $datas['accountcurrency'] = '<br><br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
1556
1557 if (isModEnabled('accounting')) {
1558 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1559 $langs->load("accountancy");
1560 $datas['accountaccounting'] = '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
1561 $datas['accountancyjournal'] = '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
1562 }
1563 // show categories for this record only in ajax to not overload lists
1564 if (isModEnabled('category') && !$nofetch) {
1565 require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
1566 $form = new Form($this->db);
1567 $datas['categories'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_ACCOUNT, 1);
1568 }
1569
1570 return $datas;
1571 }
1572
1584 public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1585 {
1586 global $conf, $langs;
1587
1588 if (!empty($conf->dol_no_mouse_hover)) {
1589 $notooltip = 1; // Force disable tooltips
1590 }
1591
1592 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1593
1594 $result = '';
1595 $classfortooltip = 'classfortooltip';
1596 $dataparams = '';
1597 $params = [
1598 'id' => $this->id,
1599 'objecttype' => $this->element,
1600 'option' => $option,
1601 'nofetch' => 1,
1602 ];
1603 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1604 $classfortooltip = 'classforajaxtooltip';
1605 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1606 $label = '';
1607 } else {
1608 $label = implode($this->getTooltipContentArray($params));
1609 }
1610
1611 $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1612 if ($mode == 'transactions') {
1613 $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1614 } elseif ($mode == 'receipts') {
1615 $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1616 }
1617
1618 if ($option != 'nolink') {
1619 // Add param to save lastsearch_values or not
1620 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1621 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1622 $add_save_lastsearch_values = 1;
1623 }
1624 if ($add_save_lastsearch_values) {
1625 $url .= '&save_lastsearch_values=1';
1626 }
1627 }
1628
1629 $linkclose = '';
1630 if (empty($notooltip)) {
1631 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1632 $label = $langs->trans("BankAccount");
1633 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1634 }
1635 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1636 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1637 } else {
1638 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1639 }
1640
1641 if ($option == 'nolink' || empty($url)) {
1642 $linkstart = '<span';
1643 } else {
1644 $linkstart = '<a href="'.$url.'"';
1645 }
1646 $linkstart .= $linkclose.'>';
1647 if ($option == 'nolink' || empty($url)) {
1648 $linkend = '</span>';
1649 } else {
1650 $linkend = '</a>';
1651 }
1652
1653 $result .= $linkstart;
1654
1655 if ($withpicto) {
1656 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1657 }
1658 if ($withpicto != 2) {
1659 $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1660 }
1661 $result .= $linkend;
1662
1663 return $result;
1664 }
1665
1666
1667 // Method after here are common to Account and CompanyBankAccount
1668
1669
1675 public function verif()
1676 {
1677 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1678
1679 $error = 0;
1680
1681 // Call functions to check BAN
1682 if (!checkIbanForAccount($this)) {
1683 $error++;
1684 $this->error = 'IBANNotValid';
1685 }
1686 // Call function to check Swift/BIC.
1687 // A non valid BIC/Swift is a problem if: it is not empty or always a problem if WITHDRAWAL_WITHOUT_BIC is not set).
1688 if (!checkSwiftForAccount($this) && (!empty($this->bic) || !getDolGlobalInt('WITHDRAWAL_WITHOUT_BIC'))) {
1689 $error++;
1690 $this->error = 'SwiftNotValid';
1691 }
1692
1693 if (! $error) {
1694 return 1;
1695 } else {
1696 return 0;
1697 }
1698 }
1699
1705 public function getCountryCode()
1706 {
1707 global $mysoc;
1708
1709 // We return country code of bank account
1710 if (!empty($this->country_code)) {
1711 return $this->country_code;
1712 }
1713
1714 // For backward compatibility, we try to guess country from other information
1715 if (!empty($this->iban)) {
1716 // If IBAN defined, we can know country of account from it
1717 $reg = array();
1718 if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
1719 return $reg[1];
1720 }
1721 }
1722
1723 // If this class is linked to a third party
1724 if (!empty($this->socid)) {
1725 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
1726 $company = new Societe($this->db);
1727 $result = $company->fetch($this->socid);
1728 if (!empty($company->country_code)) {
1729 return $company->country_code;
1730 }
1731 }
1732
1733 // We return country code of managed company
1734 if (!empty($mysoc->country_code)) {
1735 return $mysoc->country_code;
1736 }
1737
1738 return '';
1739 }
1740
1748 public function getBannerAddress($htmlkey, $object)
1749 {
1750 global $conf, $langs;
1751
1752 $out = '';
1753
1754 $outdone = 0;
1755 $coords = $this->getFullAddress(1, ', ', getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT'));
1756 if ($coords) {
1757 if (!empty($conf->use_javascript_ajax)) {
1758 // hideonsmatphone because copyToClipboard call jquery dialog that does not work with jmobile
1759 $out .= '<a href="#" class="hideonsmartphone" onclick="return copyToClipboard(\''.dol_escape_js($coords).'\',\''.dol_escape_js($langs->trans("HelpCopyToClipboard")).'\');">';
1760 $out .= img_picto($langs->trans("Address"), 'map-marker-alt');
1761 $out .= '</a> ';
1762 }
1763 $address = dol_print_address($coords, 'address_'.$htmlkey.'_'.$this->id, $this->element, $this->id, 1, ', ');
1764 if ($address) {
1765 $out .= $address;
1766 $outdone++;
1767 }
1768 $outdone++;
1769 }
1770
1771 return $out;
1772 }
1773
1774
1783 public function useDetailedBBAN()
1784 {
1785 $country_code = $this->getCountryCode();
1786
1787 if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
1788 return 1; // France, Spain, Gabon, ... - Not valid for CH
1789 }
1790 if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
1791 return 2; // Australia, England...
1792 }
1793 return 0;
1794 }
1795
1801 public function needIBAN()
1802 {
1803 global $conf;
1804
1805 if (getDolGlobalString('MAIN_IBAN_IS_NEVER_MANDATORY')) {
1806 return 0;
1807 }
1808
1809 $country_code = $this->getCountryCode();
1810
1811 $country_code_in_EEC = array(
1812 'AT', // Austria
1813 'BE', // Belgium
1814 'BG', // Bulgaria
1815 'CY', // Cyprus
1816 'CZ', // Czech republic
1817 'DE', // Germany
1818 'DK', // Danemark
1819 'EE', // Estonia
1820 'ES', // Spain
1821 'FI', // Finland
1822 'FR', // France
1823 'GB', // United Kingdom
1824 'GR', // Greece
1825 'HR', // Croatia
1826 'NL', // Holland
1827 'HU', // Hungary
1828 'IE', // Ireland
1829 'IM', // Isle of Man - Included in UK
1830 'IT', // Italy
1831 'LT', // Lithuania
1832 'LU', // Luxembourg
1833 'LV', // Latvia
1834 'MC', // Monaco - Included in France
1835 'MT', // Malta
1836 //'NO', // Norway
1837 'PL', // Poland
1838 'PT', // Portugal
1839 'RO', // Romania
1840 'SE', // Sweden
1841 'SK', // Slovakia
1842 'SI', // Slovenia
1843 'UK', // United Kingdom
1844 //'CH', // Switzerland - No. Swizerland in not in EEC
1845 );
1846
1847 if (in_array($country_code, $country_code_in_EEC)) {
1848 return 1; // France, Spain, ...
1849 }
1850 return 0;
1851 }
1852
1858 public function needBIC()
1859 {
1860 if (getDolGlobalString('MAIN_IBAN_IS_NEVER_MANDATORY')) {
1861 return 0;
1862 }
1863
1864 $country_code = $this->getCountryCode();
1865
1866 $country_code_in_EEC = array(
1867 'AD', // Andorra
1868 'BH', // Bahrein
1869 'DK', // Denmark
1870 'FR', // France
1871 'GH', // Ghana
1872 'HU', // Hungary
1873 'JP', // Japan
1874 'LV', // Latvia
1875 'SE', // Sweden
1876 );
1877
1878 if (in_array($country_code, $country_code_in_EEC)) {
1879 return 1; // Andorra, Bahrein, ...
1880 }
1881 return 0;
1882 }
1883
1890 public function info($id)
1891 {
1892 }
1893
1908 public function getFieldsToShow($includeibanbic = 0)
1909 {
1910 //Get the required properties depending on the country
1911 $detailedBBAN = $this->useDetailedBBAN();
1912
1913 if ($detailedBBAN == 0) {
1914 $fieldarray = array(
1915 'BankAccountNumber'
1916 );
1917 } elseif ($detailedBBAN == 2) {
1918 $fieldarray = array(
1919 'BankCode',
1920 'BankAccountNumber'
1921 );
1922 } else {
1923 $fieldarray = self::getAccountNumberOrder();
1924 }
1925
1926 //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1927 if ($includeibanbic) {
1928 $fieldarray[] = 'IBAN';
1929 $fieldarray[] = 'BIC';
1930 }
1931 //}
1932
1933 //Get the order the properties are shown
1934 return $fieldarray;
1935 }
1936
1947 public static function getAccountNumberOrder()
1948 {
1949 global $conf;
1950
1951 $fieldlists = array(
1952 'BankCode',
1953 'DeskCode',
1954 'BankAccountNumber',
1955 'BankAccountNumberKey'
1956 );
1957
1958 if (getDolGlobalString('BANK_SHOW_ORDER_OPTION')) {
1959 if (is_numeric(getDolGlobalString('BANK_SHOW_ORDER_OPTION'))) {
1960 if (getDolGlobalString('BANK_SHOW_ORDER_OPTION') == '1') {
1961 $fieldlists = array(
1962 'BankCode',
1963 'DeskCode',
1964 'BankAccountNumberKey',
1965 'BankAccountNumber'
1966 );
1967 }
1968 } else {
1969 //Replace the old AccountNumber key with the new BankAccountNumber key
1970 $fieldlists = explode(
1971 ' ',
1972 preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
1973 );
1974 }
1975 }
1976
1977 return $fieldlists;
1978 }
1979
1980
1988 public function initAsSpecimen()
1989 {
1990 // Example of IBAN FR7630001007941234567890185
1991 $this->specimen = 1;
1992 $this->ref = 'MBA';
1993 $this->label = 'My Big Company Bank account';
1994 $this->clos = Account::STATUS_OPEN;
1995 $this->type = Account::TYPE_CURRENT;
1996 $this->status = Account::STATUS_OPEN;
1997 $this->code_banque = '30001';
1998 $this->code_guichet = '00794';
1999 $this->number = '12345678901';
2000 $this->cle_rib = '85';
2001 $this->bic = 'AA12';
2002 $this->iban = 'FR7630001007941234567890185';
2003
2004 $this->bank = 'MyBank';
2005 $this->address = 'Rue de Paris';
2006 $this->owner_name = 'Owner';
2007 $this->owner_address = 'Owner address';
2008 $this->owner_zip = 'Owner zip';
2009 $this->owner_town = 'Owner town';
2010 $this->owner_country_id = 1;
2011 $this->country_id = 1;
2012
2013 return 1;
2014 }
2015
2024 public static function replaceThirdparty($dbs, $origin_id, $dest_id)
2025 {
2026 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type = 'company'";
2027
2028 if ($dbs->query($sql)) {
2029 return true;
2030 } else {
2031 //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.
2032 //$this->errors = $dbs->lasterror();
2033 return false;
2034 }
2035 }
2036
2044 public function getKanbanView($option = '', $arraydata = null)
2045 {
2046 global $langs;
2047
2048 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2049
2050 $return = '<div class="box-flex-item box-flex-grow-zero">';
2051 $return .= '<div class="info-box info-box-sm">';
2052 $return .= '<span class="info-box-icon bg-infobox-action">';
2053 $return .= img_picto('', $this->picto);
2054 $return .= '</span>';
2055 $return .= '<div class="info-box-content">';
2056 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
2057 if ($selected >= 0) {
2058 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2059 }
2060 if (property_exists($this, 'type_lib')) {
2061 $return .= '<br><span class="info-box-label opacitymedium" title="'.$this->type_lib[$this->type].'">'.substr($this->type_lib[$this->type], 0, 24).'...</span>';
2062 }
2063 if (method_exists($this, 'solde')) {
2064 $return .= '<br><a href="'.DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id.'">';
2065 $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>';
2066 }
2067 if (method_exists($this, 'getLibStatut')) {
2068 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2069 }
2070 $return .= '</div>';
2071 $return .= '</div>';
2072 $return .= '</div>';
2073 return $return;
2074 }
2075}
2076
2077
2078require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
2079
2083class AccountLine extends CommonObjectLine
2084{
2088 public $db;
2089
2093 public $element = 'bank';
2094
2098 public $table_element = 'bank';
2099
2103 public $picto = 'accountline';
2104
2108 public $id;
2109
2113 public $ref;
2114
2120 public $datec;
2121
2127 public $dateo;
2128
2134 public $datev;
2135
2139 public $amount;
2140
2144 public $amount_main_currency;
2145
2149 public $fk_user_author;
2150
2154 public $fk_user_rappro;
2155
2159 public $fk_type;
2160
2164 public $fk_bordereau;
2165
2169 public $fk_account;
2170
2174 public $bank_account_ref;
2175
2179 public $bank_account_label;
2180
2184 public $numero_compte;
2185
2189 public $emetteur;
2190
2194 public $rappro;
2195
2199 public $num_releve;
2200
2204 public $num_chq;
2205
2209 public $bank_chq;
2210
2214 public $label;
2215
2219 public $note;
2220
2226 public $user_rappro;
2227
2228
2234 public function __construct(DoliDB $db)
2235 {
2236 $this->db = $db;
2237 }
2238
2247 public function fetch($rowid, $ref = '', $num = '')
2248 {
2249 // Check parameters
2250 if (empty($rowid) && empty($ref) && empty($num)) {
2251 return -1;
2252 }
2253
2254 $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
2255 $sql .= " b.fk_user_author, b.fk_user_rappro,";
2256 $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
2257 $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
2258 $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
2259 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
2260 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
2261 $sql .= " WHERE b.fk_account = ba.rowid";
2262 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
2263 if ($num) {
2264 $sql .= " AND b.num_chq = '".$this->db->escape($num)."'";
2265 } elseif ($ref) {
2266 $sql .= " AND b.rowid = '".$this->db->escape($ref)."'";
2267 } else {
2268 $sql .= " AND b.rowid = ".((int) $rowid);
2269 }
2270
2271 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
2272 $result = $this->db->query($sql);
2273 if ($result) {
2274 $ret = 0;
2275
2276 $obj = $this->db->fetch_object($result);
2277 if ($obj) {
2278 $this->id = $obj->rowid;
2279 $this->rowid = $obj->rowid;
2280 $this->ref = $obj->rowid;
2281
2282 $this->datec = $this->db->jdate($obj->datec);
2283 $this->datev = $this->db->jdate($obj->datev);
2284 $this->dateo = $this->db->jdate($obj->dateo);
2285 $this->amount = $obj->amount;
2286 $this->label = $obj->label;
2287 $this->note = $obj->note;
2288
2289 $this->fk_user_author = $obj->fk_user_author;
2290 $this->fk_user_rappro = $obj->fk_user_rappro;
2291
2292 $this->fk_type = $obj->fk_type; // Type of transaction
2293 $this->rappro = (int) $obj->rappro;
2294 $this->num_releve = $obj->num_releve;
2295
2296 $this->num_chq = $obj->num_chq;
2297 $this->bank_chq = $obj->banque;
2298 $this->fk_bordereau = $obj->fk_bordereau;
2299
2300 $this->fk_account = $obj->fk_account;
2301 $this->bank_account_ref = $obj->bank_account_ref;
2302 $this->bank_account_label = $obj->bank_account_label;
2303
2304 // Retrieve all extrafield
2305 // fetch optionals attributes and labels
2306 $this->fetch_optionals();
2307
2308 $ret = 1;
2309 }
2310 $this->db->free($result);
2311 return $ret;
2312 } else {
2313 return -1;
2314 }
2315 }
2316
2322 public function insert()
2323 {
2324 $error = 0;
2325
2326 $this->db->begin();
2327
2328 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
2329 $sql .= "datec";
2330 $sql .= ", dateo";
2331 $sql .= ", datev";
2332 $sql .= ", label";
2333 $sql .= ", amount";
2334 $sql .= ", amount_main_currency";
2335 $sql .= ", fk_user_author";
2336 $sql .= ", num_chq";
2337 $sql .= ", fk_account";
2338 $sql .= ", fk_type";
2339 $sql .= ", emetteur,banque";
2340 $sql .= ", rappro";
2341 $sql .= ", numero_compte";
2342 $sql .= ", num_releve";
2343 $sql .= ", note";
2344 $sql .= ") VALUES (";
2345 $sql .= "'".$this->db->idate($this->datec)."'";
2346 $sql .= ", '".$this->db->idate($this->dateo)."'";
2347 $sql .= ", '".$this->db->idate($this->datev)."'";
2348 $sql .= ", '".$this->db->escape($this->label)."'";
2349 $sql .= ", ".price2num($this->amount);
2350 $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
2351 $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
2352 $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
2353 $sql .= ", '".$this->db->escape((string) $this->fk_account)."'";
2354 $sql .= ", '".$this->db->escape($this->fk_type)."'";
2355 $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
2356 $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
2357 $sql .= ", ".(int) $this->rappro;
2358 $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2359 $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2360 $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
2361 $sql .= ")";
2362
2363
2364 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2365 $resql = $this->db->query($sql);
2366 if ($resql) {
2367 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2368 // Actions on extra fields (by external module or standard code)
2369 $result = $this->insertExtraFields();
2370 if ($result < 0) {
2371 $error++;
2372 }
2373 } else {
2374 $error++;
2375 $this->error = $this->db->lasterror();
2376 dol_print_error($this->db);
2377 }
2378
2379 if (!$error) {
2380 $this->db->commit();
2381 return $this->id;
2382 } else {
2383 $this->db->rollback();
2384 return -1 * $error;
2385 }
2386 }
2387
2395 public function delete($user, $notrigger = 0)
2396 {
2397 $nbko = 0;
2398
2399 if ($this->rappro) {
2400 // Protection to avoid any delete of consolidated lines
2401 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2402 return -1;
2403 }
2404
2405 $this->db->begin();
2406
2407 if (!$notrigger) {
2408 // Call trigger
2409 $result = $this->call_trigger('BANKACCOUNTLINE_DELETE', $user);
2410 if ($result < 0) {
2411 $this->db->rollback();
2412 return -1;
2413 }
2414 // End call triggers
2415 }
2416
2417 // Protection to avoid any delete of accounted lines. Protection on by default
2418 if (!getDolGlobalString('BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING')) {
2419 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2420 $resql = $this->db->query($sql);
2421 if ($resql) {
2422 $obj = $this->db->fetch_object($resql);
2423 if ($obj && $obj->nb) {
2424 $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2425 $this->db->rollback();
2426 return -1;
2427 }
2428 } else {
2429 $this->error = $this->db->lasterror();
2430 $this->db->rollback();
2431 return -1;
2432 }
2433 }
2434
2435 // Delete urls
2436 $result = $this->delete_urls($user);
2437 if ($result < 0) {
2438 $nbko++;
2439 }
2440
2441 $sql = "DELETE FROM ".MAIN_DB_PREFIX."category_bankline WHERE lineid=".(int) $this->rowid;
2442 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2443 $result = $this->db->query($sql);
2444 if (!$result) {
2445 $nbko++;
2446 }
2447
2448 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_extrafields WHERE fk_object=".(int) $this->rowid;
2449 $result = $this->db->query($sql);
2450 if (!$result) {
2451 $nbko++;
2452 }
2453
2454 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2455 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2456 $result = $this->db->query($sql);
2457 if (!$result) {
2458 $nbko++;
2459 }
2460
2461 if (!$nbko) {
2462 $this->db->commit();
2463 return 1;
2464 } else {
2465 $this->db->rollback();
2466 return -$nbko;
2467 }
2468 }
2469
2470
2471 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2478 public function delete_urls($user = null)
2479 {
2480 // phpcs:enable
2481 $nbko = 0;
2482
2483 if ($this->rappro) {
2484 // Protection to avoid any delete of consolidated lines
2485 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2486 return -1;
2487 }
2488
2489 $this->db->begin();
2490
2491 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2492 dol_syslog(get_class($this)."::delete_urls", 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
2515 public function update(User $user, $notrigger = 0)
2516 {
2517 $this->db->begin();
2518
2519 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2520 $sql .= " amount = ".price2num($this->amount).",";
2521 $sql .= " datev='".$this->db->idate($this->datev)."',";
2522 $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2523 $sql .= " WHERE rowid = ".((int) $this->rowid);
2524
2525 dol_syslog(get_class($this)."::update", LOG_DEBUG);
2526 $resql = $this->db->query($sql);
2527 if ($resql) {
2528 $this->db->commit();
2529 return 1;
2530 } else {
2531 $this->db->rollback();
2532 $this->error = $this->db->error();
2533 return -1;
2534 }
2535 }
2536
2537
2543 public function updateLabel()
2544 {
2545 $this->db->begin();
2546
2547 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2548 $sql .= " label = '".$this->db->escape($this->label)."'";
2549 $sql .= " WHERE rowid = ".((int) $this->rowid);
2550
2551 dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
2552 $resql = $this->db->query($sql);
2553 if ($resql) {
2554 $this->db->commit();
2555 return 1;
2556 } else {
2557 $this->db->rollback();
2558 $this->error = $this->db->error();
2559 return -1;
2560 }
2561 }
2562
2563
2564 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2573 public function update_conciliation(User $user, $cat, $conciliated = 1)
2574 {
2575 // phpcs:enable
2576 global $conf, $langs;
2577
2578 $this->db->begin();
2579
2580 // Check statement field
2581 if (getDolGlobalString('BANK_STATEMENT_REGEX_RULE')) {
2582 if (!preg_match('/' . getDolGlobalString('BANK_STATEMENT_REGEX_RULE').'/', $this->num_releve)) {
2583 $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", getDolGlobalString('BANK_STATEMENT_REGEX_RULE'));
2584 $this->db->rollback();
2585 return -1;
2586 }
2587 }
2588
2589 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2590 $sql .= " rappro = ".((int) $conciliated);
2591 $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2592 if ($conciliated) {
2593 $sql .= ", fk_user_rappro = ".((int) $user->id);
2594 }
2595 $sql .= " WHERE rowid = ".((int) $this->id);
2596
2597 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2598 $resql = $this->db->query($sql);
2599 if ($resql) {
2600 if (!empty($cat) && $cat > 0) {
2601 $sql = "INSERT INTO ".MAIN_DB_PREFIX."category_bankline (";
2602 $sql .= "lineid";
2603 $sql .= ", fk_categ";
2604 $sql .= ") VALUES (";
2605 $sql .= $this->id;
2606 $sql .= ", ".((int) $cat);
2607 $sql .= ")";
2608
2609 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2610 $this->db->query($sql);
2611
2612 // No error check. Can fail if category already affected
2613 // TODO Do no try the insert if link already exists
2614 }
2615
2616 $this->rappro = (int) $conciliated;
2617
2618 $this->db->commit();
2619 return 1;
2620 } else {
2621 $this->db->rollback();
2622 return -1;
2623 }
2624 }
2625
2626
2627 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2635 public function datev_change($rowid, $sign = 1)
2636 {
2637 // phpcs:enable
2638 $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2639 $resql = $this->db->query($sql);
2640 if ($resql) {
2641 $obj = $this->db->fetch_object($resql);
2642 $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2643
2644 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2645 $sql .= " datev = '".$this->db->idate($newdate)."'";
2646 $sql .= " WHERE rowid = ".((int) $rowid);
2647
2648 $result = $this->db->query($sql);
2649 if ($result) {
2650 if ($this->db->affected_rows($result)) {
2651 return 1;
2652 }
2653 } else {
2654 dol_print_error($this->db);
2655 return 0;
2656 }
2657 } else {
2658 dol_print_error($this->db);
2659 }
2660 return 0;
2661 }
2662
2663 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2670 public function datev_next($id)
2671 {
2672 // phpcs:enable
2673 return $this->datev_change($id, 1);
2674 }
2675
2676 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2683 public function datev_previous($id)
2684 {
2685 // phpcs:enable
2686 return $this->datev_change($id, -1);
2687 }
2688
2689
2690 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2698 public function dateo_change($rowid, $sign = 1)
2699 {
2700 // phpcs:enable
2701 $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2702 $resql = $this->db->query($sql);
2703 if ($resql) {
2704 $obj = $this->db->fetch_object($resql);
2705 $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2706
2707 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2708 $sql .= " dateo = '".$this->db->idate($newdate)."'";
2709 $sql .= " WHERE rowid = ".((int) $rowid);
2710
2711 $result = $this->db->query($sql);
2712 if ($result) {
2713 if ($this->db->affected_rows($result)) {
2714 return 1;
2715 }
2716 } else {
2717 dol_print_error($this->db);
2718 return 0;
2719 }
2720 } else {
2721 dol_print_error($this->db);
2722 }
2723 return 0;
2724 }
2725
2726 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2733 public function dateo_next($id)
2734 {
2735 // phpcs:enable
2736 return $this->dateo_change($id, 1);
2737 }
2738
2739 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2746 public function dateo_previous($id)
2747 {
2748 // phpcs:enable
2749 return $this->dateo_change($id, -1);
2750 }
2751
2752
2759 public function info($id)
2760 {
2761 $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2762 $sql .= ' b.fk_user_author, b.fk_user_rappro';
2763 $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2764 $sql .= ' WHERE b.rowid = '.((int) $id);
2765
2766 $result = $this->db->query($sql);
2767 if ($result) {
2768 if ($this->db->num_rows($result)) {
2769 $obj = $this->db->fetch_object($result);
2770
2771 $this->id = $obj->rowid;
2772
2773 $this->user_creation_id = $obj->fk_user_author;
2774 $this->user_rappro = $obj->fk_user_rappro;
2775 $this->date_creation = $this->db->jdate($obj->datec);
2776 $this->date_modification = $this->db->jdate($obj->datem);
2777 //$this->date_rappro = $obj->daterappro; // Not yet managed
2778 }
2779 $this->db->free($result);
2780 } else {
2781 dol_print_error($this->db);
2782 }
2783 }
2784
2785
2795 public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2796 {
2797 global $conf, $langs;
2798
2799 $result = '';
2800
2801 $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2802 $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2803 if ($this->amount) {
2804 $label .= '<br><strong>'.$langs->trans("Amount").':</strong> '.price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
2805 }
2806
2807 $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">';
2808 $linkend = '</a>';
2809
2810 $result .= $linkstart;
2811 if ($withpicto) {
2812 $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);
2813 }
2814 if ($withpicto != 2) {
2815 $result .= ($this->ref ? $this->ref : $this->id);
2816 }
2817
2818 $result .= $linkend;
2819
2820 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2821 $result .= ' <span class="opacitymedium">(';
2822 }
2823 if ($option == 'showall') {
2824 $result .= $langs->trans("BankAccount").': ';
2825 $accountstatic = new Account($this->db);
2826 $accountstatic->id = $this->fk_account;
2827 $accountstatic->ref = $this->bank_account_ref;
2828 $accountstatic->label = $this->bank_account_label;
2829 $result .= $accountstatic->getNomUrl(0).', ';
2830 }
2831 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2832 $result .= $langs->trans("BankLineConciliated").': ';
2833 $result .= yn($this->rappro);
2834 }
2835 if (isModEnabled('accounting') && ($option == 'showall' || $option == 'showconciliatedandaccounted')) {
2836 $sql = "SELECT COUNT(rowid) as nb, MAX(piece_num) as banktransactionid FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
2837 $sql .= " WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2838 $resql = $this->db->query($sql);
2839 if ($resql) {
2840 $obj = $this->db->fetch_object($resql);
2841 if ($obj && $obj->nb) {
2842 $result .= ' - '.$langs->trans("Accounted").': <span title="'.$langs->trans("TransactionNumShort").' '.$obj->banktransactionid.'">'.yn(1).'</span>';
2843 } else {
2844 $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2845 }
2846 }
2847 }
2848 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2849 $result .= ')</span>';
2850 }
2851
2852 return $result;
2853 }
2854
2855
2862 public function getLibStatut($mode = 0)
2863 {
2864 return $this->LibStatut($this->status, $mode);
2865 }
2866
2867 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2875 public function LibStatut($status, $mode = 0)
2876 {
2877 // phpcs:enable
2878 return '';
2879 }
2880
2887 public function getVentilExportCompta($mode = 0)
2888 {
2889 $alreadydispatched = 0;
2890
2891 $type = 'bank';
2892
2893 $sql = " SELECT ".($mode ? 'DISTINCT piece_num' : 'COUNT(ab.rowid)')." as nb";
2894 $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type = '".$this->db->escape($type)."' AND ab.fk_doc = ".((int) $this->id);
2895 $resql = $this->db->query($sql);
2896 if ($resql) {
2897 $obj = $this->db->fetch_object($resql);
2898 if ($obj) {
2899 $alreadydispatched = $obj->nb;
2900 }
2901 } else {
2902 $this->error = $this->db->lasterror();
2903 return -1;
2904 }
2905
2906 if ($alreadydispatched) {
2907 return $alreadydispatched;
2908 }
2909 return 0;
2910 }
2911}
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:380
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition bank.lib.php:405
checkSwiftForAccount($account=null, $swift=null)
Check SWIFT information for a bank account.
Definition bank.lib.php:359
$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:171
getCountry($searchkey, $withcode='', $dbtouse=null, $outputlangs=null, $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
global $mysoc
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
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left 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 PaymentTypeShortLIQ right SELECT p pos_change as p datep as p p num_paiement as f pf amount as amount
Definition receipt.php:471
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:132