account.class.php 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670
  1. <?php
  2. /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
  4. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
  6. * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@inodbox.com>
  7. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  8. * Copyright (C) 2015-2016 Marcos García <marcosgdf@gmail.com>
  9. * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  10. * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
  11. * Copyright (C) 2019 JC Prieto <jcprieto@virtual20.com><prietojc@gmail.com>
  12. * Copyright (C) 2022-2023 Frédéric France <frederic.france@netlogic.fr>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  26. */
  27. /**
  28. * \file htdocs/compta/bank/class/account.class.php
  29. * \ingroup bank
  30. * \brief File of class to manage bank accounts
  31. */
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  33. /**
  34. * Class to manage bank accounts
  35. */
  36. class Account extends CommonObject
  37. {
  38. /**
  39. * @var string ID to identify managed object
  40. */
  41. public $element = 'bank_account';
  42. /**
  43. * @var string Name of table without prefix where object is stored
  44. */
  45. public $table_element = 'bank_account';
  46. /**
  47. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  48. */
  49. public $picto = 'account';
  50. /**
  51. * @var int Use id instead of rowid
  52. * @deprecated
  53. * @see $id
  54. */
  55. public $rowid;
  56. /**
  57. * Account Label
  58. * @var string
  59. */
  60. public $label;
  61. /**
  62. * Bank account type. Check TYPE_ constants
  63. * @var int
  64. */
  65. public $courant;
  66. /**
  67. * Bank account type. Check TYPE_ constants
  68. * @var int
  69. */
  70. public $type;
  71. /**
  72. * Bank name
  73. * @var string
  74. */
  75. public $bank;
  76. /**
  77. * Status
  78. * @var int
  79. */
  80. public $clos = self::STATUS_OPEN;
  81. /**
  82. * Does it need to be conciliated?
  83. * @var int
  84. */
  85. public $rappro = 1;
  86. /**
  87. * Webpage
  88. * @var string
  89. */
  90. public $url;
  91. /**
  92. * Bank number. If in SEPA area, you should move to IBAN field
  93. * @var string
  94. */
  95. public $code_banque;
  96. /**
  97. * Branch number. If in SEPA area, you should move to IBAN field
  98. * @var string
  99. */
  100. public $code_guichet;
  101. /**
  102. * Account number. If in SEPA area, you should move to IBAN field
  103. * @var string
  104. */
  105. public $number;
  106. /**
  107. * Bank account number control digit. If in SEPA area, you should move to IBAN field
  108. * @var string
  109. */
  110. public $cle_rib;
  111. /**
  112. * BIC/Swift code
  113. * @var string
  114. */
  115. public $bic;
  116. /**
  117. * IBAN number (International Bank Account Number). Stored into iban_prefix field into database (TODO Rename field in database)
  118. * @var string
  119. */
  120. public $iban;
  121. /**
  122. * IBAN number
  123. *
  124. * @var string
  125. * @deprecated see $iban
  126. */
  127. public $iban_prefix;
  128. /**
  129. * XML SEPA format: place Payment Type Information (PmtTpInf) in Credit Transfer Transaction Information (CdtTrfTxInf)
  130. * @var int
  131. */
  132. public $pti_in_ctti = 0;
  133. /**
  134. * Name of account holder
  135. * @var string
  136. */
  137. public $proprio;
  138. /**
  139. * Address of account holder
  140. * @var string
  141. */
  142. public $owner_address;
  143. public $owner_zip;
  144. public $owner_town;
  145. public $owner_country_id;
  146. public $owner_country_code;
  147. /**
  148. * Address of the bank account
  149. * @var string
  150. */
  151. public $domiciliation; // deprecated, use now address
  152. public $address;
  153. public $state_id;
  154. public $state_code;
  155. public $state;
  156. /**
  157. * Variable containing all account types with their respective translated label.
  158. * Defined in __construct
  159. * @var array
  160. */
  161. public $type_lib = array();
  162. /**
  163. * Variable containing all account statuses with their respective translated label.
  164. * Defined in __construct
  165. * @var array
  166. */
  167. public $status = array();
  168. /**
  169. * Accountancy code
  170. * @var string
  171. */
  172. public $account_number;
  173. /**
  174. * @var int ID
  175. */
  176. public $fk_accountancy_journal;
  177. /**
  178. * @var string Label of journal
  179. */
  180. public $accountancy_journal;
  181. /**
  182. * Currency code
  183. * @var string
  184. */
  185. public $currency_code;
  186. /**
  187. * Currency code
  188. * @var string
  189. * @deprecated Use currency_code instead
  190. */
  191. public $account_currency_code;
  192. /**
  193. * Authorized minimum balance
  194. * @var float
  195. */
  196. public $min_allowed;
  197. /**
  198. * Desired minimum balance
  199. * @var float
  200. */
  201. public $min_desired;
  202. /**
  203. * Notes
  204. * @var string
  205. */
  206. public $comment;
  207. /**
  208. * Date of the initial balance. Used in Account::create
  209. * @var int
  210. */
  211. public $date_solde;
  212. /**
  213. * Balance. Used in Account::create
  214. * @var float
  215. * @deprecated
  216. * @see $balance
  217. */
  218. public $solde;
  219. /**
  220. * Balance. Used in Account::create
  221. * @var float
  222. */
  223. public $balance;
  224. /**
  225. * Creditor Identifier CI. Some banks use different ICS for direct debit and bank tranfer
  226. * @var string
  227. */
  228. public $ics;
  229. /**
  230. * Creditor Identifier for Bank Transfer.
  231. * @var string
  232. */
  233. public $ics_transfer;
  234. /**
  235. * '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')
  236. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  237. * 'label' the translation key.
  238. * 'enabled' is a condition when the field must be managed.
  239. * 'position' is the sort order of field.
  240. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  241. * '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)
  242. * 'noteditable' says if field is not editable (1 or 0)
  243. * '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.
  244. * 'index' if we want an index in database.
  245. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  246. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  247. * '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).
  248. * 'css' is the CSS style to use on field. For example: 'maxwidth200'
  249. * 'help' is a string visible as a tooltip on field
  250. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  251. * '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.
  252. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  253. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  254. *
  255. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  256. */
  257. // BEGIN MODULEBUILDER PROPERTIES
  258. /**
  259. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  260. */
  261. public $fields = array(
  262. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
  263. 'ref' =>array('type'=>'varchar(12)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>25),
  264. 'label' =>array('type'=>'varchar(30)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>30),
  265. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>35, 'index'=>1),
  266. 'bank' =>array('type'=>'varchar(60)', 'label'=>'Bank', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
  267. 'code_banque' =>array('type'=>'varchar(128)', 'label'=>'Code banque', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
  268. 'code_guichet' =>array('type'=>'varchar(6)', 'label'=>'Code guichet', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
  269. 'number' =>array('type'=>'varchar(255)', 'label'=>'Number', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
  270. 'cle_rib' =>array('type'=>'varchar(5)', 'label'=>'Cle rib', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
  271. 'bic' =>array('type'=>'varchar(11)', 'label'=>'Bic', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
  272. 'iban_prefix' =>array('type'=>'varchar(34)', 'label'=>'Iban prefix', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
  273. 'country_iban' =>array('type'=>'varchar(2)', 'label'=>'Country iban', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
  274. 'cle_iban' =>array('type'=>'varchar(2)', 'label'=>'Cle iban', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
  275. 'domiciliation' =>array('type'=>'varchar(255)', 'label'=>'Domiciliation', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
  276. 'state_id' =>array('type'=>'integer', 'label'=>'StateId', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
  277. 'fk_pays' =>array('type'=>'integer', 'label'=>'Country', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>95),
  278. 'proprio' =>array('type'=>'varchar(60)', 'label'=>'Proprio', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
  279. 'owner_address' =>array('type'=>'varchar(255)', 'label'=>'Owner address', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
  280. 'owner_zip' =>array('type'=>'varchar(25)', 'label'=>'Owner zip', 'enabled'=>1, 'visible'=>-1, 'position'=>106),
  281. 'owner_town' =>array('type'=>'varchar(50)', 'label'=>'Owner town', 'enabled'=>1, 'visible'=>-1, 'position'=>107),
  282. 'owner_country_id' =>array('type'=>'integer', 'label'=>'Owner country', 'enabled'=>1, 'visible'=>-1, 'position'=>108),
  283. 'courant' =>array('type'=>'smallint(6)', 'label'=>'Courant', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>110),
  284. 'clos' =>array('type'=>'smallint(6)', 'label'=>'Clos', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>115),
  285. 'rappro' =>array('type'=>'smallint(6)', 'label'=>'Rappro', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
  286. 'url' =>array('type'=>'varchar(128)', 'label'=>'Url', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
  287. 'account_number' =>array('type'=>'varchar(32)', 'label'=>'Account number', 'enabled'=>1, 'visible'=>-1, 'position'=>130),
  288. 'fk_accountancy_journal' =>array('type'=>'integer', 'label'=>'Accountancy journal ID', 'enabled'=>1, 'visible'=>-1, 'position'=>132),
  289. 'accountancy_journal' =>array('type'=>'varchar(20)', 'label'=>'Accountancy journal', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
  290. 'currency_code' =>array('type'=>'varchar(3)', 'label'=>'Currency code', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>140),
  291. 'min_allowed' =>array('type'=>'integer', 'label'=>'Min allowed', 'enabled'=>1, 'visible'=>-1, 'position'=>145),
  292. 'min_desired' =>array('type'=>'integer', 'label'=>'Min desired', 'enabled'=>1, 'visible'=>-1, 'position'=>150),
  293. 'comment' =>array('type'=>'text', 'label'=>'Comment', 'enabled'=>1, 'visible'=>-1, 'position'=>155),
  294. 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>156),
  295. 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>157),
  296. 'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>160),
  297. 'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>165),
  298. 'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>170),
  299. 'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>175),
  300. 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>180),
  301. 'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
  302. );
  303. // END MODULEBUILDER PROPERTIES
  304. /**
  305. * Current account
  306. */
  307. const TYPE_CURRENT = 1;
  308. /**
  309. * Cash account
  310. */
  311. const TYPE_CASH = 2;
  312. /**
  313. * Savings account
  314. */
  315. const TYPE_SAVINGS = 0;
  316. const STATUS_OPEN = 0;
  317. const STATUS_CLOSED = 1;
  318. /**
  319. * Constructor
  320. *
  321. * @param DoliDB $db Database handler
  322. */
  323. public function __construct(DoliDB $db)
  324. {
  325. global $langs;
  326. $this->db = $db;
  327. $this->solde = 0;
  328. $this->type_lib = array(
  329. self::TYPE_SAVINGS => $langs->trans("BankType0"),
  330. self::TYPE_CURRENT => $langs->trans("BankType1"),
  331. self::TYPE_CASH => $langs->trans("BankType2"),
  332. );
  333. $this->status = array(
  334. self::STATUS_OPEN => $langs->trans("StatusAccountOpened"),
  335. self::STATUS_CLOSED => $langs->trans("StatusAccountClosed")
  336. );
  337. }
  338. /**
  339. * Shows the account number in the appropriate format
  340. *
  341. * @return string
  342. */
  343. public function __toString()
  344. {
  345. $string = '';
  346. foreach ($this->getFieldsToShow() as $val) {
  347. if ($val == 'BankCode') {
  348. $string .= $this->code_banque.' ';
  349. } elseif ($val == 'BankAccountNumber') {
  350. $string .= $this->number.' ';
  351. } elseif ($val == 'DeskCode') {
  352. $string .= $this->code_guichet.' ';
  353. } elseif ($val == 'BankAccountNumberKey') {
  354. $string .= $this->cle_rib.' ';
  355. } elseif ($val == 'BIC') {
  356. $string .= $this->bic.' ';
  357. } elseif ($val == 'IBAN') {
  358. $string .= $this->iban.' ';
  359. }
  360. }
  361. return trim($string);
  362. }
  363. /**
  364. * Return if a bank account need to be conciliated
  365. *
  366. * @return int 1 if need to be concialiated, < 0 otherwise.
  367. */
  368. public function canBeConciliated()
  369. {
  370. global $conf;
  371. if (empty($this->rappro)) {
  372. return -1;
  373. }
  374. if ($this->courant == Account::TYPE_CASH && empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
  375. return -2;
  376. }
  377. if ($this->clos) {
  378. return -3;
  379. }
  380. return 1;
  381. }
  382. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  383. /**
  384. * Add a link between bank line record and its source
  385. *
  386. * @param int $line_id Id of bank entry
  387. * @param int $url_id Id of object related to link
  388. * @param string $url Url (deprecated, we use now 'url_id' and 'type' instead)
  389. * @param string $label Link label
  390. * @param string $type Type of link ('payment', 'company', 'member', ...)
  391. * @return int <0 if KO, id line if OK
  392. */
  393. public function add_url_line($line_id, $url_id, $url, $label, $type)
  394. {
  395. // phpcs:enable
  396. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
  397. $sql .= "fk_bank";
  398. $sql .= ", url_id";
  399. $sql .= ", url"; // deprecated
  400. $sql .= ", label";
  401. $sql .= ", type";
  402. $sql .= ") VALUES (";
  403. $sql .= " ".((int) $line_id);
  404. $sql .= ", ".((int) $url_id);
  405. $sql .= ", '".$this->db->escape($url)."'"; // dperecated
  406. $sql .= ", '".$this->db->escape($label)."'";
  407. $sql .= ", '".$this->db->escape($type)."'";
  408. $sql .= ")";
  409. dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
  410. if ($this->db->query($sql)) {
  411. $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
  412. return $rowid;
  413. } else {
  414. $this->error = $this->db->lasterror();
  415. return -1;
  416. }
  417. }
  418. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  419. /**
  420. * TODO Move this into AccountLine
  421. * Return array with links from llx_bank_url
  422. *
  423. * @param int $fk_bank To search using bank transaction id
  424. * @param int $url_id To search using link to
  425. * @param string $type To search using type
  426. * @return array|int Array of links array('url'=>, 'url_id'=>, 'label'=>, 'type'=> 'fk_bank'=> ) or -1 on error
  427. */
  428. public function get_url($fk_bank = '', $url_id = '', $type = '')
  429. {
  430. // phpcs:enable
  431. $lines = array();
  432. // Check parameters
  433. if (!empty($fk_bank) && (!empty($url_id) || !empty($type))) {
  434. $this->error = "ErrorBadParameter";
  435. return -1;
  436. }
  437. $sql = "SELECT fk_bank, url_id, url, label, type";
  438. $sql .= " FROM ".MAIN_DB_PREFIX."bank_url";
  439. if ($fk_bank > 0) {
  440. $sql .= " WHERE fk_bank = ".((int) $fk_bank);
  441. } else {
  442. $sql .= " WHERE url_id = ".((int) $url_id)." AND type = '".$this->db->escape($type)."'";
  443. }
  444. $sql .= " ORDER BY type, label";
  445. dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
  446. $result = $this->db->query($sql);
  447. if ($result) {
  448. $i = 0;
  449. $num = $this->db->num_rows($result);
  450. while ($i < $num) {
  451. $obj = $this->db->fetch_object($result);
  452. // Anciens liens (pour compatibilite)
  453. $lines[$i][0] = $obj->url;
  454. $lines[$i][1] = $obj->url_id;
  455. $lines[$i][2] = $obj->label;
  456. $lines[$i][3] = $obj->type;
  457. // Nouveaux liens
  458. $lines[$i]['url'] = $obj->url;
  459. $lines[$i]['url_id'] = $obj->url_id;
  460. $lines[$i]['label'] = $obj->label;
  461. $lines[$i]['type'] = $obj->type;
  462. $lines[$i]['fk_bank'] = $obj->fk_bank;
  463. $i++;
  464. }
  465. } else {
  466. dol_print_error($this->db);
  467. }
  468. return $lines;
  469. }
  470. /**
  471. * Add an entry into table ".MAIN_DB_PREFIX."bank
  472. *
  473. * @param int $date Date operation
  474. * @param string $oper 'VIR','PRE','LIQ','VAD','CB','CHQ'...
  475. * @param string $label Descripton
  476. * @param float $amount Amount
  477. * @param string $num_chq Numero cheque or transfer
  478. * @param int $categorie Category id (optionnal)
  479. * @param User $user User that create
  480. * @param string $emetteur Name of cheque writer
  481. * @param string $banque Bank of cheque writer
  482. * @param string $accountancycode When we record a free bank entry, we must provide accounting account if accountancy module is on.
  483. * @param int $datev Date value
  484. * @param string $num_releve Label of bank receipt for reconciliation
  485. * @param float $amount_main_currency Amount
  486. * @return int Rowid of added entry, <0 if KO
  487. */
  488. public function addline($date, $oper, $label, $amount, $num_chq, $categorie, User $user, $emetteur = '', $banque = '', $accountancycode = '', $datev = null, $num_releve = '', $amount_main_currency = null)
  489. {
  490. // Deprecation warning
  491. if (is_numeric($oper)) {
  492. dol_syslog(__METHOD__.": using numeric operations is deprecated", LOG_WARNING);
  493. }
  494. if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
  495. $this->id = $this->rowid;
  496. }
  497. // Clean parameters
  498. $emetteur = trim($emetteur);
  499. $banque = trim($banque);
  500. $label = trim($label);
  501. $now = dol_now();
  502. if (is_numeric($oper)) { // Clean operation to have a code instead of a rowid
  503. $sql = "SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
  504. $sql .= " WHERE id = ".((int) $oper);
  505. $sql .= " AND entity IN (".getEntity('c_paiement').")";
  506. $resql = $this->db->query($sql);
  507. if ($resql) {
  508. $obj = $this->db->fetch_object($resql);
  509. $oper = $obj->code;
  510. } else {
  511. dol_print_error($this->db, 'Failed to get payment type code');
  512. return -1;
  513. }
  514. }
  515. // Check parameters
  516. if (!$oper) {
  517. $this->error = "oper not defined";
  518. return -1;
  519. }
  520. if (!$this->id) {
  521. $this->error = "this->id not defined";
  522. return -2;
  523. }
  524. if ($this->courant == Account::TYPE_CASH && $oper != 'LIQ') {
  525. $this->error = "ErrorCashAccountAcceptsOnlyCashMoney";
  526. return -3;
  527. }
  528. $this->db->begin();
  529. if (is_null($datev) || empty($datev)) {
  530. $datev = $date;
  531. }
  532. $accline = new AccountLine($this->db);
  533. $accline->datec = $now;
  534. $accline->dateo = $date;
  535. $accline->datev = $datev;
  536. $accline->label = $label;
  537. $accline->amount = $amount;
  538. $accline->amount_main_currency = $amount_main_currency;
  539. $accline->fk_user_author = $user->id;
  540. $accline->fk_account = $this->id;
  541. $accline->fk_type = $oper;
  542. $accline->numero_compte = $accountancycode;
  543. $accline->num_releve = $num_releve;
  544. if ($num_chq) {
  545. $accline->num_chq = $num_chq;
  546. }
  547. if ($emetteur) {
  548. $accline->emetteur = $emetteur;
  549. }
  550. if ($banque) {
  551. $accline->bank_chq = $banque;
  552. }
  553. if ($accline->insert() > 0) {
  554. if ($categorie > 0) {
  555. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class(";
  556. $sql .= "lineid, fk_categ";
  557. $sql .= ") VALUES (";
  558. $sql .= ((int) $accline->id).", '".$this->db->escape($categorie)."'";
  559. $sql .= ")";
  560. $result = $this->db->query($sql);
  561. if (!$result) {
  562. $this->error = $this->db->lasterror();
  563. $this->db->rollback();
  564. return -4;
  565. }
  566. }
  567. $this->db->commit();
  568. return $accline->id;
  569. } else {
  570. $this->error = $accline->error;
  571. $this->errors = $accline->errors;
  572. $this->db->rollback();
  573. return -5;
  574. }
  575. }
  576. /**
  577. * Create bank account into database
  578. *
  579. * @param User $user Object user making creation
  580. * @param int $notrigger 1=Disable triggers
  581. * @return int < 0 if KO, > 0 if OK
  582. */
  583. public function create(User $user, $notrigger = 0)
  584. {
  585. global $langs, $conf;
  586. $error = 0;
  587. // Clean parameters
  588. if (!$this->min_allowed) {
  589. $this->min_allowed = 0;
  590. }
  591. if (!$this->min_desired) {
  592. $this->min_desired = 0;
  593. }
  594. // Check parameters
  595. if (empty($this->country_id)) {
  596. $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
  597. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  598. return -1;
  599. }
  600. if (empty($this->ref)) {
  601. $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
  602. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  603. return -1;
  604. }
  605. if (empty($this->date_solde)) {
  606. $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateInitialBalance"));
  607. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  608. return -1;
  609. }
  610. // Chargement librairie pour acces fonction controle RIB
  611. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  612. $now = dol_now();
  613. $this->db->begin();
  614. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
  615. $sql .= "datec";
  616. $sql .= ", ref";
  617. $sql .= ", label";
  618. $sql .= ", entity";
  619. $sql .= ", account_number";
  620. $sql .= ", fk_accountancy_journal";
  621. $sql .= ", bank";
  622. $sql .= ", code_banque";
  623. $sql .= ", code_guichet";
  624. $sql .= ", number";
  625. $sql .= ", cle_rib";
  626. $sql .= ", bic";
  627. $sql .= ", iban_prefix";
  628. $sql .= ", domiciliation";
  629. $sql .= ", pti_in_ctti";
  630. $sql .= ", proprio";
  631. $sql .= ", owner_address";
  632. $sql .= ", owner_zip";
  633. $sql .= ", owner_town";
  634. $sql .= ", owner_country_id";
  635. $sql .= ", currency_code";
  636. $sql .= ", rappro";
  637. $sql .= ", min_allowed";
  638. $sql .= ", min_desired";
  639. $sql .= ", comment";
  640. $sql .= ", state_id";
  641. $sql .= ", fk_pays";
  642. $sql .= ", ics";
  643. $sql .= ", ics_transfer";
  644. $sql .= ") VALUES (";
  645. $sql .= "'".$this->db->idate($now)."'";
  646. $sql .= ", '".$this->db->escape($this->ref)."'";
  647. $sql .= ", '".$this->db->escape($this->label)."'";
  648. $sql .= ", ".((int) $conf->entity);
  649. $sql .= ", '".$this->db->escape($this->account_number)."'";
  650. $sql .= ", ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
  651. $sql .= ", '".$this->db->escape($this->bank)."'";
  652. $sql .= ", '".$this->db->escape($this->code_banque)."'";
  653. $sql .= ", '".$this->db->escape($this->code_guichet)."'";
  654. $sql .= ", '".$this->db->escape($this->number)."'";
  655. $sql .= ", '".$this->db->escape($this->cle_rib)."'";
  656. $sql .= ", '".$this->db->escape($this->bic)."'";
  657. $sql .= ", '".$this->db->escape($this->iban)."'";
  658. $sql .= ", '".$this->db->escape($this->domiciliation)."'";
  659. $sql .= ", ".((int) $this->pti_in_ctti);
  660. $sql .= ", '".$this->db->escape($this->proprio)."'";
  661. $sql .= ", '".$this->db->escape($this->owner_address)."'";
  662. $sql .= ", '".$this->db->escape($this->owner_zip)."'";
  663. $sql .= ", '".$this->db->escape($this->owner_town)."'";
  664. $sql .= ", ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
  665. $sql .= ", '".$this->db->escape($this->currency_code)."'";
  666. $sql .= ", ".((int) $this->rappro);
  667. $sql .= ", ".price2num($this->min_allowed, 'MT');
  668. $sql .= ", ".price2num($this->min_desired, 'MT');
  669. $sql .= ", '".$this->db->escape($this->comment)."'";
  670. $sql .= ", ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
  671. $sql .= ", ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
  672. $sql .= ", '".$this->db->escape($this->ics)."'";
  673. $sql .= ", '".$this->db->escape($this->ics_transfer)."'";
  674. $sql .= ")";
  675. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  676. $resql = $this->db->query($sql);
  677. if ($resql) {
  678. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
  679. $result = $this->update($user, 1);
  680. if ($result > 0) {
  681. $accline = new AccountLine($this->db);
  682. $accline->datec = $this->db->idate($now);
  683. $accline->label = '('.$langs->trans("InitialBankBalance").')';
  684. $accline->amount = price2num($this->solde);
  685. $accline->fk_user_author = $user->id;
  686. $accline->fk_account = $this->id;
  687. $accline->datev = $this->db->idate($this->date_solde);
  688. $accline->dateo = $this->db->idate($this->date_solde);
  689. $accline->fk_type = 'SOLD';
  690. if ($accline->insert() < 0) {
  691. $error++;
  692. $this->error = $accline->error;
  693. $this->errors = $accline->errors;
  694. }
  695. if (!$error) {
  696. $result = $this->insertExtraFields();
  697. if ($result < 0) {
  698. $error++;
  699. }
  700. }
  701. if (!$error && !$notrigger) {
  702. // Call trigger
  703. $result = $this->call_trigger('BANKACCOUNT_CREATE', $user);
  704. if ($result < 0) {
  705. $error++;
  706. }
  707. // End call triggers
  708. }
  709. } else {
  710. $error++;
  711. }
  712. } else {
  713. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  714. $this->error = $langs->trans("ErrorBankLabelAlreadyExists");
  715. $error++;
  716. } else {
  717. $this->error = $this->db->error()." sql=".$sql;
  718. $error++;
  719. }
  720. }
  721. if (!$error) {
  722. $this->db->commit();
  723. return $this->id;
  724. } else {
  725. $this->db->rollback();
  726. return -1 * $error;
  727. }
  728. }
  729. /**
  730. * Update bank account card
  731. *
  732. * @param User $user Object user making action
  733. * @param int $notrigger 1=Disable triggers
  734. * @return int <0 if KO, >0 if OK
  735. */
  736. public function update(User $user, $notrigger = 0)
  737. {
  738. global $langs, $conf;
  739. $error = 0;
  740. $this->db->begin();
  741. // Check parameters
  742. if (empty($this->country_id)) {
  743. $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"));
  744. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  745. return -1;
  746. }
  747. if (empty($this->ref)) {
  748. $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
  749. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  750. return -1;
  751. }
  752. if (!$this->label) {
  753. $this->label = "???";
  754. }
  755. $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
  756. $sql .= " ref = '".$this->db->escape($this->ref)."'";
  757. $sql .= ",label = '".$this->db->escape($this->label)."'";
  758. $sql .= ",courant = ".((int) $this->courant);
  759. $sql .= ",clos = ".((int) $this->clos);
  760. $sql .= ",rappro = ".((int) $this->rappro);
  761. $sql .= ",url = ".($this->url ? "'".$this->db->escape($this->url)."'" : "null");
  762. $sql .= ",account_number = '".$this->db->escape($this->account_number)."'";
  763. $sql .= ",fk_accountancy_journal = ".($this->fk_accountancy_journal > 0 ? ((int) $this->fk_accountancy_journal) : "null");
  764. $sql .= ",bank = '".$this->db->escape($this->bank)."'";
  765. $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
  766. $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
  767. $sql .= ",number='".$this->db->escape($this->number)."'";
  768. $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
  769. $sql .= ",bic='".$this->db->escape($this->bic)."'";
  770. $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
  771. $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
  772. $sql .= ",pti_in_ctti=".((int) $this->pti_in_ctti);
  773. $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
  774. $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
  775. $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
  776. $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
  777. $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
  778. $sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
  779. $sql .= ",min_allowed = ".($this->min_allowed != '' ? price2num($this->min_allowed) : "null");
  780. $sql .= ",min_desired = ".($this->min_desired != '' ? price2num($this->min_desired) : "null");
  781. $sql .= ",comment = '".$this->db->escape($this->comment)."'";
  782. $sql .= ",state_id = ".($this->state_id > 0 ? ((int) $this->state_id) : "null");
  783. $sql .= ",fk_pays = ".($this->country_id > 0 ? ((int) $this->country_id) : "null");
  784. $sql .= ",ics = '".$this->db->escape($this->ics)."'";
  785. $sql .= ",ics_transfer = '".$this->db->escape($this->ics_transfer)."'";
  786. $sql .= " WHERE rowid = ".((int) $this->id);
  787. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  788. $result = $this->db->query($sql);
  789. if ($result) {
  790. // Actions on extra fields (by external module or standard code)
  791. if (!$error) {
  792. $result = $this->insertExtraFields();
  793. if ($result < 0) {
  794. $error++;
  795. }
  796. }
  797. if (!$error && !$notrigger) {
  798. // Call trigger
  799. $result = $this->call_trigger('BANKACCOUNT_MODIFY', $user);
  800. if ($result < 0) {
  801. $error++;
  802. }
  803. // End call triggers
  804. }
  805. } else {
  806. $error++;
  807. $this->error = $this->db->lasterror();
  808. dol_print_error($this->db);
  809. }
  810. if (!$error) {
  811. $this->db->commit();
  812. return $this->id;
  813. } else {
  814. $this->db->rollback();
  815. return -1 * $error;
  816. }
  817. }
  818. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  819. /**
  820. * Update BBAN (RIB) account fields
  821. *
  822. * @param User $user Object user making update
  823. * @return int <0 if KO, >0 if OK
  824. */
  825. public function update_bban(User $user = null)
  826. {
  827. // phpcs:enable
  828. global $conf, $langs;
  829. // Load library to get BAN control function
  830. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  831. dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
  832. // Check parameters
  833. if (!$this->ref) {
  834. $this->error = $langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->trans("Ref"));
  835. return -2;
  836. }
  837. $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
  838. $sql .= " bank = '".$this->db->escape($this->bank)."'";
  839. $sql .= ",code_banque='".$this->db->escape($this->code_banque)."'";
  840. $sql .= ",code_guichet='".$this->db->escape($this->code_guichet)."'";
  841. $sql .= ",number='".$this->db->escape($this->number)."'";
  842. $sql .= ",cle_rib='".$this->db->escape($this->cle_rib)."'";
  843. $sql .= ",bic='".$this->db->escape($this->bic)."'";
  844. $sql .= ",iban_prefix = '".$this->db->escape($this->iban)."'";
  845. $sql .= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
  846. $sql .= ",proprio = '".$this->db->escape($this->proprio)."'";
  847. $sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";
  848. $sql .= ",owner_zip = '".$this->db->escape($this->owner_zip)."'";
  849. $sql .= ",owner_town = '".$this->db->escape($this->owner_town)."'";
  850. $sql .= ",owner_country_id = ".($this->owner_country_id > 0 ? ((int) $this->owner_country_id) : "null");
  851. $sql .= ",state_id = ".($this->state_id > 0 ? $this->state_id : "null");
  852. $sql .= ",fk_pays = ".($this->country_id > 0 ? $this->country_id : "null");
  853. $sql .= " WHERE rowid = ".((int) $this->id);
  854. $sql .= " AND entity = ".((int) $conf->entity);
  855. dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
  856. $result = $this->db->query($sql);
  857. if ($result) {
  858. return 1;
  859. } else {
  860. $this->error = $this->db->lasterror();
  861. dol_print_error($this->db);
  862. return -1;
  863. }
  864. }
  865. /**
  866. * Load a bank account into memory from database
  867. *
  868. * @param int $id Id of bank account to get
  869. * @param string $ref Ref of bank account to get
  870. * @return int <0 if KO, >0 if OK
  871. */
  872. public function fetch($id, $ref = '')
  873. {
  874. global $conf;
  875. if (empty($id) && empty($ref)) {
  876. $this->error = "ErrorBadParameters";
  877. return -1;
  878. }
  879. $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,";
  880. $sql .= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
  881. $sql .= " ba.domiciliation as address, ba.pti_in_ctti, ba.proprio, ba.owner_address, ba.owner_zip, ba.owner_town, ba.owner_country_id, ba.state_id, ba.fk_pays as country_id,";
  882. $sql .= " ba.account_number, ba.fk_accountancy_journal, ba.currency_code,";
  883. $sql .= " ba.min_allowed, ba.min_desired, ba.comment,";
  884. $sql .= " ba.datec as date_creation, ba.tms as date_update, ba.ics, ba.ics_transfer,";
  885. $sql .= ' c.code as country_code, c.label as country,';
  886. $sql .= ' d.code_departement as state_code, d.nom as state,';
  887. $sql .= ' aj.code as accountancy_journal';
  888. $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
  889. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
  890. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
  891. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'accounting_journal as aj ON aj.rowid=ba.fk_accountancy_journal';
  892. $sql .= " WHERE ba.entity IN (".getEntity($this->element).")";
  893. if ($id) {
  894. $sql .= " AND ba.rowid = ".((int) $id);
  895. }
  896. if ($ref) {
  897. $sql .= " AND ba.ref = '".$this->db->escape($ref)."'";
  898. }
  899. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  900. $result = $this->db->query($sql);
  901. if ($result) {
  902. if ($this->db->num_rows($result)) {
  903. $obj = $this->db->fetch_object($result);
  904. $this->id = $obj->rowid;
  905. $this->rowid = $obj->rowid;
  906. $this->ref = $obj->ref;
  907. $this->label = $obj->label;
  908. $this->type = $obj->courant;
  909. $this->courant = $obj->courant;
  910. $this->bank = $obj->bank;
  911. $this->clos = $obj->clos;
  912. $this->rappro = $obj->rappro;
  913. $this->url = $obj->url;
  914. $this->code_banque = $obj->code_banque;
  915. $this->code_guichet = $obj->code_guichet;
  916. $this->number = $obj->number;
  917. $this->cle_rib = $obj->cle_rib;
  918. $this->bic = $obj->bic;
  919. $this->iban = $obj->iban;
  920. $this->domiciliation = $obj->address;
  921. $this->address = $obj->address;
  922. $this->pti_in_ctti = $obj->pti_in_ctti;
  923. $this->proprio = $obj->proprio;
  924. $this->owner_address = $obj->owner_address;
  925. $this->owner_zip = $obj->owner_zip;
  926. $this->owner_town = $obj->owner_town;
  927. $this->owner_country_id = $obj->owner_country_id;
  928. $this->state_id = $obj->state_id;
  929. $this->state_code = $obj->state_code;
  930. $this->state = $obj->state;
  931. $this->country_id = $obj->country_id;
  932. $this->country_code = $obj->country_code;
  933. $this->country = $obj->country;
  934. $this->account_number = $obj->account_number;
  935. $this->fk_accountancy_journal = $obj->fk_accountancy_journal;
  936. $this->accountancy_journal = $obj->accountancy_journal;
  937. $this->currency_code = $obj->currency_code;
  938. $this->account_currency_code = $obj->currency_code;
  939. $this->min_allowed = $obj->min_allowed;
  940. $this->min_desired = $obj->min_desired;
  941. $this->comment = $obj->comment;
  942. $this->date_creation = $this->db->jdate($obj->date_creation);
  943. $this->date_update = $this->db->jdate($obj->date_update);
  944. $this->ics = $obj->ics;
  945. $this->ics_transfer = $obj->ics_transfer;
  946. // Retrieve all extrafield
  947. // fetch optionals attributes and labels
  948. $this->fetch_optionals();
  949. return 1;
  950. } else {
  951. return 0;
  952. }
  953. } else {
  954. $this->error = $this->db->lasterror();
  955. $this->errors[] = $this->error;
  956. return -1;
  957. }
  958. }
  959. /**
  960. * Sets object to supplied categories.
  961. *
  962. * Deletes object from existing categories not supplied.
  963. * Adds it to non existing supplied categories.
  964. * Existing categories are left untouch.
  965. *
  966. * @param int[]|int $categories Category or categories IDs
  967. * @return int <0 if KO, >0 if OK
  968. */
  969. public function setCategories($categories)
  970. {
  971. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  972. return parent::setCategoriesCommon($categories, Categorie::TYPE_ACCOUNT);
  973. }
  974. /**
  975. * Delete bank account from database
  976. *
  977. * @param User $user User deleting
  978. * @return int <0 if KO, >0 if OK
  979. */
  980. public function delete(User $user = null)
  981. {
  982. $error = 0;
  983. $this->db->begin();
  984. // @TODO Check there is no child into llx_payment_various, ... to allow deletion ?
  985. // Delete link between tag and bank account
  986. if (!$error) {
  987. $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
  988. $sql .= " WHERE fk_account = ".((int) $this->id);
  989. $resql = $this->db->query($sql);
  990. if (!$resql) {
  991. $error++;
  992. $this->error = "Error ".$this->db->lasterror();
  993. }
  994. }
  995. if (!$error) {
  996. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
  997. $sql .= " WHERE rowid = ".((int) $this->id);
  998. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  999. $result = $this->db->query($sql);
  1000. if ($result) {
  1001. // Remove extrafields
  1002. if (!$error) {
  1003. $result = $this->deleteExtraFields();
  1004. if ($result < 0) {
  1005. $error++;
  1006. dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
  1007. }
  1008. }
  1009. } else {
  1010. $error++;
  1011. $this->error = "Error ".$this->db->lasterror();
  1012. }
  1013. }
  1014. if (!$error) {
  1015. $this->db->commit();
  1016. return 1;
  1017. } else {
  1018. $this->db->rollback();
  1019. return -1;
  1020. }
  1021. }
  1022. /**
  1023. * Return label of object status
  1024. *
  1025. * @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
  1026. * @return string Label
  1027. */
  1028. public function getLibStatut($mode = 0)
  1029. {
  1030. return $this->LibStatut($this->clos, $mode);
  1031. }
  1032. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1033. /**
  1034. * Return label of given object status
  1035. *
  1036. * @param int $status Id status
  1037. * @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
  1038. * @return string Label
  1039. */
  1040. public function LibStatut($status, $mode = 0)
  1041. {
  1042. // phpcs:enable
  1043. global $langs;
  1044. $langs->load('banks');
  1045. if ($status == self::STATUS_OPEN) {
  1046. $label = $langs->transnoentitiesnoconv("StatusAccountOpened");
  1047. $labelshort = $langs->transnoentitiesnoconv("StatusAccountOpened");
  1048. $statusType = 'status4';
  1049. } else {
  1050. $label = $langs->transnoentitiesnoconv("StatusAccountClosed");
  1051. $labelshort = $langs->transnoentitiesnoconv("StatusAccountClosed");
  1052. $statusType = 'status5';
  1053. }
  1054. return dolGetStatus($label, $labelshort, '', $statusType, $mode);
  1055. }
  1056. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1057. /**
  1058. * Renvoi si un compte peut etre supprimer ou non (sans mouvements)
  1059. *
  1060. * @return boolean vrai si peut etre supprime, faux sinon
  1061. */
  1062. public function can_be_deleted()
  1063. {
  1064. // phpcs:enable
  1065. $can_be_deleted = false;
  1066. $sql = "SELECT COUNT(rowid) as nb";
  1067. $sql .= " FROM ".MAIN_DB_PREFIX."bank";
  1068. $sql .= " WHERE fk_account = ".((int) $this->id);
  1069. $resql = $this->db->query($sql);
  1070. if ($resql) {
  1071. $obj = $this->db->fetch_object($resql);
  1072. if ($obj->nb <= 1) {
  1073. $can_be_deleted = true; // Juste le solde
  1074. }
  1075. } else {
  1076. dol_print_error($this->db);
  1077. }
  1078. return $can_be_deleted;
  1079. }
  1080. /**
  1081. * Return error
  1082. *
  1083. * @return string Error string
  1084. */
  1085. public function error()
  1086. {
  1087. return $this->error;
  1088. }
  1089. /**
  1090. * Return current sold
  1091. *
  1092. * @param int $option 1=Exclude future operation date (this is to exclude input made in advance and have real account sold)
  1093. * @param int $date_end Date until we want to get bank account sold
  1094. * @param string $field dateo or datev
  1095. * @return int current sold (value date <= today)
  1096. */
  1097. public function solde($option = 0, $date_end = '', $field = 'dateo')
  1098. {
  1099. $solde = 0;
  1100. $sql = "SELECT sum(amount) as amount";
  1101. $sql .= " FROM ".MAIN_DB_PREFIX."bank";
  1102. $sql .= " WHERE fk_account = ".((int) $this->id);
  1103. if ($option == 1) {
  1104. $sql .= " AND ".$this->db->escape($field)." <= '".(!empty($date_end) ? $this->db->idate($date_end) : $this->db->idate(dol_now()))."'";
  1105. }
  1106. $resql = $this->db->query($sql);
  1107. if ($resql) {
  1108. if ($this->db->num_rows($resql)) {
  1109. $obj = $this->db->fetch_object($resql);
  1110. $solde = $obj->amount;
  1111. }
  1112. $this->db->free($resql);
  1113. } else {
  1114. $this->errors[] = $this->db->lasterror;
  1115. return -1;
  1116. }
  1117. return price2num($solde, 'MU');
  1118. }
  1119. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1120. /**
  1121. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  1122. *
  1123. * @param User $user Objet user
  1124. * @param int $filteraccountid To get info for a particular account id
  1125. * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
  1126. */
  1127. public function load_board(User $user, $filteraccountid = 0)
  1128. {
  1129. // phpcs:enable
  1130. global $conf, $langs;
  1131. if ($user->socid) {
  1132. return -1; // protection pour eviter appel par utilisateur externe
  1133. }
  1134. $sql = "SELECT b.rowid, b.datev as datefin";
  1135. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
  1136. $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
  1137. $sql .= " WHERE b.rappro=0";
  1138. $sql .= " AND b.fk_account = ba.rowid";
  1139. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  1140. $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
  1141. $sql .= " AND clos = 0";
  1142. if ($filteraccountid) {
  1143. $sql .= " AND ba.rowid = ".((int) $filteraccountid);
  1144. }
  1145. $resql = $this->db->query($sql);
  1146. if ($resql) {
  1147. $langs->load("banks");
  1148. $now = dol_now();
  1149. require_once DOL_DOCUMENT_ROOT.'/core/class/workboardresponse.class.php';
  1150. $response = new WorkboardResponse();
  1151. $response->warning_delay = $conf->bank->rappro->warning_delay / 60 / 60 / 24;
  1152. $response->label = $langs->trans("TransactionsToConciliate");
  1153. $response->labelShort = $langs->trans("TransactionsToConciliateShort");
  1154. $response->url = DOL_URL_ROOT.'/compta/bank/list.php?leftmenu=bank&amp;mainmenu=bank';
  1155. $response->img = img_object('', "payment");
  1156. while ($obj = $this->db->fetch_object($resql)) {
  1157. $response->nbtodo++;
  1158. if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) {
  1159. $response->nbtodolate++;
  1160. }
  1161. }
  1162. return $response;
  1163. } else {
  1164. dol_print_error($this->db);
  1165. $this->error = $this->db->error();
  1166. return -1;
  1167. }
  1168. }
  1169. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1170. /**
  1171. * Charge indicateurs this->nb de tableau de bord
  1172. * @param int $filteraccountid To get info for a particular account id
  1173. * @return int <0 if ko, >0 if ok
  1174. */
  1175. public function load_state_board($filteraccountid = 0)
  1176. {
  1177. // phpcs:enable
  1178. global $user;
  1179. if ($user->socid) {
  1180. return -1; // protection pour eviter appel par utilisateur externe
  1181. }
  1182. $sql = "SELECT count(b.rowid) as nb";
  1183. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
  1184. $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
  1185. $sql .= " WHERE b.fk_account = ba.rowid";
  1186. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  1187. $sql .= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
  1188. $sql .= " AND clos = 0";
  1189. if ($filteraccountid) {
  1190. $sql .= " AND ba.rowid = ".((int) $filteraccountid);
  1191. }
  1192. $resql = $this->db->query($sql);
  1193. if ($resql) {
  1194. while ($obj = $this->db->fetch_object($resql)) {
  1195. $this->nb["banklines"] = $obj->nb;
  1196. }
  1197. $this->db->free($resql);
  1198. } else {
  1199. dol_print_error($this->db);
  1200. $this->error = $this->db->error();
  1201. return -1;
  1202. }
  1203. }
  1204. /**
  1205. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  1206. *
  1207. * @return int Nb of account we can reconciliate
  1208. */
  1209. public function countAccountToReconcile()
  1210. {
  1211. global $db, $conf, $user;
  1212. //Protection against external users
  1213. if ($user->socid) {
  1214. return 0;
  1215. }
  1216. $nb = 0;
  1217. $sql = "SELECT COUNT(ba.rowid) as nb";
  1218. $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
  1219. $sql .= " WHERE ba.rappro > 0 and ba.clos = 0";
  1220. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  1221. if (empty($conf->global->BANK_CAN_RECONCILIATE_CASHACCOUNT)) {
  1222. $sql .= " AND ba.courant != 2";
  1223. }
  1224. $resql = $this->db->query($sql);
  1225. if ($resql) {
  1226. $obj = $this->db->fetch_object($resql);
  1227. $nb = $obj->nb;
  1228. } else {
  1229. dol_print_error($this->db);
  1230. }
  1231. return $nb;
  1232. }
  1233. /**
  1234. * getTooltipContentArray
  1235. * @param array $params params to construct tooltip data
  1236. * @since v18
  1237. * @return array
  1238. */
  1239. public function getTooltipContentArray($params)
  1240. {
  1241. global $langs;
  1242. include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  1243. $datas = array();
  1244. $nofetch = !empty($params['nofetch']);
  1245. $pictos = img_picto('', $this->picto).' <u class="paddingrightnow">'.$langs->trans("BankAccount").'</u>';
  1246. if (isset($this->status)) {
  1247. $pictos .= ' '.$this->getLibStatut(5);
  1248. }
  1249. $datas['picto'] = $pictos;
  1250. $datas['label'] = '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
  1251. $datas['accountnumber'] = '<br><b>'.$langs->trans('AccountNumber').':</b> '.$this->number;
  1252. $datas['iban'] = '<br><b>'.$langs->trans('IBAN').':</b> '.getIbanHumanReadable($this);
  1253. $datas['bic'] = '<br><b>'.$langs->trans('BIC').':</b> '.$this->bic;
  1254. $datas['accountcurrency'] = '<br><b>'.$langs->trans("AccountCurrency").':</b> '.$this->currency_code;
  1255. if (isModEnabled('accounting')) {
  1256. include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  1257. $langs->load("accountancy");
  1258. $datas['accountaccounting'] = '<br><b>'.$langs->trans('AccountAccounting').':</b> '.length_accountg($this->account_number);
  1259. $datas['accountancyjournal'] = '<br><b>'.$langs->trans('AccountancyJournal').':</b> '.$this->accountancy_journal;
  1260. }
  1261. // show categories for this record only in ajax to not overload lists
  1262. if (isModEnabled('categorie') && !$nofetch) {
  1263. require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
  1264. $form = new Form($this->db);
  1265. $datas['categories'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_ACCOUNT, 1);
  1266. }
  1267. return $datas;
  1268. }
  1269. /**
  1270. * Return clicable name (with picto eventually)
  1271. *
  1272. * @param int $withpicto Include picto into link
  1273. * @param string $mode ''=Link to card, 'transactions'=Link to transactions card
  1274. * @param string $option ''=Show ref, 'reflabel'=Show ref+label
  1275. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  1276. * @param int $notooltip 1=Disable tooltip
  1277. * @return string Chaine avec URL
  1278. */
  1279. public function getNomUrl($withpicto = 0, $mode = '', $option = '', $save_lastsearch_value = -1, $notooltip = 0)
  1280. {
  1281. global $conf, $langs, $user;
  1282. include_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  1283. $result = '';
  1284. $classfortooltip = 'classfortooltip';
  1285. $dataparams = '';
  1286. $params = [
  1287. 'id' => $this->id,
  1288. 'objecttype' => $this->element,
  1289. 'option' => $option,
  1290. 'nofetch' => 1,
  1291. ];
  1292. if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
  1293. $classfortooltip = 'classforajaxtooltip';
  1294. $dataparams = ' data-params='.json_encode($params);
  1295. }
  1296. $label = implode($this->getTooltipContentArray($params));
  1297. $linkclose = '"'.$dataparams.' title="'.dol_escape_htmltag($label, 1).'" class="'.$classfortooltip.'">';
  1298. $url = DOL_URL_ROOT.'/compta/bank/card.php?id='.$this->id;
  1299. if ($mode == 'transactions') {
  1300. $url = DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id;
  1301. } elseif ($mode == 'receipts') {
  1302. $url = DOL_URL_ROOT.'/compta/bank/releve.php?account='.$this->id;
  1303. }
  1304. if ($option != 'nolink') {
  1305. // Add param to save lastsearch_values or not
  1306. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  1307. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  1308. $add_save_lastsearch_values = 1;
  1309. }
  1310. if ($add_save_lastsearch_values) {
  1311. $url .= '&save_lastsearch_values=1';
  1312. }
  1313. }
  1314. $linkstart = '<a href="'.$url.$linkclose;
  1315. $linkend = '</a>';
  1316. if ($option == 'nolink') {
  1317. $linkstart = '';
  1318. $linkend = '';
  1319. }
  1320. $result .= $linkstart;
  1321. if ($withpicto) {
  1322. $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
  1323. }
  1324. if ($withpicto != 2) {
  1325. $result .= $this->ref.($option == 'reflabel' && $this->label ? ' - '.$this->label : '');
  1326. }
  1327. $result .= $linkend;
  1328. return $result;
  1329. }
  1330. // Method after here are common to Account and CompanyBankAccount
  1331. /**
  1332. * Return if an account has valid information for Direct debit payment
  1333. *
  1334. * @return int 1 if correct, <=0 if wrong
  1335. */
  1336. public function verif()
  1337. {
  1338. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  1339. $this->error_number = 0;
  1340. // Call function to check BAN
  1341. if (!checkIbanForAccount($this)) {
  1342. $this->error_number = 12;
  1343. $this->error_message = 'IBANNotValid';
  1344. }
  1345. if (!checkSwiftForAccount($this)) {
  1346. $this->error_number = 12;
  1347. $this->error_message = 'SwiftNotValid';
  1348. }
  1349. /*if (! checkBanForAccount($this))
  1350. {
  1351. $this->error_number = 12;
  1352. $this->error_message = 'BANControlError';
  1353. }*/
  1354. if ($this->error_number == 0) {
  1355. return 1;
  1356. } else {
  1357. return 0;
  1358. }
  1359. }
  1360. /**
  1361. * Return account country code
  1362. *
  1363. * @return string country code
  1364. */
  1365. public function getCountryCode()
  1366. {
  1367. global $mysoc;
  1368. // We return country code of bank account
  1369. if (!empty($this->country_code)) {
  1370. return $this->country_code;
  1371. }
  1372. // For backward compatibility, we try to guess country from other information
  1373. if (!empty($this->iban)) {
  1374. // If IBAN defined, we can know country of account from it
  1375. $reg = array();
  1376. if (preg_match("/^([a-zA-Z][a-zA-Z])/i", $this->iban, $reg)) {
  1377. return $reg[1];
  1378. }
  1379. }
  1380. // If this class is linked to a third party
  1381. if (!empty($this->socid)) {
  1382. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  1383. $company = new Societe($this->db);
  1384. $result = $company->fetch($this->socid);
  1385. if (!empty($company->country_code)) {
  1386. return $company->country_code;
  1387. }
  1388. }
  1389. // We return country code of managed company
  1390. if (!empty($mysoc->country_code)) {
  1391. return $mysoc->country_code;
  1392. }
  1393. return '';
  1394. }
  1395. /**
  1396. * Return if a bank account is defined with detailed information (bank code, desk code, number and key).
  1397. * More information on codes used by countries on page http://en.wikipedia.org/wiki/Bank_code
  1398. *
  1399. * @return int 0=No bank code need + Account number is enough
  1400. * 1=Need 2 fields for bank code: Bank, Desk (France, Spain, ...) + Account number and key
  1401. * 2=Need 1 field for bank code: Bank only (Sort code for Great Britain, BSB for Australia) + Account number
  1402. */
  1403. public function useDetailedBBAN()
  1404. {
  1405. $country_code = $this->getCountryCode();
  1406. if (in_array($country_code, array('FR', 'ES', 'GA', 'IT', 'NC'))) {
  1407. return 1; // France, Spain, Gabon, ... - Not valid for CH
  1408. }
  1409. if (in_array($country_code, array('AD', 'AU', 'BE', 'CA', 'DE', 'DK', 'GR', 'GB', 'ID', 'IE', 'IR', 'KR', 'NL', 'NZ', 'UK', 'US'))) {
  1410. return 2; // Australia, England...
  1411. }
  1412. return 0;
  1413. }
  1414. /**
  1415. * Return 1 if IBAN / BIC is mandatory (otherwise option)
  1416. *
  1417. * @return int 1 = mandatory / 0 = Not mandatory
  1418. */
  1419. public function needIBAN()
  1420. {
  1421. global $conf;
  1422. if (!empty($conf->global->MAIN_IBAN_IS_NEVER_MANDATORY)) {
  1423. return 0;
  1424. }
  1425. $country_code = $this->getCountryCode();
  1426. $country_code_in_EEC = array(
  1427. 'AT', // Austria
  1428. 'BE', // Belgium
  1429. 'BG', // Bulgaria
  1430. 'CY', // Cyprus
  1431. 'CZ', // Czech republic
  1432. 'DE', // Germany
  1433. 'DK', // Danemark
  1434. 'EE', // Estonia
  1435. 'ES', // Spain
  1436. 'FI', // Finland
  1437. 'FR', // France
  1438. 'GB', // United Kingdom
  1439. 'GR', // Greece
  1440. 'HR', // Croatia
  1441. 'NL', // Holland
  1442. 'HU', // Hungary
  1443. 'IE', // Ireland
  1444. 'IM', // Isle of Man - Included in UK
  1445. 'IT', // Italy
  1446. 'LT', // Lithuania
  1447. 'LU', // Luxembourg
  1448. 'LV', // Latvia
  1449. 'MC', // Monaco - Included in France
  1450. 'MT', // Malta
  1451. //'NO', // Norway
  1452. 'PL', // Poland
  1453. 'PT', // Portugal
  1454. 'RO', // Romania
  1455. 'SE', // Sweden
  1456. 'SK', // Slovakia
  1457. 'SI', // Slovenia
  1458. 'UK', // United Kingdom
  1459. //'CH', // Switzerland - No. Swizerland in not in EEC
  1460. );
  1461. if (in_array($country_code, $country_code_in_EEC)) {
  1462. return 1; // France, Spain, ...
  1463. }
  1464. return 0;
  1465. }
  1466. /**
  1467. * Load miscellaneous information for tab "Info"
  1468. *
  1469. * @param int $id Id of object to load
  1470. * @return void
  1471. */
  1472. public function info($id)
  1473. {
  1474. }
  1475. /**
  1476. * Returns the fields in order that this bank account should show to the user
  1477. * Will return an array with the following values:
  1478. * - BankAccountNumber
  1479. * - BankCode
  1480. * - BankAccountNumberKey
  1481. * - DeskCode
  1482. *
  1483. * Some countries show less or more bank account properties to the user
  1484. *
  1485. * @param int $includeibanbic 1=Return also key for IBAN and BIC
  1486. * @return array Array of fields to show
  1487. * @see useDetailedBBAN()
  1488. */
  1489. public function getFieldsToShow($includeibanbic = 0)
  1490. {
  1491. //Get the required properties depending on the country
  1492. $detailedBBAN = $this->useDetailedBBAN();
  1493. if ($detailedBBAN == 0) {
  1494. $fieldarray = array(
  1495. 'BankAccountNumber'
  1496. );
  1497. } elseif ($detailedBBAN == 2) {
  1498. $fieldarray = array(
  1499. 'BankCode',
  1500. 'BankAccountNumber'
  1501. );
  1502. } else {
  1503. $fieldarray = self::getAccountNumberOrder();
  1504. }
  1505. //if ($this->needIBAN()) { // return always IBAN and BIC (this was old behaviour)
  1506. if ($includeibanbic) {
  1507. $fieldarray[] = 'IBAN';
  1508. $fieldarray[] = 'BIC';
  1509. }
  1510. //}
  1511. //Get the order the properties are shown
  1512. return $fieldarray;
  1513. }
  1514. /**
  1515. * Returns the components of the bank account in order.
  1516. * Will return an array with the following values:
  1517. * - BankAccountNumber
  1518. * - BankCode
  1519. * - BankAccountNumberKey
  1520. * - DeskCode
  1521. *
  1522. * @return array
  1523. */
  1524. public static function getAccountNumberOrder()
  1525. {
  1526. global $conf;
  1527. $fieldlists = array(
  1528. 'BankCode',
  1529. 'DeskCode',
  1530. 'BankAccountNumber',
  1531. 'BankAccountNumberKey'
  1532. );
  1533. if (!empty($conf->global->BANK_SHOW_ORDER_OPTION)) {
  1534. if (is_numeric($conf->global->BANK_SHOW_ORDER_OPTION)) {
  1535. if ($conf->global->BANK_SHOW_ORDER_OPTION == '1') {
  1536. $fieldlists = array(
  1537. 'BankCode',
  1538. 'DeskCode',
  1539. 'BankAccountNumberKey',
  1540. 'BankAccountNumber'
  1541. );
  1542. }
  1543. } else {
  1544. //Replace the old AccountNumber key with the new BankAccountNumber key
  1545. $fieldlists = explode(
  1546. ' ',
  1547. preg_replace('/ ?[^Bank]AccountNumber ?/', 'BankAccountNumber', $conf->global->BANK_SHOW_ORDER_OPTION)
  1548. );
  1549. }
  1550. }
  1551. return $fieldlists;
  1552. }
  1553. /**
  1554. * Initialise an instance with random values.
  1555. * Used to build previews or test instances.
  1556. * id must be 0 if object instance is a specimen.
  1557. *
  1558. * @return void
  1559. */
  1560. public function initAsSpecimen()
  1561. {
  1562. // Example of IBAN FR7630001007941234567890185
  1563. $this->specimen = 1;
  1564. $this->ref = 'MBA';
  1565. $this->label = 'My Big Company Bank account';
  1566. $this->bank = 'MyBank';
  1567. $this->courant = Account::TYPE_CURRENT;
  1568. $this->clos = Account::STATUS_OPEN;
  1569. $this->code_banque = '30001';
  1570. $this->code_guichet = '00794';
  1571. $this->number = '12345678901';
  1572. $this->cle_rib = '85';
  1573. $this->bic = 'AA12';
  1574. $this->iban = 'FR7630001007941234567890185';
  1575. $this->domiciliation = 'Banque de France';
  1576. $this->proprio = 'Owner';
  1577. $this->owner_address = 'Owner address';
  1578. $this->owner_zip = 'Owner zip';
  1579. $this->owner_town = 'Owner town';
  1580. $this->owner_country_id = 'Owner country_id';
  1581. $this->country_id = 1;
  1582. }
  1583. /**
  1584. * Function used to replace a thirdparty id with another one.
  1585. *
  1586. * @param DoliDB $dbs Database handler
  1587. * @param int $origin_id Old thirdparty id
  1588. * @param int $dest_id New thirdparty id
  1589. * @return bool True=SQL success, False=SQL error
  1590. */
  1591. public static function replaceThirdparty($dbs, $origin_id, $dest_id)
  1592. {
  1593. $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'";
  1594. if ($dbs->query($sql)) {
  1595. return true;
  1596. } else {
  1597. //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.
  1598. //$this->errors = $dbs->lasterror();
  1599. return false;
  1600. }
  1601. }
  1602. /**
  1603. * Return clicable link of object (with eventually picto)
  1604. *
  1605. * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
  1606. * @param array $arraydata Array of data
  1607. * @return string HTML Code for Kanban thumb.
  1608. */
  1609. public function getKanbanView($option = '', $arraydata = null)
  1610. {
  1611. global $langs;
  1612. $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
  1613. $return = '<div class="box-flex-item box-flex-grow-zero">';
  1614. $return .= '<div class="info-box info-box-sm">';
  1615. $return .= '<span class="info-box-icon bg-infobox-action">';
  1616. $return .= img_picto('', $this->picto);
  1617. $return .= '</span>';
  1618. $return .= '<div class="info-box-content">';
  1619. $return .= '<span class="info-box-ref">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
  1620. $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
  1621. if (property_exists($this, 'type_lib')) {
  1622. $return .= '<br><span class="info-box-label opacitymedium" title="'.$this->type_lib[$this->type].'">'.substr($this->type_lib[$this->type], 0, 24).'...</span>';
  1623. }
  1624. if (method_exists($this, 'solde')) {
  1625. $return .= '<br><a href="'.DOL_URL_ROOT.'/compta/bank/bankentries_list.php?id='.$this->id.'">';
  1626. $return .= '<span class="opacitymedium">'.$langs->trans("Balance").'</span> : <span class="amount">'.price(price2num($this->solde(1), 'MT'), 0, $langs, 1, -1, -1, $this->currency_code).'</span>';
  1627. }
  1628. if (method_exists($this, 'getLibStatut')) {
  1629. $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
  1630. }
  1631. $return .= '</div>';
  1632. $return .= '</div>';
  1633. $return .= '</div>';
  1634. return $return;
  1635. }
  1636. }
  1637. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
  1638. /**
  1639. * Class to manage bank transaction lines
  1640. */
  1641. class AccountLine extends CommonObjectLine
  1642. {
  1643. /**
  1644. * @var string Error code (or message)
  1645. */
  1646. public $error = '';
  1647. /**
  1648. * @var DoliDB Database handler.
  1649. */
  1650. public $db;
  1651. /**
  1652. * @var string ID to identify managed object
  1653. */
  1654. public $element = 'bank';
  1655. /**
  1656. * @var string Name of table without prefix where object is stored
  1657. */
  1658. public $table_element = 'bank';
  1659. /**
  1660. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  1661. */
  1662. public $picto = 'accountline';
  1663. /**
  1664. * @var int ID
  1665. */
  1666. public $id;
  1667. /**
  1668. * @var string Ref
  1669. */
  1670. public $ref;
  1671. /**
  1672. * Date creation record (datec)
  1673. *
  1674. * @var integer
  1675. */
  1676. public $datec;
  1677. /**
  1678. * Date (dateo)
  1679. *
  1680. * @var integer
  1681. */
  1682. public $dateo;
  1683. /**
  1684. * Date value (datev)
  1685. *
  1686. * @var integer
  1687. */
  1688. public $datev;
  1689. public $amount; /* Amount of payment in the bank account currency */
  1690. public $amount_main_currency; /* Amount in the currency of company if bank account use another currency */
  1691. /**
  1692. * @var int ID
  1693. */
  1694. public $fk_user_author;
  1695. /**
  1696. * @var int ID
  1697. */
  1698. public $fk_user_rappro;
  1699. /**
  1700. * @var int ID
  1701. */
  1702. public $fk_type;
  1703. /**
  1704. * @var int ID of cheque receipt
  1705. */
  1706. public $fk_bordereau;
  1707. /**
  1708. * @var int ID of bank account
  1709. */
  1710. public $fk_account;
  1711. /**
  1712. * @var string Ref of bank account
  1713. */
  1714. public $bank_account_ref;
  1715. /**
  1716. * @var string Label of bank account
  1717. */
  1718. public $bank_account_label;
  1719. /**
  1720. * @var string Bank account numero
  1721. */
  1722. public $numero_compte;
  1723. /**
  1724. * @var string Name of check issuer
  1725. */
  1726. public $emetteur;
  1727. public $rappro; // Is it conciliated
  1728. public $num_releve; // If conciliated, what is bank statement
  1729. public $num_chq; // Num of cheque
  1730. public $bank_chq; // Bank of cheque
  1731. /**
  1732. * @var string bank transaction lines label
  1733. */
  1734. public $label;
  1735. public $note;
  1736. /**
  1737. * Constructor
  1738. *
  1739. * @param DoliDB $db Database handler
  1740. */
  1741. public function __construct(DoliDB $db)
  1742. {
  1743. $this->db = $db;
  1744. }
  1745. /**
  1746. * Load into memory content of a bank transaction line
  1747. *
  1748. * @param int $rowid Id of bank transaction to load
  1749. * @param string $ref Ref of bank transaction to load
  1750. * @param string $num External num to load (ex: num of transaction for paypal fee)
  1751. * @return int <0 if KO, 0 if OK but not found, >0 if OK and found
  1752. */
  1753. public function fetch($rowid, $ref = '', $num = '')
  1754. {
  1755. global $conf;
  1756. // Check parameters
  1757. if (empty($rowid) && empty($ref) && empty($num)) {
  1758. return -1;
  1759. }
  1760. $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
  1761. $sql .= " b.fk_user_author, b.fk_user_rappro,";
  1762. $sql .= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
  1763. $sql .= " b.fk_bordereau, b.banque, b.emetteur,";
  1764. $sql .= " ba.ref as bank_account_ref, ba.label as bank_account_label";
  1765. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b,";
  1766. $sql .= " ".MAIN_DB_PREFIX."bank_account as ba";
  1767. $sql .= " WHERE b.fk_account = ba.rowid";
  1768. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  1769. if ($num) {
  1770. $sql .= " AND b.num_chq='".$this->db->escape($num)."'";
  1771. } elseif ($ref) {
  1772. $sql .= " AND b.rowid='".$this->db->escape($ref)."'";
  1773. } else {
  1774. $sql .= " AND b.rowid = ".((int) $rowid);
  1775. }
  1776. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  1777. $result = $this->db->query($sql);
  1778. if ($result) {
  1779. $ret = 0;
  1780. $obj = $this->db->fetch_object($result);
  1781. if ($obj) {
  1782. $this->id = $obj->rowid;
  1783. $this->rowid = $obj->rowid;
  1784. $this->ref = $obj->rowid;
  1785. $this->datec = $obj->datec;
  1786. $this->datev = $obj->datev;
  1787. $this->dateo = $obj->dateo;
  1788. $this->amount = $obj->amount;
  1789. $this->label = $obj->label;
  1790. $this->note = $obj->note;
  1791. $this->fk_user_author = $obj->fk_user_author;
  1792. $this->fk_user_rappro = $obj->fk_user_rappro;
  1793. $this->fk_type = $obj->fk_type; // Type of transaction
  1794. $this->rappro = $obj->rappro;
  1795. $this->num_releve = $obj->num_releve;
  1796. $this->num_chq = $obj->num_chq;
  1797. $this->bank_chq = $obj->banque;
  1798. $this->fk_bordereau = $obj->fk_bordereau;
  1799. $this->fk_account = $obj->fk_account;
  1800. $this->bank_account_ref = $obj->bank_account_ref;
  1801. $this->bank_account_label = $obj->bank_account_label;
  1802. // Retrieve all extrafield
  1803. // fetch optionals attributes and labels
  1804. $this->fetch_optionals();
  1805. $ret = 1;
  1806. }
  1807. $this->db->free($result);
  1808. return $ret;
  1809. } else {
  1810. return -1;
  1811. }
  1812. }
  1813. /**
  1814. * Inserts a transaction to a bank account
  1815. *
  1816. * @return int <0 if KO, rowid of the line if OK
  1817. */
  1818. public function insert()
  1819. {
  1820. $error = 0;
  1821. $this->db->begin();
  1822. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
  1823. $sql .= "datec";
  1824. $sql .= ", dateo";
  1825. $sql .= ", datev";
  1826. $sql .= ", label";
  1827. $sql .= ", amount";
  1828. $sql .= ", amount_main_currency";
  1829. $sql .= ", fk_user_author";
  1830. $sql .= ", num_chq";
  1831. $sql .= ", fk_account";
  1832. $sql .= ", fk_type";
  1833. $sql .= ", emetteur,banque";
  1834. $sql .= ", rappro";
  1835. $sql .= ", numero_compte";
  1836. $sql .= ", num_releve";
  1837. $sql .= ") VALUES (";
  1838. $sql .= "'".$this->db->idate($this->datec)."'";
  1839. $sql .= ", '".$this->db->idate($this->dateo)."'";
  1840. $sql .= ", '".$this->db->idate($this->datev)."'";
  1841. $sql .= ", '".$this->db->escape($this->label)."'";
  1842. $sql .= ", ".price2num($this->amount);
  1843. $sql .= ", ".(empty($this->amount_main_currency) ? "NULL" : price2num($this->amount_main_currency));
  1844. $sql .= ", ".($this->fk_user_author > 0 ? ((int) $this->fk_user_author) : "null");
  1845. $sql .= ", ".($this->num_chq ? "'".$this->db->escape($this->num_chq)."'" : "null");
  1846. $sql .= ", '".$this->db->escape($this->fk_account)."'";
  1847. $sql .= ", '".$this->db->escape($this->fk_type)."'";
  1848. $sql .= ", ".($this->emetteur ? "'".$this->db->escape($this->emetteur)."'" : "null");
  1849. $sql .= ", ".($this->bank_chq ? "'".$this->db->escape($this->bank_chq)."'" : "null");
  1850. $sql .= ", ".(int) $this->rappro;
  1851. $sql .= ", ".($this->numero_compte ? "'".$this->db->escape($this->numero_compte)."'" : "''");
  1852. $sql .= ", ".($this->num_releve ? "'".$this->db->escape($this->num_releve)."'" : "null");
  1853. $sql .= ")";
  1854. dol_syslog(get_class($this)."::insert", LOG_DEBUG);
  1855. $resql = $this->db->query($sql);
  1856. if ($resql) {
  1857. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'bank');
  1858. // Actions on extra fields (by external module or standard code)
  1859. $result = $this->insertExtraFields();
  1860. if ($result < 0) {
  1861. $error++;
  1862. }
  1863. } else {
  1864. $error++;
  1865. $this->error = $this->db->lasterror();
  1866. dol_print_error($this->db);
  1867. }
  1868. if (!$error) {
  1869. $this->db->commit();
  1870. return $this->id;
  1871. } else {
  1872. $this->db->rollback();
  1873. return -1 * $error;
  1874. }
  1875. }
  1876. /**
  1877. * Delete bank transaction record
  1878. *
  1879. * @param User $user User object that delete
  1880. * @return int <0 if KO, >0 if OK
  1881. */
  1882. public function delete(User $user = null)
  1883. {
  1884. global $conf;
  1885. $nbko = 0;
  1886. if ($this->rappro) {
  1887. // Protection to avoid any delete of consolidated lines
  1888. $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
  1889. return -1;
  1890. }
  1891. $this->db->begin();
  1892. // Protection to avoid any delete of accounted lines. Protection on by default
  1893. if (empty($conf->global->BANK_ALLOW_TRANSACTION_DELETION_EVEN_IF_IN_ACCOUNTING)) {
  1894. $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
  1895. $resql = $this->db->query($sql);
  1896. if ($resql) {
  1897. $obj = $this->db->fetch_object($resql);
  1898. if ($obj && $obj->nb) {
  1899. $this->error = 'ErrorRecordAlreadyInAccountingDeletionNotPossible';
  1900. $this->db->rollback();
  1901. return -1;
  1902. }
  1903. } else {
  1904. $this->error = $this->db->lasterror();
  1905. $this->db->rollback();
  1906. return -1;
  1907. }
  1908. }
  1909. // Delete urls
  1910. $result = $this->delete_urls($user);
  1911. if ($result < 0) {
  1912. $nbko++;
  1913. }
  1914. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".(int) $this->rowid;
  1915. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1916. $result = $this->db->query($sql);
  1917. if (!$result) {
  1918. $nbko++;
  1919. }
  1920. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_extrafields WHERE fk_object=".(int) $this->rowid;
  1921. $result = $this->db->query($sql);
  1922. if (!$result) {
  1923. $nbko++;
  1924. }
  1925. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".(int) $this->rowid;
  1926. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1927. $result = $this->db->query($sql);
  1928. if (!$result) {
  1929. $nbko++;
  1930. }
  1931. if (!$nbko) {
  1932. $this->db->commit();
  1933. return 1;
  1934. } else {
  1935. $this->db->rollback();
  1936. return -$nbko;
  1937. }
  1938. }
  1939. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1940. /**
  1941. * Delete bank line records
  1942. *
  1943. * @param User $user User object that delete
  1944. * @return int <0 if KO, >0 if OK
  1945. */
  1946. public function delete_urls(User $user = null)
  1947. {
  1948. // phpcs:enable
  1949. $nbko = 0;
  1950. if ($this->rappro) {
  1951. // Protection to avoid any delete of consolidated lines
  1952. $this->error = "ErrorDeleteNotPossibleLineIsConsolidated";
  1953. return -1;
  1954. }
  1955. $this->db->begin();
  1956. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".(int) $this->rowid;
  1957. dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
  1958. $result = $this->db->query($sql);
  1959. if (!$result) {
  1960. $nbko++;
  1961. }
  1962. if (!$nbko) {
  1963. $this->db->commit();
  1964. return 1;
  1965. } else {
  1966. $this->db->rollback();
  1967. return -$nbko;
  1968. }
  1969. }
  1970. /**
  1971. * Update bank account record in database
  1972. *
  1973. * @param User $user Object user making update
  1974. * @param int $notrigger 0=Disable all triggers
  1975. * @return int <0 if KO, >0 if OK
  1976. */
  1977. public function update(User $user, $notrigger = 0)
  1978. {
  1979. $this->db->begin();
  1980. $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
  1981. $sql .= " amount = ".price2num($this->amount).",";
  1982. $sql .= " datev='".$this->db->idate($this->datev)."',";
  1983. $sql .= " dateo='".$this->db->idate($this->dateo)."'";
  1984. $sql .= " WHERE rowid = ".((int) $this->rowid);
  1985. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  1986. $resql = $this->db->query($sql);
  1987. if ($resql) {
  1988. $this->db->commit();
  1989. return 1;
  1990. } else {
  1991. $this->db->rollback();
  1992. $this->error = $this->db->error();
  1993. return -1;
  1994. }
  1995. }
  1996. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1997. /**
  1998. * Update conciliation field
  1999. *
  2000. * @param User $user Objet user making update
  2001. * @param int $cat Category id
  2002. * @param int $conciliated 1=Set transaction to conciliated, 0=Keep transaction non conciliated
  2003. * @return int <0 if KO, >0 if OK
  2004. */
  2005. public function update_conciliation(User $user, $cat, $conciliated = 1)
  2006. {
  2007. // phpcs:enable
  2008. global $conf, $langs;
  2009. $this->db->begin();
  2010. // Check statement field
  2011. if (!empty($conf->global->BANK_STATEMENT_REGEX_RULE)) {
  2012. if (!preg_match('/'.$conf->global->BANK_STATEMENT_REGEX_RULE.'/', $this->num_releve)) {
  2013. $this->errors[] = $langs->trans("ErrorBankStatementNameMustFollowRegex", $conf->global->BANK_STATEMENT_REGEX_RULE);
  2014. return -1;
  2015. }
  2016. }
  2017. $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
  2018. $sql .= " rappro = ".((int) $conciliated);
  2019. $sql .= ", num_releve = '".$this->db->escape($this->num_releve)."'";
  2020. if ($conciliated) {
  2021. $sql .= ", fk_user_rappro = ".$user->id;
  2022. }
  2023. $sql .= " WHERE rowid = ".((int) $this->id);
  2024. dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
  2025. $resql = $this->db->query($sql);
  2026. if ($resql) {
  2027. if (!empty($cat)) {
  2028. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
  2029. $sql .= "lineid";
  2030. $sql .= ", fk_categ";
  2031. $sql .= ") VALUES (";
  2032. $sql .= $this->id;
  2033. $sql .= ", ".((int) $cat);
  2034. $sql .= ")";
  2035. dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
  2036. $this->db->query($sql);
  2037. // No error check. Can fail if category already affected
  2038. }
  2039. $this->rappro = 1;
  2040. $this->db->commit();
  2041. return 1;
  2042. } else {
  2043. $this->db->rollback();
  2044. return -1;
  2045. }
  2046. }
  2047. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2048. /**
  2049. * Increase/decrease value date of a rowid
  2050. *
  2051. * @param int $rowid Id of line
  2052. * @param int $sign 1 or -1
  2053. * @return int >0 if OK, 0 if KO
  2054. */
  2055. public function datev_change($rowid, $sign = 1)
  2056. {
  2057. // phpcs:enable
  2058. $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
  2059. $resql = $this->db->query($sql);
  2060. if ($resql) {
  2061. $obj = $this->db->fetch_object($resql);
  2062. $newdate = $this->db->jdate($obj->datev) + (3600 * 24 * $sign);
  2063. $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
  2064. $sql .= " datev = '".$this->db->idate($newdate)."'";
  2065. $sql .= " WHERE rowid = ".((int) $rowid);
  2066. $result = $this->db->query($sql);
  2067. if ($result) {
  2068. if ($this->db->affected_rows($result)) {
  2069. return 1;
  2070. }
  2071. } else {
  2072. dol_print_error($this->db);
  2073. return 0;
  2074. }
  2075. } else {
  2076. dol_print_error($this->db);
  2077. }
  2078. return 0;
  2079. }
  2080. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2081. /**
  2082. * Increase value date of a rowid
  2083. *
  2084. * @param int $id Id of line to change
  2085. * @return int >0 if OK, 0 if KO
  2086. */
  2087. public function datev_next($id)
  2088. {
  2089. // phpcs:enable
  2090. return $this->datev_change($id, 1);
  2091. }
  2092. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2093. /**
  2094. * Decrease value date of a rowid
  2095. *
  2096. * @param int $id Id of line to change
  2097. * @return int >0 if OK, 0 if KO
  2098. */
  2099. public function datev_previous($id)
  2100. {
  2101. // phpcs:enable
  2102. return $this->datev_change($id, -1);
  2103. }
  2104. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2105. /**
  2106. * Increase/decrease operation date of a rowid
  2107. *
  2108. * @param int $rowid Id of line
  2109. * @param int $sign 1 or -1
  2110. * @return int >0 if OK, 0 if KO
  2111. */
  2112. public function dateo_change($rowid, $sign = 1)
  2113. {
  2114. // phpcs:enable
  2115. $sql = "SELECT dateo FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".((int) $rowid);
  2116. $resql = $this->db->query($sql);
  2117. if ($resql) {
  2118. $obj = $this->db->fetch_object($resql);
  2119. $newdate = $this->db->jdate($obj->dateo) + (3600 * 24 * $sign);
  2120. $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
  2121. $sql .= " dateo = '".$this->db->idate($newdate)."'";
  2122. $sql .= " WHERE rowid = ".((int) $rowid);
  2123. $result = $this->db->query($sql);
  2124. if ($result) {
  2125. if ($this->db->affected_rows($result)) {
  2126. return 1;
  2127. }
  2128. } else {
  2129. dol_print_error($this->db);
  2130. return 0;
  2131. }
  2132. } else {
  2133. dol_print_error($this->db);
  2134. }
  2135. return 0;
  2136. }
  2137. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2138. /**
  2139. * Increase operation date of a rowid
  2140. *
  2141. * @param int $id Id of line to change
  2142. * @return int >0 if OK, 0 if KO
  2143. */
  2144. public function dateo_next($id)
  2145. {
  2146. // phpcs:enable
  2147. return $this->dateo_change($id, 1);
  2148. }
  2149. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2150. /**
  2151. * Decrease operation date of a rowid
  2152. *
  2153. * @param int $id Id of line to change
  2154. * @return int >0 if OK, 0 if KO
  2155. */
  2156. public function dateo_previous($id)
  2157. {
  2158. // phpcs:enable
  2159. return $this->dateo_change($id, -1);
  2160. }
  2161. /**
  2162. * Load miscellaneous information for tab "Info"
  2163. *
  2164. * @param int $id Id of object to load
  2165. * @return void
  2166. */
  2167. public function info($id)
  2168. {
  2169. $sql = 'SELECT b.rowid, b.datec, b.tms as datem,';
  2170. $sql .= ' b.fk_user_author, b.fk_user_rappro';
  2171. $sql .= ' FROM '.MAIN_DB_PREFIX.'bank as b';
  2172. $sql .= ' WHERE b.rowid = '.((int) $id);
  2173. $result = $this->db->query($sql);
  2174. if ($result) {
  2175. if ($this->db->num_rows($result)) {
  2176. $obj = $this->db->fetch_object($result);
  2177. $this->id = $obj->rowid;
  2178. if ($obj->fk_user_author) {
  2179. $cuser = new User($this->db);
  2180. $cuser->fetch($obj->fk_user_author);
  2181. $this->user_creation = $cuser;
  2182. }
  2183. if ($obj->fk_user_rappro) {
  2184. $ruser = new User($this->db);
  2185. $ruser->fetch($obj->fk_user_rappro);
  2186. $this->user_rappro = $ruser;
  2187. }
  2188. $this->date_creation = $this->db->jdate($obj->datec);
  2189. $this->date_modification = $this->db->jdate($obj->datem);
  2190. //$this->date_rappro = $obj->daterappro; // Not yet managed
  2191. }
  2192. $this->db->free($result);
  2193. } else {
  2194. dol_print_error($this->db);
  2195. }
  2196. }
  2197. /**
  2198. * Return clickable name (with picto eventually)
  2199. *
  2200. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  2201. * @param int $maxlen Longueur max libelle
  2202. * @param string $option Option ('', 'showall', 'showconciliated', 'showconciliatedandaccounted'). Options may be slow.
  2203. * @param int $notooltip 1=Disable tooltip
  2204. * @return string Chaine avec URL
  2205. */
  2206. public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0)
  2207. {
  2208. global $langs;
  2209. $result = '';
  2210. $label = img_picto('', $this->picto).' <u>'.$langs->trans("BankTransactionLine").'</u>:<br>';
  2211. $label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
  2212. $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">';
  2213. $linkend = '</a>';
  2214. $result .= $linkstart;
  2215. if ($withpicto) {
  2216. $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);
  2217. }
  2218. if ($withpicto != 2) {
  2219. $result .= ($this->ref ? $this->ref : $this->id);
  2220. }
  2221. $result .= $linkend;
  2222. if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
  2223. $result .= ' <span class="opacitymedium">(';
  2224. }
  2225. if ($option == 'showall') {
  2226. $result .= $langs->trans("BankAccount").': ';
  2227. $accountstatic = new Account($this->db);
  2228. $accountstatic->id = $this->fk_account;
  2229. $accountstatic->ref = $this->bank_account_ref;
  2230. $accountstatic->label = $this->bank_account_label;
  2231. $result .= $accountstatic->getNomUrl(0).', ';
  2232. }
  2233. if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
  2234. $result .= $langs->trans("BankLineConciliated").': ';
  2235. $result .= yn($this->rappro);
  2236. }
  2237. if ($option == 'showall' || $option == 'showconciliatedandaccounted') {
  2238. $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE doc_type = 'bank' AND fk_doc = ".((int) $this->id);
  2239. $resql = $this->db->query($sql);
  2240. if ($resql) {
  2241. $obj = $this->db->fetch_object($resql);
  2242. if ($obj && $obj->nb) {
  2243. $result .= ' - '.$langs->trans("Accounted").': '.yn(1);
  2244. } else {
  2245. $result .= ' - '.$langs->trans("Accounted").': '.yn(0);
  2246. }
  2247. }
  2248. }
  2249. if ($option == 'showall' || $option == 'showconciliated' || $option == 'showconciliatedandaccounted') {
  2250. $result .= ')</span>';
  2251. }
  2252. return $result;
  2253. }
  2254. /**
  2255. * Return label of status (activity, closed)
  2256. *
  2257. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long
  2258. * @return string Libelle
  2259. */
  2260. public function getLibStatut($mode = 0)
  2261. {
  2262. return $this->LibStatut($this->status, $mode);
  2263. }
  2264. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2265. /**
  2266. * Renvoi le libelle d'un statut donne
  2267. *
  2268. * @param int $status Id statut
  2269. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  2270. * @return string Libelle du statut
  2271. */
  2272. public function LibStatut($status, $mode = 0)
  2273. {
  2274. // phpcs:enable
  2275. global $langs;
  2276. //$langs->load('companies');
  2277. /*
  2278. if ($mode == 0)
  2279. {
  2280. if ($status==0) return $langs->trans("ActivityCeased");
  2281. if ($status==1) return $langs->trans("InActivity");
  2282. }
  2283. if ($mode == 1)
  2284. {
  2285. if ($status==0) return $langs->trans("ActivityCeased");
  2286. if ($status==1) return $langs->trans("InActivity");
  2287. }
  2288. if ($mode == 2)
  2289. {
  2290. if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
  2291. if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
  2292. }
  2293. if ($mode == 3)
  2294. {
  2295. if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
  2296. if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
  2297. }
  2298. if ($mode == 4)
  2299. {
  2300. if ($status==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
  2301. if ($status==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
  2302. }
  2303. if ($mode == 5)
  2304. {
  2305. if ($status==0) return $langs->trans("ActivityCeased").' '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
  2306. if ($status==1) return $langs->trans("InActivity").' '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
  2307. }*/
  2308. }
  2309. /**
  2310. * Return if a bank line was dispatched into bookkeeping
  2311. *
  2312. * @return int <0 if KO, 0=no, 1=yes
  2313. */
  2314. public function getVentilExportCompta()
  2315. {
  2316. $alreadydispatched = 0;
  2317. $type = 'bank';
  2318. $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);
  2319. $resql = $this->db->query($sql);
  2320. if ($resql) {
  2321. $obj = $this->db->fetch_object($resql);
  2322. if ($obj) {
  2323. $alreadydispatched = $obj->nb;
  2324. }
  2325. } else {
  2326. $this->error = $this->db->lasterror();
  2327. return -1;
  2328. }
  2329. if ($alreadydispatched) {
  2330. return 1;
  2331. }
  2332. return 0;
  2333. }
  2334. }