refactored code more beautifully

This commit is contained in:
Luca Haid
2025-07-15 12:18:21 +00:00
parent 4277846dc5
commit bcd8f4ba56

View File

@@ -42,33 +42,22 @@ $text = $texts[$lang];
// --- Calculations ---
$discountAmount = 0;
$subTotalAfterDiscount = $subTotal; // Initialize subTotalAfterDiscount with the initial subTotal
$discountPercentage = 0; // Initialize discount percentage to 0
$subTotalAfterDiscount = $subTotal;
$vatAmount = 0;
// Check if a discount is applied
if (isset($offer->totalDiscount) && $offer->totalDiscount > 0) {
$discountPercentage = $offer->totalDiscount;
// 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; // Initialize grandTotal with the subtotal after discount
$grandTotal = $subTotalAfterDiscount;
// Check if tax should be included
if ($includeTax) {
// 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));