Class: Receipt::SubmitToTaxjar
Defined Under Namespace
Classes: Result
Instance Method Summary
collapse
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #process, #tagged_logger
Constructor Details
#initialize(receipt_detail, options = {}) ⇒ SubmitToTaxjar
Returns a new instance of SubmitToTaxjar.
9
10
11
12
13
14
15
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 9
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
115
116
117
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 115
def amount_before_tax
(@receipt_detail.amount.abs / (@document.resource_tax_rate.rate_goods * 100)) * 100
end
|
#delete ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 75
def delete
require 'taxjar'
begin
if submission_type == 'order'
existing_record = @client.show_order(taxjar_id)
@client.delete_order(taxjar_id)
else
existing_record = @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 Taxjar::Error::NotFound
puts "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
end
|
#details ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 99
def details
{
customer_id: @receipt.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
40
41
42
43
44
45
46
47
48
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 40
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
119
120
121
122
123
124
125
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 119
def submission_type
if @receipt_detail.amount.negative?
'refund'
else
'order'
end
end
|
#submit ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 23
def submit
if existing_record?
puts "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
17
18
19
20
21
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 17
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 50
def update
require 'taxjar'
begin
if submission_type == 'order'
existing_record = @client.show_order(taxjar_id)
updated_record = @client.update_order(details)
else
existing_record = @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 Taxjar::Error::NotFound
puts "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
end
|