Skip to content

Commit 2a4ce0c

Browse files
author
Noé
committed
Fix: Margin rounding error and division by zero
1 parent 82c3849 commit 2a4ce0c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

htdocs/core/class/html.formmargin.class.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ public function getMarginInfosArray($object, $force_price = false)
169169
}
170170
}
171171
}
172+
173+
// Prevent rounding error and division by zero
174+
$marginInfos['pa_products'] = (float) price2num($marginInfos['pa_products'], 'MAIN_MAX_DECIMALS_TOT');
175+
$marginInfos['pv_products'] = (float) price2num($marginInfos['pv_products'], 'MAIN_MAX_DECIMALS_TOT');
176+
$marginInfos['pa_services'] = (float) price2num($marginInfos['pa_services'], 'MAIN_MAX_DECIMALS_TOT');
177+
$marginInfos['pv_services'] = (float) price2num($marginInfos['pv_services'], 'MAIN_MAX_DECIMALS_TOT');
178+
$marginInfos['pa_total'] = (float) price2num($marginInfos['pa_total'], 'MAIN_MAX_DECIMALS_TOT');
179+
$marginInfos['pv_total'] = (float) price2num($marginInfos['pv_total'], 'MAIN_MAX_DECIMALS_TOT');
180+
172181
if ($marginInfos['pa_products'] > 0) {
173182
$marginInfos['margin_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pa_products'];
174183
}
@@ -187,7 +196,7 @@ public function getMarginInfosArray($object, $force_price = false)
187196
//if ($marginInfos['pv_total'] < 0)
188197
// $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
189198
//else
190-
$marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
199+
$marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
191200
if ($marginInfos['pa_total'] > 0) {
192201
$marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
193202
}

0 commit comments

Comments
 (0)