bankentries.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. <?php
  2. /* Copyright (C) 2001-2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2012 Vinícius Nogueira <viniciusvgn@gmail.com>
  6. * Copyright (C) 2014 Florian Henry <florian.henry@open-cooncept.pro>
  7. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  8. * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
  9. * Copyright (C) 2017 Alexandre Spangaro <aspangaro@zendsi.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/compta/bank/bankentries.php
  26. * \ingroup banque
  27. * \brief List of bank transactions
  28. */
  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.'/user/class/user.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/bankcateg.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  38. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/compta/salaries/class/paymentsalary.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
  43. require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
  44. require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
  45. $langs->load("banks");
  46. $langs->load("bills");
  47. $langs->load("categories");
  48. $langs->load("companies");
  49. $langs->load("margins");
  50. $langs->load("salaries");
  51. $langs->load("loan");
  52. $langs->load("donations");
  53. $langs->load("trips");
  54. $langs->load("members");
  55. $langs->load("compta");
  56. $id = GETPOST('id','int');
  57. $ref = GETPOST('ref','alpha');
  58. $action=GETPOST('action','alpha');
  59. $cancel=GETPOST('cancel','alpha');
  60. $confirm=GETPOST('confirm','alpha');
  61. // Security check
  62. $fieldvalue = (! empty($id) ? $id : (! empty($ref) ? $ref :''));
  63. $fieldtype = (! empty($ref) ? 'ref' :'rowid');
  64. if ($fielvalue)
  65. {
  66. if ($user->societe_id) $socid=$user->societe_id;
  67. $result=restrictedArea($user,'banque',$fieldvalue,'bank_account&bank_account','','',$fieldtype);
  68. }
  69. else
  70. {
  71. if ($user->societe_id) $socid=$user->societe_id;
  72. $result=restrictedArea($user,'banque');
  73. }
  74. $search_ref=GETPOST('search_ref','alpha');
  75. $description=GETPOST("description",'alpha');
  76. $debit=GETPOST("debit",'alpha');
  77. $credit=GETPOST("credit",'alpha');
  78. $type=GETPOST("type",'alpha');
  79. $account=GETPOST("account",'int');
  80. $bid=GETPOST("bid","int");
  81. $search_dt_start = dol_mktime(0, 0, 0, GETPOST('search_start_dtmonth', 'int'), GETPOST('search_start_dtday', 'int'), GETPOST('search_start_dtyear', 'int'));
  82. $search_dt_end = dol_mktime(0, 0, 0, GETPOST('search_end_dtmonth', 'int'), GETPOST('search_end_dtday', 'int'), GETPOST('search_end_dtyear', 'int'));
  83. $search_dv_start = dol_mktime(0, 0, 0, GETPOST('search_start_dvmonth', 'int'), GETPOST('search_start_dvday', 'int'), GETPOST('search_start_dvyear', 'int'));
  84. $search_dv_end = dol_mktime(0, 0, 0, GETPOST('search_end_dvmonth', 'int'), GETPOST('search_end_dvday', 'int'), GETPOST('search_end_dvyear', 'int'));
  85. $search_thirdparty=GETPOST("thirdparty",'alpha');
  86. $search_req_nb=GETPOST("req_nb",'alpha');
  87. $search_num_releve=GETPOST("search_num_releve",'alpha');
  88. $search_conciliated=GETPOST("search_conciliated",'int');
  89. $num_releve=GETPOST("num_releve");
  90. $cat=GETPOST("cat");
  91. $limit = GETPOST('limit')?GETPOST('limit','int'):$conf->liste_limit;
  92. $sortfield = GETPOST("sortfield",'alpha');
  93. $sortorder = GETPOST("sortorder",'alpha');
  94. $page = GETPOST("page",'int');
  95. $pageplusone = GETPOST("pageplusone",'int');
  96. if ($pageplusone) $page = $pageplusone - 1;
  97. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  98. $offset = $limit * $page;
  99. $pageprev = $page - 1;
  100. $pagenext = $page + 1;
  101. if (! $sortorder) $sortorder='ASC';
  102. if (! $sortfield) $sortfield='b.datev';
  103. $mode_balance_ok=false;
  104. //if (($sortfield == 'b.datev' || $sortfield == 'b.datev, b.dateo, b.rowid')) // TODO Manage balance when account not selected
  105. if (($sortfield == 'b.datev' || $sortfield == 'b.datev, b.dateo, b.rowid'))
  106. {
  107. $sortfield = 'b.datev, b.dateo, b.rowid';
  108. if ($id > 0 || ! empty($ref) || $account > 0) $mode_balance_ok = true;
  109. }
  110. if (strtolower($sortorder) == 'desc') $mode_balance_ok = false;
  111. $object = new Account($db);
  112. if ($id > 0 || ! empty($ref))
  113. {
  114. $result=$object->fetch($id, $ref);
  115. $account = $object->id; // Force the search field on id of account
  116. }
  117. // Initialize technical object to manage context to save list fields
  118. $contextpage='banktransactionlist'.(empty($object->ref)?'':'-'.$object->id);
  119. //var_dump($contextpage);
  120. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  121. $hookmanager->initHooks(array('banktransactionlist', $contextpage));
  122. $extrafields = new ExtraFields($db);
  123. // fetch optionals attributes and labels
  124. $extralabels = $extrafields->fetch_name_optionals_label('banktransaction');
  125. $search_array_options=$extrafields->getOptionalsFromPost($extralabels,'','search_');
  126. $arrayfields=array(
  127. 'b.rowid'=>array('label'=>$langs->trans("Ref"), 'checked'=>1),
  128. 'description'=>array('label'=>$langs->trans("Description"), 'checked'=>1),
  129. 'b.dateo'=>array('label'=>$langs->trans("DateOperationShort"), 'checked'=>1),
  130. 'b.datev'=>array('label'=>$langs->trans("DateValueShort"), 'checked'=>1),
  131. 'type'=>array('label'=>$langs->trans("Type"), 'checked'=>1),
  132. 'b.num_chq'=>array('label'=>$langs->trans("Numero"), 'checked'=>1),
  133. 'bu.label'=>array('label'=>$langs->trans("ThirdParty"), 'checked'=>1, 'position'=>500),
  134. 'ba.ref'=>array('label'=>$langs->trans("BankAccount"), 'checked'=>(($id > 0 || ! empty($ref))?0:1), 'position'=>1000),
  135. 'b.debit'=>array('label'=>$langs->trans("Debit"), 'checked'=>1, 'position'=>600),
  136. 'b.credit'=>array('label'=>$langs->trans("Credit"), 'checked'=>1, 'position'=>605),
  137. 'balance'=>array('label'=>$langs->trans("Balance"), 'checked'=>1, 'position'=>1000),
  138. 'b.num_releve'=>array('label'=>$langs->trans("AccountStatement"), 'checked'=>1, 'position'=>1010),
  139. 'b.conciliated'=>array('label'=>$langs->trans("Conciliated"), 'enabled'=> $object->rappro, 'checked'=>($action == 'reconcile'?1:0), 'position'=>1020),
  140. );
  141. // Extra fields
  142. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label))
  143. {
  144. foreach($extrafields->attribute_label as $key => $val)
  145. {
  146. $arrayfields["ef.".$key]=array('label'=>$extrafields->attribute_label[$key], 'checked'=>$extrafields->attribute_list[$key], 'position'=>$extrafields->attribute_pos[$key], 'enabled'=>$extrafields->attribute_perms[$key]);
  147. }
  148. }
  149. /*
  150. * Actions
  151. */
  152. if (GETPOST('cancel')) { $action='list'; $massaction=''; }
  153. if (! GETPOST('confirmmassaction') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
  154. $parameters=array();
  155. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  156. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  157. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  158. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETPOST("button_removefilter")) // All tests are required to be compatible with all browsers
  159. {
  160. $search_dt_start='';
  161. $search_dt_end='';
  162. $search_dv_start='';
  163. $search_dv_end='';
  164. $description="";
  165. $type="";
  166. $debit="";
  167. $credit="";
  168. $bid="";
  169. $search_ref="";
  170. $search_req_nb='';
  171. $search_thirdparty='';
  172. $search_num_releve='';
  173. $search_conciliated='';
  174. $thirdparty='';
  175. $account="";
  176. if ($id > 0 || ! empty($ref)) $account=$object->id;
  177. }
  178. if (empty($reshook))
  179. {
  180. $objectclass='Account';
  181. $objectlabel='BankTransaction';
  182. $permtoread = $user->rights->banque->lire;
  183. $permtodelete = $user->rights->banque->supprimer;
  184. $uploaddir = $conf->bank->dir_output;
  185. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
  186. }
  187. // Conciliation
  188. if (GETPOST('confirm_reconcile') && $user->rights->banque->consolidate)
  189. {
  190. $error=0;
  191. // Definition, nettoyage parametres
  192. $num_releve=trim(GETPOST("num_releve"));
  193. if ($num_releve)
  194. {
  195. $bankline=new AccountLine($db);
  196. if (isset($_POST['rowid']) && is_array($_POST['rowid']))
  197. {
  198. foreach($_POST['rowid'] as $row)
  199. {
  200. if ($row > 0)
  201. {
  202. $result=$bankline->fetch($row);
  203. $bankline->num_releve=$num_releve; //$_POST["num_releve"];
  204. $result=$bankline->update_conciliation($user, GETPOST("cat"));
  205. if ($result < 0)
  206. {
  207. setEventMessages($bankline->error, $bankline->errors, 'errors');
  208. $error++;
  209. break;
  210. }
  211. }
  212. }
  213. }
  214. else
  215. {
  216. $error++;
  217. $langs->load("errors");
  218. setEventMessages($langs->trans("NoRecordSelected"), null, 'errors');
  219. }
  220. }
  221. else
  222. {
  223. $error++;
  224. $langs->load("errors");
  225. setEventMessages($langs->trans("ErrorPleaseTypeBankTransactionReportName"), null, 'errors');
  226. }
  227. if (! $error)
  228. {
  229. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$id); // To avoid to submit twice and allow back
  230. exit;
  231. }
  232. }
  233. $dateop=-1;
  234. if (GETPOST('save') && $id && ! $cancel && $user->rights->banque->modifier)
  235. {
  236. $error = 0;
  237. if (price2num($_POST["addcredit"]) > 0)
  238. {
  239. $amount = price2num($_POST["addcredit"]);
  240. }
  241. else
  242. {
  243. $amount = - price2num($_POST["adddebit"]);
  244. }
  245. $dateop = dol_mktime(12,0,0,$_POST["opmonth"],$_POST["opday"],$_POST["opyear"]);
  246. $operation=$_POST["operation"];
  247. $num_chq=$_POST["num_chq"];
  248. $label=$_POST["label"];
  249. $cat1=$_POST["cat1"];
  250. if (! $dateop) {
  251. $error++;
  252. setEventMessages($langs->trans("ErrorFieldRequired", $langs->trans("Date")), null, 'errors');
  253. }
  254. if (! $operation) {
  255. $error++;
  256. setEventMessages($langs->trans("ErrorFieldRequired", $langs->trans("Type")), null, 'errors');
  257. }
  258. if (! $amount) {
  259. $error++;
  260. setEventMessages($langs->trans("ErrorFieldRequired", $langs->trans("Amount")), null, 'errors');
  261. }
  262. if (! $error)
  263. {
  264. $object->fetch($id);
  265. $insertid = $object->addline($dateop, $operation, $label, $amount, $num_chq, ($cat1 > 0 ? $cat1 : 0), $user);
  266. if ($insertid > 0)
  267. {
  268. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  269. header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
  270. exit;
  271. }
  272. else
  273. {
  274. setEventMessages($object->error, $object->errors, 'errors');
  275. }
  276. }
  277. else
  278. {
  279. $action='addline';
  280. }
  281. }
  282. if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->banque->modifier)
  283. {
  284. $accline=new AccountLine($db);
  285. $result=$accline->fetch(GETPOST("rowid"));
  286. $result=$accline->delete($user);
  287. }
  288. /*
  289. * View
  290. */
  291. $form = new Form($db);
  292. $formother = new FormOther($db);
  293. $companystatic=new Societe($db);
  294. $bankaccountstatic=new Account($db);
  295. $societestatic=new Societe($db);
  296. $userstatic=new User($db);
  297. $chargestatic=new ChargeSociales($db);
  298. $loanstatic=new Loan($db);
  299. $memberstatic=new Adherent($db);
  300. $paymentstatic=new Paiement($db);
  301. $paymentsupplierstatic=new PaiementFourn($db);
  302. $paymentvatstatic=new TVA($db);
  303. $paymentsalstatic=new PaymentSalary($db);
  304. $donstatic=new Don($db);
  305. $paymentexpensereportstatic=new PaymentExpenseReport($db);
  306. $bankstatic=new Account($db);
  307. $banklinestatic=new AccountLine($db);
  308. $now = dol_now();
  309. // Must be before button action
  310. $param='';
  311. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  312. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  313. if ($id > 0) $param.='&id='.urlencode($id);
  314. if (!empty($ref)) $param.='&ref='.urlencode($ref);
  315. if (!empty($search_ref)) $param.='&search_ref='.urlencode($search_ref);
  316. if (!empty($description)) $param.='&description='.urlencode($description);
  317. if (!empty($type)) $param.='&type='.urlencode($type);
  318. if (!empty($debit)) $param.='&debit='.$debit;
  319. if (!empty($credit)) $param.='&credit='.$credit;
  320. if (!empty($account)) $param.='&account='.$account;
  321. if (!empty($search_num_releve)) $param.='&search_num_releve='.urlencode($search_num_releve);
  322. if (!empty($bid)) $param.='&bid='.$bid;
  323. if (dol_strlen($search_dt_start) > 0) $param .= '&search_start_dtmonth=' . GETPOST('search_start_dtmonth', 'int') . '&search_start_dtday=' . GETPOST('search_start_dtday', 'int') . '&search_start_dtyear=' . GETPOST('search_start_dtyear', 'int');
  324. if (dol_strlen($search_dt_end) > 0) $param .= '&search_end_dtmonth=' . GETPOST('search_end_dtmonth', 'int') . '&search_end_dtday=' . GETPOST('search_end_dtday', 'int') . '&search_end_dtyear=' . GETPOST('search_end_dtyear', 'int');
  325. if (dol_strlen($search_dv_start) > 0) $param .= '&search_start_dvmonth=' . GETPOST('search_start_dvmonth', 'int') . '&search_start_dvday=' . GETPOST('search_start_dvday', 'int') . '&search_start_dvyear=' . GETPOST('search_start_dvyear', 'int');
  326. if (dol_strlen($search_dv_end) > 0) $param .= '&search_end_dvmonth=' . GETPOST('search_end_dvmonth', 'int') . '&search_end_dvday=' . GETPOST('search_end_dvday', 'int') . '&search_end_dvyear=' . GETPOST('search_end_dvyear', 'int');
  327. if ($search_req_nb) $param.='&amp;req_nb='.urlencode($search_req_nb);
  328. if (GETPOST("thirdparty")) $param.='&amp;thirdparty='.urlencode(GETPOST("thirdparty"));
  329. if ($optioncss != '') $param.='&optioncss='.$optioncss;
  330. // Add $param from extra fields
  331. foreach ($search_array_options as $key => $val)
  332. {
  333. $crit=$val;
  334. $tmpkey=preg_replace('/search_options_/','',$key);
  335. if ($val != '') $param.='&search_options_'.$tmpkey.'='.urlencode($val);
  336. }
  337. if ($id > 0 || ! empty($ref))
  338. {
  339. $title = $langs->trans("FinancialAccount").' - '.$langs->trans("Transactions");
  340. $helpurl = "";
  341. llxHeader('',$title,$helpurl);
  342. // Load bank groups
  343. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/bankcateg.class.php';
  344. $bankcateg = new BankCateg($db);
  345. $options = array();
  346. foreach ($bankcateg->fetchAll() as $bankcategory) {
  347. $options[$bankcategory->id] = $bankcategory->label;
  348. }
  349. // Bank card
  350. $head=bank_prepare_head($object);
  351. dol_fiche_head($head,'journal',$langs->trans("FinancialAccount"),0,'account');
  352. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/index.php">'.$langs->trans("BackToList").'</a>';
  353. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
  354. dol_fiche_end();
  355. /*
  356. * Buttons actions
  357. */
  358. if ($action != 'reconcile')
  359. {
  360. print '<div class="tabsAction">';
  361. if (empty($conf->global->BANK_DISABLE_DIRECT_INPUT))
  362. {
  363. if ($user->rights->banque->modifier) {
  364. print '<a class="butAction" href="'.DOL_URL_ROOT.'/compta/bank/various_payment/card.php?action=create&accountid='.$account.'">'.$langs->trans("AddBankRecord").'</a>';
  365. } else {
  366. print '<a class="butActionRefused" title="'.$langs->trans("NotEnoughPermissions").'" href="#">'.$langs->trans("AddBankRecord").'</a>';
  367. }
  368. }
  369. else
  370. {
  371. print '<a class="butActionRefused" title="'.$langs->trans("FeatureDisabled").'" href="#">'.$langs->trans("AddBankRecord").'</a>';
  372. }
  373. if ($object->canBeConciliated() > 0) {
  374. // If not cash account and can be reconciliate
  375. if ($user->rights->banque->consolidate) {
  376. print '<a class="butAction" href="'.DOL_URL_ROOT.'/compta/bank/bankentries.php?action=reconcile'.$param.'">'.$langs->trans("Conciliate").'</a>';
  377. } else {
  378. print '<a class="butActionRefused" title="'.$langs->trans("NotEnoughPermissions").'" href="#">'.$langs->trans("Conciliate").'</a>';
  379. }
  380. }
  381. print '</div>';
  382. }
  383. }
  384. else
  385. {
  386. llxHeader('', $langs->trans("BankTransactions"), '', '', 0, 0, array(), array(), $param);
  387. }
  388. $sql = "SELECT b.rowid, b.dateo as do, b.datev as dv, b.amount, b.label, b.rappro as conciliated, b.num_releve, b.num_chq,";
  389. $sql.= " b.fk_account, b.fk_type,";
  390. $sql.= " ba.rowid as bankid, ba.ref as bankref,";
  391. $sql.= " bu.url_id,";
  392. $sql.= " s.nom, s.name_alias, s.client, s.fournisseur, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur";
  393. // Add fields from extrafields
  394. foreach ($extrafields->attribute_label as $key => $val) $sql.=($extrafields->attribute_type[$key] != 'separate' ? ",ef.".$key.' as options_'.$key : '');
  395. // Add fields from hooks
  396. $parameters=array();
  397. $reshook=$hookmanager->executeHooks('printFieldListSelect',$parameters); // Note that $action and $object may have been modified by hook
  398. $sql.=$hookmanager->resPrint;
  399. $sql.= " FROM ";
  400. if ($bid) $sql.= MAIN_DB_PREFIX."bank_class as l,";
  401. $sql.= " ".MAIN_DB_PREFIX."bank_account as ba,";
  402. $sql.= " ".MAIN_DB_PREFIX."bank as b";
  403. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label)) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank_extrafields as ef on (b.rowid = ef.fk_object)";
  404. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON bu.fk_bank = b.rowid AND type = 'company'";
  405. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON bu.url_id = s.rowid";
  406. $sql.= " WHERE b.fk_account = ba.rowid";
  407. $sql.= " AND ba.entity IN (".getEntity('bank_account').")";
  408. if ($account > 0) $sql.=" AND b.fk_account = ".$account;
  409. // Search period criteria
  410. if (dol_strlen($search_dt_start)>0) $sql .= " AND b.dateo >= '" . $db->idate($search_dt_start) . "'";
  411. if (dol_strlen($search_dt_end)>0) $sql .= " AND b.dateo <= '" . $db->idate($search_dt_end) . "'";
  412. // Search period criteria
  413. if (dol_strlen($search_dv_start)>0) $sql .= " AND b.datev >= '" . $db->idate($search_dv_start) . "'";
  414. if (dol_strlen($search_dv_end)>0) $sql .= " AND b.datev <= '" . $db->idate($search_dv_end) . "'";
  415. if ($search_ref) $sql.=natural_search("b.rowid", $search_ref);
  416. if ($search_req_nb) $sql.= natural_search("b.num_chq", $search_req_nb);
  417. if ($search_num_releve) $sql.= natural_search("b.num_releve", $search_num_releve);
  418. if ($search_conciliated != '' && $search_conciliated != '-1') $sql.= " AND b.rappro = ".$search_conciliated;
  419. if ($search_thirdparty) $sql.= natural_search("s.nom", $search_thirdparty);
  420. if ($description) $sql.= natural_search("b.label", $description); // Warning some text are just translation keys, not translated strings
  421. if ($bid) $sql.= " AND b.rowid=l.lineid AND l.fk_categ=".$bid;
  422. if (! empty($type)) $sql.= " AND b.fk_type = '".$db->escape($type)."' ";
  423. // Search criteria amount
  424. $debit = price2num(str_replace('-','',$debit));
  425. $credit = price2num(str_replace('-','',$credit));
  426. if ($debit) $sql.= natural_search('- b.amount', $debit, 1);
  427. if ($credit) $sql.= natural_search('b.amount', $credit, 1);
  428. // Add where from extra fields
  429. foreach ($search_array_options as $key => $val)
  430. {
  431. $crit=$val;
  432. $tmpkey=preg_replace('/search_options_/','',$key);
  433. $typ=$extrafields->attribute_type[$tmpkey];
  434. $mode=0;
  435. if (in_array($typ, array('int','double'))) $mode=1; // Search on a numeric
  436. if ($val && ( ($crit != '' && ! in_array($typ, array('select'))) || ! empty($crit)))
  437. {
  438. $sql .= natural_search('ef.'.$tmpkey, $crit, $mode);
  439. }
  440. }
  441. // Add where from hooks
  442. $parameters=array();
  443. $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
  444. $sql.=$hookmanager->resPrint;
  445. $sql.= $db->order($sortfield,$sortorder);
  446. $nbtotalofrecords = '';
  447. $nbtotalofpages = 0;
  448. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  449. {
  450. $result = $db->query($sql);
  451. $nbtotalofrecords = $db->num_rows($result);
  452. $nbtotalofpages = ceil($nbtotalofrecords/$limit);
  453. }
  454. if (($id > 0 || ! empty($ref)) && ((string) $page == ''))
  455. {
  456. // We open a list of transaction of a dedicated account and no page was set by defaut
  457. // We force on last page.
  458. $page = ($nbtotalofpages - 1);
  459. $offset = $limit * $page;
  460. if ($page < 0) $page = 0;
  461. }
  462. if ($page >= $nbtotalofpages)
  463. {
  464. // If we made a search and result has low page than the page number we were on
  465. $page = ($nbtotalofpages -1);
  466. $offset = $limit * $page;
  467. if ($page < 0) $page = 0;
  468. }
  469. // If not account defined $mode_balance_ok=false
  470. if (empty($account)) $mode_balance_ok=false;
  471. // If a search is done $mode_balance_ok=false
  472. if (! empty($search_ref)) $mode_balance_ok=false;
  473. if (! empty($req_nb)) $mode_balance_ok=false;
  474. if (! empty($type)) $mode_balance_ok=false;
  475. if (! empty($debit)) $mode_balance_ok=false;
  476. if (! empty($credit)) $mode_balance_ok=false;
  477. if (! empty($thirdparty)) $mode_balance_ok=false;
  478. $sql.= $db->plimit($limit+1,$offset);
  479. dol_syslog('compta/bank/bankentries.php', LOG_DEBUG);
  480. $resql = $db->query($sql);
  481. if ($resql)
  482. {
  483. $var=True;
  484. $num = $db->num_rows($resql);
  485. $arrayofselected=is_array($toselect)?$toselect:array();
  486. // List of mass actions available
  487. $arrayofmassactions = array(
  488. //'presend'=>$langs->trans("SendByMail"),
  489. //'builddoc'=>$langs->trans("PDFMerge"),
  490. );
  491. //if ($user->rights->bank->supprimer) $arrayofmassactions['delete']=$langs->trans("Delete");
  492. if ($massaction == 'presend') $arrayofmassactions=array();
  493. $massactionbutton=$form->selectMassAction('', $arrayofmassactions);
  494. // Confirmation delete
  495. if ($action == 'delete')
  496. {
  497. $text=$langs->trans('ConfirmDeleteTransaction');
  498. print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id.'&rowid='.GETPOST("rowid"), $langs->trans('DeleteTransaction'), $text, 'confirm_delete', null, '', 1);
  499. }
  500. // Lines of title fields
  501. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'" name="search_form">'."\n";
  502. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  503. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  504. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  505. print '<input type="hidden" name="action" value="'.($action?$action:'search').'">';
  506. print '<input type="hidden" name="view" value="'.dol_escape_htmltag($view).'">';
  507. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  508. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  509. print '<input type="hidden" name="page" value="'.$page.'">';
  510. print '<input type="hidden" name="id" value="'.$id.'">';
  511. print '<input type="hidden" name="ref" value="'.$ref.'">';
  512. if (GETPOST('bid')) print '<input type="hidden" name="bid" value="'.GETPOST("bid").'">';
  513. // Form to reconcile
  514. if ($user->rights->banque->consolidate && $action == 'reconcile')
  515. {
  516. // print '<table class="noborder" width="100%">';
  517. // print '<tr '.$bcnd[false].'>';
  518. // print '<td>';
  519. print '<div class="valignmiddle inline-block" style="padding-right: 20px;">';
  520. print '<strong>'.$langs->trans("InputReceiptNumber").'</strong>: ';
  521. print '<input class="flat" name="num_releve" type="text" value="'.(GETPOST('num_releve')?GETPOST('num_releve'):'').'" size="10">'; // The only default value is value we just entered
  522. print '</div>';
  523. if ($options) {
  524. print $langs->trans("EventualyAddCategory").': ';
  525. print Form::selectarray('cat', $options, GETPOST('cat'), 1);
  526. }
  527. print '<br>'.$langs->trans("ThenCheckLinesAndConciliate").' ';
  528. print '<input class="button" name="confirm_reconcile" type="submit" value="'.$langs->trans("Conciliate").'">';
  529. print ' '.$langs->trans("or").' ';
  530. print '<input type="submit" name="cancel" class="button" value="'.$langs->trans("Cancel").'">';
  531. // Show last bank statements
  532. $nbmax=15; // We accept to show last 15 receipts (so we can have more than one year)
  533. $liste="";
  534. $sql = "SELECT DISTINCT num_releve FROM ".MAIN_DB_PREFIX."bank";
  535. $sql.= " WHERE fk_account=".$id." AND num_releve IS NOT NULL";
  536. $sql.= $db->order("num_releve","DESC");
  537. $sql.= $db->plimit($nbmax+1);
  538. print '<br><br>';
  539. print $langs->trans("LastAccountStatements").' : ';
  540. $resqlr=$db->query($sql);
  541. if ($resqlr)
  542. {
  543. $numr=$db->num_rows($resqlr);
  544. $i=0;
  545. $last_ok=0;
  546. while (($i < $numr) && ($i < $nbmax))
  547. {
  548. $objr = $db->fetch_object($resqlr);
  549. if (! $last_ok) {
  550. $last_releve = $objr->num_releve;
  551. $last_ok=1;
  552. }
  553. $i++;
  554. $liste='<a href="'.DOL_URL_ROOT.'/compta/bank/releve.php?account='.$id.'&amp;num='.$objr->num_releve.'">'.$objr->num_releve.'</a> &nbsp; '.$liste;
  555. }
  556. if ($numr >= $nbmax) $liste="... &nbsp; ".$liste;
  557. print $liste;
  558. if ($numr <= 0) print '<b>'.$langs->trans("None").'</b>';
  559. }
  560. else
  561. {
  562. dol_print_error($db);
  563. }
  564. print '<br><br>';
  565. // print '</td></tr></table>';
  566. }
  567. /// ajax to adjust value date with plus and less picto
  568. print '
  569. <script type="text/javascript">
  570. $(function() {
  571. $("a.ajax").each(function(){
  572. var current = $(this);
  573. current.click(function()
  574. {
  575. $.get("'.DOL_URL_ROOT.'/core/ajax/bankconciliate.php?"+current.attr("href").split("?")[1], function(data)
  576. {
  577. console.log(data)
  578. current.parent().prev().replaceWith(data);
  579. });
  580. return false;
  581. });
  582. });
  583. });
  584. </script>
  585. ';
  586. $i = 0;
  587. // Title
  588. $bankcateg=new BankCateg($db);
  589. $morehtml='<div data-role="fieldcontain">';
  590. $morehtml.= '<label for="pageplusone">'.$langs->trans("Page")."</label> "; // ' Page ';
  591. $morehtml.='<input type="text" name="pageplusone" id="pageplusone" size="1" class="flat" value="'.($page+1).'">';
  592. $morehtml.='/'.$nbtotalofpages.' ';
  593. $morehtml.='</div>';
  594. $picto='title_bank';
  595. if ($id > 0 || ! empty($ref)) $picto='';
  596. if (GETPOST("bid"))
  597. {
  598. $result=$bankcateg->fetch(GETPOST("bid"));
  599. print_barre_liste($langs->trans("BankTransactionForCategory",$bankcateg->label).' '.($socid?' '.$soc->name:''), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, $picto, 0, $morehtml, '', $limit);
  600. }
  601. else
  602. {
  603. print_barre_liste($langs->trans("BankTransactions"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, $picto, 0, $morehtml, '', $limit);
  604. }
  605. // We can add page now to param
  606. if ($page != '') $param.='&page='.urlencode($page);
  607. $moreforfilter = '';
  608. $moreforfilter.='<div class="divsearchfield">';
  609. $moreforfilter .= $langs->trans('DateOperationShort').' : ';
  610. $moreforfilter .= '<div class="nowrap'.($conf->browser->layout=='phone'?' centpercent':'').' inline-block">'.$langs->trans('From') . ' ';
  611. $moreforfilter .= $form->select_date($search_dt_start, 'search_start_dt', 0, 0, 1, "search_form", 1, 0, 1).'</div>';
  612. //$moreforfilter .= ' - ';
  613. $moreforfilter .= '<div class="nowrap'.($conf->browser->layout=='phone'?' centpercent':'').' inline-block">'.$langs->trans('to') . ' ' . $form->select_date($search_dt_end, 'search_end_dt', 0, 0, 1, "search_form", 1, 0, 1).'</div>';
  614. $moreforfilter .= '</div>';
  615. $moreforfilter.='<div class="divsearchfield">';
  616. $moreforfilter .= $langs->trans('DateValueShort').' : ';
  617. $moreforfilter .= '<div class="nowrap'.($conf->browser->layout=='phone'?' centpercent':'').' inline-block">'.$langs->trans('From') . ' ';
  618. $moreforfilter .= $form->select_date($search_dv_start, 'search_start_dv', 0, 0, 1, "search_form", 1, 0, 1).'</div>';
  619. //$moreforfilter .= ' - ';
  620. $moreforfilter .= '<div class="nowrap'.($conf->browser->layout=='phone'?' centpercent':'').' inline-block">'.$langs->trans('to') . ' ' . $form->select_date($search_dv_end, 'search_end_dv', 0, 0, 1, "search_form", 1, 0, 1).'</div>';
  621. $moreforfilter .= '</div>';
  622. $parameters=array();
  623. $reshook=$hookmanager->executeHooks('printFieldPreListTitle',$parameters); // Note that $action and $object may have been modified by hook
  624. if (empty($reshook)) $moreforfilter .= $hookmanager->resPrint;
  625. else $moreforfilter = $hookmanager->resPrint;
  626. if ($moreforfilter)
  627. {
  628. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  629. print $moreforfilter;
  630. print '</div>'."\n";
  631. }
  632. $varpage=empty($contextpage)?$_SERVER["PHP_SELF"]:$contextpage;
  633. $selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  634. print '<div class="div-table-responsive">';
  635. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  636. print '<tr class="liste_titre_filter">';
  637. if (! empty($arrayfields['b.rowid']['checked']))
  638. {
  639. print '<td class="liste_titre">';
  640. print '<input type="text" class="flat" name="search_ref" size="2" value="'.dol_escape_htmltag($search_ref).'">';
  641. print '</td>';
  642. }
  643. if (! empty($arrayfields['description']['checked']))
  644. {
  645. print '<td class="liste_titre">';
  646. //print '<input type="text" class="flat" name="description" size="10" value="'.dol_escape_htmltag($description).'">';
  647. print '</td>';
  648. }
  649. if (! empty($arrayfields['b.dateo']['checked']))
  650. {
  651. print '<td class="liste_titre">&nbsp;</td>';
  652. }
  653. if (! empty($arrayfields['b.datev']['checked']))
  654. {
  655. print '<td class="liste_titre">&nbsp;</td>';
  656. }
  657. if (! empty($arrayfields['type']['checked']))
  658. {
  659. print '<td class="liste_titre" align="center">';
  660. $form->select_types_paiements(empty($type)?'':$type, 'type', '', 2, 0, 1, 0, 1, 'maxwidth100');
  661. print '</td>';
  662. }
  663. if (! empty($arrayfields['b.num_chq']['checked']))
  664. {
  665. // Numero
  666. print '<td class="liste_titre" align="center"><input type="text" class="flat" name="req_nb" value="'.dol_escape_htmltag($search_req_nb).'" size="2"></td>';
  667. }
  668. if (! empty($arrayfields['bu.label']['checked']))
  669. {
  670. print '<td class="liste_titre"><input type="text" class="flat" name="thirdparty" value="'.dol_escape_htmltag($search_thirdparty).'" size="10"></td>';
  671. }
  672. if (! empty($arrayfields['ba.ref']['checked']))
  673. {
  674. print '<td class="liste_titre" align="right">';
  675. $form->select_comptes($account,'account',0,'',1, ($id > 0 || ! empty($ref)?' disabled="disabled"':''));
  676. print '</td>';
  677. }
  678. if (! empty($arrayfields['b.debit']['checked']))
  679. {
  680. print '<td class="liste_titre" align="right">';
  681. print '<input type="text" class="flat" name="debit" size="4" value="'.dol_escape_htmltag($debit).'">';
  682. print '</td>';
  683. }
  684. if (! empty($arrayfields['b.credit']['checked']))
  685. {
  686. print '<td class="liste_titre" align="right">';
  687. print '<input type="text" class="flat" name="credit" size="4" value="'.dol_escape_htmltag($credit).'">';
  688. print '</td>';
  689. }
  690. if (! empty($arrayfields['balance']['checked']))
  691. {
  692. print '<td class="liste_titre" align="right">';
  693. $htmltext=$langs->trans("BalanceVisibilityDependsOnSortAndFilters", $langs->transnoentitiesnoconv("DateValue"));
  694. print $form->textwithpicto('', $htmltext, 1);
  695. print '</td>';
  696. }
  697. // Numero statement
  698. if (! empty($arrayfields['b.num_releve']['checked']))
  699. {
  700. print '<td class="liste_titre" align="center"><input type="text" class="flat" name="search_num_releve" value="'.dol_escape_htmltag($search_num_releve).'" size="3"></td>';
  701. }
  702. // Conciliated
  703. if (! empty($arrayfields['b.conciliated']['checked']))
  704. {
  705. print '<td class="liste_titre" align="center">';
  706. print $form->selectyesno('search_conciliated', $search_conciliated, 1, False, 1);
  707. print '</td>';
  708. }
  709. print '<td class="liste_titre" align="middle">';
  710. print '</td>';
  711. print '<td class="liste_titre" align="middle">';
  712. $searchpicto=$form->showFilterAndCheckAddButtons($massactionbutton?1:0, 'checkforselect', 1);
  713. print $searchpicto;
  714. print '</td>';
  715. print "</tr>\n";
  716. // Fields title
  717. print '<tr class="liste_titre">';
  718. if (! empty($arrayfields['b.rowid']['checked'])) print_liste_field_titre($arrayfields['b.rowid']['label'],$_SERVER['PHP_SELF'],'b.rowid','',$param,'',$sortfield,$sortorder);
  719. if (! empty($arrayfields['description']['checked'])) print_liste_field_titre($arrayfields['description']['label'],$_SERVER['PHP_SELF'],'','',$param,'',$sortfield,$sortorder);
  720. if (! empty($arrayfields['b.dateo']['checked'])) print_liste_field_titre($arrayfields['b.dateo']['label'],$_SERVER['PHP_SELF'],'b.dateo','',$param,'align="center"',$sortfield,$sortorder);
  721. if (! empty($arrayfields['b.datev']['checked'])) print_liste_field_titre($arrayfields['b.datev']['label'],$_SERVER['PHP_SELF'],'b.datev, b.dateo, b.rowid','',$param,'align="center"',$sortfield,$sortorder);
  722. if (! empty($arrayfields['type']['checked'])) print_liste_field_titre($arrayfields['type']['label'],$_SERVER['PHP_SELF'],'','',$param,'align="center"',$sortfield,$sortorder);
  723. if (! empty($arrayfields['b.num_chq']['checked'])) print_liste_field_titre($arrayfields['b.num_chq']['label'],$_SERVER['PHP_SELF'],'b.num_chq','',$param,'align="center"',$sortfield,$sortorder);
  724. if (! empty($arrayfields['bu.label']['checked'])) print_liste_field_titre($arrayfields['bu.label']['label'],$_SERVER['PHP_SELF'],'bu.label','',$param,'',$sortfield,$sortorder);
  725. if (! empty($arrayfields['ba.ref']['checked'])) print_liste_field_titre($arrayfields['ba.ref']['label'],$_SERVER['PHP_SELF'],'ba.ref','',$param,'align="right"',$sortfield,$sortorder);
  726. if (! empty($arrayfields['b.debit']['checked'])) print_liste_field_titre($arrayfields['b.debit']['label'],$_SERVER['PHP_SELF'],'b.amount','',$param,'align="right"',$sortfield,$sortorder);
  727. if (! empty($arrayfields['b.credit']['checked'])) print_liste_field_titre($arrayfields['b.credit']['label'],$_SERVER['PHP_SELF'],'b.amount','',$param,'align="right"',$sortfield,$sortorder);
  728. if (! empty($arrayfields['balance']['checked'])) print_liste_field_titre($arrayfields['balance']['label'],$_SERVER['PHP_SELF'],'','',$param,'align="right"',$sortfield,$sortorder);
  729. if (! empty($arrayfields['b.num_releve']['checked'])) print_liste_field_titre($arrayfields['b.num_releve']['label'],$_SERVER['PHP_SELF'],'b.num_releve','',$param,'align="center"',$sortfield,$sortorder);
  730. if (! empty($arrayfields['b.conciliated']['checked'])) print_liste_field_titre($arrayfields['b.conciliated']['label'],$_SERVER['PHP_SELF'],'b.rappro','',$param,'align="center"',$sortfield,$sortorder);
  731. // Extra fields
  732. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label))
  733. {
  734. foreach($extrafields->attribute_label as $key => $val)
  735. {
  736. if (! empty($arrayfields["ef.".$key]['checked']))
  737. {
  738. $align=$extrafields->getAlignFlag($key);
  739. print_liste_field_titre($langs->trans($extralabels[$key]),$_SERVER["PHP_SELF"],"ef.".$key,"",$param,($align?'align="'.$align.'"':''),$sortfield,$sortorder);
  740. }
  741. }
  742. }
  743. // Hook fields
  744. $parameters=array('arrayfields'=>$arrayfields);
  745. $reshook=$hookmanager->executeHooks('printFieldListTitle',$parameters); // Note that $action and $object may have been modified by hook
  746. print $hookmanager->resPrint;
  747. print_liste_field_titre('', $_SERVER["PHP_SELF"],"",'','','align="right"',$sortfield,$sortorder,'maxwidthsearch ');
  748. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"],"",'','','align="center"',$sortfield,$sortorder,'maxwidthsearch ');
  749. print "</tr>\n";
  750. $balance = 0; // For balance
  751. $balancecalculated = false;
  752. // Loop on each record
  753. $sign = 1;
  754. $totalarray=array();
  755. while ($i < min($num,$limit))
  756. {
  757. $objp = $db->fetch_object($resql);
  758. // If we are in a situation where we need/can show balance, we calculate the start of balance
  759. if (! $balancecalculated && ! empty($arrayfields['balance']['checked']) && $mode_balance_ok)
  760. {
  761. if (! $account)
  762. {
  763. dol_print_error('', 'account is not defined but $mode_balance_ok is true');
  764. exit;
  765. }
  766. //Loop on each record
  767. $sign = 1;
  768. $i = 0;
  769. $sqlforbalance='SELECT SUM(b.amount) as balance';
  770. $sqlforbalance.= " FROM ";
  771. $sqlforbalance.= " ".MAIN_DB_PREFIX."bank_account as ba,";
  772. $sqlforbalance.= " ".MAIN_DB_PREFIX."bank as b";
  773. $sqlforbalance.= " WHERE b.fk_account = ba.rowid";
  774. $sqlforbalance.= " AND ba.entity IN (".getEntity('bank_account').")";
  775. $sqlforbalance.= " AND b.fk_account = ".$account;
  776. $sqlforbalance.= " AND b.datev < '" . $db->idate($db->jdate($objp->dv)) . "'";
  777. $resqlforbalance = $db->query($sqlforbalance);
  778. //print $sqlforbalance;
  779. if ($resqlforbalance)
  780. {
  781. $objforbalance = $db->fetch_object($resqlforbalance);
  782. if ($objforbalance)
  783. {
  784. $balance = $objforbalance->balance;
  785. }
  786. }
  787. else dol_print_error($db);
  788. $balancecalculated=true;
  789. }
  790. $balance = price2num($balance + ($sign * $objp->amount),'MT');
  791. if (empty($cachebankaccount[$objp->bankid]))
  792. {
  793. $bankaccounttmp = new Account($db);
  794. $bankaccounttmp->fetch($objp->bankid);
  795. $cachebankaccount[$objp->bankid]=$bankaccounttmp;
  796. $bankaccount = $bankaccounttmp;
  797. }
  798. else
  799. {
  800. $bankaccount = $cachebankaccount[$objp->bankid];
  801. }
  802. print '<tr class="oddeven">';
  803. // Ref
  804. if (! empty($arrayfields['b.rowid']['checked']))
  805. {
  806. print '<td align="left" class="nowrap">';
  807. print "<a href=\"ligne.php?rowid=".$objp->rowid.'">'.img_object($langs->trans("ShowPayment").': '.$objp->rowid, 'account', 'class="classfortooltip"').' '.$objp->rowid."</a> &nbsp; ";
  808. print '</td>';
  809. if (! $i) $totalarray['nbfield']++;
  810. }
  811. // Description
  812. if (! empty($arrayfields['description']['checked']))
  813. {
  814. print "<td>";
  815. //print "<a href=\"ligne.php?rowid=".$objp->rowid."&amp;account=".$objp->fk_account."\">";
  816. $reg=array();
  817. preg_match('/\((.+)\)/i',$objp->label,$reg); // Si texte entoure de parenthee on tente recherche de traduction
  818. if ($reg[1] && $langs->trans($reg[1])!=$reg[1]) print $langs->trans($reg[1]);
  819. else print dol_trunc($objp->label,40);
  820. //print "</a>&nbsp;";
  821. // Add links after description
  822. $links = $bankaccountstatic->get_url($objp->rowid);
  823. $cachebankaccount=array();
  824. foreach($links as $key=>$val)
  825. {
  826. if ($links[$key]['type']=='payment')
  827. {
  828. $paymentstatic->id=$links[$key]['url_id'];
  829. $paymentstatic->ref=$links[$key]['url_id'];
  830. print ' '.$paymentstatic->getNomUrl(2);
  831. }
  832. elseif ($links[$key]['type']=='payment_supplier')
  833. {
  834. $paymentsupplierstatic->id=$links[$key]['url_id'];
  835. $paymentsupplierstatic->ref=$links[$key]['url_id'];
  836. print ' '.$paymentsupplierstatic->getNomUrl(2);
  837. }
  838. elseif ($links[$key]['type']=='payment_sc')
  839. {
  840. print '<a href="'.DOL_URL_ROOT.'/compta/payment_sc/card.php?id='.$links[$key]['url_id'].'">';
  841. print ' '.img_object($langs->trans('ShowPayment'),'payment').' ';
  842. //print $langs->trans("SocialContributionPayment");
  843. print '</a>';
  844. }
  845. elseif ($links[$key]['type']=='payment_vat')
  846. {
  847. $paymentvatstatic->id=$links[$key]['url_id'];
  848. $paymentvatstatic->ref=$links[$key]['url_id'];
  849. print ' '.$paymentvatstatic->getNomUrl(2);
  850. }
  851. elseif ($links[$key]['type']=='payment_salary')
  852. {
  853. $paymentsalstatic->id=$links[$key]['url_id'];
  854. $paymentsalstatic->ref=$links[$key]['url_id'];
  855. print ' '.$paymentsalstatic->getNomUrl(2);
  856. }
  857. elseif ($links[$key]['type']=='payment_loan')
  858. {
  859. print '<a href="'.DOL_URL_ROOT.'/loan/payment/card.php?id='.$links[$key]['url_id'].'">';
  860. print ' '.img_object($langs->trans('ShowPayment'),'payment').' ';
  861. print '</a>';
  862. }
  863. elseif ($links[$key]['type']=='payment_donation')
  864. {
  865. print '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$links[$key]['url_id'].'">';
  866. print ' '.img_object($langs->trans('ShowPayment'),'payment').' ';
  867. print '</a>';
  868. }
  869. elseif ($links[$key]['type']=='payment_expensereport')
  870. {
  871. $paymentexpensereportstatic->id=$links[$key]['url_id'];
  872. $paymentexpensereportstatic->ref=$links[$key]['url_id'];
  873. print ' '.$paymentexpensereportstatic->getNomUrl(2);
  874. }
  875. elseif ($links[$key]['type']=='banktransfert')
  876. {
  877. // Do not show link to transfer since there is no transfer card (avoid confusion). Can already be accessed from transaction detail.
  878. if ($objp->amount > 0)
  879. {
  880. $banklinestatic->fetch($links[$key]['url_id']);
  881. $bankstatic->id=$banklinestatic->fk_account;
  882. $bankstatic->label=$banklinestatic->bank_account_ref;
  883. print ' ('.$langs->trans("TransferFrom").' ';
  884. print $bankstatic->getNomUrl(1,'transactions');
  885. print ' '.$langs->trans("toward").' ';
  886. $bankstatic->id=$objp->bankid;
  887. $bankstatic->label=$objp->bankref;
  888. print $bankstatic->getNomUrl(1,'');
  889. print ')';
  890. }
  891. else
  892. {
  893. $bankstatic->id=$objp->bankid;
  894. $bankstatic->label=$objp->bankref;
  895. print ' ('.$langs->trans("TransferFrom").' ';
  896. print $bankstatic->getNomUrl(1,'');
  897. print ' '.$langs->trans("toward").' ';
  898. $banklinestatic->fetch($links[$key]['url_id']);
  899. $bankstatic->id=$banklinestatic->fk_account;
  900. $bankstatic->label=$banklinestatic->bank_account_ref;
  901. print $bankstatic->getNomUrl(1,'transactions');
  902. print ')';
  903. }
  904. //var_dump($links);
  905. }
  906. elseif ($links[$key]['type']=='company')
  907. {
  908. }
  909. elseif ($links[$key]['type']=='user')
  910. {
  911. }
  912. elseif ($links[$key]['type']=='member')
  913. {
  914. }
  915. elseif ($links[$key]['type']=='sc')
  916. {
  917. }
  918. else
  919. {
  920. // Show link with label $links[$key]['label']
  921. if (! empty($objp->label) && ! empty($links[$key]['label'])) print ' - ';
  922. print '<a href="'.$links[$key]['url'].$links[$key]['url_id'].'">';
  923. if (preg_match('/^\((.*)\)$/i',$links[$key]['label'],$reg))
  924. {
  925. // Label generique car entre parentheses. On l'affiche en le traduisant
  926. if ($reg[1]=='paiement') $reg[1]='Payment';
  927. print ' '.$langs->trans($reg[1]);
  928. }
  929. else
  930. {
  931. print ' '.$links[$key]['label'];
  932. }
  933. print '</a>';
  934. }
  935. }
  936. print '</td>';
  937. if (! $i) $totalarray['nbfield']++;
  938. }
  939. // Date ope
  940. if (! empty($arrayfields['b.dateo']['checked']))
  941. {
  942. print '<td align="center" class="nowrap">'.dol_print_date($db->jdate($objp->do),"day")."</td>\n";
  943. if (! $i) $totalarray['nbfield']++;
  944. }
  945. // Date value
  946. if (! empty($arrayfields['b.datev']['checked']))
  947. {
  948. print '<td align="center" class="nowrap">';
  949. print '<span id="datevalue_'.$objp->rowid.'">'.dol_print_date($db->jdate($objp->dv),"day")."</span>";
  950. print '&nbsp;';
  951. print '<span class="inline-block">';
  952. print '<a class="ajax" href="'.$_SERVER['PHP_SELF'].'?action=dvprev&amp;account='.$objp->bankid.'&amp;rowid='.$objp->rowid.'">';
  953. print img_edit_remove() . "</a> ";
  954. print '<a class="ajax" href="'.$_SERVER['PHP_SELF'].'?action=dvnext&amp;account='.$objp->bankid.'&amp;rowid='.$objp->rowid.'">';
  955. print img_edit_add() ."</a>";
  956. print '</span>';
  957. print "</td>\n";
  958. if (! $i) $totalarray['nbfield']++;
  959. }
  960. // Payment type
  961. if (! empty($arrayfields['type']['checked']))
  962. {
  963. print '<td align="center" class="nowrap">';
  964. $labeltype=($langs->trans("PaymentTypeShort".$objp->fk_type)!="PaymentTypeShort".$objp->fk_type)?$langs->trans("PaymentTypeShort".$objp->fk_type):$langs->getLabelFromKey($db,$objp->fk_type,'c_paiement','code','libelle');
  965. if ($labeltype == 'SOLD') print '&nbsp;'; //$langs->trans("InitialBankBalance");
  966. else print $labeltype;
  967. print "</td>\n";
  968. if (! $i) $totalarray['nbfield']++;
  969. }
  970. // Num cheque
  971. if (! empty($arrayfields['b.num_chq']['checked']))
  972. {
  973. print '<td class="nowrap" align="center">'.($objp->num_chq?$objp->num_chq:"")."</td>\n";
  974. if (! $i) $totalarray['nbfield']++;
  975. }
  976. // Third party
  977. if (! empty($arrayfields['bu.label']['checked']))
  978. {
  979. print "<td>";
  980. if ($objp->url_id)
  981. {
  982. $companystatic->id=$objp->url_id;
  983. $companystatic->name=$objp->nom;
  984. $companystatic->name_alias=$objp->name_alias;
  985. $companystatic->client=$objp->client;
  986. $companystatic->fournisseur=$objp->fournisseur;
  987. $companystatic->code_client=$objp->code_client;
  988. $companystatic->code_fournisseur=$objp->code_fournisseur;
  989. $companystatic->code_compta=$objp->code_compta;
  990. $companystatic->code_compta_fournisseur=$objp->code_compta_fournisseur;
  991. print $companystatic->getNomUrl(1);
  992. }
  993. else
  994. {
  995. print '&nbsp;';
  996. }
  997. print '</td>';
  998. if (! $i) $totalarray['nbfield']++;
  999. }
  1000. // Bank account
  1001. if (! empty($arrayfields['ba.ref']['checked']))
  1002. {
  1003. print '<td align="right" class="nowrap">';
  1004. print $bankaccount->getNomUrl(1);
  1005. print "</td>\n";
  1006. if (! $i) $totalarray['nbfield']++;
  1007. }
  1008. // Debit
  1009. if (! empty($arrayfields['b.debit']['checked']))
  1010. {
  1011. print '<td align="right">';
  1012. if ($objp->amount < 0)
  1013. {
  1014. print price($objp->amount * -1);
  1015. $totalarray['totaldeb'] += $objp->amount;
  1016. }
  1017. print "</td>\n";
  1018. if (! $i) $totalarray['nbfield']++;
  1019. if (! $i) $totalarray['totaldebfield']=$totalarray['nbfield'];
  1020. }
  1021. // Credit
  1022. if (! empty($arrayfields['b.credit']['checked']))
  1023. {
  1024. print '<td align="right">';
  1025. if ($objp->amount > 0)
  1026. {
  1027. print price($objp->amount);
  1028. $totalarray['totalcred'] += $objp->amount;
  1029. }
  1030. print "</td>\n";
  1031. if (! $i) $totalarray['nbfield']++;
  1032. if (! $i) $totalarray['totalcredfield']=$totalarray['nbfield'];
  1033. }
  1034. // Balance
  1035. if (! empty($arrayfields['balance']['checked']))
  1036. {
  1037. if ($mode_balance_ok)
  1038. {
  1039. if ($balance >= 0)
  1040. {
  1041. print '<td align="right" class="nowrap">&nbsp;'.price($balance).'</td>';
  1042. }
  1043. else
  1044. {
  1045. print '<td align="right" class="error nowrap">&nbsp;'.price($balance).'</td>';
  1046. }
  1047. }
  1048. else
  1049. {
  1050. print '<td align="right">-</td>';
  1051. }
  1052. if (! $i) $totalarray['nbfield']++;
  1053. }
  1054. if (! empty($arrayfields['b.num_releve']['checked']))
  1055. {
  1056. print '<td class="nowrap" align="center">';
  1057. // Transaction reconciliated or edit link
  1058. if ($bankaccount->canBeConciliated() > 0)
  1059. {
  1060. if ($objp->conciliated) // If line not conciliated and account can be conciliated
  1061. {
  1062. print '<a href="releve.php?num='.$objp->num_releve.'&amp;account='.$objp->bankid.'">'.$objp->num_releve.'</a>';
  1063. }
  1064. else if ($action == 'reconcile')
  1065. {
  1066. print '<input class="flat" name="rowid['.$objp->rowid.']" type="checkbox" value="'.$objp->rowid.'" size="1"'.(! empty($_POST['rowid'][$objp->rowid])?' checked':'').'>';
  1067. }
  1068. }
  1069. print '</td>';
  1070. if (! $i) $totalarray['nbfield']++;
  1071. }
  1072. if (! empty($arrayfields['b.conciliated']['checked']))
  1073. {
  1074. print '<td class="nowrap" align="center">';
  1075. print $objp->conciliated?$langs->trans("Yes"):$langs->trans("No");
  1076. print '</td>';
  1077. if (! $i) $totalarray['nbfield']++;
  1078. }
  1079. // Action edit/delete
  1080. print '<td class="nowrap" align="center">';
  1081. // Transaction reconciliated or edit link
  1082. if ($objp->conciliated && $bankaccount->canBeConciliated() > 0) // If line not conciliated and account can be conciliated
  1083. {
  1084. print '<a href="'.DOL_URL_ROOT.'/compta/bank/ligne.php?rowid='.$objp->rowid.'&amp;account='.$objp->bankid.'&amp;page='.$page.'">';
  1085. print img_edit();
  1086. print '</a>';
  1087. }
  1088. else
  1089. {
  1090. if ($user->rights->banque->modifier || $user->rights->banque->consolidate)
  1091. {
  1092. print '<a href="'.DOL_URL_ROOT.'/compta/bank/ligne.php?rowid='.$objp->rowid.'&amp;account='.$objp->bankid.'&amp;page='.$page.'">';
  1093. print img_edit();
  1094. print '</a>';
  1095. }
  1096. else
  1097. {
  1098. print '<a href="'.DOL_URL_ROOT.'/compta/bank/ligne.php?rowid='.$objp->rowid.'&amp;account='.$objp->bankid.'&amp;page='.$page.'">';
  1099. print img_view();
  1100. print '</a>';
  1101. }
  1102. if ($bankaccount->canBeConciliated() > 0 && empty($objp->conciliated))
  1103. {
  1104. if ($db->jdate($objp->dv) < ($now - $conf->bank->rappro->warning_delay))
  1105. {
  1106. print ' '.img_warning($langs->trans("ReconciliationLate"));
  1107. }
  1108. }
  1109. print '&nbsp;';
  1110. if ($user->rights->banque->modifier)
  1111. {
  1112. print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete&amp;rowid='.$objp->rowid.'&amp;id='.$objp->bankid.'&amp;page='.$page.'">';
  1113. print img_delete();
  1114. print '</a>';
  1115. }
  1116. }
  1117. print '</td>';
  1118. if (! $i) $totalarray['nbfield']++;
  1119. // Action column
  1120. print '<td class="nowrap" align="center">';
  1121. if ($massactionbutton || $massaction) // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
  1122. {
  1123. $selected=0;
  1124. if (in_array($obj->rowid, $arrayofselected)) $selected=1;
  1125. print '<input id="cb'.$obj->rowid.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$obj->rowid.'"'.($selected?' checked="checked"':'').'>';
  1126. }
  1127. print '</td>';
  1128. if (! $i) $totalarray['nbfield']++;
  1129. print "</tr>";
  1130. $i++;
  1131. }
  1132. // Show total line
  1133. if (isset($totalarray['totaldebfield']) || isset($totalarray['totalcredfield']))
  1134. {
  1135. print '<tr class="liste_total">';
  1136. $i=0;
  1137. while ($i < $totalarray['nbfield'])
  1138. {
  1139. $i++;
  1140. if ($i == 1)
  1141. {
  1142. if ($num < $limit && empty($offset)) print '<td align="left">'.$langs->trans("Total").'</td>';
  1143. else print '<td align="left">'.$langs->trans("Totalforthispage").'</td>';
  1144. }
  1145. elseif ($totalarray['totaldebfield'] == $i) print '<td align="right">'.price(-1 * $totalarray['totaldeb']).'</td>';
  1146. elseif ($totalarray['totalcredfield'] == $i) print '<td align="right">'.price($totalarray['totalcred']).'</td>';
  1147. else print '<td></td>';
  1148. }
  1149. print '</tr>';
  1150. }
  1151. print "</table>";
  1152. print "</div>";
  1153. print '</form>';
  1154. $db->free($resql);
  1155. }
  1156. else
  1157. {
  1158. dol_print_error($db);
  1159. }
  1160. // If no data to display after a search
  1161. if ($_POST["action"] == "search" && ! $num)
  1162. {
  1163. print '<div class="opacitymedium">'.$langs->trans("NoRecordFound").'</div>';
  1164. }
  1165. llxFooter();
  1166. $db->close();