account.class.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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. * Copytight (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/compta/bank/class/account.class.php
  24. * \ingroup banque
  25. * \brief File of class to manage bank accounts
  26. */
  27. require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
  28. /**
  29. * Class to manage bank accounts
  30. */
  31. class Account extends CommonObject
  32. {
  33. public $element='bank_account';
  34. public $table_element='bank_account';
  35. var $rowid; // deprecated
  36. var $id;
  37. var $ref;
  38. var $label;
  39. //! 1=Compte courant/check/carte, 2=Compte liquide, 0=Compte épargne
  40. var $courant;
  41. var $type; // same as courant
  42. //! Name
  43. var $bank;
  44. var $clos;
  45. var $rappro=1; // If bank need to be conciliated
  46. var $url;
  47. //! BBAN field for French Code banque
  48. var $code_banque;
  49. //! BBAN field for French Code guichet
  50. var $code_guichet;
  51. //! BBAN main account number
  52. var $number;
  53. //! BBAN field for French Cle de controle
  54. var $cle_rib;
  55. //! BIC/SWIFT number
  56. var $bic;
  57. //! IBAN number (International Bank Account Number)
  58. var $iban_prefix;
  59. var $proprio;
  60. var $owner_address;
  61. var $state_id;
  62. var $state_code;
  63. var $state;
  64. var $country_id;
  65. var $country_code;
  66. var $country;
  67. var $type_lib=array();
  68. var $account_number;
  69. var $accountancy_journal;
  70. var $currency_code;
  71. var $min_allowed;
  72. var $min_desired;
  73. var $comment;
  74. /**
  75. * Constructor
  76. *
  77. * @param DoliDB $db Database handler
  78. */
  79. function __construct($db)
  80. {
  81. global $langs;
  82. $this->db = $db;
  83. $this->clos = 0;
  84. $this->solde = 0;
  85. $this->type_lib[0]=$langs->trans("BankType0");
  86. $this->type_lib[1]=$langs->trans("BankType1");
  87. $this->type_lib[2]=$langs->trans("BankType2");
  88. $this->status[0]=$langs->trans("StatusAccountOpened");
  89. $this->status[1]=$langs->trans("StatusAccountClosed");
  90. return 1;
  91. }
  92. /**
  93. * Return if a bank account need to be conciliated
  94. *
  95. * @return int 1 if need to be concialiated, < 0 otherwise.
  96. */
  97. function canBeConciliated()
  98. {
  99. if (empty($this->rappro)) return -1;
  100. if ($this->courant == 2) return -2;
  101. if ($this->clos) return -3;
  102. return 1;
  103. }
  104. /**
  105. * Add a link between bank line record and its source
  106. *
  107. * @param int $line_id Id ecriture bancaire
  108. * @param int $url_id Id parametre url
  109. * @param string $url Url
  110. * @param string $label Link label
  111. * @param string $type Type of link ('payment', 'company', 'member', ...)
  112. * @return int <0 if KO, id line if OK
  113. */
  114. function add_url_line($line_id, $url_id, $url, $label, $type)
  115. {
  116. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
  117. $sql.= "fk_bank";
  118. $sql.= ", url_id";
  119. $sql.= ", url";
  120. $sql.= ", label";
  121. $sql.= ", type";
  122. $sql.= ") VALUES (";
  123. $sql.= "'".$line_id."'";
  124. $sql.= ", '".$url_id."'";
  125. $sql.= ", '".$url."'";
  126. $sql.= ", '".$this->db->escape($label)."'";
  127. $sql.= ", '".$type."'";
  128. $sql.= ")";
  129. dol_syslog(get_class($this)."::add_url_line", LOG_DEBUG);
  130. if ($this->db->query($sql))
  131. {
  132. $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
  133. return $rowid;
  134. }
  135. else
  136. {
  137. $this->error=$this->db->lasterror();
  138. return -1;
  139. }
  140. }
  141. /**
  142. * TODO Move this into AccountLine
  143. * Return array with links from llx_bank_url
  144. *
  145. * @param int $fk_bank To search using bank transaction id
  146. * @param int $url_id To search using link to
  147. * @param string $type To search using type
  148. * @return array Array of links
  149. */
  150. function get_url($fk_bank='', $url_id='', $type='')
  151. {
  152. $lines = array();
  153. // Check parameters
  154. if (! empty($fk_bank) && (! empty($url_id) || ! empty($type)))
  155. {
  156. $this->error="ErrorBadParameter";
  157. return -1;
  158. }
  159. $sql = "SELECT fk_bank, url_id, url, label, type";
  160. $sql.= " FROM ".MAIN_DB_PREFIX."bank_url";
  161. if ($fk_bank > 0) {
  162. $sql.= " WHERE fk_bank = ".$fk_bank;
  163. }
  164. else { $sql.= " WHERE url_id = ".$url_id." AND type = '".$type."'";
  165. }
  166. $sql.= " ORDER BY type, label";
  167. dol_syslog(get_class($this)."::get_url", LOG_DEBUG);
  168. $result = $this->db->query($sql);
  169. if ($result)
  170. {
  171. $i = 0;
  172. $num = $this->db->num_rows($result);
  173. while ($i < $num)
  174. {
  175. $obj = $this->db->fetch_object($result);
  176. // Anciens liens (pour compatibilite)
  177. $lines[$i][0] = $obj->url;
  178. $lines[$i][1] = $obj->url_id;
  179. $lines[$i][2] = $obj->label;
  180. $lines[$i][3] = $obj->type;
  181. // Nouveaux liens
  182. $lines[$i]['url'] = $obj->url;
  183. $lines[$i]['url_id'] = $obj->url_id;
  184. $lines[$i]['label'] = $obj->label;
  185. $lines[$i]['type'] = $obj->type;
  186. $lines[$i]['fk_bank'] = $obj->fk_bank;
  187. $i++;
  188. }
  189. }
  190. else dol_print_error($this->db);
  191. return $lines;
  192. }
  193. /**
  194. * Add an entry into table ".MAIN_DB_PREFIX."bank
  195. *
  196. * @param timsestmap $date Date operation
  197. * @param string $oper 1,2,3,4... (deprecated) or TYP,VIR,PRE,LIQ,VAD,CB,CHQ...
  198. * @param string $label Descripton
  199. * @param float $amount Amount
  200. * @param string $num_chq Numero cheque ou virement
  201. * @param string $categorie Categorie optionnelle
  202. * @param User $user User that create
  203. * @param string $emetteur Name of cheque writer
  204. * @param string $banque Bank of cheque writer
  205. * @return int Rowid of added entry, <0 if KO
  206. */
  207. function addline($date, $oper, $label, $amount, $num_chq, $categorie, $user, $emetteur='',$banque='')
  208. {
  209. // Clean parameters
  210. $emetteur=trim($emetteur);
  211. $banque=trim($banque);
  212. $now=dol_now();
  213. if (is_numeric($oper)) // Clean oper to have a code instead of a rowid
  214. {
  215. $sql ="SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
  216. $sql.=" WHERE id=".$oper;
  217. $resql=$this->db->query($sql);
  218. if ($resql)
  219. {
  220. $obj=$this->db->fetch_object($resql);
  221. $oper=$obj->code;
  222. }
  223. else
  224. {
  225. dol_print_error($this->db,'Failed to get payment type code');
  226. return -1;
  227. }
  228. }
  229. // Check parameters
  230. if (! $oper)
  231. {
  232. $this->error="oper not defined";
  233. return -1;
  234. }
  235. if (! $this->rowid)
  236. {
  237. $this->error="this->rowid not defined";
  238. return -2;
  239. }
  240. if ($this->courant == 2 && $oper != 'LIQ')
  241. {
  242. $this->error="ErrorCashAccountAcceptsOnlyCashMoney";
  243. return -3;
  244. }
  245. $this->db->begin();
  246. $datev = $date;
  247. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
  248. $sql.= "datec";
  249. $sql.= ", dateo";
  250. $sql.= ", datev";
  251. $sql.= ", label";
  252. $sql.= ", amount";
  253. $sql.= ", fk_user_author";
  254. $sql.= ", num_chq";
  255. $sql.= ", fk_account";
  256. $sql.= ", fk_type";
  257. $sql.= ",emetteur,banque";
  258. $sql.= ") VALUES (";
  259. $sql.= "'".$this->db->idate($now)."'";
  260. $sql.= ", '".$this->db->idate($date)."'";
  261. $sql.= ", '".$this->db->idate($datev)."'";
  262. $sql.= ", '".$this->db->escape($label)."'";
  263. $sql.= ", ".price2num($amount);
  264. $sql.= ", '".$user->id."'";
  265. $sql.= ", ".($num_chq?"'".$num_chq."'":"null");
  266. $sql.= ", '".$this->rowid."'";
  267. $sql.= ", '".$oper."'";
  268. $sql.= ", ".($emetteur?"'".$this->db->escape($emetteur)."'":"null");
  269. $sql.= ", ".($banque?"'".$this->db->escape($banque)."'":"null");
  270. $sql.= ")";
  271. dol_syslog(get_class($this)."::addline", LOG_DEBUG);
  272. if ($this->db->query($sql))
  273. {
  274. $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank");
  275. if ($categorie)
  276. {
  277. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
  278. $sql.= "lineid";
  279. $sql.= ", fk_categ";
  280. $sql.= ") VALUES (";
  281. $sql.= "'".$rowid."'";
  282. $sql.= ", '".$categorie."'";
  283. $sql.= ")";
  284. $result = $this->db->query($sql);
  285. if (! $result)
  286. {
  287. $this->db->rollback();
  288. $this->error=$this->db->error();
  289. return -3;
  290. }
  291. }
  292. $this->db->commit();
  293. return $rowid;
  294. }
  295. else
  296. {
  297. $this->error=$this->db->lasterror();
  298. $this->db->rollback();
  299. return -2;
  300. }
  301. }
  302. /**
  303. * Create bank account into database
  304. *
  305. * @return int < 0 if KO, > 0 if OK
  306. */
  307. function create()
  308. {
  309. global $langs,$conf;
  310. // Clean parameters
  311. if (! $this->min_allowed) $this->min_allowed=0;
  312. if (! $this->min_desired) $this->min_desired=0;
  313. $this->state_id = ($this->state_id?$this->state_id:$this->state_id);
  314. $this->country_id = ($this->country_id?$this->country_id:$this->country_id);
  315. // Check parameters
  316. if (empty($this->country_id))
  317. {
  318. $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Country"));
  319. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  320. return -1;
  321. }
  322. if (empty($this->ref))
  323. {
  324. $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Ref"));
  325. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  326. return -1;
  327. }
  328. if (empty($this->date_solde))
  329. {
  330. $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("DateInitialBalance"));
  331. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  332. return -1;
  333. }
  334. // Chargement librairie pour acces fonction controle RIB
  335. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  336. $now=dol_now();
  337. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
  338. $sql.= "datec";
  339. $sql.= ", ref";
  340. $sql.= ", label";
  341. $sql.= ", entity";
  342. $sql.= ", account_number";
  343. $sql.= ", accountancy_journal";
  344. $sql.= ", currency_code";
  345. $sql.= ", rappro";
  346. $sql.= ", min_allowed";
  347. $sql.= ", min_desired";
  348. $sql.= ", comment";
  349. $sql.= ", state_id";
  350. $sql.= ", fk_pays";
  351. $sql.= ") VALUES (";
  352. $sql.= "'".$this->db->idate($now)."'";
  353. $sql.= ", '".$this->db->escape($this->ref)."'";
  354. $sql.= ", '".$this->db->escape($this->label)."'";
  355. $sql.= ", ".$conf->entity;
  356. $sql.= ", '".$this->db->escape($this->account_number)."'";
  357. $sql.= ", '".$this->db->escape($this->accountancy_journal)."'";
  358. $sql.= ", '".$this->currency_code."'";
  359. $sql.= ", ".$this->rappro;
  360. $sql.= ", ".price2num($this->min_allowed);
  361. $sql.= ", ".price2num($this->min_desired);
  362. $sql.= ", '".$this->db->escape($this->comment)."'";
  363. $sql.= ", ".($this->state_id>0?"'".$this->state_id."'":"null");
  364. $sql.= ", ".$this->country_id;
  365. $sql.= ")";
  366. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  367. $resql=$this->db->query($sql);
  368. if ($resql)
  369. {
  370. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
  371. $result=$this->update();
  372. if ($result > 0)
  373. {
  374. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (";
  375. $sql.= "datec";
  376. $sql.= ", label";
  377. $sql.= ", amount";
  378. $sql.= ", fk_account";
  379. $sql.= ", datev";
  380. $sql.= ", dateo";
  381. $sql.= ", fk_type";
  382. $sql.= ", rappro";
  383. $sql.= ") VALUES (";
  384. $sql.= "'".$this->db->idate($now)."'";
  385. $sql.= ", '(".$langs->trans("InitialBankBalance").")'";
  386. $sql.= ", ".price2num($this->solde);
  387. $sql.= ", '".$this->id."'";
  388. $sql.= ", '".$this->db->idate($this->date_solde)."'";
  389. $sql.= ", '".$this->db->idate($this->date_solde)."'";
  390. $sql.= ", 'SOLD'";
  391. $sql.= ", 0"; // Not conciliated by default
  392. $sql.= ")";
  393. $resql=$this->db->query($sql);
  394. if (! $resql)
  395. {
  396. $this->error=$this->db->lasterror();
  397. return -3;
  398. }
  399. }
  400. return $this->id;
  401. }
  402. else
  403. {
  404. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  405. {
  406. $this->error=$langs->trans("ErrorBankLabelAlreadyExists");
  407. return -1;
  408. }
  409. else {
  410. $this->error=$this->db->error()." sql=".$sql;
  411. return -2;
  412. }
  413. }
  414. }
  415. /**
  416. * Update bank account card
  417. *
  418. * @param User $user Object user making action
  419. * @return int <0 si ko, >0 si ok
  420. */
  421. function update($user='')
  422. {
  423. global $langs,$conf;
  424. // Clean parameters
  425. if (! $this->min_allowed) $this->min_allowed=0;
  426. if (! $this->min_desired) $this->min_desired=0;
  427. $this->state_id = ($this->state_id?$this->state_id:$this->state_id);
  428. $this->country_id = ($this->country_id?$this->country_id:$this->country_id);
  429. // Check parameters
  430. if (empty($this->country_id))
  431. {
  432. $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Country"));
  433. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  434. return -1;
  435. }
  436. if (empty($this->ref))
  437. {
  438. $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->transnoentitiesnoconv("Ref"));
  439. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  440. return -1;
  441. }
  442. if (! $this->label) $this->label = "???";
  443. $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
  444. $sql.= " ref = '".$this->db->escape($this->ref)."'";
  445. $sql.= ",label = '".$this->db->escape($this->label)."'";
  446. $sql.= ",courant = ".$this->courant;
  447. $sql.= ",clos = ".$this->clos;
  448. $sql.= ",rappro = ".$this->rappro;
  449. $sql.= ",url = ".($this->url?"'".$this->url."'":"null");
  450. $sql.= ",account_number = '".$this->account_number."'";
  451. $sql.= ",accountancy_journal = '".$this->accountancy_journal."'";
  452. $sql.= ",currency_code = '".$this->currency_code."'";
  453. $sql.= ",min_allowed = '".price2num($this->min_allowed)."'";
  454. $sql.= ",min_desired = '".price2num($this->min_desired)."'";
  455. $sql.= ",comment = '".$this->db->escape($this->comment)."'";
  456. $sql.= ",state_id = ".($this->state_id>0?"'".$this->state_id."'":"null");
  457. $sql.= ",fk_pays = ".$this->country_id;
  458. $sql.= " WHERE rowid = ".$this->id;
  459. $sql.= " AND entity = ".$conf->entity;
  460. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  461. $result = $this->db->query($sql);
  462. if ($result)
  463. {
  464. return 1;
  465. }
  466. else
  467. {
  468. $this->error=$this->db->lasterror();
  469. dol_print_error($this->db);
  470. return -1;
  471. }
  472. }
  473. /**
  474. * Update BBAN (RIB) account fields
  475. *
  476. * @param User $user Object user making update
  477. * @return int <0 if KO, >0 if OK
  478. */
  479. function update_bban($user='')
  480. {
  481. global $conf,$langs;
  482. // Clean parameters
  483. $this->state_id = ($this->state_id?$this->state_id:$this->state_id);
  484. $this->country_id = ($this->country_id?$this->country_id:$this->country_id);
  485. // Chargement librairie pour acces fonction controle RIB
  486. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  487. dol_syslog(get_class($this)."::update_bban $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban");
  488. // Check parameters
  489. if (! $this->ref)
  490. {
  491. $this->error=$langs->transnoentitiesnoconv("ErrorFieldRequired",$langs->trans("Ref"));
  492. return -2;
  493. }
  494. $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
  495. $sql.= " bank = '".$this->db->escape($this->bank)."'";
  496. $sql.= ",code_banque='".$this->code_banque."'";
  497. $sql.= ",code_guichet='".$this->code_guichet."'";
  498. $sql.= ",number='".$this->number."'";
  499. $sql.= ",cle_rib='".$this->cle_rib."'";
  500. $sql.= ",bic='".$this->bic."'";
  501. $sql.= ",iban_prefix = '".$this->iban."'";
  502. $sql.= ",domiciliation='".$this->db->escape($this->domiciliation)."'";
  503. $sql.= ",proprio = '".$this->db->escape($this->proprio)."'";
  504. $sql.= ",owner_address = '".$this->db->escape($this->owner_address)."'";
  505. $sql.= ",state_id = ".($this->state_id>0?"'".$this->state_id."'":"null");
  506. $sql.= ",fk_pays = ".$this->country_id;
  507. $sql.= " WHERE rowid = ".$this->id;
  508. $sql.= " AND entity = ".$conf->entity;
  509. dol_syslog(get_class($this)."::update_bban", LOG_DEBUG);
  510. $result = $this->db->query($sql);
  511. if ($result)
  512. {
  513. return 1;
  514. }
  515. else
  516. {
  517. $this->error=$this->db->lasterror();
  518. dol_print_error($this->db);
  519. return -1;
  520. }
  521. }
  522. /**
  523. * Load a bank account into memory from database
  524. *
  525. * @param int $id Id of bank account to get
  526. * @param string $ref Ref of bank account to get
  527. * @return int <0 if KO, >0 if OK
  528. */
  529. function fetch($id,$ref='')
  530. {
  531. global $conf;
  532. if (empty($id) && empty($ref))
  533. {
  534. $this->error="ErrorBadParameters";
  535. return -1;
  536. }
  537. $sql = "SELECT ba.rowid, ba.ref, ba.label, ba.bank, ba.number, ba.courant, ba.clos, ba.rappro, ba.url,";
  538. $sql.= " ba.code_banque, ba.code_guichet, ba.cle_rib, ba.bic, ba.iban_prefix as iban,";
  539. $sql.= " ba.domiciliation, ba.proprio, ba.owner_address, ba.state_id, ba.fk_pays as country_id,";
  540. $sql.= " ba.account_number, ba.accountancy_journal, ba.currency_code,";
  541. $sql.= " ba.min_allowed, ba.min_desired, ba.comment,";
  542. $sql.= ' c.code as country_code, c.label as country,';
  543. $sql.= ' d.code_departement as state_code, d.nom as state';
  544. $sql.= " FROM ".MAIN_DB_PREFIX."bank_account as ba";
  545. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON ba.fk_pays = c.rowid';
  546. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON ba.state_id = d.rowid';
  547. $sql.= " WHERE entity IN (".getEntity($this->element, 1).")";
  548. if ($id) $sql.= " AND ba.rowid = ".$id;
  549. if ($ref) $sql.= " AND ba.ref = '".$this->db->escape($ref)."'";
  550. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  551. $result = $this->db->query($sql);
  552. if ($result)
  553. {
  554. if ($this->db->num_rows($result))
  555. {
  556. $obj = $this->db->fetch_object($result);
  557. $this->id = $obj->rowid;
  558. $this->rowid = $obj->rowid; // deprecated
  559. $this->ref = $obj->ref;
  560. $this->label = $obj->label;
  561. $this->type = $obj->courant;
  562. $this->courant = $obj->courant;
  563. $this->bank = $obj->bank;
  564. $this->clos = $obj->clos;
  565. $this->rappro = $obj->rappro;
  566. $this->url = $obj->url;
  567. $this->code_banque = $obj->code_banque;
  568. $this->code_guichet = $obj->code_guichet;
  569. $this->number = $obj->number;
  570. $this->cle_rib = $obj->cle_rib;
  571. $this->bic = $obj->bic;
  572. $this->iban = $obj->iban;
  573. $this->iban_prefix = $obj->iban; // deprecated
  574. $this->domiciliation = $obj->domiciliation;
  575. $this->proprio = $obj->proprio;
  576. $this->owner_address = $obj->owner_address;
  577. $this->state_id = $obj->state_id;
  578. $this->state_code = $obj->state_code;
  579. $this->state = $obj->state;
  580. $this->country_id = $obj->country_id;
  581. $this->country_code = $obj->country_code;
  582. $this->country = $obj->country;
  583. $this->account_number = $obj->account_number;
  584. $this->accountancy_journal = $obj->accountancy_journal;
  585. $this->currency_code = $obj->currency_code;
  586. $this->account_currency_code = $obj->currency_code;
  587. $this->min_allowed = $obj->min_allowed;
  588. $this->min_desired = $obj->min_desired;
  589. $this->comment = $obj->comment;
  590. return 1;
  591. }
  592. else
  593. {
  594. return 0;
  595. }
  596. $this->db->free($result);
  597. }
  598. else
  599. {
  600. dol_print_error($this->db);
  601. return -1;
  602. }
  603. }
  604. /**
  605. * Delete bank account from database
  606. *
  607. * @return int <0 if KO, >0 if OK
  608. */
  609. function delete()
  610. {
  611. global $conf;
  612. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account";
  613. $sql.= " WHERE rowid = ".$this->rowid;
  614. $sql.= " AND entity = ".$conf->entity;
  615. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  616. $result = $this->db->query($sql);
  617. if ($result) {
  618. return 1;
  619. }
  620. else {
  621. dol_print_error($this->db);
  622. return -1;
  623. }
  624. }
  625. /**
  626. * Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
  627. *
  628. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long
  629. * @return string Libelle
  630. */
  631. function getLibStatut($mode=0)
  632. {
  633. return $this->LibStatut($this->clos,$mode);
  634. }
  635. /**
  636. * Renvoi le libelle d'un statut donne
  637. *
  638. * @param int $statut Id statut
  639. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  640. * @return string Libelle du statut
  641. */
  642. function LibStatut($statut,$mode=0)
  643. {
  644. global $langs;
  645. $langs->load('banks');
  646. if ($mode == 0)
  647. {
  648. if ($statut==0) return $langs->trans("StatusAccountOpened");
  649. if ($statut==1) return $langs->trans("StatusAccountClosed");
  650. }
  651. if ($mode == 1)
  652. {
  653. if ($statut==0) return $langs->trans("StatusAccountOpened");
  654. if ($statut==1) return $langs->trans("StatusAccountClosed");
  655. }
  656. if ($mode == 2)
  657. {
  658. if ($statut==0) return img_picto($langs->trans("StatusAccountOpened"),'statut4').' '.$langs->trans("StatusAccountOpened");
  659. if ($statut==1) return img_picto($langs->trans("StatusAccountClosed"),'statut5').' '.$langs->trans("StatusAccountClosed");
  660. }
  661. if ($mode == 3)
  662. {
  663. if ($statut==0) return img_picto($langs->trans("StatusAccountOpened"),'statut4');
  664. if ($statut==1) return img_picto($langs->trans("StatusAccountClosed"),'statut5');
  665. }
  666. if ($mode == 4)
  667. {
  668. if ($statut==0) return img_picto($langs->trans("StatusAccountOpened"),'statut4').' '.$langs->trans("StatusAccountOpened");
  669. if ($statut==1) return img_picto($langs->trans("StatusAccountClosed"),'statut5').' '.$langs->trans("StatusAccountClosed");
  670. }
  671. if ($mode == 5)
  672. {
  673. if ($statut==0) return $langs->trans("StatusAccountOpened").' '.img_picto($langs->trans("StatusAccountOpened"),'statut4');
  674. if ($statut==1) return $langs->trans("StatusAccountClosed").' '.img_picto($langs->trans("StatusAccountClosed"),'statut5');
  675. }
  676. }
  677. /**
  678. * Renvoi si un compte peut etre supprimer ou non (sans mouvements)
  679. *
  680. * @return boolean vrai si peut etre supprime, faux sinon
  681. */
  682. function can_be_deleted()
  683. {
  684. $can_be_deleted=false;
  685. $sql = "SELECT COUNT(rowid) as nb";
  686. $sql.= " FROM ".MAIN_DB_PREFIX."bank";
  687. $sql.= " WHERE fk_account=".$this->id;
  688. $resql = $this->db->query($sql);
  689. if ($resql) {
  690. $obj=$this->db->fetch_object($resql);
  691. if ($obj->nb <= 1) $can_be_deleted=true; // Juste le solde
  692. }
  693. else {
  694. dol_print_error($this->db);
  695. }
  696. return $can_be_deleted;
  697. }
  698. /**
  699. * Return error
  700. *
  701. * @return string Error string
  702. */
  703. function error()
  704. {
  705. return $this->error;
  706. }
  707. /**
  708. * Return current sold
  709. *
  710. * @param int $option 1=Exclude future operation date (this is to exclude input made in advance and have real account sold)
  711. * @return int Current sold (value date <= today)
  712. */
  713. function solde($option=0)
  714. {
  715. $sql = "SELECT sum(amount) as amount";
  716. $sql.= " FROM ".MAIN_DB_PREFIX."bank";
  717. $sql.= " WHERE fk_account = ".$this->id;
  718. if ($option == 1) $sql.= " AND dateo <= '".$this->db->idate(dol_now())."'";
  719. $resql = $this->db->query($sql);
  720. if ($resql)
  721. {
  722. if ($this->db->num_rows($resql))
  723. {
  724. $obj=$this->db->fetch_object($resql);
  725. $solde = $obj->amount;
  726. }
  727. $this->db->free($resql);
  728. return $solde;
  729. }
  730. }
  731. /**
  732. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  733. *
  734. * @param User $user Objet user
  735. * @param int $filteraccountid To get info for a particular account id
  736. * @return int <0 if KO, 0=Nothing to show, >0 if OK
  737. */
  738. function load_board($user,$filteraccountid=0)
  739. {
  740. global $conf;
  741. if ($user->societe_id) return -1; // protection pour eviter appel par utilisateur externe
  742. $now=dol_now();
  743. $this->nbtodo=$this->nbtodolate=0;
  744. $sql = "SELECT b.rowid, b.datev as datefin";
  745. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b,";
  746. $sql.= " ".MAIN_DB_PREFIX."bank_account as ba";
  747. $sql.= " WHERE b.rappro=0";
  748. $sql.= " AND b.fk_account = ba.rowid";
  749. $sql.= " AND ba.entity = ".$conf->entity;
  750. $sql.= " AND (ba.rappro = 1 AND ba.courant != 2)"; // Compte rapprochable
  751. if ($filteraccountid) $sql.=" AND ba.rowid = ".$filteraccountid;
  752. //print $sql;
  753. $resql=$this->db->query($sql);
  754. if ($resql)
  755. {
  756. $num=$this->db->num_rows($resql);
  757. while ($obj=$this->db->fetch_object($resql))
  758. {
  759. $this->nbtodo++;
  760. if ($this->db->jdate($obj->datefin) < ($now - $conf->bank->rappro->warning_delay)) $this->nbtodolate++;
  761. }
  762. return $num;
  763. }
  764. else
  765. {
  766. dol_print_error($this->db);
  767. $this->error=$this->db->error();
  768. return -1;
  769. }
  770. }
  771. /**
  772. * Renvoie nom clicable (avec eventuellement le picto)
  773. *
  774. * @param int $withpicto Inclut le picto dans le lien
  775. * @param string $mode ''=Link to card, 'transactions'=Link to transactions card
  776. * @return string Chaine avec URL
  777. */
  778. function getNomUrl($withpicto=0, $mode='')
  779. {
  780. global $langs;
  781. $result='';
  782. if (empty($mode))
  783. {
  784. $lien = '<a href="'.DOL_URL_ROOT.'/compta/bank/fiche.php?id='.$this->id.'">';
  785. $lienfin='</a>';
  786. }
  787. else if ($mode == 'transactions')
  788. {
  789. $lien = '<a href="'.DOL_URL_ROOT.'/compta/bank/account.php?account='.$this->id.'">';
  790. $lienfin='</a>';
  791. }
  792. if ($withpicto) $result.=($lien.img_object($langs->trans("ShowAccount"),'account').$lienfin.' ');
  793. $result.=$lien.$this->label.$lienfin;
  794. return $result;
  795. }
  796. // Method after here are common to Account and CompanyBankAccount
  797. /**
  798. * Return if an account has valid information
  799. *
  800. * @return int 1 if correct, <=0 if wrong
  801. */
  802. function verif()
  803. {
  804. require_once DOL_DOCUMENT_ROOT . '/core/lib/bank.lib.php';
  805. // Call function to check BAN
  806. if (! checkBanForAccount($this))
  807. {
  808. $this->error_number = 12;
  809. $this->error_message = 'RIBControlError';
  810. }
  811. if ($this->error_number == 0)
  812. {
  813. return 1;
  814. }
  815. else
  816. {
  817. return 0;
  818. }
  819. }
  820. /**
  821. * Return account country code
  822. *
  823. * @return string country code
  824. */
  825. function getCountryCode()
  826. {
  827. global $mysoc;
  828. // We return country code of bank account
  829. if (! empty($this->country_code)) return $this->country_code;
  830. // For backward compatibility, we try to guess country from other information
  831. if (! empty($this->iban))
  832. {
  833. if ($mysoc->country_code === 'IN') return $mysoc->country_code; // Test to know if we can trust IBAN
  834. // If IBAN defined, we can know country of account from it
  835. if (preg_match("/^([a-zA-Z][a-zA-Z])/i",$this->iban,$reg)) return $reg[1];
  836. }
  837. // If this class is linked to a third party
  838. if (! empty($this->socid))
  839. {
  840. require_once DOL_DOCUMENT_ROOT .'/societe/class/societe.class.php';
  841. $company=new Societe($this->db);
  842. $result=$company->fetch($this->socid);
  843. if (! empty($company->country_code)) return $company->country_code;
  844. }
  845. // We return country code of managed company
  846. if (! empty($mysoc->country_code)) return $mysoc->country_code;
  847. return '';
  848. }
  849. /**
  850. * Return if a bank account is defined with detailed information (bank code, desk code, number and key).
  851. * More information on codes used by countries on page http://en.wikipedia.org/wiki/Bank_code
  852. *
  853. * @return int 0=No bank code need + Account number is enough
  854. * 1=Need 2 fields for bank code: Bank, Desk (France, Spain, ...) + Account number and key
  855. * 2=Neek 1 field for bank code: Bank only (Sort code for Great Britain, BSB for Australia) + Account number
  856. */
  857. function useDetailedBBAN()
  858. {
  859. $country_code=$this->getCountryCode();
  860. if (in_array($country_code,array('CH','DE','FR','ES','GA','IT'))) return 1; // France, Spain, Gabon
  861. if (in_array($country_code,array('AU','BE','CA','DK','GR','GB','ID','IE','IR','KR','NL','NZ','UK','US'))) return 2; // Australia, Great Britain...
  862. return 0;
  863. }
  864. /**
  865. * Load miscellaneous information for tab "Info"
  866. *
  867. * @param int $id Id of object to load
  868. * @return void
  869. */
  870. function info($id)
  871. {
  872. }
  873. /**
  874. * Initialise an instance with random values.
  875. * Used to build previews or test instances.
  876. * id must be 0 if object instance is a specimen.
  877. *
  878. * @return void
  879. */
  880. function initAsSpecimen()
  881. {
  882. $this->ref = 'MBA';
  883. $this->label = 'My Bank account';
  884. $this->bank = 'MyBank';
  885. $this->courant = 1;
  886. $this->clos = 0;
  887. $this->code_banque = '123';
  888. $this->code_guichet = '456';
  889. $this->number = 'ABC12345';
  890. $this->cle_rib = 50;
  891. $this->bic = 'AA12';
  892. $this->iban = 'FR999999999';
  893. $this->iban_prefix = 'FR'; // deprecated
  894. $this->domiciliation = 'The bank addresse';
  895. $this->proprio = 'Owner';
  896. $this->owner_address = 'Owner address';
  897. $this->country_id = 1;
  898. }
  899. }
  900. /**
  901. * Class to manage bank transaction lines
  902. */
  903. class AccountLine extends CommonObject
  904. {
  905. var $error;
  906. var $db;
  907. var $element='bank';
  908. var $table_element='bank';
  909. var $id;
  910. var $ref;
  911. var $datec;
  912. var $dateo;
  913. var $datev;
  914. var $amount;
  915. var $label;
  916. var $note;
  917. var $fk_user_author;
  918. var $fk_user_rappro;
  919. var $fk_type;
  920. var $rappro; // Is it conciliated
  921. var $num_releve; // If conciliated, what is bank receipt
  922. var $num_chq; // Num of cheque
  923. var $bank_chq; // Bank of cheque
  924. var $fk_bordereau; // Id of cheque receipt
  925. var $fk_account; // Id of bank account
  926. var $bank_account_label; // Label of bank account
  927. /**
  928. * Constructor
  929. *
  930. * @param DoliDB $db Database handler
  931. */
  932. function __construct($db)
  933. {
  934. $this->db = $db;
  935. }
  936. /**
  937. * Load into memory content of a bank transaction line
  938. *
  939. * @param int $rowid Id of bank transaction to load
  940. * @param string $ref Ref of bank transaction to load
  941. * @param string $num External num to load (ex: num of transaction for paypal fee)
  942. * @return int <0 if KO, 0 if OK but not found, >0 if OK and found
  943. */
  944. function fetch($rowid,$ref='',$num='')
  945. {
  946. global $conf;
  947. // Check parameters
  948. if (empty($rowid) && empty($ref) && empty($num)) return -1;
  949. $sql = "SELECT b.rowid, b.datec, b.datev, b.dateo, b.amount, b.label as label, b.fk_account,";
  950. $sql.= " b.fk_user_author, b.fk_user_rappro,";
  951. $sql.= " b.fk_type, b.num_releve, b.num_chq, b.rappro, b.note,";
  952. $sql.= " b.fk_bordereau, b.banque, b.emetteur,";
  953. //$sql.= " b.author"; // Is this used ?
  954. $sql.= " ba.ref as bank_account_ref, ba.label as bank_account_label";
  955. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b,";
  956. $sql.= " ".MAIN_DB_PREFIX."bank_account as ba";
  957. $sql.= " WHERE b.fk_account = ba.rowid";
  958. $sql.= " AND ba.entity = ".$conf->entity;
  959. if ($num) $sql.= " AND b.num_chq='".$this->db->escape($num)."'";
  960. else if ($ref) $sql.= " AND b.rowid='".$this->db->escape($ref)."'";
  961. else $sql.= " AND b.rowid=".$rowid;
  962. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  963. $result = $this->db->query($sql);
  964. if ($result)
  965. {
  966. $ret=0;
  967. $obj = $this->db->fetch_object($result);
  968. if ($obj)
  969. {
  970. $this->id = $obj->rowid;
  971. $this->rowid = $obj->rowid;
  972. $this->ref = $obj->rowid;
  973. $this->datec = $obj->datec;
  974. $this->datev = $obj->datev;
  975. $this->dateo = $obj->dateo;
  976. $this->amount = $obj->amount;
  977. $this->label = $obj->label;
  978. $this->note = $obj->note;
  979. $this->fk_user_author = $obj->fk_user_author;
  980. $this->fk_user_rappro = $obj->fk_user_rappro;
  981. $this->fk_type = $obj->fk_type; // Type of transaction
  982. $this->rappro = $obj->rappro;
  983. $this->num_releve = $obj->num_releve;
  984. $this->num_chq = $obj->num_chq;
  985. $this->bank_chq = $obj->bank_chq;
  986. $this->fk_bordereau = $obj->fk_bordereau;
  987. $this->fk_account = $obj->fk_account;
  988. $this->bank_account_ref = $obj->bank_account_ref;
  989. $this->bank_account_label = $obj->bank_account_label;
  990. $ret=1;
  991. }
  992. $this->db->free($result);
  993. return $ret;
  994. }
  995. else
  996. {
  997. dol_print_error($this->db);
  998. return -1;
  999. }
  1000. }
  1001. /**
  1002. * Delete transaction bank line record
  1003. *
  1004. * @param User $user User object that delete
  1005. * @return int <0 if KO, >0 if OK
  1006. */
  1007. function delete($user=0)
  1008. {
  1009. $nbko=0;
  1010. if ($this->rappro)
  1011. {
  1012. // Protection to avoid any delete of consolidated lines
  1013. $this->error="DeleteNotPossibleLineIsConsolidated";
  1014. return -1;
  1015. }
  1016. $this->db->begin();
  1017. // Delete urls
  1018. $result=$this->delete_urls($user);
  1019. if ($result < 0)
  1020. {
  1021. $nbko++;
  1022. }
  1023. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".$this->rowid;
  1024. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1025. $result = $this->db->query($sql);
  1026. if (! $result) $nbko++;
  1027. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".$this->rowid;
  1028. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1029. $result = $this->db->query($sql);
  1030. if (! $result) $nbko++;
  1031. if (! $nbko)
  1032. {
  1033. $this->db->commit();
  1034. return 1;
  1035. }
  1036. else
  1037. {
  1038. $this->db->rollback();
  1039. return -$nbko;
  1040. }
  1041. }
  1042. /**
  1043. * Delete bank line records
  1044. *
  1045. * @param User $user User object that delete
  1046. * @return int <0 if KO, >0 if OK
  1047. */
  1048. function delete_urls($user=0)
  1049. {
  1050. $nbko=0;
  1051. if ($this->rappro)
  1052. {
  1053. // Protection to avoid any delete of consolidated lines
  1054. $this->error="ErrorDeleteNotPossibleLineIsConsolidated";
  1055. return -1;
  1056. }
  1057. $this->db->begin();
  1058. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".$this->rowid;
  1059. dol_syslog(get_class($this)."::delete_urls", LOG_DEBUG);
  1060. $result = $this->db->query($sql);
  1061. if (! $result) $nbko++;
  1062. if (! $nbko)
  1063. {
  1064. $this->db->commit();
  1065. return 1;
  1066. }
  1067. else
  1068. {
  1069. $this->db->rollback();
  1070. return -$nbko;
  1071. }
  1072. }
  1073. /**
  1074. * Update bank account record in database
  1075. *
  1076. * @param User $user Object user making update
  1077. * @param int $notrigger 0=Disable all triggers
  1078. * @return int <0 if KO, >0 if OK
  1079. */
  1080. function update($user,$notrigger=0)
  1081. {
  1082. $this->db->begin();
  1083. $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
  1084. $sql.= " amount = ".price2num($this->amount).",";
  1085. $sql.= " datev='".$this->db->idate($this->datev)."',";
  1086. $sql.= " dateo='".$this->db->idate($this->dateo)."'";
  1087. $sql.= " WHERE rowid = ".$this->rowid;
  1088. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  1089. $resql = $this->db->query($sql);
  1090. if ($resql)
  1091. {
  1092. $this->db->commit();
  1093. return 1;
  1094. }
  1095. else
  1096. {
  1097. $this->db->rollback();
  1098. $this->error=$this->db->error();
  1099. return -1;
  1100. }
  1101. }
  1102. /**
  1103. * Update conciliation field
  1104. *
  1105. * @param User $user Objet user making update
  1106. * @param int $cat Category id
  1107. * @return int <0 if KO, >0 if OK
  1108. */
  1109. function update_conciliation($user,$cat)
  1110. {
  1111. $this->db->begin();
  1112. $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
  1113. $sql.= " rappro = 1";
  1114. $sql.= ", num_releve = '".$this->num_releve."'";
  1115. $sql.= ", fk_user_rappro = ".$user->id;
  1116. $sql.= " WHERE rowid = ".$this->id;
  1117. dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
  1118. $resql = $this->db->query($sql);
  1119. if ($resql)
  1120. {
  1121. if (! empty($cat))
  1122. {
  1123. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_class (";
  1124. $sql.= "lineid";
  1125. $sql.= ", fk_categ";
  1126. $sql.= ") VALUES (";
  1127. $sql.= $this->id;
  1128. $sql.= ", ".$cat;
  1129. $sql.= ")";
  1130. dol_syslog(get_class($this)."::update_conciliation", LOG_DEBUG);
  1131. $resql = $this->db->query($sql);
  1132. // No error check. Can fail if category already affected
  1133. }
  1134. $this->rappro=1;
  1135. $this->db->commit();
  1136. return 1;
  1137. }
  1138. else
  1139. {
  1140. $this->db->rollback();
  1141. return -1;
  1142. }
  1143. }
  1144. /**
  1145. * Increase/decrease value date of a rowid
  1146. *
  1147. * @param int $rowid Id of line
  1148. * @param int $sign 1 or -1
  1149. * @return int >0 if OK, 0 if KO
  1150. */
  1151. function datev_change($rowid,$sign=1)
  1152. {
  1153. $sql = "SELECT datev FROM ".MAIN_DB_PREFIX."bank WHERE rowid = ".$rowid;
  1154. $resql = $this->db->query($sql);
  1155. if ($resql)
  1156. {
  1157. $obj=$this->db->fetch_object($resql);
  1158. $newdate=$this->db->jdate($obj->datev)+(3600*24*$sign);
  1159. $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
  1160. $sql.= " datev = '".$this->db->idate($newdate)."'";
  1161. $sql.= " WHERE rowid = ".$rowid;
  1162. $result = $this->db->query($sql);
  1163. if ($result)
  1164. {
  1165. if ($this->db->affected_rows($result))
  1166. {
  1167. return 1;
  1168. }
  1169. }
  1170. else
  1171. {
  1172. dol_print_error($this->db);
  1173. return 0;
  1174. }
  1175. }
  1176. else dol_print_error($this->db);
  1177. return 0;
  1178. }
  1179. /**
  1180. * Increase value date of a rowid
  1181. *
  1182. * @param int $id Id of line to change
  1183. * @return int >0 if OK, 0 if KO
  1184. */
  1185. function datev_next($id)
  1186. {
  1187. return $this->datev_change($id,1);
  1188. }
  1189. /**
  1190. * Decrease value date of a rowid
  1191. *
  1192. * @param int $id Id of line to change
  1193. * @return int >0 if OK, 0 if KO
  1194. */
  1195. function datev_previous($id)
  1196. {
  1197. return $this->datev_change($id,-1);
  1198. }
  1199. /**
  1200. * Load miscellaneous information for tab "Info"
  1201. *
  1202. * @param int $id Id of object to load
  1203. * @return void
  1204. */
  1205. function info($id)
  1206. {
  1207. $sql = 'SELECT b.rowid, b.datec,';
  1208. $sql.= ' b.fk_user_author, b.fk_user_rappro';
  1209. $sql.= ' FROM '.MAIN_DB_PREFIX.'bank as b';
  1210. $sql.= ' WHERE b.rowid = '.$id;
  1211. $result=$this->db->query($sql);
  1212. if ($result)
  1213. {
  1214. if ($this->db->num_rows($result))
  1215. {
  1216. $obj = $this->db->fetch_object($result);
  1217. $this->id = $obj->rowid;
  1218. if ($obj->fk_user_author)
  1219. {
  1220. $cuser = new User($this->db);
  1221. $cuser->fetch($obj->fk_user_author);
  1222. $this->user_creation = $cuser;
  1223. }
  1224. if ($obj->fk_user_rappro)
  1225. {
  1226. $ruser = new User($this->db);
  1227. $ruser->fetch($obj->fk_user_rappro);
  1228. $this->user_rappro = $ruser;
  1229. }
  1230. $this->date_creation = $this->db->jdate($obj->datec);
  1231. //$this->date_rappro = $obj->daterappro; // Not yet managed
  1232. }
  1233. $this->db->free($result);
  1234. }
  1235. else
  1236. {
  1237. dol_print_error($this->db);
  1238. }
  1239. }
  1240. /**
  1241. * Renvoie nom clicable (avec eventuellement le picto)
  1242. *
  1243. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  1244. * @param int $maxlen Longueur max libelle
  1245. * @param string $option Option ('showall')
  1246. * @return string Chaine avec URL
  1247. */
  1248. function getNomUrl($withpicto=0,$maxlen=0,$option='')
  1249. {
  1250. global $langs;
  1251. $result='';
  1252. $lien = '<a href="'.DOL_URL_ROOT.'/compta/bank/ligne.php?rowid='.$this->rowid.'">';
  1253. $lienfin='</a>';
  1254. if ($withpicto) $result.=($lien.img_object($langs->trans("ShowTransaction"),'account').$lienfin.' ');
  1255. $result.=$lien.$this->rowid.$lienfin;
  1256. if ($option == 'showall' || $option == 'showconciliated') $result.=' (';
  1257. if ($option == 'showall')
  1258. {
  1259. $result.=$langs->trans("BankAccount").': ';
  1260. $accountstatic=new Account($this->db);
  1261. $accountstatic->id=$this->fk_account;
  1262. $accountstatic->label=$this->bank_account_label;
  1263. $result.=$accountstatic->getNomUrl(0).', ';
  1264. }
  1265. if ($option == 'showall' || $option == 'showconciliated')
  1266. {
  1267. $result.=$langs->trans("BankLineConciliated").': ';
  1268. $result.=yn($this->rappro);
  1269. }
  1270. if ($option == 'showall' || $option == 'showconciliated') $result.=')';
  1271. return $result;
  1272. }
  1273. }