lib/taxedo/region.rb in taxedo-0.0.4 vs lib/taxedo/region.rb in taxedo-0.0.5

- old
+ new

@@ -1,26 +1,37 @@ +require 'yaml' 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) + options = { :on => Time.now, :rule => 'standard', :equations => 'compiled'}.merge(options) receipt = Taxedo::Receipt.new(@id, amount) receipt.currency = @data['currency'] + receipt.equation_type = equation_type_from_date(options[:on]).nil? ? options[:equations] : equation_type_from_date(options[:on]) taxes(options[:rule], options[:on]).each do |tax| receipt.add_tax tax[0], tax[1] end return receipt end private + + def equation_type_from_date(start_at) + selection = Time.at(0) + @data['equation_types'].each do |equation_start_at, equation| + equation_start_at = Time.parse(equation_start_at.to_s) + selection = equation_start_at if equation_start_at <= start_at and equation_start_at > selection + end + @data['equation_types'][selection.strftime('%Y%m%d').to_i] + end def tax_from_start_date(tax_id, start_at) selection = Time.at(0) @data['taxes'][tax_id].each do |tax_start_at, tax_rate|