Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zidance's avatar

What is the way to assign discount amount on items price in purchase order?

For example, i have a value as shown below

$item_A = $ 112.50 @ 6% tax, after tax = 9.25
$item_B = $ 10.00 @ 0% tax

I bought 2 item_A ($ 238.50) and 1 item_B ($ 10)

$sub_total = $ 235.00
$tax_amt = $ 13.50
$total = $ 248.50

At here i want to apply a discount $40 and then share among on the items price, which mean my total should be $208.75.

discount will be based on setting either applied before or after tax

// formula to share among
$distribute = sub-total of item / total amount * discount

After get the $distribute, then will divide the quantity and add to the current unit price.

In this case, if i am recalculating the total amount, it will became lesser due to the unit price of item applied the discount.

My expected result is when applied $40 discount on $248.50, it will became $208.50 with a $40 discount applied even recalculate it.

is it we need to remain the tax amount of the original price?

What is the solution to use to deal with this situation? Anyone can help and give suggestion on this case? Thanks.

0 likes
3 replies
bobbybouwmann's avatar

A discount is a discount right? The original price and original tax won't change, whenever you apply a discount on the whole order. Instead, you subtract something from the total amount. That is the easiest approach.

So there are two forms of discounts. A discount that you apply at the whole order, so $40 off of the total amount or 20% off of the total amount.

The second form of discount is based on a discount per item. This way you don't have to apply it at the end anymore. In that case, you do change the original price and tax since it only applies to that specific item.

It sounds to me that you have the first form where you subtract $40 of the total amount. So keep it as simple as possible here, by just discounting it from the total price ;)

If you do want to update the tax values as well, you indeed have to calculate everything per item which is way more complicated and also you get rounding issues with 2 numbers behind the comma.

1 like
zidance's avatar

There's different discount method to be stated to allow user to choose. Just as you said, two forms of discounts. The first one will be easy if direct minus from total without changing the original price and tax. But it relates to inventory, and that's the request i received that need to share the price among the items by percentage occupied in total.

I am not sure is there a way to achieve that, hmm...

bobbybouwmann's avatar

Mmh you determine a percentage that you need to take off the price for each product. You can do that by taking the sum of the quantity of all the products. From there you can calculate the percentage per item which should be discounted based on the discount price. You can then remove that percentage of the product and that should work.

Not easy to do because of rounding, but it's probably a working solution.

Let me know if you need an example.

Please or to participate in this conversation.