dolibarr 19.0.3
account.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
4 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
6 * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@inodbox.com>
7 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8 * Copyright (C) 2015-2016 Marcos García <marcosgdf@gmail.com>
9 * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
10 * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
11 * Copyright (C) 2019 JC Prieto <jcprieto@virtual20.com><prietojc@gmail.com>
12 * Copyright (C) 2022-2023 Frédéric France <frederic.france@netlogic.fr>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27
34require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
35
36
40class Account extends CommonObject
41{
45 public $element = 'bank_account';
46
50 public $table_element = 'bank_account';
51
56 public $ismultientitymanaged = 1;
57
61 public $picto = 'account';
62
68 public $rowid;
69
74 public $label;
75
80 public $courant;
81
86 public $type;
87
92 public $bank;
93
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
166 public $proprio;
167
172 public $owner_address;
173 public $owner_zip;
174 public $owner_town;
175 public $owner_country_id;
176 public $owner_country_code;
177
182 public $domiciliation; // deprecated, use now address
183 public $address;
184 public $state_id;
185 public $state_code;
186 public $state;
187 public $country_id;
188
194 public $type_lib = array();
195
201 public $status = array();
202
207 public $account_number;
208
212 public $fk_accountancy_journal;
216 public $accountancy_journal;
217
222 public $currency_code;
223
229 public $account_currency_code;
230
235 public $min_allowed;
236
241 public $min_desired;
242
247 public $comment;
248
253 public $date_solde;
254
261 public $solde;
262
267 public $balance;
268
273 public $ics;
274
279 public $ics_transfer;
280
284 public $oldref;
285
286
311 // BEGIN MODULEBUILDER PROPERTIES
315 public $fields = array(
316 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
317 'ref' =>array('type'=>'varchar(12)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>25),
318 'label' =>array('type'=>'varchar(30)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>30),
319 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>35, 'index'=>1),
320 'bank' =>array('type'=>'varchar(60)', 'label'=>'Bank', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
321 'code_banque' =>array('type'=>'varchar(128)', 'label'=>'Code banque', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
322 'code_guichet' =>array('type'=>'varchar(6)', 'label'=>'Code guichet', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
323 'number' =>array('type'=>'varchar(255)', 'label'=>'Number', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
324 'cle_rib' =>array('type'=>'varchar(5)', 'label'=>'Cle rib', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
325 'bic' =>array('type'=>'varchar(11)', 'label'=>'Bic', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
326 'iban_prefix' =>array('type'=>'varchar(34)', 'label'=>'Iban prefix', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
327 'country_iban' =>array('type'=>'varchar(2)', 'label'=>'Country iban', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
328 'cle_iban' =>array('type'=>'varchar(2)', 'label'=>'Cle iban', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
329 'domiciliation' =>array('type'=>'varchar(255)', 'label'=>'Domiciliation', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
330 'state_id' =>array('type'=>'integer', 'label'=>'StateId', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
331 'fk_pays' =>array('type'=>'integer', 'label'=>'Country', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>95),
332 'proprio' =>array('type'=>'varchar(60)', 'label'=>'Proprio', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
333 'owner_address' =>array('type'=>'varchar(255)', 'label'=>'Owner address', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
334 'owner_zip' =>array('type'=>'varchar(25)', 'label'=>'Owner zip', 'enabled'=>1, 'visible'=>-1, 'position'=>106),
335 'owner_town' =>array('type'=>'varchar(50)', 'label'=>'Owner town', 'enabled'=>1, 'visible'=>-1, 'position'=>107),
336 'owner_country_id' =>array('type'=>'integer', 'label'=>'Owner country', 'enabled'=>1, 'visible'=>-1, 'position'=>108),
337 'courant' =>array('type'=>'smallint(6)', 'label'=>'Courant', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>110),
338 'clos' =>array('type'=>'smallint(6)', 'label'=>'Clos', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>115),
339 'rappro' =>array('type'=>'smallint(6)', 'label'=>'Rappro', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
340 'url' =>array('type'=>'varchar(128)', 'label'=>'Url', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
341 'account_number' =>array('type'=>'varchar(32)', 'label'=>'Account number', 'enabled'=>1, 'visible'=>-1, 'position'=>130),
342 'fk_accountancy_journal' =>array('type'=>'integer', 'label'=>'Accountancy journal ID', 'enabled'=>1, 'visible'=>-1, 'position'=>132),
343 'accountancy_journal' =>array('type'=>'varchar(20)', 'label'=>'Accountancy journal', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
344 'currency_code' =>array('type'=>'varchar(3)', 'label'=>'Currency code', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>140),
345 'min_allowed' =>array('type'=>'integer', 'label'=>'Min allowed', 'enabled'=>1, 'visible'=>-1, 'position'=>145),
346 'min_desired' =>array('type'=>'integer', 'label'=>'Min desired', 'enabled'=>1, 'visible'=>-1, 'position'=>150),
347 'comment' =>array('type'=>'text', 'label'=>'Comment', 'enabled'=>1, 'visible'=>-1, 'position'=>155),
348 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>156),
349 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>157),
350 'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>160),
351 'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>165),
352 'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>170),
353 'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>175),
354 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>180),
355 'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
356 );
357 // END MODULEBUILDER PROPERTIES
358
362 const TYPE_CURRENT = 1;
366 const TYPE_CASH = 2;
370 const TYPE_SAVINGS = 0;
371
372
373 const STATUS_OPEN = 0;
374 const STATUS_CLOSED = 1;
375
376
382 public function __construct(DoliDB $db)
383 {
384 global $langs;
385
386 $this->db = $db;
387
388 $this->solde = 0;
389
390 $this->type_lib = array(
391 self::TYPE_SAVINGS => $langs->trans("BankType0"),
392 self::TYPE_CURRENT => $langs->trans("BankType1"),
393 self::TYPE_CASH => $langs->trans("BankType2"),
394 );
395
396 $this->status = array(
397 self::STATUS_OPEN => $langs->trans("StatusAccountOpened"),
398 self::STATUS_CLOSED => $langs->trans("StatusAccountClosed")
399 );
400 }
401
407 public function __toString()
408 {
409 $string = '';
410 foreach ($this->getFieldsToShow() as $val) {
411 if ($val == 'BankCode') {
412 $string .= $this->code_banque.' ';
413 } elseif ($val == 'BankAccountNumber') {
414 $string .= $this->number.' ';
415 } elseif ($val == 'DeskCode') {
416 $string .= $this->code_guichet.' ';
417 } elseif ($val == 'BankAccountNumberKey') {
418 $string .= $this->cle_rib.' ';
419 } elseif ($val == 'BIC') {
420 $string .= $this->bic.' ';
421 } elseif ($val == 'IBAN') {
422 $string .= $this->iban.' ';
423 }
424 }
425
426 return trim($string);
427 }
428
429
435 public function canBeConciliated()
436 {
437 global $conf;
438
439 if (empty($this->rappro)) {
440 return -1;
441 }
442 if ($this->courant == Account::TYPE_CASH && !getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
443 return -2;
444 }
445 if ($this->clos) {
446 return -3;
447 }
448 return 1;
449 }
450
451
452 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
463 public function add_url_line($line_id, $url_id, $url, $label, $type)
464 {
465 // phpcs:enable
466 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
467 $sql .= "fk_bank";
468 $sql .= ", url_id";
469 $sql .= ", url"; // deprecated
470 $sql .= ", label";
471 $sql .= ", type";
472 $sql .= ") VALUES (";
473 $sql .= " ".((int) $line_id);
474 $sql .= ", ".((int) $url_id);
475 $sql .= ", '".$this->db->escape($url)."'"; // dperecated
476 $sql .= ", '".$this->db->escape($label)."'";
477 $sql .= ", '".$this->db->escape($type)."'";
478 $sql .= ")";
479
480 dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
481 if ($this->db->query($sql)) {
482 $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
483 return $rowid;
484 } else {
485 $this->error = $this->db->lasterror();
486 return -1;
487 }
488 }
489
490 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
500 public function get_url($fk_bank = 0, $url_id = 0, $type = '')
501 {
502 // phpcs:enable
503 $lines = array();
504
505 // Check parameters
506 if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) {
507 $this->error = "ErrorBadParameter";
508 return -1;
509 }
510
511 $sql = "SELECT fk_bank, url_id, url, label, type";
512 $sql .= " FROM ".MAIN_DB_PREFIX."bank_url";
513 if ($fk_bank > 0) {
514 $sql .= " WHERE fk_bank = ".((int) $fk_bank);
515 } else {
516 $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'";
517 }
518 $sql .= " ORDER BY type, label";
519
520 dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
521 $result = $this->db->query($sql);
522 if ($result) {
523 $i = 0;
524 $num = $this->db->num_rows($result);
525 while ($i < $num) {
526 $obj = $this->db->fetch_object($result);
527 // Anciens liens (pour compatibilite)
528 $lines[$i][0] = $obj->url;
529 $lines[$i][1] = $obj->url_id;
530 $lines[$i][2] = $obj->label;
531 $lines[$i][3] = $obj->type;
532 // Nouveaux liens
533 $lines[$i]['url'] = $obj->url;
534 $lines[$i]['url_id'] = $obj->url_id;
535 $lines[$i]['label'] = $obj->label;
536 $lines[$i]['type'] = $obj->type;
537 $lines[$i]['fk_bank'] = $obj->fk_bank;
538 $i++;
539 }
540 } else {
541 dol_print_error($this->db);
542 }
543
544 return $lines;
545 }
546
565 public function addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur = '', $banque = '', $accountancycode = '', $datev = null, $num_releve = '', $amount_main_currency = null)
566 {
567 global $langs;
568
569 // Deprecation warning
570 if (is_numeric($oper)) {
571 dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING);
572 }
573
574 if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
575 $this->id = $this->rowid;
576 }
577
578 // Clean parameters
579 $emetteur = trim($emetteur);
580 $banque = trim($banque);
581 $label = trim($label);
582
583 $now = dol_now();
584
585 if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid
586 $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
587 $sql .= " WHERE id = ".((int) $oper);
588 $sql .= " AND entity IN (".getEntity('c_paiement').")";
589 $resql = $this->db->query($sql);
590 if ($resql) {
591 $obj = $this->db->fetch_object($resql);
592 $oper = $obj->code;
593 } else {
594 dol_print_error($this->db, 'Failed to get payment type code');
595 return -1;
596 }
597 }
598
599 // Check parameters
600 if (!$oper) {
601 $this->error = $langs->trans("OperNotDefined");
602 return -1;
603 }
604 if (!$this->id) {
605 $this->error = $langs->trans("ThisIdNotDefined");
606 return -2;
607 }
608 if ($this->courant == Account::TYPE_CASH && $oper != 'LIQ') {
609 $this->error = "ErrorCashAccountAcceptsOnlyCashMoney";
610 return -3;
611 }
612
613 $this->db->begin();
614
615 if (is_null($datev) || empty($datev)) {
616 $datev = $date;
617 }
618
619 $accline = new AccountLine($this->db);
620 $accline->datec = $now;
621 $accline->dateo = $date;
622 $accline->datev = $datev;
623 $accline->label = $label;
624 $accline->amount = $amount;
625 $accline->amount_main_currency = $amount_main_currency;
626 $accline->fk_user_author = $user->id;
627 $accline->fk_account = $this->id;
628 $accline->fk_type = $oper;
629 $accline->numero_compte = $accountancycode;
630 $accline->num_releve = $num_releve;
631
632 if ($num_chq) {
633 $accline->num_chq = $num_chq;
634 }
635
636 if ($emetteur) {
637 $accline->emetteur = $emetteur;
638 }
639
640 if ($banque) {
641 $accline->bank_chq = $banque;
642 }
643
644 if ($accline->insert() > 0) {
645 if ($categorie > 0) {
646 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class(";
647 $sql .= "lineid, fk_categ";
648 $sql .= ") VALUES (";
649 $sql .= ((int) $accline->id).", '".$this->db->escape($categorie)."'";
650 $sql .= ")";
651
652 $result = $this->db->query($sql);
653 if (!$result) {
654 $this->error = $this->db->lasterror();
655 $this->db->rollback();
656
657 return -4;
658 }
659 }
660
661 $this->db->commit();
662
663 return $accline->id;
664 } else {
665 $this->error = $accline->error;
666 $this->errors = $accline->errors;
667 $this->db->rollback();
668
669 return -5;
670 }
671 }
672
680 public function create(User $user, $notrigger = 0)
681 {
682 global $langs, $conf;
683
684 $error = 0;
685
686 // Clean parameters
687 if (!$this->min_allowed) {
688 $this->min_allowed = 0;
689 }
690 if (!$this->min_desired) {
691 $this->min_desired = 0;
692 }
693
694 // Check parameters
695 if (empty($this->country_id)) {
696 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
697 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
698 return -1;
699 }
700 if (empty($this->ref)) {
701 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
702 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
703 return -1;
704 }
705 if (empty($this->date_solde)) {
706 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance"));
707 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
708 return -1;
709 }
710
711 // Load librairies to check BAN
712 $balance = $this->balance;
713 if (empty($balance) && !empty($this->solde)) {
714 $balance = $this->solde;
715 }
716 if (empty($balance)) {
717 $balance = 0;
718 }
719
720 // Load the library to validate/check a BAN account
721 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
722
723 $now = dol_now();
724
725 $this->db->begin();
726
727 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
728 $sql .= "datec";
729 $sql .= ", ref";
730 $sql .= ", label";
731 $sql .= ", entity";
732 $sql .= ", account_number";
733 $sql .= ", fk_accountancy_journal";
734 $sql .= ", bank";
735 $sql .= ", code_banque";
736 $sql .= ", code_guichet";
737 $sql .= ", number";
738 $sql .= ", cle_rib";
739 $sql .= ", bic";
740 $sql .= ", iban_prefix";
741 $sql .= ", domiciliation";
742 $sql .= ", pti_in_ctti";
743 $sql .= ", proprio";
744 $sql .= ", owner_address";
745 $sql .= ", owner_zip";
746 $sql .= ", owner_town";
747 $sql .= ", owner_country_id";
748 $sql .= ", currency_code";
749 $sql .= ", rappro";
750 $sql .= ", min_allowed";
751 $sql .= ", min_desired";
752 $sql .= ", comment";
753 $sql .= ", state_id";
754 $sql .= ", fk_pays";
755 $sql .= ", ics";
756 $sql .= ", ics_transfer";
757 $sql .= ") VALUES (";
758 $sql .= "'".$this->db->idate($now)."'";
759 $sql .= ", '".$this->db->escape($this->ref)."'";
760 $sql .= ", '".$this->db->escape($this->label)."'";
761 $sql .= ", ".((int) $conf->entity);
762 $sql .= ", '".$this->db->escape($this->account_number)."'";
763 $sql .= ", ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
764 $sql .= ", '".$this->db->escape($this->bank)."'";
765 $sql .= ", '".$this->db->escape($this->code_banque)."'";
766 $sql .= ", '".$this->db->escape($this->code_guichet)."'";
767 $sql .= ", '".$this->db->escape($this->number)."'";
768 $sql .= ", '".$this->db->escape($this->cle_rib)."'";
769 $sql .= ", '".$this->db->escape($this->bic)."'";
770 $sql .= ", '".$this->db->escape($this->iban)."'";
771 $sql .= ", '".$this->db->escape($this->domiciliation)."'";
772 $sql .= ", ".((int) $this->pti_in_ctti);
773 $sql .= ", '".$this->db->escape($this->proprio)."'";
774 $sql .= ", '".$this->db->escape($this->owner_address)."'";
775 $sql .= ", '".$this->db->escape($this->owner_zip)."'";
776 $sql .= ", '".$this->db->escape($this->owner_town)."'";
777 $sql .= ", ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
778 $sql .= ", '".$this->db->escape($this->currency_code)."'";
779 $sql .= ", ".((int) $this->rappro);
780 $sql .= ", ".price2num($this->min_allowed, 'MT');
781 $sql .= ", ".price2num($this->min_desired, 'MT');
782 $sql .= ", '".$this->db->escape($this->comment)."'";
783 $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
784 $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
785 $sql .= ", '".$this->db->escape($this->ics)."'";
786 $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
787 $sql .= ")";
788
789 dol_syslog(get_class($this)."::create", LOG_DEBUG);
790 $resql = $this->db->query($sql);
791 if ($resql) {
792 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
793
794 $result = $this->update($user, 1);
795 if ($result > 0) {
796 $accline = new AccountLine($this->db);
797 $accline->datec = $now;
798 $accline->label = '('.$langs->trans("InitialBankBalance").')';
799 $accline->amount = price2num($balance);
800 $accline->fk_user_author = $user->id;
801 $accline->fk_account = $this->id;
802 $accline->datev = $this->date_solde;
803 $accline->dateo = $this->date_solde;
804 $accline->fk_type = 'SOLD';
805
806 if ($accline->insert() < 0) {
807 $error++;
808 $this->error = $accline->error;
809 $this->errors = $accline->errors;
810 }
811
812 if (!$error) {
813 $result = $this->insertExtraFields();
814 if ($result < 0) {
815 $error++;
816 }
817 }
818
819 if (!$error && !$notrigger) {
820 // Call trigger
821 $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
822 if ($result < 0) {
823 $error++;
824 }
825 // End call triggers
826 }
827 } else {
828 $error++;
829 }
830 } else {
831 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
832 $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
833 $error++;
834 } else {
835 $this->error = $this->db->error()." sql=".$sql;
836 $error++;
837 }
838 }
839
840 if (!$error) {
841 $this->db->commit();
842 return $this->id;
843 } else {
844 $this->db->rollback();
845 return -1 * $error;
846 }
847 }
848
856 public function update(User $user, $notrigger = 0)
857 {
858 global $langs, $conf;
859
860 $error = 0;
861
862 $this->db->begin();
863
864 // Check parameters
865 if (empty($this->country_id)) {
866 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
867 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
868 return -1;
869 }
870 if (empty($this->ref)) {
871 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
872 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
873 return -1;
874 }
875 if (!$this->label) {
876 $this->label = "???";
877 }
878
879 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
880
881 $sql .= " ref = '".$this->db->escape($this->ref)."'";
882 $sql .= ",label = '".$this->db->escape($this->label)."'";
883
884 $sql .= ",courant = ".((int) $this->courant);
885 $sql .= ",clos = ".((int) $this->clos);
886 $sql .= ",rappro = ".((int) $this->rappro);
887 $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
888 $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
889 $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
890 $sql .= ",bank = '".$this->db->escape($this->bank)."'";
891 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
892 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
893 $sql .= ",number='".$this->db->escape($this->number)."'";
894 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
895 $sql .= ",bic='".$this->db->escape($this->bic)."'";
896 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
897 $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
898 $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
899 $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
900 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
901 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
902 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
903 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
904
905 $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
906
907 $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
908 $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
909 $sql .= ",comment = '".$this->db->escape($this->comment)."'";
910
911 $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
912 $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
913 $sql .= ",ics = '".$this->db->escape($this->ics)."'";
914 $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
915
916 $sql .= " WHERE rowid = ".((int) $this->id);
917
918 dol_syslog(get_class($this)."::update", LOG_DEBUG);
919 $result = $this->db->query($sql);
920 if ($result) {
921 // Actions on extra fields (by external module or standard code)
922 if (!$error) {
923 $result = $this->insertExtraFields();
924 if ($result < 0) {
925 $error++;
926 }
927 }
928
929 if (!$error && !empty($this->oldref) && $this->oldref !== $this->ref) {
930 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'bank/".$this->db->escape($this->ref)."'";
931 $sql .= " WHERE filepath = 'bank/".$this->db->escape($this->oldref)."' and src_object_type='bank_account' and entity = ".((int) $conf->entity);
932 $resql = $this->db->query($sql);
933 if (!$resql) {
934 $error++;
935 $this->error = $this->db->lasterror();
936 }
937
938 // We rename directory in order not to lose the attachments
939 $oldref = dol_sanitizeFileName($this->oldref);
940 $newref = dol_sanitizeFileName($this->ref);
941 $dirsource = $conf->bank->dir_output.'/'.$oldref;
942 $dirdest = $conf->bank->dir_output.'/'.$newref;
943 if (file_exists($dirsource)) {
944 dol_syslog(get_class($this)."::update rename dir ".$dirsource." into ".$dirdest, LOG_DEBUG);
945 if (@rename($dirsource, $dirdest)) {
946 dol_syslog("Rename ok", LOG_DEBUG);
947 }
948 }
949 }
950
951 if (!$error && !$notrigger) {
952 // Call trigger
953 $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
954 if ($result < 0) {
955 $error++;
956 }
957 // End call triggers
958 }
959 } else {
960 $error++;
961 $this->error = $this->db->lasterror();
962 dol_print_error($this->db);
963 }
964
965 if (!$error) {
966 $this->db->commit();
967 return $this->id;
968 } else {
969 $this->db->rollback();
970 return -1 * $error;
971 }
972 }
973
974
975 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
982 public function update_bban(User $user = null)
983 {
984 // phpcs:enable
985 global $conf, $langs;
986
987 // Load library to get BAN control function
988 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
989
990 dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
991
992 // Check parameters
993 if (!$this->ref) {
994 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
995 return -2;
996 }
997
998 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
999 $sql .= " bank = '".$this->db->escape($this->bank)."'";
1000 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
1001 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
1002 $sql .= ",number='".$this->db->escape($this->number)."'";
1003 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
1004 $sql .= ",bic='".$this->db->escape($this->bic)."'";
1005 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
1006 $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
1007 $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
1008 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
1009 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
1010 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
1011 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
1012 $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null");
1013 $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null");
1014 $sql .= " WHERE rowid = ".((int) $this->id);
1015 $sql .= " AND entity = ".((int) $conf->entity);
1016
1017 dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
1018
1019 $result = $this->db->query($sql);
1020 if ($result) {
1021 return 1;
1022 } else {
1023 $this->error = $this->db->lasterror();
1024 dol_print_error($this->db);
1025 return -1;
1026 }
1027 }
1028
1029
1037 public function fetch($id, $ref = '')
1038 {
1039 if (empty($id) && empty($ref)) {
1040 $this->error = "ErrorBadParameters";
1041 return -1;
1042 }
1043
1044 $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,";
1045 $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
1046 $sql .= " ba.domiciliation as address, ba.pti_in_ctti, ba.proprio, ba.owner_address, ba.owner_zip, ba.owner_town, ba.owner_country_id, ba.state_id, ba.fk_pays as country_id,";
1047 $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
1048 $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
1049 $sql .= " ba.datec as date_creation, ba.tms as date_modification, ba.ics, ba.ics_transfer,";
1050 $sql .= ' c.code as country_code, c.label as country,';
1051 $sql .= ' d.code_departement as state_code, d.nom as state,';
1052 $sql .= ' aj.code as accountancy_journal';
1053 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1054 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
1055 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
1056 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal';
1057 $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
1058 if ($id) {
1059 $sql .= " AND ba.rowid = ".((int) $id);
1060 }
1061 if ($ref) {
1062 $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
1063 }
1064
1065 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1066 $result = $this->db->query($sql);
1067 if ($result) {
1068 if ($this->db->num_rows($result)) {
1069 $obj = $this->db->fetch_object($result);
1070
1071 $this->id = $obj->rowid;
1072 $this->rowid = $obj->rowid;
1073 $this->ref = $obj->ref;
1074 $this->label = $obj->label;
1075 $this->type = $obj->courant;
1076 $this->courant = $obj->courant;
1077 $this->bank = $obj->bank;
1078 $this->clos = $obj->clos;
1079 $this->rappro = $obj->rappro;
1080 $this->url = $obj->url;
1081
1082 $this->code_banque = $obj->code_banque;
1083 $this->code_guichet = $obj->code_guichet;
1084 $this->number = $obj->number;
1085 $this->cle_rib = $obj->cle_rib;
1086 $this->bic = $obj->bic;
1087 $this->iban = $obj->iban;
1088 $this->domiciliation = $obj->address;
1089 $this->address = $obj->address;
1090 $this->pti_in_ctti = $obj->pti_in_ctti;
1091 $this->proprio = $obj->proprio;
1092 $this->owner_address = $obj->owner_address;
1093 $this->owner_zip = $obj->owner_zip;
1094 $this->owner_town = $obj->owner_town;
1095 $this->owner_country_id = $obj->owner_country_id;
1096
1097 $this->state_id = $obj->state_id;
1098 $this->state_code = $obj->state_code;
1099 $this->state = $obj->state;
1100
1101 $this->country_id = $obj->country_id;
1102 $this->country_code = $obj->country_code;
1103 $this->country = $obj->country;
1104
1105 $this->account_number = $obj->account_number;
1106 $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
1107 $this->accountancy_journal = $obj->accountancy_journal;
1108
1109 $this->currency_code = $obj->currency_code;
1110 $this->account_currency_code = $obj->currency_code;
1111 $this->min_allowed = $obj->min_allowed;
1112 $this->min_desired = $obj->min_desired;
1113 $this->comment = $obj->comment;
1114
1115 $this->date_creation = $this->db->jdate($obj->date_creation);
1116 $this->date_modification = $this->db->jdate($obj->date_modification);
1117 $this->date_update = $this->date_modification; // For compatibility
1118
1119 $this->ics = $obj->ics;
1120 $this->ics_transfer = $obj->ics_transfer;
1121
1122 // Retrieve all extrafield
1123 // fetch optionals attributes and labels
1124 $this->fetch_optionals();
1125
1126 return 1;
1127 } else {
1128 return 0;
1129 }
1130 } else {
1131 $this->error = $this->db->lasterror();
1132 $this->errors[] = $this->error;
1133 return -1;
1134 }
1135 }
1136
1147 public function setCategories($categories)
1148 {
1149 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1150 return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
1151 }
1152
1159 public function delete(User $user = null)
1160 {
1161 $error = 0;
1162
1163 $this->db->begin();
1164
1165 // @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
1166
1167 // Delete link between tag and bank account
1168 if (!$error) {
1169 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1170 $sql .= " WHERE fk_account = ".((int) $this->id);
1171
1172 $resql = $this->db->query($sql);
1173 if (!$resql) {
1174 $error++;
1175 $this->error = "Error ".$this->db->lasterror();
1176 }
1177 }
1178
1179 if (!$error) {
1180 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
1181 $sql .= " WHERE rowid = ".((int) $this->id);
1182
1183 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1184 $result = $this->db->query($sql);
1185 if ($result) {
1186 // Remove extrafields
1187 if (!$error) {
1188 $result = $this->deleteExtraFields();
1189 if ($result < 0) {
1190 $error++;
1191 dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1192 }
1193 }
1194 } else {
1195 $error++;
1196 $this->error = "Error ".$this->db->lasterror();
1197 }
1198 }
1199
1200 if (!$error) {
1201 $this->db->commit();
1202 return 1;
1203 } else {
1204 $this->db->rollback();
1205 return -1;
1206 }
1207 }
1208
1209
1216 public function getLibStatut($mode = 0)
1217 {
1218 return $this->LibStatut($this->clos, $mode);
1219 }
1220
1221 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1229 public function LibStatut($status, $mode = 0)
1230 {
1231 // phpcs:enable
1232 global $langs;
1233 $langs->load('banks');
1234
1235 if ($status == self::STATUS_OPEN) {
1236 $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1237 $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1238 $statusType = 'status4';
1239 } else {
1240 $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1241 $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1242 $statusType = 'status5';
1243 }
1244
1245 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1246 }
1247
1248
1249 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1255 public function can_be_deleted()
1256 {
1257 // phpcs:enable
1258 $can_be_deleted = false;
1259
1260 $sql = "SELECT COUNT(rowid) as nb";
1261 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1262 $sql .= " WHERE fk_account = ".((int) $this->id);
1263
1264 $resql = $this->db->query($sql);
1265 if ($resql) {
1266 $obj = $this->db->fetch_object($resql);
1267 if ($obj->nb <= 1) {
1268 $can_be_deleted = true; // Juste le solde
1269 }
1270 } else {
1271 dol_print_error($this->db);
1272 }
1273 return $can_be_deleted;
1274 }
1275
1276
1282 public function error()
1283 {
1284 return $this->error;
1285 }
1286
1295 public function solde($option = 0, $date_end = '', $field = 'dateo')
1296 {
1297 $solde = 0;
1298
1299 $sql = "SELECT sum(amount) as amount";
1300 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1301 $sql .= " WHERE fk_account = ".((int) $this->id);
1302 if ($option == 1) {
1303 $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1304 }
1305
1306 $resql = $this->db->query($sql);
1307 if ($resql) {
1308 if ($this->db->num_rows($resql)) {
1309 $obj = $this->db->fetch_object($resql);
1310 $solde = $obj->amount;
1311 }
1312 $this->db->free($resql);
1313 } else {
1314 $this->errors[] = $this->db->lasterror;
1315 return -1;
1316 }
1317
1318 return price2num($solde, 'MU');
1319 }
1320
1321 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1329 public function load_board(User $user, $filteraccountid = 0)
1330 {
1331 // phpcs:enable
1332 global $conf, $langs;
1333
1334 if ($user->socid) {
1335 return -1; // protection pour eviter appel par utilisateur externe
1336 }
1337
1338 $sql = "SELECT b.rowid, b.datev as datefin";
1339 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1340 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1341 $sql .= " WHERE b.rappro=0";
1342 $sql .= " AND b.fk_account = ba.rowid";
1343 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1344 $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1345 $sql .= " AND clos = 0";
1346 if ($filteraccountid) {
1347 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1348 }
1349
1350 $resql = $this->db->query($sql);
1351 if ($resql) {
1352 $langs->load("banks");
1353 $now = dol_now();
1354
1355 require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1356
1357 $response = new WorkboardResponse();
1358 $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1359 $response->label = $langs->trans("TransactionsToConciliate");
1360 $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1361 $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1362 $response->img = img_object('', "payment");
1363
1364 while ($obj = $this->db->fetch_object($resql)) {
1365 $response->nbtodo++;
1366 if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1367 $response->nbtodolate++;
1368 }
1369 }
1370
1371 return $response;
1372 } else {
1373 dol_print_error($this->db);
1374 $this->error = $this->db->error();
1375 return -1;
1376 }
1377 }
1378
1379 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1386 public function load_state_board($filteraccountid = 0)
1387 {
1388 // phpcs:enable
1389 global $user;
1390
1391 if ($user->socid) {
1392 return -1; // protection pour eviter appel par utilisateur externe
1393 }
1394
1395 $sql = "SELECT count(b.rowid) as nb";
1396 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1397 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1398 $sql .= " WHERE b.fk_account = ba.rowid";
1399 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1400 $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1401 $sql .= " AND clos = 0";
1402 if ($filteraccountid) {
1403 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1404 }
1405
1406 $resql = $this->db->query($sql);
1407 if ($resql) {
1408 while ($obj = $this->db->fetch_object($resql)) {
1409 $this->nb["banklines"] = $obj->nb;
1410 }
1411 $this->db->free($resql);
1412 return 1;
1413 } else {
1414 dol_print_error($this->db);
1415 $this->error = $this->db->error();
1416 return -1;
1417 }
1418 }
1419
1420
1426 public function countAccountToReconcile()
1427 {
1428 global $db, $conf, $user;
1429
1430 //Protection against external users
1431 if ($user->socid) {
1432 return 0;
1433 }
1434
1435 $nb = 0;
1436
1437 $sql = "SELECT COUNT(ba.rowid) as nb";
1438 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1439 $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1440 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1441 if (!getDolGlobalString('BANK_CAN_RECONCILIATE_CASHACCOUNT')) {
1442 $sql .= " AND ba.courant != 2";
1443 }
1444 $resql = $this->db->query($sql);
1445 if ($resql) {
1446 $obj = $this->db->fetch_object($resql);
1447 $nb = $obj->nb;
1448 } else {
1449 dol_print_error($this->db);
1450 }
1451
1452 return $nb;
1453 }
1454
1462 public function getTooltipContentArray($params)
1463 {
1464 global $langs;
1465 $langs->loadLangs(['banks', 'compta']);
1466 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1467
1468 $datas = array();
1469
1470 $nofetch = !empty($params['nofetch']);
1471 $pictos = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
1472 if (isset($this->status)) {
1473 $pictos .= ' '.$this->getLibStatut(5);
1474 }
1475 $datas['picto'] = $pictos;
1476 $datas['label'] = '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1477 $datas['accountnumber'] = '<br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
1478 $datas['iban'] = '<br><b>'.$langs->trans('IBAN').':</b> '.getIbanHumanReadable($this);
1479 $datas['bic'] = '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
1480 $datas['accountcurrency'] = '<br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
1481
1482 if (isModEnabled('accounting')) {
1483 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1484 $langs->load("accountancy");
1485 $datas['accountaccounting'] = '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
1486 $datas['accountancyjournal'] = '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
1487 }
1488 // show categories for this record only in ajax to not overload lists
1489 if (isModEnabled('categorie') && !$nofetch) {
1490 require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
1491 $form = new Form($this->db);
1492 $datas['categories'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_ACCOUNT, 1);
1493 }
1494
1495 return $datas;
1496 }
1497
1509 public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1510 {
1511 global $conf, $langs;
1512
1513 if (!empty($conf->dol_no_mouse_hover)) {
1514 $notooltip = 1; // Force disable tooltips
1515 }
1516
1517 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1518
1519 $result = '';
1520 $classfortooltip = 'classfortooltip';
1521 $dataparams = '';
1522 $params = [
1523 'id' => $this->id,
1524 'objecttype' => $this->element,
1525 'option' => $option,
1526 'nofetch' => 1,
1527 ];
1528 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1529 $classfortooltip = 'classforajaxtooltip';
1530 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1531 $label = '';
1532 } else {
1533 $label = implode($this->getTooltipContentArray($params));
1534 }
1535
1536 $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1537 if ($mode == 'transactions') {
1538 $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1539 } elseif ($mode == 'receipts') {
1540 $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1541 }
1542
1543 if ($option != 'nolink') {
1544 // Add param to save lastsearch_values or not
1545 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1546 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1547 $add_save_lastsearch_values = 1;
1548 }
1549 if ($add_save_lastsearch_values) {
1550 $url .= '&save_lastsearch_values=1';
1551 }
1552 }
1553
1554 $linkclose = '';
1555 if (empty($notooltip)) {
1556 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1557 $label = $langs->trans("BankAccount");
1558 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1559 }
1560 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1561 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1562 } else {
1563 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1564 }
1565
1566 if ($option == 'nolink' || empty($url)) {
1567 $linkstart = '<span';
1568 } else {
1569 $linkstart = '<a href="'.$url.'"';
1570 }
1571 $linkstart .= $linkclose.'>';
1572 if ($option == 'nolink' || empty($url)) {
1573 $linkend = '</span>';
1574 } else {
1575 $linkend = '</a>';
1576 }
1577
1578 $result .= $linkstart;
1579
1580 if ($withpicto) {
1581 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1582 }
1583 if ($withpicto != 2) {
1584 $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1585 }
1586 $result .= $linkend;
1587
1588 return $result;
1589 }
1590
1591
1592 // Method after here are common to Account and CompanyBankAccount
1593
1594
1600 public function verif()
1601 {
1602 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1603
1604 $error = 0;
1605
1606 // Call functions to check BAN
1607 if (!checkIbanForAccount($this)) {
1608 $error++;
1609 $this->error = 'IBANNotValid';
1610 }
1611 if (!checkSwiftForAccount($this)) {
1612 $error++;
1613 $this->error = 'SwiftNotValid';
1614 }
1615
1616 if (! $error) {
1617 return 1;
1618 } else {
1619 return 0;
1620 }
1621 }
1622
1628 public function getCountryCode()
1629 {
1630 global $mysoc;
1631
1632 // We return country code of bank account
1633 if (!empty($this->country_code)) {
1634 return $this->country_code;
1635 }
1636
1637 // For backward compatibility, we try to guess country from other information
1638 if (!empty($this->iban)) {
1639 // If IBAN defined, we can know country of account from it
1640 $reg = array();
1641 if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
1642 return $reg[1];
1643 }
1644 }
1645
1646 // If this class is linked to a third party
1647 if (!empty($this->socid)) {
1648 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
1649 $company = new Societe($this->db);
1650 $result = $company->fetch($this->socid);
1651 if (!empty($company->country_code)) {
1652 return $company->country_code;
1653 }
1654 }
1655
1656 // We return country code of managed company
1657 if (!empty($mysoc->country_code)) {
1658 return $mysoc->country_code;
1659 }
1660
1661 return '';
1662 }
1663
1671 public function getBannerAddress($htmlkey, $object)
1672 {
1673 global $conf, $langs;
1674
1675 $out = '';
1676
1677 $outdone = 0;
1678 $coords = $this->getFullAddress(1, ', ', getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT'));
1679 if ($coords) {
1680 if (!empty($conf->use_javascript_ajax)) {
1681 // hideonsmatphone because copyToClipboard call jquery dialog that does not work with jmobile
1682 $out .= '<a href="#" class="hideonsmartphone" onclick="return copyToClipboard(\''.dol_escape_js($coords).'\',\''.dol_escape_js($langs->trans("HelpCopyToClipboard")).'\');">';
1683 $out .= img_picto($langs->trans("Address"), 'map-marker-alt');
1684 $out .= '</a> ';
1685 }
1686 $address = dol_print_address($coords, 'address_'.$htmlkey.'_'.$this->id, $this->element, $this->id, 1, ', ');
1687 if ($address) {
1688 $out .= $address;
1689 $outdone++;
1690 }
1691 $outdone++;
1692 }
1693
1694 return $out;
1695 }
1696
1697
1706 public function useDetailedBBAN()
1707 {
1708 $country_code = $this->getCountryCode();
1709
1710 if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
1711 return 1; // France, Spain, Gabon, ... - Not valid for CH
1712 }
1713 if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
1714 return 2; // Australia, England...
1715 }
1716 return 0;
1717 }
1718
1724 public function needIBAN()
1725 {
1726 global $conf;
1727
1728 if (getDolGlobalString('MAIN_IBAN_IS_NEVER_MANDATORY')) {
1729 return 0;
1730 }
1731
1732 $country_code = $this->getCountryCode();
1733
1734 $country_code_in_EEC = array(
1735 'AT', // Austria
1736 'BE', // Belgium
1737 'BG', // Bulgaria
1738 'CY', // Cyprus
1739 'CZ', // Czech republic
1740 'DE', // Germany
1741 'DK', // Danemark
1742 'EE', // Estonia
1743 'ES', // Spain
1744 'FI', // Finland
1745 'FR', // France
1746 'GB', // United Kingdom
1747 'GR', // Greece
1748 'HR', // Croatia
1749 'NL', // Holland
1750 'HU', // Hungary
1751 'IE', // Ireland
1752 'IM', // Isle of Man - Included in UK
1753 'IT', // Italy
1754 'LT', // Lithuania
1755 'LU', // Luxembourg
1756 'LV', // Latvia
1757 'MC', // Monaco - Included in France
1758 'MT', // Malta
1759 //'NO', // Norway
1760 'PL', // Poland
1761 'PT', // Portugal
1762 'RO', // Romania
1763 'SE', // Sweden
1764 'SK', // Slovakia
1765 'SI', // Slovenia
1766 'UK', // United Kingdom
1767 //'CH', // Switzerland - No. Swizerland in not in EEC
1768 );
1769
1770 if (in_array($country_code, $country_code_in_EEC)) {
1771 return 1; // France, Spain, ...
1772 }
1773 return 0;
1774 }
1775
1782 public function info($id)
1783 {
1784 }
1785
1800 public function getFieldsToShow($includeibanbic = 0)
1801 {
1802 //Get the required properties depending on the country
1803 $detailedBBAN = $this->useDetailedBBAN();
1804
1805 if ($detailedBBAN == 0) {
1806 $fieldarray = array(
1807 'BankAccountNumber'
1808 );
1809 } elseif ($detailedBBAN == 2) {
1810 $fieldarray = array(
1811 'BankCode',
1812 'BankAccountNumber'
1813 );
1814 } else {
1815 $fieldarray = self::getAccountNumberOrder();
1816 }
1817
1818 //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1819 if ($includeibanbic) {
1820 $fieldarray[] = 'IBAN';
1821 $fieldarray[] = 'BIC';
1822 }
1823 //}
1824
1825 //Get the order the properties are shown
1826 return $fieldarray;
1827 }
1828
1839 public static function getAccountNumberOrder()
1840 {
1841 global $conf;
1842
1843 $fieldlists = array(
1844 'BankCode',
1845 'DeskCode',
1846 'BankAccountNumber',
1847 'BankAccountNumberKey'
1848 );
1849
1850 if (getDolGlobalString('BANK_SHOW_ORDER_OPTION')) {
1851 if (is_numeric($conf->global->BANK_SHOW_ORDER_OPTION)) {
1852 if (getDolGlobalString('BANK_SHOW_ORDER_OPTION') == '1') {
1853 $fieldlists = array(
1854 'BankCode',
1855 'DeskCode',
1856 'BankAccountNumberKey',
1857 'BankAccountNumber'
1858 );
1859 }
1860 } else {
1861 //Replace the old AccountNumber key with the new BankAccountNumber key
1862 $fieldlists = explode(
1863 ' ',
1864 preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
1865 );
1866 }
1867 }
1868
1869 return $fieldlists;
1870 }
1871
1872
1880 public function initAsSpecimen()
1881 {
1882 // Example of IBAN FR7630001007941234567890185
1883 $this->specimen = 1;
1884 $this->ref = 'MBA';
1885 $this->label = 'My Big Company Bank account';
1886 $this->bank = 'MyBank';
1887 $this->courant = Account::TYPE_CURRENT;
1888 $this->clos = Account::STATUS_OPEN;
1889 $this->code_banque = '30001';
1890 $this->code_guichet = '00794';
1891 $this->number = '12345678901';
1892 $this->cle_rib = '85';
1893 $this->bic = 'AA12';
1894 $this->iban = 'FR7630001007941234567890185';
1895 $this->domiciliation = 'Banque de France';
1896 $this->proprio = 'Owner';
1897 $this->owner_address = 'Owner address';
1898 $this->owner_zip = 'Owner zip';
1899 $this->owner_town = 'Owner town';
1900 $this->owner_country_id = 'Owner country_id';
1901 $this->country_id = 1;
1902 }
1903
1912 public static function replaceThirdparty($dbs, $origin_id, $dest_id)
1913 {
1914 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'";
1915
1916 if ($dbs->query($sql)) {
1917 return true;
1918 } else {
1919 //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.
1920 //$this->errors = $dbs->lasterror();
1921 return false;
1922 }
1923 }
1924
1932 public function getKanbanView($option = '', $arraydata = null)
1933 {
1934 global $langs;
1935
1936 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1937
1938 $return = '<div class="box-flex-item box-flex-grow-zero">';
1939 $return .= '<div class="info-box info-box-sm">';
1940 $return .= '<span class="info-box-icon bg-infobox-action">';
1941 $return .= img_picto('', $this->picto);
1942 $return .= '</span>';
1943 $return .= '<div class="info-box-content">';
1944 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
1945 if ($selected >= 0) {
1946 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1947 }
1948 if (property_exists($this, 'type_lib')) {
1949 $return .= '<br><span class="info-box-label opacitymedium" title="'.$this->type_lib[$this->type].'">'.substr($this->type_lib[$this->type], 0, 24).'...</span>';
1950 }
1951 if (method_exists($this, 'solde')) {
1952 $return .= '<br><a href="'.DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id.'">';
1953 $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>';
1954 }
1955 if (method_exists($this, 'getLibStatut')) {
1956 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
1957 }
1958 $return .= '</div>';
1959 $return .= '</div>';
1960 $return .= '</div>';
1961 return $return;
1962 }
1963}
1964
1965
1966require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
1967
1971class AccountLine extends CommonObjectLine
1972{
1976 public $error = '';
1977
1981 public $db;
1982
1986 public $element = 'bank';
1987
1991 public $table_element = 'bank';
1992
1996 public $picto = 'accountline';
1997
2001 public $id;
2002
2006 public $ref;
2007
2013 public $datec;
2014
2020 public $dateo;
2021
2027 public $datev;
2028
2029 public $amount; /* Amount of payment in the bank account currency */
2030 public $amount_main_currency; /* Amount in the currency of company if bank account use another currency */
2031
2035 public $fk_user_author;
2036
2040 public $fk_user_rappro;
2041
2045 public $fk_type;
2046
2050 public $fk_bordereau;
2051
2055 public $fk_account;
2056
2060 public $bank_account_ref;
2061
2065 public $bank_account_label;
2066
2070 public $numero_compte;
2071
2075 public $emetteur;
2076
2077 public $rappro; // Is it conciliated
2078 public $num_releve; // If conciliated, what is bank statement
2079 public $num_chq; // Num of cheque
2080 public $bank_chq; // Bank of cheque
2081
2085 public $label;
2086
2087 public $note;
2088
2092 public $user_rappro;
2093
2094
2100 public function __construct(DoliDB $db)
2101 {
2102 $this->db = $db;
2103 }
2104
2113 public function fetch($rowid, $ref = '', $num = '')
2114 {
2115 // Check parameters
2116 if (empty($rowid) && empty($ref) && empty($num)) {
2117 return -1;
2118 }
2119
2120 $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
2121 $sql .= " b.fk_user_author, b.fk_user_rappro,";
2122 $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
2123 $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
2124 $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
2125 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
2126 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
2127 $sql .= " WHERE b.fk_account = ba.rowid";
2128 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
2129 if ($num) {
2130 $sql .= " AND b.num_chq = '".$this->db->escape($num)."'";
2131 } elseif ($ref) {
2132 $sql .= " AND b.rowid = '".$this->db->escape($ref)."'";
2133 } else {
2134 $sql .= " AND b.rowid = ".((int) $rowid);
2135 }
2136
2137 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
2138 $result = $this->db->query($sql);
2139 if ($result) {
2140 $ret = 0;
2141
2142 $obj = $this->db->fetch_object($result);
2143 if ($obj) {
2144 $this->id = $obj->rowid;
2145 $this->rowid = $obj->rowid;
2146 $this->ref = $obj->rowid;
2147
2148 $this->datec = $this->db->jdate($obj->datec);
2149 $this->datev = $this->db->jdate($obj->datev);
2150 $this->dateo = $this->db->jdate($obj->dateo);
2151 $this->amount = $obj->amount;
2152 $this->label = $obj->label;
2153 $this->note = $obj->note;
2154
2155 $this->fk_user_author = $obj->fk_user_author;
2156 $this->fk_user_rappro = $obj->fk_user_rappro;
2157
2158 $this->fk_type = $obj->fk_type; // Type of transaction
2159 $this->rappro = (int) $obj->rappro;
2160 $this->num_releve = $obj->num_releve;
2161
2162 $this->num_chq = $obj->num_chq;
2163 $this->bank_chq = $obj->banque;
2164 $this->fk_bordereau = $obj->fk_bordereau;
2165
2166 $this->fk_account = $obj->fk_account;
2167 $this->bank_account_ref = $obj->bank_account_ref;
2168 $this->bank_account_label = $obj->bank_account_label;
2169
2170 // Retrieve all extrafield
2171 // fetch optionals attributes and labels
2172 $this->fetch_optionals();
2173
2174 $ret = 1;
2175 }
2176 $this->db->free($result);
2177 return $ret;
2178 } else {
2179 return -1;
2180 }
2181 }
2182
2188 public function insert()
2189 {
2190 $error = 0;
2191
2192 $this->db->begin();
2193
2194 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
2195 $sql .= "datec";
2196 $sql .= ", dateo";
2197 $sql .= ", datev";
2198 $sql .= ", label";
2199 $sql .= ", amount";
2200 $sql .= ", amount_main_currency";
2201 $sql .= ", fk_user_author";
2202 $sql .= ", num_chq";
2203 $sql .= ", fk_account";
2204 $sql .= ", fk_type";
2205 $sql .= ", emetteur,banque";
2206 $sql .= ", rappro";
2207 $sql .= ", numero_compte";
2208 $sql .= ", num_releve";
2209 $sql .= ") VALUES (";
2210 $sql .= "'".$this->db->idate($this->datec)."'";
2211 $sql .= ", '".$this->db->idate($this->dateo)."'";
2212 $sql .= ", '".$this->db->idate($this->datev)."'";
2213 $sql .= ", '".$this->db->escape($this->label)."'";
2214 $sql .= ", ".price2num($this->amount);
2215 $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
2216 $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
2217 $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
2218 $sql .= ", '".$this->db->escape($this->fk_account)."'";
2219 $sql .= ", '".$this->db->escape($this->fk_type)."'";
2220 $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
2221 $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
2222 $sql .= ", ".(int) $this->rappro;
2223 $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2224 $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2225 $sql .= ")";
2226
2227
2228 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2229 $resql = $this->db->query($sql);
2230 if ($resql) {
2231 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2232 // Actions on extra fields (by external module or standard code)
2233 $result = $this->insertExtraFields();
2234 if ($result < 0) {
2235 $error++;
2236 }
2237 } else {
2238 $error++;
2239 $this->error = $this->db->lasterror();
2240 dol_print_error($this->db);
2241 }
2242
2243 if (!$error) {
2244 $this->db->commit();
2245 return $this->id;
2246 } else {
2247 $this->db->rollback();
2248 return -1 * $error;
2249 }
2250 }
2251
2259 public function delete(User $user = null, $notrigger = 0)
2260 {
2261 $nbko = 0;
2262
2263 if ($this->rappro) {
2264 // Protection to avoid any delete of consolidated lines
2265 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2266 return -1;
2267 }
2268
2269 $this->db->begin();
2270
2271 if (!$notrigger) {
2272 // Call trigger
2273 $result = $this->call_trigger('BANKACCOUNTLINE_DELETE', $user);
2274 if ($result < 0) {
2275 $this->db->rollback();
2276 return -1;
2277 }
2278 // End call triggers
2279 }
2280
2281 // Protection to avoid any delete of accounted lines. Protection on by default
2282 if (!getDolGlobalString('BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING')) {
2283 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2284 $resql = $this->db->query($sql);
2285 if ($resql) {
2286 $obj = $this->db->fetch_object($resql);
2287 if ($obj && $obj->nb) {
2288 $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2289 $this->db->rollback();
2290 return -1;
2291 }
2292 } else {
2293 $this->error = $this->db->lasterror();
2294 $this->db->rollback();
2295 return -1;
2296 }
2297 }
2298
2299 // Delete urls
2300 $result = $this->delete_urls($user);
2301 if ($result < 0) {
2302 $nbko++;
2303 }
2304
2305 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".(int) $this->rowid;
2306 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2307 $result = $this->db->query($sql);
2308 if (!$result) {
2309 $nbko++;
2310 }
2311
2312 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_extrafields WHERE fk_object=".(int) $this->rowid;
2313 $result = $this->db->query($sql);
2314 if (!$result) {
2315 $nbko++;
2316 }
2317
2318 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2319 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2320 $result = $this->db->query($sql);
2321 if (!$result) {
2322 $nbko++;
2323 }
2324
2325 if (!$nbko) {
2326 $this->db->commit();
2327 return 1;
2328 } else {
2329 $this->db->rollback();
2330 return -$nbko;
2331 }
2332 }
2333
2334
2335 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2342 public function delete_urls(User $user = null)
2343 {
2344 // phpcs:enable
2345 $nbko = 0;
2346
2347 if ($this->rappro) {
2348 // Protection to avoid any delete of consolidated lines
2349 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2350 return -1;
2351 }
2352
2353 $this->db->begin();
2354
2355 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2356 dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
2357 $result = $this->db->query($sql);
2358 if (!$result) {
2359 $nbko++;
2360 }
2361
2362 if (!$nbko) {
2363 $this->db->commit();
2364 return 1;
2365 } else {
2366 $this->db->rollback();
2367 return -$nbko;
2368 }
2369 }
2370
2371
2379 public function update(User $user, $notrigger = 0)
2380 {
2381 $this->db->begin();
2382
2383 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2384 $sql .= " amount = ".price2num($this->amount).",";
2385 $sql .= " datev='".$this->db->idate($this->datev)."',";
2386 $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2387 $sql .= " WHERE rowid = ".((int) $this->rowid);
2388
2389 dol_syslog(get_class($this)."::update", LOG_DEBUG);
2390 $resql = $this->db->query($sql);
2391 if ($resql) {
2392 $this->db->commit();
2393 return 1;
2394 } else {
2395 $this->db->rollback();
2396 $this->error = $this->db->error();
2397 return -1;
2398 }
2399 }
2400
2401
2402 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2411 public function update_conciliation(User $user, $cat, $conciliated = 1)
2412 {
2413 // phpcs:enable
2414 global $conf, $langs;
2415
2416 $this->db->begin();
2417
2418 // Check statement field
2419 if (getDolGlobalString('BANK_STATEMENT_REGEX_RULE')) {
2420 if (!preg_match('/' . getDolGlobalString('BANK_STATEMENT_REGEX_RULE').'/', $this->num_releve)) {
2421 $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", $conf->global->BANK_STATEMENT_REGEX_RULE);
2422 return -1;
2423 }
2424 }
2425
2426 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2427 $sql .= " rappro = ".((int) $conciliated);
2428 $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2429 if ($conciliated) {
2430 $sql .= ", fk_user_rappro = ".$user->id;
2431 }
2432 $sql .= " WHERE rowid = ".((int) $this->id);
2433
2434 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2435 $resql = $this->db->query($sql);
2436 if ($resql) {
2437 if (!empty($cat) && $cat > 0) {
2438 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
2439 $sql .= "lineid";
2440 $sql .= ", fk_categ";
2441 $sql .= ") VALUES (";
2442 $sql .= $this->id;
2443 $sql .= ", ".((int) $cat);
2444 $sql .= ")";
2445
2446 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2447 $this->db->query($sql);
2448
2449 // No error check. Can fail if category already affected
2450 // TODO Do no try the insert if link already exists
2451 }
2452
2453 $this->rappro = 1;
2454
2455 $this->db->commit();
2456 return 1;
2457 } else {
2458 $this->db->rollback();
2459 return -1;
2460 }
2461 }
2462
2463
2464 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2472 public function datev_change($rowid, $sign = 1)
2473 {
2474 // phpcs:enable
2475 $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2476 $resql = $this->db->query($sql);
2477 if ($resql) {
2478 $obj = $this->db->fetch_object($resql);
2479 $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2480
2481 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2482 $sql .= " datev = '".$this->db->idate($newdate)."'";
2483 $sql .= " WHERE rowid = ".((int) $rowid);
2484
2485 $result = $this->db->query($sql);
2486 if ($result) {
2487 if ($this->db->affected_rows($result)) {
2488 return 1;
2489 }
2490 } else {
2491 dol_print_error($this->db);
2492 return 0;
2493 }
2494 } else {
2495 dol_print_error($this->db);
2496 }
2497 return 0;
2498 }
2499
2500 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2507 public function datev_next($id)
2508 {
2509 // phpcs:enable
2510 return $this->datev_change($id, 1);
2511 }
2512
2513 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2520 public function datev_previous($id)
2521 {
2522 // phpcs:enable
2523 return $this->datev_change($id, -1);
2524 }
2525
2526
2527 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2535 public function dateo_change($rowid, $sign = 1)
2536 {
2537 // phpcs:enable
2538 $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2539 $resql = $this->db->query($sql);
2540 if ($resql) {
2541 $obj = $this->db->fetch_object($resql);
2542 $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2543
2544 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2545 $sql .= " dateo = '".$this->db->idate($newdate)."'";
2546 $sql .= " WHERE rowid = ".((int) $rowid);
2547
2548 $result = $this->db->query($sql);
2549 if ($result) {
2550 if ($this->db->affected_rows($result)) {
2551 return 1;
2552 }
2553 } else {
2554 dol_print_error($this->db);
2555 return 0;
2556 }
2557 } else {
2558 dol_print_error($this->db);
2559 }
2560 return 0;
2561 }
2562
2563 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2570 public function dateo_next($id)
2571 {
2572 // phpcs:enable
2573 return $this->dateo_change($id, 1);
2574 }
2575
2576 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2583 public function dateo_previous($id)
2584 {
2585 // phpcs:enable
2586 return $this->dateo_change($id, -1);
2587 }
2588
2589
2596 public function info($id)
2597 {
2598 $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2599 $sql .= ' b.fk_user_author, b.fk_user_rappro';
2600 $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2601 $sql .= ' WHERE b.rowid = '.((int) $id);
2602
2603 $result = $this->db->query($sql);
2604 if ($result) {
2605 if ($this->db->num_rows($result)) {
2606 $obj = $this->db->fetch_object($result);
2607
2608 $this->id = $obj->rowid;
2609
2610 $this->user_creation_id = $obj->fk_user_author;
2611 $this->user_rappro = $obj->fk_user_rappro;
2612 $this->date_creation = $this->db->jdate($obj->datec);
2613 $this->date_modification = $this->db->jdate($obj->datem);
2614 //$this->date_rappro = $obj->daterappro; // Not yet managed
2615 }
2616 $this->db->free($result);
2617 } else {
2618 dol_print_error($this->db);
2619 }
2620 }
2621
2622
2632 public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2633 {
2634 global $conf, $langs;
2635
2636 $result = '';
2637
2638 $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2639 $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2640 if ($this->amount) {
2641 $label .= '<br><strong>'.$langs->trans("Amount").':</strong> '.price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
2642 }
2643
2644 $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">';
2645 $linkend = '</a>';
2646
2647 $result .= $linkstart;
2648 if ($withpicto) {
2649 $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);
2650 }
2651 if ($withpicto != 2) {
2652 $result .= ($this->ref ? $this->ref : $this->id);
2653 }
2654
2655 $result .= $linkend;
2656
2657 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2658 $result .= ' <span class="opacitymedium">(';
2659 }
2660 if ($option == 'showall') {
2661 $result .= $langs->trans("BankAccount").': ';
2662 $accountstatic = new Account($this->db);
2663 $accountstatic->id = $this->fk_account;
2664 $accountstatic->ref = $this->bank_account_ref;
2665 $accountstatic->label = $this->bank_account_label;
2666 $result .= $accountstatic->getNomUrl(0).', ';
2667 }
2668 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2669 $result .= $langs->trans("BankLineConciliated").': ';
2670 $result .= yn($this->rappro);
2671 }
2672 if (isModEnabled('accounting') && ($option == 'showall' || $option == 'showconciliatedandaccounted')) {
2673 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
2674 $sql .= " WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2675 $resql = $this->db->query($sql);
2676 if ($resql) {
2677 $obj = $this->db->fetch_object($resql);
2678 if ($obj && $obj->nb) {
2679 $result .= ' - '.$langs->trans("Accounted").': '.yn(1);
2680 } else {
2681 $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2682 }
2683 }
2684 }
2685 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2686 $result .= ')</span>';
2687 }
2688
2689 return $result;
2690 }
2691
2692
2699 public function getLibStatut($mode = 0)
2700 {
2701 return $this->LibStatut($this->status, $mode);
2702 }
2703
2704 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2712 public function LibStatut($status, $mode = 0)
2713 {
2714 // phpcs:enable
2715 //global $langs;
2716
2717 //$langs->load('companies');
2718 /*
2719 if ($mode == 0)
2720 {
2721 if ($status==0) return $langs->trans("ActivityCeased");
2722 if ($status==1) return $langs->trans("InActivity");
2723 }
2724 if ($mode == 1)
2725 {
2726 if ($status==0) return $langs->trans("ActivityCeased");
2727 if ($status==1) return $langs->trans("InActivity");
2728 }
2729 if ($mode == 2)
2730 {
2731 if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2732 if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2733 }
2734 if ($mode == 3)
2735 {
2736 if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2737 if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2738 }
2739 if ($mode == 4)
2740 {
2741 if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2742 if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2743 }
2744 if ($mode == 5)
2745 {
2746 if ($status==0) return $langs->trans("ActivityCeased").' '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2747 if ($status==1) return $langs->trans("InActivity").' '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2748 }*/
2749
2750 return '';
2751 }
2752
2758 public function getVentilExportCompta()
2759 {
2760 $alreadydispatched = 0;
2761
2762 $type = 'bank';
2763
2764 $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);
2765 $resql = $this->db->query($sql);
2766 if ($resql) {
2767 $obj = $this->db->fetch_object($resql);
2768 if ($obj) {
2769 $alreadydispatched = $obj->nb;
2770 }
2771 } else {
2772 $this->error = $this->db->lasterror();
2773 return -1;
2774 }
2775
2776 if ($alreadydispatched) {
2777 return 1;
2778 }
2779 return 0;
2780 }
2781}
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
Definition security.php:604
checkSwiftForAccount(Account $account=null, $swift=null)
Check SWIFT informations for a bank account.
Definition bank.lib.php:282
checkIbanForAccount(Account $account=null, $ibantocheck=null)
Check IBAN number informations for a bank account.
Definition bank.lib.php:303
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition bank.lib.php:328
$object ref
Definition info.php:79
Class to manage bank accounts.
solde($option=0, $date_end='', $field='dateo')
Return current sold.
fetch($id, $ref='')
Load a bank account into memory from database.
__construct(DoliDB $db)
Constructor.
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.
update(User $user, $notrigger=0)
Update bank account card.
getCountryCode()
Return account country code.
getLibStatut($mode=0)
Return label of object status.
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.
load_state_board($filteraccountid=0)
Charge indicateurs this->nb de tableau de bord.
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()
Renvoi si un compte peut etre supprimer ou non (sans mouvements)
update_bban(User $user=null)
Update BBAN (RIB) account fields.
const TYPE_CURRENT
Current account.
setCategories($categories)
Sets object to supplied categories.
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 ...
delete(User $user=null)
Delete bank account from database.
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...
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.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
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.
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e rowid
Definition invoice.php:1907
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:121