fix: receipt total now uses actual payment amount from DB
Instead of recalculating the grand total from line items + rates (which can drift by a penny due to floating point), use the actual PaymentFromCreditCard or PaymentPaidInCash values from the Payments table. This ensures the receipt always matches what the customer was actually charged.
This commit is contained in:
parent
fd3183035e
commit
b25198b3f5
1 changed files with 18 additions and 3 deletions
|
|
@ -152,17 +152,32 @@ $isCardOrder = (float) $order['PaymentFromCreditCard'] > 0;
|
|||
$receiptTip = (float) ($order['TipAmount'] ?? 0);
|
||||
$totalBeforeCardFee = $cartGrandTotal + $taxAmountRaw + $payfritFeeRaw + $deliveryFee + $receiptTip;
|
||||
|
||||
if ($isCardOrder) {
|
||||
// Use ACTUAL payment amounts from the database as the source of truth,
|
||||
// so the receipt total always matches what was really charged.
|
||||
$actualCardPaid = (float) $order['PaymentFromCreditCard'];
|
||||
$actualCashPaid = (float) $order['PaymentPaidInCash'];
|
||||
$taxAmount = round($taxAmountRaw * 100) / 100;
|
||||
|
||||
if ($isCardOrder && $actualCardPaid > 0) {
|
||||
// Grand total = what was charged to card + any balance applied
|
||||
$orderGrandTotal = round(($actualCardPaid + $balanceApplied) * 100) / 100;
|
||||
// Back-calculate card/processing fee as the remainder
|
||||
$cardFee = $orderGrandTotal - $cartGrandTotal - $taxAmount - round($payfritFeeRaw * 100) / 100 - $deliveryFee - $receiptTip;
|
||||
$cardFee = round($cardFee * 100) / 100;
|
||||
if ($cardFee < 0) $cardFee = 0;
|
||||
} elseif ($isCashOrder && $actualCashPaid > 0) {
|
||||
$orderGrandTotal = round(($actualCashPaid + $balanceApplied) * 100) / 100;
|
||||
$cardFee = 0;
|
||||
} elseif ($isCardOrder) {
|
||||
// Fallback: recalculate if no payment record yet
|
||||
$cardFeePercent = 0.029;
|
||||
$cardFeeFixed = 0.30;
|
||||
$totalCustomerPaysRaw = ($totalBeforeCardFee + $cardFeeFixed) / (1 - $cardFeePercent);
|
||||
$orderGrandTotal = round($totalCustomerPaysRaw * 100) / 100;
|
||||
$taxAmount = round($taxAmountRaw * 100) / 100;
|
||||
$cardFee = $orderGrandTotal - $cartGrandTotal - $taxAmount - round($payfritFeeRaw * 100) / 100 - $deliveryFee - $receiptTip;
|
||||
$cardFee = round($cardFee * 100) / 100;
|
||||
} else {
|
||||
$orderGrandTotal = round($totalBeforeCardFee * 100) / 100;
|
||||
$taxAmount = round($taxAmountRaw * 100) / 100;
|
||||
$cardFee = 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue