Class: Spree::TaxExemption

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model
Defined in:
app/models/spree/tax_exemption.rb,
app/models/spree/tax_exemption/item_override.rb

Overview

A claim that a sale, or part of one, is exempt from tax — resale, government purchase, a charity's own use. Ephemeral evidence handed to a tax provider through Spree::TaxProvider::Base#estimate, never persisted: the claim is the input, the zero-amount TaxLine rows are the record.

Exemption is per jurisdiction (a certificate valid in one state and not the next) and per item (a resale order can hold own-use lines), which is why this is a scoped entry rather than a flag. One entry per certificate.

Defined Under Namespace

Classes: ItemOverride

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_snapshot(snapshot) ⇒ Spree::TaxExemption

Unknown keys are dropped rather than raising: a snapshot is read back long after it was written, possibly by a different version of Spree.

Parameters:

  • snapshot (Hash)

Returns:



84
85
86
87
88
89
# File 'app/models/spree/tax_exemption.rb', line 84

def self.from_snapshot(snapshot)
  snapshot = snapshot.to_h.stringify_keys
  overrides = Array(snapshot['item_overrides']).map { |override| ItemOverride.from_snapshot(override) }

  new(snapshot.slice(*attribute_names).symbolize_keys.merge(item_overrides: overrides))
end

Instance Method Details

#covers_item?(item) ⇒ Boolean

Whether this entry claims exemption for the given item.

Parameters:

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'app/models/spree/tax_exemption.rb', line 37

def covers_item?(item)
  override = override_for(item)
  return override.exempt? unless override.nil?

  true
end

#covers_jurisdiction?(country_code, state_code = nil) ⇒ Boolean

Whether this entry's scope reaches the given jurisdiction. An entry with no country claims every jurisdiction; one with a country but no state claims every state within it.

Parameters:

  • country_code (String, nil)
  • state_code (String, nil) (defaults to: nil)

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'app/models/spree/tax_exemption.rb', line 51

def covers_jurisdiction?(country_code, state_code = nil)
  return true if self.country_code.blank?
  return false unless self.country_code.casecmp?(country_code.to_s)
  return true if self.state_code.blank?

  self.state_code.casecmp?(state_code.to_s)
end

#reason_code_for(item) ⇒ String?

The reason to record for an item — the per-line override's when it names one, the entry's otherwise.

Parameters:

Returns:

  • (String, nil)


64
65
66
# File 'app/models/spree/tax_exemption.rb', line 64

def reason_code_for(item)
  override_for(item)&.reason_code.presence || reason_code
end

#to_snapshot ⇒ Hash

The claim as plain data, for freezing onto a placed order. Certificates lapse, get revoked and are edited, so a provider filing or crediting that sale later has to read the evidence the sale was priced with rather than whatever resolves on the day.

Returns:

  • (Hash)


74
75
76
77
# File 'app/models/spree/tax_exemption.rb', line 74

def to_snapshot
  attributes.except('item_overrides').
    merge('item_overrides' => Array(item_overrides).map(&:to_snapshot))
end