report.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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@open-dsi.fr>
  10. * Copyright (C) 2018 Andreu Bisquerra <jove@bisquerra.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/compta/cashcontrol/report.php
  27. * \ingroup cashdesk|takepos
  28. * \brief List of bank transactions
  29. */
  30. require '../../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/cashcontrol/class/cashcontrol.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  34. $id = GETPOST('id','int');
  35. $_GET['optioncss']="print";
  36. include_once 'class/cashcontrol.class.php';
  37. $cashcontrol= new CashControl($db);
  38. $cashcontrol->fetch($id);
  39. $limit = GETPOST('limit')?GETPOST('limit','int'):$conf->liste_limit;
  40. $sortorder='ASC';
  41. $sortfield='b.datev,b.dateo,b.rowid';
  42. $arrayfields=array(
  43. 'b.rowid'=>array('label'=>$langs->trans("Ref"), 'checked'=>1),
  44. 'b.dateo'=>array('label'=>$langs->trans("DateOperationShort"), 'checked'=>1),
  45. 'b.num_chq'=>array('label'=>$langs->trans("Number"), 'checked'=>1),
  46. 'ba.ref'=>array('label'=>$langs->trans("BankAccount"), 'checked'=>1),
  47. 'b.debit'=>array('label'=>$langs->trans("Debit"), 'checked'=>1, 'position'=>600),
  48. 'b.credit'=>array('label'=>$langs->trans("Credit"), 'checked'=>1, 'position'=>605),
  49. );
  50. $syear = $cashcontrol->year_close;
  51. $smonth = $cashcontrol->month_close;
  52. $sday = $cashcontrol->day_close;
  53. $posmodule = $cashcontrol->posmodule;
  54. $terminalid = $cashcontrol->posnumber;
  55. /*
  56. * View
  57. */
  58. llxHeader('', $langs->trans("CashControl"), '', '', 0, 0, array(), array(), $param);
  59. /*$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,";
  60. $sql.= " b.fk_account, b.fk_type,";
  61. $sql.= " ba.rowid as bankid, ba.ref as bankref,";
  62. $sql.= " bu.url_id,";
  63. $sql.= " f.module_source, f.facnumber as facnumber";
  64. $sql.= " FROM ";
  65. //if ($bid) $sql.= MAIN_DB_PREFIX."bank_class as l,";
  66. $sql.= " ".MAIN_DB_PREFIX."bank_account as ba,";
  67. $sql.= " ".MAIN_DB_PREFIX."bank as b";
  68. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON bu.fk_bank = b.rowid AND type = 'payment'";
  69. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON bu.url_id = f.rowid";
  70. $sql.= " WHERE b.fk_account = ba.rowid";
  71. // Define filter on invoice
  72. $sql.= " AND f.module_source = '".$db->escape($cashcontrol->posmodule)."'";
  73. $sql.= " AND f.pos_source = '".$db->escape($cashcontrol->posnumber)."'";
  74. $sql.= " AND f.entity IN (".getEntity('facture').")";
  75. // Define filter on data
  76. if ($syear && ! $smonth) $sql.= " AND dateo BETWEEN '".$db->idate(dol_get_first_day($syear, 1))."' AND '".$db->idate(dol_get_last_day($syear, 12))."'";
  77. elseif ($syear && $smonth && ! $sday) $sql.= " AND dateo BETWEEN '".$db->idate(dol_get_first_day($syear, $smonth))."' AND '".$db->idate(dol_get_last_day($syear, $smonth))."'";
  78. elseif ($syear && $smonth && $sday) $sql.= " AND dateo BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $smonth, $sday, $syear))."' AND '".$db->idate(dol_mktime(23, 59, 59, $smonth, $sday, $syear))."'";
  79. else dol_print_error('', 'Year not defined');
  80. // Define filter on bank account
  81. $sql.=" AND (b.fk_account=".$conf->global->CASHDESK_ID_BANKACCOUNT_CASH;
  82. $sql.=" OR b.fk_account=".$conf->global->CASHDESK_ID_BANKACCOUNT_CB;
  83. $sql.=" OR b.fk_account=".$conf->global->CASHDESK_ID_BANKACCOUNT_CHEQUE;
  84. $sql.=")";
  85. */
  86. $sql = "SELECT f.rowid as facid, f.facnumber, f.datef as do, pf.amount as amount, b.fk_account as bankid, cp.code";
  87. $sql.= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf, ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."c_paiement as cp, ".MAIN_DB_PREFIX."bank as b";
  88. $sql.= " WHERE pf.fk_facture = f.rowid AND p.rowid = pf.fk_paiement AND cp.id = p.fk_paiement AND p.fk_bank = b.rowid";
  89. $sql.= " AND f.module_source = '".$db->escape($posmodule)."'";
  90. $sql.= " AND f.pos_source = '".$db->escape($terminalid)."'";
  91. $sql.= " AND f.paye = 1";
  92. $sql.= " AND p.entity IN (".getEntity('facture').")";
  93. /*if ($key == 'cash') $sql.=" AND cp.code = 'LIQ'";
  94. elseif ($key == 'cheque') $sql.=" AND cp.code = 'CHQ'";
  95. elseif ($key == 'card') $sql.=" AND cp.code = 'CB'";
  96. else
  97. {
  98. dol_print_error('Value for key = '.$key.' not supported');
  99. exit;
  100. }*/
  101. if ($syear && ! $smonth) $sql.= " AND datef BETWEEN '".$db->idate(dol_get_first_day($syear, 1))."' AND '".$db->idate(dol_get_last_day($syear, 12))."'";
  102. elseif ($syear && $smonth && ! $sday) $sql.= " AND datef BETWEEN '".$db->idate(dol_get_first_day($syear, $smonth))."' AND '".$db->idate(dol_get_last_day($syear, $smonth))."'";
  103. elseif ($syear && $smonth && $sday) $sql.= " AND datef BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $smonth, $sday, $syear))."' AND '".$db->idate(dol_mktime(23, 59, 59, $smonth, $sday, $syear))."'";
  104. else dol_print_error('', 'Year not defined');
  105. $resql = $db->query($sql);
  106. if ($resql)
  107. {
  108. $num = $db->num_rows($resql);
  109. $i = 0;
  110. print "<center><h2>";
  111. if ($cashcontrol->status==2) print $langs->trans("CashControl")." ".$cashcontrol->id;
  112. else print $langs->trans("CashControl")." - ".$langs->trans("Draft");
  113. print "<br>".$langs->trans("DateCreationShort").": ".dol_print_date($cashcontrol->date_creation, 'dayhour')."</h2></center>";
  114. $invoicetmp = new Facture($db);
  115. print "<div style='text-align: right'><h2>";
  116. print $langs->trans("InitialBankBalance").' - '.$langs->trans("Cash")." : ".price($cashcontrol->opening);
  117. print "</h2></div>";
  118. print '<div class="div-table-responsive">';
  119. print '<table class="tagtable liste">'."\n";
  120. // Fields title
  121. print '<tr class="liste_titre">';
  122. print_liste_field_titre($arrayfields['b.rowid']['label'],$_SERVER['PHP_SELF'],'b.rowid','',$param,'',$sortfield,$sortorder);
  123. print_liste_field_titre($arrayfields['b.dateo']['label'],$_SERVER['PHP_SELF'],'b.dateo','',$param,'align="left"',$sortfield,$sortorder);
  124. print_liste_field_titre($arrayfields['ba.ref']['label'],$_SERVER['PHP_SELF'],'ba.ref','',$param,'align="right"',$sortfield,$sortorder);
  125. print_liste_field_titre($arrayfields['b.debit']['label'],$_SERVER['PHP_SELF'],'b.amount','',$param,'align="right"',$sortfield,$sortorder);
  126. print_liste_field_titre($arrayfields['b.credit']['label'],$_SERVER['PHP_SELF'],'b.amount','',$param,'align="right"',$sortfield,$sortorder);
  127. print "</tr>\n";
  128. $posconciliatecol = 0;
  129. // Loop on each record
  130. $sign = 1;
  131. $cash=$bank=$cheque=$other=0;
  132. $totalarray=array();
  133. while ($i < min($num,$limit))
  134. {
  135. $objp = $db->fetch_object($resql);
  136. if (empty($cachebankaccount[$objp->bankid]))
  137. {
  138. $bankaccounttmp = new Account($db);
  139. $bankaccounttmp->fetch($objp->bankid);
  140. $cachebankaccount[$objp->bankid]=$bankaccounttmp;
  141. $bankaccount = $bankaccounttmp;
  142. }
  143. else
  144. {
  145. $bankaccount = $cachebankaccount[$objp->bankid];
  146. }
  147. /*if ($first == "yes")
  148. {
  149. print '<tr class="oddeven">';
  150. print '<td>'.$langs->trans("InitialBankBalance").' - '.$langs->trans("Cash").'</td>';
  151. print '<td></td><td></td><td></td><td class="right">'.price($cashcontrol->opening).'</td>';
  152. print '</tr>';
  153. $first = "no";
  154. }*/
  155. print '<tr class="oddeven">';
  156. // Ref
  157. print '<td class="nowrap left">';
  158. $invoicetmp->fetch($objp->facid);
  159. print $invoicetmp->getNomUrl(1);
  160. print '</td>';
  161. if (! $i) $totalarray['nbfield']++;
  162. // Date ope
  163. print '<td class="nowrap left">';
  164. print '<span id="dateoperation_'.$objp->rowid.'">'.dol_print_date($db->jdate($objp->do),"day")."</span>";
  165. print "</td>\n";
  166. if (! $i) $totalarray['nbfield']++;
  167. // Bank account
  168. print '<td class="nowrap right">';
  169. print $bankaccount->getNomUrl(1);
  170. if ($conf->global->CASHDESK_ID_BANKACCOUNT_CASH==$bankaccount->id) $cash+=$objp->amount;
  171. elseif ($conf->global->CASHDESK_ID_BANKACCOUNT_CB==$bankaccount->id) $bank+=$objp->amount;
  172. elseif ($conf->global->CASHDESK_ID_BANKACCOUNT_CHEQUE==$bankaccount->id) $cheque+=$objp->amount;
  173. else $other+=$objp->amount;
  174. print "</td>\n";
  175. if (! $i) $totalarray['nbfield']++;
  176. // Debit
  177. print '<td class="right">';
  178. if ($objp->amount < 0)
  179. {
  180. print price($objp->amount * -1);
  181. $totalarray['totaldeb'] += $objp->amount;
  182. }
  183. print "</td>\n";
  184. if (! $i) $totalarray['nbfield']++;
  185. if (! $i) $totalarray['totaldebfield']=$totalarray['nbfield'];
  186. // Credit
  187. print '<td class="right">';
  188. if ($objp->amount > 0)
  189. {
  190. print price($objp->amount);
  191. $totalarray['totalcred'] += $objp->amount;
  192. }
  193. print "</td>\n";
  194. if (! $i) $totalarray['nbfield']++;
  195. if (! $i) $totalarray['totalcredfield']=$totalarray['nbfield'];
  196. print "</tr>";
  197. $i++;
  198. }
  199. // Show total line
  200. if (isset($totalarray['totaldebfield']) || isset($totalarray['totalcredfield']))
  201. {
  202. print '<tr class="liste_total">';
  203. $i=0;
  204. while ($i < $totalarray['nbfield'])
  205. {
  206. $i++;
  207. if ($i == 1)
  208. {
  209. if ($num < $limit && empty($offset)) print '<td class="left">'.$langs->trans("Total").'</td>';
  210. else print '<td class="left">'.$langs->trans("Totalforthispage").'</td>';
  211. }
  212. elseif ($totalarray['totaldebfield'] == $i) print '<td class="right">'.price(-1 * $totalarray['totaldeb']).'</td>';
  213. elseif ($totalarray['totalcredfield'] == $i) print '<td class="right">'.price($totalarray['totalcred']).'</td>';
  214. else print '<td></td>';
  215. }
  216. print '</tr>';
  217. }
  218. print "</table>";
  219. $cash=$cash+$cashcontrol->opening;
  220. print "<div style='text-align: right'><h2>";
  221. print $langs->trans("Cash").": ".price($cash)."<br><br>";
  222. print $langs->trans("PaymentTypeCB").": ".price($bank)."<br><br>";
  223. print $langs->trans("PaymentTypeCHQ").": ".price($cheque)."<br><br>";
  224. if ($other) print $langs->trans("Other").": ".price($other)."<br><br>";
  225. print "</h2></div>";
  226. //save totals to DB
  227. /*
  228. $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence ";
  229. $sql .= "SET";
  230. $sql .= " cash='".$cash."'";
  231. $sql .= ", card='".$bank."'";
  232. $sql .= " where rowid=".$id;
  233. $db->query($sql);
  234. */
  235. print "</div>";
  236. print '</form>';
  237. $db->free($resql);
  238. }
  239. else
  240. {
  241. dol_print_error($db);
  242. }
  243. llxFooter();
  244. $db->close();