account.class.php 74 KB

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