Amazon MWS US Marketplace: How to get tax rate used on an order line? - amazon-mws

How do I get the tax rate or tax rate's that were used to calculate a tax amount on an order line with Amazon MWS?
In the Orders API there is an ItemPrice and ItemTax XML element but these only contain the amounts rather than the rate used.
I need to know what rate or rate's were used to calculate the ItemTax and what state these taxes apply too. I also need to get this information programmatically via the MWS API.
In the MWS Reports API I have tried many reports trying to find this information.
_GET_FLAT_FILE_ORDER_REPORT_DATA_ does not contain tax rate/state
_GET_FLAT_FILE_SALES_TAX_DATA_ This report can't be requested or scheduled. We don't have access to the users seller central account because we are developing an integration.
_SC_VAT_TAX_REPORT_ I can't use this because it is not for US
_GET_VAT_TRANSACTION_DATA_ I can't use this because it is not for US
For example from the Orders API ListOrderItems
<ItemTax>
<CurrencyCode>USD</CurrencyCode>
<Amount>0.81</Amount>
</ItemTax>
<ItemPrice>
<CurrencyCode>USD</CurrencyCode>
<Amount>12.99</Amount>
</ItemPrice>
If you try calculate backwards to the rate
0.81 / 12.99 = 0.0623556581986143
This isn't accurate because the ItemTax has already been rounded to 2 dp and I also need to know what state this tax was calculated from.

The correct way to calculate tax rate is to use the following formula:
tax_rate = -1 + gross_price / (gross_price - tax)
In your example:
gross_price = 12.99
tax = 0.81
tax_rate = -1 + 12.99 / (12.99 - 0.81) = -1 + 12.99 / 12.18 = 0,0665024630541 ≈ 6,65%
Still this solution is prone to rounding errors but should be more accurate.

Related

Using Binance API Results

I've recently started making calls to Binance API, specifically this:
https://api.binance.com/api/v3/ticker/24hr?symbol=BTCBUSD
This call only seems to have 1 parameter (symbol). My question concerns the meaning of the returned fields, or how to interpret them.
Here is a sample of returned data from the above call.
{
"symbol": "BTCBUSD",
"priceChange": "1519.63000000",
"priceChangePercent": "3.308",
"weightedAvgPrice": "47059.10006256",
"prevClosePrice": "45935.14000000",
"lastPrice": "47454.77000000",
"lastQty": "0.08014400",
"bidPrice": "47454.76000000",
"bidQty": "0.00858100",
"askPrice": "47454.77000000",
"askQty": "0.22488900",
"openPrice": "45935.14000000",
"highPrice": "48444.00000000",
"lowPrice": "45044.05000000",
"volume": "18094.48897600",
"quoteVolume": "851510367.30253731",
"openTime": 1614347997320,
"closeTime": 1614434397320,
"firstId": 116330237,
"lastId": 117165845,
"count": 835609
}
What does openPrice and prevClosePrice actually mean? They change between each call.
How does priceChange and priceChangePercent work? What fields are these values calculated from? And why are they markedly different to what I see when viewing Binance site for BTC/BUSD 24 HR period at the same time as I make the call?
Can anyone shed some light on these figures? I've searched online but not been able to find anything descriptive.
When I calculate the price change percent based on the difference between lastPrice and openPrice I get the following result which does match the priceChangePercent value.
((47454.77 - 45935.14) / 45935.14) * 100 = 3.308
openPrice = last candle started at this price
prevClosePrice = last 2nd candle closed at this price (it must be same with next candles open price)
priceChange = difference between start of the day price and current price (start of the day 00.00 in UTC i guess)
priceChangePercent = same with priceChange but percentage (also you can see in binance symbol lists these things, just order by change)

Calculate cost of credits

Consider an ecommerce site like istock, envato, etc.
Users can purchase items directly, or via pre-purchased credits which are sold at a discount (all relative to US dollar).
The credit discounts are variable, however. They can change based on quantity, strategy changes, coupons, there can be short-term sales, etc. For example, 25 credits might cost $22.50 (10% discount) while 100 credits might cost $75 (25% discount).
Assume we have a past history of all previous purchases so that the average cost of all credits is attainable by the following formula:
AverageCost = (p1.costPer * p1.numberBought) + (pn.costPer * pn.numberBought) / (p1.numberBought + pn.numberBought)
How can the spending history be used to calculate the current real value of credits in the user's account?
If the user has never bought any credits before, or has never spent any credits, then the value of a credit is exactly AverageCost (as demonstrated above)
However - if they have spent some credits previously, and bought new credits - how does the previous spending shift the average value of current credits?
Use case is a revenue share, where revenue split must be calculated based on money value, even when paid via credits sold in this manner.
Therefore, it could very well be that another relevant variable is knowing what has already been paid out (and any data which comes along with that)

Currency Exchange Rate in SQL Server

From Mexico's official currency exchange website, http://dof.gob.mx/index.php, I need to pull the USD rate on the far right of the page into a simple math equation in a SQL Server 2005 query to calculate USD to Mexican Pesos for invoices. Is this possible, and if so how?
It would be much simpler to pull date from this kind of web API service where you can simply calculate up-to-date conversion of USD to MXN:
http://openexchangerates.org/documentation
And then just store all prices either in USD or MXN in the database, and apply conversion when needed.
E.g. in this list of exchange rates all ratios are compared to USD. So USD to MXN ratio is now 13.3744. So to convert dollars to Peso you need to multiply amount in dollars to the exchange rate:
var dollars = 25;
var peso = dollars * 13.3744;
and peso value is:
334.36
I hope that'll help anyone!

Implementing a sales tax strategy for Invoices

Here in South Africa we have Value Added Tax (VAT) which is pretty much identical to Sales Tax and is currently fixed at 14%, but could change at any time.
I need to include VAT on invoices (which are immutable) consisting of several Invoice Lines. Each line references a Product with a Boolean property, IsTaxable, and almost all products are taxable.
I don't want to store pre-tax prices in the database, because that just makes it hard to read the real price that the customer is going to pay and everywhere I display those prices, I then have to remember to add tax. And when the VAT rate does change, for this particular business, it is undesirable for all prices to change automagically.
So I reckon a reverse tax calculation is the way to go and probably not uncommon. The invoice total is the sum of all invoice line totals, which includes any line discounts and should be tax-inclusive. Therefore the invoice total itself is tax-inclusive:
TaxTotal = InvoiceTotal / (1 + TaxRate),
where InvoiceTotal is tax-inclusive and TaxRate == 0.14
Since invoices cannot be changed once issued (they are immutable), should I:
Store a single Tax amount in my Invoices table that does not change? Or...
Store a tax amount for each invoice line and calculate the invoice tax total every time I display the invoice?
Option 2 seems safer from a DBA point-of-view since if an invoice is ever manually changed, then Tax will be calculated correctly, but if the invoice has already been issued, this still presents a problem of inconsistency. If I stick with option 1, then I cannot display tax for a single line item, but it makes managing the tax total and doing aggregate calculations easier, though it also presents inconsistency if ever changed.
I can't do both since that would be duplicating data.
Which is the right way to go? Or is a reverse tax calculation a really bad idea?
Store the pre tax value in the data base, you can also store the with tax value and use that for most use cases.
Th big problem I forsee is the rounding rules for VAT on invoices.These (at least in the UK) are really strict and there is no way for your reverse calculation to get this right.
Also you need to store the tax item by item as the VAT dragons will expect you to refund exactly the tax paid if an item is returned. You really need to get hold of the local sales tax rules before you start.
My experience is that you can get dragged over the coals if your calculations are out by as little as a penny, and, if you are audited you need to be able to show how you arrived at the VAT figure so not storing anything used in your calculations will catch you out.
I totally agree with James Anderson! In Germany the rules according VAT calculations are as strict as in the UK.
We have to accumulate the net value by VAT percentage (we have three types: 0, 7 and 19 percent) rounded by two digits. On this rounded value we have to calculcate VAT.
VAT has to be rounded by two digits and has to be showed at the invoice.
But nonetheless you can store prices including tax. It depends whether the net prices or the end prices stay unchanged when tax rises. In Germany usually B2B net prices stay unchanged but B2C end prices stay unchanged - it depends.
You can calculate it this way:
with cPriceIncludingVAT as (
select InvoiceNo, VATPercentage,
PriceIncludingVAT = cast(sum(amount * price) as decimal(12,2))
from InvoiceLines inner join VAT on VAT.VATID=InvoiceLines.VATID
group by InvoiceNo, VATPercentage
),
cVATcalculated as (
select InvoiceNo, VATPercentage, PriceIncludingVAT,
VAT = cast(PriceIncludingVAT * VATPercentage /
(1+VATPercentage) as decimal(12,2))
from cVATcalculated
)
select InvoiceNo, VATPercentage, PriceIncludingVAT, VAT,
NetPrice = PriceIncludingVAT - VAT
from cVATcalculated;
If you save this as a view you should be able to reprint a dynamically calculated VAT value exactly. When there is an accounting system you can (and should) export exactly the same data you printed.
Usually you should save values like these as field values within the database - but I understand if you'd like to have a more dynamic approach ...
The other answers are good, but as idevlop mentions, it's almost a certainty that at some time in the future, you'll start having different rates for different categories of products. Adding that capability up front will save you a ton of heartache later on. Been there, done that.

SQL Server code to duplicate Excel calculation that includes circular reference

Is there a way to duplicate a formula with a circular reference from a Excel file into SQL Server? My client uses a excel file to calculate a Selling Price. The Selling Price field is (costs/1-Projected Margin)) = 6.5224 (1-.6) = 16.3060. One of the numbers that goes into the costs is commission which is defined as SellingPrice times a commission rate.
Costs = 6.5224
Projected Margin = 60%
Commissions = 16.3060(Selling Price) * .10(Commission Rate) = 1.6306 (which is part of the 6.5224)
They get around the circular reference issue in Excel because Excel allows them to check a Enable Iterative Calculation option and stops the iterations after 100 times.
Is this possible using SQL Server 2005?
Thanks
Don
This is a business problem, not an IT one, so it follows that you need a business solution, not an IT one. It doesn't sound like you're working for a particularly astute customer. Essentially, you're feeding the commission back into the costs and recalculating commission 100 times. So the salesman is earning commission based on their commission?!? Seriously? :-)
I would try persuading them to calculate costs and commissions separately. In professional organisations with good accounting practices were I've worked before these costs are often broken down into operating and non-operating or raw materials costs, which should improve their understanding of their business. To report total costs later on, add commission and raw materials costs. No circular loops and good accounting reports.
At banks where I've worked these costs are often called things like Cost (no commissions or fees), Net Cost (Cost + Commission) and then bizzarely Net Net Cost (Cost + Commission + Fees). Depending on the business model, cost breakdowns can get quite interesting.
Here are 2 sensible options you might suggest for them to calculate the selling price.
Option 1: If you're going to calculate margin to exclude commission then
Price before commission = Cost + (Cost * (1 - Projected Margin))
Selling price = Price before commission + (Price before commision * Commission)
Option 2: If your client insists on calculating margin to include commission (which it sounds like they might want to do) then
Cost price = Cost + (Cost * Commission)
Profit per Unit or Contribution per Unit = Cost price * (1-Projected Margin)
Selling Price = Cost Price + Profit per Unit
This is sensible in accounting terms and a doddle to implement with SQL or any other software tool. It also means your customer has a way of analysing their sales to highlight per unit costs and per unit profits when the projected margin is different per product. This invariably happens as the business grows.
Don't blindly accept calculations from spreadsheets. Think them through and don't be afraid to ask your customer what they're trying to achieve. All too often broken business processes make it as far as the IT department before being called into question. Don't be afraid of doing a good job and that sometimes means challenging customer requests when they don't make sense.
Good luck!
No, it is not possible
mysql> select 2+a as a;
ERROR 1054 (42S22): Unknown column 'a' in 'field list'
sql expressions can only refer to expressions that already exist.
You can not even write
mysql> select 2 as a, 2+a as b;
ERROR 1054 (42S22): Unknown column 'a' in 'field list'
The way to look at databases is as transactional engines that take data from one state into another state in one step (with combination of operators that operate not only on scalar values, but also on sets).
Whilst I agree with #Sir Wobin's answer, if you do want to write some recursive code, you may be able to do it by abusing Recursive Common Table Expressions:
with RecurseCalc as (
select CAST(1.5 as float) as Value,1 as Iter
union all
select 2 * Value,1+Iter from RecurseCalc where Iter < 100
), FinalResult as (
select top 1 Value from RecurseCalc order by Iter desc
)
select * from FinalResult option (maxrecursion 100)

Resources