account.class.php 59 KB

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