dolibarr 21.0.0-beta
list_print_total.tpl.php
1<?php
2/* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *
17 */
18
24'@phan-var-force array{nbfield:int,type?:array<int,string>,pos?:array<int,int>,val?:array<int,float>} $totalarray';
25
26// Move fields of totalizable into the common array pos and val
27if (!empty($totalarray['totalizable']) && is_array($totalarray['totalizable'])) {
28 foreach ($totalarray['totalizable'] as $keytotalizable => $valtotalizable) {
29 $totalarray['pos'][$valtotalizable['pos']] = $keytotalizable;
30 $totalarray['val'][$keytotalizable] = isset($valtotalizable['total']) ? $valtotalizable['total'] : 0;
31 }
32}
33// Show total line
34if (isset($totalarray['pos'])) {
35 //print '<tfoot>';
36 print '<tr class="liste_total">';
37 $i = 0;
38 while ($i < $totalarray['nbfield']) {
39 $i++;
40 if (!empty($totalarray['pos'][$i])) {
41 printTotalValCell($totalarray['type'][$i] ?? '', empty($totalarray['val'][$totalarray['pos'][$i]]) ? 0 : $totalarray['val'][$totalarray['pos'][$i]]);
42 } else {
43 if ($i == 1) {
44 if ((is_null($limit) || $num < $limit) && empty($offset)) {
45 print '<td>'.$langs->trans("Total").'</td>';
46 } else {
47 print '<td>';
48 if (is_object($form)) {
49 print $form->textwithpicto($langs->trans("Total"), $langs->transnoentitiesnoconv("Totalforthispage"));
50 } else {
51 print $langs->trans("Totalforthispage");
52 }
53 print '</td>';
54 }
55 } else {
56 print '<td></td>';
57 }
58 }
59 }
60 print '</tr>';
61 // Add grand total if necessary ie only if different of page total already printed above
62 if (getDolGlobalString('MAIN_GRANDTOTAL_LIST_SHOW') && (!(is_null($limit) || $num < $limit))) {
63 if (isset($totalarray['pos']) && is_array($totalarray['pos']) && count($totalarray['pos']) > 0) {
64 $sumsarray = false;
65 $tbsumfields = [];
66 foreach ($totalarray['pos'] as $field) {
67 $fieldforsum = preg_replace('/[^a-z0-9]/', '', $field);
68 $tbsumfields[] = "sum($field) as $fieldforsum";
69 }
70 if (isset($sqlfields)) { // In project, commande list, this var is defined
71 $sqlforgrandtotal = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT '. implode(",", $tbsumfields), $sql);
72 } else {
73 $sqlforgrandtotal = preg_replace('/^SELECT[a-zA-Z0-9\._\s\‍(\‍),=<>\:\-\']+\sFROM/', 'SELECT '. implode(",", $tbsumfields). ' FROM ', $sql);
74 }
75 $sqlforgrandtotal = preg_replace('/GROUP BY .*$/', '', $sqlforgrandtotal). '';
76 $resql = $db->query($sqlforgrandtotal);
77 if ($resql) {
78 $sumsarray = $db->fetch_array($resql);
79 } else {
80 //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
81 }
82 if (is_array($sumsarray) && count($sumsarray) > 0) {
83 print '<tr class="liste_grandtotal">';
84 $i = 0;
85 while ($i < $totalarray['nbfield']) {
86 $i++;
87 if (!empty($totalarray['pos'][$i])) {
88 $fieldname = preg_replace('/[^a-z0-9]/', '', $totalarray['pos'][$i]);
89 printTotalValCell($totalarray['type'][$i], $sumsarray[$fieldname]);
90 } else {
91 if ($i == 1) {
92 print '<td>';
93 if (is_object($form)) {
94 print $form->textwithpicto($langs->trans("GrandTotal"), $langs->transnoentitiesnoconv("TotalforAllPages"));
95 } else {
96 print $langs->trans("GrandTotal");
97 }
98 print '</td>';
99 } else {
100 print '<td></td>';
101 }
102 }
103 }
104 print '</tr>';
105 }
106 }
107 }
108 //print '</tfoot>';
109}
110
118function printTotalValCell($type, $val)
119{
120 // if $totalarray['type'] not present we consider it as number
121 if (empty($type)) {
122 $type = 'real';
123 }
124 switch ($type) {
125 case 'duration':
126 print '<td class="right">';
127 print(!empty($val) ? convertSecondToTime((int) $val, 'allhourmin') : 0);
128 print '</td>';
129 break;
130 case 'string': // This type is no more used. type is now varchar(x)
131 print '<td class="left">';
132 print(!empty($val) ? $val : '');
133 print '</td>';
134 break;
135 case 'stock':
136 print '<td class="right">';
137 print price2num(!empty($val) ? $val : 0, 'MS');
138 print '</td>';
139 break;
140 default:
141 print '<td class="right">';
142 print price(!empty($val) ? $val : 0);
143 print '</td>';
144 break;
145 }
146}
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:244
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.