Skip to content

Commit de2236a

Browse files
committed
feat: refactor and complete the calculator and wrapper around the client
1 parent a21d85e commit de2236a

2 files changed

Lines changed: 112 additions & 6 deletions

File tree

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,72 @@
11
module SpreeAviorTax
22
class AviorTax
3-
attr_reader :order, :shipment, :client
3+
attr_reader :order, :line_item, :shipment, :client
44

5-
def initialize; end
5+
def initialize
6+
@client = SpreeAviorTax::Client.new client_params
7+
end
8+
9+
def calculate_for_order(order, line_item)
10+
@order = order
11+
@line_item = line_item
12+
client.calculate_tax [line_item_params]
13+
end
14+
15+
def calculate_for_shipment(order, shipment)
16+
@order = order
17+
@shipment = shipment
18+
client.calculate_tax [shipment_params]
19+
end
20+
21+
private
22+
23+
def client_params
24+
{
25+
service_url: SpreeAviorTax::Config[:service_url],
26+
token: SpreeAviorTax::Config[:token]
27+
}
28+
end
29+
30+
def line_item_params
31+
SpreeAviorTax::API::Input::Product.new(
32+
date: order.updated_at.strftime('%Y%m%d'),
33+
record_number: '523355',
34+
seller_id: 'S00010022',
35+
seller_location_id: '1',
36+
seller_state: 'FL',
37+
delivery_method: 'N',
38+
customer_entity_code: 'T',
39+
ship_to_address: order.tax_address.address1,
40+
ship_to_suite: order.tax_address.address2,
41+
ship_to_city: order.tax_address.city,
42+
ship_to_county: 'Miami-Dade',
43+
ship_to_state: order.tax_address.state.abbr,
44+
ship_to_zip_code: order.tax_address.zipcode,
45+
ship_to_zip_plus: '',
46+
sku: line_item.variant.sku,
47+
amount_of_sale: line_item.discounted_amount
48+
)
49+
end
50+
51+
def shipment_params
52+
SpreeAviorTax::API::Input::Product.new(
53+
date: order.updated_at.strftime('%Y%m%d'),
54+
record_number: '523355',
55+
seller_id: 'S00010022',
56+
seller_location_id: '1',
57+
seller_state: 'FL',
58+
delivery_method: 'N',
59+
customer_entity_code: 'T',
60+
ship_to_address: order.tax_address.address1,
61+
ship_to_suite: order.tax_address.address2,
62+
ship_to_city: order.tax_address.city,
63+
ship_to_county: 'Miami-Dade',
64+
ship_to_state: order.tax_address.state.abbr,
65+
ship_to_zip_code: order.tax_address.zipcode,
66+
ship_to_zip_plus: '',
67+
sku: '',
68+
amount_of_sale: shipment.discounted_cost
69+
)
70+
end
671
end
772
end

app/models/spree_avior_tax/calculator/avior_tax_calculator.rb

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module Calculator
33
class AviorTaxCalculator < Spree::Calculator
44
include Spree::VatPriceCalculation
55

6+
CACHE_EXPIRATION_DURATION = 10.minutes
7+
68
def self.description
79
Spree.t(:avior_tax)
810
end
@@ -15,16 +17,17 @@ def compute_line_item(item)
1517
return 0 unless SpreeAviorTax::Config[:enabled]
1618
return 0 if rate.included_in_price
1719

18-
result = LineItemTax.call(item: item)
19-
round_to_two_places(result.value)
20+
round_to_two_places(tax_for_line_item(item))
2021
end
2122

2223
def compute_shipment(shipment)
2324
return 0 unless SpreeAviorTax::Config[:enabled]
2425
return 0 if rate.included_in_price
2526

26-
result = ShipmentTax.call(shipment: shipment)
27-
round_to_two_places(result.value)
27+
round_to_two_places(tax_for_shipment(shipment))
28+
rescue SpreeAviorTax::API::Errors::AviorTaxServerError => e
29+
errors.add(:base, e.message)
30+
Rails.logger.error "AviorTax server error: #{e.message}"
2831
end
2932

3033
def compute_shipping_rate(_shipping_rate)
@@ -36,6 +39,44 @@ def compute_shipping_rate(_shipping_rate)
3639
def rate
3740
calculable
3841
end
42+
43+
def tax_for_line_item(item)
44+
order = item.order
45+
tax_address = order.tax_address
46+
47+
rails_cache_key = cache_key(order, item, tax_address)
48+
49+
Rails.cache.fetch(rails_cache_key, expires_in: CACHE_EXPIRATION_DURATION) do
50+
response = AviorTax.new.calculate_for_order(order, item)
51+
body = response.first
52+
amount = BigDecimal(body.fips_tax_amount_1) + BigDecimal(body.fips_tax_amount_2)
53+
amount
54+
end
55+
end
56+
57+
def tax_for_shipment(shipment)
58+
order = shipment.order
59+
tax_address = order.tax_address
60+
61+
rails_cache_key = cache_key(order, shipment, tax_address)
62+
63+
Rails.cache.fetch(rails_cache_key, expires_in: CACHE_EXPIRATION_DURATION) do
64+
response = AviorTax.new.calculate_for_shipment(order, shipment)
65+
body = response.first
66+
amount = BigDecimal(body.fips_tax_amount_1) + BigDecimal(body.fips_tax_amount_2)
67+
amount
68+
end
69+
end
70+
71+
def cache_key(order, item, address)
72+
if item.is_a?(Spree::LineItem)
73+
[Spree::LineItem.to_s, order.id, item.id, address.state_id, address.zipcode, item.taxable_amount, :amount_to_collect]
74+
else
75+
[Spree::Shipment.to_s, order.id, item.id, address.state_id, address.zipcode, item.cost, item.adjustments.reject do |adjustment|
76+
adjustment.source_type == Spree::TaxRate.to_s
77+
end.map(&:amount).sum.to_f, :amount_to_collect]
78+
end
79+
end
3980
end
4081
end
4182
end

0 commit comments

Comments
 (0)