Module: Models::TaxjarSubmittable

Extended by:
ActiveSupport::Concern
Includes:
AfterCommitEverywhere
Included in:
CreditMemo, Invoice, ReceiptDetail
Defined in:
app/concerns/models/taxjar_submittable.rb

Instance Method Summary collapse

Instance Method Details

#customer_sync_instanceObject



54
55
56
57
58
59
60
61
# File 'app/concerns/models/taxjar_submittable.rb', line 54

def customer_sync_instance
  if billing_entity.tax_exemptions.effective_on(effective_date).where(state_code: shipping_address.state_code).any?
    Customer::SyncWithTaxjar.new(billing_entity, { effective_date: })
  elsif tax_exempt? && billing_entity.is_e_commerce_misc?
    # used for invoices/credit memos sold through etailers where the receiving customer is tax exempt
    Customer::SyncAddressWithTaxjar.new(billing_entity, shipping_address, { effective_date: })
  end
end

#delete_from_taxjarObject



83
84
85
86
87
88
89
90
# File 'app/concerns/models/taxjar_submittable.rb', line 83

def delete_from_taxjar
  res = taxjar_submission_instance.delete
  if res.success?
    exempt_from_taxjar
  else
    res.message
  end
end

#evaluate_taxjar_submissionObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/concerns/models/taxjar_submittable.rb', line 29

def evaluate_taxjar_submission
  return 'already submitted' unless pending_submission?

  if 
    if is_a?(Invoice) && Rails.env.production? && !(try(:invoice_type) == 'CI')
      # Queue worker after transaction commits to ensure invoice exists
      after_commit { SubmitInvoiceToTaxjarWorker.perform_in(30.seconds, id) }
    else
      submit_to_taxjar
    end
  else
    exempt_from_taxjar
  end
end

#record_already_exists_on_taxjar?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'app/concerns/models/taxjar_submittable.rb', line 108

def record_already_exists_on_taxjar?
  taxjar_submission_instance.existing_record?
end

#resubmit_to_taxjarObject



73
74
75
76
77
78
79
80
81
# File 'app/concerns/models/taxjar_submittable.rb', line 73

def resubmit_to_taxjar
  sync_customer_with_taxjar
  res = taxjar_submission_instance.update
  if res.success?
    
  else
    res.message
  end
end

#should_be_submitted_to_taxjar?Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/concerns/models/taxjar_submittable.rb', line 112

def 
  return false if shipping_address.nil? || shipping_address.state_code.nil? || !shipping_address.country_iso.in?(%w[US CA])

  if is_a?(ReceiptDetail)
    .present? &&
      TAXABLE_STATES.detect do |state, v|
        state == shipping_address.state_code.to_sym &&
          v[:source] == :taxjar &&
          v[:account] == ..number
      end
  else
    TAXABLE_STATES.detect do |state, v|
      state == shipping_address.state_code.to_sym &&
        v[:source] == :taxjar &&
        (v[:effective].nil? || Date.parse(v[:effective]) <= effective_date)
    end
  end
end

#should_sync_customer_with_taxjar?Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
# File 'app/concerns/models/taxjar_submittable.rb', line 131

def should_sync_customer_with_taxjar?
  return false if billing_entity.nil?

  billing_entity.tax_exemptions.effective_on(effective_date).where(state_code: shipping_address.state_code).any? ||
    (tax_exempt? && billing_entity.is_e_commerce_misc?)
end

#submit_to_taxjarObject



63
64
65
66
67
68
69
70
71
# File 'app/concerns/models/taxjar_submittable.rb', line 63

def submit_to_taxjar
  sync_customer_with_taxjar
  res = taxjar_submission_instance.submit
  if res.success?
    
  else
    res.message
  end
end

#sync_customer_with_taxjarObject



92
93
94
95
96
# File 'app/concerns/models/taxjar_submittable.rb', line 92

def sync_customer_with_taxjar
  return true unless Rails.env.production?

  customer_sync_instance.sync if should_sync_customer_with_taxjar?
end

#taxjar_customer_idObject



98
99
100
101
102
103
104
105
106
# File 'app/concerns/models/taxjar_submittable.rb', line 98

def taxjar_customer_id
  return nil if billing_entity.nil?

  if should_sync_customer_with_taxjar?
    customer_sync_instance.taxjar_id
  else
    Customer::SyncWithTaxjar.new(billing_entity).taxjar_id
  end
end

#taxjar_submission_instanceObject



44
45
46
47
48
49
50
51
52
# File 'app/concerns/models/taxjar_submittable.rb', line 44

def taxjar_submission_instance
  if is_a?(Invoice)
    Invoicing::SubmitToTaxjar.new(self)
  elsif is_a?(CreditMemo)
    CreditMemo::SubmitToTaxjar.new(self)
  elsif is_a?(ReceiptDetail)
    Receipt::SubmitToTaxjar.new(self)
  end
end