Add test cases for no tax. Fix issues found when computing the total if no tax. #744

This commit is contained in:
Korina Cordero 2023-05-31 06:09:54 -04:00
parent 49317188c9
commit 725704e951
13 changed files with 967 additions and 130 deletions

File diff suppressed because it is too large Load diff

View file

@ -24,7 +24,7 @@ class BatteryReplacementWarranty implements InvoiceInterface
return 'battery_warranty';
}
public function compute($criteria, $stype_fees)
public function compute($criteria, $stype_fees, &$total)
{
$stype = $criteria->getServiceType();
@ -48,6 +48,9 @@ class BatteryReplacementWarranty implements InvoiceInterface
'title' => $this->getTitle($batt),
'price' => $price,
];
$qty_price = bcmul($price, $qty, 2);
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
}
}

View file

@ -24,7 +24,7 @@ class BatterySales implements InvoiceInterface
return 'battery_new';
}
public function compute($criteria, $stype_fees)
public function compute($criteria, $stype_fees, &$total)
{
$stype = $criteria->getServiceType();
@ -56,6 +56,11 @@ class BatterySales implements InvoiceInterface
'title' => $this->getTitle($batt),
'price' => $price,
];
$qty_price = bcmul($price, $qty, 2);
$total['sell_price'] = bcadd($total['sell_price'], $qty_price, 2);
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
}
}
}

View file

@ -4,6 +4,8 @@ namespace App\Invoice;
use App\InvoiceInterface;
use App\Ramcar\DiscountApply;
class DiscountType implements InvoiceInterface
{
public function check($criteria)
@ -25,7 +27,7 @@ class DiscountType implements InvoiceInterface
return 'discount_type';
}
public function compute($criteria, $stype_fees)
public function compute($criteria, $stype_fees, &$total)
{
$items = [];

View file

@ -26,13 +26,12 @@ class Fuel implements InvoiceInterface
return 'fuel';
}
public function compute($criteria, $stype_fees)
public function compute($criteria, $stype_fees, &$total)
{
$stype = $criteria->getServiceType();
$items = [];
if ($stype == $this->getID())
{
// check if customer vehicle has a motolite battery
@ -53,6 +52,9 @@ class Fuel implements InvoiceInterface
'price' => $fee,
];
$qty_fee = bcmul($qty, $fee, 2);
$total_price = $qty_fee;
$stype_fees_id = $this->getID() . '_fee_' . $ftype;
switch ($ftype)
@ -68,6 +70,9 @@ class Fuel implements InvoiceInterface
'price' => $price,
];
$qty_price = bcmul($price, $qty, 2);
$total_price = bcadd($total_price, $qty_price, 2);
break;
default:
$qty = 1;
@ -78,8 +83,14 @@ class Fuel implements InvoiceInterface
'title' => $this->getTitle('Unknown'),
'price' => $price,
];
$qty_price = bcmul($price, $qty, 2);
$total_price = bcadd($total_price, $qty_price, 2);
break;
}
$total['total_price'] = bcadd($total['total_price'], $total_price, 2);
}
return $items;

View file

@ -23,7 +23,7 @@ class Jumpstart implements InvoiceInterface
return 'jumpstart_troubleshoot';
}
public function compute($criteria, $stype_fees)
public function compute($criteria, $stype_fees, &$total)
{
$stype = $criteria->getServiceType();
@ -43,6 +43,9 @@ class Jumpstart implements InvoiceInterface
'title' => $this->getServiceTitle(),
'price' => $fee,
];
$qty_price = bcmul($fee, $qty, 2);
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
}
return $items;

View file

@ -23,7 +23,7 @@ class JumpstartWarranty implements InvoiceInterface
return 'jumpstart_warranty';
}
public function compute($criteria, $stype_fees)
public function compute($criteria, $stype_fees, &$total)
{
$stype = $criteria->getServiceType();
@ -43,6 +43,9 @@ class JumpstartWarranty implements InvoiceInterface
'title' => $this->getServiceTitle(),
'price' => $fee,
];
$qty_price = bcmul($fee, $qty, 2);
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
}
return $items;

View file

@ -25,7 +25,7 @@ class Overheat implements InvoiceInterface
return 'overheat';
}
public function compute($criteria, $stype_fees)
public function compute($criteria, $stype_fees, &$total)
{
$stype = $criteria->getServiceType();
$has_coolant = $criteria->hasCoolant();
@ -50,6 +50,9 @@ class Overheat implements InvoiceInterface
'price' => $fee,
];
$qty_fee = bcmul($qty, $fee, 2);
$total_price = $qty_fee;
if ($has_coolant)
{
$coolant_fee = $stype_fees['coolant_fee'];
@ -59,7 +62,12 @@ class Overheat implements InvoiceInterface
'title' => $this->getServiceCoolantTitle(),
'price' => $coolant_fee,
];
$qty_price = bcmul($coolant_fee, $qty, 2);
$total_price = bcadd($total_price, $qty_price, 2);
}
$total['total_price'] = bcadd($total['total_price'], $total_price, 2);
}
return $items;

View file

@ -23,7 +23,7 @@ class PostRecharged implements InvoiceInterface
return 'post_recharged';
}
public function compute($criteria, $stype_fees)
public function compute($criteria, $stype_fees, &$total)
{
$stype = $criteria->getServiceType();
@ -41,6 +41,9 @@ class PostRecharged implements InvoiceInterface
'price' => $fee,
];
$qty_price = bcmul($fee, $qty, 2);
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
}
return $items;

View file

@ -23,7 +23,7 @@ class PostReplacement implements InvoiceInterface
return 'post_replacement';
}
public function compute($criteria, $stype_fees)
public function compute($criteria, $stype_fees, &$total)
{
$stype = $criteria->getServiceType();
@ -41,6 +41,8 @@ class PostReplacement implements InvoiceInterface
'price' => $fee,
];
$qty_price = bcmul($fee, $qty, 2);
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
}
return $items;

View file

@ -53,29 +53,21 @@ class Tax implements InvoiceInterface
$price_minus_vat = bcsub($price, $vat, 2);
$qty_price_minus_vat = bcmul($price_minus_vat, $qty, 2);
$total['sell_price'] = bcadd($total['sell_price'], $qty_price, 2);
$total['vat'] = bcadd($total['vat'], $qty_vat, 2);
$total['vat_ex_price'] = bcadd($total['vat_ex_price'], $qty_price_minus_vat, 2);
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
}
}
else
{
// for all other service types
// add the item price to total_price. we compute VAT from the total price
$total_price = bcadd($total_price, $price, 2);
}
}
}
// compute VAT after adding all item costs, if service type is not battery sales
if (!$is_battery_sales)
{
$total_price = $total['total_price'];
$vat_ex_price = $this->getTaxExclusivePrice($total_price, $tax_rate);
$vat = bcsub($total_price, $vat_ex_price, 2);
$total['total_price'] = $total_price;
$total['vat_ex_price'] = $vat_ex_price;
$total['vat'] = $vat;
}

View file

@ -23,7 +23,7 @@ class TireRepair implements InvoiceInterface
return 'tire';
}
public function compute($criteria, $stype_fees)
public function compute($criteria, $stype_fees, &$total)
{
$stype = $criteria->getServiceType();
@ -46,6 +46,9 @@ class TireRepair implements InvoiceInterface
'title' => $this->getServiceTitle(),
'price' => $fee,
];
$qty_price = bcmul($fee, $qty, 2);
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
}
return $items;

View file

@ -60,7 +60,7 @@ class InvoiceManager
'battery_warranty_fee' => 0,
'other_services_fee' => 200,
'fuel_fee_gas' => 320,
'fuel_fee_diesel' => 320,
'fuel_fee_diesel' => 340,
];
}
@ -124,7 +124,7 @@ class InvoiceManager
{
$items = [];
$computed_items = $active_rule->compute($criteria, $stype_fees);
$computed_items = $active_rule->compute($criteria, $stype_fees, $total);
foreach ($computed_items as $computed_item)
{
if (!empty($computed_item))
@ -133,6 +133,17 @@ class InvoiceManager
}
}
// check if tax is needed
if ($is_taxable)
{
$tax_rate = $this->getTaxRate();
if (isset($active_rules['tax']))
{
$active_rules['tax']->compute($tax_rate, $items, $total);
}
}
// process trade-ins by calling the trade-in rule if there are trade-ins among the entries
if ($flag_trade_in)
{
@ -146,14 +157,12 @@ class InvoiceManager
}
}
// check if tax is needed
if ($is_taxable)
// process promos, if any
if (count($promos) > 0)
{
$tax_rate = $this->getTaxRate();
if (isset($active_rules['tax']))
if (isset($active_rules['discount_type']))
{
$active_rules['tax']->compute($tax_rate, $items, $total);
$discount_items = $active_rules['discount_type']->compute($criteria, $stype_fees);
}
}
@ -171,15 +180,6 @@ class InvoiceManager
$invoice_items[] = $invoice_item;
}
// process promos, if any
if (count($promos) > 0)
{
if (isset($active_rules['discount_type']))
{
$discount_items = $active_rules['discount_type']->compute($criteria, $stype_fees);
}
}
// also need to return the total
$data[] = [
'invoice_items' => $invoice_items,