Error in tax calculation when price option is stored in 8 decimal points.
This is normally a case when we want the price option to be inclusive of our tax amount. Eg we want a 15% GST tax to be applied on the price option so that the total amount is calculated as eg $310.
i.e, user pays $310 which already includes a 15% tax. To have this setup on our priceset, we calculated the exact amount so that when 15% is applied on it, the total amount is displayed as $310.
The main amount, in this case, will be 269.565217391.
Tax Amount = 15% of 269.565217391 = 40.4347826086
Calculated Total Amount = 269.565217391 + 40.4347826086 = 310.
To reproduce -
- Enable Tax from the contribution settings page and add configuration for 15% GST to
Event Feefinancial type.
- Create a price set with the same FT.
- Add a radio price field with option amount = 269.565217391. Save.
- Add another radio price field with option amount = 86.956521739 (so that 15% tax gets a total of $100). Save.
- Use this price set on an event.
- Navigate to the live page. Select both the price option as shown in the below screenshot -
- Click continue and notice that the total amount displayed on the confirm page is incorrectly calculated.
This is because civi rounds the calculated tax amount to 2 decimal points even if 8 dp value is stored for the price option in the database.
The relevant codebase is here https://github.com/civicrm/civicrm-core/blob/master/CRM/Price/BAO/PriceSet.php#L1490-L1493. This calculates the tax amount => rounds it to 2dp and then stores it into the line item array to render it on the confirmation page.