This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
sales_invoice.rb
152 lines (119 loc) · 4.97 KB
/
sales_invoice.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# A SalesInvoice is persisted by Avatax but it's not recognized as complete until it's "committed".
class SpreeAvatax::SalesInvoice < ActiveRecord::Base
DOC_TYPE = 'SalesInvoice'
CANCEL_CODE = 'DocVoided'
class CommitInvoiceNotFound < StandardError; end
class AlreadyCommittedError < StandardError; end
belongs_to :order, class_name: "Spree::Order", inverse_of: :avatax_sales_invoice
validates :order, presence: true
validates :doc_id, presence: true
validates :doc_code, presence: true
validates :doc_date, presence: true
class << self
# Calls the Avatax API to generate a sales invoice and calculate taxes on the line items.
# On failure it will raise.
# On success it updates taxes on the order and its line items and create a SalesInvoice record.
# At this point the record is saved but uncommitted on Avatax's end.
# After the order completes the ".commit" method will get called and we'll commit the
# sales invoice, which marks it as complete on Avatax's end.
def generate(order)
bench_start = Time.now
return if order.completed? || !SpreeAvatax::Shared.taxable_order?(order)
result = SpreeAvatax::SalesShared.get_tax(order, DOC_TYPE)
# run this immediately to ensure that everything matches up before modifying the database
tax_line_data = SpreeAvatax::SalesShared.build_tax_line_data(order, result)
if sales_invoice = order.avatax_sales_invoice
if sales_invoice.committed_at.nil?
sales_invoice.destroy
else
raise AlreadyCommittedError.new("Sales invoice #{sales_invoice.id} is already committed.")
end
end
sales_invoice = order.create_avatax_sales_invoice!({
transaction_id: result[:transaction_id],
doc_id: result[:doc_id],
doc_code: result[:doc_code],
doc_date: result[:doc_date],
pre_tax_total: result[:total_amount],
additional_tax_total: result[:total_tax],
})
SpreeAvatax::SalesShared.update_taxes(order, tax_line_data)
sales_invoice
rescue Exception => e
if SpreeAvatax::Config.sales_invoice_generate_error_handler
SpreeAvatax::Config.sales_invoice_generate_error_handler.call(order, e)
else
raise
end
ensure
duration = (Time.now - bench_start).round
Rails.logger.info "avatax_sales_invoice_generate_duration=#{(duration*1000).round}"
end
def commit(order)
return if !SpreeAvatax::Shared.taxable_order?(order)
raise CommitInvoiceNotFound.new("No invoice for order #{order.number}") if order.avatax_sales_invoice.nil?
post_tax(order.avatax_sales_invoice)
order.avatax_sales_invoice.update!(committed_at: Time.now)
order.avatax_sales_invoice
rescue Exception => e
if SpreeAvatax::Config.sales_invoice_commit_error_handler
SpreeAvatax::Config.sales_invoice_commit_error_handler.call(order, e)
else
raise
end
end
def cancel(order)
return if order.avatax_sales_invoice.nil?
result = cancel_tax(order.avatax_sales_invoice)
order.avatax_sales_invoice.update!({
canceled_at: Time.now,
cancel_transaction_id: result[:transaction_id],
})
rescue Exception => e
if SpreeAvatax::Config.sales_invoice_cancel_error_handler
SpreeAvatax::Config.sales_invoice_cancel_error_handler.call(order, e)
else
raise
end
end
private
def post_tax(sales_invoice)
params = posttax_params(sales_invoice)
logger.info "[avatax] posttax sales_invoice=#{sales_invoice.id} order=#{sales_invoice.order_id}"
logger.debug { "[avatax] params: #{params.to_json}" }
response = SpreeAvatax::Shared.tax_svc.posttax(params)
SpreeAvatax::Shared.require_success!(response)
response
end
def cancel_tax(sales_invoice)
params = canceltax_params(sales_invoice)
logger.info "[avatax] canceltax sales_invoice=#{sales_invoice.id}"
logger.debug { "[avatax] params: #{params.to_json}" }
response = SpreeAvatax::Shared.tax_svc.canceltax(params)
SpreeAvatax::Shared.require_success!(response)
response
end
# see https://github.com/avadev/AvaTax-Calc-SOAP-Ruby/blob/master/PostTaxTest.rb
def posttax_params(sales_invoice)
{
doccode: sales_invoice.doc_code,
companycode: SpreeAvatax::Config.company_code,
doctype: DOC_TYPE,
docdate: sales_invoice.doc_date,
commit: true,
totalamount: sales_invoice.pre_tax_total,
totaltax: sales_invoice.additional_tax_total,
}
end
# see https://github.com/avadev/AvaTax-Calc-SOAP-Ruby/blob/master/CancelTaxTest.rb
def canceltax_params(sales_invoice)
{
# Required Parameters
doccode: sales_invoice.doc_code,
doctype: DOC_TYPE,
cancelcode: CANCEL_CODE,
companycode: SpreeAvatax::Config.company_code,
}
end
end
end