dolibarr 21.0.0-alpha
list_print_total.tpl.php
1<?php
2/* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
3 */
4
5'@phan-var-force array{nbfield:int,type?:array<int,string>,pos?:array<int,int>,val?:array<int,float>} $totalarray';
6
7// Move fields of totalizable into the common array pos and val
8if (!empty($totalarray['totalizable']) && is_array($totalarray['totalizable'])) {
9 foreach ($totalarray['totalizable'] as $keytotalizable => $valtotalizable) {
10 $totalarray['pos'][$valtotalizable['pos']] = $keytotalizable;
11 $totalarray['val'][$keytotalizable] = isset($valtotalizable['total']) ? $valtotalizable['total'] : 0;
12 }
13}
14// Show total line
15if (isset($totalarray['pos'])) {
16 print '<tfoot>';
17 print '<tr class="liste_total">';
18 $i = 0;
19 while ($i < $totalarray['nbfield']) {
20 $i++;
21 if (!empty($totalarray['pos'][$i])) {
22 printTotalValCell($totalarray['type'][$i] ?? '', empty($totalarray['val'][$totalarray['pos'][$i]]) ? 0 : $totalarray['val'][$totalarray['pos'][$i]]);
23 } else {
24 if ($i == 1) {
25 if ((is_null($limit) || $num < $limit) && empty($offset)) {
26 print '<td>'.$langs->trans("Total").'</td>';
27 } else {
28 print '<td>';
29 if (is_object($form)) {
30 print $form->textwithpicto($langs->trans("Total"), $langs->transnoentitiesnoconv("Totalforthispage"));
31 } else {
32 print $langs->trans("Totalforthispage");
33 }
34 print '</td>';
35 }
36 } else {
37 print '<td></td>';
38 }
39 }
40 }
41 print '</tr>';
42 // Add grand total if necessary ie only if different of page total already printed above
43 if (getDolGlobalString('MAIN_GRANDTOTAL_LIST_SHOW') && (!(is_null($limit) || $num < $limit))) {
44 if (isset($totalarray['pos']) && is_array($totalarray['pos']) && count($totalarray['pos']) > 0) {
45 $sumsarray = false;
46 $tbsumfields = [];
47 foreach ($totalarray['pos'] as $field) {
48 $fieldforsum = preg_replace('/[^a-z0-9]/', '', $field);
49 $tbsumfields[] = "sum($field) as $fieldforsum";
50 }
51 if (isset($sqlfields)) { // In project, commande list, this var is defined
52 $sqlforgrandtotal = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT '. implode(",", $tbsumfields), $sql);
53 } else {
54 $sqlforgrandtotal = preg_replace('/^SELECT[a-zA-Z0-9\._\s\‍(\‍),=<>\:\-\']+\sFROM/', 'SELECT '. implode(",", $tbsumfields). ' FROM ', $sql);
55 }
56 $sqlforgrandtotal = preg_replace('/GROUP BY .*$/', '', $sqlforgrandtotal). '';
57 $resql = $db->query($sqlforgrandtotal);
58 if ($resql) {
59 $sumsarray = $db->fetch_array($resql);
60 } else {
61 //dol_print_error($db); // as we're not sure it's ok for ALL lists, we don't print sq errors, they'll be in logs
62 }
63 if (is_array($sumsarray) && count($sumsarray) > 0) {
64 print '<tr class="liste_grandtotal">';
65 $i = 0;
66 while ($i < $totalarray['nbfield']) {
67 $i++;
68 if (!empty($totalarray['pos'][$i])) {
69 printTotalValCell($totalarray['type'][$i], $sumsarray[$totalarray['pos'][$i]]);
70 } else {
71 if ($i == 1) {
72 print '<td>';
73 if (is_object($form)) {
74 print $form->textwithpicto($langs->trans("GrandTotal"), $langs->transnoentitiesnoconv("TotalforAllPages"));
75 } else {
76 print $langs->trans("GrandTotal");
77 }
78 print '</td>';
79 } else {
80 print '<td></td>';
81 }
82 }
83 }
84 print '</tr>';
85 }
86 }
87 }
88 print '</tfoot>';
89}
90
98function printTotalValCell($type, $val)
99{
100 // if $totalarray['type'] not present we consider it as number
101 if (empty($type)) {
102 $type = 'real';
103 }
104 switch ($type) {
105 case 'duration':
106 print '<td class="right">';
107 print(!empty($val) ? convertSecondToTime((int) $val, 'allhourmin') : 0);
108 print '</td>';
109 break;
110 case 'string': // This type is no more used. type is now varchar(x)
111 print '<td class="left">';
112 print(!empty($val) ? $val : '');
113 print '</td>';
114 break;
115 case 'stock':
116 print '<td class="right">';
117 print price2num(!empty($val) ? $val : 0, 'MS');
118 print '</td>';
119 break;
120 default:
121 print '<td class="right">';
122 print price(!empty($val) ? $val : 0);
123 print '</td>';
124 break;
125 }
126}
convertSecondToTime($iSecond, $format='all', $lengthOfDay=86400, $lengthOfWeek=7)
Return, in clear text, value of a number of seconds in days, hours and minutes.
Definition date.lib.php:241
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.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.