Sha256: 7f7105ab1acb0bd8004837a6fadc52dbb292a58c81e3e8c395aca9929ecbfb9d

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require 'time'

class Taxedo::Region
  def initialize(id)
    @id = id
    @data = YAML.load_file(Taxedo.base_data_path + "regions.yml")['taxedo']['regions'][@id]
  end

  def calculate(amount, options={})
    options = { :on => Time.now, :rule => 'standard' }.merge(options)

    receipt = Taxedo::Receipt.new(@id, amount)
    receipt.currency = @data['currency']

    taxes(options[:rule], options[:on]).each do |tax|
      receipt.add_tax tax[0], tax[1]
    end

    return receipt
  end

  private

  def tax_from_start_date(tax_id, start_at)
    selection = Time.at(0)

    @data['taxes'][tax_id].each do |tax_start_at, tax_rate|
      tax_start_at = Time.parse(tax_start_at.to_s)
      selection = tax_start_at if tax_start_at <= start_at and tax_start_at > selection
    end

    return selection.strftime('%Y%m%d').to_i
  end

  def taxes(rule, start_at)
    raise 'TAXEDO: This tax rule doesn\'t exists!' if not @data['rules'][rule]

    @data['rules'][rule].map do |tax_id|
      tax_key = tax_from_start_date(tax_id, start_at)

      [tax_id, @data['taxes'][tax_id][tax_key]]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
taxedo-0.0.4 lib/taxedo/region.rb