Sha256: 485dea452a8d28af1cb2fc927cff77f579ee0834120bc0eda299a985f8fa158d

Contents?: true

Size: 881 Bytes

Versions: 29

Compression:

Stored size: 881 Bytes

Contents

module Pacioli
  class PostingRule < ActiveRecord::Base
    belongs_to :company, foreign_key: :pacioli_company_id

    serialize :rules

    def with_name(name)
      self.name = name
    end

    def debit(account_name, options={})
      self.rules ||= {}
      self.rules[:debits] ||= []
      self.rules[:debits] << prepare_rules(account_name, options)
    end

    def credit(account_name, options={})
      self.rules ||= {}
      self.rules[:credits] ||= []
      self.rules[:credits] << prepare_rules(account_name, options)
    end

    def prepare_rules(account_name, options={})
      options[:percentage] = 100 if options.empty?
      options[:account] = account_name
      options
    end

    def balanced?
      self.rules[:credits].inject(0) { |sum, cr| sum += cr[:percentage] } == self.rules[:debits].inject(0) { |sum, dr| sum += dr[:percentage] }
    end

  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
pacioli-0.0.29 lib/pacioli/posting_rule.rb
pacioli-0.0.28 lib/pacioli/posting_rule.rb
pacioli-0.0.27 lib/pacioli/posting_rule.rb
pacioli-0.0.26 lib/pacioli/posting_rule.rb
pacioli-0.0.25 lib/pacioli/posting_rule.rb
pacioli-0.0.24 lib/pacioli/posting_rule.rb
pacioli-0.0.23 lib/pacioli/posting_rule.rb
pacioli-0.0.22 lib/pacioli/posting_rule.rb
pacioli-0.0.21 lib/pacioli/posting_rule.rb
pacioli-0.0.20 lib/pacioli/posting_rule.rb
pacioli-0.0.19 lib/pacioli/posting_rule.rb
pacioli-0.0.18 lib/pacioli/posting_rule.rb
pacioli-0.0.17 lib/pacioli/posting_rule.rb
pacioli-0.0.16 lib/pacioli/posting_rule.rb
pacioli-0.0.15 lib/pacioli/posting_rule.rb
pacioli-0.0.14 lib/pacioli/posting_rule.rb
pacioli-0.0.13 lib/pacioli/posting_rule.rb
pacioli-0.0.12 lib/pacioli/posting_rule.rb
pacioli-0.0.11 lib/pacioli/posting_rule.rb
pacioli-0.0.10 lib/pacioli/posting_rule.rb