Class: Invoicing::SubmitToTaxjar

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/invoicing/submit_to_taxjar.rb

Overview

Service object: submit to taxjar.

Defined Under Namespace

Classes: Result

Constant Summary collapse

MARKETPLACE_PROVIDERS =

Marketplace providers.

{
  5_322_434 => 'amazon',
  23_041_827 => 'walmart'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(invoice, options = {}) ⇒ SubmitToTaxjar

Returns a new instance of SubmitToTaxjar.



17
18
19
20
21
22
# File 'app/services/invoicing/submit_to_taxjar.rb', line 17

def initialize(invoice, options = {})
  @invoice = invoice
  @options = options
  @logger = options[:logger] || Rails.logger
  @client = Api::Taxjar.client
end

Instance Method Details

#deleteObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/services/invoicing/submit_to_taxjar.rb', line 124

def delete
  @client.show_order(taxjar_id)
  @client.delete_order(taxjar_id)
  Result.new(
    success: true,
    message: "Record on Taxjar for invoice #{taxjar_id} successfully deleted"
  )
rescue Api::Taxjar::Error::NotFound
  @logger.info "Record not found on Taxjar for invoice #{taxjar_id}"
  Result.new(
    success: false,
    message: "Record not found on Taxjar for invoice #{taxjar_id}"
  )
end

#detailsObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/services/invoicing/submit_to_taxjar.rb', line 139

def details
  line_items = []
  if @invoice.line_items.present?
    @invoice.line_items.parents_only.non_shipping.each do |li|
      next if li.taxable_total.zero?

      product_identifier = li.item.present? ? li.item.sku : li.cm_category
      description = if li.item.present?
                      "#{li.item.sku} - #{li.item.name}"
                    else
                      li.description.presence || li.cm_category
                    end
      price = [li.price, li.discounted_price].max
      discount = [-li.line_discounts.sum(:amount), 0].max
      line_items << {
                      quantity: li.quantity,
                      product_identifier: product_identifier,
                      product_tax_code: li.item&.product_tax_code&.product_tax_code,
                      description: description,
                      unit_price: price,
                      discount: discount,
                      sales_tax: li.tax_total
                    }
    end
  end
  amount = @invoice.line_items.to_a.sum(&:taxable_total).to_f
  shipping = @invoice.shipping_cost.to_f == @invoice.line_items.shipping_only.to_a.sum(&:taxable_total).to_f ? @invoice.shipping_cost.to_f : @invoice.line_items.shipping_only.to_a.sum(&:taxable_total).to_f

  # Map customer IDs to their marketplace providers
  marketplace_provider = MARKETPLACE_PROVIDERS[@invoice.customer_id]

  # Build base transaction details
  base_details = {
    customer_id: @invoice.taxjar_customer_id,
    transaction_id: taxjar_id,
    provider: marketplace_provider.presence || 'api',
    transaction_date: @invoice.gl_date.to_s,
    to_country: @invoice.shipping_address.country.iso,
    to_zip: @invoice.shipping_address.zip,
    to_state: @invoice.shipping_address.state_code,
    to_city: @invoice.shipping_address.city,
    to_street: @invoice.shipping_address.street1,
    amount: amount,
    shipping: shipping,
    sales_tax: @invoice.tax_total.to_f,
    line_items: line_items
  }

  # Add exemption_type for marketplace providers only
  base_details[:exemption_type] = 'marketplace' if marketplace_provider.present?

  base_details
end

#existing_record?Boolean

Returns:

  • (Boolean)


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
# File 'app/services/invoicing/submit_to_taxjar.rb', line 65

def existing_record?
  marketplace_provider = MARKETPLACE_PROVIDERS[@invoice.customer_id]

  # Check if record already exists
  begin
    existing_records = @client.list_orders({
      from_transaction_date: @invoice.gl_date.to_s,
      to_transaction_date: @invoice.gl_date.to_s
    })
    return true if existing_records.include?(taxjar_id)
  rescue Api::Taxjar::Error
    # list_orders failed, try show_order instead
  end

  # Try to fetch the record directly (with provider parameter for marketplace orders)
  begin
    existing_record = if marketplace_provider.present?
                        @client.show_order(taxjar_id, { provider: marketplace_provider })
                      else
                        @client.show_order(taxjar_id)
                      end
    existing_record.present?
  rescue Api::Taxjar::Error::NotFound
    false
  end
end

#submitObject



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
# File 'app/services/invoicing/submit_to_taxjar.rb', line 30

def submit
  if existing_record?
    @logger.info "Record already exists on Taxjar for invoice #{taxjar_id}"
    Result.new(
      success: false,
      message: "Record already exists on Taxjar for invoice #{taxjar_id}"
    )
  else
    new_record = @client.create_order(details)
    Result.new(
      success: true,
      original_tax_amount: @invoice.tax_total,
      calculated_tax: new_record.sales_tax
    )
  end
rescue Api::Taxjar::Error::UnprocessableEntity => e
  # Handle "Provider tranx already imported" - transaction exists via marketplace integration
  if e.message.include?('already imported')
    Result.new(
      success: true,
      message: "Record already exists on Taxjar via marketplace provider for invoice #{taxjar_id}"
    )
  else
    Result.new(
      success: false,
      message: e.message
    )
  end
rescue Api::Taxjar::Error::ServerError => e
  Result.new(
    success: false,
    message: e.message
  )
end

#taxjar_idObject



24
25
26
27
28
# File 'app/services/invoicing/submit_to_taxjar.rb', line 24

def taxjar_id
  str = @invoice.reference_number.clone
  str.prepend("#{Rails.env}-") if !Rails.env.production? && !str.start_with?("#{Rails.env}-")
  str
end

#updateObject



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
# File 'app/services/invoicing/submit_to_taxjar.rb', line 92

def update
  marketplace_provider = MARKETPLACE_PROVIDERS[@invoice.customer_id]

  # Fetch existing record with provider parameter if applicable
  if marketplace_provider.present?
    @client.show_order(taxjar_id, { provider: marketplace_provider })
  else
    @client.show_order(taxjar_id)
  end

  updated_record = @client.update_order(details)

  Result.new(
    success: true,
    original_tax_amount: @invoice.tax_total,
    calculated_tax: updated_record.sales_tax
  )
rescue Api::Taxjar::Error::NotFound
  @logger.info "Record not found on Taxjar for invoice #{taxjar_id}"
  Result.new(
    success: false,
    message: "Record not found on Taxjar for invoice #{taxjar_id}"
  )
rescue Api::Taxjar::Error::Forbidden => e
  Rails.logger.warn "TaxJar API returned 403 Forbidden for invoice #{taxjar_id}. " \
                    "Check TaxJar credentials or account permissions. #{e.message}"
  Result.new(
    success: false,
    message: "TaxJar request forbidden for invoice #{taxjar_id}: #{e.message}"
  )
end