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
- #customer_sync_instance ⇒ Object
- #delete_from_taxjar ⇒ Object
- #evaluate_taxjar_submission ⇒ Object
- #record_already_exists_on_taxjar? ⇒ Boolean
- #resubmit_to_taxjar ⇒ Object
- #should_be_submitted_to_taxjar? ⇒ Boolean
- #should_sync_customer_with_taxjar? ⇒ Boolean
- #submit_to_taxjar ⇒ Object
- #sync_customer_with_taxjar ⇒ Object
- #taxjar_customer_id ⇒ Object
- #taxjar_submission_instance ⇒ Object
Instance Method Details
#customer_sync_instance ⇒ Object
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_taxjar ⇒ Object
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. end end |
#evaluate_taxjar_submission ⇒ Object
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 should_be_submitted_to_taxjar? 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
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_taxjar ⇒ Object
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? submitted_to_taxjar else res. end end |
#should_be_submitted_to_taxjar? ⇒ 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 should_be_submitted_to_taxjar? return false if shipping_address.nil? || shipping_address.state_code.nil? || !shipping_address.country_iso.in?(%w[US CA]) if is_a?(ReceiptDetail) ledger_company_account.present? && TAXABLE_STATES.detect do |state, v| state == shipping_address.state_code.to_sym && v[:source] == :taxjar && v[:account] == ledger_company_account.ledger_detail_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
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_taxjar ⇒ Object
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? submitted_to_taxjar else res. end end |
#sync_customer_with_taxjar ⇒ Object
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_id ⇒ Object
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_instance ⇒ Object
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 |