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