Class: Invoicing::SubmitToTaxjar

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

Defined Under Namespace

Classes: Result

Constant Summary collapse

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.



12
13
14
15
16
17
18
19
20
# File 'app/services/invoicing/submit_to_taxjar.rb', line 12

def initialize(invoice, options = {})
  @invoice = invoice
  @options = options
  @logger = options[:logger] || Rails.logger
  @client = Api::Taxjar.client
  @client.set_api_config('headers', {
    'x-api-version' => '2022-01-24'
  })
end

Instance Method Details

#deleteObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/services/invoicing/submit_to_taxjar.rb', line 129

def delete
  require 'taxjar'
  begin
    existing_record = @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 Taxjar::Error::NotFound
    puts "Record not found on Taxjar for invoice #{taxjar_id}"
    Result.new(
      success: false,
      message: "Record not found on Taxjar for invoice #{taxjar_id}"
    )
  end
end

#detailsObject



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
192
193
194
195
196
197
198
199
# File 'app/services/invoicing/submit_to_taxjar.rb', line 147

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 ? li.discounted_price : li.price
      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)


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

def existing_record?
  require 'taxjar'
  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 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 Taxjar::Error::NotFound
    false
  end
end

#submitObject



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

def submit
  require 'taxjar'
  begin
    if existing_record?
      puts "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 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 Taxjar::Error::ServerError => e
    Result.new(
      success: false,
      message: e.message
    )
  end
end

#taxjar_idObject



22
23
24
25
26
# File 'app/services/invoicing/submit_to_taxjar.rb', line 22

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

#updateObject



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

def update
  require 'taxjar'
  begin
    marketplace_provider = MARKETPLACE_PROVIDERS[@invoice.customer_id]

    # Fetch existing record with provider parameter if applicable
    existing_record = 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 Taxjar::Error::NotFound
    puts "Record not found on Taxjar for invoice #{taxjar_id}"
    Result.new(
      success: false,
      message: "Record not found on Taxjar for invoice #{taxjar_id}"
    )
  rescue 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
end