dolibarr  20.0.0-alpha
report.php
Go to the documentation of this file.
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  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <https://www.gnu.org/licenses/>.
25  */
26 
33 if (!defined('NOREQUIREMENU')) {
34  define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
35 }
36 if (!defined('NOBROWSERNOTIF')) {
37  define('NOBROWSERNOTIF', '1'); // Disable browser notification
38 }
39 
40 $optioncss = "print";
41 
42 // Load Dolibarr environment
43 require '../../main.inc.php';
44 require_once DOL_DOCUMENT_ROOT.'/compta/cashcontrol/class/cashcontrol.class.php';
45 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
46 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
47 require_once DOL_DOCUMENT_ROOT.'/compta/cashcontrol/class/cashcontrol.class.php';
48 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
49 
50 
51 $langs->loadLangs(array("bills", "banks"));
52 
53 $id = GETPOSTINT('id');
54 
55 $object = new CashControl($db);
56 $object->fetch($id);
57 
58 //$limit = GETPOST('limit')?GETPOST('limit', 'int'):$conf->liste_limit;
59 $sortorder = 'ASC';
60 $sortfield = 'b.datev,b.dateo,b.rowid';
61 
62 $arrayfields = array(
63  'b.rowid' => array('label' => $langs->trans("Ref"), 'checked' => 1),
64  'b.dateo' => array('label' => $langs->trans("DateOperationShort"), 'checked' => 1),
65  'b.num_chq' => array('label' => $langs->trans("Number"), 'checked' => 1),
66  'ba.ref' => array('label' => $langs->trans("BankAccount"), 'checked' => 1),
67  'cp.code' => array('label' => $langs->trans("PaymentMode"), 'checked' => 1),
68  'b.debit' => array('label' => $langs->trans("Debit"), 'checked' => 1, 'position' => 600),
69  'b.credit' => array('label' => $langs->trans("Credit"), 'checked' => 1, 'position' => 605),
70 );
71 
72 $syear = $object->year_close;
73 $smonth = $object->month_close;
74 $sday = $object->day_close;
75 
76 $posmodule = $object->posmodule;
77 $terminalid = $object->posnumber;
78 
79 // Security check
80 if ($user->socid > 0) { // Protection if external user
81  //$socid = $user->socid;
83 }
84 if (!$user->hasRight('cashdesk', 'run') && !$user->hasRight('takepos', 'run')) {
86 }
87 
88 
89 /*
90  * View
91  */
92 
93 $title = $langs->trans("CashControl");
94 $param = '';
95 
96 llxHeader('', $title, '', '', 0, 0, array(), array(), $param);
97 
98 /*$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,";
99 $sql.= " b.fk_account, b.fk_type,";
100 $sql.= " ba.rowid as bankid, ba.ref as bankref,";
101 $sql.= " bu.url_id,";
102 $sql.= " f.module_source, f.ref as ref";
103 $sql.= " FROM ";
104 //if ($bid) $sql.= MAIN_DB_PREFIX."bank_class as l,";
105 $sql.= " ".MAIN_DB_PREFIX."bank_account as ba,";
106 $sql.= " ".MAIN_DB_PREFIX."bank as b";
107 $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON bu.fk_bank = b.rowid AND type = 'payment'";
108 $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON bu.url_id = f.rowid";
109 $sql.= " WHERE b.fk_account = ba.rowid";
110 // Define filter on invoice
111 $sql.= " AND f.module_source = '".$db->escape($object->posmodule)."'";
112 $sql.= " AND f.pos_source = '".$db->escape($object->posnumber)."'";
113 $sql.= " AND f.entity IN (".getEntity('facture').")";
114 // Define filter on data
115 if ($syear && ! $smonth) $sql.= " AND dateo BETWEEN '".$db->idate(dol_get_first_day($syear, 1))."' AND '".$db->idate(dol_get_last_day($syear, 12))."'";
116 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))."'";
117 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))."'";
118 else dol_print_error(null, 'Year not defined');
119 // Define filter on bank account
120 $sql.=" AND (b.fk_account = ".((int) $conf->global->CASHDESK_ID_BANKACCOUNT_CASH);
121 $sql.=" OR b.fk_account = ".((int) $conf->global->CASHDESK_ID_BANKACCOUNT_CB);
122 $sql.=" OR b.fk_account = ".((int) $conf->global->CASHDESK_ID_BANKACCOUNT_CHEQUE);
123 $sql.=")";
124 */
125 $sql = "SELECT f.rowid as facid, f.ref, f.datef as do, pf.amount as amount, b.fk_account as bankid, cp.code";
126 $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";
127 $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";
128 $sql .= " AND f.module_source = '".$db->escape($posmodule)."'";
129 $sql .= " AND f.pos_source = '".$db->escape($terminalid)."'";
130 $sql .= " AND f.paye = 1";
131 $sql .= " AND p.entity = ".$conf->entity; // Never share entities for features related to accountancy
132 /*if ($key == 'cash') $sql.=" AND cp.code = 'LIQ'";
133 elseif ($key == 'cheque') $sql.=" AND cp.code = 'CHQ'";
134 elseif ($key == 'card') $sql.=" AND cp.code = 'CB'";
135 else
136 {
137  dol_print_error(null, 'Value for key = '.$key.' not supported');
138  exit;
139 }*/
140 if ($syear && !$smonth) {
141  $sql .= " AND datef BETWEEN '".$db->idate(dol_get_first_day($syear, 1))."' AND '".$db->idate(dol_get_last_day($syear, 12))."'";
142 } elseif ($syear && $smonth && !$sday) {
143  $sql .= " AND datef BETWEEN '".$db->idate(dol_get_first_day($syear, $smonth))."' AND '".$db->idate(dol_get_last_day($syear, $smonth))."'";
144 } elseif ($syear && $smonth && $sday) {
145  $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))."'";
146 } else {
147  dol_print_error(null, 'Year not defined');
148 }
149 
150 $resql = $db->query($sql);
151 if ($resql) {
152  $num = $db->num_rows($resql);
153  $i = 0;
154 
155  print "<!-- title of cash fence -->\n";
156  print '<center>';
157  print '<h2>';
158  if ($object->status != $object::STATUS_DRAFT) {
159  print $langs->trans("CashControl")." ".$object->id;
160  } else {
161  print $langs->trans("CashControl")." - ".$langs->trans("Draft");
162  }
163  print "</h2>";
164  print $mysoc->name;
165  print '<br>'.$langs->trans("DateCreationShort").": ".dol_print_date($object->date_creation, 'dayhour');
166  $userauthor = $object->fk_user_valid;
167  if (empty($userauthor)) {
168  $userauthor = $object->fk_user_creat;
169  }
170 
171  $uservalid = new User($db);
172  if ($userauthor > 0) {
173  $uservalid->fetch($userauthor);
174  print '<br>'.$langs->trans("Author").': '.$uservalid->getFullName($langs);
175  }
176  print '<br>'.$langs->trans("Period").': '.$object->year_close.($object->month_close ? '-'.$object->month_close : '').($object->day_close ? '-'.$object->day_close : '');
177  print '</center>';
178 
179  $invoicetmp = new Facture($db);
180 
181  print "<div style='text-align: right'><h2>";
182  print $langs->trans("InitialBankBalance").' - '.$langs->trans("Cash").' : <div class="inline-block amount width100">'.price($object->opening).'</div>';
183  print "</h2></div>";
184 
185  print '<div class="div-table-responsive">';
186  print '<table class="tagtable liste">'."\n";
187 
188  $param = '';
189 
190  // Fields title
191  print '<tr class="liste_titre">';
192  print_liste_field_titre($arrayfields['b.rowid']['label'], $_SERVER['PHP_SELF'], 'b.rowid', '', $param, '', $sortfield, $sortorder);
193  print_liste_field_titre($arrayfields['b.dateo']['label'], $_SERVER['PHP_SELF'], 'b.dateo', '', $param, '"', $sortfield, $sortorder, 'center ');
194  print_liste_field_titre($arrayfields['ba.ref']['label'], $_SERVER['PHP_SELF'], 'ba.ref', '', $param, '', $sortfield, $sortorder, 'right ');
195  print_liste_field_titre($arrayfields['cp.code']['label'], $_SERVER['PHP_SELF'], 'cp.code', '', $param, '', $sortfield, $sortorder, 'right ');
196  print_liste_field_titre($arrayfields['b.debit']['label'], $_SERVER['PHP_SELF'], 'b.amount', '', $param, '', $sortfield, $sortorder, 'right ');
197  print_liste_field_titre($arrayfields['b.credit']['label'], $_SERVER['PHP_SELF'], 'b.amount', '', $param, '', $sortfield, $sortorder, 'right ');
198  print "</tr>\n";
199 
200  // Loop on each record
201  $cash = $bank = $cheque = $other = 0;
202 
203  $totalqty = 0;
204  $totalvat = 0;
205  $totalvatperrate = array();
206  $totallocaltax1 = 0;
207  $totallocaltax2 = 0;
208  $cachebankaccount = array();
209  $cacheinvoiceid = array();
210  $transactionspertype = array();
211  $amountpertype = array();
212 
213  $totalarray = array('nbfield' => 0, 'pos' => array());
214  while ($i < $num) {
215  $objp = $db->fetch_object($resql);
216 
217  // Load bankaccount
218  if (empty($cachebankaccount[$objp->bankid])) {
219  $bankaccounttmp = new Account($db);
220  $bankaccounttmp->fetch($objp->bankid);
221  $cachebankaccount[$objp->bankid] = $bankaccounttmp;
222  $bankaccount = $bankaccounttmp;
223  } else {
224  $bankaccount = $cachebankaccount[$objp->bankid];
225  }
226 
227  $invoicetmp->fetch($objp->facid);
228 
229  if (empty($cacheinvoiceid[$objp->facid])) {
230  $cacheinvoiceid[$objp->facid] = $objp->facid; // First time this invoice is found into list of invoice x payments
231  foreach ($invoicetmp->lines as $line) {
232  $totalqty += $line->qty;
233  $totalvat += $line->total_tva;
234  if ($line->tva_tx) {
235  if (empty($totalvatperrate[$line->tva_tx])) {
236  $totalvatperrate[$line->tva_tx] = 0;
237  }
238  $totalvatperrate[$line->tva_tx] += $line->total_tva;
239  }
240  $totallocaltax1 += $line->total_localtax1;
241  $totallocaltax2 += $line->total_localtax2;
242  }
243  }
244 
245  print '<tr class="oddeven">';
246 
247  // Ref
248  print '<td class="nowrap left">';
249  print $invoicetmp->getNomUrl(1);
250  print '</td>';
251  if (!$i) {
252  $totalarray['nbfield']++;
253  }
254 
255  // Date ope
256  print '<td class="nowrap left">';
257  print '<span id="dateoperation_'.$objp->rowid.'">'.dol_print_date($db->jdate($objp->do), "day")."</span>";
258  print "</td>\n";
259  if (!$i) {
260  $totalarray['nbfield']++;
261  }
262 
263  if ($object->posmodule == "takepos") {
264  $var1 = 'CASHDESK_ID_BANKACCOUNT_CASH'.$object->posnumber;
265  } else {
266  $var1 = 'CASHDESK_ID_BANKACCOUNT_CASH';
267  }
268 
269  // Bank account
270  print '<td class="nowrap right">';
271  print $bankaccount->getNomUrl(1);
272  if ($objp->code == 'CHQ') {
273  $cheque += $objp->amount;
274  if (empty($transactionspertype[$objp->code])) {
275  $transactionspertype[$objp->code] = 0;
276  }
277  $transactionspertype[$objp->code] += 1;
278  } elseif ($objp->code == 'CB') {
279  $bank += $objp->amount;
280  if (empty($transactionspertype[$objp->code])) {
281  $transactionspertype[$objp->code] = 0;
282  }
283  $transactionspertype[$objp->code] += 1;
284  } else {
285  if (getDolGlobalString($var1) == $bankaccount->id) {
286  $cash += $objp->amount;
287  // } elseif (getDolGlobalString($var2) == $bankaccount->id) $bank+=$objp->amount;
288  //elseif (getDolGlobalString($var3) == $bankaccount->id) $cheque+=$objp->amount;
289  if (empty($transactionspertype['CASH'])) {
290  $transactionspertype['CASH'] = 0;
291  }
292  $transactionspertype['CASH'] += 1;
293  } else {
294  $other += $objp->amount;
295  if (empty($transactionspertype['OTHER'])) {
296  $transactionspertype['OTHER'] = 0;
297  }
298  $transactionspertype['OTHER'] += 1;
299  }
300  }
301  print "</td>\n";
302  if (!$i) {
303  $totalarray['nbfield']++;
304  }
305 
306  // Type
307  print '<td class="right">';
308  print $objp->code;
309  if (empty($amountpertype[$objp->code])) {
310  $amountpertype[$objp->code] = 0;
311  }
312  print "</td>\n";
313  if (!$i) {
314  $totalarray['nbfield']++;
315  }
316 
317  // Debit
318  print '<td class="right">';
319  if ($objp->amount < 0) {
320  print '<span class="amount">'.price($objp->amount * -1).'</span>';
321  $totalarray['val']['totaldebfield'] += $objp->amount;
322  $amountpertype[$objp->code] += $objp->amount;
323  }
324  print "</td>\n";
325  if (!$i) {
326  $totalarray['nbfield']++;
327  }
328  if (!$i) {
329  $totalarray['pos'][$totalarray['nbfield']] = 'totaldebfield';
330  }
331 
332  // Credit
333  print '<td class="right">';
334  if ($objp->amount > 0) {
335  print '<span class="amount">'.price($objp->amount).'</span>';
336  $totalarray['val']['totalcredfield'] += $objp->amount;
337  $amountpertype[$objp->code] -= $objp->amount;
338  }
339  print "</td>\n";
340  if (!$i) {
341  $totalarray['nbfield']++;
342  }
343  if (!$i) {
344  $totalarray['pos'][$totalarray['nbfield']] = 'totalcredfield';
345  }
346 
347  print "</tr>";
348 
349  $i++;
350  }
351 
352  // Show total line
353  include DOL_DOCUMENT_ROOT.'/core/tpl/list_print_total.tpl.php';
354 
355  print "</table>";
356  print "</div>";
357 
358  //$cash = $amountpertype['LIQ'] + $object->opening;
359  $cash = price2num($cash + (float) $object->opening, 'MT');
360 
361  print '<div style="text-align: right">';
362  print '<h2>';
363 
364  print $langs->trans("Cash").(!empty($transactionspertype['CASH']) ? ' ('.$transactionspertype['CASH'].')' : '').' : <div class="inline-block amount width100">'.price($cash).'</div>';
365  if ($object->status == $object::STATUS_VALIDATED && $cash != $object->cash) {
366  print ' <> <div class="inline-block amountremaintopay fontsizeunset">'.$langs->trans("Declared").': '.price($object->cash).'</div>';
367  }
368  print "<br>";
369 
370  //print '<br>';
371  print $langs->trans("PaymentTypeCHQ").(!empty($transactionspertype['CHQ']) ? ' ('.$transactionspertype['CHQ'].')' : '').' : <div class="inline-block amount width100">'.price($cheque).'</div>';
372  if ($object->status == $object::STATUS_VALIDATED && $cheque != $object->cheque) {
373  print ' <> <div class="inline-block amountremaintopay fontsizeunset">'.$langs->trans("Declared").' : '.price($object->cheque).'</div>';
374  }
375  print "<br>";
376 
377  //print '<br>';
378  print $langs->trans("PaymentTypeCB").(!empty($transactionspertype['CB']) ? ' ('.$transactionspertype['CB'].')' : '').' : <div class="inline-block amount width100">'.price($bank).'</div>';
379  if ($object->status == $object::STATUS_VALIDATED && $bank != $object->card) {
380  print ' <> <div class="inline-block amountremaintopay fontsizeunset">'.$langs->trans("Declared").': '.price($object->card).'</div>';
381  }
382  print "<br>";
383 
384  // print '<br>';
385  if ($other) {
386  print ''.$langs->trans("Other").($transactionspertype['OTHER'] ? ' ('.$transactionspertype['OTHER'].')' : '').' : <div class="inline-block amount width100">'.price($other)."</div>";
387  print '<br>';
388  }
389 
390  print $langs->trans("Total").' ('.$totalqty.' '.$langs->trans("Articles").') : <div class="inline-block amount width100">'.price((float) $cash + (float) $cheque + (float) $bank + (float) $other).'</div>';
391 
392  print '<br>'.$langs->trans("TotalVAT").' : <div class="inline-block amount width100">'.price($totalvat).'</div>';
393 
394  if ($mysoc->useLocalTax(1)) {
395  print '<br>'.$langs->trans("TotalLT1").' : <div class="inline-block amount width100">'.price($totallocaltax1).'</div>';
396  }
397  if ($mysoc->useLocalTax(1)) {
398  print '<br>'.$langs->trans("TotalLT2").' : <div class="inline-block amount width100">'.price($totallocaltax2).'</div>';
399  }
400 
401  if (!empty($totalvatperrate) && is_array($totalvatperrate)) {
402  print '<br><br><div class="small inline-block">'.$langs->trans("VATRate").'</div>';
403  foreach ($totalvatperrate as $keyrate => $valuerate) {
404  print '<br><div class="small">'.$langs->trans("VATRate").' '.vatrate($keyrate, 1).' : <div class="inline-block amount width100">'.price($valuerate).'</div></div>';
405  }
406  }
407 
408  print '</h2>';
409  print '</div>';
410 
411  print '</form>';
412 
413  $db->free($resql);
414 } else {
415  dol_print_error($db);
416 }
417 
418 llxFooter();
419 
420 $db->close();
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
Class to manage bank accounts.
Class to manage cash fence.
Class to manage invoices.
Class to manage Dolibarr users.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:744
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:594
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:613
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0, $html=0)
Return a string with VAT rate label formatted for view output Used into pdf and HTML pages.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.