Class: ErpOrders::Taxation

Inherits:
Object
  • Object
show all
Defined in:
lib/erp_orders/taxation.rb

Instance Method Summary collapse

Instance Method Details

#calculate_online_tax(taxed_record, ctx) ⇒ Object

for online sales tax is only applied if the item purchased is being shipped to the same state the product is being shipped from



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/erp_orders/taxation.rb', line 16

def calculate_online_tax(taxed_record, ctx)
  # use yaml file for now, eventually needs to be updated to use tax service
  tax_service = YAML.load_file(File.join(Rails.root, 'config', 'taxes.yml')).symbolize_keys

  # if the origin state is the same as the destination state
  # determine taxes else taxes are 0
  if ctx[:origin_address][:state] == ctx[:destination_address][:state]
    taxes = (tax_service[:state_tax_rate] * ctx[:amount]).round(2)
    taxed_record.taxed = true
    taxed_record.sales_tax = taxes
    taxed_record.save!

    # create a tax line to record the tax rate
    if taxed_record.sales_tax_lines.empty?
      sales_tax_line = SalesTaxLine.new
      sales_tax_line.taxed_record = taxed_record
    else
      sales_tax_line = taxed_record.sales_tax_lines.first
    end

    sales_tax_line.rate = tax_service[:state_tax_rate]
    sales_tax_line.save!
  else
    taxes = 0
    taxed_record.taxed = false
    taxed_record.sales_tax = taxes
    taxed_record.save
  end

  taxes
end

#calculate_tax(taxed_record, ctx) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/erp_orders/taxation.rb', line 6

def calculate_tax(taxed_record, ctx)

  if ctx[:is_online_sale]
    calculate_online_tax(taxed_record, ctx)
  end

end