From 4277846dc5b9ba8339ad22cb8a788f4bd0827d04 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Tue, 15 Jul 2025 12:17:17 +0000 Subject: [PATCH] Fixed calculations rounding --- Layout/default/WarehouseOffer/PDF_MAIN.php | 25 ++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Layout/default/WarehouseOffer/PDF_MAIN.php b/Layout/default/WarehouseOffer/PDF_MAIN.php index 0c074662a..9cddc3234 100644 --- a/Layout/default/WarehouseOffer/PDF_MAIN.php +++ b/Layout/default/WarehouseOffer/PDF_MAIN.php @@ -42,20 +42,33 @@ $text = $texts[$lang]; // --- Calculations --- $discountAmount = 0; -$subTotalAfterDiscount = $subTotal; +$subTotalAfterDiscount = $subTotal; // Initialize subTotalAfterDiscount with the initial subTotal +$discountPercentage = 0; // Initialize discount percentage to 0 + +// Check if a discount is applied if (isset($offer->totalDiscount) && $offer->totalDiscount > 0) { $discountPercentage = $offer->totalDiscount; - $discountAmount = ($subTotal * $discountPercentage) / 100; - $subTotalAfterDiscount = $subTotal - $discountAmount; + // Calculate the discount amount and round it to 2 decimal places + // Using PHP_ROUND_HALF_UP for consistent rounding (e.g., 0.005 rounds up to 0.01) + $discountAmount = round(($subTotal * $discountPercentage) / 100, 2, PHP_ROUND_HALF_UP); + + // Calculate the subtotal after applying the discount and round it to 2 decimal places + $subTotalAfterDiscount = round($subTotal - $discountAmount, 2, PHP_ROUND_HALF_UP); } $vatAmount = 0; -$grandTotal = $subTotalAfterDiscount; +$grandTotal = $subTotalAfterDiscount; // Initialize grandTotal with the subtotal after discount + +// Check if tax should be included if ($includeTax) { - $vatAmount = $subTotalAfterDiscount * $vatRate; - $grandTotal = $subTotalAfterDiscount + $vatAmount; + // Calculate the VAT amount and round it to 2 decimal places + $vatAmount = round($subTotalAfterDiscount * $vatRate, 2, PHP_ROUND_HALF_UP); + + // Calculate the final grand total by adding VAT and round it to 2 decimal places + $grandTotal = round($subTotalAfterDiscount + $vatAmount, 2, PHP_ROUND_HALF_UP); } +// Format dates (these calculations are not monetary and don't need rounding) $formattedOfferDate = date("d.m.Y", $offerDate); $formattedValidUntil = date("d.m.Y", strtotime("+14 days", $offerDate));