dolibarr 20.0.0
account.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
4 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
6 * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@inodbox.com>
7 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8 * Copyright (C) 2015-2016 Marcos García <marcosgdf@gmail.com>
9 * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
10 * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
11 * Copyright (C) 2019 JC Prieto <jcprieto@virtual20.com><prietojc@gmail.com>
12 * Copyright (C) 2022-2024 Frédéric France <frederic.france@free.fr>
13 * Copyright (C) 2024 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;
193 public $owner_country_id;
194 public $owner_country_code;
195
202 public $domiciliation;
203
208 public $address;
209 public $state_id;
210 public $state_code;
211 public $state;
212 public $country_id;
213
219 public $type_lib = array();
220
225 public $account_number;
226
230 public $fk_accountancy_journal;
231
235 public $accountancy_journal;
236
241 public $currency_code;
242
249 public $account_currency_code;
250
255 public $min_allowed;
256
261 public $min_desired;
262
267 public $comment;
268
273 public $date_solde;
274
281 public $solde;
282
287 public $balance;
288
293 public $ics;
294
299 public $ics_transfer;
300
304 public $oldref;
305
306
331 // BEGIN MODULEBUILDER PROPERTIES
335 public $fields = array(
336 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
337 'ref' => array('type' => 'varchar(12)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'showoncombobox' => 1, 'position' => 25),
338 'label' => array('type' => 'varchar(30)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30),
339 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 35, 'index' => 1),
340 'bank' => array('type' => 'varchar(60)', 'label' => 'Bank', 'enabled' => 1, 'visible' => -1, 'position' => 40),
341 'code_banque' => array('type' => 'varchar(128)', 'label' => 'Code banque', 'enabled' => 1, 'visible' => -1, 'position' => 45),
342 'code_guichet' => array('type' => 'varchar(6)', 'label' => 'Code guichet', 'enabled' => 1, 'visible' => -1, 'position' => 50),
343 'number' => array('type' => 'varchar(255)', 'label' => 'Number', 'enabled' => 1, 'visible' => -1, 'position' => 55),
344 'cle_rib' => array('type' => 'varchar(5)', 'label' => 'Cle rib', 'enabled' => 1, 'visible' => -1, 'position' => 60),
345 'bic' => array('type' => 'varchar(11)', 'label' => 'Bic', 'enabled' => 1, 'visible' => -1, 'position' => 65),
346 'iban_prefix' => array('type' => 'varchar(34)', 'label' => 'Iban prefix', 'enabled' => 1, 'visible' => -1, 'position' => 70),
347 'country_iban' => array('type' => 'varchar(2)', 'label' => 'Country iban', 'enabled' => 1, 'visible' => -1, 'position' => 75),
348 'cle_iban' => array('type' => 'varchar(2)', 'label' => 'Cle iban', 'enabled' => 1, 'visible' => -1, 'position' => 80),
349 'domiciliation' => array('type' => 'varchar(255)', 'label' => 'Domiciliation', 'enabled' => 1, 'visible' => -1, 'position' => 85),
350 'state_id' => array('type' => 'integer', 'label' => 'StateId', 'enabled' => 1, 'visible' => -1, 'position' => 90),
351 'fk_pays' => array('type' => 'integer', 'label' => 'Country', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 95),
352 'proprio' => array('type' => 'varchar(60)', 'label' => 'Proprio', 'enabled' => 1, 'visible' => -1, 'position' => 100),
353 'owner_address' => array('type' => 'varchar(255)', 'label' => 'Owner address', 'enabled' => 1, 'visible' => -1, 'position' => 105),
354 'owner_zip' => array('type' => 'varchar(25)', 'label' => 'Owner zip', 'enabled' => 1, 'visible' => -1, 'position' => 106),
355 'owner_town' => array('type' => 'varchar(50)', 'label' => 'Owner town', 'enabled' => 1, 'visible' => -1, 'position' => 107),
356 'owner_country_id' => array('type' => 'integer', 'label' => 'Owner country', 'enabled' => 1, 'visible' => -1, 'position' => 108),
357 'courant' => array('type' => 'smallint(6)', 'label' => 'Courant', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 110),
358 'clos' => array('type' => 'smallint(6)', 'label' => 'Clos', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 115),
359 'rappro' => array('type' => 'smallint(6)', 'label' => 'Rappro', 'enabled' => 1, 'visible' => -1, 'position' => 120),
360 'url' => array('type' => 'varchar(128)', 'label' => 'Url', 'enabled' => 1, 'visible' => -1, 'position' => 125),
361 'account_number' => array('type' => 'varchar(32)', 'label' => 'Account number', 'enabled' => 1, 'visible' => -1, 'position' => 130),
362 'fk_accountancy_journal' => array('type' => 'integer', 'label' => 'Accountancy journal ID', 'enabled' => 1, 'visible' => -1, 'position' => 132),
363 'accountancy_journal' => array('type' => 'varchar(20)', 'label' => 'Accountancy journal', 'enabled' => 1, 'visible' => -1, 'position' => 135),
364 'currency_code' => array('type' => 'varchar(3)', 'label' => 'Currency code', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 140),
365 'min_allowed' => array('type' => 'integer', 'label' => 'Min allowed', 'enabled' => 1, 'visible' => -1, 'position' => 145),
366 'min_desired' => array('type' => 'integer', 'label' => 'Min desired', 'enabled' => 1, 'visible' => -1, 'position' => 150),
367 'comment' => array('type' => 'text', 'label' => 'Comment', 'enabled' => 1, 'visible' => -1, 'position' => 155),
368 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 156),
369 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 157),
370 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'position' => 160),
371 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 165),
372 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 170),
373 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 175),
374 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 180),
375 'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 185),
376 );
377 // END MODULEBUILDER PROPERTIES
378
382 const TYPE_CURRENT = 1;
386 const TYPE_CASH = 2;
390 const TYPE_SAVINGS = 0;
391
392
393 const STATUS_OPEN = 0;
394 const STATUS_CLOSED = 1;
395
396
402 public function __construct(DoliDB $db)
403 {
404 global $langs;
405
406 $this->db = $db;
407
408 $this->ismultientitymanaged = 1;
409
410 $this->balance = 0;
411
412 $this->type_lib = array(
413 self::TYPE_SAVINGS => $langs->transnoentitiesnoconv("BankType0"),
414 self::TYPE_CURRENT => $langs->transnoentitiesnoconv("BankType1"),
415 self::TYPE_CASH => $langs->transnoentitiesnoconv("BankType2"),
416 );
417
418 $this->labelStatus = array(
419 self::STATUS_OPEN => $langs->transnoentitiesnoconv("StatusAccountOpened"),
420 self::STATUS_CLOSED => $langs->transnoentitiesnoconv("StatusAccountClosed")
421 );
422 }
423
429 public function __toString()
430 {
431 $string = '';
432 foreach ($this->getFieldsToShow() as $val) {
433 if ($val == 'BankCode') {
434 $string .= $this->code_banque.' ';
435 } elseif ($val == 'BankAccountNumber') {
436 $string .= $this->number.' ';
437 } elseif ($val == 'DeskCode') {
438 $string .= $this->code_guichet.' ';
439 } elseif ($val == 'BankAccountNumberKey') {
440 $string .= $this->cle_rib.' ';
441 } elseif ($val == 'BIC') {
442 $string .= $this->bic.' ';
443 } elseif ($val == 'IBAN') {
444 $string .= $this->iban.' ';
445 }
446 }
447
448 return trim($string);
449 }
450
451
457 public function canBeConciliated()
458 {
459 global $conf;
460
461 if (empty($this->rappro)) {
462 return -1;
463 }
464 if ($this->type == Account::TYPE_CASH && !getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
465 return -2;
466 }
467 if ($this->status) {
468 return -3;
469 }
470 return 1;
471 }
472
473
474 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
485 public function add_url_line($line_id, $url_id, $url, $label, $type)
486 {
487 // phpcs:enable
488 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
489 $sql .= "fk_bank";
490 $sql .= ", url_id";
491 $sql .= ", url"; // deprecated
492 $sql .= ", label";
493 $sql .= ", type";
494 $sql .= ") VALUES (";
495 $sql .= ((int) $line_id);
496 $sql .= ", ".((int) $url_id);
497 $sql .= ", '".$this->db->escape($url)."'"; // deprecated
498 $sql .= ", '".$this->db->escape($label)."'";
499 $sql .= ", '".$this->db->escape($type)."'";
500 $sql .= ")";
501
502 dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
503 if ($this->db->query($sql)) {
504 $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
505 return $rowid;
506 } else {
507 $this->error = $this->db->lasterror();
508 return -1;
509 }
510 }
511
512 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
522 public function get_url($fk_bank = 0, $url_id = 0, $type = '')
523 {
524 // phpcs:enable
525 $lines = array();
526
527 // Check parameters
528 if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) {
529 $this->error = "ErrorBadParameter";
530 return -1;
531 }
532
533 $sql = "SELECT fk_bank, url_id, url, label, type";
534 $sql .= " FROM ".MAIN_DB_PREFIX."bank_url";
535 if ($fk_bank > 0) {
536 $sql .= " WHERE fk_bank = ".((int) $fk_bank);
537 } else {
538 $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'";
539 }
540 $sql .= " ORDER BY type, label";
541
542 dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
543 $result = $this->db->query($sql);
544 if ($result) {
545 $i = 0;
546 $num = $this->db->num_rows($result);
547 while ($i < $num) {
548 $obj = $this->db->fetch_object($result);
549 // Anciens liens (pour compatibilite)
550 $lines[$i][0] = $obj->url;
551 $lines[$i][1] = $obj->url_id;
552 $lines[$i][2] = $obj->label;
553 $lines[$i][3] = $obj->type;
554 // Nouveaux liens
555 $lines[$i]['url'] = $obj->url;
556 $lines[$i]['url_id'] = $obj->url_id;
557 $lines[$i]['label'] = $obj->label;
558 $lines[$i]['type'] = $obj->type;
559 $lines[$i]['fk_bank'] = $obj->fk_bank;
560 $i++;
561 }
562 } else {
563 dol_print_error($this->db);
564 }
565
566 return $lines;
567 }
568
587 public function addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur = '', $banque = '', $accountancycode = '', $datev = null, $num_releve = '', $amount_main_currency = null)
588 {
589 global $langs;
590
591 // Deprecation warning
592 if (is_numeric($oper)) {
593 dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING);
594 }
595
596 if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
597 $this->id = $this->rowid;
598 }
599
600 // Clean parameters
601 $emetteur = trim($emetteur);
602 $banque = trim($banque);
603 $label = trim($label);
604
605 $now = dol_now();
606
607 if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid
608 $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
609 $sql .= " WHERE id = ".((int) $oper);
610 $sql .= " AND entity IN (".getEntity('c_paiement').")";
611 $resql = $this->db->query($sql);
612 if ($resql) {
613 $obj = $this->db->fetch_object($resql);
614 $oper = $obj->code;
615 } else {
616 dol_print_error($this->db, 'Failed to get payment type code');
617 return -1;
618 }
619 }
620
621 // Check parameters
622 if (!$oper) {
623 $this->error = $langs->trans("OperNotDefined");
624 return -1;
625 }
626 if (!$this->id) {
627 $this->error = $langs->trans("ThisIdNotDefined");
628 return -2;
629 }
630 if ($this->type == Account::TYPE_CASH && $oper != 'LIQ') {
631 $this->error = "ErrorCashAccountAcceptsOnlyCashMoney";
632 return -3;
633 }
634
635 $this->db->begin();
636
637 if (is_null($datev) || empty($datev)) {
638 $datev = $date;
639 }
640
641 $accline = new AccountLine($this->db);
642 $accline->datec = $now;
643 $accline->dateo = $date;
644 $accline->datev = $datev;
645 $accline->label = $label;
646 $accline->amount = $amount;
647 $accline->amount_main_currency = $amount_main_currency;
648 $accline->fk_user_author = $user->id;
649 $accline->fk_account = $this->id;
650 $accline->fk_type = $oper;
651 $accline->numero_compte = $accountancycode;
652 $accline->num_releve = $num_releve;
653
654 if ($num_chq) {
655 $accline->num_chq = $num_chq;
656 }
657
658 if ($emetteur) {
659 $accline->emetteur = $emetteur;
660 }
661
662 if ($banque) {
663 $accline->bank_chq = $banque;
664 }
665
666 if ($accline->insert() > 0) {
667 if ($categorie > 0) {
668 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class(";
669 $sql .= "lineid, fk_categ";
670 $sql .= ") VALUES (";
671 $sql .= ((int) $accline->id).", '".$this->db->escape($categorie)."'";
672 $sql .= ")";
673
674 $result = $this->db->query($sql);
675 if (!$result) {
676 $this->error = $this->db->lasterror();
677 $this->db->rollback();
678
679 return -4;
680 }
681 }
682
683 $this->db->commit();
684
685 return $accline->id;
686 } else {
687 $this->setErrorsFromObject($accline);
688 $this->db->rollback();
689
690 return -5;
691 }
692 }
693
701 public function create(User $user, $notrigger = 0)
702 {
703 global $langs, $conf;
704
705 $error = 0;
706
707 // Clean parameters
708 if (!$this->min_allowed) {
709 $this->min_allowed = 0;
710 }
711 if (!$this->min_desired) {
712 $this->min_desired = 0;
713 }
714
715 // Check parameters
716 if (empty($this->country_id)) {
717 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
718 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
719 return -1;
720 }
721 if (empty($this->ref)) {
722 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
723 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
724 return -1;
725 }
726 if (empty($this->date_solde)) {
727 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance"));
728 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
729 return -1;
730 }
731
732 // Load libraries to check BAN
733 $balance = $this->balance;
734 if (empty($balance) && !empty($this->solde)) {
735 $balance = $this->solde;
736 }
737 if (empty($balance)) {
738 $balance = 0;
739 }
740 if (empty($this->address && !empty($this->domiciliation))) {
741 dol_syslog(get_class($this)."::create domiciliation is deprecated use address", LOG_NOTICE);
742 $this->address = $this->domiciliation;
743 }
744 if (empty($this->status && !empty($this->clos))) {
745 dol_syslog(get_class($this)."::create clos is deprecated use status", LOG_NOTICE);
746 $this->status = $this->clos;
747 }
748
749 // Load the library to validate/check a BAN account
750 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
751
752 $now = dol_now();
753
754 $this->db->begin();
755
756 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
757 $sql .= "datec";
758 $sql .= ", ref";
759 $sql .= ", label";
760 $sql .= ", entity";
761 $sql .= ", account_number";
762 $sql .= ", fk_accountancy_journal";
763 $sql .= ", bank";
764 $sql .= ", code_banque";
765 $sql .= ", code_guichet";
766 $sql .= ", number";
767 $sql .= ", cle_rib";
768 $sql .= ", bic";
769 $sql .= ", iban_prefix";
770 $sql .= ", domiciliation";
771 $sql .= ", pti_in_ctti";
772 $sql .= ", proprio";
773 $sql .= ", owner_address";
774 $sql .= ", owner_zip";
775 $sql .= ", owner_town";
776 $sql .= ", owner_country_id";
777 $sql .= ", currency_code";
778 $sql .= ", rappro";
779 $sql .= ", min_allowed";
780 $sql .= ", min_desired";
781 $sql .= ", comment";
782 $sql .= ", state_id";
783 $sql .= ", fk_pays";
784 $sql .= ", ics";
785 $sql .= ", ics_transfer";
786 $sql .= ") VALUES (";
787 $sql .= "'".$this->db->idate($now)."'";
788 $sql .= ", '".$this->db->escape($this->ref)."'";
789 $sql .= ", '".$this->db->escape($this->label)."'";
790 $sql .= ", ".((int) $conf->entity);
791 $sql .= ", '".$this->db->escape($this->account_number)."'";
792 $sql .= ", ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
793 $sql .= ", '".$this->db->escape($this->bank)."'";
794 $sql .= ", '".$this->db->escape($this->code_banque)."'";
795 $sql .= ", '".$this->db->escape($this->code_guichet)."'";
796 $sql .= ", '".$this->db->escape($this->number)."'";
797 $sql .= ", '".$this->db->escape($this->cle_rib)."'";
798 $sql .= ", '".$this->db->escape($this->bic)."'";
799 $sql .= ", '".$this->db->escape($this->iban)."'";
800 $sql .= ", '".$this->db->escape($this->address)."'";
801 $sql .= ", ".((int) $this->pti_in_ctti);
802 $sql .= ", '".$this->db->escape($this->owner_name ? $this->owner_name : $this->proprio)."'";
803 $sql .= ", '".$this->db->escape($this->owner_address)."'";
804 $sql .= ", '".$this->db->escape($this->owner_zip)."'";
805 $sql .= ", '".$this->db->escape($this->owner_town)."'";
806 $sql .= ", ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
807 $sql .= ", '".$this->db->escape($this->currency_code)."'";
808 $sql .= ", ".((int) $this->rappro);
809 $sql .= ", ".price2num($this->min_allowed, 'MT');
810 $sql .= ", ".price2num($this->min_desired, 'MT');
811 $sql .= ", '".$this->db->escape($this->comment)."'";
812 $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
813 $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
814 $sql .= ", '".$this->db->escape($this->ics)."'";
815 $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
816 $sql .= ")";
817
818 dol_syslog(get_class($this)."::create", LOG_DEBUG);
819 $resql = $this->db->query($sql);
820 if ($resql) {
821 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
822
823 $result = $this->update($user, 1);
824 if ($result > 0) {
825 $accline = new AccountLine($this->db);
826 $accline->datec = $now;
827 $accline->label = '('.$langs->trans("InitialBankBalance").')';
828 $accline->amount = (float) price2num($balance);
829 $accline->fk_user_author = $user->id;
830 $accline->fk_account = $this->id;
831 $accline->datev = $this->date_solde;
832 $accline->dateo = $this->date_solde;
833 $accline->fk_type = 'SOLD';
834
835 if ($accline->insert() < 0) {
836 $error++;
837 $this->error = $accline->error;
838 $this->errors = $accline->errors;
839 }
840
841 if (!$error) {
842 $result = $this->insertExtraFields();
843 if ($result < 0) {
844 $error++;
845 }
846 }
847
848 if (!$error && !$notrigger) {
849 // Call trigger
850 $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
851 if ($result < 0) {
852 $error++;
853 }
854 // End call triggers
855 }
856 } else {
857 $error++;
858 }
859 } else {
860 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
861 $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
862 $error++;
863 } else {
864 $this->error = $this->db->error()." sql=".$sql;
865 $error++;
866 }
867 }
868
869 if (!$error) {
870 $this->db->commit();
871 return $this->id;
872 } else {
873 $this->db->rollback();
874 return -1 * $error;
875 }
876 }
877
885 public function update(User $user, $notrigger = 0)
886 {
887 global $langs, $conf;
888
889 $error = 0;
890
891 $this->db->begin();
892
893 // Check parameters
894 if (empty($this->country_id)) {
895 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
896 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
897 return -1;
898 }
899 if (empty($this->ref)) {
900 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
901 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
902 return -1;
903 }
904 if (!$this->label) {
905 $this->label = "???";
906 }
907
908 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET";
909 $sql .= " ref = '".$this->db->escape($this->ref)."'";
910 $sql .= ",label = '".$this->db->escape($this->label)."'";
911 $sql .= ",courant = ".((int) $this->type);
912 $sql .= ",clos = ".((int) $this->status);
913 $sql .= ",rappro = ".((int) $this->rappro);
914 $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
915 $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
916 $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
917 $sql .= ",bank = '".$this->db->escape($this->bank)."'";
918 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
919 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
920 $sql .= ",number='".$this->db->escape($this->number)."'";
921 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
922 $sql .= ",bic='".$this->db->escape($this->bic)."'";
923 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
924 $sql .= ",domiciliation='".$this->db->escape($this->address)."'";
925 $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
926 $sql .= ",proprio = '".$this->db->escape($this->owner_name ? $this->owner_name : $this->proprio)."'";
927 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
928 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
929 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
930 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
931
932 $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
933
934 $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
935 $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
936 $sql .= ",comment = '".$this->db->escape($this->comment)."'";
937
938 $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
939 $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
940 $sql .= ",ics = '".$this->db->escape($this->ics)."'";
941 $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
942
943 $sql .= " WHERE rowid = ".((int) $this->id);
944
945 dol_syslog(get_class($this)."::update", LOG_DEBUG);
946 $result = $this->db->query($sql);
947 if ($result) {
948 // Actions on extra fields (by external module or standard code)
949 if (!$error) {
950 $result = $this->insertExtraFields();
951 if ($result < 0) {
952 $error++;
953 }
954 }
955
956 if (!$error && !empty($this->oldref) && $this->oldref !== $this->ref) {
957 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'bank/".$this->db->escape($this->ref)."'";
958 $sql .= " WHERE filepath = 'bank/".$this->db->escape($this->oldref)."' and src_object_type='bank_account' and entity = ".((int) $conf->entity);
959 $resql = $this->db->query($sql);
960 if (!$resql) {
961 $error++;
962 $this->error = $this->db->lasterror();
963 }
964
965 // We rename directory in order not to lose the attachments
966 $oldref = dol_sanitizeFileName($this->oldref);
967 $newref = dol_sanitizeFileName($this->ref);
968 $dirsource = $conf->bank->dir_output.'/'.$oldref;
969 $dirdest = $conf->bank->dir_output.'/'.$newref;
970 if (file_exists($dirsource)) {
971 dol_syslog(get_class($this)."::update rename dir ".$dirsource." into ".$dirdest, LOG_DEBUG);
972 if (@rename($dirsource, $dirdest)) {
973 dol_syslog("Rename ok", LOG_DEBUG);
974 }
975 }
976 }
977
978 if (!$error && !$notrigger) {
979 // Call trigger
980 $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
981 if ($result < 0) {
982 $error++;
983 }
984 // End call triggers
985 }
986 } else {
987 $error++;
988 $this->error = $this->db->lasterror();
989 dol_print_error($this->db);
990 }
991
992 if (!$error) {
993 $this->db->commit();
994 return $this->id;
995 } else {
996 $this->db->rollback();
997 return -1 * $error;
998 }
999 }
1000
1001
1002 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1009 public function update_bban(User $user = null)
1010 {
1011 // phpcs:enable
1012 global $conf, $langs;
1013
1014 // Load library to get BAN control function
1015 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1016
1017 dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
1018
1019 // Check parameters
1020 if (!$this->ref) {
1021 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
1022 return -2;
1023 }
1024
1025 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET";
1026 $sql .= " bank = '".$this->db->escape($this->bank)."'";
1027 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
1028 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
1029 $sql .= ",number='".$this->db->escape($this->number)."'";
1030 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
1031 $sql .= ",bic='".$this->db->escape($this->bic)."'";
1032 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
1033 $sql .= ",domiciliation='".$this->db->escape($this->address ? $this->address : $this->domiciliation)."'";
1034 $sql .= ",proprio = '".$this->db->escape($this->owner_name ? $this->owner_name : $this->proprio)."'";
1035 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
1036 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
1037 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
1038 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
1039 $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null");
1040 $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null");
1041 $sql .= " WHERE rowid = ".((int) $this->id);
1042 $sql .= " AND entity = ".((int) $conf->entity);
1043
1044 dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
1045
1046 $result = $this->db->query($sql);
1047 if ($result) {
1048 return 1;
1049 } else {
1050 $this->error = $this->db->lasterror();
1051 dol_print_error($this->db);
1052 return -1;
1053 }
1054 }
1055
1056
1064 public function fetch($id, $ref = '')
1065 {
1066 if (empty($id) && empty($ref)) {
1067 $this->error = "ErrorBadParameters";
1068 return -1;
1069 }
1070
1071 $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant as type, ba.clos as status, ba.rappro, ba.url,";
1072 $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
1073 $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,";
1074 $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
1075 $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
1076 $sql .= " ba.datec as date_creation, ba.tms as date_modification, ba.ics, ba.ics_transfer,";
1077 $sql .= ' c.code as country_code, c.label as country,';
1078 $sql .= ' d.code_departement as state_code, d.nom as state,';
1079 $sql .= ' aj.code as accountancy_journal';
1080 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1081 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
1082 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
1083 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal';
1084 $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
1085 if ($id) {
1086 $sql .= " AND ba.rowid = ".((int) $id);
1087 }
1088 if ($ref) {
1089 $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
1090 }
1091
1092 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1093 $result = $this->db->query($sql);
1094 if ($result) {
1095 if ($this->db->num_rows($result)) {
1096 $obj = $this->db->fetch_object($result);
1097
1098 $this->id = $obj->rowid;
1099 $this->rowid = $obj->rowid;
1100 $this->ref = $obj->ref;
1101 $this->label = $obj->label;
1102 $this->type = $obj->type;
1103 $this->courant = $obj->type;
1104 $this->bank = $obj->bank;
1105 $this->clos = $obj->status;
1106 $this->status = $obj->status;
1107 $this->rappro = $obj->rappro;
1108 $this->url = $obj->url;
1109
1110 $this->code_banque = $obj->code_banque;
1111 $this->code_guichet = $obj->code_guichet;
1112 $this->number = $obj->number;
1113 $this->cle_rib = $obj->cle_rib;
1114 $this->bic = $obj->bic;
1115 $this->iban = $obj->iban;
1116 $this->domiciliation = $obj->address;
1117 $this->address = $obj->address;
1118 $this->pti_in_ctti = $obj->pti_in_ctti;
1119 $this->proprio = $obj->owner_name;
1120 $this->owner_name = $obj->owner_name;
1121 $this->owner_address = $obj->owner_address;
1122 $this->owner_zip = $obj->owner_zip;
1123 $this->owner_town = $obj->owner_town;
1124 $this->owner_country_id = $obj->owner_country_id;
1125
1126 $this->state_id = $obj->state_id;
1127 $this->state_code = $obj->state_code;
1128 $this->state = $obj->state;
1129
1130 $this->country_id = $obj->country_id;
1131 $this->country_code = $obj->country_code;
1132 $this->country = $obj->country;
1133
1134 $this->account_number = $obj->account_number;
1135 $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
1136 $this->accountancy_journal = $obj->accountancy_journal;
1137
1138 $this->currency_code = $obj->currency_code;
1139 $this->account_currency_code = $obj->currency_code;
1140 $this->min_allowed = $obj->min_allowed;
1141 $this->min_desired = $obj->min_desired;
1142 $this->comment = $obj->comment;
1143
1144 $this->date_creation = $this->db->jdate($obj->date_creation);
1145 $this->date_modification = $this->db->jdate($obj->date_modification);
1146
1147 $this->ics = $obj->ics;
1148 $this->ics_transfer = $obj->ics_transfer;
1149
1150 // Retrieve all extrafield
1151 // fetch optionals attributes and labels
1152 $this->fetch_optionals();
1153
1154 return 1;
1155 } else {
1156 return 0;
1157 }
1158 } else {
1159 $this->error = $this->db->lasterror();
1160 $this->errors[] = $this->error;
1161 return -1;
1162 }
1163 }
1164
1175 public function setCategories($categories)
1176 {
1177 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1178 return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
1179 }
1180
1188 public function delete(User $user = null, $notrigger = 0)
1189 {
1190 $error = 0;
1191
1192 $this->db->begin();
1193
1194 // @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
1195
1196 // Delete link between tag and bank account
1197 if (!$error) {
1198 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1199 $sql .= " WHERE fk_account = ".((int) $this->id);
1200
1201 $resql = $this->db->query($sql);
1202 if (!$resql) {
1203 $error++;
1204 $this->error = "Error ".$this->db->lasterror();
1205 }
1206 }
1207
1208 if (!$error) {
1209 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
1210 $sql .= " WHERE rowid = ".((int) $this->id);
1211
1212 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1213 $result = $this->db->query($sql);
1214 if ($result) {
1215 // Remove extrafields
1216 if (!$error) {
1217 $result = $this->deleteExtraFields();
1218 if ($result < 0) {
1219 $error++;
1220 dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1221 }
1222 }
1223 } else {
1224 $error++;
1225 $this->error = "Error ".$this->db->lasterror();
1226 }
1227 }
1228
1229 if (!$error) {
1230 $this->db->commit();
1231 return 1;
1232 } else {
1233 $this->db->rollback();
1234 return -1;
1235 }
1236 }
1237
1238
1245 public function getLibStatut($mode = 0)
1246 {
1247 return $this->LibStatut($this->status, $mode);
1248 }
1249
1250 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1258 public function LibStatut($status, $mode = 0)
1259 {
1260 // phpcs:enable
1261 global $langs;
1262 $langs->load('banks');
1263
1264 if ($status == self::STATUS_OPEN) {
1265 $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1266 $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1267 $statusType = 'status4';
1268 } else {
1269 $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1270 $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1271 $statusType = 'status5';
1272 }
1273
1274 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1275 }
1276
1277
1278 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1284 public function can_be_deleted()
1285 {
1286 // phpcs:enable
1287 $can_be_deleted = false;
1288
1289 $sql = "SELECT COUNT(rowid) as nb";
1290 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1291 $sql .= " WHERE fk_account = ".((int) $this->id);
1292
1293 $resql = $this->db->query($sql);
1294 if ($resql) {
1295 $obj = $this->db->fetch_object($resql);
1296 if ($obj->nb <= 1) {
1297 $can_be_deleted = true; // Juste le solde
1298 }
1299 } else {
1300 dol_print_error($this->db);
1301 }
1302 return $can_be_deleted;
1303 }
1304
1305
1311 public function error()
1312 {
1313 return $this->error;
1314 }
1315
1324 public function solde($option = 0, $date_end = '', $field = 'dateo')
1325 {
1326 $solde = 0;
1327
1328 $sql = "SELECT sum(amount) as amount";
1329 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1330 $sql .= " WHERE fk_account = ".((int) $this->id);
1331 if ($option == 1) {
1332 $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1333 }
1334
1335 $resql = $this->db->query($sql);
1336 if ($resql) {
1337 if ($this->db->num_rows($resql)) {
1338 $obj = $this->db->fetch_object($resql);
1339 $solde = $obj->amount;
1340 }
1341 $this->db->free($resql);
1342 } else {
1343 $this->errors[] = $this->db->lasterror;
1344 return -1;
1345 }
1346
1347 return (float) price2num($solde, 'MU');
1348 }
1349
1350 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1358 public function load_board(User $user, $filteraccountid = 0)
1359 {
1360 // phpcs:enable
1361 global $conf, $langs;
1362
1363 if ($user->socid) {
1364 return -1; // protection pour eviter appel par utilisateur externe
1365 }
1366
1367 $sql = "SELECT b.rowid, b.datev as datefin";
1368 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1369 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1370 $sql .= " WHERE b.rappro=0";
1371 $sql .= " AND b.fk_account = ba.rowid";
1372 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1373 $sql .= " AND (ba.rappro = 1 AND ba.courant != " . Account::TYPE_CASH . ")"; // Compte rapprochable
1374 $sql .= " AND clos = 0";
1375 if ($filteraccountid) {
1376 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1377 }
1378
1379 $resql = $this->db->query($sql);
1380 if ($resql) {
1381 $langs->load("banks");
1382 $now = dol_now();
1383
1384 require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1385
1386 $response = new WorkboardResponse();
1387 $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1388 $response->label = $langs->trans("TransactionsToConciliate");
1389 $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1390 $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1391 $response->img = img_object('', "payment");
1392
1393 while ($obj = $this->db->fetch_object($resql)) {
1394 $response->nbtodo++;
1395 if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1396 $response->nbtodolate++;
1397 }
1398 }
1399
1400 return $response;
1401 } else {
1402 dol_print_error($this->db);
1403 $this->error = $this->db->error();
1404 return -1;
1405 }
1406 }
1407
1414 public function loadStateBoard($filteraccountid = 0)
1415 {
1416 global $user;
1417
1418 if ($user->socid) {
1419 return -1; // protection pour eviter appel par utilisateur externe
1420 }
1421
1422 $sql = "SELECT count(b.rowid) as nb";
1423 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1424 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1425 $sql .= " WHERE b.fk_account = ba.rowid";
1426 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1427 $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1428 $sql .= " AND clos = 0";
1429 if ($filteraccountid) {
1430 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1431 }
1432
1433 $resql = $this->db->query($sql);
1434 if ($resql) {
1435 while ($obj = $this->db->fetch_object($resql)) {
1436 $this->nb["banklines"] = $obj->nb;
1437 }
1438 $this->db->free($resql);
1439 return 1;
1440 } else {
1441 dol_print_error($this->db);
1442 $this->error = $this->db->error();
1443 return -1;
1444 }
1445 }
1446
1447
1453 public function countAccountToReconcile()
1454 {
1455 global $user;
1456
1457 //Protection against external users
1458 if ($user->socid) {
1459 return 0;
1460 }
1461
1462 $nb = 0;
1463
1464 $sql = "SELECT COUNT(ba.rowid) as nb";
1465 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1466 $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1467 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1468 if (!getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
1469 $sql .= " AND ba.courant != " . Account::TYPE_CASH;
1470 }
1471
1472 $resql = $this->db->query($sql);
1473 if ($resql) {
1474 $obj = $this->db->fetch_object($resql);
1475 $nb = $obj->nb;
1476 } else {
1477 dol_print_error($this->db);
1478 }
1479
1480 return $nb;
1481 }
1482
1490 public function getTooltipContentArray($params)
1491 {
1492 global $langs;
1493 $langs->loadLangs(['banks', 'compta']);
1494 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1495
1496 $datas = array();
1497
1498 $nofetch = !empty($params['nofetch']);
1499 $pictos = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
1500 if (isset($this->status)) {
1501 $pictos .= ' '.$this->getLibStatut(5);
1502 }
1503 $datas['picto'] = $pictos;
1504 $datas['label'] = '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1505 $datas['accountnumber'] = '<br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
1506 $datas['iban'] = '<br><b>'.$langs->trans('IBAN').':</b> '.getIbanHumanReadable($this);
1507 $datas['bic'] = '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
1508 $datas['accountcurrency'] = '<br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
1509
1510 if (isModEnabled('accounting')) {
1511 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1512 $langs->load("accountancy");
1513 $datas['accountaccounting'] = '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
1514 $datas['accountancyjournal'] = '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
1515 }
1516 // show categories for this record only in ajax to not overload lists
1517 if (isModEnabled('category') && !$nofetch) {
1518 require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
1519 $form = new Form($this->db);
1520 $datas['categories'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_ACCOUNT, 1);
1521 }
1522
1523 return $datas;
1524 }
1525
1537 public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1538 {
1539 global $conf, $langs;
1540
1541 if (!empty($conf->dol_no_mouse_hover)) {
1542 $notooltip = 1; // Force disable tooltips
1543 }
1544
1545 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1546
1547 $result = '';
1548 $classfortooltip = 'classfortooltip';
1549 $dataparams = '';
1550 $params = [
1551 'id' => $this->id,
1552 'objecttype' => $this->element,
1553 'option' => $option,
1554 'nofetch' => 1,
1555 ];
1556 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1557 $classfortooltip = 'classforajaxtooltip';
1558 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1559 $label = '';
1560 } else {
1561 $label = implode($this->getTooltipContentArray($params));
1562 }
1563
1564 $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1565 if ($mode == 'transactions') {
1566 $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1567 } elseif ($mode == 'receipts') {
1568 $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1569 }
1570
1571 if ($option != 'nolink') {
1572 // Add param to save lastsearch_values or not
1573 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1574 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1575 $add_save_lastsearch_values = 1;
1576 }
1577 if ($add_save_lastsearch_values) {
1578 $url .= '&save_lastsearch_values=1';
1579 }
1580 }
1581
1582 $linkclose = '';
1583 if (empty($notooltip)) {
1584 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1585 $label = $langs->trans("BankAccount");
1586 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1587 }
1588 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1589 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1590 } else {
1591 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1592 }
1593
1594 if ($option == 'nolink' || empty($url)) {
1595 $linkstart = '<span';
1596 } else {
1597 $linkstart = '<a href="'.$url.'"';
1598 }
1599 $linkstart .= $linkclose.'>';
1600 if ($option == 'nolink' || empty($url)) {
1601 $linkend = '</span>';
1602 } else {
1603 $linkend = '</a>';
1604 }
1605
1606 $result .= $linkstart;
1607
1608 if ($withpicto) {
1609 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1610 }
1611 if ($withpicto != 2) {
1612 $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1613 }
1614 $result .= $linkend;
1615
1616 return $result;
1617 }
1618
1619
1620 // Method after here are common to Account and CompanyBankAccount
1621
1622
1628 public function verif()
1629 {
1630 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1631
1632 $error = 0;
1633
1634 // Call functions to check BAN
1635 if (!checkIbanForAccount($this)) {
1636 $error++;
1637 $this->error = 'IBANNotValid';
1638 }
1639 if (!checkSwiftForAccount($this)) {
1640 $error++;
1641 $this->error = 'SwiftNotValid';
1642 }
1643
1644 if (! $error) {
1645 return 1;
1646 } else {
1647 return 0;
1648 }
1649 }
1650
1656 public function getCountryCode()
1657 {
1658 global $mysoc;
1659
1660 // We return country code of bank account
1661 if (!empty($this->country_code)) {
1662 return $this->country_code;
1663 }
1664
1665 // For backward compatibility, we try to guess country from other information
1666 if (!empty($this->iban)) {
1667 // If IBAN defined, we can know country of account from it
1668 $reg = array();
1669 if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
1670 return $reg[1];
1671 }
1672 }
1673
1674 // If this class is linked to a third party
1675 if (!empty($this->socid)) {
1676 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
1677 $company = new Societe($this->db);
1678 $result = $company->fetch($this->socid);
1679 if (!empty($company->country_code)) {
1680 return $company->country_code;
1681 }
1682 }
1683
1684 // We return country code of managed company
1685 if (!empty($mysoc->country_code)) {
1686 return $mysoc->country_code;
1687 }
1688
1689 return '';
1690 }
1691
1699 public function getBannerAddress($htmlkey, $object)
1700 {
1701 global $conf, $langs;
1702
1703 $out = '';
1704
1705 $outdone = 0;
1706 $coords = $this->getFullAddress(1, ', ', getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT'));
1707 if ($coords) {
1708 if (!empty($conf->use_javascript_ajax)) {
1709 // hideonsmatphone because copyToClipboard call jquery dialog that does not work with jmobile
1710 $out .= '<a href="#" class="hideonsmartphone" onclick="return copyToClipboard(\''.dol_escape_js($coords).'\',\''.dol_escape_js($langs->trans("HelpCopyToClipboard")).'\');">';
1711 $out .= img_picto($langs->trans("Address"), 'map-marker-alt');
1712 $out .= '</a> ';
1713 }
1714 $address = dol_print_address($coords, 'address_'.$htmlkey.'_'.$this->id, $this->element, $this->id, 1, ', ');
1715 if ($address) {
1716 $out .= $address;
1717 $outdone++;
1718 }
1719 $outdone++;
1720 }
1721
1722 return $out;
1723 }
1724
1725
1734 public function useDetailedBBAN()
1735 {
1736 $country_code = $this->getCountryCode();
1737
1738 if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
1739 return 1; // France, Spain, Gabon, ... - Not valid for CH
1740 }
1741 if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
1742 return 2; // Australia, England...
1743 }
1744 return 0;
1745 }
1746
1752 public function needIBAN()
1753 {
1754 global $conf;
1755
1756 if (getDolGlobalString('MAIN_IBAN_IS_NEVER_MANDATORY')) {
1757 return 0;
1758 }
1759
1760 $country_code = $this->getCountryCode();
1761
1762 $country_code_in_EEC = array(
1763 'AT', // Austria
1764 'BE', // Belgium
1765 'BG', // Bulgaria
1766 'CY', // Cyprus
1767 'CZ', // Czech republic
1768 'DE', // Germany
1769 'DK', // Danemark
1770 'EE', // Estonia
1771 'ES', // Spain
1772 'FI', // Finland
1773 'FR', // France
1774 'GB', // United Kingdom
1775 'GR', // Greece
1776 'HR', // Croatia
1777 'NL', // Holland
1778 'HU', // Hungary
1779 'IE', // Ireland
1780 'IM', // Isle of Man - Included in UK
1781 'IT', // Italy
1782 'LT', // Lithuania
1783 'LU', // Luxembourg
1784 'LV', // Latvia
1785 'MC', // Monaco - Included in France
1786 'MT', // Malta
1787 //'NO', // Norway
1788 'PL', // Poland
1789 'PT', // Portugal
1790 'RO', // Romania
1791 'SE', // Sweden
1792 'SK', // Slovakia
1793 'SI', // Slovenia
1794 'UK', // United Kingdom
1795 //'CH', // Switzerland - No. Swizerland in not in EEC
1796 );
1797
1798 if (in_array($country_code, $country_code_in_EEC)) {
1799 return 1; // France, Spain, ...
1800 }
1801 return 0;
1802 }
1803
1810 public function info($id)
1811 {
1812 }
1813
1828 public function getFieldsToShow($includeibanbic = 0)
1829 {
1830 //Get the required properties depending on the country
1831 $detailedBBAN = $this->useDetailedBBAN();
1832
1833 if ($detailedBBAN == 0) {
1834 $fieldarray = array(
1835 'BankAccountNumber'
1836 );
1837 } elseif ($detailedBBAN == 2) {
1838 $fieldarray = array(
1839 'BankCode',
1840 'BankAccountNumber'
1841 );
1842 } else {
1843 $fieldarray = self::getAccountNumberOrder();
1844 }
1845
1846 //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1847 if ($includeibanbic) {
1848 $fieldarray[] = 'IBAN';
1849 $fieldarray[] = 'BIC';
1850 }
1851 //}
1852
1853 //Get the order the properties are shown
1854 return $fieldarray;
1855 }
1856
1867 public static function getAccountNumberOrder()
1868 {
1869 global $conf;
1870
1871 $fieldlists = array(
1872 'BankCode',
1873 'DeskCode',
1874 'BankAccountNumber',
1875 'BankAccountNumberKey'
1876 );
1877
1878 if (getDolGlobalString('BANK_SHOW_ORDER_OPTION')) {
1879 if (is_numeric(getDolGlobalString('BANK_SHOW_ORDER_OPTION'))) {
1880 if (getDolGlobalString('BANK_SHOW_ORDER_OPTION') == '1') {
1881 $fieldlists = array(
1882 'BankCode',
1883 'DeskCode',
1884 'BankAccountNumberKey',
1885 'BankAccountNumber'
1886 );
1887 }
1888 } else {
1889 //Replace the old AccountNumber key with the new BankAccountNumber key
1890 $fieldlists = explode(
1891 ' ',
1892 preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
1893 );
1894 }
1895 }
1896
1897 return $fieldlists;
1898 }
1899
1900
1908 public function initAsSpecimen()
1909 {
1910 // Example of IBAN FR7630001007941234567890185
1911 $this->specimen = 1;
1912 $this->ref = 'MBA';
1913 $this->label = 'My Big Company Bank account';
1914 $this->courant = Account::TYPE_CURRENT;
1915 $this->clos = Account::STATUS_OPEN;
1916 $this->type = Account::TYPE_CURRENT;
1917 $this->status = Account::STATUS_OPEN;
1918 $this->code_banque = '30001';
1919 $this->code_guichet = '00794';
1920 $this->number = '12345678901';
1921 $this->cle_rib = '85';
1922 $this->bic = 'AA12';
1923 $this->iban = 'FR7630001007941234567890185';
1924
1925 $this->bank = 'MyBank';
1926 $this->address = 'Rue de Paris';
1927 $this->proprio = 'Owner';
1928 $this->owner_name = 'Owner';
1929 $this->owner_address = 'Owner address';
1930 $this->owner_zip = 'Owner zip';
1931 $this->owner_town = 'Owner town';
1932 $this->owner_country_id = 'Owner country_id';
1933 $this->country_id = 1;
1934
1935 return 1;
1936 }
1937
1946 public static function replaceThirdparty($dbs, $origin_id, $dest_id)
1947 {
1948 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'";
1949
1950 if ($dbs->query($sql)) {
1951 return true;
1952 } else {
1953 //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.
1954 //$this->errors = $dbs->lasterror();
1955 return false;
1956 }
1957 }
1958
1966 public function getKanbanView($option = '', $arraydata = null)
1967 {
1968 global $langs;
1969
1970 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1971
1972 $return = '<div class="box-flex-item box-flex-grow-zero">';
1973 $return .= '<div class="info-box info-box-sm">';
1974 $return .= '<span class="info-box-icon bg-infobox-action">';
1975 $return .= img_picto('', $this->picto);
1976 $return .= '</span>';
1977 $return .= '<div class="info-box-content">';
1978 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
1979 if ($selected >= 0) {
1980 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1981 }
1982 if (property_exists($this, 'type_lib')) {
1983 $return .= '<br><span class="info-box-label opacitymedium" title="'.$this->type_lib[$this->type].'">'.substr($this->type_lib[$this->type], 0, 24).'...</span>';
1984 }
1985 if (method_exists($this, 'solde')) {
1986 $return .= '<br><a href="'.DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id.'">';
1987 $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>';
1988 }
1989 if (method_exists($this, 'getLibStatut')) {
1990 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
1991 }
1992 $return .= '</div>';
1993 $return .= '</div>';
1994 $return .= '</div>';
1995 return $return;
1996 }
1997}
1998
1999
2000require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
2001
2005class AccountLine extends CommonObjectLine
2006{
2010 public $db;
2011
2015 public $element = 'bank';
2016
2020 public $table_element = 'bank';
2021
2025 public $picto = 'accountline';
2026
2030 public $id;
2031
2035 public $ref;
2036
2042 public $datec;
2043
2049 public $dateo;
2050
2056 public $datev;
2057
2061 public $amount;
2062
2066 public $amount_main_currency;
2067
2071 public $fk_user_author;
2072
2076 public $fk_user_rappro;
2077
2081 public $fk_type;
2082
2086 public $fk_bordereau;
2087
2091 public $fk_account;
2092
2096 public $bank_account_ref;
2097
2101 public $bank_account_label;
2102
2106 public $numero_compte;
2107
2111 public $emetteur;
2112
2116 public $rappro;
2117
2121 public $num_releve;
2122
2126 public $num_chq;
2127
2131 public $bank_chq;
2132
2136 public $label;
2137
2141 public $note;
2142
2147 public $user_rappro;
2148
2149
2155 public function __construct(DoliDB $db)
2156 {
2157 $this->db = $db;
2158 }
2159
2168 public function fetch($rowid, $ref = '', $num = '')
2169 {
2170 // Check parameters
2171 if (empty($rowid) && empty($ref) && empty($num)) {
2172 return -1;
2173 }
2174
2175 $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
2176 $sql .= " b.fk_user_author, b.fk_user_rappro,";
2177 $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
2178 $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
2179 $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
2180 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
2181 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
2182 $sql .= " WHERE b.fk_account = ba.rowid";
2183 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
2184 if ($num) {
2185 $sql .= " AND b.num_chq = '".$this->db->escape($num)."'";
2186 } elseif ($ref) {
2187 $sql .= " AND b.rowid = '".$this->db->escape($ref)."'";
2188 } else {
2189 $sql .= " AND b.rowid = ".((int) $rowid);
2190 }
2191
2192 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
2193 $result = $this->db->query($sql);
2194 if ($result) {
2195 $ret = 0;
2196
2197 $obj = $this->db->fetch_object($result);
2198 if ($obj) {
2199 $this->id = $obj->rowid;
2200 $this->rowid = $obj->rowid;
2201 $this->ref = $obj->rowid;
2202
2203 $this->datec = $this->db->jdate($obj->datec);
2204 $this->datev = $this->db->jdate($obj->datev);
2205 $this->dateo = $this->db->jdate($obj->dateo);
2206 $this->amount = $obj->amount;
2207 $this->label = $obj->label;
2208 $this->note = $obj->note;
2209
2210 $this->fk_user_author = $obj->fk_user_author;
2211 $this->fk_user_rappro = $obj->fk_user_rappro;
2212
2213 $this->fk_type = $obj->fk_type; // Type of transaction
2214 $this->rappro = (int) $obj->rappro;
2215 $this->num_releve = $obj->num_releve;
2216
2217 $this->num_chq = $obj->num_chq;
2218 $this->bank_chq = $obj->banque;
2219 $this->fk_bordereau = $obj->fk_bordereau;
2220
2221 $this->fk_account = $obj->fk_account;
2222 $this->bank_account_ref = $obj->bank_account_ref;
2223 $this->bank_account_label = $obj->bank_account_label;
2224
2225 // Retrieve all extrafield
2226 // fetch optionals attributes and labels
2227 $this->fetch_optionals();
2228
2229 $ret = 1;
2230 }
2231 $this->db->free($result);
2232 return $ret;
2233 } else {
2234 return -1;
2235 }
2236 }
2237
2243 public function insert()
2244 {
2245 $error = 0;
2246
2247 $this->db->begin();
2248
2249 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
2250 $sql .= "datec";
2251 $sql .= ", dateo";
2252 $sql .= ", datev";
2253 $sql .= ", label";
2254 $sql .= ", amount";
2255 $sql .= ", amount_main_currency";
2256 $sql .= ", fk_user_author";
2257 $sql .= ", num_chq";
2258 $sql .= ", fk_account";
2259 $sql .= ", fk_type";
2260 $sql .= ", emetteur,banque";
2261 $sql .= ", rappro";
2262 $sql .= ", numero_compte";
2263 $sql .= ", num_releve";
2264 $sql .= ") VALUES (";
2265 $sql .= "'".$this->db->idate($this->datec)."'";
2266 $sql .= ", '".$this->db->idate($this->dateo)."'";
2267 $sql .= ", '".$this->db->idate($this->datev)."'";
2268 $sql .= ", '".$this->db->escape($this->label)."'";
2269 $sql .= ", ".price2num($this->amount);
2270 $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
2271 $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
2272 $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
2273 $sql .= ", '".$this->db->escape($this->fk_account)."'";
2274 $sql .= ", '".$this->db->escape($this->fk_type)."'";
2275 $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
2276 $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
2277 $sql .= ", ".(int) $this->rappro;
2278 $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2279 $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2280 $sql .= ")";
2281
2282
2283 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2284 $resql = $this->db->query($sql);
2285 if ($resql) {
2286 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2287 // Actions on extra fields (by external module or standard code)
2288 $result = $this->insertExtraFields();
2289 if ($result < 0) {
2290 $error++;
2291 }
2292 } else {
2293 $error++;
2294 $this->error = $this->db->lasterror();
2295 dol_print_error($this->db);
2296 }
2297
2298 if (!$error) {
2299 $this->db->commit();
2300 return $this->id;
2301 } else {
2302 $this->db->rollback();
2303 return -1 * $error;
2304 }
2305 }
2306
2314 public function delete(User $user = null, $notrigger = 0)
2315 {
2316 $nbko = 0;
2317
2318 if ($this->rappro) {
2319 // Protection to avoid any delete of consolidated lines
2320 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2321 return -1;
2322 }
2323
2324 $this->db->begin();
2325
2326 if (!$notrigger) {
2327 // Call trigger
2328 $result = $this->call_trigger('BANKACCOUNTLINE_DELETE', $user);
2329 if ($result < 0) {
2330 $this->db->rollback();
2331 return -1;
2332 }
2333 // End call triggers
2334 }
2335
2336 // Protection to avoid any delete of accounted lines. Protection on by default
2337 if (!getDolGlobalString('BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING')) {
2338 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2339 $resql = $this->db->query($sql);
2340 if ($resql) {
2341 $obj = $this->db->fetch_object($resql);
2342 if ($obj && $obj->nb) {
2343 $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2344 $this->db->rollback();
2345 return -1;
2346 }
2347 } else {
2348 $this->error = $this->db->lasterror();
2349 $this->db->rollback();
2350 return -1;
2351 }
2352 }
2353
2354 // Delete urls
2355 $result = $this->delete_urls($user);
2356 if ($result < 0) {
2357 $nbko++;
2358 }
2359
2360 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".(int) $this->rowid;
2361 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2362 $result = $this->db->query($sql);
2363 if (!$result) {
2364 $nbko++;
2365 }
2366
2367 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_extrafields WHERE fk_object=".(int) $this->rowid;
2368 $result = $this->db->query($sql);
2369 if (!$result) {
2370 $nbko++;
2371 }
2372
2373 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2374 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2375 $result = $this->db->query($sql);
2376 if (!$result) {
2377 $nbko++;
2378 }
2379
2380 if (!$nbko) {
2381 $this->db->commit();
2382 return 1;
2383 } else {
2384 $this->db->rollback();
2385 return -$nbko;
2386 }
2387 }
2388
2389
2390 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2397 public function delete_urls(User $user = null)
2398 {
2399 // phpcs:enable
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 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2411 dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
2412 $result = $this->db->query($sql);
2413 if (!$result) {
2414 $nbko++;
2415 }
2416
2417 if (!$nbko) {
2418 $this->db->commit();
2419 return 1;
2420 } else {
2421 $this->db->rollback();
2422 return -$nbko;
2423 }
2424 }
2425
2426
2434 public function update(User $user, $notrigger = 0)
2435 {
2436 $this->db->begin();
2437
2438 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2439 $sql .= " amount = ".price2num($this->amount).",";
2440 $sql .= " datev='".$this->db->idate($this->datev)."',";
2441 $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2442 $sql .= " WHERE rowid = ".((int) $this->rowid);
2443
2444 dol_syslog(get_class($this)."::update", LOG_DEBUG);
2445 $resql = $this->db->query($sql);
2446 if ($resql) {
2447 $this->db->commit();
2448 return 1;
2449 } else {
2450 $this->db->rollback();
2451 $this->error = $this->db->error();
2452 return -1;
2453 }
2454 }
2455
2456
2462 public function updateLabel()
2463 {
2464 $this->db->begin();
2465
2466 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2467 $sql .= " label = '".$this->db->escape($this->label)."'";
2468 $sql .= " WHERE rowid = ".((int) $this->rowid);
2469
2470 dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
2471 $resql = $this->db->query($sql);
2472 if ($resql) {
2473 $this->db->commit();
2474 return 1;
2475 } else {
2476 $this->db->rollback();
2477 $this->error = $this->db->error();
2478 return -1;
2479 }
2480 }
2481
2482
2483 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2492 public function update_conciliation(User $user, $cat, $conciliated = 1)
2493 {
2494 // phpcs:enable
2495 global $conf, $langs;
2496
2497 $this->db->begin();
2498
2499 // Check statement field
2500 if (getDolGlobalString('BANK_STATEMENT_REGEX_RULE')) {
2501 if (!preg_match('/' . getDolGlobalString('BANK_STATEMENT_REGEX_RULE').'/', $this->num_releve)) {
2502 $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", getDolGlobalString('BANK_STATEMENT_REGEX_RULE'));
2503 return -1;
2504 }
2505 }
2506
2507 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2508 $sql .= " rappro = ".((int) $conciliated);
2509 $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2510 if ($conciliated) {
2511 $sql .= ", fk_user_rappro = ".$user->id;
2512 }
2513 $sql .= " WHERE rowid = ".((int) $this->id);
2514
2515 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2516 $resql = $this->db->query($sql);
2517 if ($resql) {
2518 if (!empty($cat) && $cat > 0) {
2519 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
2520 $sql .= "lineid";
2521 $sql .= ", fk_categ";
2522 $sql .= ") VALUES (";
2523 $sql .= $this->id;
2524 $sql .= ", ".((int) $cat);
2525 $sql .= ")";
2526
2527 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2528 $this->db->query($sql);
2529
2530 // No error check. Can fail if category already affected
2531 // TODO Do no try the insert if link already exists
2532 }
2533
2534 $this->rappro = (int) $conciliated;
2535
2536 $this->db->commit();
2537 return 1;
2538 } else {
2539 $this->db->rollback();
2540 return -1;
2541 }
2542 }
2543
2544
2545 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2553 public function datev_change($rowid, $sign = 1)
2554 {
2555 // phpcs:enable
2556 $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2557 $resql = $this->db->query($sql);
2558 if ($resql) {
2559 $obj = $this->db->fetch_object($resql);
2560 $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2561
2562 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2563 $sql .= " datev = '".$this->db->idate($newdate)."'";
2564 $sql .= " WHERE rowid = ".((int) $rowid);
2565
2566 $result = $this->db->query($sql);
2567 if ($result) {
2568 if ($this->db->affected_rows($result)) {
2569 return 1;
2570 }
2571 } else {
2572 dol_print_error($this->db);
2573 return 0;
2574 }
2575 } else {
2576 dol_print_error($this->db);
2577 }
2578 return 0;
2579 }
2580
2581 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2588 public function datev_next($id)
2589 {
2590 // phpcs:enable
2591 return $this->datev_change($id, 1);
2592 }
2593
2594 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2601 public function datev_previous($id)
2602 {
2603 // phpcs:enable
2604 return $this->datev_change($id, -1);
2605 }
2606
2607
2608 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2616 public function dateo_change($rowid, $sign = 1)
2617 {
2618 // phpcs:enable
2619 $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2620 $resql = $this->db->query($sql);
2621 if ($resql) {
2622 $obj = $this->db->fetch_object($resql);
2623 $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2624
2625 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2626 $sql .= " dateo = '".$this->db->idate($newdate)."'";
2627 $sql .= " WHERE rowid = ".((int) $rowid);
2628
2629 $result = $this->db->query($sql);
2630 if ($result) {
2631 if ($this->db->affected_rows($result)) {
2632 return 1;
2633 }
2634 } else {
2635 dol_print_error($this->db);
2636 return 0;
2637 }
2638 } else {
2639 dol_print_error($this->db);
2640 }
2641 return 0;
2642 }
2643
2644 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2651 public function dateo_next($id)
2652 {
2653 // phpcs:enable
2654 return $this->dateo_change($id, 1);
2655 }
2656
2657 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2664 public function dateo_previous($id)
2665 {
2666 // phpcs:enable
2667 return $this->dateo_change($id, -1);
2668 }
2669
2670
2677 public function info($id)
2678 {
2679 $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2680 $sql .= ' b.fk_user_author, b.fk_user_rappro';
2681 $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2682 $sql .= ' WHERE b.rowid = '.((int) $id);
2683
2684 $result = $this->db->query($sql);
2685 if ($result) {
2686 if ($this->db->num_rows($result)) {
2687 $obj = $this->db->fetch_object($result);
2688
2689 $this->id = $obj->rowid;
2690
2691 $this->user_creation_id = $obj->fk_user_author;
2692 $this->user_rappro = $obj->fk_user_rappro;
2693 $this->date_creation = $this->db->jdate($obj->datec);
2694 $this->date_modification = $this->db->jdate($obj->datem);
2695 //$this->date_rappro = $obj->daterappro; // Not yet managed
2696 }
2697 $this->db->free($result);
2698 } else {
2699 dol_print_error($this->db);
2700 }
2701 }
2702
2703
2713 public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2714 {
2715 global $conf, $langs;
2716
2717 $result = '';
2718
2719 $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2720 $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2721 if ($this->amount) {
2722 $label .= '<br><strong>'.$langs->trans("Amount").':</strong> '.price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
2723 }
2724
2725 $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">';
2726 $linkend = '</a>';
2727
2728 $result .= $linkstart;
2729 if ($withpicto) {
2730 $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);
2731 }
2732 if ($withpicto != 2) {
2733 $result .= ($this->ref ? $this->ref : $this->id);
2734 }
2735
2736 $result .= $linkend;
2737
2738 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2739 $result .= ' <span class="opacitymedium">(';
2740 }
2741 if ($option == 'showall') {
2742 $result .= $langs->trans("BankAccount").': ';
2743 $accountstatic = new Account($this->db);
2744 $accountstatic->id = $this->fk_account;
2745 $accountstatic->ref = $this->bank_account_ref;
2746 $accountstatic->label = $this->bank_account_label;
2747 $result .= $accountstatic->getNomUrl(0).', ';
2748 }
2749 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2750 $result .= $langs->trans("BankLineConciliated").': ';
2751 $result .= yn($this->rappro);
2752 }
2753 if (isModEnabled('accounting') && ($option == 'showall' || $option == 'showconciliatedandaccounted')) {
2754 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
2755 $sql .= " WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2756 $resql = $this->db->query($sql);
2757 if ($resql) {
2758 $obj = $this->db->fetch_object($resql);
2759 if ($obj && $obj->nb) {
2760 $result .= ' - '.$langs->trans("Accounted").': '.yn(1);
2761 } else {
2762 $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2763 }
2764 }
2765 }
2766 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2767 $result .= ')</span>';
2768 }
2769
2770 return $result;
2771 }
2772
2773
2780 public function getLibStatut($mode = 0)
2781 {
2782 return $this->LibStatut($this->status, $mode);
2783 }
2784
2785 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2793 public function LibStatut($status, $mode = 0)
2794 {
2795 // phpcs:enable
2796 return '';
2797 }
2798
2804 public function getVentilExportCompta()
2805 {
2806 $alreadydispatched = 0;
2807
2808 $type = 'bank';
2809
2810 $sql = " SELECT COUNT(ab.rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type='".$this->db->escape($type)."' AND ab.fk_doc = ".((int) $this->id);
2811 $resql = $this->db->query($sql);
2812 if ($resql) {
2813 $obj = $this->db->fetch_object($resql);
2814 if ($obj) {
2815 $alreadydispatched = $obj->nb;
2816 }
2817 } else {
2818 $this->error = $this->db->lasterror();
2819 return -1;
2820 }
2821
2822 if ($alreadydispatched) {
2823 return 1;
2824 }
2825 return 0;
2826 }
2827}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition security.php:636
checkSwiftForAccount(Account $account=null, $swift=null)
Check SWIFT information for a bank account.
Definition bank.lib.php:284
checkIbanForAccount(Account $account=null, $ibantocheck=null)
Check IBAN number information for a bank account.
Definition bank.lib.php:305
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition bank.lib.php:330
$object ref
Definition info.php:79
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 clicable 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.
update(User $user, $notrigger=0)
Update bank account card.
getCountryCode()
Return account country code.
getLibStatut($mode=0)
Return label of object status.
delete(User $user=null, $notrigger=0)
Delete bank account from database.
getBannerAddress($htmlkey, $object)
Return full address for banner.
error()
Return error.
info($id)
Load miscellaneous information for tab "Info".
const TYPE_CASH
Cash account.
__toString()
Shows the account number in the appropriate format.
const TYPE_SAVINGS
Savings account.
getTooltipContentArray($params)
getTooltipContentArray
canBeConciliated()
Return if a bank account need to be conciliated.
create(User $user, $notrigger=0)
Create bank account into database.
can_be_deleted()
Indicates if an account can be deleted or not (without movements)
update_bban(User $user=null)
Update BBAN (RIB) account fields.
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.
addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur='', $banque='', $accountancycode='', $datev=null, $num_releve='', $amount_main_currency=null)
Add an entry into table ".MAIN_DB_PREFIX."bank.
LibStatut($status, $mode=0)
Return label of given object status.
Class to manage bank transaction lines.
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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
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 '.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
table table fiche title col title div col center btnTitle icon
Automatically calls the icon named with the corresponding "object_" prefix.
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:139
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e rowid
Definition invoice.php:1991