0
votes

This is indeed a nightmare. I really need the help of your collective minds on this one:

In Canada, taxes are calculated differently from province to province:

Alberta - 5%
British Columbia - 5% GST and 7% PST

Calculating the straight tax on the price of each product is easy ... I loop over all of the items within the my cart and calculate the tax per the product price. Even when items are PST exempt, I can check for that...

The tricky part is when I have to apply a coupon code with a discount!

Obviously any discounts need to be applied to the pre-tax amounts... but I need to process each item separately to calculate the tax. I can't deduct say, $20 off of the cart total, because each item has it's own set price... which means while I'm processing each item, I need to know what the prices are AFTER the discount.

But remember how some products are PST exempt? Well, that weighs into the complication here:

Item 1 - $24.99 - GST (5%) and PST (7%)
Item 2 - $11.99 - GST (5%)

Discount - $20.00

How on earth can I apply the cart discount, but keep the integrity of the taxes?

1

1 Answers

0
votes

Can you do something like this (pseudocode)?

$item_price = $10
$final_price = ((($item_price * $discount) * $gst) * $pst)

then set discount to the inverse of 1.

i.e. no discount: $discount = 1.0; 20% discount: $discount = .8;

Edit: actually - you havent really given enough information. If the discount is a percentage of the total - then my code above will work.

But if your discount is a 'fixed' price - then you have a problem. Do you take the price off the first item, or distribute it to all items evenly across the cart? There will be tax implications either way.

Assuming you want to distribute the price across all items - then something like this would work:

$discount = $20;
$num_of_items_in_cart = 5;
$discount_per_item = $discount/$num_of_items_in_cart;
$final_price = ((($item_price * $discount_per_item) * $gst) * $pst)