Class: Receipt::SubmitToTaxjar
Overview
Service object: submit to taxjar.
Defined Under Namespace
Classes: Result
Instance Attribute Summary
Attributes inherited from BaseService
#options
Instance Method Summary
collapse
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #process, #tagged_logger
Constructor Details
#initialize(receipt_detail, options = {}) ⇒ SubmitToTaxjar
Returns a new instance of SubmitToTaxjar.
14
15
16
17
18
19
20
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 14
def initialize(receipt_detail, options = {})
@receipt_detail = receipt_detail
@document = Invoice.find_by(reference_number: receipt_detail.tax_document_reference) || CreditMemo.find_by(reference_number: receipt_detail.tax_document_reference)
@options = options
@logger = options[:logger] || Rails.logger
@client = Api::Taxjar.client
end
|
Instance Method Details
#amount_before_tax ⇒ Object
114
115
116
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 114
def amount_before_tax
(@receipt_detail.amount.abs / (@document.resource_tax_rate.rate_goods * 100)) * 100
end
|
#delete ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 77
def delete
if submission_type == 'order'
@client.show_order(taxjar_id)
@client.delete_order(taxjar_id)
else
@client.show_refund(taxjar_id)
@client.delete_refund(taxjar_id)
end
Result.new(
success: true,
message: "Record on Taxjar for receipt detail #{taxjar_id} successfully deleted"
)
rescue Api::Taxjar::Error::NotFound
@logger.info "Record not found on Taxjar for receipt detail #{taxjar_id}"
Result.new(
success: false,
message: "Record not found on Taxjar for receipt detail #{taxjar_id}"
)
end
|
#details ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 98
def details
{
customer_id: @receipt_detail.taxjar_customer_id,
transaction_id: taxjar_id,
transaction_date: @receipt_detail.gl_date.to_s,
to_country: @document.shipping_address.country.iso,
to_zip: @document.shipping_address.zip,
to_state: @document.shipping_address.state_code,
to_city: @document.shipping_address.city,
to_street: @document.shipping_address.street1,
amount: amount_before_tax,
shipping: 0,
sales_tax: @receipt_detail.amount.abs
}
end
|
#existing_record? ⇒ Boolean
45
46
47
48
49
50
51
52
53
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 45
def existing_record?
search_params = {
from_transaction_date: @receipt_detail.gl_date.to_s,
to_transaction_date: @receipt_detail.gl_date.to_s
}
existing_records = submission_type == 'order' ? @client.list_orders(search_params) : @client.list_refunds(search_params)
existing_records.include?(taxjar_id)
end
|
#submission_type ⇒ Object
118
119
120
121
122
123
124
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 118
def submission_type
if @receipt_detail.amount.negative?
'refund'
else
'order'
end
end
|
#submit ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 28
def submit
if existing_record?
@logger.info "Record already exists on Taxjar for receipt detail #{taxjar_id}"
Result.new(
success: false,
message: "Record already exists on Taxjar for receipt detail #{taxjar_id}"
)
else
new_record = submission_type == 'order' ? @client.create_order(details) : @client.create_refund(details)
Result.new(
success: true,
original_tax_amount: @receipt_detail.amount,
calculated_tax: new_record.sales_tax
)
end
end
|
#taxjar_id ⇒ Object
22
23
24
25
26
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 22
def taxjar_id
str = "RD#{@receipt_detail.id}"
str.prepend("#{Rails.env}-") if !Rails.env.production? && !str.start_with?("#{Rails.env}-")
str
end
|
#update ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 55
def update
if submission_type == 'order'
@client.show_order(taxjar_id)
updated_record = @client.update_order(details)
else
@client.show_refund(taxjar_id)
updated_record = @client.update_refund(details)
end
Result.new(
success: true,
original_tax_amount: @receipt_detail.amount,
calculated_tax: updated_record.sales_tax
)
rescue Api::Taxjar::Error::NotFound
@logger.info "Record not found on Taxjar for receipt detail #{taxjar_id}"
Result.new(
success: false,
message: "Record not found on Taxjar for receipt detail #{taxjar_id}"
)
end
|