Class: CreditMemo::SubmitToTaxjar

Inherits:
BaseService show all
Defined in:
app/services/credit_memo/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

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #process, #tagged_logger

Constructor Details

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

Returns a new instance of SubmitToTaxjar.



14
15
16
17
18
19
20
21
22
# File 'app/services/credit_memo/submit_to_taxjar.rb', line 14

def initialize(credit_memo, options = {})
  @credit_memo = credit_memo
  @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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/services/credit_memo/submit_to_taxjar.rb', line 102

def delete
  require 'taxjar'
  begin
    existing_record = @client.show_refund(taxjar_id)
    @client.delete_refund(taxjar_id)
    Result.new(
      success: true,
      message: "Record on Taxjar for credit memo #{taxjar_id} successfully deleted"
    )
  rescue Taxjar::Error::NotFound
    puts "Record not found on Taxjar for credit memo #{taxjar_id}"
    Result.new(
      success: false,
      message: "Record not found on Taxjar for credit memo #{taxjar_id}"
    )
  end
end

#detailsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
# File 'app/services/credit_memo/submit_to_taxjar.rb', line 120

def details
  line_items = []
  if @credit_memo.line_items.present?
    @credit_memo.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
      unit_price = li.price.zero? ? ((li.taxable_total / li.quantity) * -1) : (li.price * -1)
      # unit_price = li.taxable_total
      description = if li.item.present?
                      (li.item.sku + ' - ' + li.item.name)
                    else
                      li.description.presence || li.cm_category
                    end
      line_items << {
                      quantity: (li.quantity * -1),
                      product_identifier: product_identifier,
                      product_tax_code: li.item&.product_tax_code&.product_tax_code,
                      description: description,
                      unit_price: unit_price,
                      discount: [li.line_discounts.sum(:amount), 0].max,
                      sales_tax: (li.tax_total * -1)
                    }
    end
  end

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

  # Build base transaction details
  base_details = {
    customer_id: @credit_memo.taxjar_customer_id,
    transaction_id: taxjar_id,
    provider: marketplace_provider.presence || 'api',
    transaction_date: @credit_memo.gl_date.to_s,
    to_country: @credit_memo.shipping_address.country.iso,
    to_zip: @credit_memo.shipping_address.zip,
    to_state: @credit_memo.shipping_address.state_code,
    to_city: @credit_memo.shipping_address.city,
    to_street: @credit_memo.shipping_address.street1,
    amount: @credit_memo.line_items.to_a.sum(&:taxable_total).to_f,
    shipping: @credit_memo.shipping_cost.to_f == @credit_memo.line_items.shipping_only.to_a.sum(&:taxable_total).to_f ? @credit_memo.shipping_cost.to_f : @credit_memo.line_items.shipping_only.to_a.sum(&:taxable_total).to_f,
    sales_tax: (@credit_memo.tax_total * -1).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)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/services/credit_memo/submit_to_taxjar.rb', line 55

def existing_record?
  require 'taxjar'
  # check it already exists
  begin
    existing_records = @client.list_refunds({
      from_transaction_date: @credit_memo.gl_date.to_s,
      to_transaction_date: @credit_memo.gl_date.to_s
    })
    existing_records.include?(taxjar_id)
  rescue Taxjar::Error
    begin
      existing_record = @client.show_refund(taxjar_id)
      existing_record.present?
    rescue Taxjar::Error::NotFound
      false
    end
  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
# File 'app/services/credit_memo/submit_to_taxjar.rb', line 30

def submit
  require 'taxjar'
  begin
    if existing_record?
      puts "Record already exists on Taxjar for credit memo #{taxjar_id}"
      Result.new(
        success: false,
        message: "Record already exists on Taxjar for credit memo #{taxjar_id}"
      )
    else
      new_record = @client.create_refund(details)
      Result.new(
        success: true,
        original_tax_amount: (@credit_memo.tax_total * -1).to_f,
        calculated_tax: new_record.sales_tax
      )
    end
  rescue Taxjar::Error::ServerError => e
    Result.new(
      success: false,
      message: e.message
    )
  end
end

#taxjar_idObject



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

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

#updateObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/services/credit_memo/submit_to_taxjar.rb', line 74

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

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

    updated_record = @client.update_refund(details)

    Result.new(
      success: true,
      original_tax_amount: (@credit_memo.tax_total * -1).to_f,
      calculated_tax: updated_record.sales_tax
    )
  rescue Taxjar::Error::NotFound
    puts "Record not found on Taxjar for credit memo #{taxjar_id}"
    Result.new(
      success: false,
      message: "Record not found on Taxjar for credit memo #{taxjar_id}"
    )
  end
end