dolibarr/htdocs/compta/cashcontrol/report.php

419 lines
16 KiB
PHP
Raw Normal View History

2018-12-08 21:33:54 +01:00
<?php
/* Copyright (C) 2001-2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
2020-10-27 10:32:25 +01:00
* Copyright (C) 2004-2020 Laurent Destailleur <eldy@users.sourceforge.net>
2018-12-08 21:33:54 +01:00
* Copyright (C) 2005-2010 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2012 Vinícius Nogueira <viniciusvgn@gmail.com>
* Copyright (C) 2014 Florian Henry <florian.henry@open-cooncept.pro>
* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
* Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
2019-01-28 21:39:22 +01:00
* Copyright (C) 2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
2018-12-15 18:27:38 +01:00
* Copyright (C) 2018 Andreu Bisquerra <jove@bisquerra.com>
2018-12-08 21:33:54 +01:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-12-08 21:33:54 +01:00
*/
/**
2018-12-19 16:12:55 +01:00
* \file htdocs/compta/cashcontrol/report.php
2019-01-07 19:35:20 +01:00
* \ingroup cashdesk|takepos
* \brief List of sales from POS
2018-12-08 21:33:54 +01:00
*/
2021-02-23 21:09:01 +01:00
if (!defined('NOREQUIREMENU')) {
define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
}
if (!defined('NOBROWSERNOTIF')) {
define('NOBROWSERNOTIF', '1'); // Disable browser notification
}
2020-10-27 10:32:25 +01:00
$_GET['optioncss'] = "print";
2018-12-15 19:20:30 +01:00
require '../../main.inc.php';
2018-12-15 18:27:38 +01:00
require_once DOL_DOCUMENT_ROOT.'/compta/cashcontrol/class/cashcontrol.class.php';
2018-12-08 21:33:54 +01:00
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
2019-01-07 19:35:20 +01:00
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
2020-01-24 15:26:18 +01:00
require_once DOL_DOCUMENT_ROOT.'/compta/cashcontrol/class/cashcontrol.class.php';
2021-04-15 15:07:06 +02:00
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
2018-12-08 21:33:54 +01:00
2020-10-27 10:35:22 +01:00
$langs->loadLangs(array("bills", "banks"));
2020-01-24 15:59:36 +01:00
$id = GETPOST('id', 'int');
2018-12-08 21:33:54 +01:00
$object = new CashControl($db);
$object->fetch($id);
2018-12-08 21:33:54 +01:00
2019-05-19 01:52:50 +02:00
//$limit = GETPOST('limit')?GETPOST('limit', 'int'):$conf->liste_limit;
$sortorder = 'ASC';
$sortfield = 'b.datev,b.dateo,b.rowid';
2018-12-08 21:33:54 +01:00
$arrayfields = array(
'b.rowid'=>array('label'=>$langs->trans("Ref"), 'checked'=>1),
'b.dateo'=>array('label'=>$langs->trans("DateOperationShort"), 'checked'=>1),
'b.num_chq'=>array('label'=>$langs->trans("Number"), 'checked'=>1),
'ba.ref'=>array('label'=>$langs->trans("BankAccount"), 'checked'=>1),
'cp.code'=>array('label'=>$langs->trans("PaymentMode"), 'checked'=>1),
2020-01-24 15:59:36 +01:00
'b.debit'=>array('label'=>$langs->trans("Debit"), 'checked'=>1, 'position'=>600),
'b.credit'=>array('label'=>$langs->trans("Credit"), 'checked'=>1, 'position'=>605),
2018-12-08 21:33:54 +01:00
);
$syear = $object->year_close;
$smonth = $object->month_close;
$sday = $object->day_close;
2018-12-19 16:12:55 +01:00
$posmodule = $object->posmodule;
$terminalid = $object->posnumber;
2018-12-19 16:12:55 +01:00
2021-03-18 14:41:48 +01:00
// Security check
if ($user->socid > 0) { // Protection if external user
//$socid = $user->socid;
accessforbidden();
}
if (empty($user->rights->cashdesk->run) && empty($user->rights->takepos->run)) {
2021-03-18 14:41:48 +01:00
accessforbidden();
}
2018-12-19 16:12:55 +01:00
2018-12-08 21:33:54 +01:00
/*
* View
*/
2018-12-15 18:27:38 +01:00
2021-02-16 11:15:42 +01:00
$title = $langs->trans("CashControl");
2020-03-15 13:57:31 +01:00
$param = '';
2021-02-16 11:15:42 +01:00
llxHeader('', $title, '', '', 0, 0, array(), array(), $param);
2018-12-08 21:33:54 +01:00
2018-12-19 16:12:55 +01:00
/*$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,";
2018-12-08 21:33:54 +01:00
$sql.= " b.fk_account, b.fk_type,";
$sql.= " ba.rowid as bankid, ba.ref as bankref,";
$sql.= " bu.url_id,";
2019-05-04 17:23:57 +02:00
$sql.= " f.module_source, f.ref as ref";
2018-12-08 21:33:54 +01:00
$sql.= " FROM ";
2018-12-19 16:12:55 +01:00
//if ($bid) $sql.= MAIN_DB_PREFIX."bank_class as l,";
2018-12-08 21:33:54 +01:00
$sql.= " ".MAIN_DB_PREFIX."bank_account as ba,";
$sql.= " ".MAIN_DB_PREFIX."bank as b";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON bu.fk_bank = b.rowid AND type = 'payment'";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON bu.url_id = f.rowid";
$sql.= " WHERE b.fk_account = ba.rowid";
2018-12-19 16:12:55 +01:00
// Define filter on invoice
$sql.= " AND f.module_source = '".$db->escape($object->posmodule)."'";
$sql.= " AND f.pos_source = '".$db->escape($object->posnumber)."'";
2018-12-19 16:12:55 +01:00
$sql.= " AND f.entity IN (".getEntity('facture').")";
// Define filter on data
if ($syear && ! $smonth) $sql.= " AND dateo BETWEEN '".$db->idate(dol_get_first_day($syear, 1))."' AND '".$db->idate(dol_get_last_day($syear, 12))."'";
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))."'";
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))."'";
else dol_print_error('', 'Year not defined');
// Define filter on bank account
$sql.=" AND (b.fk_account = ".((int) $conf->global->CASHDESK_ID_BANKACCOUNT_CASH);
$sql.=" OR b.fk_account = ".((int) $conf->global->CASHDESK_ID_BANKACCOUNT_CB);
$sql.=" OR b.fk_account = ".((int) $conf->global->CASHDESK_ID_BANKACCOUNT_CHEQUE);
2018-12-08 21:33:54 +01:00
$sql.=")";
2018-12-19 16:12:55 +01:00
*/
$sql = "SELECT f.rowid as facid, f.ref, f.datef as do, pf.amount as amount, b.fk_account as bankid, cp.code";
$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";
$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";
$sql .= " AND f.module_source = '".$db->escape($posmodule)."'";
$sql .= " AND f.pos_source = '".$db->escape($terminalid)."'";
$sql .= " AND f.paye = 1";
$sql .= " AND p.entity = ".$conf->entity; // Never share entities for features related to accountancy
2018-12-19 16:12:55 +01:00
/*if ($key == 'cash') $sql.=" AND cp.code = 'LIQ'";
elseif ($key == 'cheque') $sql.=" AND cp.code = 'CHQ'";
elseif ($key == 'card') $sql.=" AND cp.code = 'CB'";
else
{
dol_print_error('Value for key = '.$key.' not supported');
exit;
}*/
2021-02-23 21:09:01 +01:00
if ($syear && !$smonth) {
$sql .= " AND datef BETWEEN '".$db->idate(dol_get_first_day($syear, 1))."' AND '".$db->idate(dol_get_last_day($syear, 12))."'";
} 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))."'";
} 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))."'";
} else {
dol_print_error('', 'Year not defined');
}
2018-12-19 16:12:55 +01:00
2018-12-08 21:33:54 +01:00
$resql = $db->query($sql);
2020-10-27 21:28:26 +01:00
if ($resql) {
2018-12-08 21:33:54 +01:00
$num = $db->num_rows($resql);
$i = 0;
2018-12-15 18:27:38 +01:00
2020-10-27 10:27:44 +01:00
print "<!-- title of cash fence -->\n";
print '<center>';
print '<h2>';
if ($object->status != $object::STATUS_DRAFT) {
print $langs->trans("CashControl")." ".$object->id;
2021-02-23 21:09:01 +01:00
} else {
print $langs->trans("CashControl")." - ".$langs->trans("Draft");
}
print "</h2>";
print $mysoc->name;
print '<br>'.$langs->trans("DateCreationShort").": ".dol_print_date($object->date_creation, 'dayhour');
$userauthor = $object->fk_user_valid;
if (empty($userauthor)) {
$userauthor = $object->fk_user_creat;
}
$uservalid = new User($db);
if ($userauthor > 0) {
$uservalid->fetch($userauthor);
print '<br>'.$langs->trans("Author").': '.$uservalid->getFullName($langs);
}
print '<br>'.$langs->trans("Period").': '.$object->year_close.($object->month_close ? '-'.$object->month_close : '').($object->day_close ? '-'.$object->day_close : '');
print '</center>';
2018-12-08 21:33:54 +01:00
2019-01-07 19:35:20 +01:00
$invoicetmp = new Facture($db);
print "<div style='text-align: right'><h2>";
print $langs->trans("InitialBankBalance").' - '.$langs->trans("Cash").' : <div class="inline-block amount width100">'.price($object->opening).'</div>';
2019-01-07 19:35:20 +01:00
print "</h2></div>";
2018-12-08 21:33:54 +01:00
print '<div class="div-table-responsive">';
print '<table class="tagtable liste">'."\n";
2018-12-08 21:33:54 +01:00
$param = '';
2020-01-24 15:59:36 +01:00
2018-12-08 21:33:54 +01:00
// Fields title
print '<tr class="liste_titre">';
print_liste_field_titre($arrayfields['b.rowid']['label'], $_SERVER['PHP_SELF'], 'b.rowid', '', $param, '', $sortfield, $sortorder);
2020-01-24 15:59:36 +01:00
print_liste_field_titre($arrayfields['b.dateo']['label'], $_SERVER['PHP_SELF'], 'b.dateo', '', $param, '"', $sortfield, $sortorder, 'center ');
print_liste_field_titre($arrayfields['ba.ref']['label'], $_SERVER['PHP_SELF'], 'ba.ref', '', $param, '', $sortfield, $sortorder, 'right ');
print_liste_field_titre($arrayfields['cp.code']['label'], $_SERVER['PHP_SELF'], 'cp.code', '', $param, '', $sortfield, $sortorder, 'right ');
print_liste_field_titre($arrayfields['b.debit']['label'], $_SERVER['PHP_SELF'], 'b.amount', '', $param, '', $sortfield, $sortorder, 'right ');
print_liste_field_titre($arrayfields['b.credit']['label'], $_SERVER['PHP_SELF'], 'b.amount', '', $param, '', $sortfield, $sortorder, 'right ');
2018-12-08 21:33:54 +01:00
print "</tr>\n";
// Loop on each record
$cash = $bank = $cheque = $other = 0;
2018-12-08 21:33:54 +01:00
2021-06-09 03:54:19 +02:00
$totalqty = 0;
$totalvat = 0;
$totalvatperrate = array();
$totallocaltax1 = 0;
$totallocaltax2 = 0;
$cachebankaccount = array();
$cacheinvoiceid = array();
2021-06-09 03:54:19 +02:00
$transactionspertype = array();
$amountpertype = array();
2021-06-09 03:54:19 +02:00
$totalarray = array();
while ($i < $num) {
$objp = $db->fetch_object($resql);
// Load bankaccount
if (empty($cachebankaccount[$objp->bankid])) {
$bankaccounttmp = new Account($db);
$bankaccounttmp->fetch($objp->bankid);
$cachebankaccount[$objp->bankid] = $bankaccounttmp;
$bankaccount = $bankaccounttmp;
} else {
$bankaccount = $cachebankaccount[$objp->bankid];
}
2018-12-15 18:27:38 +01:00
$invoicetmp->fetch($objp->facid);
2020-01-24 15:59:36 +01:00
if (empty($cacheinvoiceid[$objp->facid])) {
$cacheinvoiceid[$objp->facid] = $objp->facid; // First time this invoice is found into list of invoice x payments
2021-06-09 17:50:21 +02:00
foreach ($invoicetmp->lines as $line) {
$totalqty += $line->qty;
$totalvat += $line->total_tva;
if ($line->tva_tx) {
if (empty($totalvatperrate[$line->tva_tx])) {
$totalvatperrate[$line->tva_tx] = 0;
}
$totalvatperrate[$line->tva_tx] += $line->total_tva;
}
$totallocaltax1 += $line->total_localtax1;
$totallocaltax2 += $line->total_localtax2;
}
}
2018-12-08 21:33:54 +01:00
2018-12-19 16:12:55 +01:00
print '<tr class="oddeven">';
// Ref
print '<td class="nowrap left">';
print $invoicetmp->getNomUrl(1);
print '</td>';
2021-02-23 21:09:01 +01:00
if (!$i) {
$totalarray['nbfield']++;
}
// Date ope
print '<td class="nowrap left">';
print '<span id="dateoperation_'.$objp->rowid.'">'.dol_print_date($db->jdate($objp->do), "day")."</span>";
print "</td>\n";
2021-02-23 21:09:01 +01:00
if (!$i) {
$totalarray['nbfield']++;
}
if ($object->posmodule == "takepos") {
$var1 = 'CASHDESK_ID_BANKACCOUNT_CASH'.$object->posnumber;
2020-05-21 15:05:19 +02:00
} else {
2020-01-24 15:59:36 +01:00
$var1 = 'CASHDESK_ID_BANKACCOUNT_CASH';
}
2021-06-09 03:54:19 +02:00
// Bank account
print '<td class="nowrap right">';
print $bankaccount->getNomUrl(1);
2020-01-24 15:59:36 +01:00
if ($objp->code == 'CHQ') {
$cheque += $objp->amount;
2021-06-09 03:54:19 +02:00
if (empty($transactionspertype[$objp->code])) {
$transactionspertype[$objp->code] = 0;
}
$transactionspertype[$objp->code] += 1;
2020-01-24 15:59:36 +01:00
} elseif ($objp->code == 'CB') {
$bank += $objp->amount;
2021-06-09 03:54:19 +02:00
if (empty($transactionspertype[$objp->code])) {
$transactionspertype[$objp->code] = 0;
}
$transactionspertype[$objp->code] += 1;
2020-01-24 15:59:36 +01:00
} else {
2021-02-23 21:09:01 +01:00
if ($conf->global->$var1 == $bankaccount->id) {
$cash += $objp->amount;
2021-03-01 20:37:16 +01:00
// } elseif ($conf->global->$var2 == $bankaccount->id) $bank+=$objp->amount;
//elseif ($conf->global->$var3 == $bankaccount->id) $cheque+=$objp->amount;
2021-06-09 03:54:19 +02:00
if (empty($transactionspertype['CASH'])) {
$transactionspertype['CASH'] = 0;
}
$transactionspertype['CASH'] += 1;
2021-03-01 20:37:16 +01:00
} else {
2021-02-23 21:09:01 +01:00
$other += $objp->amount;
2021-06-09 03:54:19 +02:00
if (empty($transactionspertype['OTHER'])) {
$transactionspertype['OTHER'] = 0;
}
$transactionspertype['OTHER'] += 1;
2021-02-23 21:09:01 +01:00
}
2019-08-18 09:33:02 +02:00
}
2018-12-08 21:33:54 +01:00
print "</td>\n";
2021-02-23 21:09:01 +01:00
if (!$i) {
$totalarray['nbfield']++;
}
// Type
print '<td class="right">';
2021-02-23 21:09:01 +01:00
print $objp->code;
if (empty($amountpertype[$objp->code])) {
$amountpertype[$objp->code] = 0;
}
print "</td>\n";
2021-02-23 21:09:01 +01:00
if (!$i) {
$totalarray['nbfield']++;
}
// Debit
print '<td class="right">';
if ($objp->amount < 0) {
2021-04-15 15:07:06 +02:00
print '<span class="amount">'.price($objp->amount * -1).'</span>';
$totalarray['val']['totaldebfield'] += $objp->amount;
$amountpertype[$objp->code] += $objp->amount;
}
print "</td>\n";
2021-02-23 21:09:01 +01:00
if (!$i) {
$totalarray['nbfield']++;
}
if (!$i) {
$totalarray['pos'][$totalarray['nbfield']] = 'totaldebfield';
}
// Credit
print '<td class="right">';
if ($objp->amount > 0) {
2021-04-15 15:07:06 +02:00
print '<span class="amount">'.price($objp->amount).'</span>';
$totalarray['val']['totalcredfield'] += $objp->amount;
$amountpertype[$objp->code] -= $objp->amount;
}
print "</td>\n";
2021-02-23 21:09:01 +01:00
if (!$i) {
$totalarray['nbfield']++;
}
if (!$i) {
$totalarray['pos'][$totalarray['nbfield']] = 'totalcredfield';
}
2018-12-08 21:33:54 +01:00
print "</tr>";
$i++;
}
// Show total line
2019-11-05 12:47:38 +01:00
include DOL_DOCUMENT_ROOT.'/core/tpl/list_print_total.tpl.php';
2018-12-08 21:33:54 +01:00
print "</table>";
print "</div>";
//$cash = $amountpertype['LIQ'] + $object->opening;
$cash = price2num($cash + $object->opening, 'MT');
2018-12-15 18:27:38 +01:00
print '<div style="text-align: right">';
print '<h2>';
2020-01-24 15:59:36 +01:00
print $langs->trans("Cash").($transactionspertype['CASH'] ? ' ('.$transactionspertype['CASH'].')' : '').' : <div class="inline-block amount width100">'.price($cash).'</div>';
if ($object->status == $object::STATUS_VALIDATED && $cash != $object->cash) {
print ' <> <div class="inline-block amountremaintopay fontsizeunset">'.$langs->trans("Declared").': '.price($object->cash).'</div>';
2020-01-24 15:26:18 +01:00
}
print "<br>";
2020-01-24 15:59:36 +01:00
//print '<br>';
print $langs->trans("PaymentTypeCHQ").($transactionspertype['CHQ'] ? ' ('.$transactionspertype['CHQ'].')' : '').' : <div class="inline-block amount width100">'.price($cheque).'</div>';
if ($object->status == $object::STATUS_VALIDATED && $cheque != $object->cheque) {
print ' <> <div class="inline-block amountremaintopay fontsizeunset">'.$langs->trans("Declared").' : '.price($object->cheque).'</div>';
2020-01-24 15:26:18 +01:00
}
print "<br>";
2020-01-24 15:59:36 +01:00
//print '<br>';
print $langs->trans("PaymentTypeCB").($transactionspertype['CB'] ? ' ('.$transactionspertype['CB'].')' : '').' : <div class="inline-block amount width100">'.price($bank).'</div>';
if ($object->status == $object::STATUS_VALIDATED && $bank != $object->card) {
print ' <> <div class="inline-block amountremaintopay fontsizeunset">'.$langs->trans("Declared").': '.price($object->card).'</div>';
2020-01-24 15:59:36 +01:00
}
print "<br>";
2020-01-24 15:59:36 +01:00
// print '<br>';
2020-01-24 15:26:18 +01:00
if ($other) {
print ''.$langs->trans("Other").($transactionspertype['OTHER'] ? ' ('.$transactionspertype['OTHER'].')' : '').' : <div class="inline-block amount width100">'.price($other)."</div>";
print '<br>';
2020-01-24 15:26:18 +01:00
}
2018-12-15 18:27:38 +01:00
print $langs->trans("Total").' ('.$totalqty.' '.$langs->trans("Articles").') : <div class="inline-block amount width100">'.price($cash + $cheque + $bank + $other).'</div>';
print '<br>'.$langs->trans("TotalVAT").' : <div class="inline-block amount width100">'.price($totalvat).'</div>';
if ($mysoc->useLocalTax(1)) {
print '<br>'.$langs->trans("TotalLT1").' : <div class="inline-block amount width100">'.price($totallocaltax1).'</div>';
}
if ($mysoc->useLocalTax(1)) {
print '<br>'.$langs->trans("TotalLT2").' : <div class="inline-block amount width100">'.price($totallocaltax2).'</div>';
}
if (!empty($totalvatperrate) && is_array($totalvatperrate)) {
print '<br><br><div class="small inline-block">'.$langs->trans("VATRate").'</div>';
foreach ($totalvatperrate as $keyrate => $valuerate) {
print '<br><div class="small">'.$langs->trans("VATRate").' '.vatrate($keyrate, 1).' : <div class="inline-block amount width100">'.price($valuerate).'</div></div>';
}
}
2018-12-08 21:33:54 +01:00
print '</h2>';
print '</div>';
2018-12-08 21:33:54 +01:00
print '</form>';
2018-12-15 18:27:38 +01:00
2018-12-08 21:33:54 +01:00
$db->free($resql);
2020-05-21 15:05:19 +02:00
} else {
2018-12-08 21:33:54 +01:00
dol_print_error($db);
}
llxFooter();
$db->close();