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.
11
12
13
14
15
16
17
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 11
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
111
112
113
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 111
def amount_before_tax
(@receipt_detail.amount.abs / (@document.resource_tax_rate.rate_goods * 100)) * 100
end
|
#delete ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 74
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 95
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
42
43
44
45
46
47
48
49
50
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 42
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
115
116
117
118
119
120
121
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 115
def submission_type
if @receipt_detail.amount.negative?
'refund'
else
'order'
end
end
|
#submit ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 25
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
19
20
21
22
23
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 19
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'app/services/receipt/submit_to_taxjar.rb', line 52
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
|