account.class.php 75 KB

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