Skip to content

Commit

Permalink
allow date ranges on balance calculations and calculation with DB sum
Browse files Browse the repository at this point in the history
  • Loading branch information
mbulat committed Jul 28, 2015
1 parent fd4aa3f commit bcabf46
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 24 deletions.
20 changes: 10 additions & 10 deletions app/models/plutus/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ class Account < ActiveRecord::Base
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
def balance(options={})
if self.class == Plutus::Account
raise(NoMethodError, "undefined method 'balance'")
else
if self.normal_credit_balance ^ contra
credits_balance - debits_balance
credits_balance(options) - debits_balance(options)
else
debits_balance - credits_balance
debits_balance(options) - credits_balance(options)
end
end
end
Expand All @@ -78,8 +78,8 @@ def balance
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
credit_amounts.balance
def credits_balance(options={})
credit_amounts.balance(options)
end

# The debit balance for the account.
Expand All @@ -89,8 +89,8 @@ def credits_balance
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
debit_amounts.balance
def debits_balance(options={})
debit_amounts.balance(options)
end

# This class method is used to return the balance of all accounts
Expand All @@ -103,17 +103,17 @@ def debits_balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
#
# @return [BigDecimal] The decimal value balance
def self.balance
def self.balance(options={})
if self.new.class == Plutus::Account
raise(NoMethodError, "undefined method 'balance'")
else
accounts_balance = BigDecimal.new('0')
accounts = self.all
accounts.each do |account|
if account.contra
accounts_balance -= account.balance
accounts_balance -= account.balance(options)
else
accounts_balance += account.balance
accounts_balance += account.balance(options)
end
end
accounts_balance
Expand Down
12 changes: 11 additions & 1 deletion app/models/plutus/amounts_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ module Plutus
# Association extension for has_many :amounts relations. Internal.
module AmountsExtension
# Returns a sum of the referenced Amount objects.
def balance
def balance(hash)
if hash[:from_date] && hash[:to_date]
from_date = hash[:from_date].kind_of?(Date) ? hash[:from_date] : Date.parse(hash[:from_date])
to_date = hash[:to_date].kind_of?(Date) ? hash[:to_date] : Date.parse(hash[:to_date])
includes(:entry).where('plutus_entries.date' => from_date..to_date).sum(:amount)
else
sum(:amount)
end
end

def balance_for_new_record
balance = BigDecimal.new('0')
each do |amount_record|
if amount_record.amount
Expand Down
8 changes: 4 additions & 4 deletions app/models/plutus/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Asset < Account
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
super
def balance(options={})
super(options)
end

# This class method is used to return
Expand All @@ -35,8 +35,8 @@ def balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
#
# @return [BigDecimal] The decimal value balance
def self.balance
super
def self.balance(options={})
super(options)
end
end
end
2 changes: 1 addition & 1 deletion app/models/plutus/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def has_debit_amounts?
end

def amounts_cancel?
errors[:base] << "The credit and debit amounts are not equal" if credit_amounts.balance != debit_amounts.balance
errors[:base] << "The credit and debit amounts are not equal" if credit_amounts.balance_for_new_record != debit_amounts.balance_for_new_record
end
end
end
4 changes: 2 additions & 2 deletions app/models/plutus/equity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Equity < Account
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
def balance(options={})
super
end

Expand All @@ -35,7 +35,7 @@ def balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
#
# @return [BigDecimal] The decimal value balance
def self.balance
def self.balance(options={})
super
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/plutus/expense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Expense < Account
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
def balance(options={})
super
end

Expand All @@ -35,7 +35,7 @@ def balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
#
# @return [BigDecimal] The decimal value balance
def self.balance
def self.balance(options={})
super
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/plutus/liability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Liability < Account
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
def balance(options={})
super
end

Expand All @@ -35,7 +35,7 @@ def balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
#
# @return [BigDecimal] The decimal value balance
def self.balance
def self.balance(options={})
super
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/plutus/revenue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Revenue < Account
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
def balance(options={})
super
end

Expand All @@ -35,7 +35,7 @@ def balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
#
# @return [BigDecimal] The decimal value balance
def self.balance
def self.balance(options={})
super
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/support/account_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
describe "instance methods" do
its(:balance) { should be_kind_of(BigDecimal) }

it "reports a balance with date range" do
account.balance(:from_date => Date.today - 1.year, :to_date => Date.today).should be_kind_of(BigDecimal)
end

it { should respond_to(:credit_entries) }
it { should respond_to(:debit_entries) }
end
Expand Down

0 comments on commit bcabf46

Please sign in to comment.