account.class.php 62 KB

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