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