account.class.php 74 KB

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