saberworks has asked for the wisdom of the Perl Monks concerning the following question:

For a semi-offtopic question, I hope I don't get burned. This is written in mod_perl though so maybe it counts. Anyway, I work for a company that provides hosted online store software for a niche market. Each retailer is in charge of setting up their store through our web interface. One of the things they do is input tax rates for various states they are required to collect taxes for. They also get to input products and specify whether the product is taxable (there is no way to differentiate whether the product is taxable in some states but not others).

This is an old system written by someone with no sense of logic, so please bear with me.

Now, the problem is coming when I am trying to calculate tax in conjunction with promotions. Retailers can set up promotions such as, "Spend $50.00 and get $5.00 off your order," "Spend $50.00 and get 10% off your order," or "Spend $50.00 and shipping is free."

($50.00 is arbitrary, it can be anything)

The problem comes when trying to calculate tax for this. If the customer orders 3 items for a total of $50.00, and only two of them are taxable, taking a percentage is certainly possible (take 10% off the taxable subtotal and 10% off the non-taxable subtotal, add them together, and you get your promotion amount). Now what if they get $5.00 off their order? Should I calculate a ratio and divide the $5.00 between the taxable and non-taxable? Should I apply it to taxable so they get a better deal? What if the amount is over the taxable, then just apply the rest to the non-taxable portion? The logic gets convoluted.

Currently, the system works like this (written by the old programmer):
  1. Calculate the subtotal (all items, taxable + nontaxable).
  2. Calculate only the taxable subtotal.
  3. Calculate tax to the taxable subtotal.
  4. Calculate shipping.
  5. Add subotal + tax + shipping to get "grand total"
  6. Calculate the discount amount (using the grand total - so if they had $48.00 worth of items and $2.00 tax they would receive the "spend $50.00 and get XX promotion" deal)
  7. Subtract the discount amount from the grand total, and now you have the "real" grand total
This seems wrong to me. It seems like we are charging more tax than we should and we are also giving people discounts who don't "really" qualify. Anyone else have any experience with this?

Replies are listed 'Best First'.
Re: Calculating discounts and tax (OT)
by kvale (Monsignor) on Mar 03, 2005 at 20:47 UTC
    An issue like this is not a matter of programming; it is a matter of state and federal tax law, combined with company policy. The best way to solve this is to ask your customers what their taxation policy is and implement their protocol. That way, when there is a tax dispute, it is not your problem, it is theirs.

    -Mark

Re: Calculating discounts and tax (OT)
by husker (Chaplain) on Mar 03, 2005 at 20:45 UTC
    DISCLAIMER: I not an accountant or lawyer, nor did I sleep at a Holiday Inn Express last night.

    Typically, tax is computer on the sales price. Discounts and rebates are applied after tax is calculated. (Watch how this works next time you use a coupon on a grocery store).

    However, this is a tax question for lawyers and accountants, and may even very by state. I'd definitely consult a tax lawyer in your area, no matter what advice you get here.

Re: Calculating discounts and tax (OT)
by TedPride (Priest) on Mar 04, 2005 at 03:53 UTC
    Discounts are applied after tax is calculated, but taxes are not calculated based on regular retail price. They're calculated based on the price you sell the item for. What the company does for those discounts is calculate internally the price that with tax factored in would result in a discount of x dollars, and give you that price. Or to put it another way, they calculate regular price + tax, subtract discount, then calculate a price internally that with tax added would give you that discount. So:

    Item costs $10.00
    Tax amount is 5%, or $0.50
    Total is $10.50
    Discount is $1.00
    New price you pay is $9.50
    INTERNAL price for the item is $9.05, with $0.45 tax.

    Any other way of doing things wouldn't work.

      I don't understand this post. If you pay $10.00 for an item and there is a $1.00 discount, then you actually paid $9.00 for it and you should be charged 5% of $9.00 for tax and thus you get a tax of .45 and a total of $9.45. Not $9.50. I don't get your reasoning here :(