app/models/account.rb in has_accounts-0.19.1 vs app/models/account.rb in has_accounts-0.20.0

- old
+ new

@@ -1,32 +1,33 @@ class Account < ActiveRecord::Base # Access restrictions - attr_accessible :title, :code, :account_type_id + attr_accessible :title, :code # Scopes default_scope :order => 'code' - + # Dummy scope to make scoped_by happy scope :by_value_period, scoped - + # Validation validates_presence_of :code, :title - + # String def to_s(format = :default) "%s (%s)" % [title, code] end # Account Type # ============ belongs_to :account_type + attr_accessible :title, :code, :account_type_id, :account_type validates_presence_of :account_type def is_asset_account? Account.by_type(['current_assets', 'capital_assets', 'costs']).exists?(self) end - + def is_liability_account? !is_asset_account? end def is_balance_account? @@ -42,16 +43,16 @@ end # Holder # ====== belongs_to :holder, :polymorphic => true - + # Bookings # ======== has_many :credit_bookings, :class_name => "Booking", :foreign_key => "credit_account_id" has_many :debit_bookings, :class_name => "Booking", :foreign_key => "debit_account_id" - + def bookings Booking.by_account(id) end # Attachments @@ -62,11 +63,11 @@ # Helpers # ======= def self.overview(value_range = Date.today, format = :default) Account.all.map{|a| a.to_s(value_range, format)} end - + # Calculations def turnover(selector = Date.today, inclusive = true) equality = "=" if inclusive if selector.respond_to?(:first) and selector.respond_to?(:last) @@ -106,17 +107,17 @@ end end credit_amount = credit_bookings.where(condition).sum(:amount) debit_amount = debit_bookings.where(condition).sum(:amount) - + [credit_amount || 0.0, debit_amount || 0.0] end - + def saldo(selector = Date.today, inclusive = true) credit_amount, debit_amount = turnover(selector, inclusive) amount = credit_amount - debit_amount - + return is_asset_account? ? amount : -amount end end