report.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /* Copyright (C) 2001-2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2020 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 <https://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/compta/cashcontrol/report.php
  27. * \ingroup cashdesk|takepos
  28. * \brief List of sales from POS
  29. */
  30. if (!defined('NOREQUIREMENU')) {
  31. define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
  32. }
  33. if (!defined('NOBROWSERNOTIF')) {
  34. define('NOBROWSERNOTIF', '1'); // Disable browser notification
  35. }
  36. $_GET['optioncss'] = "print";
  37. // Load Dolibarr environment
  38. require '../../main.inc.php';
  39. require_once DOL_DOCUMENT_ROOT.'/compta/cashcontrol/class/cashcontrol.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/compta/cashcontrol/class/cashcontrol.class.php';
  43. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  44. $langs->loadLangs(array("bills", "banks"));
  45. $id = GETPOST('id', 'int');
  46. $object = new CashControl($db);
  47. $object->fetch($id);
  48. //$limit = GETPOST('limit')?GETPOST('limit', 'int'):$conf->liste_limit;
  49. $sortorder = 'ASC';
  50. $sortfield = 'b.datev,b.dateo,b.rowid';
  51. $arrayfields = array(
  52. 'b.rowid'=>array('label'=>$langs->trans("Ref"), 'checked'=>1),
  53. 'b.dateo'=>array('label'=>$langs->trans("DateOperationShort"), 'checked'=>1),
  54. 'b.num_chq'=>array('label'=>$langs->trans("Number"), 'checked'=>1),
  55. 'ba.ref'=>array('label'=>$langs->trans("BankAccount"), 'checked'=>1),
  56. 'cp.code'=>array('label'=>$langs->trans("PaymentMode"), 'checked'=>1),
  57. 'b.debit'=>array('label'=>$langs->trans("Debit"), 'checked'=>1, 'position'=>600),
  58. 'b.credit'=>array('label'=>$langs->trans("Credit"), 'checked'=>1, 'position'=>605),
  59. );
  60. $syear = $object->year_close;
  61. $smonth = $object->month_close;
  62. $sday = $object->day_close;
  63. $posmodule = $object->posmodule;
  64. $terminalid = $object->posnumber;
  65. // Security check
  66. if ($user->socid > 0) { // Protection if external user
  67. //$socid = $user->socid;
  68. accessforbidden();
  69. }
  70. if (empty($user->rights->cashdesk->run) && empty($user->rights->takepos->run)) {
  71. accessforbidden();
  72. }
  73. /*
  74. * View
  75. */
  76. $title = $langs->trans("CashControl");
  77. $param = '';
  78. llxHeader('', $title, '', '', 0, 0, array(), array(), $param);
  79. /*$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,";
  80. $sql.= " b.fk_account, b.fk_type,";
  81. $sql.= " ba.rowid as bankid, ba.ref as bankref,";
  82. $sql.= " bu.url_id,";
  83. $sql.= " f.module_source, f.ref as ref";
  84. $sql.= " FROM ";
  85. //if ($bid) $sql.= MAIN_DB_PREFIX."bank_class as l,";
  86. $sql.= " ".MAIN_DB_PREFIX."bank_account as ba,";
  87. $sql.= " ".MAIN_DB_PREFIX."bank as b";
  88. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON bu.fk_bank = b.rowid AND type = 'payment'";
  89. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON bu.url_id = f.rowid";
  90. $sql.= " WHERE b.fk_account = ba.rowid";
  91. // Define filter on invoice
  92. $sql.= " AND f.module_source = '".$db->escape($object->posmodule)."'";
  93. $sql.= " AND f.pos_source = '".$db->escape($object->posnumber)."'";
  94. $sql.= " AND f.entity IN (".getEntity('facture').")";
  95. // Define filter on data
  96. if ($syear && ! $smonth) $sql.= " AND dateo BETWEEN '".$db->idate(dol_get_first_day($syear, 1))."' AND '".$db->idate(dol_get_last_day($syear, 12))."'";
  97. 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))."'";
  98. 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))."'";
  99. else dol_print_error('', 'Year not defined');
  100. // Define filter on bank account
  101. $sql.=" AND (b.fk_account = ".((int) $conf->global->CASHDESK_ID_BANKACCOUNT_CASH);
  102. $sql.=" OR b.fk_account = ".((int) $conf->global->CASHDESK_ID_BANKACCOUNT_CB);
  103. $sql.=" OR b.fk_account = ".((int) $conf->global->CASHDESK_ID_BANKACCOUNT_CHEQUE);
  104. $sql.=")";
  105. */
  106. $sql = "SELECT f.rowid as facid, f.ref, f.datef as do, pf.amount as amount, b.fk_account as bankid, cp.code";
  107. $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";
  108. $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";
  109. $sql .= " AND f.module_source = '".$db->escape($posmodule)."'";
  110. $sql .= " AND f.pos_source = '".$db->escape($terminalid)."'";
  111. $sql .= " AND f.paye = 1";
  112. $sql .= " AND p.entity = ".$conf->entity; // Never share entities for features related to accountancy
  113. /*if ($key == 'cash') $sql.=" AND cp.code = 'LIQ'";
  114. elseif ($key == 'cheque') $sql.=" AND cp.code = 'CHQ'";
  115. elseif ($key == 'card') $sql.=" AND cp.code = 'CB'";
  116. else
  117. {
  118. dol_print_error('Value for key = '.$key.' not supported');
  119. exit;
  120. }*/
  121. if ($syear && !$smonth) {
  122. $sql .= " AND datef BETWEEN '".$db->idate(dol_get_first_day($syear, 1))."' AND '".$db->idate(dol_get_last_day($syear, 12))."'";
  123. } elseif ($syear && $smonth && !$sday) {
  124. $sql .= " AND datef BETWEEN '".$db->idate(dol_get_first_day($syear, $smonth))."' AND '".$db->idate(dol_get_last_day($syear, $smonth))."'";
  125. } elseif ($syear && $smonth && $sday) {
  126. $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))."'";
  127. } else {
  128. dol_print_error('', 'Year not defined');
  129. }
  130. $resql = $db->query($sql);
  131. if ($resql) {
  132. $num = $db->num_rows($resql);
  133. $i = 0;
  134. print "<!-- title of cash fence -->\n";
  135. print '<center>';
  136. print '<h2>';
  137. if ($object->status != $object::STATUS_DRAFT) {
  138. print $langs->trans("CashControl")." ".$object->id;
  139. } else {
  140. print $langs->trans("CashControl")." - ".$langs->trans("Draft");
  141. }
  142. print "</h2>";
  143. print $mysoc->name;
  144. print '<br>'.$langs->trans("DateCreationShort").": ".dol_print_date($object->date_creation, 'dayhour');
  145. $userauthor = $object->fk_user_valid;
  146. if (empty($userauthor)) {
  147. $userauthor = $object->fk_user_creat;
  148. }
  149. $uservalid = new User($db);
  150. if ($userauthor > 0) {
  151. $uservalid->fetch($userauthor);
  152. print '<br>'.$langs->trans("Author").': '.$uservalid->getFullName($langs);
  153. }
  154. print '<br>'.$langs->trans("Period").': '.$object->year_close.($object->month_close ? '-'.$object->month_close : '').($object->day_close ? '-'.$object->day_close : '');
  155. print '</center>';
  156. $invoicetmp = new Facture($db);
  157. print "<div style='text-align: right'><h2>";
  158. print $langs->trans("InitialBankBalance").' - '.$langs->trans("Cash").' : <div class="inline-block amount width100">'.price($object->opening).'</div>';
  159. print "</h2></div>";
  160. print '<div class="div-table-responsive">';
  161. print '<table class="tagtable liste">'."\n";
  162. $param = '';
  163. // Fields title
  164. print '<tr class="liste_titre">';
  165. print_liste_field_titre($arrayfields['b.rowid']['label'], $_SERVER['PHP_SELF'], 'b.rowid', '', $param, '', $sortfield, $sortorder);
  166. print_liste_field_titre($arrayfields['b.dateo']['label'], $_SERVER['PHP_SELF'], 'b.dateo', '', $param, '"', $sortfield, $sortorder, 'center ');
  167. print_liste_field_titre($arrayfields['ba.ref']['label'], $_SERVER['PHP_SELF'], 'ba.ref', '', $param, '', $sortfield, $sortorder, 'right ');
  168. print_liste_field_titre($arrayfields['cp.code']['label'], $_SERVER['PHP_SELF'], 'cp.code', '', $param, '', $sortfield, $sortorder, 'right ');
  169. print_liste_field_titre($arrayfields['b.debit']['label'], $_SERVER['PHP_SELF'], 'b.amount', '', $param, '', $sortfield, $sortorder, 'right ');
  170. print_liste_field_titre($arrayfields['b.credit']['label'], $_SERVER['PHP_SELF'], 'b.amount', '', $param, '', $sortfield, $sortorder, 'right ');
  171. print "</tr>\n";
  172. // Loop on each record
  173. $cash = $bank = $cheque = $other = 0;
  174. $totalqty = 0;
  175. $totalvat = 0;
  176. $totalvatperrate = array();
  177. $totallocaltax1 = 0;
  178. $totallocaltax2 = 0;
  179. $cachebankaccount = array();
  180. $cacheinvoiceid = array();
  181. $transactionspertype = array();
  182. $amountpertype = array();
  183. $totalarray = array();
  184. while ($i < $num) {
  185. $objp = $db->fetch_object($resql);
  186. // Load bankaccount
  187. if (empty($cachebankaccount[$objp->bankid])) {
  188. $bankaccounttmp = new Account($db);
  189. $bankaccounttmp->fetch($objp->bankid);
  190. $cachebankaccount[$objp->bankid] = $bankaccounttmp;
  191. $bankaccount = $bankaccounttmp;
  192. } else {
  193. $bankaccount = $cachebankaccount[$objp->bankid];
  194. }
  195. $invoicetmp->fetch($objp->facid);
  196. if (empty($cacheinvoiceid[$objp->facid])) {
  197. $cacheinvoiceid[$objp->facid] = $objp->facid; // First time this invoice is found into list of invoice x payments
  198. foreach ($invoicetmp->lines as $line) {
  199. $totalqty += $line->qty;
  200. $totalvat += $line->total_tva;
  201. if ($line->tva_tx) {
  202. if (empty($totalvatperrate[$line->tva_tx])) {
  203. $totalvatperrate[$line->tva_tx] = 0;
  204. }
  205. $totalvatperrate[$line->tva_tx] += $line->total_tva;
  206. }
  207. $totallocaltax1 += $line->total_localtax1;
  208. $totallocaltax2 += $line->total_localtax2;
  209. }
  210. }
  211. print '<tr class="oddeven">';
  212. // Ref
  213. print '<td class="nowrap left">';
  214. print $invoicetmp->getNomUrl(1);
  215. print '</td>';
  216. if (!$i) {
  217. $totalarray['nbfield']++;
  218. }
  219. // Date ope
  220. print '<td class="nowrap left">';
  221. print '<span id="dateoperation_'.$objp->rowid.'">'.dol_print_date($db->jdate($objp->do), "day")."</span>";
  222. print "</td>\n";
  223. if (!$i) {
  224. $totalarray['nbfield']++;
  225. }
  226. if ($object->posmodule == "takepos") {
  227. $var1 = 'CASHDESK_ID_BANKACCOUNT_CASH'.$object->posnumber;
  228. } else {
  229. $var1 = 'CASHDESK_ID_BANKACCOUNT_CASH';
  230. }
  231. // Bank account
  232. print '<td class="nowrap right">';
  233. print $bankaccount->getNomUrl(1);
  234. if ($objp->code == 'CHQ') {
  235. $cheque += $objp->amount;
  236. if (empty($transactionspertype[$objp->code])) {
  237. $transactionspertype[$objp->code] = 0;
  238. }
  239. $transactionspertype[$objp->code] += 1;
  240. } elseif ($objp->code == 'CB') {
  241. $bank += $objp->amount;
  242. if (empty($transactionspertype[$objp->code])) {
  243. $transactionspertype[$objp->code] = 0;
  244. }
  245. $transactionspertype[$objp->code] += 1;
  246. } else {
  247. if ($conf->global->$var1 == $bankaccount->id) {
  248. $cash += $objp->amount;
  249. // } elseif ($conf->global->$var2 == $bankaccount->id) $bank+=$objp->amount;
  250. //elseif ($conf->global->$var3 == $bankaccount->id) $cheque+=$objp->amount;
  251. if (empty($transactionspertype['CASH'])) {
  252. $transactionspertype['CASH'] = 0;
  253. }
  254. $transactionspertype['CASH'] += 1;
  255. } else {
  256. $other += $objp->amount;
  257. if (empty($transactionspertype['OTHER'])) {
  258. $transactionspertype['OTHER'] = 0;
  259. }
  260. $transactionspertype['OTHER'] += 1;
  261. }
  262. }
  263. print "</td>\n";
  264. if (!$i) {
  265. $totalarray['nbfield']++;
  266. }
  267. // Type
  268. print '<td class="right">';
  269. print $objp->code;
  270. if (empty($amountpertype[$objp->code])) {
  271. $amountpertype[$objp->code] = 0;
  272. }
  273. print "</td>\n";
  274. if (!$i) {
  275. $totalarray['nbfield']++;
  276. }
  277. // Debit
  278. print '<td class="right">';
  279. if ($objp->amount < 0) {
  280. print '<span class="amount">'.price($objp->amount * -1).'</span>';
  281. $totalarray['val']['totaldebfield'] += $objp->amount;
  282. $amountpertype[$objp->code] += $objp->amount;
  283. }
  284. print "</td>\n";
  285. if (!$i) {
  286. $totalarray['nbfield']++;
  287. }
  288. if (!$i) {
  289. $totalarray['pos'][$totalarray['nbfield']] = 'totaldebfield';
  290. }
  291. // Credit
  292. print '<td class="right">';
  293. if ($objp->amount > 0) {
  294. print '<span class="amount">'.price($objp->amount).'</span>';
  295. $totalarray['val']['totalcredfield'] += $objp->amount;
  296. $amountpertype[$objp->code] -= $objp->amount;
  297. }
  298. print "</td>\n";
  299. if (!$i) {
  300. $totalarray['nbfield']++;
  301. }
  302. if (!$i) {
  303. $totalarray['pos'][$totalarray['nbfield']] = 'totalcredfield';
  304. }
  305. print "</tr>";
  306. $i++;
  307. }
  308. // Show total line
  309. include DOL_DOCUMENT_ROOT.'/core/tpl/list_print_total.tpl.php';
  310. print "</table>";
  311. print "</div>";
  312. //$cash = $amountpertype['LIQ'] + $object->opening;
  313. $cash = price2num($cash + $object->opening, 'MT');
  314. print '<div style="text-align: right">';
  315. print '<h2>';
  316. print $langs->trans("Cash").(!empty($transactionspertype['CASH']) ? ' ('.$transactionspertype['CASH'].')' : '').' : <div class="inline-block amount width100">'.price($cash).'</div>';
  317. if ($object->status == $object::STATUS_VALIDATED && $cash != $object->cash) {
  318. print ' <> <div class="inline-block amountremaintopay fontsizeunset">'.$langs->trans("Declared").': '.price($object->cash).'</div>';
  319. }
  320. print "<br>";
  321. //print '<br>';
  322. print $langs->trans("PaymentTypeCHQ").(!empty($transactionspertype['CHQ']) ? ' ('.$transactionspertype['CHQ'].')' : '').' : <div class="inline-block amount width100">'.price($cheque).'</div>';
  323. if ($object->status == $object::STATUS_VALIDATED && $cheque != $object->cheque) {
  324. print ' <> <div class="inline-block amountremaintopay fontsizeunset">'.$langs->trans("Declared").' : '.price($object->cheque).'</div>';
  325. }
  326. print "<br>";
  327. //print '<br>';
  328. print $langs->trans("PaymentTypeCB").(!empty($transactionspertype['CB']) ? ' ('.$transactionspertype['CB'].')' : '').' : <div class="inline-block amount width100">'.price($bank).'</div>';
  329. if ($object->status == $object::STATUS_VALIDATED && $bank != $object->card) {
  330. print ' <> <div class="inline-block amountremaintopay fontsizeunset">'.$langs->trans("Declared").': '.price($object->card).'</div>';
  331. }
  332. print "<br>";
  333. // print '<br>';
  334. if ($other) {
  335. print ''.$langs->trans("Other").($transactionspertype['OTHER'] ? ' ('.$transactionspertype['OTHER'].')' : '').' : <div class="inline-block amount width100">'.price($other)."</div>";
  336. print '<br>';
  337. }
  338. print $langs->trans("Total").' ('.$totalqty.' '.$langs->trans("Articles").') : <div class="inline-block amount width100">'.price($cash + $cheque + $bank + $other).'</div>';
  339. print '<br>'.$langs->trans("TotalVAT").' : <div class="inline-block amount width100">'.price($totalvat).'</div>';
  340. if ($mysoc->useLocalTax(1)) {
  341. print '<br>'.$langs->trans("TotalLT1").' : <div class="inline-block amount width100">'.price($totallocaltax1).'</div>';
  342. }
  343. if ($mysoc->useLocalTax(1)) {
  344. print '<br>'.$langs->trans("TotalLT2").' : <div class="inline-block amount width100">'.price($totallocaltax2).'</div>';
  345. }
  346. if (!empty($totalvatperrate) && is_array($totalvatperrate)) {
  347. print '<br><br><div class="small inline-block">'.$langs->trans("VATRate").'</div>';
  348. foreach ($totalvatperrate as $keyrate => $valuerate) {
  349. print '<br><div class="small">'.$langs->trans("VATRate").' '.vatrate($keyrate, 1).' : <div class="inline-block amount width100">'.price($valuerate).'</div></div>';
  350. }
  351. }
  352. print '</h2>';
  353. print '</div>';
  354. print '</form>';
  355. $db->free($resql);
  356. } else {
  357. dol_print_error($db);
  358. }
  359. llxFooter();
  360. $db->close();