releve.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <?php
  2. /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2013 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  6. * Copyright (C) 2017 Patrick Delcroix <pmpdelcroix@gmail.com>
  7. * Copyright (C) 2019 Nicolas ZABOURI <info@inovea-conseil.com>
  8. * Copyright (C) 2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/compta/bank/releve.php
  25. * \ingroup banque
  26. * \brief Page to show a bank statement report
  27. */
  28. // Load Dolibarr environment
  29. require '../../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  38. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/cheque/class/remisecheque.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/don/class/paymentdonation.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/loan/class/paymentloan.class.php';
  43. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/paymentvarious.class.php';
  44. //show files
  45. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  46. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
  47. // Load translation files required by the page
  48. $langs->loadLangs(array("banks", "categories", "companies", "bills", "trips", "donations", "loan", "salaries"));
  49. $action = GETPOST('action', 'aZ09');
  50. $id = GETPOST('account', 'int') ? GETPOST('account', 'int') : GETPOST('id', 'int');
  51. $ref = GETPOST('ref', 'alpha');
  52. $dvid = GETPOST('dvid', 'alpha');
  53. $numref = GETPOST('num', 'alpha');
  54. $ve = GETPOST("ve", 'alpha');
  55. $brref = GETPOST('brref', 'alpha');
  56. $oldbankreceipt = GETPOST('oldbankreceipt', 'alpha');
  57. $newbankreceipt = GETPOST('newbankreceipt', 'alpha');
  58. $rel = GETPOST("rel", 'alphanohtml');
  59. $backtopage = GETPOST('backtopage', 'alpha');
  60. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  61. $hookmanager->initHooks(array('bankaccountstatement', 'globalcard'));
  62. if ($user->hasRight('banque', 'consolidate') && $action == 'dvnext' && !empty($dvid)) {
  63. $al = new AccountLine($db);
  64. $al->datev_next($dvid);
  65. }
  66. if ($user->hasRight('banque', 'consolidate') && $action == 'dvprev' && !empty($dvid)) {
  67. $al = new AccountLine($db);
  68. $al->datev_previous($dvid);
  69. }
  70. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  71. $sortfield = GETPOST('sortfield', 'aZ09comma');
  72. $sortorder = GETPOST('sortorder', 'aZ09comma');
  73. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  74. $pageplusone = GETPOST("pageplusone", 'int');
  75. if ($pageplusone) {
  76. $page = $pageplusone - 1;
  77. }
  78. if (empty($page) || $page == -1) {
  79. $page = 0;
  80. } // If $page is not defined, or '' or -1
  81. $offset = $limit * $page;
  82. $pageprev = $page - 1;
  83. $pagenext = $page + 1;
  84. if (!$sortorder) {
  85. $sortorder = "ASC";
  86. }
  87. if (!$sortfield) {
  88. $sortfield = "s.nom";
  89. }
  90. $object = new Account($db);
  91. if ($id > 0 || !empty($ref)) {
  92. $result = $object->fetch($id, $ref);
  93. // if fetch from ref, $id may be empty
  94. $id = $object->id; // Force the search field on id of account
  95. }
  96. // Initialize technical object to manage context to save list fields
  97. $contextpage = 'banktransactionlist'.(empty($object->ref) ? '' : '-'.$object->id);
  98. // Security check
  99. $fieldid = (!empty($ref) ? $ref : $id);
  100. $fieldname = (!empty($ref) ? 'ref' : 'rowid');
  101. if ($user->socid) {
  102. $socid = $user->socid;
  103. }
  104. $result = restrictedArea($user, 'banque', $fieldid, 'bank_account', '', '', $fieldname);
  105. $error = 0;
  106. // Define number of receipt to show (current, previous or next one ?)
  107. $foundprevious = '';
  108. $foundnext = '';
  109. // Search previous receipt number
  110. $sql = "SELECT b.num_releve as num";
  111. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  112. $sql .= " WHERE b.num_releve < '".$db->escape($numref)."'";
  113. $sql .= " AND b.fk_account = ".((int) $object->id);
  114. $sql .= " ORDER BY b.num_releve DESC";
  115. $sql .= $db->plimit(1);
  116. dol_syslog("htdocs/compta/bank/releve.php", LOG_DEBUG);
  117. $resql = $db->query($sql);
  118. if ($resql) {
  119. $numrows = $db->num_rows($resql);
  120. if ($numrows > 0) {
  121. $obj = $db->fetch_object($resql);
  122. if ($rel == 'prev') {
  123. $numref = $obj->num;
  124. }
  125. $foundprevious = $obj->num;
  126. }
  127. } else {
  128. dol_print_error($db);
  129. }
  130. // Search next receipt
  131. $sql = "SELECT b.num_releve as num";
  132. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  133. $sql .= " WHERE b.num_releve > '".$db->escape($numref)."'";
  134. $sql .= " AND b.fk_account = ".((int) $object->id);
  135. $sql .= " ORDER BY b.num_releve ASC";
  136. $sql .= $db->plimit(1);
  137. dol_syslog("htdocs/compta/bank/releve.php", LOG_DEBUG);
  138. $resql = $db->query($sql);
  139. if ($resql) {
  140. $numrows = $db->num_rows($resql);
  141. if ($numrows > 0) {
  142. $obj = $db->fetch_object($resql);
  143. if ($rel == 'next') {
  144. $numref = $obj->num;
  145. }
  146. $foundnext = $obj->num;
  147. }
  148. } else {
  149. dol_print_error($db);
  150. }
  151. $sql = "SELECT b.rowid, b.dateo as do, b.datev as dv,";
  152. $sql .= " b.amount, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type,";
  153. $sql .= " b.fk_bordereau,";
  154. $sql .= " bc.ref,";
  155. $sql .= " ba.rowid as bankid, ba.ref as bankref, ba.label as banklabel";
  156. $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as ba,";
  157. $sql .= " ".MAIN_DB_PREFIX."bank as b";
  158. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bordereau_cheque as bc ON bc.rowid=b.fk_bordereau';
  159. $sql .= " WHERE b.num_releve = '".$db->escape($numref)."'";
  160. if (empty($numref)) {
  161. $sql .= " OR b.num_releve is null";
  162. }
  163. $sql .= " AND b.fk_account = ".((int) $object->id);
  164. $sql .= " AND b.fk_account = ba.rowid";
  165. $sql .= " AND ba.entity IN (".getEntity($object->element).")";
  166. $sql .= $db->order("b.datev, b.datec", "ASC"); // We add date of creation to have correct order when everything is done the same day
  167. $sqlrequestforbankline = $sql;
  168. /*
  169. * Actions
  170. */
  171. if ($action == 'confirm_editbankreceipt' && !empty($oldbankreceipt) && !empty($newbankreceipt)) {
  172. // Test to check newbankreceipt does not exists yet
  173. $sqltest = "SELECT b.rowid FROM ".MAIN_DB_PREFIX."bank as b, ".MAIN_DB_PREFIX."bank_account as ba";
  174. $sqltest .= " WHERE b.fk_account = ba.rowid AND ba.entity = ".((int) $conf->entity);
  175. $sqltest .= " AND num_releve = '".$db->escape($newbankreceipt)."'";
  176. $sqltest .= $db->plimit(1); // Need the first one only
  177. $resql = $db->query($sqltest);
  178. if ($resql) {
  179. $obj = $db->fetch_object($resql);
  180. if ($obj && $obj->rowid) {
  181. setEventMessages('ErrorBankReceiptAlreadyExists', null, 'errors');
  182. $error++;
  183. }
  184. } else {
  185. dol_print_error($db);
  186. }
  187. // Update bank receipt name
  188. if (!$error) {
  189. $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."bank SET num_releve = '".$db->escape($newbankreceipt)."'";
  190. $sqlupdate .= " WHERE num_releve = '".$db->escape($oldbankreceipt)."' AND fk_account = ".((int) $id);
  191. $resql = $db->query($sqlupdate);
  192. if (!$resql) {
  193. dol_print_error($db);
  194. }
  195. }
  196. $action = 'view';
  197. }
  198. /*
  199. * View
  200. */
  201. $form = new Form($db);
  202. $societestatic = new Societe($db);
  203. $chargestatic = new ChargeSociales($db);
  204. $memberstatic = new Adherent($db);
  205. $paymentstatic = new Paiement($db);
  206. $paymentsupplierstatic = new PaiementFourn($db);
  207. $paymentvatstatic = new Tva($db);
  208. $bankstatic = new Account($db);
  209. $banklinestatic = new AccountLine($db);
  210. $remisestatic = new RemiseCheque($db);
  211. $paymentdonationstatic = new PaymentDonation($db);
  212. $paymentloanstatic = new PaymentLoan($db);
  213. $paymentvariousstatic = new PaymentVarious($db);
  214. // Must be before button action
  215. $param = '';
  216. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  217. $param .= '&contextpage='.$contextpage;
  218. }
  219. if ($limit > 0 && $limit != $conf->liste_limit) {
  220. $param .= '&limit='.$limit;
  221. }
  222. if ($id > 0) {
  223. $param .= '&id='.urlencode($id);
  224. }
  225. if (empty($numref)) {
  226. $title = $object->ref.' - '.$langs->trans("AccountStatements");
  227. $helpurl = "";
  228. } else {
  229. $title = $langs->trans("FinancialAccount").' - '.$langs->trans("AccountStatements");
  230. $helpurl = "";
  231. }
  232. llxHeader('', $title, $helpurl);
  233. if (empty($numref)) {
  234. $sortfield = 'numr';
  235. $sortorder = 'DESC';
  236. // List of all standing receipts
  237. $sql = "SELECT DISTINCT(b.num_releve) as numr";
  238. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  239. $sql .= " WHERE b.fk_account = ".((int) $object->id);
  240. $sql .= $db->order($sortfield, $sortorder);
  241. // Count total nb of records
  242. $totalnboflines = 0;
  243. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  244. $result = $db->query($sql);
  245. $totalnboflines = $db->num_rows($result);
  246. }
  247. $sql .= $db->plimit($conf->liste_limit + 1, $offset);
  248. $result = $db->query($sql);
  249. if ($result) {
  250. $numrows = $db->num_rows($result);
  251. $i = 0;
  252. // Onglets
  253. $head = bank_prepare_head($object);
  254. print dol_get_fiche_head($head, 'statement', $langs->trans("FinancialAccount"), 0, 'account');
  255. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  256. $morehtmlref = '';
  257. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
  258. print dol_get_fiche_end();
  259. print '<div class="tabsAction">';
  260. if ($object->canBeConciliated() > 0) {
  261. $allowautomaticconciliation = false; // TODO
  262. $titletoconciliatemanual = $langs->trans("Conciliate");
  263. $titletoconciliateauto = $langs->trans("Conciliate");
  264. if ($allowautomaticconciliation) {
  265. $titletoconciliatemanual .= ' ('.$langs->trans("Manual").')';
  266. $titletoconciliateauto .= ' ('.$langs->trans("Auto").')';
  267. }
  268. // If not cash account and can be reconciliate
  269. if ($user->hasRight('banque', 'consolidate')) {
  270. $buttonreconcile = '<a class="butAction" href="'.DOL_URL_ROOT.'/compta/bank/bankentries_list.php?action=reconcile&sortfield=b.datev,b.dateo,b.rowid&sortorder=asc,asc,asc&search_conciliated=0&search_account='.$id.$param.'">'.$titletoconciliatemanual.'</a>';
  271. } else {
  272. $buttonreconcile = '<a class="butActionRefused classfortooltip" title="'.$langs->trans("NotEnoughPermissions").'" href="#">'.$titletoconciliatemanual.'</a>';
  273. }
  274. if ($allowautomaticconciliation) {
  275. // If not cash account and can be reconciliate
  276. if ($user->hasRight('banque', 'consolidate')) {
  277. $newparam = $param;
  278. $newparam = preg_replace('/search_conciliated=\d+/i', '', $newparam);
  279. $buttonreconcile .= ' <a class="butAction" style="margin-bottom: 5px !important; margin-top: 5px !important" href="'.DOL_URL_ROOT.'/compta/bank/bankentries_list.php?action=reconcile&sortfield=b.datev,b.dateo,b.rowid&sortorder=asc,asc,asc&search_conciliated=0'.$newparam.'">'.$titletoconciliateauto.'</a>';
  280. } else {
  281. $buttonreconcile .= ' <a class="butActionRefused" style="margin-bottom: 5px !important; margin-top: 5px !important" title="'.$langs->trans("NotEnoughPermissions").'" href="#">'.$titletoconciliateauto.'</a>';
  282. }
  283. }
  284. print $buttonreconcile;
  285. }
  286. print '</div>';
  287. print_barre_liste('', $page, $_SERVER["PHP_SELF"], "&account=".$object->id, $sortfield, $sortorder, '', $numrows, $totalnboflines, '');
  288. print '<form name="aaa" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  289. print '<input type="hidden" name="token" value="'.newToken().'">';
  290. print '<input type="hidden" name="action" value="confirm_editbankreceipt">';
  291. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  292. print '<input type="hidden" name="account" value="'.$object->id.'">';
  293. print '<input type="hidden" name="page" value="'.$page.'">';
  294. print '<table class="noborder centpercent">';
  295. print '<tr class="liste_titre">';
  296. print '<td>'.$langs->trans("Ref").'</td>';
  297. print '<td class="right">'.$langs->trans("InitialBankBalance").'</td>';
  298. print '<td class="right">'.$langs->trans("EndBankBalance").'</td>';
  299. print '<td></td>';
  300. print '</tr>';
  301. $balancestart = array();
  302. $content = array();
  303. while ($i < min($numrows, $conf->liste_limit)) {
  304. $objp = $db->fetch_object($result);
  305. if (!isset($objp->numr)) {
  306. //
  307. } else {
  308. print '<tr class="oddeven">';
  309. print '<td>';
  310. if ($action != 'editbankreceipt' || $objp->numr != $brref) {
  311. print '<a href="releve.php?num='.$objp->numr.'&account='.$object->id.'">'.$objp->numr.'</a>';
  312. } else {
  313. print '<input type="hidden" name="oldbankreceipt" value="'.$objp->numr.'">';
  314. print '<input type="text" name="newbankreceipt" value="'.$objp->numr.'">';
  315. print '<input type="submit" class="button smallpaddingimp" name="actionnewbankreceipt" value="'.$langs->trans("Save").'">';
  316. print '<input type="submit" class="button button-cancel smallpaddingimp" name="cancel" value="'.$langs->trans("Cancel").'">';
  317. }
  318. print '</td>';
  319. // Calculate start amount
  320. $sql = "SELECT sum(b.amount) as amount";
  321. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  322. $sql .= " WHERE b.num_releve < '".$db->escape($objp->numr)."'";
  323. $sql .= " AND b.fk_account = ".((int) $object->id);
  324. $resql = $db->query($sql);
  325. if ($resql) {
  326. $obj = $db->fetch_object($resql);
  327. $balancestart[$objp->numr] = $obj->amount;
  328. $db->free($resql);
  329. }
  330. print '<td class="right"><span class="amount">'.price($balancestart[$objp->numr], '', $langs, 1, -1, -1, empty($object->currency_code) ? $conf->currency : $object->currency_code).'</span></td>';
  331. // Calculate end amount
  332. $sql = "SELECT sum(b.amount) as amount";
  333. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  334. $sql .= " WHERE b.num_releve = '".$db->escape($objp->numr)."'";
  335. $sql .= " AND b.fk_account = ".((int) $object->id);
  336. $resql = $db->query($sql);
  337. if ($resql) {
  338. $obj = $db->fetch_object($resql);
  339. $content[$objp->numr] = $obj->amount;
  340. $db->free($resql);
  341. }
  342. print '<td class="right"><span class="amount">'.price(($balancestart[$objp->numr] + $content[$objp->numr]), '', $langs, 1, -1, -1, empty($object->currency_code) ? $conf->currency : $object->currency_code).'</span></td>';
  343. print '<td class="center">';
  344. if ($user->hasRight('banque', 'consolidate') && $action != 'editbankreceipt') {
  345. print '<a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?account='.$object->id.($page > 0 ? '&page='.$page : '').'&action=editbankreceipt&token='.newToken().'&brref='.urlencode($objp->numr).'">'.img_edit().'</a>';
  346. }
  347. print '</td>';
  348. print '</tr>'."\n";
  349. }
  350. $i++;
  351. }
  352. print "</table>\n";
  353. print '</form>';
  354. print "\n</div>\n";
  355. } else {
  356. dol_print_error($db);
  357. }
  358. } else {
  359. /**
  360. * Show list of record into a bank statement
  361. */
  362. // Onglets
  363. $head = account_statement_prepare_head($object, $numref);
  364. print dol_get_fiche_head($head, 'statement', $langs->trans("AccountStatement"), -1, 'account');
  365. $morehtmlright = '';
  366. $morehtmlright .= '<div class="pagination"><ul>';
  367. if ($foundprevious) {
  368. $morehtmlright .= '<li class="pagination"><a class="paginationnext" href="'.$_SERVER["PHP_SELF"].'?num='.urlencode($foundprevious).'&amp;ve='.urlencode($ve).'&amp;account='.((int) $object->id).'"><i class="fa fa-chevron-left" title="'.dol_escape_htmltag($langs->trans("Previous")).'"></i></a></li>';
  369. }
  370. $morehtmlright .= '<li class="pagination"><span class="active">'.$langs->trans("AccountStatement")." ".$numref.'</span></li>';
  371. if ($foundnext) {
  372. $morehtmlright .= '<li class="pagination"><a class="paginationnext" href="'.$_SERVER["PHP_SELF"].'?num='.urlencode($foundnext).'&amp;ve='.urlencode($ve).'&amp;account='.((int) $object->id).'"><i class="fa fa-chevron-right" title="'.dol_escape_htmltag($langs->trans("Next")).'"></i></a></li>';
  373. }
  374. $morehtmlright .= '</ul></div>';
  375. $title = $langs->trans("AccountStatement").' '.$numref.' - '.$langs->trans("BankAccount").' '.$object->getNomUrl(1, 'receipts');
  376. print load_fiche_titre($title, $morehtmlright, '');
  377. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  378. print '<input type="hidden" name="token" value="'.newToken().'">';
  379. print '<input type="hidden" name="action" value="add">';
  380. print '<div class="div-table-responsive">';
  381. print '<table class="noborder centpercent">';
  382. print '<tr class="liste_titre">';
  383. print '<td class="center">'.$langs->trans("DateOperationShort").'</td>';
  384. print '<td class="center">'.$langs->trans("DateValueShort").'</td>';
  385. print '<td>'.$langs->trans("Type").'</td>';
  386. print '<td>'.$langs->trans("Description").'</td>';
  387. print '<td class="right" width="60">'.$langs->trans("Debit").'</td>';
  388. print '<td class="right" width="60">'.$langs->trans("Credit").'</td>';
  389. print '<td class="right">'.$langs->trans("Balance").'</td>';
  390. print '<td>&nbsp;</td>';
  391. print "</tr>\n";
  392. // Calcul du solde de depart du releve
  393. $sql = "SELECT sum(b.amount) as amount";
  394. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  395. $sql .= " WHERE b.num_releve < '".$db->escape($numref)."'";
  396. $sql .= " AND b.fk_account = ".((int) $object->id);
  397. $resql = $db->query($sql);
  398. if ($resql) {
  399. $obj = $db->fetch_object($resql);
  400. $total = $obj->amount;
  401. $db->free($resql);
  402. }
  403. $totalc = $totald = 0;
  404. // Recherche les ecritures pour le releve
  405. $sql = $sqlrequestforbankline;
  406. $result = $db->query($sql);
  407. if ($result) {
  408. $numrows = $db->num_rows($result);
  409. $i = 0;
  410. // Ligne Solde debut releve
  411. print '<tr class="oddeven"><td colspan="3"></td>';
  412. print '<td colspan="3"><b>'.$langs->trans("InitialBankBalance")." :</b></td>";
  413. print '<td class="right"><b>'.price($total).'</b></td><td>&nbsp;</td>';
  414. print "</tr>\n";
  415. while ($i < $numrows) {
  416. $objp = $db->fetch_object($result);
  417. $total = $total + $objp->amount;
  418. print '<tr class="oddeven">';
  419. // Date operation
  420. print '<td class="nowrap center">'.dol_print_date($db->jdate($objp->do), "day").'</td>';
  421. // Date de valeur
  422. print '<td valign="center" class="center nowrap">';
  423. print '<span class="spanforajaxedit">'.dol_print_date($db->jdate($objp->dv), "day").'</span>';
  424. print '&nbsp;';
  425. print '<span class="inline-block">';
  426. print '<a class="ajaxforbankoperationchange reposition" href="'.$_SERVER['PHP_SELF'].'?action=dvprev&amp;num='.$numref.'&amp;account='.$object->id.'&amp;rowid='.$objp->rowid.'&amp;dvid='.$objp->rowid.'">';
  427. print img_edit_remove()."</a> ";
  428. print '<a class="ajaxforbankoperationchange reposition" href="'.$_SERVER['PHP_SELF'].'?action=dvnext&amp;num='.$numref.'&amp;account='.$object->id.'&amp;rowid='.$objp->rowid.'&amp;dvid='.$objp->rowid.'">';
  429. print img_edit_add()."</a>";
  430. print '</span>';
  431. print "</td>\n";
  432. // Type and num
  433. if ($objp->fk_type == 'SOLD') {
  434. $type_label = '&nbsp;';
  435. } else {
  436. $type_label = ($langs->trans("PaymentTypeShort".$objp->fk_type) != "PaymentTypeShort".$objp->fk_type) ? $langs->trans("PaymentTypeShort".$objp->fk_type) : $objp->fk_type;
  437. }
  438. $link = '';
  439. if ($objp->fk_bordereau > 0) {
  440. $remisestatic->id = $objp->fk_bordereau;
  441. $remisestatic->ref = $objp->ref;
  442. $link = ' '.$remisestatic->getNomUrl(1);
  443. }
  444. print '<td class="nowrap">'.$type_label.' '.($objp->num_chq ? $objp->num_chq : '').$link.'</td>';
  445. // Description
  446. print '<td valign="center">';
  447. print '<a href="'.DOL_URL_ROOT.'/compta/bank/line.php?rowid='.$objp->rowid.'&account='.$object->id.'">';
  448. $reg = array();
  449. preg_match('/\((.+)\)/i', $objp->label, $reg); // Si texte entoure de parenthese on tente recherche de traduction
  450. if ($reg[1] && $langs->trans($reg[1]) != $reg[1]) {
  451. print $langs->trans($reg[1]);
  452. } else {
  453. print $objp->label;
  454. }
  455. print '</a>';
  456. /*
  457. * Add links under the label (link to payment, company, user, social contribution...)
  458. */
  459. $newline = 1;
  460. $links = $object->get_url($objp->rowid);
  461. foreach ($links as $key => $val) {
  462. if (!$newline) {
  463. print ' - ';
  464. } else {
  465. print '<br>';
  466. }
  467. if ($links[$key]['type'] == 'payment') {
  468. $paymentstatic->id = $links[$key]['url_id'];
  469. $paymentstatic->ref = $langs->trans("Payment");
  470. print ' '.$paymentstatic->getNomUrl(1);
  471. $newline = 0;
  472. } elseif ($links[$key]['type'] == 'payment_supplier') {
  473. $paymentsupplierstatic->id = $links[$key]['url_id'];
  474. $paymentsupplierstatic->ref = $langs->trans("Payment");
  475. print ' '.$paymentsupplierstatic->getNomUrl(1);
  476. $newline = 0;
  477. } elseif ($links[$key]['type'] == 'payment_sc') {
  478. print '<a href="'.DOL_URL_ROOT.'/compta/payment_sc/card.php?id='.$links[$key]['url_id'].'">';
  479. print ' '.img_object($langs->trans('ShowPayment'), 'payment').' ';
  480. print $langs->trans("SocialContributionPayment");
  481. print '</a>';
  482. $newline = 0;
  483. } elseif ($links[$key]['type'] == 'payment_vat') {
  484. $paymentvatstatic->id = $links[$key]['url_id'];
  485. $paymentvatstatic->ref = $langs->trans("Payment");
  486. print ' '.$paymentvatstatic->getNomUrl(1);
  487. } elseif ($links[$key]['type'] == 'payment_salary') {
  488. print '<a href="'.DOL_URL_ROOT.'/salaries/card.php?id='.$links[$key]['url_id'].'">';
  489. print ' '.img_object($langs->trans('ShowPayment'), 'payment').' ';
  490. print $langs->trans("Payment");
  491. print '</a>';
  492. $newline = 0;
  493. } elseif ($links[$key]['type'] == 'payment_donation') {
  494. $paymentdonationstatic->id = $links[$key]['url_id'];
  495. $paymentdonationstatic->ref = $langs->trans("Payment");
  496. print ' '.$paymentdonationstatic->getNomUrl(1);
  497. $newline = 0;
  498. } elseif ($links[$key]['type'] == 'payment_loan') {
  499. $paymentloanstatic->id = $links[$key]['url_id'];
  500. $paymentloanstatic->ref = $langs->trans("Payment");
  501. print ' '.$paymentloanstatic->getNomUrl(1);
  502. $newline = 0;
  503. } elseif ($links[$key]['type'] == 'payment_various') {
  504. $paymentvariousstatic->id = $links[$key]['url_id'];
  505. $paymentvariousstatic->ref = $langs->trans("Payment");
  506. print ' '.$paymentvariousstatic->getNomUrl(1);
  507. $newline = 0;
  508. } elseif ($links[$key]['type'] == 'banktransfert') {
  509. // Do not show link to transfer since there is no transfer card (avoid confusion). Can already be accessed from transaction detail.
  510. if ($objp->amount > 0) {
  511. $banklinestatic->fetch($links[$key]['url_id']);
  512. $bankstatic->id = $banklinestatic->fk_account;
  513. $bankstatic->label = $banklinestatic->bank_account_label;
  514. print ' ('.$langs->trans("from").' ';
  515. print $bankstatic->getNomUrl(1, 'transactions');
  516. print ' '.$langs->trans("toward").' ';
  517. $bankstatic->id = $objp->bankid;
  518. $bankstatic->label = $objp->bankref;
  519. print $bankstatic->getNomUrl(1, '');
  520. print ')';
  521. } else {
  522. $bankstatic->id = $objp->bankid;
  523. $bankstatic->label = $objp->bankref;
  524. print ' ('.$langs->trans("from").' ';
  525. print $bankstatic->getNomUrl(1, '');
  526. print ' '.$langs->trans("toward").' ';
  527. $banklinestatic->fetch($links[$key]['url_id']);
  528. $bankstatic->id = $banklinestatic->fk_account;
  529. $bankstatic->label = $banklinestatic->bank_account_label;
  530. print $bankstatic->getNomUrl(1, 'transactions');
  531. print ')';
  532. }
  533. } elseif ($links[$key]['type'] == 'company') {
  534. $societestatic->id = $links[$key]['url_id'];
  535. $societestatic->name = $links[$key]['label'];
  536. print $societestatic->getNomUrl(1, 'company', 24);
  537. $newline = 0;
  538. } elseif ($links[$key]['type'] == 'member') {
  539. print '<a href="'.DOL_URL_ROOT.'/adherents/card.php?rowid='.$links[$key]['url_id'].'">';
  540. print img_object($langs->trans('ShowMember'), 'user').' ';
  541. print $links[$key]['label'];
  542. print '</a>';
  543. $newline = 0;
  544. } elseif ($links[$key]['type'] == 'user') {
  545. print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$links[$key]['url_id'].'">';
  546. print img_object($langs->trans('ShowUser'), 'user').' ';
  547. print $links[$key]['label'];
  548. print '</a>';
  549. $newline = 0;
  550. } elseif ($links[$key]['type'] == 'sc') {
  551. print '<a href="'.DOL_URL_ROOT.'/compta/sociales/card.php?id='.$links[$key]['url_id'].'">';
  552. print img_object($langs->trans('ShowBill'), 'bill').' ';
  553. print $langs->trans("SocialContribution");
  554. print '</a>';
  555. $newline = 0;
  556. } else {
  557. print '<a href="'.$links[$key]['url'].$links[$key]['url_id'].'">';
  558. print $links[$key]['label'];
  559. print '</a>';
  560. $newline = 0;
  561. }
  562. }
  563. // Categories
  564. if ($ve) {
  565. $sql = "SELECT label";
  566. $sql .= " FROM ".MAIN_DB_PREFIX."bank_categ as ct";
  567. $sql .= ", ".MAIN_DB_PREFIX."bank_class as cl";
  568. $sql .= " WHERE ct.rowid = cl.fk_categ";
  569. $sql .= " AND ct.entity = ".((int) $conf->entity);
  570. $sql .= " AND cl.lineid = ".((int) $objp->rowid);
  571. $resc = $db->query($sql);
  572. if ($resc) {
  573. $numc = $db->num_rows($resc);
  574. $ii = 0;
  575. if ($numc && !$newline) {
  576. print '<br>';
  577. }
  578. while ($ii < $numc) {
  579. $objc = $db->fetch_object($resc);
  580. print "<br>-&nbsp;<i>".$objc->label."</i>";
  581. $ii++;
  582. }
  583. } else {
  584. dol_print_error($db);
  585. }
  586. }
  587. print "</td>";
  588. if ($objp->amount < 0) {
  589. $totald = $totald + abs($objp->amount);
  590. print '<td class="nowrap right">'.price($objp->amount * -1)."</td><td>&nbsp;</td>\n";
  591. } else {
  592. $totalc = $totalc + abs($objp->amount);
  593. print '<td>&nbsp;</td><td class="nowrap right">'.price($objp->amount)."</td>\n";
  594. }
  595. print '<td class="nowrap right">'.price(price2num($total, 'MT'))."</td>\n";
  596. if ($user->hasRight('banque', 'modifier') || $user->hasRight('banque', 'consolidate')) {
  597. print '<td class="center"><a class="editfielda reposition" href="'.DOL_URL_ROOT.'/compta/bank/line.php?rowid='.$objp->rowid.'&account='.$object->id.'&backtopage='.urlencode($_SERVER["PHP_SELF"].'?account='.$object->id.'&num='.$numref).'">';
  598. print img_edit();
  599. print "</a></td>";
  600. } else {
  601. print "<td class=\"center\">&nbsp;</td>";
  602. }
  603. print "</tr>";
  604. $i++;
  605. }
  606. $db->free($result);
  607. } else {
  608. dol_print_error($db);
  609. }
  610. // Line Total
  611. print "\n".'<tr class="liste_total"><td class="right" colspan="4">'.$langs->trans("Total").' :</td><td class="right">'.price($totald).'</td><td class="right">'.price($totalc)."</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
  612. // Line Balance
  613. print "\n<tr>";
  614. print "<td class=\"right\" colspan=\"3\">&nbsp;</td><td colspan=\"3\"><b>".$langs->trans("EndBankBalance")." :</b></td>";
  615. print '<td class="right"><b>'.price(price2num($total, 'MT'))."</b></td><td>&nbsp;</td>";
  616. print "</tr>\n";
  617. print "</table>";
  618. // Code to adjust value date with plus and less picto using an Ajax call instead of a full reload of page
  619. $urlajax = DOL_URL_ROOT.'/core/ajax/bankconciliate.php?token='.currentToken();
  620. print '
  621. <script type="text/javascript">
  622. $(function() {
  623. $("a.ajaxforbankoperationchange").each(function(){
  624. var current = $(this);
  625. current.click(function()
  626. {
  627. console.log("We click on ajaxforbankoperationchange");
  628. var url = "'.$urlajax.'&"+current.attr("href").split("?")[1];
  629. $.get(url, function(data)
  630. {
  631. console.log(url)
  632. console.log(data)
  633. current.parent().parent().find(".spanforajaxedit").replaceWith(data);
  634. });
  635. return false;
  636. });
  637. });
  638. });
  639. </script>
  640. ';
  641. print "</div>";
  642. print "</form>\n";
  643. }
  644. // End of page
  645. llxFooter();
  646. $db->close();