dolibarr 20.0.0
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 printTotalValCell($totalarray['type'][$i], $sumsarray[$totalarray['pos'][$i]]);
68 } else {
69 if ($i == 1) {
70 print '<td>';
71 if (is_object($form)) {
72 print $form->textwithpicto($langs->trans("GrandTotal"), $langs->transnoentitiesnoconv("TotalforAllPages"));
73 } else {
74 print $langs->trans("GrandTotal");
75 }
76 print '</td>';
77 } else {
78 print '<td></td>';
79 }
80 }
81 }
82 print '</tr>';
83 }
84 }
85 }
86 print '</tfoot>';
87}
88
96function printTotalValCell($type, $val)
97{
98 // if $totalarray['type'] not present we consider it as number
99 if (empty($type)) {
100 $type = 'real';
101 }
102 switch ($type) {
103 case 'duration':
104 print '<td class="right">';
105 print(!empty($val) ? convertSecondToTime($val, 'allhourmin') : 0);
106 print '</td>';
107 break;
108 case 'string': // This type is no more used. type is now varchar(x)
109 print '<td class="left">';
110 print(!empty($val) ? $val : '');
111 print '</td>';
112 break;
113 case 'stock':
114 print '<td class="right">';
115 print price2num(!empty($val) ? $val : 0, 'MS');
116 print '</td>';
117 break;
118 default:
119 print '<td class="right">';
120 print price(!empty($val) ? $val : 0);
121 print '</td>';
122 break;
123 }
124}
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.