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
26if (!function_exists('printTotalValCell')) { // allow two list with total on same screen
27
35 function printTotalValCell($type, $val)
36 {
37 // if $totalarray['type'] not present we consider it as number
38 if (empty($type)) {
39 $type = 'real';
40 }
41 switch ($type) {
42 case 'duration':
43 print '<td class="right">';
44 print(!empty($val) ? convertSecondToTime((int) $val, 'allhourmin') : 0);
45 print '</td>';
46 break;
47 case 'string': // This type is no more used. type is now varchar(x)
48 print '<td class="left">';
49 print(!empty($val) ? $val : '');
50 print '</td>';
51 break;
52 case 'stock':
53 print '<td class="right">';
54 print price2num(!empty($val) ? $val : 0, 'MS');
55 print '</td>';
56 break;
57 default:
58 print '<td class="right">';
59 print price(!empty($val) ? $val : 0);
60 print '</td>';
61 break;
62 }
63 }
64}
65
66// Move fields of totalizable into the common array pos and val
67if (!empty($totalarray['totalizable']) && is_array($totalarray['totalizable'])) {
68 foreach ($totalarray['totalizable'] as $keytotalizable => $valtotalizable) {
69 $totalarray['pos'][$valtotalizable['pos']] = $keytotalizable;
70 $totalarray['val'][$keytotalizable] = isset($valtotalizable['total']) ? $valtotalizable['total'] : 0;
71 }
72}
73// Show total line
74if (isset($totalarray['pos'])) {
75 //print '<tfoot>';
76 print '<tr class="liste_total">';
77 $i = 0;
78 while ($i < $totalarray['nbfield']) {
79 $i++;
80 if (!empty($totalarray['pos'][$i])) {
81 printTotalValCell($totalarray['type'][$i] ?? '', empty($totalarray['val'][$totalarray['pos'][$i]]) ? 0 : $totalarray['val'][$totalarray['pos'][$i]]);
82 } else {
83 if ($i == 1) {
84 if ((is_null($limit) || $num < $limit) && empty($offset)) {
85 print '<td>'.$langs->trans("Total").'</td>';
86 } else {
87 print '<td>';
88 if (is_object($form)) {
89 print $form->textwithpicto($langs->trans("Total"), $langs->transnoentitiesnoconv("Totalforthispage"));
90 } else {
91 print $langs->trans("Totalforthispage");
92 }
93 print '</td>';
94 }
95 } else {
96 print '<td></td>';
97 }
98 }
99 }
100 print '</tr>';
101 // Add grand total if necessary ie only if different of page total already printed above
102 if (getDolGlobalString('MAIN_GRANDTOTAL_LIST_SHOW') && (!(is_null($limit) || $num < $limit))) {
103 if (isset($totalarray['pos']) && is_array($totalarray['pos']) && count($totalarray['pos']) > 0) {
104 $sumsarray = false;
105 $tbsumfields = [];
106 foreach ($totalarray['pos'] as $field) {
107 $fieldforsum = preg_replace('/[^a-z0-9]/', '', $field);
108 $tbsumfields[] = "sum($field) as $fieldforsum";
109 }
110 if (isset($sqlfields)) { // In project, commande list, this var is defined
111 $sqlforgrandtotal = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT '. implode(",", $tbsumfields), $sql);
112 } else {
113 $sqlforgrandtotal = preg_replace('/^SELECT[a-zA-Z0-9\._\s\‍(\‍),=<>\:\-\']+\sFROM/', 'SELECT '. implode(",", $tbsumfields). ' FROM ', $sql);
114 }
115 $sqlforgrandtotal = preg_replace('/GROUP BY .*$/', '', $sqlforgrandtotal). '';
116 $resql = $db->query($sqlforgrandtotal);
117 if ($resql) {
118 $sumsarray = $db->fetch_array($resql);
119 } else {
120 //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
121 }
122 if (is_array($sumsarray) && count($sumsarray) > 0) {
123 print '<tr class="liste_grandtotal">';
124 $i = 0;
125 while ($i < $totalarray['nbfield']) {
126 $i++;
127 if (!empty($totalarray['pos'][$i])) {
128 $fieldname = preg_replace('/[^a-z0-9]/', '', $totalarray['pos'][$i]);
129 printTotalValCell($totalarray['type'][$i], $sumsarray[$fieldname]);
130 } else {
131 if ($i == 1) {
132 print '<td>';
133 if (is_object($form)) {
134 print $form->textwithpicto($langs->trans("GrandTotal"), $langs->transnoentitiesnoconv("TotalforAllPages"));
135 } else {
136 print $langs->trans("GrandTotal");
137 }
138 print '</td>';
139 } else {
140 print '<td></td>';
141 }
142 }
143 }
144 print '</tr>';
145 }
146 }
147 }
148 //print '</tfoot>';
149}
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.