Price Calculation

Several resources involve storing price data, which generally involves a tax calculation, but may also involve other calculations if discounts, promotions and so on are applicable.

It is beyond the scope of this platform to provide accurate accounting functionality at this time - where pricing data is passed, it is stored in the database without validation.

Optionally, it is possible for the platform to calculate infill values for missing data. This has the following caveats:

  • Tax rates are stored with up to 4 decimal places of accuracy. Additional decimal places will be rounded to 4 decimal places.

  • Calculations rely on the round() function, and as such rounding errors can apply (ie. net + base prices do not equal gross price). We do not recommend using the prices stored on this platform for tax reporting purposes, nor will we accept any liability for incorrect tax filings resulting from use of this data.

  • We do not track fraction-of-cents adjustments for tax calculations made. As stated above, a commercial accounting system should be used for tax calculations.

With this in mind, if one opts to infill missing values, it is possible to pass pricing data such as the following:

{
  "base": 27810,
  "net": 25810,
  "gross": null,
  "tax": null,
  "tax_rate": 0.175,
  "currency_code": "USD"
}

In the above example, a product has a base price of $278.10, with a net price of $258.10, due to a $20 discount. The gross and tax amounts were not provided, but a sales tax of 17.5% is applicable.

The infill values generated for the above are:

{
  "base": 27810,
  "net": 25810,
  "gross": 30327,
  "tax": 4517,
  "tax_rate": 0.175,
  "currency_code": "USD"
}

As you can see, the gross value (i.e. including tax) has been calculated as $303.27, along with the tax component amounting to $45.17.

Code Used for Calculation

For developers looking to understand the calculation being made above, and the required conditions, the below code sample is the function that we use to calculate tax internally.

def calculate_price_data(base, net, gross, tax, tax_rate):
    res_base, res_net, res_gross, res_tax, res_tax_rate = base, net, gross, tax, tax_rate

    if not tax_rate:
        if net and gross:
            res_tax_rate = round(gross / net, 4)

    if not tax:
        if net and gross:
            res_tax = gross - net
        elif gross and tax_rate:
            res_tax = gross - round(gross / (1.0 + tax_rate))
        elif net and tax_rate:
            res_tax = round(net * tax_rate)

    if not net:
        if gross and tax:
            res_net = gross - tax
        elif gross and tax_rate:
            res_net = round(gross / (1.0 + tax_rate))
        elif tax and tax_rate:
            res_net = round(tax / tax_rate)

    if not gross:
        if net and tax:
            res_gross = net + tax
        elif net and tax_rate:
            res_gross = net + round(net * tax_rate)
        elif tax and tax_rate:
            res_gross = tax + round(tax / tax_rate)

    return res_base, res_net, res_gross, res_tax, res_tax_rate

Please note that the base price is never calculated - this is because we do not capture information on price modifiers (such as discounts) that account for the possible discrepancy between base and net prices currently, thus it is not possible to calculate an accurate base price.