public Price getTax(Collection<? extends LineItem> lines) { long taxTotalInCents = 0; Decimal orderLevelDiscountMultiplier = getDiscountMultiplier(); for (LineItem lineItem : lines) { long itemTaxTotal; long itemVal = lineItem.getPrice().getCents() * lineItem.getUnitQuantity().longValue(); long itemDiscountTotal = 0; if (lineItem.getAmountDiscount() != null) { itemDiscountTotal += lineItem.getAmountDiscount().getCents(); } if (lineItem.getPercentDiscounts() != null) { long percentageDiscountTotal = lineItem.getPercentDiscounts().stream().mapToLong(value -> (itemVal * value.multiply(100000).longValue())).sum(); itemDiscountTotal += percentageDiscountTotal; } long itemTotalAfterDiscounts = itemVal - itemDiscountTotal; long itemTotalAfterAllDiscounts = orderLevelDiscountMultiplier.multiply(itemTotalAfterDiscounts).longValue(); itemTaxTotal = lineItem.getTaxRates().stream().mapToLong(taxRate -> (itemTotalAfterAllDiscounts * taxRate.getRate().multiply(100000).longValue())).sum(); taxTotalInCents += itemTaxTotal; } BigDecimal taxTotalRoundVal = BigDecimal.valueOf(taxTotalInCents).divide(new BigDecimal(10000000), RoundingMode.HALF_UP); return new Price(taxTotalRoundVal.longValue()); }
This is the method we're utilizing to calculate the tax for the line items.
We're seeing discrepancies for tax values calculated by us and what's displayed on Clover Orders/Register apps, and it's mostly 1 cent over or under.
Example:
Item Price - $1.87
Tax Elements - 8.25% and 5%
Tax Total from our Calculations - $0.248 -> After Round Half Up - $0.25
Tax Total from Clover Orders/Register App - $0.24
Please assist in figuring out the best approach to calculate the taxes for line items. A guide on how tax calculations are handled on Clover end would be really helpful. Because we have to tally the amounts we charge to the values displayed on Clover end, else the order either gets overcharged by 1 cent or gets undercharged and they go on to Partially-Paid status.
TIA.