2004-10-19 22:35:36 +02:00
< ? php
2015-03-02 22:44:11 +01:00
/* Copyright ( C ) 2001 - 2007 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2003 Jean - Louis Bergamo < jlb @ j1b . org >
* Copyright ( C ) 2004 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2004 Christophe Combelles < ccomb @ free . fr >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2010 Regis Houssin < regis . houssin @ inodbox . com >
2015-03-02 22:44:11 +01:00
* Copyright ( C ) 2013 Florian Henry < florian . henry @ open - concept . pro >
2016-03-01 15:46:06 +01:00
* Copyright ( C ) 2015 - 2016 Marcos García < marcosgdf @ gmail . com >
2019-01-28 21:39:22 +01:00
* Copyright ( C ) 2015 - 2017 Alexandre Spangaro < aspangaro @ open - dsi . fr >
2016-04-28 11:14:00 +02:00
* Copyright ( C ) 2016 Ferran Marcet < fmarcet @ 2 byte . es >
2020-11-09 13:17:24 +01:00
* Copyright ( C ) 2019 JC Prieto < jcprieto @ virtual20 . com >< prietojc @ gmail . com >
2012-03-21 12:33:51 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2012-03-21 12:33:51 +01:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2012-03-21 12:33:51 +01:00
*/
2002-12-23 15:19:41 +01:00
2005-02-04 23:49:22 +01:00
/**
2010-06-17 23:19:00 +02:00
* \file htdocs / compta / bank / class / account . class . php
2015-03-02 22:44:11 +01:00
* \ingroup bank
2011-03-31 17:27:18 +02:00
* \brief File of class to manage bank accounts
2008-07-31 18:07:40 +02:00
*/
2019-11-14 12:09:15 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2007-11-05 23:37:41 +01:00
2004-09-30 21:31:24 +02:00
2005-06-11 13:56:51 +02:00
/**
2012-03-21 12:33:51 +01:00
* Class to manage bank accounts
2008-07-31 18:07:40 +02:00
*/
2007-11-05 23:37:41 +01:00
class Account extends CommonObject
2003-06-20 16:30:08 +02:00
{
2018-08-23 17:07:27 +02:00
/**
* @ var string ID to identify managed object
*/
2017-10-13 13:28:26 +02:00
public $element = 'bank_account' ;
2018-09-02 13:38:11 +02:00
2018-08-22 18:34:50 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2017-10-13 13:28:26 +02:00
public $table_element = 'bank_account' ;
2018-09-02 13:38:11 +02:00
2018-09-05 10:31:12 +02:00
/**
* @ var string String with name of icon for myobject . Must be the part after the 'object_' into object_myobject . png
*/
2017-10-13 13:28:26 +02:00
public $picto = 'account' ;
/**
* @ var int Use id instead of rowid
* @ deprecated
2018-10-24 02:58:29 +02:00
* @ see $id
2017-10-13 13:28:26 +02:00
*/
public $rowid ;
/**
2018-09-28 14:44:47 +02:00
* Account Label
2017-10-13 13:28:26 +02:00
* @ var string
*/
public $label ;
/**
* Bank account type . Check TYPE_ constants
* @ var int
*/
public $courant ;
/**
* Bank account type . Check TYPE_ constants
* @ var int
*/
public $type ;
/**
* Bank name
* @ var string
*/
public $bank ;
/**
* Status
* @ var int
*/
public $clos = self :: STATUS_OPEN ;
/**
* Does it need to be conciliated ?
* @ var int
*/
2019-11-14 12:09:15 +01:00
public $rappro = 1 ;
2017-10-13 13:28:26 +02:00
/**
* Webpage
* @ var string
*/
public $url ;
/**
* Bank number . If in SEPA area , you should move to IBAN field
* @ var string
*/
public $code_banque ;
/**
* Branch number . If in SEPA area , you should move to IBAN field
* @ var string
*/
public $code_guichet ;
/**
* Account number . If in SEPA area , you should move to IBAN field
* @ var string
*/
public $number ;
/**
* Bank account number control digit . If in SEPA area , you should move to IBAN field
* @ var string
*/
public $cle_rib ;
/**
* BIC / Swift code
* @ var string
*/
public $bic ;
/**
2021-12-21 14:51:57 +01:00
* IBAN number ( International Bank Account Number ) . Stored into iban_prefix field into database ( TODO Rename field in database )
2019-12-11 23:46:12 +01:00
* @ var string
2017-10-13 13:28:26 +02:00
*/
public $iban ;
2021-12-21 14:51:57 +01:00
/**
* IBAN number
*
* @ var string
* @ deprecated see $iban
*/
public $iban_prefix ;
2022-06-13 03:53:40 +02:00
/**
* Address of the bank
* @ var string
*/
public $domiciliation ;
2022-02-23 12:11:40 +01:00
/**
* XML SEPA format : place Payment Type Information ( PmtTpInf ) in Credit Transfer Transaction Information ( CdtTrfTxInf )
* @ var int
*/
public $pti_in_ctti = 0 ;
2017-10-13 13:28:26 +02:00
/**
* Name of account holder
* @ var string
*/
public $proprio ;
/**
* Address of account holder
* @ var string
*/
public $owner_address ;
public $state_id ;
public $state_code ;
public $state ;
2016-03-01 15:46:06 +01:00
2016-03-02 11:04:52 +01:00
/**
* Variable containing all account types with their respective translated label .
* Defined in __construct
* @ var array
*/
2017-10-13 13:28:26 +02:00
public $type_lib = array ();
2016-03-02 11:04:52 +01:00
/**
* Variable containing all account statuses with their respective translated label .
* Defined in __construct
* @ var array
*/
public $status = array ();
2016-03-01 15:46:06 +01:00
2017-10-13 13:28:26 +02:00
/**
* Accountancy code
* @ var string
*/
public $account_number ;
2018-10-09 15:38:42 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2017-05-06 08:33:12 +02:00
public $fk_accountancy_journal ;
2021-06-05 01:29:24 +02:00
/**
* @ var string Label of journal
*/
public $accountancy_journal ;
2016-03-01 15:46:06 +01:00
2017-10-13 13:28:26 +02:00
/**
* Currency code
* @ var string
*/
public $currency_code ;
/**
* Currency code
* @ var string
* @ deprecated Use currency_code instead
*/
public $account_currency_code ;
/**
* Authorized minimum balance
* @ var float
*/
public $min_allowed ;
/**
* Desired minimum balance
* @ var float
*/
public $min_desired ;
/**
* Notes
* @ var string
*/
public $comment ;
/**
* Date of the initial balance . Used in Account :: create
* @ var int
*/
public $date_solde ;
2020-12-31 10:49:27 +01:00
2020-11-09 13:17:24 +01:00
/**
2020-11-12 10:32:52 +01:00
* Creditor Identifier CI . Some banks use different ICS for direct debit and bank tranfer
2020-11-09 13:17:24 +01:00
* @ var string
*/
public $ics ;
2020-12-31 10:49:27 +01:00
2020-11-12 10:32:52 +01:00
/**
* Creditor Identifier for Bank Transfer .
* @ var string
*/
2020-11-09 13:17:24 +01:00
public $ics_transfer ;
2020-12-31 10:49:27 +01:00
2017-10-13 13:28:26 +02:00
2020-04-12 16:51:35 +02:00
/**
* 'type' if the field format ( 'integer' , 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]' , 'varchar(x)' , 'double(24,8)' , 'real' , 'price' , 'text' , 'html' , 'date' , 'datetime' , 'timestamp' , 'duration' , 'mail' , 'phone' , 'url' , 'password' )
* Note : Filter can be a string like " (t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL) "
* 'label' the translation key .
* 'enabled' is a condition when the field must be managed .
* 'position' is the sort order of field .
* 'notnull' is set to 1 if not null in database . Set to - 1 if we must set data to null if empty ( '' or 0 ) .
* 'visible' says if field is visible in list ( Examples : 0 = Not visible , 1 = Visible on list and create / update / view forms , 2 = Visible on list only , 3 = Visible on create / update / view form only ( not list ), 4 = Visible on list and update / view form only ( not create ) . 5 = Visible on list and view only ( not create / not update ) . Using a negative value means field is not shown by default on list but can be selected for viewing )
* 'noteditable' says if field is not editable ( 1 or 0 )
* 'default' is a default value for creation ( can still be overwrote by the Setup of Default Values if field is editable in creation form ) . Note : If default is set to '(PROV)' and field is 'ref' , the default value will be set to '(PROVid)' where id is rowid when a new record is created .
* 'index' if we want an index in database .
* 'foreignkey' => 'tablename.field' if the field is a foreign key ( it is recommanded to name the field fk_ ... ) .
* 'searchall' is 1 if we want to search in this field when making a search from the quick search button .
* 'isameasure' must be set to 1 if you want to have a total on list for this field . Field type must be summable like integer or double ( 24 , 8 ) .
* 'css' is the CSS style to use on field . For example : 'maxwidth200'
* 'help' is a string visible as a tooltip on field
* 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
* 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute . In most cases , this is never set into the definition of $fields into class , but is set dynamically by some part of code .
2021-04-29 12:10:55 +02:00
* 'arrayofkeyval' to set list of value if type is a list of predefined values . For example : array ( " 0 " => " Draft " , " 1 " => " Active " , " -1 " => " Cancel " )
2020-04-12 16:51:35 +02:00
* 'comment' is not used . You can store here any text of your choice . It is not used by application .
*
* Note : To have value dynamic , you can set value to 0 in definition and edit the value on the fly into the constructor .
*/
// BEGIN MODULEBUILDER PROPERTIES
/**
* @ var array Array with all fields and their property . Do not use it as a static var . It may be modified by constructor .
*/
2020-04-13 13:24:42 +02:00
public $fields = array (
2020-04-12 16:51:35 +02:00
'rowid' => array ( 'type' => 'integer' , 'label' => 'TechnicalID' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 10 ),
'ref' => array ( 'type' => 'varchar(12)' , 'label' => 'Ref' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'showoncombobox' => 1 , 'position' => 25 ),
'label' => array ( 'type' => 'varchar(30)' , 'label' => 'Label' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 30 ),
'entity' => array ( 'type' => 'integer' , 'label' => 'Entity' , 'default' => 1 , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'position' => 35 , 'index' => 1 ),
'bank' => array ( 'type' => 'varchar(60)' , 'label' => 'Bank' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 40 ),
'code_banque' => array ( 'type' => 'varchar(128)' , 'label' => 'Code banque' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 45 ),
'code_guichet' => array ( 'type' => 'varchar(6)' , 'label' => 'Code guichet' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 50 ),
'number' => array ( 'type' => 'varchar(255)' , 'label' => 'Number' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 55 ),
'cle_rib' => array ( 'type' => 'varchar(5)' , 'label' => 'Cle rib' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 60 ),
'bic' => array ( 'type' => 'varchar(11)' , 'label' => 'Bic' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 65 ),
'iban_prefix' => array ( 'type' => 'varchar(34)' , 'label' => 'Iban prefix' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 70 ),
'country_iban' => array ( 'type' => 'varchar(2)' , 'label' => 'Country iban' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 75 ),
'cle_iban' => array ( 'type' => 'varchar(2)' , 'label' => 'Cle iban' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 80 ),
'domiciliation' => array ( 'type' => 'varchar(255)' , 'label' => 'Domiciliation' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 85 ),
'state_id' => array ( 'type' => 'integer' , 'label' => 'State id' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 90 ),
'fk_pays' => array ( 'type' => 'integer' , 'label' => 'Fk pays' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 95 ),
'proprio' => array ( 'type' => 'varchar(60)' , 'label' => 'Proprio' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 100 ),
'owner_address' => array ( 'type' => 'text' , 'label' => 'Owner address' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 105 ),
'courant' => array ( 'type' => 'smallint(6)' , 'label' => 'Courant' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 110 ),
'clos' => array ( 'type' => 'smallint(6)' , 'label' => 'Clos' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 115 ),
'rappro' => array ( 'type' => 'smallint(6)' , 'label' => 'Rappro' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 120 ),
'url' => array ( 'type' => 'varchar(128)' , 'label' => 'Url' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 125 ),
'account_number' => array ( 'type' => 'varchar(32)' , 'label' => 'Account number' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 130 ),
2021-06-05 01:29:24 +02:00
'fk_accountancy_journal' => array ( 'type' => 'integer' , 'label' => 'Accountancy journal ID' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 132 ),
2020-04-12 16:51:35 +02:00
'accountancy_journal' => array ( 'type' => 'varchar(20)' , 'label' => 'Accountancy journal' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 135 ),
'currency_code' => array ( 'type' => 'varchar(3)' , 'label' => 'Currency code' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 140 ),
'min_allowed' => array ( 'type' => 'integer' , 'label' => 'Min allowed' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 145 ),
'min_desired' => array ( 'type' => 'integer' , 'label' => 'Min desired' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 150 ),
'comment' => array ( 'type' => 'text' , 'label' => 'Comment' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 155 ),
'datec' => array ( 'type' => 'datetime' , 'label' => 'DateCreation' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 156 ),
'tms' => array ( 'type' => 'timestamp' , 'label' => 'DateModification' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 157 ),
'fk_user_author' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'Fk user author' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 160 ),
'fk_user_modif' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserModif' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' =>- 1 , 'position' => 165 ),
2022-09-06 05:30:35 +02:00
'note_public' => array ( 'type' => 'text' , 'label' => 'NotePublic' , 'enabled' => 1 , 'visible' => 0 , 'position' => 170 ),
2020-04-12 16:51:35 +02:00
'model_pdf' => array ( 'type' => 'varchar(255)' , 'label' => 'Model pdf' , 'enabled' => 1 , 'visible' => 0 , 'position' => 175 ),
'import_key' => array ( 'type' => 'varchar(14)' , 'label' => 'ImportId' , 'enabled' => 1 , 'visible' =>- 2 , 'position' => 180 ),
'extraparams' => array ( 'type' => 'varchar(255)' , 'label' => 'Extraparams' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 185 ),
);
// END MODULEBUILDER PROPERTIES
2017-10-13 13:28:26 +02:00
/**
* Current account
*/
const TYPE_CURRENT = 1 ;
/**
* Cash account
*/
const TYPE_CASH = 2 ;
/**
* Savings account
*/
const TYPE_SAVINGS = 0 ;
2021-06-05 01:29:24 +02:00
2017-10-13 13:28:26 +02:00
const STATUS_OPEN = 0 ;
const STATUS_CLOSED = 1 ;
2021-06-05 01:29:24 +02:00
2017-10-13 13:28:26 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2020-10-31 14:32:18 +01:00
public function __construct ( DoliDB $db )
2017-10-13 13:28:26 +02:00
{
global $langs ;
$this -> db = $db ;
$this -> solde = 0 ;
$this -> type_lib = array (
self :: TYPE_SAVINGS => $langs -> trans ( " BankType0 " ),
self :: TYPE_CURRENT => $langs -> trans ( " BankType1 " ),
self :: TYPE_CASH => $langs -> trans ( " BankType2 " ),
);
$this -> status = array (
self :: STATUS_OPEN => $langs -> trans ( " StatusAccountOpened " ),
self :: STATUS_CLOSED => $langs -> trans ( " StatusAccountClosed " )
);
}
2011-02-07 18:51:16 +01:00
2016-03-19 23:00:33 +01:00
/**
2018-12-02 11:01:11 +01:00
* Shows the account number in the appropriate format
2016-03-19 23:00:33 +01:00
*
* @ return string
*/
public function __toString ()
{
$string = '' ;
foreach ( $this -> getFieldsToShow () as $val ) {
if ( $val == 'BankCode' ) {
$string .= $this -> code_banque . ' ' ;
} elseif ( $val == 'BankAccountNumber' ) {
$string .= $this -> number . ' ' ;
} elseif ( $val == 'DeskCode' ) {
$string .= $this -> code_guichet . ' ' ;
} elseif ( $val == 'BankAccountNumberKey' ) {
$string .= $this -> cle_rib . ' ' ;
2020-05-21 15:05:19 +02:00
} elseif ( $val == 'BIC' ) {
2016-06-30 15:49:57 +02:00
$string .= $this -> bic . ' ' ;
2020-05-21 15:05:19 +02:00
} elseif ( $val == 'IBAN' ) {
2016-06-30 15:49:57 +02:00
$string .= $this -> iban . ' ' ;
2016-03-19 23:00:33 +01:00
}
}
return trim ( $string );
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
/**
* Return if a bank account need to be conciliated
*
* @ return int 1 if need to be concialiated , < 0 otherwise .
*/
2020-10-31 14:32:18 +01:00
public function canBeConciliated ()
2017-10-13 13:28:26 +02:00
{
global $conf ;
2011-08-05 23:05:19 +02:00
2021-02-23 21:09:01 +01:00
if ( empty ( $this -> rappro )) {
return - 1 ;
}
if ( $this -> courant == Account :: TYPE_CASH && empty ( $conf -> global -> BANK_CAN_RECONCILIATE_CASHACCOUNT )) {
return - 2 ;
}
if ( $this -> clos ) {
return - 3 ;
}
2017-10-13 13:28:26 +02:00
return 1 ;
}
2011-02-07 18:51:16 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Add a link between bank line record and its source
*
2021-08-03 15:02:09 +02:00
* @ param int $line_id Id of bank entry
* @ param int $url_id Id of object related to link
* @ param string $url Url ( deprecated , we use now 'url_id' and 'type' instead )
2017-10-13 13:28:26 +02:00
* @ param string $label Link label
* @ param string $type Type of link ( 'payment' , 'company' , 'member' , ... )
* @ return int < 0 if KO , id line if OK
*/
2020-10-31 14:32:18 +01:00
public function add_url_line ( $line_id , $url_id , $url , $label , $type )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2017-10-13 13:28:26 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " bank_url ( " ;
2019-11-14 12:09:15 +01:00
$sql .= " fk_bank " ;
$sql .= " , url_id " ;
2021-08-03 15:02:09 +02:00
$sql .= " , url " ; // deprecated
2019-11-14 12:09:15 +01:00
$sql .= " , label " ;
$sql .= " , type " ;
$sql .= " ) VALUES ( " ;
2020-09-19 23:30:29 +02:00
$sql .= " " . (( int ) $line_id );
2021-08-03 15:02:09 +02:00
$sql .= " , " . (( int ) $url_id );
$sql .= " , ' " . $this -> db -> escape ( $url ) . " ' " ; // dperecated
2019-11-14 12:09:15 +01:00
$sql .= " , ' " . $this -> db -> escape ( $label ) . " ' " ;
2020-09-19 23:30:29 +02:00
$sql .= " , ' " . $this -> db -> escape ( $type ) . " ' " ;
2019-11-14 12:09:15 +01:00
$sql .= " ) " ;
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::add_url_line " , LOG_DEBUG );
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sql )) {
2017-10-13 13:28:26 +02:00
$rowid = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " bank_url " );
return $rowid ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-13 13:28:26 +02:00
return - 1 ;
}
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* TODO Move this into AccountLine
* Return array with links from llx_bank_url
*
* @ param int $fk_bank To search using bank transaction id
* @ param int $url_id To search using link to
* @ param string $type To search using type
2018-09-11 00:29:01 +02:00
* @ return array | int Array of links array ( 'url' => , 'url_id' => , 'label' => , 'type' => 'fk_bank' => ) or - 1 on error
2017-10-13 13:28:26 +02:00
*/
2020-10-31 14:32:18 +01:00
public function get_url ( $fk_bank = '' , $url_id = '' , $type = '' )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2017-10-13 13:28:26 +02:00
$lines = array ();
// Check parameters
2021-02-23 21:09:01 +01:00
if ( ! empty ( $fk_bank ) && ( ! empty ( $url_id ) || ! empty ( $type ))) {
2019-11-14 12:09:15 +01:00
$this -> error = " ErrorBadParameter " ;
2017-10-13 13:28:26 +02:00
return - 1 ;
}
$sql = " SELECT fk_bank, url_id, url, label, type " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " bank_url " ;
2017-10-13 13:28:26 +02:00
if ( $fk_bank > 0 ) {
2021-03-30 17:53:25 +02:00
$sql .= " WHERE fk_bank = " . (( int ) $fk_bank );
2021-02-23 21:09:01 +01:00
} else {
2021-03-30 17:53:25 +02:00
$sql .= " WHERE url_id = " . (( int ) $url_id ) . " AND type = ' " . $this -> db -> escape ( $type ) . " ' " ;
2017-10-13 13:28:26 +02:00
}
2019-11-14 12:09:15 +01:00
$sql .= " ORDER BY type, label " ;
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::get_url " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
2017-10-13 13:28:26 +02:00
$i = 0 ;
$num = $this -> db -> num_rows ( $result );
2021-02-23 21:09:01 +01:00
while ( $i < $num ) {
2017-10-13 13:28:26 +02:00
$obj = $this -> db -> fetch_object ( $result );
// Anciens liens (pour compatibilite)
$lines [ $i ][ 0 ] = $obj -> url ;
$lines [ $i ][ 1 ] = $obj -> url_id ;
$lines [ $i ][ 2 ] = $obj -> label ;
$lines [ $i ][ 3 ] = $obj -> type ;
// Nouveaux liens
$lines [ $i ][ 'url' ] = $obj -> url ;
$lines [ $i ][ 'url_id' ] = $obj -> url_id ;
$lines [ $i ][ 'label' ] = $obj -> label ;
$lines [ $i ][ 'type' ] = $obj -> type ;
$lines [ $i ][ 'fk_bank' ] = $obj -> fk_bank ;
$i ++ ;
}
2021-02-23 21:09:01 +01:00
} else {
dol_print_error ( $this -> db );
}
2017-10-13 13:28:26 +02:00
return $lines ;
}
/**
* Add an entry into table " .MAIN_DB_PREFIX. " bank
*
* @ param int $date Date operation
2020-08-05 20:05:59 +02:00
* @ param string $oper 'VIR' , 'PRE' , 'LIQ' , 'VAD' , 'CB' , 'CHQ' ...
2017-10-13 13:28:26 +02:00
* @ param string $label Descripton
* @ param float $amount Amount
2020-09-10 18:25:38 +02:00
* @ param string $num_chq Numero cheque or transfer
2017-10-13 13:28:26 +02:00
* @ param int $categorie Category id ( optionnal )
* @ param User $user User that create
* @ param string $emetteur Name of cheque writer
* @ param string $banque Bank of cheque writer
* @ param string $accountancycode When we record a free bank entry , we must provide accounting account if accountancy module is on .
2018-04-29 20:18:50 +02:00
* @ param int $datev Date value
2021-02-23 21:09:01 +01:00
* @ param string $num_releve Label of bank receipt for reconciliation
2022-04-09 00:55:40 +02:00
* @ param float $amount_main_currency Amount
2017-10-13 13:28:26 +02:00
* @ return int Rowid of added entry , < 0 if KO
*/
2022-04-09 00:55:40 +02:00
public function addline ( $date , $oper , $label , $amount , $num_chq , $categorie , User $user , $emetteur = '' , $banque = '' , $accountancycode = '' , $datev = null , $num_releve = '' , $amount_main_currency = null )
2017-10-13 13:28:26 +02:00
{
2018-12-02 11:01:11 +01:00
// Deprecation warning
2017-10-13 13:28:26 +02:00
if ( is_numeric ( $oper )) {
2019-11-14 12:09:15 +01:00
dol_syslog ( __METHOD__ . " : using numeric operations is deprecated " , LOG_WARNING );
2017-10-13 13:28:26 +02:00
}
2021-04-24 21:15:09 +02:00
if ( empty ( $this -> id ) && ! empty ( $this -> rowid )) { // For backward compatibility
$this -> id = $this -> rowid ;
}
2017-10-13 13:28:26 +02:00
// Clean parameters
2019-11-14 12:09:15 +01:00
$emetteur = trim ( $emetteur );
$banque = trim ( $banque );
2021-04-24 20:52:48 +02:00
$label = trim ( $label );
2017-10-13 13:28:26 +02:00
2019-11-14 12:09:15 +01:00
$now = dol_now ();
2017-10-13 13:28:26 +02:00
2021-02-23 21:09:01 +01:00
if ( is_numeric ( $oper )) { // Clean operation to have a code instead of a rowid
2017-10-13 13:28:26 +02:00
$sql = " SELECT code FROM " . MAIN_DB_PREFIX . " c_paiement " ;
2021-04-24 20:18:11 +02:00
$sql .= " WHERE id = " . (( int ) $oper );
2019-11-14 12:09:15 +01:00
$sql .= " AND entity IN ( " . getEntity ( 'c_paiement' ) . " ) " ;
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2019-11-14 12:09:15 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$oper = $obj -> code ;
2020-05-21 15:05:19 +02:00
} else {
2019-01-27 11:55:16 +01:00
dol_print_error ( $this -> db , 'Failed to get payment type code' );
2017-10-13 13:28:26 +02:00
return - 1 ;
}
}
// Check parameters
2021-02-23 21:09:01 +01:00
if ( ! $oper ) {
2019-11-14 12:09:15 +01:00
$this -> error = " oper not defined " ;
2017-10-13 13:28:26 +02:00
return - 1 ;
}
2021-04-24 21:15:09 +02:00
if ( ! $this -> id ) {
$this -> error = " this->id not defined " ;
2017-10-13 13:28:26 +02:00
return - 2 ;
}
2021-02-23 21:09:01 +01:00
if ( $this -> courant == Account :: TYPE_CASH && $oper != 'LIQ' ) {
2019-11-14 12:09:15 +01:00
$this -> error = " ErrorCashAccountAcceptsOnlyCashMoney " ;
2017-10-13 13:28:26 +02:00
return - 3 ;
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$this -> db -> begin ();
2011-02-07 18:51:16 +01:00
2021-02-23 21:09:01 +01:00
if ( is_null ( $datev ) || empty ( $datev )) {
$datev = $date ;
}
2011-02-07 18:51:16 +01:00
2016-03-05 16:26:30 +01:00
$accline = new AccountLine ( $this -> db );
$accline -> datec = $now ;
$accline -> dateo = $date ;
$accline -> datev = $datev ;
$accline -> label = $label ;
$accline -> amount = $amount ;
2022-04-09 00:55:40 +02:00
$accline -> amount_main_currency = $amount_main_currency ;
2016-03-05 16:26:30 +01:00
$accline -> fk_user_author = $user -> id ;
2021-04-24 21:15:09 +02:00
$accline -> fk_account = $this -> id ;
2016-03-05 16:26:30 +01:00
$accline -> fk_type = $oper ;
2017-07-15 03:41:07 +02:00
$accline -> numero_compte = $accountancycode ;
2021-01-05 02:18:42 +01:00
$accline -> num_releve = $num_releve ;
2016-03-05 16:26:30 +01:00
if ( $num_chq ) {
$accline -> num_chq = $num_chq ;
}
2011-02-07 18:51:16 +01:00
2016-03-05 16:26:30 +01:00
if ( $emetteur ) {
$accline -> emetteur = $emetteur ;
}
if ( $banque ) {
$accline -> bank_chq = $banque ;
}
if ( $accline -> insert () > 0 ) {
2019-11-14 12:09:15 +01:00
if ( $categorie > 0 ) {
2021-04-24 21:15:09 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " bank_class( " ;
2017-05-10 23:27:48 +02:00
$sql .= " lineid, fk_categ " ;
2016-03-05 16:26:30 +01:00
$sql .= " ) VALUES ( " ;
2021-04-24 21:15:09 +02:00
$sql .= (( int ) $accline -> id ) . " , ' " . $this -> db -> escape ( $categorie ) . " ' " ;
2016-03-05 16:26:30 +01:00
$sql .= " ) " ;
$result = $this -> db -> query ( $sql );
if ( ! $result ) {
2017-04-08 01:25:52 +02:00
$this -> error = $this -> db -> lasterror ();
2016-03-05 16:26:30 +01:00
$this -> db -> rollback ();
2020-09-10 18:25:38 +02:00
2022-08-12 15:45:27 +02:00
return - 4 ;
2016-03-05 16:26:30 +01:00
}
}
$this -> db -> commit ();
2020-09-10 18:25:38 +02:00
2016-03-05 16:26:30 +01:00
return $accline -> id ;
} else {
2021-02-08 20:44:40 +01:00
$this -> error = $accline -> error ;
$this -> errors = $accline -> errors ;
2016-03-05 16:26:30 +01:00
$this -> db -> rollback ();
2020-09-10 18:25:38 +02:00
2022-08-12 15:45:27 +02:00
return - 5 ;
2016-03-05 16:26:30 +01:00
}
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
/**
* Create bank account into database
*
* @ param User $user Object user making creation
* @ param int $notrigger 1 = Disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function create ( User $user , $notrigger = 0 )
2017-10-13 13:28:26 +02:00
{
2021-04-24 21:15:09 +02:00
global $langs , $conf ;
2017-10-13 13:28:26 +02:00
2019-11-14 12:09:15 +01:00
$error = 0 ;
2018-03-10 00:13:41 +01:00
2017-10-13 13:28:26 +02:00
// Clean parameters
2021-02-23 21:09:01 +01:00
if ( ! $this -> min_allowed ) {
$this -> min_allowed = 0 ;
}
if ( ! $this -> min_desired ) {
$this -> min_desired = 0 ;
}
2017-10-13 13:28:26 +02:00
// Check parameters
2021-02-23 21:09:01 +01:00
if ( empty ( $this -> country_id )) {
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> transnoentitiesnoconv ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Country " ));
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_ERR );
return - 1 ;
}
2021-02-23 21:09:01 +01:00
if ( empty ( $this -> ref )) {
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> transnoentitiesnoconv ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Ref " ));
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_ERR );
return - 1 ;
}
2021-02-23 21:09:01 +01:00
if ( empty ( $this -> date_solde )) {
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> transnoentitiesnoconv ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " DateInitialBalance " ));
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_ERR );
return - 1 ;
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
// Chargement librairie pour acces fonction controle RIB
require_once DOL_DOCUMENT_ROOT . '/core/lib/bank.lib.php' ;
2011-02-07 18:51:16 +01:00
2019-11-14 12:09:15 +01:00
$now = dol_now ();
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$this -> db -> begin ();
2017-07-15 03:41:07 +02:00
2017-10-13 13:28:26 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " bank_account ( " ;
2019-11-14 12:09:15 +01:00
$sql .= " datec " ;
$sql .= " , ref " ;
$sql .= " , label " ;
$sql .= " , entity " ;
$sql .= " , account_number " ;
$sql .= " , fk_accountancy_journal " ;
$sql .= " , bank " ;
$sql .= " , code_banque " ;
$sql .= " , code_guichet " ;
$sql .= " , number " ;
$sql .= " , cle_rib " ;
$sql .= " , bic " ;
$sql .= " , iban_prefix " ;
$sql .= " , domiciliation " ;
2022-02-23 12:11:40 +01:00
$sql .= " , pti_in_ctti " ;
2019-11-14 12:09:15 +01:00
$sql .= " , proprio " ;
$sql .= " , owner_address " ;
$sql .= " , currency_code " ;
$sql .= " , rappro " ;
$sql .= " , min_allowed " ;
$sql .= " , min_desired " ;
$sql .= " , comment " ;
$sql .= " , state_id " ;
$sql .= " , fk_pays " ;
2020-11-09 13:17:24 +01:00
$sql .= " , ics " ;
$sql .= " , ics_transfer " ;
2019-11-14 12:09:15 +01:00
$sql .= " ) VALUES ( " ;
$sql .= " ' " . $this -> db -> idate ( $now ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> label ) . " ' " ;
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $conf -> entity );
2019-11-14 12:09:15 +01:00
$sql .= " , ' " . $this -> db -> escape ( $this -> account_number ) . " ' " ;
$sql .= " , " . ( $this -> fk_accountancy_journal > 0 ? $this -> db -> escape ( $this -> fk_accountancy_journal ) : " null " );
$sql .= " , ' " . $this -> db -> escape ( $this -> bank ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> code_banque ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> code_guichet ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> number ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> cle_rib ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> bic ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> iban ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> domiciliation ) . " ' " ;
2022-02-23 12:11:40 +01:00
$sql .= " , " . (( int ) $this -> pti_in_ctti );
2019-11-14 12:09:15 +01:00
$sql .= " , ' " . $this -> db -> escape ( $this -> proprio ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> owner_address ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> currency_code ) . " ' " ;
2021-01-01 21:03:44 +01:00
$sql .= " , " . (( int ) $this -> rappro );
2021-09-03 21:25:17 +02:00
$sql .= " , " . price2num ( $this -> min_allowed , 'MT' );
$sql .= " , " . price2num ( $this -> min_desired , 'MT' );
2019-11-14 12:09:15 +01:00
$sql .= " , ' " . $this -> db -> escape ( $this -> comment ) . " ' " ;
2021-06-11 18:44:01 +02:00
$sql .= " , " . ( $this -> state_id > 0 ? (( int ) $this -> state_id ) : " null " );
$sql .= " , " . ( $this -> country_id > 0 ? (( int ) $this -> country_id ) : " null " );
2021-01-01 21:03:44 +01:00
$sql .= " , ' " . $this -> db -> escape ( $this -> ics ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> ics_transfer ) . " ' " ;
2019-11-14 12:09:15 +01:00
$sql .= " ) " ;
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2019-11-14 12:09:15 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2017-10-13 13:28:26 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " bank_account " );
2012-03-21 12:33:51 +01:00
2019-11-14 12:09:15 +01:00
$result = $this -> update ( $user , 1 );
2021-02-23 21:09:01 +01:00
if ( $result > 0 ) {
2016-03-05 16:26:30 +01:00
$accline = new AccountLine ( $this -> db );
$accline -> datec = $this -> db -> idate ( $now );
$accline -> label = '(' . $langs -> trans ( " InitialBankBalance " ) . ')' ;
$accline -> amount = price2num ( $this -> solde );
2017-02-27 11:15:59 +01:00
$accline -> fk_user_author = $user -> id ;
2016-03-05 16:26:30 +01:00
$accline -> fk_account = $this -> id ;
$accline -> datev = $this -> db -> idate ( $this -> date_solde );
$accline -> dateo = $this -> db -> idate ( $this -> date_solde );
$accline -> fk_type = 'SOLD' ;
if ( $accline -> insert () < 0 ) {
2016-10-05 21:49:04 +02:00
$error ++ ;
2017-10-13 13:28:26 +02:00
$this -> error = $accline -> error ;
$this -> errors = $accline -> errors ;
2016-03-05 16:26:30 +01:00
}
2015-03-04 21:59:03 +01:00
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2019-11-14 12:09:15 +01:00
$result = $this -> insertExtraFields ();
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2016-10-05 21:49:04 +02:00
}
2017-07-15 03:41:07 +02:00
2021-02-23 21:09:01 +01:00
if ( ! $error && ! $notrigger ) {
2017-10-13 13:28:26 +02:00
// Call trigger
2019-11-14 12:09:15 +01:00
$result = $this -> call_trigger ( 'BANKACCOUNT_CREATE' , $user );
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-13 13:28:26 +02:00
// End call triggers
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$error ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 21:09:01 +01:00
if ( $this -> db -> errno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' ) {
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> trans ( " ErrorBankLabelAlreadyExists " );
2017-10-13 13:28:26 +02:00
$error ++ ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> error () . " sql= " . $sql ;
2017-10-13 13:28:26 +02:00
$error ++ ;
}
}
2016-10-05 21:49:04 +02:00
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2017-10-13 13:28:26 +02:00
$this -> db -> commit ();
return $this -> id ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$this -> db -> rollback ();
2019-11-14 12:09:15 +01:00
return - 1 * $error ;
2017-10-13 13:28:26 +02:00
}
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
/**
* Update bank account card
*
* @ param User $user Object user making action
* @ param int $notrigger 1 = Disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function update ( User $user , $notrigger = 0 )
2017-10-13 13:28:26 +02:00
{
2021-04-24 21:15:09 +02:00
global $langs , $conf ;
2011-02-07 18:51:16 +01:00
2019-11-14 12:09:15 +01:00
$error = 0 ;
2017-07-15 03:41:07 +02:00
2017-10-13 13:28:26 +02:00
$this -> db -> begin ();
2017-07-15 03:41:07 +02:00
2017-10-13 13:28:26 +02:00
// Check parameters
2021-02-23 21:09:01 +01:00
if ( empty ( $this -> country_id )) {
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> transnoentitiesnoconv ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Country " ));
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::update " . $this -> error , LOG_ERR );
return - 1 ;
}
2021-02-23 21:09:01 +01:00
if ( empty ( $this -> ref )) {
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> transnoentitiesnoconv ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Ref " ));
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::update " . $this -> error , LOG_ERR );
return - 1 ;
}
2021-02-23 21:09:01 +01:00
if ( ! $this -> label ) {
$this -> label = " ??? " ;
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " bank_account SET " ;
2011-02-07 18:51:16 +01:00
2019-11-14 12:09:15 +01:00
$sql .= " ref = ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
$sql .= " ,label = ' " . $this -> db -> escape ( $this -> label ) . " ' " ;
2021-04-24 21:15:09 +02:00
$sql .= " ,courant = " . (( int ) $this -> courant );
$sql .= " ,clos = " . (( int ) $this -> clos );
$sql .= " ,rappro = " . (( int ) $this -> rappro );
2019-11-14 12:09:15 +01:00
$sql .= " ,url = " . ( $this -> url ? " ' " . $this -> db -> escape ( $this -> url ) . " ' " : " null " );
$sql .= " ,account_number = ' " . $this -> db -> escape ( $this -> account_number ) . " ' " ;
$sql .= " ,fk_accountancy_journal = " . ( $this -> fk_accountancy_journal > 0 ? $this -> db -> escape ( $this -> fk_accountancy_journal ) : " null " );
$sql .= " ,bank = ' " . $this -> db -> escape ( $this -> bank ) . " ' " ;
$sql .= " ,code_banque=' " . $this -> db -> escape ( $this -> code_banque ) . " ' " ;
$sql .= " ,code_guichet=' " . $this -> db -> escape ( $this -> code_guichet ) . " ' " ;
$sql .= " ,number=' " . $this -> db -> escape ( $this -> number ) . " ' " ;
$sql .= " ,cle_rib=' " . $this -> db -> escape ( $this -> cle_rib ) . " ' " ;
$sql .= " ,bic=' " . $this -> db -> escape ( $this -> bic ) . " ' " ;
$sql .= " ,iban_prefix = ' " . $this -> db -> escape ( $this -> iban ) . " ' " ;
$sql .= " ,domiciliation=' " . $this -> db -> escape ( $this -> domiciliation ) . " ' " ;
2022-02-23 12:11:40 +01:00
$sql .= " ,pti_in_ctti= " . (( int ) $this -> pti_in_ctti );
2019-11-14 12:09:15 +01:00
$sql .= " ,proprio = ' " . $this -> db -> escape ( $this -> proprio ) . " ' " ;
$sql .= " ,owner_address = ' " . $this -> db -> escape ( $this -> owner_address ) . " ' " ;
$sql .= " ,currency_code = ' " . $this -> db -> escape ( $this -> currency_code ) . " ' " ;
$sql .= " ,min_allowed = " . ( $this -> min_allowed != '' ? price2num ( $this -> min_allowed ) : " null " );
$sql .= " ,min_desired = " . ( $this -> min_desired != '' ? price2num ( $this -> min_desired ) : " null " );
$sql .= " ,comment = ' " . $this -> db -> escape ( $this -> comment ) . " ' " ;
2021-07-05 18:46:27 +02:00
$sql .= " ,state_id = " . ( $this -> state_id > 0 ? (( int ) $this -> state_id ) : " null " );
$sql .= " ,fk_pays = " . ( $this -> country_id > 0 ? (( int ) $this -> country_id ) : " null " );
2021-01-01 21:03:44 +01:00
$sql .= " ,ics = ' " . $this -> db -> escape ( $this -> ics ) . " ' " ;
$sql .= " ,ics_transfer = ' " . $this -> db -> escape ( $this -> ics_transfer ) . " ' " ;
2019-11-14 12:09:15 +01:00
2021-04-24 21:15:09 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
2017-10-13 13:28:26 +02:00
// Actions on extra fields (by external module or standard code)
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2020-04-23 13:21:39 +02:00
$result = $this -> insertExtraFields ();
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-13 13:28:26 +02:00
}
2021-02-23 21:09:01 +01:00
if ( ! $error && ! $notrigger ) {
2017-10-13 13:28:26 +02:00
// Call trigger
2022-04-02 10:31:58 +02:00
$result = $this -> call_trigger ( 'BANKACCOUNT_MODIFY' , $user );
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-13 13:28:26 +02:00
// End call triggers
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$error ++ ;
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-13 13:28:26 +02:00
dol_print_error ( $this -> db );
}
2017-07-15 03:41:07 +02:00
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2017-10-13 13:28:26 +02:00
$this -> db -> commit ();
return $this -> id ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$this -> db -> rollback ();
2019-11-14 12:09:15 +01:00
return - 1 * $error ;
2016-10-05 21:49:04 +02:00
}
2017-10-13 13:28:26 +02:00
}
2012-03-21 12:33:51 +01:00
2011-02-07 18:51:16 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
2018-08-13 17:26:32 +02:00
* Update BBAN ( RIB ) account fields
2017-10-13 13:28:26 +02:00
*
2018-08-13 17:26:32 +02:00
* @ param User $user Object user making update
* @ return int < 0 if KO , > 0 if OK
2017-10-13 13:28:26 +02:00
*/
2020-10-31 14:32:18 +01:00
public function update_bban ( User $user = null )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-11-14 12:09:15 +01:00
global $conf , $langs ;
2011-02-07 18:51:16 +01:00
2021-01-01 21:06:47 +01:00
// Load library to get BAN control function
2017-10-13 13:28:26 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/bank.lib.php' ;
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::update_bban $this->code_banque , $this->code_guichet , $this->number , $this->cle_rib , $this->iban " );
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
// Check parameters
2021-02-23 21:09:01 +01:00
if ( ! $this -> ref ) {
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> transnoentitiesnoconv ( " ErrorFieldRequired " , $langs -> trans ( " Ref " ));
2017-10-13 13:28:26 +02:00
return - 2 ;
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " bank_account SET " ;
2019-11-14 12:09:15 +01:00
$sql .= " bank = ' " . $this -> db -> escape ( $this -> bank ) . " ' " ;
$sql .= " ,code_banque=' " . $this -> db -> escape ( $this -> code_banque ) . " ' " ;
$sql .= " ,code_guichet=' " . $this -> db -> escape ( $this -> code_guichet ) . " ' " ;
$sql .= " ,number=' " . $this -> db -> escape ( $this -> number ) . " ' " ;
$sql .= " ,cle_rib=' " . $this -> db -> escape ( $this -> cle_rib ) . " ' " ;
$sql .= " ,bic=' " . $this -> db -> escape ( $this -> bic ) . " ' " ;
$sql .= " ,iban_prefix = ' " . $this -> db -> escape ( $this -> iban ) . " ' " ;
$sql .= " ,domiciliation=' " . $this -> db -> escape ( $this -> domiciliation ) . " ' " ;
$sql .= " ,proprio = ' " . $this -> db -> escape ( $this -> proprio ) . " ' " ;
$sql .= " ,owner_address = ' " . $this -> db -> escape ( $this -> owner_address ) . " ' " ;
$sql .= " ,state_id = " . ( $this -> state_id > 0 ? $this -> state_id : " null " );
2021-01-01 21:06:47 +01:00
$sql .= " ,fk_pays = " . ( $this -> country_id > 0 ? $this -> country_id : " null " );
2021-08-27 16:33:03 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
$sql .= " AND entity = " . (( int ) $conf -> entity );
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::update_bban " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
2017-10-13 13:28:26 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-13 13:28:26 +02:00
dol_print_error ( $this -> db );
return - 1 ;
}
}
2011-05-01 19:46:00 +02:00
2017-10-13 13:28:26 +02:00
/**
* Load a bank account into memory from database
*
* @ param int $id Id of bank account to get
* @ param string $ref Ref of bank account to get
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function fetch ( $id , $ref = '' )
2017-10-13 13:28:26 +02:00
{
global $conf ;
2021-02-23 21:09:01 +01:00
if ( empty ( $id ) && empty ( $ref )) {
2019-11-14 12:09:15 +01:00
$this -> error = " ErrorBadParameters " ;
2017-10-13 13:28:26 +02:00
return - 1 ;
}
$sql = " SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url, " ;
2019-11-14 12:09:15 +01:00
$sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban, " ;
2022-02-23 12:11:40 +01:00
$sql .= " ba.domiciliation, ba.pti_in_ctti, ba.proprio, ba.owner_address, ba.state_id, ba.fk_pays as country_id, " ;
2019-11-14 12:09:15 +01:00
$sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code, " ;
$sql .= " ba.min_allowed, ba.min_desired, ba.comment, " ;
2020-11-09 13:17:24 +01:00
$sql .= " ba.datec as date_creation, ba.tms as date_update, ba.ics, ba.ics_transfer, " ;
2019-11-14 12:09:15 +01:00
$sql .= ' c.code as country_code, c.label as country,' ;
$sql .= ' d.code_departement as state_code, d.nom as state' ;
2020-10-31 14:32:18 +01:00
$sql .= ' , aj.code as accountancy_journal' ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " bank_account as ba " ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_country as c ON ba.fk_pays = c.rowid' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_departements as d ON ba.state_id = d.rowid' ;
2020-10-31 14:32:18 +01:00
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal' ;
2019-11-14 12:09:15 +01:00
$sql .= " WHERE ba.entity IN ( " . getEntity ( $this -> element ) . " ) " ;
2021-02-23 21:09:01 +01:00
if ( $id ) {
2021-03-22 11:30:18 +01:00
$sql .= " AND ba.rowid = " . (( int ) $id );
2021-02-23 21:09:01 +01:00
}
if ( $ref ) {
$sql .= " AND ba.ref = ' " . $this -> db -> escape ( $ref ) . " ' " ;
}
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
if ( $this -> db -> num_rows ( $result )) {
2017-10-13 13:28:26 +02:00
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
$this -> rowid = $obj -> rowid ;
$this -> ref = $obj -> ref ;
$this -> label = $obj -> label ;
$this -> type = $obj -> courant ;
$this -> courant = $obj -> courant ;
$this -> bank = $obj -> bank ;
$this -> clos = $obj -> clos ;
$this -> rappro = $obj -> rappro ;
$this -> url = $obj -> url ;
$this -> code_banque = $obj -> code_banque ;
$this -> code_guichet = $obj -> code_guichet ;
$this -> number = $obj -> number ;
$this -> cle_rib = $obj -> cle_rib ;
$this -> bic = $obj -> bic ;
$this -> iban = $obj -> iban ;
$this -> domiciliation = $obj -> domiciliation ;
2022-02-23 12:11:40 +01:00
$this -> pti_in_ctti = $obj -> pti_in_ctti ;
2017-10-13 13:28:26 +02:00
$this -> proprio = $obj -> proprio ;
$this -> owner_address = $obj -> owner_address ;
$this -> state_id = $obj -> state_id ;
$this -> state_code = $obj -> state_code ;
$this -> state = $obj -> state ;
$this -> country_id = $obj -> country_id ;
$this -> country_code = $obj -> country_code ;
$this -> country = $obj -> country ;
$this -> account_number = $obj -> account_number ;
2017-05-06 08:33:12 +02:00
$this -> fk_accountancy_journal = $obj -> fk_accountancy_journal ;
2017-11-25 12:13:03 +01:00
$this -> accountancy_journal = $obj -> accountancy_journal ;
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$this -> currency_code = $obj -> currency_code ;
2019-11-14 12:09:15 +01:00
$this -> account_currency_code = $obj -> currency_code ;
2017-10-13 13:28:26 +02:00
$this -> min_allowed = $obj -> min_allowed ;
$this -> min_desired = $obj -> min_desired ;
$this -> comment = $obj -> comment ;
$this -> date_creation = $this -> db -> jdate ( $obj -> date_creation );
$this -> date_update = $this -> db -> jdate ( $obj -> date_update );
2020-12-31 10:49:27 +01:00
2020-11-09 13:17:24 +01:00
$this -> ics = $obj -> ics ;
$this -> ics_transfer = $obj -> ics_transfer ;
2017-10-13 13:28:26 +02:00
2020-10-23 20:08:35 +02:00
// Retrieve all extrafield
2017-10-13 13:28:26 +02:00
// fetch optionals attributes and labels
2018-02-20 19:38:18 +01:00
$this -> fetch_optionals ();
2017-10-13 13:28:26 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
return 0 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> lasterror ;
$this -> errors [] = $this -> error ;
2017-10-13 13:28:26 +02:00
return - 1 ;
}
}
2016-04-30 14:45:24 +02:00
2017-10-13 13:28:26 +02:00
/**
* Sets object to supplied categories .
*
* Deletes object from existing categories not supplied .
* Adds it to non existing supplied categories .
* Existing categories are left untouch .
*
* @ param int [] | int $categories Category or categories IDs
2020-10-31 14:32:18 +01:00
* @ return void
2017-10-13 13:28:26 +02:00
*/
2020-10-31 14:32:18 +01:00
public function setCategories ( $categories )
{
2019-11-14 12:09:15 +01:00
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2020-12-24 14:56:26 +01:00
return parent :: setCategoriesCommon ( $categories , Categorie :: TYPE_ACCOUNT );
2017-10-13 13:28:26 +02:00
}
2017-07-15 03:41:07 +02:00
2017-10-13 13:28:26 +02:00
/**
* Delete bank account from database
*
* @ param User $user User deleting
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function delete ( User $user = null )
2017-10-13 13:28:26 +02:00
{
global $conf ;
2017-07-15 03:41:07 +02:00
2019-11-14 12:09:15 +01:00
$error = 0 ;
2017-07-15 03:41:07 +02:00
2017-10-13 13:28:26 +02:00
$this -> db -> begin ();
2017-07-15 03:41:07 +02:00
2023-03-13 19:45:42 +01:00
// @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
2017-10-13 13:28:26 +02:00
// Delete link between tag and bank account
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2017-10-13 13:28:26 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " categorie_account " ;
2021-08-27 16:33:03 +02:00
$sql .= " WHERE fk_account = " . (( int ) $this -> id );
2017-10-13 13:28:26 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( ! $resql ) {
2017-10-13 13:28:26 +02:00
$error ++ ;
$this -> error = " Error " . $this -> db -> lasterror ();
}
}
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2017-10-13 13:28:26 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " bank_account " ;
2021-06-09 15:36:47 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> rowid );
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
2017-10-13 13:28:26 +02:00
// Remove extrafields
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2019-11-14 12:09:15 +01:00
$result = $this -> deleteExtraFields ();
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
2017-10-13 13:28:26 +02:00
$error ++ ;
dol_syslog ( get_class ( $this ) . " ::delete error -4 " . $this -> error , LOG_ERR );
}
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$error ++ ;
$this -> error = " Error " . $this -> db -> lasterror ();
}
}
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2017-10-13 13:28:26 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
/**
2016-10-05 21:49:04 +02:00
* Return label of object status
2017-10-13 13:28:26 +02:00
*
2016-10-05 21:49:04 +02:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = short label + picto , 6 = Long label + picto
2017-10-13 13:28:26 +02:00
* @ return string Label
*/
2020-10-31 14:32:18 +01:00
public function getLibStatut ( $mode = 0 )
2017-10-13 13:28:26 +02:00
{
2019-01-27 11:55:16 +01:00
return $this -> LibStatut ( $this -> clos , $mode );
2017-10-13 13:28:26 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Return label of given object status
*
2019-10-24 16:43:23 +02:00
* @ param int $status Id status
2016-10-05 21:49:04 +02:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = short label + picto , 6 = Long label + picto
2017-10-13 13:28:26 +02:00
* @ return string Label
*/
2020-10-31 14:32:18 +01:00
public function LibStatut ( $status , $mode = 0 )
2016-03-07 11:31:16 +01:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2016-03-07 11:31:16 +01:00
global $langs ;
$langs -> load ( 'banks' );
2019-10-24 16:43:23 +02:00
if ( $status == self :: STATUS_OPEN ) {
2021-10-16 19:37:57 +02:00
$label = $langs -> transnoentitiesnoconv ( " StatusAccountOpened " );
$labelshort = $langs -> transnoentitiesnoconv ( " StatusAccountOpened " );
2019-10-24 16:43:23 +02:00
$statusType = 'status4' ;
2016-03-07 11:31:16 +01:00
} else {
2021-10-16 19:37:57 +02:00
$label = $langs -> transnoentitiesnoconv ( " StatusAccountClosed " );
$labelshort = $langs -> transnoentitiesnoconv ( " StatusAccountClosed " );
2019-10-24 16:43:23 +02:00
$statusType = 'status5' ;
2016-03-07 11:31:16 +01:00
}
2019-10-24 16:43:23 +02:00
return dolGetStatus ( $label , $labelshort , '' , $statusType , $mode );
2016-03-07 11:31:16 +01:00
}
2011-02-07 18:51:16 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Renvoi si un compte peut etre supprimer ou non ( sans mouvements )
*
* @ return boolean vrai si peut etre supprime , faux sinon
*/
2020-10-31 14:32:18 +01:00
public function can_be_deleted ()
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-11-14 12:09:15 +01:00
$can_be_deleted = false ;
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$sql = " SELECT COUNT(rowid) as nb " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " bank " ;
2021-08-27 16:33:03 +02:00
$sql .= " WHERE fk_account = " . (( int ) $this -> id );
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
2019-11-14 12:09:15 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-23 21:09:01 +01:00
if ( $obj -> nb <= 1 ) {
$can_be_deleted = true ; // Juste le solde
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
dol_print_error ( $this -> db );
}
return $can_be_deleted ;
}
2016-06-30 15:49:57 +02:00
2017-10-13 13:28:26 +02:00
/**
* Return error
*
* @ return string Error string
*/
2020-10-31 14:32:18 +01:00
public function error ()
2017-10-13 13:28:26 +02:00
{
return $this -> error ;
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
/**
* Return current sold
*
* @ param int $option 1 = Exclude future operation date ( this is to exclude input made in advance and have real account sold )
2022-06-14 17:56:55 +02:00
* @ param int $date_end Date until we want to get bank account sold
2022-02-24 15:26:11 +01:00
* @ param string $field dateo or datev
* @ return int current sold ( value date <= today )
2017-10-13 13:28:26 +02:00
*/
2022-02-24 15:32:59 +01:00
public function solde ( $option = 0 , $date_end = '' , $field = 'dateo' )
2017-10-13 13:28:26 +02:00
{
2019-11-14 12:09:15 +01:00
$solde = 0 ;
2018-03-10 00:13:41 +01:00
2017-10-13 13:28:26 +02:00
$sql = " SELECT sum(amount) as amount " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " bank " ;
2021-08-27 16:33:03 +02:00
$sql .= " WHERE fk_account = " . (( int ) $this -> id );
2021-02-23 21:09:01 +01:00
if ( $option == 1 ) {
2022-02-24 15:26:11 +01:00
$sql .= " AND " . $this -> db -> escape ( $field ) . " <= ' " . ( ! empty ( $date_end ) ? $this -> db -> idate ( $date_end ) : $this -> db -> idate ( dol_now ())) . " ' " ;
2021-02-23 21:09:01 +01:00
}
2016-06-30 15:49:57 +02:00
2017-10-13 13:28:26 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
2019-11-14 12:09:15 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2017-10-13 13:28:26 +02:00
$solde = $obj -> amount ;
}
$this -> db -> free ( $resql );
2018-03-10 00:13:41 +01:00
} else {
2019-11-14 12:09:15 +01:00
$this -> errors [] = $this -> db -> lasterror ;
2018-03-10 00:13:41 +01:00
return - 1 ;
2017-10-13 13:28:26 +02:00
}
2018-03-10 00:13:41 +01:00
2018-10-24 02:58:29 +02:00
return price2num ( $solde , 'MU' );
2017-10-13 13:28:26 +02:00
}
2017-10-03 16:00:52 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Load indicators for dashboard ( this -> nbtodo and this -> nbtodolate )
*
* @ param User $user Objet user
* @ param int $filteraccountid To get info for a particular account id
* @ return WorkboardResponse | int < 0 if KO , WorkboardResponse if OK
*/
2020-10-31 14:32:18 +01:00
public function load_board ( User $user , $filteraccountid = 0 )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2017-10-13 13:28:26 +02:00
global $conf , $langs ;
2021-02-23 21:09:01 +01:00
if ( $user -> socid ) {
return - 1 ; // protection pour eviter appel par utilisateur externe
}
2017-10-13 13:28:26 +02:00
$sql = " SELECT b.rowid, b.datev as datefin " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " bank as b, " ;
$sql .= " " . MAIN_DB_PREFIX . " bank_account as ba " ;
$sql .= " WHERE b.rappro=0 " ;
$sql .= " AND b.fk_account = ba.rowid " ;
$sql .= " AND ba.entity IN ( " . getEntity ( 'bank_account' ) . " ) " ;
$sql .= " AND (ba.rappro = 1 AND ba.courant != 2) " ; // Compte rapprochable
$sql .= " AND clos = 0 " ;
2021-02-23 21:09:01 +01:00
if ( $filteraccountid ) {
2021-04-24 20:18:11 +02:00
$sql .= " AND ba.rowid = " . (( int ) $filteraccountid );
2021-02-23 21:09:01 +01:00
}
2020-04-10 10:59:32 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2017-10-13 13:28:26 +02:00
$langs -> load ( " banks " );
2020-04-10 10:59:32 +02:00
$now = dol_now ();
2017-10-03 16:00:52 +02:00
2017-10-13 13:28:26 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/workboardresponse.class.php' ;
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$response = new WorkboardResponse ();
2020-04-10 10:59:32 +02:00
$response -> warning_delay = $conf -> bank -> rappro -> warning_delay / 60 / 60 / 24 ;
$response -> label = $langs -> trans ( " TransactionsToConciliate " );
2019-08-19 10:01:42 +02:00
$response -> labelShort = $langs -> trans ( " TransactionsToConciliateShort " );
2020-04-10 10:59:32 +02:00
$response -> url = DOL_URL_ROOT . '/compta/bank/list.php?leftmenu=bank&mainmenu=bank' ;
$response -> img = img_object ( '' , " payment " );
2017-10-13 13:28:26 +02:00
2021-02-23 21:09:01 +01:00
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2017-10-13 13:28:26 +02:00
$response -> nbtodo ++ ;
if ( $this -> db -> jdate ( $obj -> datefin ) < ( $now - $conf -> bank -> rappro -> warning_delay )) {
$response -> nbtodolate ++ ;
}
}
return $response ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
dol_print_error ( $this -> db );
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> error ();
2017-10-13 13:28:26 +02:00
return - 1 ;
}
}
2011-03-31 17:27:18 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Charge indicateurs this -> nb de tableau de bord
* @ param int $filteraccountid To get info for a particular account id
* @ return int < 0 if ko , > 0 if ok
*/
2020-10-31 14:32:18 +01:00
public function load_state_board ( $filteraccountid = 0 )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2017-10-13 13:28:26 +02:00
global $user ;
2011-03-31 17:27:18 +02:00
2021-02-23 21:09:01 +01:00
if ( $user -> socid ) {
return - 1 ; // protection pour eviter appel par utilisateur externe
}
2011-03-31 17:27:18 +02:00
2017-10-13 13:28:26 +02:00
$sql = " SELECT count(b.rowid) as nb " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " bank as b, " ;
$sql .= " " . MAIN_DB_PREFIX . " bank_account as ba " ;
$sql .= " WHERE b.fk_account = ba.rowid " ;
$sql .= " AND ba.entity IN ( " . getEntity ( 'bank_account' ) . " ) " ;
$sql .= " AND (ba.rappro = 1 AND ba.courant != 2) " ; // Compte rapprochable
$sql .= " AND clos = 0 " ;
2021-02-23 21:09:01 +01:00
if ( $filteraccountid ) {
2021-04-24 20:18:11 +02:00
$sql .= " AND ba.rowid = " . (( int ) $filteraccountid );
2021-02-23 21:09:01 +01:00
}
2020-04-10 10:59:32 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2020-04-10 10:59:32 +02:00
$this -> nb [ " banklines " ] = $obj -> nb ;
2017-10-13 13:28:26 +02:00
}
$this -> db -> free ( $resql );
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
dol_print_error ( $this -> db );
2020-04-10 10:59:32 +02:00
$this -> error = $this -> db -> error ();
2017-10-13 13:28:26 +02:00
return - 1 ;
}
}
2017-10-12 00:12:26 +02:00
2017-10-12 00:38:59 +02:00
2017-10-13 13:28:26 +02:00
/**
* Load indicators for dashboard ( this -> nbtodo and this -> nbtodolate )
*
* @ return int Nb of account we can reconciliate
*/
2020-09-19 23:30:29 +02:00
public function countAccountToReconcile ()
2017-10-13 13:28:26 +02:00
{
global $db , $conf , $user ;
//Protection against external users
2019-10-31 20:46:31 +01:00
if ( $user -> socid ) {
2017-10-13 13:28:26 +02:00
return 0 ;
}
2019-11-14 12:09:15 +01:00
$nb = 0 ;
2017-10-13 13:28:26 +02:00
$sql = " SELECT COUNT(ba.rowid) as nb " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " bank_account as ba " ;
$sql .= " WHERE ba.rappro > 0 and ba.clos = 0 " ;
$sql .= " AND ba.entity IN ( " . getEntity ( 'bank_account' ) . " ) " ;
2021-02-23 21:09:01 +01:00
if ( empty ( $conf -> global -> BANK_CAN_RECONCILIATE_CASHACCOUNT )) {
$sql .= " AND ba.courant != 2 " ;
}
2020-09-19 23:30:29 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2017-10-13 13:28:26 +02:00
$nb = $obj -> nb ;
2021-02-23 21:09:01 +01:00
} else {
dol_print_error ( $this -> db );
}
2017-10-13 13:28:26 +02:00
return $nb ;
}
/**
* Return clicable name ( with picto eventually )
*
* @ param int $withpicto Include picto into link
* @ param string $mode '' = Link to card , 'transactions' = Link to transactions card
* @ param string $option '' = Show ref , 'reflabel' = Show ref + label
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
2020-10-31 14:32:18 +01:00
* @ param int $notooltip 1 = Disable tooltip
2017-10-13 13:28:26 +02:00
* @ return string Chaine avec URL
*/
2020-10-31 14:32:18 +01:00
public function getNomUrl ( $withpicto = 0 , $mode = '' , $option = '' , $save_lastsearch_value = - 1 , $notooltip = 0 )
2017-10-13 13:28:26 +02:00
{
2018-07-05 11:52:41 +02:00
global $conf , $langs , $user ;
2017-10-13 13:28:26 +02:00
2019-11-14 12:09:15 +01:00
$result = '' ;
2020-11-07 15:54:58 +01:00
$label = img_picto ( '' , $this -> picto ) . ' <u class="paddingrightnow">' . $langs -> trans ( " BankAccount " ) . '</u>' ;
if ( isset ( $this -> status )) {
$label .= ' ' . $this -> getLibStatut ( 5 );
}
2020-04-17 19:15:22 +02:00
$label .= '<br><b>' . $langs -> trans ( 'Label' ) . ':</b> ' . $this -> label ;
2019-11-14 12:09:15 +01:00
$label .= '<br><b>' . $langs -> trans ( 'AccountNumber' ) . ':</b> ' . $this -> number ;
2020-06-24 19:59:06 +02:00
$label .= '<br><b>' . $langs -> trans ( 'IBAN' ) . ':</b> ' . $this -> iban ;
$label .= '<br><b>' . $langs -> trans ( 'BIC' ) . ':</b> ' . $this -> bic ;
2019-11-14 12:09:15 +01:00
$label .= '<br><b>' . $langs -> trans ( " AccountCurrency " ) . ':</b> ' . $this -> currency_code ;
2018-07-17 18:16:30 +02:00
2021-02-23 21:09:01 +01:00
if ( empty ( $user -> rights -> banque -> lire ) || ! empty ( $user -> socid )) {
2018-07-17 18:16:30 +02:00
$option = 'nolink' ;
}
2022-08-29 11:21:27 +02:00
if ( isModEnabled ( 'accounting' )) {
2017-10-13 13:28:26 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php' ;
$langs -> load ( " accountancy " );
2019-11-14 12:09:15 +01:00
$label .= '<br><b>' . $langs -> trans ( 'AccountAccounting' ) . ':</b> ' . length_accountg ( $this -> account_number );
$label .= '<br><b>' . $langs -> trans ( 'AccountancyJournal' ) . ':</b> ' . $this -> accountancy_journal ;
2017-10-13 13:28:26 +02:00
}
2020-02-28 18:57:17 +01:00
2017-10-13 13:28:26 +02:00
$linkclose = '" title="' . dol_escape_htmltag ( $label , 1 ) . '" class="classfortooltip">' ;
$url = DOL_URL_ROOT . '/compta/bank/card.php?id=' . $this -> id ;
2021-02-23 21:09:01 +01:00
if ( $mode == 'transactions' ) {
2017-10-13 13:28:26 +02:00
$url = DOL_URL_ROOT . '/compta/bank/bankentries_list.php?id=' . $this -> id ;
2021-02-23 21:09:01 +01:00
} elseif ( $mode == 'receipts' ) {
2017-10-13 13:28:26 +02:00
$url = DOL_URL_ROOT . '/compta/bank/releve.php?account=' . $this -> id ;
}
2021-02-23 21:09:01 +01:00
if ( $option != 'nolink' ) {
2017-10-13 13:28:26 +02:00
// Add param to save lastsearch_values or not
2019-11-14 12:09:15 +01:00
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
2021-02-23 21:09:01 +01:00
if ( $save_lastsearch_value == - 1 && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) {
$add_save_lastsearch_values = 1 ;
}
if ( $add_save_lastsearch_values ) {
$url .= '&save_lastsearch_values=1' ;
}
2017-10-13 13:28:26 +02:00
}
2017-11-02 15:03:09 +01:00
$linkstart = '<a href="' . $url . $linkclose ;
$linkend = '</a>' ;
2020-10-31 14:32:18 +01:00
if ( $option == 'nolink' ) {
$linkstart = '' ;
$linkend = '' ;
}
2018-07-17 18:16:30 +02:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
2021-02-23 21:09:01 +01:00
if ( $withpicto ) {
$result .= img_object (( $notooltip ? '' : $label ), $this -> picto , ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . 'classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 );
}
if ( $withpicto != 2 ) {
$result .= $this -> ref . ( $option == 'reflabel' && $this -> label ? ' - ' . $this -> label : '' );
}
2017-11-02 15:03:09 +01:00
$result .= $linkend ;
2017-10-13 13:28:26 +02:00
return $result ;
}
// Method after here are common to Account and CompanyBankAccount
/**
* Return if an account has valid information for Direct debit payment
*
* @ return int 1 if correct , <= 0 if wrong
*/
2020-10-31 14:32:18 +01:00
public function verif ()
2017-10-13 13:28:26 +02:00
{
2019-11-14 12:09:15 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/bank.lib.php' ;
2017-10-13 13:28:26 +02:00
$this -> error_number = 0 ;
// Call function to check BAN
2022-04-06 16:15:34 +02:00
if ( ! checkIbanForAccount ( $this )) {
2017-10-13 13:28:26 +02:00
$this -> error_number = 12 ;
2022-04-06 16:15:34 +02:00
$this -> error_message = 'IBANNotValid' ;
}
if ( ! checkSwiftForAccount ( $this )) {
$this -> error_number = 12 ;
$this -> error_message = 'SwiftNotValid' ;
2017-10-13 13:28:26 +02:00
}
/* if ( ! checkBanForAccount ( $this ))
{
2021-02-23 21:09:01 +01:00
$this -> error_number = 12 ;
$this -> error_message = 'BANControlError' ;
} */
if ( $this -> error_number == 0 ) {
2017-10-13 13:28:26 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
return 0 ;
}
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
/**
* Return account country code
*
* @ return string country code
*/
2020-10-31 14:32:18 +01:00
public function getCountryCode ()
2017-10-13 13:28:26 +02:00
{
global $mysoc ;
// We return country code of bank account
2021-02-23 21:09:01 +01:00
if ( ! empty ( $this -> country_code )) {
return $this -> country_code ;
}
2017-10-13 13:28:26 +02:00
// For backward compatibility, we try to guess country from other information
2021-02-23 21:09:01 +01:00
if ( ! empty ( $this -> iban )) {
2017-10-13 13:28:26 +02:00
// If IBAN defined, we can know country of account from it
2019-12-15 15:03:22 +01:00
$reg = array ();
2021-02-23 21:09:01 +01:00
if ( preg_match ( " /^([a-zA-Z][a-zA-Z])/i " , $this -> iban , $reg )) {
return $reg [ 1 ];
}
2017-10-13 13:28:26 +02:00
}
// If this class is linked to a third party
2021-02-23 21:09:01 +01:00
if ( ! empty ( $this -> socid )) {
2019-11-14 12:09:15 +01:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
$company = new Societe ( $this -> db );
$result = $company -> fetch ( $this -> socid );
2021-02-23 21:09:01 +01:00
if ( ! empty ( $company -> country_code )) {
return $company -> country_code ;
}
2017-10-13 13:28:26 +02:00
}
// We return country code of managed company
2021-02-23 21:09:01 +01:00
if ( ! empty ( $mysoc -> country_code )) {
return $mysoc -> country_code ;
}
2017-10-13 13:28:26 +02:00
return '' ;
}
/**
* Return if a bank account is defined with detailed information ( bank code , desk code , number and key ) .
* More information on codes used by countries on page http :// en . wikipedia . org / wiki / Bank_code
*
* @ return int 0 = No bank code need + Account number is enough
* 1 = Need 2 fields for bank code : Bank , Desk ( France , Spain , ... ) + Account number and key
* 2 = Need 1 field for bank code : Bank only ( Sort code for Great Britain , BSB for Australia ) + Account number
*/
2020-10-31 14:32:18 +01:00
public function useDetailedBBAN ()
2017-10-13 13:28:26 +02:00
{
2019-11-14 12:09:15 +01:00
$country_code = $this -> getCountryCode ();
2017-10-13 13:28:26 +02:00
2022-02-14 20:06:42 +01:00
if ( in_array ( $country_code , array ( 'FR' , 'ES' , 'GA' , 'IT' , 'NC' ))) {
2021-02-23 21:09:01 +01:00
return 1 ; // France, Spain, Gabon, ... - Not valid for CH
}
2022-02-14 20:06:42 +01:00
if ( in_array ( $country_code , array ( 'AD' , 'AU' , 'BE' , 'CA' , 'DE' , 'DK' , 'GR' , 'GB' , 'ID' , 'IE' , 'IR' , 'KR' , 'NL' , 'NZ' , 'UK' , 'US' ))) {
2021-02-23 21:09:01 +01:00
return 2 ; // Australia, England...
}
2017-10-13 13:28:26 +02:00
return 0 ;
}
/**
* Return 1 if IBAN / BIC is mandatory ( otherwise option )
*
* @ return int 1 = mandatory / 0 = Not mandatory
*/
2020-10-31 14:32:18 +01:00
public function needIBAN ()
2017-10-13 13:28:26 +02:00
{
2021-08-06 16:35:49 +02:00
global $conf ;
2021-08-11 21:04:03 +02:00
if ( ! empty ( $conf -> global -> MAIN_IBAN_IS_NEVER_MANDATORY )) {
return 0 ;
}
2021-08-06 16:35:49 +02:00
2019-11-14 12:09:15 +01:00
$country_code = $this -> getCountryCode ();
$country_code_in_EEC = array (
'AT' , // Austria
'BE' , // Belgium
'BG' , // Bulgaria
'CY' , // Cyprus
'CZ' , // Czech republic
'DE' , // Germany
'DK' , // Danemark
'EE' , // Estonia
'ES' , // Spain
'FI' , // Finland
'FR' , // France
'GB' , // United Kingdom
'GR' , // Greece
'HR' , // Croatia
'NL' , // Holland
'HU' , // Hungary
'IE' , // Ireland
'IM' , // Isle of Man - Included in UK
'IT' , // Italy
'LT' , // Lithuania
'LU' , // Luxembourg
'LV' , // Latvia
'MC' , // Monaco - Included in France
'MT' , // Malta
2017-10-13 13:28:26 +02:00
//'NO', // Norway
2019-11-14 12:09:15 +01:00
'PL' , // Poland
'PT' , // Portugal
'RO' , // Romania
'SE' , // Sweden
'SK' , // Slovakia
'SI' , // Slovenia
'UK' , // United Kingdom
2017-10-13 13:28:26 +02:00
//'CH', // Switzerland - No. Swizerland in not in EEC
);
2011-03-31 17:27:18 +02:00
2021-02-23 21:09:01 +01:00
if ( in_array ( $country_code , $country_code_in_EEC )) {
return 1 ; // France, Spain, ...
}
2017-10-13 13:28:26 +02:00
return 0 ;
}
/**
* Load miscellaneous information for tab " Info "
*
* @ param int $id Id of object to load
* @ return void
*/
2020-10-31 14:32:18 +01:00
public function info ( $id )
2017-10-13 13:28:26 +02:00
{
}
2012-07-29 20:07:03 +02:00
2016-03-19 23:00:33 +01:00
/**
* Returns the fields in order that this bank account should show to the user
* Will return an array with the following values :
* - BankAccountNumber
* - BankCode
* - BankAccountNumberKey
* - DeskCode
*
* Some countries show less or more bank account properties to the user
2017-07-15 03:41:07 +02:00
*
2016-10-05 21:49:04 +02:00
* @ param int $includeibanbic 1 = Return also key for IBAN and BIC
2019-03-21 20:15:19 +01:00
* @ return array Array of fields to show
* @ see useDetailedBBAN ()
2016-03-19 23:00:33 +01:00
*/
2019-01-27 15:20:16 +01:00
public function getFieldsToShow ( $includeibanbic = 0 )
2016-03-19 23:00:33 +01:00
{
//Get the required properties depending on the country
$detailedBBAN = $this -> useDetailedBBAN ();
if ( $detailedBBAN == 0 ) {
2019-11-14 12:09:15 +01:00
$fieldarray = array (
2016-06-30 15:49:57 +02:00
'BankAccountNumber'
2016-03-19 23:00:33 +01:00
);
} elseif ( $detailedBBAN == 2 ) {
2019-11-14 12:09:15 +01:00
$fieldarray = array (
2016-06-30 15:49:57 +02:00
'BankCode' ,
'BankAccountNumber'
2016-03-19 23:00:33 +01:00
);
2016-06-30 16:07:53 +02:00
} else {
2019-11-14 12:09:15 +01:00
$fieldarray = self :: getAccountNumberOrder ();
2016-06-30 16:07:53 +02:00
}
2016-08-01 15:15:32 +02:00
//if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
2021-02-23 21:09:01 +01:00
if ( $includeibanbic ) {
2019-11-14 12:09:15 +01:00
$fieldarray [] = 'IBAN' ;
$fieldarray [] = 'BIC' ;
2016-10-05 21:49:04 +02:00
}
2016-08-01 15:15:32 +02:00
//}
2016-03-19 23:00:33 +01:00
//Get the order the properties are shown
2016-06-30 16:07:53 +02:00
return $fieldarray ;
2016-03-19 23:00:33 +01:00
}
/**
* Returns the components of the bank account in order .
* Will return an array with the following values :
* - BankAccountNumber
* - BankCode
* - BankAccountNumberKey
* - DeskCode
*
* @ return array
*/
public static function getAccountNumberOrder ()
{
global $conf ;
$fieldlists = array (
2016-06-30 15:49:57 +02:00
'BankCode' ,
'DeskCode' ,
'BankAccountNumber' ,
'BankAccountNumberKey'
2016-03-19 23:00:33 +01:00
);
if ( ! empty ( $conf -> global -> BANK_SHOW_ORDER_OPTION )) {
if ( is_numeric ( $conf -> global -> BANK_SHOW_ORDER_OPTION )) {
if ( $conf -> global -> BANK_SHOW_ORDER_OPTION == '1' ) {
$fieldlists = array (
'BankCode' ,
'DeskCode' ,
'BankAccountNumberKey' ,
'BankAccountNumber'
);
}
} else {
//Replace the old AccountNumber key with the new BankAccountNumber key
$fieldlists = explode (
' ' ,
2020-10-31 14:32:18 +01:00
preg_replace ( '/ ?[^Bank]AccountNumber ?/' , 'BankAccountNumber' , $conf -> global -> BANK_SHOW_ORDER_OPTION )
2016-03-19 23:00:33 +01:00
);
}
}
return $fieldlists ;
}
2012-07-29 20:07:03 +02:00
2017-10-13 13:28:26 +02:00
/**
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
* @ return void
*/
2020-10-31 14:32:18 +01:00
public function initAsSpecimen ()
2017-10-13 13:28:26 +02:00
{
2022-07-03 14:29:59 +02:00
// Example of IBAN FR7630001007941234567890185
2017-10-13 13:28:26 +02:00
$this -> specimen = 1 ;
$this -> ref = 'MBA' ;
$this -> label = 'My Big Company Bank account' ;
$this -> bank = 'MyBank' ;
$this -> courant = Account :: TYPE_CURRENT ;
$this -> clos = Account :: STATUS_OPEN ;
2022-07-03 14:29:59 +02:00
$this -> code_banque = '30001' ;
$this -> code_guichet = '00794' ;
$this -> number = '12345678901' ;
$this -> cle_rib = '85' ;
2017-10-13 13:28:26 +02:00
$this -> bic = 'AA12' ;
2022-07-03 14:29:59 +02:00
$this -> iban = 'FR7630001007941234567890185' ;
$this -> domiciliation = 'Banque de France' ;
2017-10-13 13:28:26 +02:00
$this -> proprio = 'Owner' ;
$this -> owner_address = 'Owner address' ;
$this -> country_id = 1 ;
}
2021-05-04 17:28:20 +02:00
/**
* Function used to replace a thirdparty id with another one .
*
2021-09-27 15:50:19 +02:00
* @ param DoliDB $dbs Database handler
2021-05-04 17:28:20 +02:00
* @ param int $origin_id Old thirdparty id
* @ param int $dest_id New thirdparty id
2021-09-27 15:50:19 +02:00
* @ return bool True = SQL success , False = SQL error
2021-05-04 17:28:20 +02:00
*/
2021-09-27 15:50:19 +02:00
public static function replaceThirdparty ( $dbs , $origin_id , $dest_id )
2021-05-04 17:28:20 +02:00
{
2021-05-06 16:38:34 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " bank_url SET url_id = " . (( int ) $dest_id ) . " WHERE url_id = " . (( int ) $origin_id ) . " AND type='company' " ;
2021-05-04 17:28:20 +02:00
2021-09-27 15:50:19 +02:00
if ( $dbs -> query ( $sql )) {
return true ;
} else {
2021-11-23 22:36:14 +01:00
//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.
2021-09-27 15:50:19 +02:00
//$this->errors = $dbs->lasterror();
2021-05-04 17:28:20 +02:00
return false ;
}
}
2017-10-13 13:28:26 +02:00
}
/**
* Class to manage bank transaction lines
*/
class AccountLine extends CommonObject
{
2018-08-22 10:37:16 +02:00
/**
* @ var string Error code ( or message )
*/
2019-11-14 12:09:15 +01:00
public $error = '' ;
2018-09-02 13:38:11 +02:00
2020-10-31 14:32:18 +01:00
/**
* @ var DoliDB Database handler .
*/
public $db ;
2018-09-02 13:38:11 +02:00
2018-08-22 11:48:26 +02:00
/**
* @ var string ID to identify managed object
*/
2019-11-14 12:09:15 +01:00
public $element = 'bank' ;
2018-09-02 13:38:11 +02:00
2018-08-22 12:04:50 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2019-11-14 12:09:15 +01:00
public $table_element = 'bank' ;
2018-09-02 13:38:11 +02:00
2018-09-05 10:31:12 +02:00
/**
* @ var string String with name of icon for myobject . Must be the part after the 'object_' into object_myobject . png
*/
2020-09-15 13:27:48 +02:00
public $picto = 'accountline' ;
2017-10-13 13:28:26 +02:00
2018-08-22 11:24:31 +02:00
/**
* @ var int ID
*/
public $id ;
2018-09-02 13:38:11 +02:00
2020-10-31 14:32:18 +01:00
/**
* @ var string Ref
*/
public $ref ;
2018-08-31 18:36:15 +02:00
2019-03-21 14:59:21 +01:00
/**
2020-10-31 14:32:18 +01:00
* Date creation record ( datec )
*
* @ var integer
*/
public $datec ;
2019-03-21 20:15:19 +01:00
2020-10-31 14:32:18 +01:00
/**
* Date ( dateo )
*
* @ var integer
*/
2018-09-02 10:42:31 +02:00
public $dateo ;
2017-10-13 13:28:26 +02:00
/**
2020-10-31 14:32:18 +01:00
* Date value ( datev )
*
* @ var integer
*/
2018-09-02 10:42:31 +02:00
public $datev ;
2019-03-21 20:15:19 +01:00
2022-04-09 00:55:40 +02:00
public $amount ; /* Amount of payment in the bank account currency */
public $amount_main_currency ; /* Amount in the currency of company if bank account use another currency */
2018-09-02 13:38:11 +02:00
2020-10-31 14:32:18 +01:00
/**
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_user_author ;
2018-10-09 15:38:42 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_user_rappro ;
2018-10-09 15:38:42 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_type ;
2018-10-09 15:38:42 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID of cheque receipt
*/
2018-10-09 15:38:42 +02:00
public $fk_bordereau ;
/**
2020-10-31 14:32:18 +01:00
* @ var int ID of bank account
*/
2018-10-09 15:38:42 +02:00
public $fk_account ;
2020-12-16 11:21:48 +01:00
/**
* @ var string Ref of bank account
*/
public $bank_account_ref ;
2020-05-27 23:23:01 +02:00
/**
* @ var string Label of bank account
*/
public $bank_account_label ;
2017-10-13 13:28:26 +02:00
2020-12-16 11:21:48 +01:00
/**
* @ var string Bank account numero
*/
public $numero_compte ;
2020-10-31 14:32:18 +01:00
/**
2020-05-27 23:23:01 +02:00
* @ var string Name of check issuer
2018-09-20 12:27:06 +02:00
*/
public $emetteur ;
2017-10-13 13:28:26 +02:00
2020-12-16 11:21:48 +01:00
public $rappro ; // Is it conciliated
public $num_releve ; // If conciliated, what is bank statement
public $num_chq ; // Num of cheque
public $bank_chq ; // Bank of cheque
/**
* @ var string bank transaction lines label
*/
public $label ;
public $note ;
2017-10-13 13:28:26 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2020-10-31 14:32:18 +01:00
public function __construct ( DoliDB $db )
2017-10-13 13:28:26 +02:00
{
$this -> db = $db ;
}
/**
* Load into memory content of a bank transaction line
*
* @ param int $rowid Id of bank transaction to load
* @ param string $ref Ref of bank transaction to load
* @ param string $num External num to load ( ex : num of transaction for paypal fee )
* @ return int < 0 if KO , 0 if OK but not found , > 0 if OK and found
*/
2020-10-31 14:32:18 +01:00
public function fetch ( $rowid , $ref = '' , $num = '' )
2017-10-13 13:28:26 +02:00
{
global $conf ;
// Check parameters
2021-02-23 21:09:01 +01:00
if ( empty ( $rowid ) && empty ( $ref ) && empty ( $num )) {
return - 1 ;
}
2017-10-13 13:28:26 +02:00
$sql = " SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account, " ;
2019-11-14 12:09:15 +01:00
$sql .= " b.fk_user_author, b.fk_user_rappro, " ;
$sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note, " ;
$sql .= " b.fk_bordereau, b.banque, b.emetteur, " ;
$sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " bank as b, " ;
$sql .= " " . MAIN_DB_PREFIX . " bank_account as ba " ;
$sql .= " WHERE b.fk_account = ba.rowid " ;
$sql .= " AND ba.entity IN ( " . getEntity ( 'bank_account' ) . " ) " ;
2021-02-23 21:09:01 +01:00
if ( $num ) {
$sql .= " AND b.num_chq=' " . $this -> db -> escape ( $num ) . " ' " ;
} elseif ( $ref ) {
$sql .= " AND b.rowid=' " . $this -> db -> escape ( $ref ) . " ' " ;
} else {
$sql .= " AND b.rowid = " . (( int ) $rowid );
}
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
2019-11-14 12:09:15 +01:00
$ret = 0 ;
2009-03-12 23:49:05 +01:00
2017-10-13 13:28:26 +02:00
$obj = $this -> db -> fetch_object ( $result );
2021-02-23 21:09:01 +01:00
if ( $obj ) {
2019-11-14 12:09:15 +01:00
$this -> id = $obj -> rowid ;
2017-10-13 13:28:26 +02:00
$this -> rowid = $obj -> rowid ;
2019-11-14 12:09:15 +01:00
$this -> ref = $obj -> rowid ;
2002-12-23 15:19:41 +01:00
2017-10-13 13:28:26 +02:00
$this -> datec = $obj -> datec ;
$this -> datev = $obj -> datev ;
$this -> dateo = $obj -> dateo ;
2019-11-14 12:09:15 +01:00
$this -> amount = $obj -> amount ;
2017-10-13 13:28:26 +02:00
$this -> label = $obj -> label ;
$this -> note = $obj -> note ;
2005-10-15 17:32:01 +02:00
2017-10-13 13:28:26 +02:00
$this -> fk_user_author = $obj -> fk_user_author ;
$this -> fk_user_rappro = $obj -> fk_user_rappro ;
2019-11-14 12:09:15 +01:00
$this -> fk_type = $obj -> fk_type ; // Type of transaction
$this -> rappro = $obj -> rappro ;
$this -> num_releve = $obj -> num_releve ;
2017-10-13 13:28:26 +02:00
2019-11-14 12:09:15 +01:00
$this -> num_chq = $obj -> num_chq ;
$this -> bank_chq = $obj -> banque ;
$this -> fk_bordereau = $obj -> fk_bordereau ;
2017-10-13 13:28:26 +02:00
2019-11-14 12:09:15 +01:00
$this -> fk_account = $obj -> fk_account ;
2017-10-13 13:28:26 +02:00
$this -> bank_account_ref = $obj -> bank_account_ref ;
$this -> bank_account_label = $obj -> bank_account_label ;
2019-11-14 12:09:15 +01:00
$ret = 1 ;
2017-10-13 13:28:26 +02:00
}
$this -> db -> free ( $result );
return $ret ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
return - 1 ;
}
}
2011-02-07 18:51:16 +01:00
2016-03-05 16:26:30 +01:00
/**
* Inserts a transaction to a bank account
*
* @ return int < 0 if KO , rowid of the line if OK
*/
public function insert ()
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " bank ( " ;
$sql .= " datec " ;
$sql .= " , dateo " ;
$sql .= " , datev " ;
$sql .= " , label " ;
$sql .= " , amount " ;
2022-04-09 00:55:40 +02:00
$sql .= " , amount_main_currency " ;
2016-03-05 16:26:30 +01:00
$sql .= " , fk_user_author " ;
$sql .= " , num_chq " ;
$sql .= " , fk_account " ;
$sql .= " , fk_type " ;
2017-07-15 03:41:07 +02:00
$sql .= " , emetteur,banque " ;
2016-03-05 16:26:30 +01:00
$sql .= " , rappro " ;
2017-07-15 03:41:07 +02:00
$sql .= " , numero_compte " ;
2021-01-05 02:18:42 +01:00
$sql .= " , num_releve " ;
2016-03-05 16:26:30 +01:00
$sql .= " ) VALUES ( " ;
$sql .= " ' " . $this -> db -> idate ( $this -> datec ) . " ' " ;
$sql .= " , ' " . $this -> db -> idate ( $this -> dateo ) . " ' " ;
$sql .= " , ' " . $this -> db -> idate ( $this -> datev ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> label ) . " ' " ;
$sql .= " , " . price2num ( $this -> amount );
2022-04-09 00:55:40 +02:00
$sql .= " , " . ( empty ( $this -> amount_main_currency ) ? " NULL " : price2num ( $this -> amount_main_currency ));
$sql .= " , " . ( $this -> fk_user_author > 0 ? (( int ) $this -> fk_user_author ) : " null " );
2017-09-15 15:41:07 +02:00
$sql .= " , " . ( $this -> num_chq ? " ' " . $this -> db -> escape ( $this -> num_chq ) . " ' " : " null " );
$sql .= " , ' " . $this -> db -> escape ( $this -> fk_account ) . " ' " ;
2016-03-05 16:26:30 +01:00
$sql .= " , ' " . $this -> db -> escape ( $this -> fk_type ) . " ' " ;
$sql .= " , " . ( $this -> emetteur ? " ' " . $this -> db -> escape ( $this -> emetteur ) . " ' " : " null " );
$sql .= " , " . ( $this -> bank_chq ? " ' " . $this -> db -> escape ( $this -> bank_chq ) . " ' " : " null " );
$sql .= " , " . ( int ) $this -> rappro ;
2017-07-15 03:41:07 +02:00
$sql .= " , " . ( $this -> numero_compte ? " ' " . $this -> db -> escape ( $this -> numero_compte ) . " ' " : " '' " );
2021-01-05 02:18:42 +01:00
$sql .= " , " . ( $this -> num_releve ? " ' " . $this -> db -> escape ( $this -> num_releve ) . " ' " : " null " );
2016-03-05 16:26:30 +01:00
$sql .= " ) " ;
dol_syslog ( get_class ( $this ) . " ::insert " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . 'bank' );
return $this -> id ;
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
/**
2020-02-05 16:30:39 +01:00
* Delete bank transaction record
2017-10-13 13:28:26 +02:00
*
* @ param User $user User object that delete
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function delete ( User $user = null )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
global $conf ;
2020-02-05 16:30:39 +01:00
2019-11-14 12:09:15 +01:00
$nbko = 0 ;
2011-02-07 18:51:16 +01:00
2021-02-23 21:09:01 +01:00
if ( $this -> rappro ) {
2017-10-13 13:28:26 +02:00
// Protection to avoid any delete of consolidated lines
2019-11-14 12:09:15 +01:00
$this -> error = " ErrorDeleteNotPossibleLineIsConsolidated " ;
2017-10-13 13:28:26 +02:00
return - 1 ;
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$this -> db -> begin ();
2011-08-05 23:05:19 +02:00
2020-02-05 16:30:39 +01:00
// Protection to avoid any delete of accounted lines. Protection on by default
2021-02-23 21:09:01 +01:00
if ( empty ( $conf -> global -> BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING )) {
2021-08-23 17:41:11 +02:00
$sql = " SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . " accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = " . (( int ) $this -> id );
2020-02-05 16:30:39 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj && $obj -> nb ) {
$this -> error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible' ;
$this -> db -> rollback ();
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-02-05 16:30:39 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
}
2017-10-13 13:28:26 +02:00
// Delete urls
2019-11-14 12:09:15 +01:00
$result = $this -> delete_urls ( $user );
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
2017-10-13 13:28:26 +02:00
$nbko ++ ;
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " bank_class WHERE lineid= " . ( int ) $this -> rowid ;
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( ! $result ) {
$nbko ++ ;
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " bank WHERE rowid= " . ( int ) $this -> rowid ;
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( ! $result ) {
$nbko ++ ;
}
2017-10-13 13:28:26 +02:00
2021-02-23 21:09:01 +01:00
if ( ! $nbko ) {
2017-10-13 13:28:26 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$this -> db -> rollback ();
return - $nbko ;
}
}
2011-08-05 23:05:19 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Delete bank line records
*
* @ param User $user User object that delete
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function delete_urls ( User $user = null )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-11-14 12:09:15 +01:00
$nbko = 0 ;
2011-08-05 23:05:19 +02:00
2021-02-23 21:09:01 +01:00
if ( $this -> rappro ) {
2017-10-13 13:28:26 +02:00
// Protection to avoid any delete of consolidated lines
2019-11-14 12:09:15 +01:00
$this -> error = " ErrorDeleteNotPossibleLineIsConsolidated " ;
2017-10-13 13:28:26 +02:00
return - 1 ;
}
2011-08-05 23:05:19 +02:00
2017-10-13 13:28:26 +02:00
$this -> db -> begin ();
2011-08-05 23:05:19 +02:00
2017-10-13 13:28:26 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " bank_url WHERE fk_bank= " . ( int ) $this -> rowid ;
dol_syslog ( get_class ( $this ) . " ::delete_urls " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( ! $result ) {
$nbko ++ ;
}
2011-02-07 18:51:16 +01:00
2021-02-23 21:09:01 +01:00
if ( ! $nbko ) {
2017-10-13 13:28:26 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$this -> db -> rollback ();
return - $nbko ;
}
}
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
/**
* Update bank account record in database
*
* @ param User $user Object user making update
* @ param int $notrigger 0 = Disable all triggers
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function update ( User $user , $notrigger = 0 )
2017-10-13 13:28:26 +02:00
{
$this -> db -> begin ();
2017-07-15 03:41:07 +02:00
2017-10-13 13:28:26 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " bank SET " ;
2019-11-14 12:09:15 +01:00
$sql .= " amount = " . price2num ( $this -> amount ) . " , " ;
$sql .= " datev=' " . $this -> db -> idate ( $this -> datev ) . " ', " ;
$sql .= " dateo=' " . $this -> db -> idate ( $this -> dateo ) . " ' " ;
2021-06-09 15:36:47 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> rowid );
2011-02-07 18:51:16 +01:00
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2017-10-13 13:28:26 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$this -> db -> rollback ();
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> error ();
2017-10-13 13:28:26 +02:00
return - 1 ;
}
}
2017-07-15 03:41:07 +02:00
2009-01-09 20:53:26 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
2018-08-13 17:26:32 +02:00
* Update conciliation field
2017-10-13 13:28:26 +02:00
*
2018-10-10 12:47:18 +02:00
* @ param User $user Objet user making update
* @ param int $cat Category id
* @ param int $conciliated 1 = Set transaction to conciliated , 0 = Keep transaction non conciliated
* @ return int < 0 if KO , > 0 if OK
2017-10-13 13:28:26 +02:00
*/
2020-10-31 14:32:18 +01:00
public function update_conciliation ( User $user , $cat , $conciliated = 1 )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-11-14 12:09:15 +01:00
global $conf , $langs ;
2017-10-13 13:28:26 +02:00
$this -> db -> begin ();
// Check statement field
2021-02-23 21:09:01 +01:00
if ( ! empty ( $conf -> global -> BANK_STATEMENT_REGEX_RULE )) {
if ( ! preg_match ( '/' . $conf -> global -> BANK_STATEMENT_REGEX_RULE . '/' , $this -> num_releve )) {
2019-11-14 12:09:15 +01:00
$this -> errors [] = $langs -> trans ( " ErrorBankStatementNameMustFollowRegex " , $conf -> global -> BANK_STATEMENT_REGEX_RULE );
2017-10-13 13:28:26 +02:00
return - 1 ;
}
}
$sql = " UPDATE " . MAIN_DB_PREFIX . " bank SET " ;
2021-03-19 12:08:40 +01:00
$sql .= " rappro = " . (( int ) $conciliated );
2019-11-14 12:09:15 +01:00
$sql .= " , num_releve = ' " . $this -> db -> escape ( $this -> num_releve ) . " ' " ;
2021-02-23 21:09:01 +01:00
if ( $conciliated ) {
$sql .= " , fk_user_rappro = " . $user -> id ;
}
2021-03-19 12:08:40 +01:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::update_conciliation " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
if ( ! empty ( $cat )) {
2017-10-13 13:28:26 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " bank_class ( " ;
2019-11-14 12:09:15 +01:00
$sql .= " lineid " ;
$sql .= " , fk_categ " ;
$sql .= " ) VALUES ( " ;
$sql .= $this -> id ;
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $cat );
2019-11-14 12:09:15 +01:00
$sql .= " ) " ;
2017-10-13 13:28:26 +02:00
dol_syslog ( get_class ( $this ) . " ::update_conciliation " , LOG_DEBUG );
$this -> db -> query ( $sql );
// No error check. Can fail if category already affected
}
2019-11-14 12:09:15 +01:00
$this -> rappro = 1 ;
2017-10-13 13:28:26 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Increase / decrease value date of a rowid
*
* @ param int $rowid Id of line
* @ param int $sign 1 or - 1
* @ return int > 0 if OK , 0 if KO
*/
2020-10-31 14:32:18 +01:00
public function datev_change ( $rowid , $sign = 1 )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2021-03-19 12:08:40 +01:00
$sql = " SELECT datev FROM " . MAIN_DB_PREFIX . " bank WHERE rowid = " . (( int ) $rowid );
2017-10-13 13:28:26 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2019-11-14 12:09:15 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$newdate = $this -> db -> jdate ( $obj -> datev ) + ( 3600 * 24 * $sign );
2017-10-13 13:28:26 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " bank SET " ;
2019-11-14 12:09:15 +01:00
$sql .= " datev = ' " . $this -> db -> idate ( $newdate ) . " ' " ;
2021-03-19 12:08:40 +01:00
$sql .= " WHERE rowid = " . (( int ) $rowid );
2017-10-13 13:28:26 +02:00
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
if ( $this -> db -> affected_rows ( $result )) {
2017-10-13 13:28:26 +02:00
return 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
dol_print_error ( $this -> db );
return 0 ;
}
2021-02-23 21:09:01 +01:00
} else {
dol_print_error ( $this -> db );
}
2017-10-13 13:28:26 +02:00
return 0 ;
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Increase value date of a rowid
*
* @ param int $id Id of line to change
* @ return int > 0 if OK , 0 if KO
*/
2020-10-31 14:32:18 +01:00
public function datev_next ( $id )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-01-27 11:55:16 +01:00
return $this -> datev_change ( $id , 1 );
2017-10-13 13:28:26 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Decrease value date of a rowid
*
* @ param int $id Id of line to change
* @ return int > 0 if OK , 0 if KO
*/
2020-10-31 14:32:18 +01:00
public function datev_previous ( $id )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-01-27 11:55:16 +01:00
return $this -> datev_change ( $id , - 1 );
2017-10-13 13:28:26 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Increase / decrease operation date of a rowid
*
* @ param int $rowid Id of line
* @ param int $sign 1 or - 1
* @ return int > 0 if OK , 0 if KO
*/
2020-10-31 14:32:18 +01:00
public function dateo_change ( $rowid , $sign = 1 )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2021-03-19 12:08:40 +01:00
$sql = " SELECT dateo FROM " . MAIN_DB_PREFIX . " bank WHERE rowid = " . (( int ) $rowid );
2017-10-13 13:28:26 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2019-11-14 12:09:15 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$newdate = $this -> db -> jdate ( $obj -> dateo ) + ( 3600 * 24 * $sign );
2017-10-13 13:28:26 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " bank SET " ;
2019-11-14 12:09:15 +01:00
$sql .= " dateo = ' " . $this -> db -> idate ( $newdate ) . " ' " ;
2021-03-19 12:08:40 +01:00
$sql .= " WHERE rowid = " . (( int ) $rowid );
2017-10-13 13:28:26 +02:00
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
if ( $this -> db -> affected_rows ( $result )) {
2017-10-13 13:28:26 +02:00
return 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
dol_print_error ( $this -> db );
return 0 ;
}
2021-02-23 21:09:01 +01:00
} else {
dol_print_error ( $this -> db );
}
2017-10-13 13:28:26 +02:00
return 0 ;
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Increase operation date of a rowid
*
* @ param int $id Id of line to change
* @ return int > 0 if OK , 0 if KO
*/
2020-10-31 14:32:18 +01:00
public function dateo_next ( $id )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-01-27 11:55:16 +01:00
return $this -> dateo_change ( $id , 1 );
2017-10-13 13:28:26 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Decrease operation date of a rowid
*
* @ param int $id Id of line to change
* @ return int > 0 if OK , 0 if KO
*/
2020-10-31 14:32:18 +01:00
public function dateo_previous ( $id )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-01-27 11:55:16 +01:00
return $this -> dateo_change ( $id , - 1 );
2017-10-13 13:28:26 +02:00
}
/**
* Load miscellaneous information for tab " Info "
*
* @ param int $id Id of object to load
* @ return void
*/
2020-10-31 14:32:18 +01:00
public function info ( $id )
2017-10-13 13:28:26 +02:00
{
$sql = 'SELECT b.rowid, b.datec, b.tms as datem,' ;
2019-11-14 12:09:15 +01:00
$sql .= ' b.fk_user_author, b.fk_user_rappro' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'bank as b' ;
2021-03-14 11:48:39 +01:00
$sql .= ' WHERE b.rowid = ' . (( int ) $id );
2017-10-13 13:28:26 +02:00
2019-11-14 12:09:15 +01:00
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
if ( $this -> db -> num_rows ( $result )) {
2017-10-13 13:28:26 +02:00
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
2021-02-23 21:09:01 +01:00
if ( $obj -> fk_user_author ) {
2017-10-13 13:28:26 +02:00
$cuser = new User ( $this -> db );
$cuser -> fetch ( $obj -> fk_user_author );
2019-11-14 12:09:15 +01:00
$this -> user_creation = $cuser ;
2017-10-13 13:28:26 +02:00
}
2021-02-23 21:09:01 +01:00
if ( $obj -> fk_user_rappro ) {
2017-10-13 13:28:26 +02:00
$ruser = new User ( $this -> db );
$ruser -> fetch ( $obj -> fk_user_rappro );
$this -> user_rappro = $ruser ;
}
$this -> date_creation = $this -> db -> jdate ( $obj -> datec );
$this -> date_modification = $this -> db -> jdate ( $obj -> datem );
//$this->date_rappro = $obj->daterappro; // Not yet managed
}
$this -> db -> free ( $result );
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
dol_print_error ( $this -> db );
}
}
/**
2020-02-05 19:23:00 +01:00
* Return clickable name ( with picto eventually )
2017-10-13 13:28:26 +02:00
*
* @ param int $withpicto 0 = No picto , 1 = Include picto into link , 2 = Only picto
* @ param int $maxlen Longueur max libelle
2020-02-05 19:23:00 +01:00
* @ param string $option Option ( '' , 'showall' , 'showconciliated' , 'showconciliatedandaccounted' ) . Options may be slow .
2018-03-10 00:13:41 +01:00
* @ param int $notooltip 1 = Disable tooltip
2017-10-13 13:28:26 +02:00
* @ return string Chaine avec URL
*/
2020-10-31 14:32:18 +01:00
public function getNomUrl ( $withpicto = 0 , $maxlen = 0 , $option = '' , $notooltip = 0 )
2017-10-13 13:28:26 +02:00
{
global $langs ;
2019-11-14 12:09:15 +01:00
$result = '' ;
2020-09-15 13:27:48 +02:00
2022-07-26 11:53:00 +02:00
$label = img_picto ( '' , $this -> picto ) . ' <u>' . $langs -> trans ( " BankTransactionLine " ) . '</u>:<br>' ;
2020-09-15 13:27:48 +02:00
$label .= '<b>' . $langs -> trans ( " Ref " ) . ':</b> ' . $this -> ref ;
2021-03-14 12:20:23 +01:00
$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">' ;
2019-11-14 12:09:15 +01:00
$linkend = '</a>' ;
2017-10-13 13:28:26 +02:00
2017-11-23 15:22:32 +01:00
$result .= $linkstart ;
2021-02-23 21:09:01 +01:00
if ( $withpicto ) {
$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 );
}
if ( $withpicto != 2 ) {
$result .= ( $this -> ref ? $this -> ref : $this -> id );
}
2017-11-23 15:22:32 +01:00
$result .= $linkend ;
2017-10-13 13:28:26 +02:00
2021-02-23 21:09:01 +01:00
if ( $option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted' ) {
$result .= ' <span class="opacitymedium">(' ;
}
if ( $option == 'showall' ) {
2019-11-14 12:09:15 +01:00
$result .= $langs -> trans ( " BankAccount " ) . ': ' ;
$accountstatic = new Account ( $this -> db );
$accountstatic -> id = $this -> fk_account ;
$accountstatic -> ref = $this -> bank_account_ref ;
$accountstatic -> label = $this -> bank_account_label ;
$result .= $accountstatic -> getNomUrl ( 0 ) . ', ' ;
2017-10-13 13:28:26 +02:00
}
2021-02-23 21:09:01 +01:00
if ( $option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted' ) {
2019-11-14 12:09:15 +01:00
$result .= $langs -> trans ( " BankLineConciliated " ) . ': ' ;
$result .= yn ( $this -> rappro );
2017-10-13 13:28:26 +02:00
}
2021-02-23 21:09:01 +01:00
if ( $option == 'showall' || $option == 'showconciliatedandaccounted' ) {
2021-08-23 17:41:11 +02:00
$sql = " SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . " accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = " . (( int ) $this -> id );
2020-02-05 19:23:00 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj && $obj -> nb ) {
$result .= ' - ' . $langs -> trans ( " Accounted " ) . ': ' . yn ( 1 );
} else {
$result .= ' - ' . $langs -> trans ( " Accounted " ) . ': ' . yn ( 0 );
}
}
}
2021-02-23 21:09:01 +01:00
if ( $option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted' ) {
$result .= ')</span>' ;
}
2017-10-13 13:28:26 +02:00
return $result ;
}
/**
* Return label of status ( activity , closed )
*
* @ param int $mode 0 = libelle long , 1 = libelle court , 2 = Picto + Libelle court , 3 = Picto , 4 = Picto + Libelle long
* @ return string Libelle
*/
2020-10-31 14:32:18 +01:00
public function getLibStatut ( $mode = 0 )
2017-10-13 13:28:26 +02:00
{
2019-01-27 11:55:16 +01:00
return $this -> LibStatut ( $this -> status , $mode );
2017-10-13 13:28:26 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-13 13:28:26 +02:00
/**
* Renvoi le libelle d ' un statut donne
*
2019-10-24 16:43:23 +02:00
* @ param int $status Id statut
2017-10-13 13:28:26 +02:00
* @ param int $mode 0 = libelle long , 1 = libelle court , 2 = Picto + Libelle court , 3 = Picto , 4 = Picto + Libelle long , 5 = Libelle court + Picto
* @ return string Libelle du statut
*/
2020-10-31 14:32:18 +01:00
public function LibStatut ( $status , $mode = 0 )
2017-10-13 13:28:26 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2017-10-13 13:28:26 +02:00
global $langs ;
//$langs->load('companies');
/*
2021-02-23 21:09:01 +01:00
if ( $mode == 0 )
{
if ( $status == 0 ) return $langs -> trans ( " ActivityCeased " );
if ( $status == 1 ) return $langs -> trans ( " InActivity " );
}
if ( $mode == 1 )
{
if ( $status == 0 ) return $langs -> trans ( " ActivityCeased " );
if ( $status == 1 ) return $langs -> trans ( " InActivity " );
}
if ( $mode == 2 )
{
if ( $status == 0 ) return img_picto ( $langs -> trans ( " ActivityCeased " ), 'statut5' , 'class="pictostatus"' ) . ' ' . $langs -> trans ( " ActivityCeased " );
if ( $status == 1 ) return img_picto ( $langs -> trans ( " InActivity " ), 'statut4' , 'class="pictostatus"' ) . ' ' . $langs -> trans ( " InActivity " );
}
if ( $mode == 3 )
{
if ( $status == 0 ) return img_picto ( $langs -> trans ( " ActivityCeased " ), 'statut5' , 'class="pictostatus"' );
if ( $status == 1 ) return img_picto ( $langs -> trans ( " InActivity " ), 'statut4' , 'class="pictostatus"' );
}
if ( $mode == 4 )
{
if ( $status == 0 ) return img_picto ( $langs -> trans ( " ActivityCeased " ), 'statut5' , 'class="pictostatus"' ) . ' ' . $langs -> trans ( " ActivityCeased " );
if ( $status == 1 ) return img_picto ( $langs -> trans ( " InActivity " ), 'statut4' , 'class="pictostatus"' ) . ' ' . $langs -> trans ( " InActivity " );
}
if ( $mode == 5 )
{
if ( $status == 0 ) return $langs -> trans ( " ActivityCeased " ) . ' ' . img_picto ( $langs -> trans ( " ActivityCeased " ), 'statut5' , 'class="pictostatus"' );
if ( $status == 1 ) return $langs -> trans ( " InActivity " ) . ' ' . img_picto ( $langs -> trans ( " InActivity " ), 'statut4' , 'class="pictostatus"' );
} */
2017-10-13 13:28:26 +02:00
}
2017-07-15 03:41:07 +02:00
2018-01-22 04:13:29 +01:00
/**
* Return if a bank line was dispatched into bookkeeping
*
* @ return int < 0 if KO , 0 = no , 1 = yes
*/
public function getVentilExportCompta ()
{
$alreadydispatched = 0 ;
$type = 'bank' ;
2021-03-30 17:53:25 +02:00
$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 );
2018-01-22 04:13:29 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2018-01-22 04:13:29 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-23 21:09:01 +01:00
if ( $obj ) {
2018-01-22 04:13:29 +01:00
$alreadydispatched = $obj -> nb ;
}
2020-05-21 15:05:19 +02:00
} else {
2018-01-22 04:13:29 +01:00
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
2021-02-23 21:09:01 +01:00
if ( $alreadydispatched ) {
2018-01-22 04:13:29 +01:00
return 1 ;
}
return 0 ;
}
2005-10-15 17:32:01 +02:00
}