account.class.php 74 KB

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