Sha256: a8ba9dee280d151d65d904ace8fd20590df98f564956c65bf893cea414e9092b

Contents?: true

Size: 1.3 KB

Versions: 8

Compression:

Stored size: 1.3 KB

Contents

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

    validates :description, presence: true
    validates :klass, presence: true

    accepts_nested_attributes_for :tax_method_values, :allow_destroy => true

    validates_each :tax_method_values do |record, attr, value|
      if record.klass.present?
        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

      ((100*total_tax.to_f).to_i).to_f/(100.to_f)
    end

    def admin_label
      self.description
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
piggybak-0.7.7 app/models/piggybak/tax_method.rb
piggybak-0.7.6 app/models/piggybak/tax_method.rb
piggybak-0.7.5 app/models/piggybak/tax_method.rb
piggybak-0.7.4 app/models/piggybak/tax_method.rb
piggybak-0.7.3 app/models/piggybak/tax_method.rb
piggybak-0.7.2 app/models/piggybak/tax_method.rb
piggybak-0.7.1 app/models/piggybak/tax_method.rb
piggybak-0.7.0 app/models/piggybak/tax_method.rb