vinsol-spree-contrib / spree_taxjar

Spree Taxjar is a US sales tax extension for Spree using the Taxjar service
BSD 3-Clause "New" or "Revised" License
6 stars 22 forks source link

Texas [U.S.A] state not including shipping charges in tax calculations but should include #16

Closed pikender closed 7 years ago

pikender commented 7 years ago

Prerequisite

Setup spree_taxjar as instructed in README

Zone Setup

Create a Zone with:

Tax Category Setup

Create a tax category with:

Tax Rate Setup

Create a tax rate with:

Shipping Category Setup

Create a shipping category with:

Shipping Method Setup

Create a shipping category with:

Product Setup

Create or Update an existing product with:

Follow the Checkout with Product having Tax Category as TaxJar

Expected

On Checkout -> Payment Step, Tax calculated should take shipping in account when needed but missing now (Texas is a state which levy tax on shipping)

Analysis

It's found that we are creating tax adjustments for line_items by introspecting taxjar_response.breakdown.line_items which is rightly calculating tax on line_item cost and storing in line_item.additional_tax but missing tax applied on shipping which can be further verified based on freight_taxable is true/false.

Probable Solution

We can create an order adjustment for the shipping tax difference and add and show in Order additional_tax_total.

Though, I will leave the best implementation to the owners as Spree ecosystem should behave well for all cases with the fix 😊

Sample Api Response

Note: Shipping Cost as 0 and 0.2 in below requests

2.3.1 :002 > client = ::Taxjar::Client.new(api_key: Spree::Config[:taxjar_api_key]) 
 => #<Taxjar::Client:0x007faefa1906d0 @api_key="<Removed from here>"> 
2.3.1 :003 > client.tax_for_order({:to_country=>"US", :to_zip=>"79001", :to_state=>"TX", :to_city=>"Adrian", :amount=> 22.0, :shipping=> 2.0, :line_items=>[{:id=>12, :quantity=>1, :unit_price=>20}]})
 => #<Taxjar::Tax order_total_amount: 22.0, shipping: 2.0, taxable_amount: 22.0, amount_to_collect: 1.82, rate: 0.0825, has_nexus: true, freight_taxable: true, tax_source: "origin"> 
2.3.1 :004 > client.tax_for_order({:to_country=>"US", :to_zip=>"79001", :to_state=>"TX", :to_city=>"Adrian", :amount=> 22.0, :shipping=> 0.0, :line_items=>[{:id=>12, :quantity=>1, :unit_price=>20}]})
 => #<Taxjar::Tax order_total_amount: 20.0, shipping: 0.0, taxable_amount: 20.0, amount_to_collect: 1.65, rate: 0.0825, has_nexus: true, freight_taxable: true, tax_source: "origin"> 
2.3.1 :005 > 
pikender commented 7 years ago

Taxjar Api allows shipping to be passed for order and not shipments.

Also, I see no provision to add shipping as line_items like Avalara does.

As per my understanding, shipping tax should be stored as shipment adjustments with shipments storing additional_tax_total as value.

The deadlock happens between Taxjar Api and how Spree supports shipping taxes, as we get a consolidated shipping tax value, if applicable, from taxjar api so no easy/direct way to assign to shipments.

It looks from outside that creating a tax adjustment for shipping at order level might solve the issue and better managed as per Spree ecosystem.

Please guide me on better implementation in this regard.

nishant-cyro commented 7 years ago

@pikender - We need to add a new adjustment here - https://github.com/spree/spree/blob/master/core/app/models/spree/order.rb#L311 for Order.

Then, We need to modify https://github.com/vinsol-spree-contrib/spree_taxjar/blob/master/app/models/spree/calculator/taxjar_calculator.rb#L18 accordingly.

pikender commented 7 years ago

It's solved by making a Standard Tier Taxjar API request per shipment when compute_shipment hook for Taxjar Calculator is called.

So for a Order with N Shipments, N + 1 API calls are made to calculate correct sales tax.

N Standard Tier Taxjar Api Calls for calculating shipment tax and 1 Advanced Tier Taxjar Api Call for calculating line_items tax