dolibarr 18.0.6
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
193 public $type_lib = array();
194
200 public $status = array();
201
206 public $account_number;
207
211 public $fk_accountancy_journal;
215 public $accountancy_journal;
216
221 public $currency_code;
222
228 public $account_currency_code;
229
234 public $min_allowed;
235
240 public $min_desired;
241
246 public $comment;
247
252 public $date_solde;
253
260 public $solde;
261
266 public $balance;
267
272 public $ics;
273
278 public $ics_transfer;
279
283 public $oldref;
284
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 && empty($conf->global->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 = '', $url_id = '', $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 $balance = $this->balance;
712 if (empty($balance) && !empty($this->solde)) {
713 $balance = $this->solde;
714 }
715 if (empty($balance)) {
716 $balance = 0;
717 }
718
719 // Chargement librairie pour acces fonction controle RIB
720 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
721
722 $now = dol_now();
723
724 $this->db->begin();
725
726 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
727 $sql .= "datec";
728 $sql .= ", ref";
729 $sql .= ", label";
730 $sql .= ", entity";
731 $sql .= ", account_number";
732 $sql .= ", fk_accountancy_journal";
733 $sql .= ", bank";
734 $sql .= ", code_banque";
735 $sql .= ", code_guichet";
736 $sql .= ", number";
737 $sql .= ", cle_rib";
738 $sql .= ", bic";
739 $sql .= ", iban_prefix";
740 $sql .= ", domiciliation";
741 $sql .= ", pti_in_ctti";
742 $sql .= ", proprio";
743 $sql .= ", owner_address";
744 $sql .= ", owner_zip";
745 $sql .= ", owner_town";
746 $sql .= ", owner_country_id";
747 $sql .= ", currency_code";
748 $sql .= ", rappro";
749 $sql .= ", min_allowed";
750 $sql .= ", min_desired";
751 $sql .= ", comment";
752 $sql .= ", state_id";
753 $sql .= ", fk_pays";
754 $sql .= ", ics";
755 $sql .= ", ics_transfer";
756 $sql .= ") VALUES (";
757 $sql .= "'".$this->db->idate($now)."'";
758 $sql .= ", '".$this->db->escape($this->ref)."'";
759 $sql .= ", '".$this->db->escape($this->label)."'";
760 $sql .= ", ".((int) $conf->entity);
761 $sql .= ", '".$this->db->escape($this->account_number)."'";
762 $sql .= ", ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
763 $sql .= ", '".$this->db->escape($this->bank)."'";
764 $sql .= ", '".$this->db->escape($this->code_banque)."'";
765 $sql .= ", '".$this->db->escape($this->code_guichet)."'";
766 $sql .= ", '".$this->db->escape($this->number)."'";
767 $sql .= ", '".$this->db->escape($this->cle_rib)."'";
768 $sql .= ", '".$this->db->escape($this->bic)."'";
769 $sql .= ", '".$this->db->escape($this->iban)."'";
770 $sql .= ", '".$this->db->escape($this->domiciliation)."'";
771 $sql .= ", ".((int) $this->pti_in_ctti);
772 $sql .= ", '".$this->db->escape($this->proprio)."'";
773 $sql .= ", '".$this->db->escape($this->owner_address)."'";
774 $sql .= ", '".$this->db->escape($this->owner_zip)."'";
775 $sql .= ", '".$this->db->escape($this->owner_town)."'";
776 $sql .= ", ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
777 $sql .= ", '".$this->db->escape($this->currency_code)."'";
778 $sql .= ", ".((int) $this->rappro);
779 $sql .= ", ".price2num($this->min_allowed, 'MT');
780 $sql .= ", ".price2num($this->min_desired, 'MT');
781 $sql .= ", '".$this->db->escape($this->comment)."'";
782 $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
783 $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
784 $sql .= ", '".$this->db->escape($this->ics)."'";
785 $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
786 $sql .= ")";
787
788 dol_syslog(get_class($this)."::create", LOG_DEBUG);
789 $resql = $this->db->query($sql);
790 if ($resql) {
791 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
792
793 $result = $this->update($user, 1);
794 if ($result > 0) {
795 $accline = new AccountLine($this->db);
796 $accline->datec = $this->db->idate($now);
797 $accline->label = '('.$langs->trans("InitialBankBalance").')';
798 $accline->amount = price2num($balance);
799 $accline->fk_user_author = $user->id;
800 $accline->fk_account = $this->id;
801 $accline->datev = $this->db->idate($this->date_solde);
802 $accline->dateo = $this->db->idate($this->date_solde);
803 $accline->fk_type = 'SOLD';
804
805 if ($accline->insert() < 0) {
806 $error++;
807 $this->error = $accline->error;
808 $this->errors = $accline->errors;
809 }
810
811 if (!$error) {
812 $result = $this->insertExtraFields();
813 if ($result < 0) {
814 $error++;
815 }
816 }
817
818 if (!$error && !$notrigger) {
819 // Call trigger
820 $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
821 if ($result < 0) {
822 $error++;
823 }
824 // End call triggers
825 }
826 } else {
827 $error++;
828 }
829 } else {
830 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
831 $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
832 $error++;
833 } else {
834 $this->error = $this->db->error()." sql=".$sql;
835 $error++;
836 }
837 }
838
839 if (!$error) {
840 $this->db->commit();
841 return $this->id;
842 } else {
843 $this->db->rollback();
844 return -1 * $error;
845 }
846 }
847
855 public function update(User $user, $notrigger = 0)
856 {
857 global $langs, $conf;
858
859 $error = 0;
860
861 $this->db->begin();
862
863 // Check parameters
864 if (empty($this->country_id)) {
865 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
866 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
867 return -1;
868 }
869 if (empty($this->ref)) {
870 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
871 dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
872 return -1;
873 }
874 if (!$this->label) {
875 $this->label = "???";
876 }
877
878 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
879
880 $sql .= " ref = '".$this->db->escape($this->ref)."'";
881 $sql .= ",label = '".$this->db->escape($this->label)."'";
882
883 $sql .= ",courant = ".((int) $this->courant);
884 $sql .= ",clos = ".((int) $this->clos);
885 $sql .= ",rappro = ".((int) $this->rappro);
886 $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
887 $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
888 $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
889 $sql .= ",bank = '".$this->db->escape($this->bank)."'";
890 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
891 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
892 $sql .= ",number='".$this->db->escape($this->number)."'";
893 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
894 $sql .= ",bic='".$this->db->escape($this->bic)."'";
895 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
896 $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
897 $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
898 $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
899 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
900 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
901 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
902 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
903
904 $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
905
906 $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
907 $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
908 $sql .= ",comment = '".$this->db->escape($this->comment)."'";
909
910 $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
911 $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
912 $sql .= ",ics = '".$this->db->escape($this->ics)."'";
913 $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
914
915 $sql .= " WHERE rowid = ".((int) $this->id);
916
917 dol_syslog(get_class($this)."::update", LOG_DEBUG);
918 $result = $this->db->query($sql);
919 if ($result) {
920 // Actions on extra fields (by external module or standard code)
921 if (!$error) {
922 $result = $this->insertExtraFields();
923 if ($result < 0) {
924 $error++;
925 }
926 }
927
928 if (!$error && !empty($this->oldref) && $this->oldref !== $this->ref) {
929 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'bank/".$this->db->escape($this->ref)."'";
930 $sql .= " WHERE filepath = 'bank/".$this->db->escape($this->oldref)."' and src_object_type='bank_account' and entity = ".((int) $conf->entity);
931 $resql = $this->db->query($sql);
932 if (!$resql) {
933 $error++;
934 $this->error = $this->db->lasterror();
935 }
936
937 // We rename directory in order not to lose the attachments
938 $oldref = dol_sanitizeFileName($this->oldref);
939 $newref = dol_sanitizeFileName($this->ref);
940 $dirsource = $conf->bank->dir_output.'/'.$oldref;
941 $dirdest = $conf->bank->dir_output.'/'.$newref;
942 if (file_exists($dirsource)) {
943 dol_syslog(get_class($this)."::update rename dir ".$dirsource." into ".$dirdest, LOG_DEBUG);
944 if (@rename($dirsource, $dirdest)) {
945 dol_syslog("Rename ok", LOG_DEBUG);
946 }
947 }
948 }
949
950 if (!$error && !$notrigger) {
951 // Call trigger
952 $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
953 if ($result < 0) {
954 $error++;
955 }
956 // End call triggers
957 }
958 } else {
959 $error++;
960 $this->error = $this->db->lasterror();
961 dol_print_error($this->db);
962 }
963
964 if (!$error) {
965 $this->db->commit();
966 return $this->id;
967 } else {
968 $this->db->rollback();
969 return -1 * $error;
970 }
971 }
972
973
974 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
981 public function update_bban(User $user = null)
982 {
983 // phpcs:enable
984 global $conf, $langs;
985
986 // Load library to get BAN control function
987 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
988
989 dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
990
991 // Check parameters
992 if (!$this->ref) {
993 $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
994 return -2;
995 }
996
997 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
998 $sql .= " bank = '".$this->db->escape($this->bank)."'";
999 $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
1000 $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
1001 $sql .= ",number='".$this->db->escape($this->number)."'";
1002 $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
1003 $sql .= ",bic='".$this->db->escape($this->bic)."'";
1004 $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
1005 $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
1006 $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
1007 $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
1008 $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
1009 $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
1010 $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
1011 $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null");
1012 $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null");
1013 $sql .= " WHERE rowid = ".((int) $this->id);
1014 $sql .= " AND entity = ".((int) $conf->entity);
1015
1016 dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
1017
1018 $result = $this->db->query($sql);
1019 if ($result) {
1020 return 1;
1021 } else {
1022 $this->error = $this->db->lasterror();
1023 dol_print_error($this->db);
1024 return -1;
1025 }
1026 }
1027
1028
1036 public function fetch($id, $ref = '')
1037 {
1038 if (empty($id) && empty($ref)) {
1039 $this->error = "ErrorBadParameters";
1040 return -1;
1041 }
1042
1043 $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,";
1044 $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
1045 $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,";
1046 $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
1047 $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
1048 $sql .= " ba.datec as date_creation, ba.tms as date_modification, ba.ics, ba.ics_transfer,";
1049 $sql .= ' c.code as country_code, c.label as country,';
1050 $sql .= ' d.code_departement as state_code, d.nom as state,';
1051 $sql .= ' aj.code as accountancy_journal';
1052 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1053 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
1054 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
1055 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal';
1056 $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
1057 if ($id) {
1058 $sql .= " AND ba.rowid = ".((int) $id);
1059 }
1060 if ($ref) {
1061 $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
1062 }
1063
1064 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1065 $result = $this->db->query($sql);
1066 if ($result) {
1067 if ($this->db->num_rows($result)) {
1068 $obj = $this->db->fetch_object($result);
1069
1070 $this->id = $obj->rowid;
1071 $this->rowid = $obj->rowid;
1072 $this->ref = $obj->ref;
1073 $this->label = $obj->label;
1074 $this->type = $obj->courant;
1075 $this->courant = $obj->courant;
1076 $this->bank = $obj->bank;
1077 $this->clos = $obj->clos;
1078 $this->rappro = $obj->rappro;
1079 $this->url = $obj->url;
1080
1081 $this->code_banque = $obj->code_banque;
1082 $this->code_guichet = $obj->code_guichet;
1083 $this->number = $obj->number;
1084 $this->cle_rib = $obj->cle_rib;
1085 $this->bic = $obj->bic;
1086 $this->iban = $obj->iban;
1087 $this->domiciliation = $obj->address;
1088 $this->address = $obj->address;
1089 $this->pti_in_ctti = $obj->pti_in_ctti;
1090 $this->proprio = $obj->proprio;
1091 $this->owner_address = $obj->owner_address;
1092 $this->owner_zip = $obj->owner_zip;
1093 $this->owner_town = $obj->owner_town;
1094 $this->owner_country_id = $obj->owner_country_id;
1095
1096 $this->state_id = $obj->state_id;
1097 $this->state_code = $obj->state_code;
1098 $this->state = $obj->state;
1099
1100 $this->country_id = $obj->country_id;
1101 $this->country_code = $obj->country_code;
1102 $this->country = $obj->country;
1103
1104 $this->account_number = $obj->account_number;
1105 $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
1106 $this->accountancy_journal = $obj->accountancy_journal;
1107
1108 $this->currency_code = $obj->currency_code;
1109 $this->account_currency_code = $obj->currency_code;
1110 $this->min_allowed = $obj->min_allowed;
1111 $this->min_desired = $obj->min_desired;
1112 $this->comment = $obj->comment;
1113
1114 $this->date_creation = $this->db->jdate($obj->date_creation);
1115 $this->date_modification = $this->db->jdate($obj->date_modification);
1116 $this->date_update = $this->date_modification; // For compatibility
1117
1118 $this->ics = $obj->ics;
1119 $this->ics_transfer = $obj->ics_transfer;
1120
1121 // Retrieve all extrafield
1122 // fetch optionals attributes and labels
1123 $this->fetch_optionals();
1124
1125 return 1;
1126 } else {
1127 return 0;
1128 }
1129 } else {
1130 $this->error = $this->db->lasterror();
1131 $this->errors[] = $this->error;
1132 return -1;
1133 }
1134 }
1135
1146 public function setCategories($categories)
1147 {
1148 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1149 return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
1150 }
1151
1158 public function delete(User $user = null)
1159 {
1160 $error = 0;
1161
1162 $this->db->begin();
1163
1164 // @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
1165
1166 // Delete link between tag and bank account
1167 if (!$error) {
1168 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
1169 $sql .= " WHERE fk_account = ".((int) $this->id);
1170
1171 $resql = $this->db->query($sql);
1172 if (!$resql) {
1173 $error++;
1174 $this->error = "Error ".$this->db->lasterror();
1175 }
1176 }
1177
1178 if (!$error) {
1179 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
1180 $sql .= " WHERE rowid = ".((int) $this->id);
1181
1182 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1183 $result = $this->db->query($sql);
1184 if ($result) {
1185 // Remove extrafields
1186 if (!$error) {
1187 $result = $this->deleteExtraFields();
1188 if ($result < 0) {
1189 $error++;
1190 dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
1191 }
1192 }
1193 } else {
1194 $error++;
1195 $this->error = "Error ".$this->db->lasterror();
1196 }
1197 }
1198
1199 if (!$error) {
1200 $this->db->commit();
1201 return 1;
1202 } else {
1203 $this->db->rollback();
1204 return -1;
1205 }
1206 }
1207
1208
1215 public function getLibStatut($mode = 0)
1216 {
1217 return $this->LibStatut($this->clos, $mode);
1218 }
1219
1220 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1228 public function LibStatut($status, $mode = 0)
1229 {
1230 // phpcs:enable
1231 global $langs;
1232 $langs->load('banks');
1233
1234 if ($status == self::STATUS_OPEN) {
1235 $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
1236 $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
1237 $statusType = 'status4';
1238 } else {
1239 $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
1240 $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
1241 $statusType = 'status5';
1242 }
1243
1244 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1245 }
1246
1247
1248 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1254 public function can_be_deleted()
1255 {
1256 // phpcs:enable
1257 $can_be_deleted = false;
1258
1259 $sql = "SELECT COUNT(rowid) as nb";
1260 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1261 $sql .= " WHERE fk_account = ".((int) $this->id);
1262
1263 $resql = $this->db->query($sql);
1264 if ($resql) {
1265 $obj = $this->db->fetch_object($resql);
1266 if ($obj->nb <= 1) {
1267 $can_be_deleted = true; // Juste le solde
1268 }
1269 } else {
1270 dol_print_error($this->db);
1271 }
1272 return $can_be_deleted;
1273 }
1274
1275
1281 public function error()
1282 {
1283 return $this->error;
1284 }
1285
1294 public function solde($option = 0, $date_end = '', $field = 'dateo')
1295 {
1296 $solde = 0;
1297
1298 $sql = "SELECT sum(amount) as amount";
1299 $sql .= " FROM ".MAIN_DB_PREFIX."bank";
1300 $sql .= " WHERE fk_account = ".((int) $this->id);
1301 if ($option == 1) {
1302 $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
1303 }
1304
1305 $resql = $this->db->query($sql);
1306 if ($resql) {
1307 if ($this->db->num_rows($resql)) {
1308 $obj = $this->db->fetch_object($resql);
1309 $solde = $obj->amount;
1310 }
1311 $this->db->free($resql);
1312 } else {
1313 $this->errors[] = $this->db->lasterror;
1314 return -1;
1315 }
1316
1317 return price2num($solde, 'MU');
1318 }
1319
1320 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1328 public function load_board(User $user, $filteraccountid = 0)
1329 {
1330 // phpcs:enable
1331 global $conf, $langs;
1332
1333 if ($user->socid) {
1334 return -1; // protection pour eviter appel par utilisateur externe
1335 }
1336
1337 $sql = "SELECT b.rowid, b.datev as datefin";
1338 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1339 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1340 $sql .= " WHERE b.rappro=0";
1341 $sql .= " AND b.fk_account = ba.rowid";
1342 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1343 $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1344 $sql .= " AND clos = 0";
1345 if ($filteraccountid) {
1346 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1347 }
1348
1349 $resql = $this->db->query($sql);
1350 if ($resql) {
1351 $langs->load("banks");
1352 $now = dol_now();
1353
1354 require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
1355
1356 $response = new WorkboardResponse();
1357 $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
1358 $response->label = $langs->trans("TransactionsToConciliate");
1359 $response->labelShort = $langs->trans("TransactionsToConciliateShort");
1360 $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
1361 $response->img = img_object('', "payment");
1362
1363 while ($obj = $this->db->fetch_object($resql)) {
1364 $response->nbtodo++;
1365 if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
1366 $response->nbtodolate++;
1367 }
1368 }
1369
1370 return $response;
1371 } else {
1372 dol_print_error($this->db);
1373 $this->error = $this->db->error();
1374 return -1;
1375 }
1376 }
1377
1378 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1385 public function load_state_board($filteraccountid = 0)
1386 {
1387 // phpcs:enable
1388 global $user;
1389
1390 if ($user->socid) {
1391 return -1; // protection pour eviter appel par utilisateur externe
1392 }
1393
1394 $sql = "SELECT count(b.rowid) as nb";
1395 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
1396 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
1397 $sql .= " WHERE b.fk_account = ba.rowid";
1398 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1399 $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
1400 $sql .= " AND clos = 0";
1401 if ($filteraccountid) {
1402 $sql .= " AND ba.rowid = ".((int) $filteraccountid);
1403 }
1404
1405 $resql = $this->db->query($sql);
1406 if ($resql) {
1407 while ($obj = $this->db->fetch_object($resql)) {
1408 $this->nb["banklines"] = $obj->nb;
1409 }
1410 $this->db->free($resql);
1411 return 1;
1412 } else {
1413 dol_print_error($this->db);
1414 $this->error = $this->db->error();
1415 return -1;
1416 }
1417 }
1418
1419
1425 public function countAccountToReconcile()
1426 {
1427 global $db, $conf, $user;
1428
1429 //Protection against external users
1430 if ($user->socid) {
1431 return 0;
1432 }
1433
1434 $nb = 0;
1435
1436 $sql = "SELECT COUNT(ba.rowid) as nb";
1437 $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
1438 $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
1439 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
1440 if (empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
1441 $sql .= " AND ba.courant != 2";
1442 }
1443 $resql = $this->db->query($sql);
1444 if ($resql) {
1445 $obj = $this->db->fetch_object($resql);
1446 $nb = $obj->nb;
1447 } else {
1448 dol_print_error($this->db);
1449 }
1450
1451 return $nb;
1452 }
1453
1461 public function getTooltipContentArray($params)
1462 {
1463 global $langs;
1464 $langs->loadLangs(['banks', 'compta']);
1465 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1466
1467 $datas = array();
1468
1469 $nofetch = !empty($params['nofetch']);
1470 $pictos = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
1471 if (isset($this->status)) {
1472 $pictos .= ' '.$this->getLibStatut(5);
1473 }
1474 $datas['picto'] = $pictos;
1475 $datas['label'] = '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1476 $datas['accountnumber'] = '<br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
1477 $datas['iban'] = '<br><b>'.$langs->trans('IBAN').':</b> '.getIbanHumanReadable($this);
1478 $datas['bic'] = '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
1479 $datas['accountcurrency'] = '<br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
1480
1481 if (isModEnabled('accounting')) {
1482 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1483 $langs->load("accountancy");
1484 $datas['accountaccounting'] = '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
1485 $datas['accountancyjournal'] = '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
1486 }
1487 // show categories for this record only in ajax to not overload lists
1488 if (isModEnabled('categorie') && !$nofetch) {
1489 require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
1490 $form = new Form($this->db);
1491 $datas['categories'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_ACCOUNT, 1);
1492 }
1493
1494 return $datas;
1495 }
1496
1508 public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1509 {
1510 global $conf, $langs;
1511
1512 if (!empty($conf->dol_no_mouse_hover)) {
1513 $notooltip = 1; // Force disable tooltips
1514 }
1515
1516 include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1517
1518 $result = '';
1519 $classfortooltip = 'classfortooltip';
1520 $dataparams = '';
1521 $params = [
1522 'id' => $this->id,
1523 'objecttype' => $this->element,
1524 'option' => $option,
1525 'nofetch' => 1,
1526 ];
1527 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1528 $classfortooltip = 'classforajaxtooltip';
1529 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1530 $label = '';
1531 } else {
1532 $label = implode($this->getTooltipContentArray($params));
1533 }
1534
1535 $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
1536 if ($mode == 'transactions') {
1537 $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
1538 } elseif ($mode == 'receipts') {
1539 $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
1540 }
1541
1542 if ($option != 'nolink') {
1543 // Add param to save lastsearch_values or not
1544 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1545 if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1546 $add_save_lastsearch_values = 1;
1547 }
1548 if ($add_save_lastsearch_values) {
1549 $url .= '&save_lastsearch_values=1';
1550 }
1551 }
1552
1553 $linkclose = '';
1554 if (empty($notooltip)) {
1555 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1556 $label = $langs->trans("BankAccount");
1557 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1558 }
1559 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1560 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1561 } else {
1562 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1563 }
1564
1565 if ($option == 'nolink' || empty($url)) {
1566 $linkstart = '<span';
1567 } else {
1568 $linkstart = '<a href="'.$url.'"';
1569 }
1570 $linkstart .= $linkclose.'>';
1571 if ($option == 'nolink' || empty($url)) {
1572 $linkend = '</span>';
1573 } else {
1574 $linkend = '</a>';
1575 }
1576
1577 $result .= $linkstart;
1578
1579 if ($withpicto) {
1580 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1581 }
1582 if ($withpicto != 2) {
1583 $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
1584 }
1585 $result .= $linkend;
1586
1587 return $result;
1588 }
1589
1590
1591 // Method after here are common to Account and CompanyBankAccount
1592
1593
1599 public function verif()
1600 {
1601 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
1602
1603 $this->error_number = 0;
1604
1605 // Call function to check BAN
1606
1607 if (!checkIbanForAccount($this)) {
1608 $this->error_number = 12;
1609 $this->error_message = 'IBANNotValid';
1610 }
1611 if (!checkSwiftForAccount($this)) {
1612 $this->error_number = 12;
1613 $this->error_message = 'SwiftNotValid';
1614 }
1615 /*if (! checkBanForAccount($this))
1616 {
1617 $this->error_number = 12;
1618 $this->error_message = 'BANControlError';
1619 }*/
1620
1621 if ($this->error_number == 0) {
1622 return 1;
1623 } else {
1624 return 0;
1625 }
1626 }
1627
1633 public function getCountryCode()
1634 {
1635 global $mysoc;
1636
1637 // We return country code of bank account
1638 if (!empty($this->country_code)) {
1639 return $this->country_code;
1640 }
1641
1642 // For backward compatibility, we try to guess country from other information
1643 if (!empty($this->iban)) {
1644 // If IBAN defined, we can know country of account from it
1645 $reg = array();
1646 if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
1647 return $reg[1];
1648 }
1649 }
1650
1651 // If this class is linked to a third party
1652 if (!empty($this->socid)) {
1653 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
1654 $company = new Societe($this->db);
1655 $result = $company->fetch($this->socid);
1656 if (!empty($company->country_code)) {
1657 return $company->country_code;
1658 }
1659 }
1660
1661 // We return country code of managed company
1662 if (!empty($mysoc->country_code)) {
1663 return $mysoc->country_code;
1664 }
1665
1666 return '';
1667 }
1668
1677 public function useDetailedBBAN()
1678 {
1679 $country_code = $this->getCountryCode();
1680
1681 if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
1682 return 1; // France, Spain, Gabon, ... - Not valid for CH
1683 }
1684 if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
1685 return 2; // Australia, England...
1686 }
1687 return 0;
1688 }
1689
1695 public function needIBAN()
1696 {
1697 global $conf;
1698
1699 if (!empty($conf->global->MAIN_IBAN_IS_NEVER_MANDATORY)) {
1700 return 0;
1701 }
1702
1703 $country_code = $this->getCountryCode();
1704
1705 $country_code_in_EEC = array(
1706 'AT', // Austria
1707 'BE', // Belgium
1708 'BG', // Bulgaria
1709 'CY', // Cyprus
1710 'CZ', // Czech republic
1711 'DE', // Germany
1712 'DK', // Danemark
1713 'EE', // Estonia
1714 'ES', // Spain
1715 'FI', // Finland
1716 'FR', // France
1717 'GB', // United Kingdom
1718 'GR', // Greece
1719 'HR', // Croatia
1720 'NL', // Holland
1721 'HU', // Hungary
1722 'IE', // Ireland
1723 'IM', // Isle of Man - Included in UK
1724 'IT', // Italy
1725 'LT', // Lithuania
1726 'LU', // Luxembourg
1727 'LV', // Latvia
1728 'MC', // Monaco - Included in France
1729 'MT', // Malta
1730 //'NO', // Norway
1731 'PL', // Poland
1732 'PT', // Portugal
1733 'RO', // Romania
1734 'SE', // Sweden
1735 'SK', // Slovakia
1736 'SI', // Slovenia
1737 'UK', // United Kingdom
1738 //'CH', // Switzerland - No. Swizerland in not in EEC
1739 );
1740
1741 if (in_array($country_code, $country_code_in_EEC)) {
1742 return 1; // France, Spain, ...
1743 }
1744 return 0;
1745 }
1746
1753 public function info($id)
1754 {
1755 }
1756
1771 public function getFieldsToShow($includeibanbic = 0)
1772 {
1773 //Get the required properties depending on the country
1774 $detailedBBAN = $this->useDetailedBBAN();
1775
1776 if ($detailedBBAN == 0) {
1777 $fieldarray = array(
1778 'BankAccountNumber'
1779 );
1780 } elseif ($detailedBBAN == 2) {
1781 $fieldarray = array(
1782 'BankCode',
1783 'BankAccountNumber'
1784 );
1785 } else {
1786 $fieldarray = self::getAccountNumberOrder();
1787 }
1788
1789 //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
1790 if ($includeibanbic) {
1791 $fieldarray[] = 'IBAN';
1792 $fieldarray[] = 'BIC';
1793 }
1794 //}
1795
1796 //Get the order the properties are shown
1797 return $fieldarray;
1798 }
1799
1810 public static function getAccountNumberOrder()
1811 {
1812 global $conf;
1813
1814 $fieldlists = array(
1815 'BankCode',
1816 'DeskCode',
1817 'BankAccountNumber',
1818 'BankAccountNumberKey'
1819 );
1820
1821 if (!empty($conf->global->BANK_SHOW_ORDER_OPTION)) {
1822 if (is_numeric($conf->global->BANK_SHOW_ORDER_OPTION)) {
1823 if ($conf->global->BANK_SHOW_ORDER_OPTION == '1') {
1824 $fieldlists = array(
1825 'BankCode',
1826 'DeskCode',
1827 'BankAccountNumberKey',
1828 'BankAccountNumber'
1829 );
1830 }
1831 } else {
1832 //Replace the old AccountNumber key with the new BankAccountNumber key
1833 $fieldlists = explode(
1834 ' ',
1835 preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
1836 );
1837 }
1838 }
1839
1840 return $fieldlists;
1841 }
1842
1843
1851 public function initAsSpecimen()
1852 {
1853 // Example of IBAN FR7630001007941234567890185
1854 $this->specimen = 1;
1855 $this->ref = 'MBA';
1856 $this->label = 'My Big Company Bank account';
1857 $this->bank = 'MyBank';
1858 $this->courant = Account::TYPE_CURRENT;
1859 $this->clos = Account::STATUS_OPEN;
1860 $this->code_banque = '30001';
1861 $this->code_guichet = '00794';
1862 $this->number = '12345678901';
1863 $this->cle_rib = '85';
1864 $this->bic = 'AA12';
1865 $this->iban = 'FR7630001007941234567890185';
1866 $this->domiciliation = 'Banque de France';
1867 $this->proprio = 'Owner';
1868 $this->owner_address = 'Owner address';
1869 $this->owner_zip = 'Owner zip';
1870 $this->owner_town = 'Owner town';
1871 $this->owner_country_id = 'Owner country_id';
1872 $this->country_id = 1;
1873 }
1874
1883 public static function replaceThirdparty($dbs, $origin_id, $dest_id)
1884 {
1885 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'";
1886
1887 if ($dbs->query($sql)) {
1888 return true;
1889 } else {
1890 //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.
1891 //$this->errors = $dbs->lasterror();
1892 return false;
1893 }
1894 }
1895
1903 public function getKanbanView($option = '', $arraydata = null)
1904 {
1905 global $langs;
1906
1907 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1908
1909 $return = '<div class="box-flex-item box-flex-grow-zero">';
1910 $return .= '<div class="info-box info-box-sm">';
1911 $return .= '<span class="info-box-icon bg-infobox-action">';
1912 $return .= img_picto('', $this->picto);
1913 $return .= '</span>';
1914 $return .= '<div class="info-box-content">';
1915 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
1916 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1917
1918 if (property_exists($this, 'type_lib')) {
1919 $return .= '<br><span class="info-box-label opacitymedium" title="'.$this->type_lib[$this->type].'">'.substr($this->type_lib[$this->type], 0, 24).'...</span>';
1920 }
1921 if (method_exists($this, 'solde')) {
1922 $return .= '<br><a href="'.DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id.'">';
1923 $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>';
1924 }
1925 if (method_exists($this, 'getLibStatut')) {
1926 $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
1927 }
1928 $return .= '</div>';
1929 $return .= '</div>';
1930 $return .= '</div>';
1931 return $return;
1932 }
1933}
1934
1935
1936require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
1937
1942{
1946 public $error = '';
1947
1951 public $db;
1952
1956 public $element = 'bank';
1957
1961 public $table_element = 'bank';
1962
1966 public $picto = 'accountline';
1967
1971 public $id;
1972
1976 public $ref;
1977
1983 public $datec;
1984
1990 public $dateo;
1991
1997 public $datev;
1998
1999 public $amount; /* Amount of payment in the bank account currency */
2000 public $amount_main_currency; /* Amount in the currency of company if bank account use another currency */
2001
2005 public $fk_user_author;
2006
2010 public $fk_user_rappro;
2011
2015 public $fk_type;
2016
2020 public $fk_bordereau;
2021
2025 public $fk_account;
2026
2030 public $bank_account_ref;
2031
2035 public $bank_account_label;
2036
2040 public $numero_compte;
2041
2045 public $emetteur;
2046
2047 public $rappro; // Is it conciliated
2048 public $num_releve; // If conciliated, what is bank statement
2049 public $num_chq; // Num of cheque
2050 public $bank_chq; // Bank of cheque
2051
2055 public $label;
2056
2057 public $note;
2058
2059
2060
2066 public function __construct(DoliDB $db)
2067 {
2068 $this->db = $db;
2069 }
2070
2079 public function fetch($rowid, $ref = '', $num = '')
2080 {
2081 global $conf;
2082
2083 // Check parameters
2084 if (empty($rowid) && empty($ref) && empty($num)) {
2085 return -1;
2086 }
2087
2088 $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
2089 $sql .= " b.fk_user_author, b.fk_user_rappro,";
2090 $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
2091 $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
2092 $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
2093 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
2094 $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
2095 $sql .= " WHERE b.fk_account = ba.rowid";
2096 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
2097 if ($num) {
2098 $sql .= " AND b.num_chq='".$this->db->escape($num)."'";
2099 } elseif ($ref) {
2100 $sql .= " AND b.rowid='".$this->db->escape($ref)."'";
2101 } else {
2102 $sql .= " AND b.rowid = ".((int) $rowid);
2103 }
2104
2105 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
2106 $result = $this->db->query($sql);
2107 if ($result) {
2108 $ret = 0;
2109
2110 $obj = $this->db->fetch_object($result);
2111 if ($obj) {
2112 $this->id = $obj->rowid;
2113 $this->rowid = $obj->rowid;
2114 $this->ref = $obj->rowid;
2115
2116 $this->datec = $obj->datec;
2117 $this->datev = $obj->datev;
2118 $this->dateo = $obj->dateo;
2119 $this->amount = $obj->amount;
2120 $this->label = $obj->label;
2121 $this->note = $obj->note;
2122
2123 $this->fk_user_author = $obj->fk_user_author;
2124 $this->fk_user_rappro = $obj->fk_user_rappro;
2125
2126 $this->fk_type = $obj->fk_type; // Type of transaction
2127 $this->rappro = $obj->rappro;
2128 $this->num_releve = $obj->num_releve;
2129
2130 $this->num_chq = $obj->num_chq;
2131 $this->bank_chq = $obj->banque;
2132 $this->fk_bordereau = $obj->fk_bordereau;
2133
2134 $this->fk_account = $obj->fk_account;
2135 $this->bank_account_ref = $obj->bank_account_ref;
2136 $this->bank_account_label = $obj->bank_account_label;
2137
2138 // Retrieve all extrafield
2139 // fetch optionals attributes and labels
2140 $this->fetch_optionals();
2141
2142 $ret = 1;
2143 }
2144 $this->db->free($result);
2145 return $ret;
2146 } else {
2147 return -1;
2148 }
2149 }
2150
2156 public function insert()
2157 {
2158 $error = 0;
2159
2160 $this->db->begin();
2161
2162 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
2163 $sql .= "datec";
2164 $sql .= ", dateo";
2165 $sql .= ", datev";
2166 $sql .= ", label";
2167 $sql .= ", amount";
2168 $sql .= ", amount_main_currency";
2169 $sql .= ", fk_user_author";
2170 $sql .= ", num_chq";
2171 $sql .= ", fk_account";
2172 $sql .= ", fk_type";
2173 $sql .= ", emetteur,banque";
2174 $sql .= ", rappro";
2175 $sql .= ", numero_compte";
2176 $sql .= ", num_releve";
2177 $sql .= ") VALUES (";
2178 $sql .= "'".$this->db->idate($this->datec)."'";
2179 $sql .= ", '".$this->db->idate($this->dateo)."'";
2180 $sql .= ", '".$this->db->idate($this->datev)."'";
2181 $sql .= ", '".$this->db->escape($this->label)."'";
2182 $sql .= ", ".price2num($this->amount);
2183 $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
2184 $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
2185 $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
2186 $sql .= ", '".$this->db->escape($this->fk_account)."'";
2187 $sql .= ", '".$this->db->escape($this->fk_type)."'";
2188 $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
2189 $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
2190 $sql .= ", ".(int) $this->rappro;
2191 $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
2192 $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
2193 $sql .= ")";
2194
2195 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
2196 $resql = $this->db->query($sql);
2197 if ($resql) {
2198 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
2199 // Actions on extra fields (by external module or standard code)
2200 $result = $this->insertExtraFields();
2201 if ($result < 0) {
2202 $error++;
2203 }
2204 } else {
2205 $error++;
2206 $this->error = $this->db->lasterror();
2207 dol_print_error($this->db);
2208 }
2209
2210 if (!$error) {
2211 $this->db->commit();
2212 return $this->id;
2213 } else {
2214 $this->db->rollback();
2215 return -1 * $error;
2216 }
2217 }
2218
2225 public function delete(User $user = null)
2226 {
2227 global $conf;
2228
2229 $nbko = 0;
2230
2231 if ($this->rappro) {
2232 // Protection to avoid any delete of consolidated lines
2233 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2234 return -1;
2235 }
2236
2237 $this->db->begin();
2238
2239 // Protection to avoid any delete of accounted lines. Protection on by default
2240 if (empty($conf->global->BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING)) {
2241 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2242 $resql = $this->db->query($sql);
2243 if ($resql) {
2244 $obj = $this->db->fetch_object($resql);
2245 if ($obj && $obj->nb) {
2246 $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
2247 $this->db->rollback();
2248 return -1;
2249 }
2250 } else {
2251 $this->error = $this->db->lasterror();
2252 $this->db->rollback();
2253 return -1;
2254 }
2255 }
2256
2257 // Delete urls
2258 $result = $this->delete_urls($user);
2259 if ($result < 0) {
2260 $nbko++;
2261 }
2262
2263 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".(int) $this->rowid;
2264 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2265 $result = $this->db->query($sql);
2266 if (!$result) {
2267 $nbko++;
2268 }
2269
2270 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_extrafields WHERE fk_object=".(int) $this->rowid;
2271 $result = $this->db->query($sql);
2272 if (!$result) {
2273 $nbko++;
2274 }
2275
2276 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
2277 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
2278 $result = $this->db->query($sql);
2279 if (!$result) {
2280 $nbko++;
2281 }
2282
2283 if (!$nbko) {
2284 $this->db->commit();
2285 return 1;
2286 } else {
2287 $this->db->rollback();
2288 return -$nbko;
2289 }
2290 }
2291
2292
2293 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2300 public function delete_urls(User $user = null)
2301 {
2302 // phpcs:enable
2303 $nbko = 0;
2304
2305 if ($this->rappro) {
2306 // Protection to avoid any delete of consolidated lines
2307 $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
2308 return -1;
2309 }
2310
2311 $this->db->begin();
2312
2313 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
2314 dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
2315 $result = $this->db->query($sql);
2316 if (!$result) {
2317 $nbko++;
2318 }
2319
2320 if (!$nbko) {
2321 $this->db->commit();
2322 return 1;
2323 } else {
2324 $this->db->rollback();
2325 return -$nbko;
2326 }
2327 }
2328
2329
2337 public function update(User $user, $notrigger = 0)
2338 {
2339 $this->db->begin();
2340
2341 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2342 $sql .= " amount = ".price2num($this->amount).",";
2343 $sql .= " datev='".$this->db->idate($this->datev)."',";
2344 $sql .= " dateo='".$this->db->idate($this->dateo)."'";
2345 $sql .= " WHERE rowid = ".((int) $this->rowid);
2346
2347 dol_syslog(get_class($this)."::update", LOG_DEBUG);
2348 $resql = $this->db->query($sql);
2349 if ($resql) {
2350 $this->db->commit();
2351 return 1;
2352 } else {
2353 $this->db->rollback();
2354 $this->error = $this->db->error();
2355 return -1;
2356 }
2357 }
2358
2359
2360 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2369 public function update_conciliation(User $user, $cat, $conciliated = 1)
2370 {
2371 // phpcs:enable
2372 global $conf, $langs;
2373
2374 $this->db->begin();
2375
2376 // Check statement field
2377 if (!empty($conf->global->BANK_STATEMENT_REGEX_RULE)) {
2378 if (!preg_match('/'.$conf->global->BANK_STATEMENT_REGEX_RULE.'/', $this->num_releve)) {
2379 $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", $conf->global->BANK_STATEMENT_REGEX_RULE);
2380 return -1;
2381 }
2382 }
2383
2384 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2385 $sql .= " rappro = ".((int) $conciliated);
2386 $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
2387 if ($conciliated) {
2388 $sql .= ", fk_user_rappro = ".$user->id;
2389 }
2390 $sql .= " WHERE rowid = ".((int) $this->id);
2391
2392 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2393 $resql = $this->db->query($sql);
2394 if ($resql) {
2395 if (!empty($cat) && $cat > 0) {
2396 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
2397 $sql .= "lineid";
2398 $sql .= ", fk_categ";
2399 $sql .= ") VALUES (";
2400 $sql .= $this->id;
2401 $sql .= ", ".((int) $cat);
2402 $sql .= ")";
2403
2404 dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
2405 $this->db->query($sql);
2406
2407 // No error check. Can fail if category already affected
2408 // TODO Do no try the insert if link already exists
2409 }
2410
2411 $this->rappro = 1;
2412
2413 $this->db->commit();
2414 return 1;
2415 } else {
2416 $this->db->rollback();
2417 return -1;
2418 }
2419 }
2420
2421
2422 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2430 public function datev_change($rowid, $sign = 1)
2431 {
2432 // phpcs:enable
2433 $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2434 $resql = $this->db->query($sql);
2435 if ($resql) {
2436 $obj = $this->db->fetch_object($resql);
2437 $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
2438
2439 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2440 $sql .= " datev = '".$this->db->idate($newdate)."'";
2441 $sql .= " WHERE rowid = ".((int) $rowid);
2442
2443 $result = $this->db->query($sql);
2444 if ($result) {
2445 if ($this->db->affected_rows($result)) {
2446 return 1;
2447 }
2448 } else {
2449 dol_print_error($this->db);
2450 return 0;
2451 }
2452 } else {
2453 dol_print_error($this->db);
2454 }
2455 return 0;
2456 }
2457
2458 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2465 public function datev_next($id)
2466 {
2467 // phpcs:enable
2468 return $this->datev_change($id, 1);
2469 }
2470
2471 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2478 public function datev_previous($id)
2479 {
2480 // phpcs:enable
2481 return $this->datev_change($id, -1);
2482 }
2483
2484
2485 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2493 public function dateo_change($rowid, $sign = 1)
2494 {
2495 // phpcs:enable
2496 $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
2497 $resql = $this->db->query($sql);
2498 if ($resql) {
2499 $obj = $this->db->fetch_object($resql);
2500 $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
2501
2502 $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
2503 $sql .= " dateo = '".$this->db->idate($newdate)."'";
2504 $sql .= " WHERE rowid = ".((int) $rowid);
2505
2506 $result = $this->db->query($sql);
2507 if ($result) {
2508 if ($this->db->affected_rows($result)) {
2509 return 1;
2510 }
2511 } else {
2512 dol_print_error($this->db);
2513 return 0;
2514 }
2515 } else {
2516 dol_print_error($this->db);
2517 }
2518 return 0;
2519 }
2520
2521 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2528 public function dateo_next($id)
2529 {
2530 // phpcs:enable
2531 return $this->dateo_change($id, 1);
2532 }
2533
2534 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2541 public function dateo_previous($id)
2542 {
2543 // phpcs:enable
2544 return $this->dateo_change($id, -1);
2545 }
2546
2547
2554 public function info($id)
2555 {
2556 $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
2557 $sql .= ' b.fk_user_author, b.fk_user_rappro';
2558 $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
2559 $sql .= ' WHERE b.rowid = '.((int) $id);
2560
2561 $result = $this->db->query($sql);
2562 if ($result) {
2563 if ($this->db->num_rows($result)) {
2564 $obj = $this->db->fetch_object($result);
2565 $this->id = $obj->rowid;
2566
2567 if ($obj->fk_user_author) {
2568 $cuser = new User($this->db);
2569 $cuser->fetch($obj->fk_user_author);
2570 $this->user_creation = $cuser;
2571 }
2572 if ($obj->fk_user_rappro) {
2573 $ruser = new User($this->db);
2574 $ruser->fetch($obj->fk_user_rappro);
2575 $this->user_rappro = $ruser;
2576 }
2577
2578 $this->date_creation = $this->db->jdate($obj->datec);
2579 $this->date_modification = $this->db->jdate($obj->datem);
2580 //$this->date_rappro = $obj->daterappro; // Not yet managed
2581 }
2582 $this->db->free($result);
2583 } else {
2584 dol_print_error($this->db);
2585 }
2586 }
2587
2588
2598 public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
2599 {
2600 global $conf, $langs;
2601
2602 $result = '';
2603
2604 $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
2605 $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
2606 if ($this->amount) {
2607 $label .= '<br><strong>'.$langs->trans("Amount").':</strong> '.price($this->amount, 0, $langs, 1, -1, -1, $conf->currency);
2608 }
2609
2610 $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">';
2611 $linkend = '</a>';
2612
2613 $result .= $linkstart;
2614 if ($withpicto) {
2615 $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);
2616 }
2617 if ($withpicto != 2) {
2618 $result .= ($this->ref ? $this->ref : $this->id);
2619 }
2620
2621 $result .= $linkend;
2622
2623 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2624 $result .= ' <span class="opacitymedium">(';
2625 }
2626 if ($option == 'showall') {
2627 $result .= $langs->trans("BankAccount").': ';
2628 $accountstatic = new Account($this->db);
2629 $accountstatic->id = $this->fk_account;
2630 $accountstatic->ref = $this->bank_account_ref;
2631 $accountstatic->label = $this->bank_account_label;
2632 $result .= $accountstatic->getNomUrl(0).', ';
2633 }
2634 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2635 $result .= $langs->trans("BankLineConciliated").': ';
2636 $result .= yn($this->rappro);
2637 }
2638 if (isModEnabled('accounting') && ($option == 'showall' || $option == 'showconciliatedandaccounted')) {
2639 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
2640 $sql .= " WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
2641 $resql = $this->db->query($sql);
2642 if ($resql) {
2643 $obj = $this->db->fetch_object($resql);
2644 if ($obj && $obj->nb) {
2645 $result .= ' - '.$langs->trans("Accounted").': '.yn(1);
2646 } else {
2647 $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
2648 }
2649 }
2650 }
2651 if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
2652 $result .= ')</span>';
2653 }
2654
2655 return $result;
2656 }
2657
2658
2665 public function getLibStatut($mode = 0)
2666 {
2667 return $this->LibStatut($this->status, $mode);
2668 }
2669
2670 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2678 public function LibStatut($status, $mode = 0)
2679 {
2680 // phpcs:enable
2681 //global $langs;
2682
2683 //$langs->load('companies');
2684 /*
2685 if ($mode == 0)
2686 {
2687 if ($status==0) return $langs->trans("ActivityCeased");
2688 if ($status==1) return $langs->trans("InActivity");
2689 }
2690 if ($mode == 1)
2691 {
2692 if ($status==0) return $langs->trans("ActivityCeased");
2693 if ($status==1) return $langs->trans("InActivity");
2694 }
2695 if ($mode == 2)
2696 {
2697 if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2698 if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2699 }
2700 if ($mode == 3)
2701 {
2702 if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2703 if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2704 }
2705 if ($mode == 4)
2706 {
2707 if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2708 if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2709 }
2710 if ($mode == 5)
2711 {
2712 if ($status==0) return $langs->trans("ActivityCeased").' '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2713 if ($status==1) return $langs->trans("InActivity").' '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2714 }*/
2715
2716 return '';
2717 }
2718
2719
2725 public function getVentilExportCompta()
2726 {
2727 $alreadydispatched = 0;
2728
2729 $type = 'bank';
2730
2731 $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);
2732 $resql = $this->db->query($sql);
2733 if ($resql) {
2734 $obj = $this->db->fetch_object($resql);
2735 if ($obj) {
2736 $alreadydispatched = $obj->nb;
2737 }
2738 } else {
2739 $this->error = $this->db->lasterror();
2740 return -1;
2741 }
2742
2743 if ($alreadydispatched) {
2744 return 1;
2745 }
2746 return 0;
2747 }
2748}
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
checkIbanForAccount(Account $account)
Check IBAN number informations for a bank account.
Definition bank.lib.php:295
getIbanHumanReadable(Account $account)
Returns the iban human readable.
Definition bank.lib.php:317
checkSwiftForAccount($account)
Check SWIFT informations for a bank account.
Definition bank.lib.php:279
$object ref
Definition info.php:78
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.
static replaceThirdparty($dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
update(User $user, $notrigger=0)
Update bank account card.
getCountryCode()
Return account country code.
needIBAN()
Return 1 if IBAN / BIC is mandatory (otherwise option)
getLibStatut($mode=0)
Return label of object status.
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.
useDetailedBBAN()
Return if a bank account is defined with detailed information (bank code, desk code,...
static getAccountNumberOrder()
Returns the components of the bank account in order.
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.
initAsSpecimen()
Initialise an instance with random values.
get_url($fk_bank='', $url_id='', $type='')
TODO Move this into AccountLine Return array with links from llx_bank_url.
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
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)
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.
info($id)
Load miscellaneous information for tab "Info".
__construct(DoliDB $db)
Constructor.
update_conciliation(User $user, $cat, $conciliated=1)
Update conciliation field.
getLibStatut($mode=0)
Return the label of the status.
fetch($rowid, $ref='', $num='')
Load into memory content of a bank transaction line.
dateo_change($rowid, $sign=1)
Increase/decrease operation date of a rowid.
datev_next($id)
Increase value date of a rowid.
LibStatut($status, $mode=0)
Return the label of a given status.
update(User $user, $notrigger=0)
Update bank account record in database.
insert()
Inserts a transaction to a bank account.
getNomUrl($withpicto=0, $maxlen=0, $option='', $notooltip=0)
Return clickable name (with picto eventually)
delete_urls(User $user=null)
Delete bank line records.
getVentilExportCompta()
Return if a bank line was dispatched into bookkeeping.
datev_change($rowid, $sign=1)
Increase/decrease value date of a rowid.
dateo_previous($id)
Decrease operation date of a rowid.
dateo_next($id)
Increase operation date of a rowid.
datev_previous($id)
Decrease value date of a rowid.
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...
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.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
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.
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
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)
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return 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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e rowid
Definition invoice.php:1632
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:120