Sha256: 81bd1c51be29be07bf1b6dfa6ee1121e88805b9156b2d5ef5f7f183d77c76ddf

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

module Piggybak
  class TaxMethod < ActiveRecord::Base
    has_many :tax_method_values, :dependent => :destroy
    alias :metadata :tax_method_values

    validates_presence_of :description
    validates_presence_of :klass

    accepts_nested_attributes_for :tax_method_values, :allow_destroy => true

    validates_each :tax_method_values do |record, attr, value|
      if record.klass
        calculator = record.klass.constantize
        metadata_keys = value.collect { |v| v.key }.sort
        if calculator::KEYS.sort != metadata_keys
          if calculator::KEYS.empty?
            record.errors.add attr, "You don't need any metadata for this method."
          else
            record.errors.add attr, "You must define key values for #{calculator::KEYS.join(', ')} for this tax method."
          end
        end
      end
    end

    def klass_enum 
      Piggybak.config.tax_calculators
    end

    def self.calculate_tax(object)
      total_tax = 0

      TaxMethod.all.each do |tax_method|
        calculator = tax_method.klass.constantize
        if calculator.available?(tax_method, object)
          total_tax += calculator.rate(tax_method, object)
        end 
      end
   
      total_tax
    end

    def admin_label
      self.description
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
piggybak-0.6.8 app/models/piggybak/tax_method.rb
piggybak-0.6.7 app/models/piggybak/tax_method.rb
piggybak-0.6.6 app/models/piggybak/tax_method.rb
piggybak-0.6.5 app/models/piggybak/tax_method.rb
piggybak-0.6.4 app/models/piggybak/tax_method.rb
piggybak-0.6.3 app/models/piggybak/tax_method.rb
piggybak-0.6.2 app/models/piggybak/tax_method.rb
piggybak-0.6.1 app/models/piggybak/tax_method.rb
piggybak-0.6.0 app/models/piggybak/tax_method.rb