Module: Models::TaxableLine

Extended by:
ActiveSupport::Concern
Included in:
LineItem
Defined in:
app/concerns/models/taxable_line.rb

Instance Attribute Summary collapse

Belongs to collapse

Instance Method Summary collapse

Instance Attribute Details

#do_not_calculate_taxObject

Returns the value of attribute do_not_calculate_tax.



15
16
17
# File 'app/concerns/models/taxable_line.rb', line 15

def do_not_calculate_tax
  @do_not_calculate_tax
end

Instance Method Details

#calculate_taxObject



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

def calculate_tax
  if tax_class_changed?
    update_tax_rate(**resource.get_rates_for_line(self),  auto_save: false)
  elsif tax_rate
    self.tax_total = taxable_total.to_f * tax_rate unless do_not_calculate_tax == true
  elsif resource.nil? || resource.is_a?(RoomConfiguration)
    update_tax_rate(rate: 0, tax_type: nil, resource_tax_rate_id: nil, auto_save: false)
  else
    resource.refresh_tax_rate
  end
end

#destination_stateState

Returns:

See Also:



10
# File 'app/concerns/models/taxable_line.rb', line 10

belongs_to :destination_state, class_name: 'State', foreign_key: 'destination_state_code', primary_key: 'code', optional: true

#has_tax?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/concerns/models/taxable_line.rb', line 51

def has_tax?
  tax_rate.present? and tax_type.present? and tax_total.present? and tax_total > 0
end

#is_international?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'app/concerns/models/taxable_line.rb', line 43

def is_international?
  if resource&.origin_address && resource.shipping_address
    resource.origin_address.country_iso3 != resource.shipping_address.country_iso3
  else
    false # default to handle legacy
  end
end

#origin_stateState

Returns:

See Also:



9
# File 'app/concerns/models/taxable_line.rb', line 9

belongs_to :origin_state, class_name: 'State', foreign_key: 'origin_state_code', primary_key: 'code', optional: true

#resource_tax_rateResourceTaxRate



11
# File 'app/concerns/models/taxable_line.rb', line 11

belongs_to :resource_tax_rate, optional: true

#set_initial_tax_rateObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/concerns/models/taxable_line.rb', line 18

def set_initial_tax_rate
  # !!!! MUST use !resource.nil? here - if you call resource.present? it will
  # !!!! do an active record call and ignore any changes that may have been made in memory
  return :no_resource if resource.nil?

  if resource.is_a?(RoomConfiguration)
    update_tax_rate(rate: 0, tax_type: nil, resource_tax_rate_id: nil, auto_save: false)
  else
    tr = resource.get_rates_for_line(self)
    update_tax_rate(rate: tr[:rate], tax_type: tr[:tax_type], resource_tax_rate_id: tr[:resource_tax_rate_id], auto_save: false)
  end
end

#update_tax_rate(rate:, resource_tax_rate_id:, tax_type:, auto_save: true) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/concerns/models/taxable_line.rb', line 55

def update_tax_rate(rate:, resource_tax_rate_id:, tax_type:, auto_save: true)
  self.tax_rate = rate
  self.tax_type = tax_type
  self.resource_tax_rate_id = resource_tax_rate_id
  self.tax_total = taxable_total.to_f * rate unless do_not_calculate_tax == true
  return unless auto_save

  # Retry on deadlock - can occur when parallel processes (e.g., customer merge workers)
  # update line_items on the same orders simultaneously
  Retryable.retryable(tries: 3, sleep: ->(n) { 2**n }, on: [ActiveRecord::Deadlocked]) do
    save
  end
end