Ecommerce Developer
 
 

Design & Inspiration

Coding for Tax Calculations: Everything You Never Wanted to Know – Part 2

 

Editor's Note: This is part two of our two-part series explaining the complexities of coding for taxes arising from ecommerce sales. The author is David Brokenshire, a programmer with Elastic Path, an ecommerce platform for enterprise-level merchants. Part 1 of Brokenshire's series was published earlier.

Calculating sales-related taxes is complicated enough for a single retail store in one location, let alone when you have multiple customers and locations worldwide, as is the case with ecommerce sites.

Every jurisdiction has its own rules and you need to know these rules when writing code to perform tax calculations. Because how to calculate taxes is complicated, this is the second of two articles on the topic.

In this article, I will discuss some of the issues involved, things you need to know, and the "gotchas" to watch out for.

I Am a Software Engineer, Not a Lawyer or Accountant

As I mentioned in the first article, I’m not a lawyer and I’m not an accountant. So I’m not providing legal or accounting advice. If you are handling tax calculations in code, do yourself and your company or customers a favor and consult a tax expert. But I am a software engineer with experience coding tax calculations (I just worked on this at Elastic Path Software, where I work), and it is from that experience that I write.

Calculating Multiple Taxes

In many jurisdictions, multiple taxes are added to customer orders. For example, federal, state, municipal and environmental taxes may be added to a customer order. In addition, specific items in a cart may have specific taxes applied.

As discussed in Part 1, calculating these taxes requires different formulas depending on whether prices for items in the customer's cart are tax exclusive or tax inclusive. Suggestions for coding formulas are provided below, followed by an example.

Tax Exclusive

Tax exclusive calculations refer to the tax being an addition to the price of the item. At checkout, the customer would pay for the item plus the tax on that item for a total purchase price. As programmers, you can compute the tax separately for each item or for the entire order. Those options are called, respectively, line level calculations and order level calculations, as explained below.

Line Level Calculations:

  1. For each tax that applies to an item, calculate the amount of tax payable.
  2. Repeat for all items.
  3. Do one of the following:
    • To get the tax totals, sum all the tax values calculated.
    • To get individual tax totals, such as how much federal tax applies on the order, sum each individual tax. For example, sum the federal tax owing for each line item into a federal tax total for the order.

Table showing order example

For the above graphic, the individual tax totals would be:

Cuteness tax: $4.50 Federal tax: $9.00 + $1.00 + $8.00 + $.20 = $18.20 State tax: $3.60 + $.40 + $3.20 = $7.20

And the total taxes due would be:

$4.50 + $18.20 + $7.20 = $29.90

Order Level Calculations:

You cannot directly calculate taxes at the order level. You must examine each line in an order to decide whether to apply a specific tax or not. The basic process is to determine to which lines a specific tax applies to, add the prices of these lines, then determine the tax on that sum. To determine the total taxes on the order, repeat this process for each tax, then sum the results, and finally round to the currency level.

Tax Inclusive

Tax inclusive calculations refer to the tax being included in the price of the item. At checkout, the price of the items includes the taxes due from the merchant. Like tax exclusive calculations, you can compute the tax separately for each item or for the entire order.

Line Level Calculations:

Tax inclusive calculations at the line item level are a bit more complex than those for tax exclusive calculations. We need to figure out how much of the price is due to each tax. To do this, we essentially figure out the total amount of tax in the price and then figure out the proportion for each tax.

To calculate multiple inclusive taxes, such as a value added tax (VAT) and an environmental tax (ENV):

vatTaxes = (VAT/(1 + VAT + ENV))*price, envTaxes = (ENV/(1 + VAT + ENV))*price

Changing Tax Rates

Tax rates change. It may be infrequent for large national sales taxes, but when you look at all the jurisdictions that an ecommerce merchant may sell into, you find rates change regularly. In Canada over the last five years the federal Goods and Services Tax (GST) has decreased twice and several provinces are merging their local sales tax with the federal tax. So you may see taxes that appear and disappear, as well as tax rates that change. You must keep track of tax rates and actual taxes calculated on an order over time for accounting purposes, and for calculating refunds on returns.

Taxes and Order Returns

In the simplest case for returns, a customer returns an entire order and gets the full price paid and the taxes back. If the customer returns only a portion of the order things get more complicated.

There are two basic options for determining how much tax to return to a customer when that customer makes a partial return. Option one is to use the historical tax rate from the time of the order to calculate the taxes to refund. Option two is to use your record of how much of each tax was paid by the customer on the order to determine how much to return.

Using the Historical Tax Rate

This technique is quite simple. Using the historical tax rate, perform your tax calculation on the items to be returned, as if it was a new order. The drawback of this technique is rounding could make the total tax returned not perfectly match the original taxes paid. This would happen in our “when to round” example from the first article in this series, if you calculated the taxes originally at the order level, and then the customer made two separate returns.

Using the Record of Tax Paid

In this technique, we use the amount of tax paid (as stored in the order) to calculate the amount of tax to return. The general procedure is to determine what fraction of the order price is being returned, and return that fraction of the taxes paid.

For example, a customer buys three tennis rackets having total item taxes of $29.34. The customer then returns one tennis racket. We should return (1/3)*$29.34=$9.78 in tax. If the customer returns two tennis rackets, the tax refund will be (2/3)*$29.34=$19.56. The line item amounts are summed to give the total tax to return.

This approach becomes more complicated when multiple tax rates are involved. If you are calculating taxes on the order, you also need to consider which tax rates apply to the items being returned, and, for each tax rate, determine the value of items being returned that are subject to the tax rate and the proportion that value is of the original amount subject to that tax rate.

If you are calculating line-level taxes, you need to determine what proportion of each line is being returned and return that fraction of the taxes paid on that line.

Discounts

Why do you need to care about discounts when calculating taxes? Ideally, you don't. Your tax calculations just take a price and compute the taxes added to or included in that price. However, be aware that discounts affect calculation results for inclusive tax jurisdictions differently than they affect exclusive jurisdictions. The scenario below explains how.

How Discounts Affect Calculations: Discount Scenario

By way of example, assume there is two tax jurisdictions, both with a 10 percent tax rate. One jurisdiction has tax inclusive pricing; the other has tax exclusive pricing. In each jurisdiction, a customer would like to buy a $100.00 bike. Let’s start by looking at the situation with no discount.

  • No Discount: Jurisdiction with Tax Inclusive Pricing

    The bike is advertised at $100, the customer pays $100, and the retailer sends the government $9.09 in tax, keeping $90.01.

  • No Discount: Jurisdiction with Tax Exclusive Pricing

    The bike is advertised at a cost of $90.91, the customer pays $100 after taxes ($90.91*1.1), and the retailer sends the government $9.09 in tax, keeping $90.91.

As you can see, when there is no discount, the results in exclusive and inclusive jurisdictions are equivalent. The customer pays $100, the government gets $9.09, and the merchant gets $90.91.

But wait. We have a sale, and merchants in both jurisdictions are offering $10 off the bike. Let's see how that affects the pricing, the taxes, and how much money the merchant gets to keep.

  • With Discount: Jurisdiction with Tax Inclusive Pricing

    The bike is advertised at a cost of $100 with a $10 discount. The customer pays ($100 - $10) = $90. The retailer sends the government (0.10/(1 + 0.10) * $90.00 = $8.18 in tax, keeping $81.82.

  • With Discount: Jurisdiction with Tax Exclusive Pricing

    The bike is advertised at a cost of $90.91 with a $10 discount. The customer pays ($90.91-$10)*(1 + 0.1) = $89.00, the retailer sends the government $8.09 in tax, keeping $80.91.

Again, as you can see, when there is a discount, in the exclusive jurisdiction the customer pays $1.00 less, the government gets $0.09 less, and the merchant gets $0.91 less.

The scenario above should have demonstrated that discounts don't change your calculations. However, the results of the calculations are quite different for inclusive pricing than for exclusive pricing. In case a disgruntled merchant in an exclusive tax jurisdiction ever asks you, it's worth knowing why they have to give away an extra $0.91.

One Solution: Percentage Discounts

If your company or your client sells in both inclusive and exclusive jurisdictions you can avoid this problem by encouraging percentage discounts, which work out the same in both cases, or adjust the amount of discount you're giving to make sure the same discount is given in both the exclusive and the inclusive jurisdiction.

Conclusion

There are many details to consider with tax calculations, and the details have important legal and financial implications. Sadly, these details vary significantly around the world, which impacts international ecommerce sites. Hopefully I've given you enough review of the issues involved to get started coding, or at least enough to understand the implications when choosing a tax package or expanding into a new jurisdiction.

0 Comments

Rss-sm