From e89f93024f4c7afae63bea8dd6717e0797db7246 Mon Sep 17 00:00:00 2001 From: Vincent Maury Date: Mon, 12 Feb 2024 05:34:45 +0100 Subject: [PATCH] NEW #24031 add option MAIN_GRANDTOTAL_LIST_SHOW to always show grand total to lists (#27247) * Fix #24031 add - if option seleced - grand total to lists * Fix #24031 add - if option seleced - grand total to lists * Fix #24031 add - if option seleced - grand total to lists * Fix #24031 add - if option selected - grand total to lists * Update ihm.php --------- Co-authored-by: Laurent Destailleur --- htdocs/admin/ihm.php | 2 +- htdocs/core/tpl/list_print_total.tpl.php | 112 +++++++++++++++++------ htdocs/langs/en_US/admin.lang | 1 + htdocs/langs/en_US/main.lang | 2 + htdocs/langs/fr_FR/admin.lang | 1 + htdocs/langs/fr_FR/main.lang | 2 + 6 files changed, 93 insertions(+), 27 deletions(-) diff --git a/htdocs/admin/ihm.php b/htdocs/admin/ihm.php index 682d5271d5d..2e13c2d5c7a 100644 --- a/htdocs/admin/ihm.php +++ b/htdocs/admin/ihm.php @@ -437,7 +437,7 @@ if ($mode == 'other') { print '' . $langs->trans("DefaultMaxSizeShortList") . ''; print ''; - // Max size of lists + // Display checkboxes and fields menu left / right print '' . $langs->trans("MAIN_CHECKBOX_LEFT_COLUMN") . ''; print ajax_constantonoff("MAIN_CHECKBOX_LEFT_COLUMN", array(), $conf->entity, 0, 0, 1, 0, 0, 0, '', 'other'); print ''; diff --git a/htdocs/core/tpl/list_print_total.tpl.php b/htdocs/core/tpl/list_print_total.tpl.php index 88aff7cce82..970eb93c84d 100644 --- a/htdocs/core/tpl/list_print_total.tpl.php +++ b/htdocs/core/tpl/list_print_total.tpl.php @@ -9,37 +9,13 @@ if (!empty($totalarray['totalizable']) && is_array($totalarray['totalizable'])) } // Show total line if (isset($totalarray['pos'])) { + print ''; print ''; $i = 0; while ($i < $totalarray['nbfield']) { $i++; if (!empty($totalarray['pos'][$i])) { - // if $totalarray['type'] not present we consider it as number - if (empty($totalarray['type'][$i])) { - $totalarray['type'][$i] = 'real'; - } - switch ($totalarray['type'][$i]) { - case 'duration': - print ''; - print (!empty($totalarray['val'][$totalarray['pos'][$i]]) ? convertSecondToTime($totalarray['val'][$totalarray['pos'][$i]], 'allhourmin') : 0); - print ''; - break; - case 'string': // This type is no more used. type is now varchar(x) - print ''; - print (!empty($totalarray['val'][$totalarray['pos'][$i]]) ? $totalarray['val'][$totalarray['pos'][$i]] : ''); - print ''; - break; - case 'stock': - print ''; - print price2num(!empty($totalarray['val'][$totalarray['pos'][$i]]) ? $totalarray['val'][$totalarray['pos'][$i]] : 0, 'MS'); - print ''; - break; - default: - print ''; - print price(!empty($totalarray['val'][$totalarray['pos'][$i]]) ? $totalarray['val'][$totalarray['pos'][$i]] : 0); - print ''; - break; - } + printTotalValCell($totalarray['type'][$i], $totalarray['val'][$totalarray['pos'][$i]]); } else { if ($i == 1) { if (is_null($limit) || $num < $limit) { @@ -59,4 +35,88 @@ if (isset($totalarray['pos'])) { } } print ''; + // Add grand total if necessary ie only if different of page total already printed above + if (getDolGlobalString('MAIN_GRANDTOTAL_LIST_SHOW') && (!(is_null($limit) || $num < $limit))) { + if (isset($totalarray['pos']) && is_array($totalarray['pos']) && count($totalarray['pos']) > 0) { + $sumsarray = false; + $tbsumfields = []; + foreach ($totalarray['pos'] as $field) { + $fieldforsum = preg_replace('/[^a-z0-9]/', '', $field); + $tbsumfields[] = "sum($field) as $fieldforsum"; + } + if (isset($sqlfields)) { // In project, commande list, this var is defined + $sqlforgrandtotal = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT '. implode(",", $tbsumfields), $sql); + } else { + $sqlforgrandtotal = preg_replace('/^SELECT[a-zA-Z0-9\._\s\(\),=<>\:\-\']+\sFROM/', 'SELECT '. implode(",", $tbsumfields). ' FROM ', $sql); + } + $sqlforgrandtotal = preg_replace('/GROUP BY .*$/', '', $sqlforgrandtotal). ''; + $resql = $db->query($sqlforgrandtotal); + if ($resql) { + $sumsarray = $db->fetch_array($resql); + } else { + //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 + } + if (is_array($sumsarray) && count($sumsarray) >0) { + print ''; + $i = 0; + while ($i < $totalarray['nbfield']) { + $i++; + if (!empty($totalarray['pos'][$i])) { + printTotalValCell($totalarray['type'][$i], $sumsarray[$totalarray['pos'][$i]]); + } else { + if ($i == 1) { + print ''; + if (is_object($form)) { + print $form->textwithpicto($langs->trans("GrandTotal"), $langs->transnoentitiesnoconv("TotalforAllPages")); + } else { + print $langs->trans("GrandTotal"); + } + print ''; + } else { + print ''; + } + } + } + print ''; + } + } + } + print ''; +} + +/** print a total cell value according to its type + * + * @param string $type of field (duration, string..) + * @param string $val the value to display + * + * @return void (direct print) + */ +function printTotalValCell($type, $val) +{ + // if $totalarray['type'] not present we consider it as number + if (empty($type)) { + $type = 'real'; + } + switch ($type) { + case 'duration': + print ''; + print (!empty($val) ? convertSecondToTime($val, 'allhourmin') : 0); + print ''; + break; + case 'string': // This type is no more used. type is now varchar(x) + print ''; + print (!empty($val) ? $val : ''); + print ''; + break; + case 'stock': + print ''; + print price2num(!empty($val) ? $val : 0, 'MS'); + print ''; + break; + default: + print ''; + print price(!empty($val) ? $val : 0); + print ''; + break; + } } diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 30beafc1214..49e1d02e212 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -1197,6 +1197,7 @@ Skin=Skin theme DefaultSkin=Default skin theme MaxSizeList=Max length for list DefaultMaxSizeList=Default max length for lists +DisplayGrandTotalInList=Display grand total (for all pages) in lists footer DefaultMaxSizeShortList=Default max length for short lists (i.e. in customer card) MessageOfDay=Message of the day MessageLogin=Login page message diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang index e94b0281eb3..29a03f34ad3 100644 --- a/htdocs/langs/en_US/main.lang +++ b/htdocs/langs/en_US/main.lang @@ -420,6 +420,8 @@ TotalTTCShort=Total (inc. tax) TotalHT=Total (excl. tax) TotalHTforthispage=Total (excl. tax) for this page Totalforthispage=Total for this page +GrandTotal=Grand total +TotalforAllPages=Total for all pages TotalTTC=Total (inc. tax) TotalTTCToYourCredit=Total (inc. tax) to your credit TotalVAT=Total tax diff --git a/htdocs/langs/fr_FR/admin.lang b/htdocs/langs/fr_FR/admin.lang index 9864434ff14..b8100956424 100644 --- a/htdocs/langs/fr_FR/admin.lang +++ b/htdocs/langs/fr_FR/admin.lang @@ -1202,6 +1202,7 @@ Skin=Thème visuel DefaultSkin=Thème visuel par défaut MaxSizeList=Longueur maximale des listes DefaultMaxSizeList=Longueur maximale par défaut des listes +DisplayGrandTotalInList=Affiche le total général (de toutes les pages) en bas des listes DefaultMaxSizeShortList=Longueur maximale par défaut des listes courtes (e.g. dans la fiche client) MessageOfDay=Message du jour MessageLogin=Message page de connexion diff --git a/htdocs/langs/fr_FR/main.lang b/htdocs/langs/fr_FR/main.lang index 0e1cf0ec73a..4b5abcdf118 100644 --- a/htdocs/langs/fr_FR/main.lang +++ b/htdocs/langs/fr_FR/main.lang @@ -420,6 +420,8 @@ TotalTTCShort=Total TTC TotalHT=Total HT TotalHTforthispage=Montant (HT) pour la page Totalforthispage=Total pour cette page +GrandTotal=Total général +TotalforAllPages=Total général de toutes les pages TotalTTC=Total TTC TotalTTCToYourCredit=Total TTC à votre crédit TotalVAT=Total TVA