* Copyright (C) 2004-2020 Laurent Destailleur * Copyright (C) 2005-2010 Regis Houssin * Copyright (C) 2012 Vinícius Nogueira * Copyright (C) 2014 Florian Henry * Copyright (C) 2015 Jean-François Ferry * Copyright (C) 2016 Juanjo Menent * Copyright (C) 2017 Alexandre Spangaro * Copyright (C) 2018 Andreu Bisquerra * Copyright (C) 2024 MDW * * 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 * along with this program. If not, see . */ /** * \file htdocs/compta/cashcontrol/report.php * \ingroup cashdesk|takepos * \brief List of sales from POS */ 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 } $optioncss = "print"; // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/compta/cashcontrol/class/cashcontrol.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/cashcontrol/class/cashcontrol.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; $langs->loadLangs(array("bills", "banks")); $id = GETPOSTINT('id'); $summaryonly = GETPOSTINT('summaryonly'); // May be used for ticket Z $object = new CashControl($db); $object->fetch($id); //$limit = GETPOST('limit')?GETPOST('limit', 'int'):$conf->liste_limit; $sortorder = 'ASC'; $sortfield = 'b.datev,b.dateo,b.rowid'; $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), 'b.debit' => array('label' => $langs->trans("Debit"), 'checked' => 1, 'position' => 600), 'b.credit' => array('label' => $langs->trans("Credit"), 'checked' => 1, 'position' => 605), ); $syear = $object->year_close; $smonth = $object->month_close; $sday = $object->day_close; $posmodule = $object->posmodule; $terminalid = $object->posnumber; // Security check if ($user->socid > 0) { // Protection if external user //$socid = $user->socid; accessforbidden(); } if (!$user->hasRight('cashdesk', 'run') && !$user->hasRight('takepos', 'run')) { accessforbidden(); } /* * View */ $title = $langs->trans("CashControl"); $param = ''; $conf->dol_hide_topmenu = 1; $conf->dol_hide_leftmenu = 1; llxHeader('', $title, '', '', 0, 0, array(), array(), $param); print '
'; /*$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,"; $sql.= " b.fk_account, b.fk_type,"; $sql.= " ba.rowid as bankid, ba.ref as bankref,"; $sql.= " bu.url_id,"; $sql.= " f.module_source, f.ref as ref"; $sql.= " FROM "; //if ($bid) $sql.= MAIN_DB_PREFIX."category_bankline as l,"; $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"; // Define filter on invoice $sql.= " AND f.module_source = '".$db->escape($object->posmodule)."'"; $sql.= " AND f.pos_source = '".$db->escape($object->posnumber)."'"; $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(null, '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); $sql.=")"; */ $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 /*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(null, 'Value for key = '.$key.' not supported'); exit; }*/ 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(null, 'Year not defined'); } $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); $i = 0; print "\n"; print '
'; print '

'; $nameterminal = getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$object->posnumber); print $langs->trans("CashControl")." #".$object->id.(($nameterminal != "TAKEPOS_TERMINAL_NAME_".$object->posnumber) ? '
'.$nameterminal : ''); if ($object->status == $object::STATUS_DRAFT) { print '
('.$langs->trans("Draft").")"; } print "

"; print $mysoc->name; print '
'.$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 '
'.$langs->trans("Author").': '.$uservalid->getFullName($langs); } print '
'.$langs->trans("Period").': '.$object->year_close.($object->month_close ? '-'.$object->month_close : '').($object->day_close ? '-'.$object->day_close : ''); print '
'; $invoicetmp = new Facture($db); if (!$summaryonly) { print "

"; print $langs->trans("InitialBankBalance").' - '.$langs->trans("Cash").' :
'.price($object->opening).'
'; print "

"; } else { print '
'; } $param = ''; if (!$summaryonly) { print '
'; print ''."\n"; // Fields title print ''; print_liste_field_titre($arrayfields['b.rowid']['label'], $_SERVER['PHP_SELF'], 'b.rowid', '', $param, '', $sortfield, $sortorder); 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 '); print "\n"; } // Loop on each record $cash = $bank = $cheque = $other = 0; $totalqty = 0; $totalvat = 0; $totalvatperrate = array(); $totallocaltax1 = 0; $totallocaltax2 = 0; $cachebankaccount = array(); $cacheinvoiceid = array(); $transactionspertype = array(); $amountpertype = array(); $totalarray = array('nbfield' => 0, 'pos' => 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]; } $invoicetmp->fetch($objp->facid); if (empty($cacheinvoiceid[$objp->facid])) { $cacheinvoiceid[$objp->facid] = $objp->facid; // First time this invoice is found into list of invoice x payments 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; } } if ($object->posmodule == "takepos") { $var1 = 'CASHDESK_ID_BANKACCOUNT_CASH'.$object->posnumber; } else { $var1 = 'CASHDESK_ID_BANKACCOUNT_CASH'; } if ($objp->code == 'CHQ') { $cheque += $objp->amount; if (empty($transactionspertype[$objp->code])) { $transactionspertype[$objp->code] = 0; } $transactionspertype[$objp->code] += 1; } elseif ($objp->code == 'CB') { $bank += $objp->amount; if (empty($transactionspertype[$objp->code])) { $transactionspertype[$objp->code] = 0; } $transactionspertype[$objp->code] += 1; } else { if (getDolGlobalString($var1) == $bankaccount->id) { $cash += $objp->amount; // } elseif (getDolGlobalString($var2) == $bankaccount->id) $bank+=$objp->amount; //elseif (getDolGlobalString($var3) == $bankaccount->id) $cheque+=$objp->amount; if (empty($transactionspertype['CASH'])) { $transactionspertype['CASH'] = 0; } $transactionspertype['CASH'] += 1; } else { $other += $objp->amount; if (empty($transactionspertype['OTHER'])) { $transactionspertype['OTHER'] = 0; } $transactionspertype['OTHER'] += 1; } } if (empty($amountpertype[$objp->code])) { $amountpertype[$objp->code] = 0; } if ($objp->amount < 0) { $amountpertype[$objp->code] += $objp->amount; } if ($objp->amount > 0) { $amountpertype[$objp->code] -= $objp->amount; } if (!$summaryonly) { print ''; // Ref print ''; if (!$i) { $totalarray['nbfield']++; } // Date ope print '\n"; if (!$i) { $totalarray['nbfield']++; } // Bank account print '\n"; if (!$i) { $totalarray['nbfield']++; } // Type print '\n"; if (!$i) { $totalarray['nbfield']++; } // Debit print '\n"; if (!$i) { $totalarray['nbfield']++; } if (!$i) { $totalarray['pos'][$totalarray['nbfield']] = 'totaldebfield'; } // Credit print '\n"; if (!$i) { $totalarray['nbfield']++; } if (!$i) { $totalarray['pos'][$totalarray['nbfield']] = 'totalcredfield'; } print ""; } $i++; } if (!$summaryonly) { // Show total line include DOL_DOCUMENT_ROOT.'/core/tpl/list_print_total.tpl.php'; print "
'; print $invoicetmp->getNomUrl(1); print ''; print ''.dol_print_date($db->jdate($objp->do), "day").""; print "'; print $bankaccount->getNomUrl(1); print "'; print $objp->code; print "'; if ($objp->amount < 0) { print ''.price($objp->amount * -1).''; $totalarray['val']['totaldebfield'] += $objp->amount; } print "'; if ($objp->amount > 0) { print ''.price($objp->amount).''; $totalarray['val']['totalcredfield'] += $objp->amount; } print "
"; print "
"; } //$cash = $amountpertype['LIQ'] + $object->opening; $newcash = price2num($cash + (float) $object->opening, 'MT'); print '
'; print '

'; print $langs->trans("Cash").(!empty($transactionspertype['CASH']) ? ' ('.$transactionspertype['CASH'].' '.$langs->trans("Articles").')' : '').' : '; if (!$summaryonly) { print '
'.($cash >= 0 ? '+' : '').price($cash).'
'; print '
'.price($newcash).'
'; } else { print '
'; print '
'.price($cash).'
'; } if (!$summaryonly && $object->status == $object::STATUS_VALIDATED && $newcash != $object->cash) { print '
<> '.$langs->trans("Declared").': '.price($object->cash).'
'; } print "
"; //print '
'; print $langs->trans("PaymentTypeCHQ").(!empty($transactionspertype['CHQ']) ? ' ('.$transactionspertype['CHQ'].' '.$langs->trans("Articles").')' : '').' : '; print '
'; print '
'.price($cheque).'
'; if (!$summaryonly && $object->status == $object::STATUS_VALIDATED && $cheque != $object->cheque) { print '
<> '.$langs->trans("Declared").' : '.price($object->cheque).'
'; } print "
"; //print '
'; print $langs->trans("PaymentTypeCB").(!empty($transactionspertype['CB']) ? ' ('.$transactionspertype['CB'].' '.$langs->trans("Articles").')' : '').' : '; print '
'; print '
'.price($bank).'
'; if (!$summaryonly && $object->status == $object::STATUS_VALIDATED && $bank != $object->card) { print '
<> '.$langs->trans("Declared").': '.price($object->card).'
'; } print "
"; // print '
'; if ($other) { print ''.$langs->trans("Other").(!empty($transactionspertype['OTHER']) ? ' ('.$transactionspertype['OTHER'].' '.$langs->trans("Articles").')' : '').' : '; print '
'; print '
'.price($other)."
"; print '
'; } print "
"; print $langs->trans("Total").' ('.$totalqty.' '.$langs->trans("Articles").') :
'.price((float) $cash + (float) $cheque + (float) $bank + (float) $other).'
'; print '
'.$langs->trans("TotalVAT").' :
'.price($totalvat).'
'; if ($mysoc->useLocalTax(1)) { print '
'.$langs->trans("TotalLT1").' :
'.price($totallocaltax1).'
'; } if ($mysoc->useLocalTax(1)) { print '
'.$langs->trans("TotalLT2").' :
'.price($totallocaltax2).'
'; } if (!empty($totalvatperrate) && is_array($totalvatperrate)) { print '

'.$langs->trans("VATRate").'
'; foreach ($totalvatperrate as $keyrate => $valuerate) { print '
'.$langs->trans("VATRate").' '.vatrate($keyrate, 1).' :
'.price($valuerate).'
'; } } print '

'; print '
'; print ''; $db->free($resql); } else { dol_print_error($db); } print '
'; llxFooter(); $db->close();