Skip to content

Commit

Permalink
Merge pull request mbulat#15 from willglynn/balance_refactoring
Browse files Browse the repository at this point in the history
Balance refactoring
  • Loading branch information
mbulat committed Feb 19, 2013
2 parents 01803e5 + 1fea8af commit 9358431
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 162 deletions.
26 changes: 24 additions & 2 deletions app/models/plutus/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,36 @@ module Plutus
class Account < ActiveRecord::Base
attr_accessible :name, :contra

has_many :credit_amounts
has_many :debit_amounts
has_many :credit_amounts, :extend => AmountsExtension
has_many :debit_amounts, :extend => AmountsExtension
has_many :credit_transactions, :through => :credit_amounts, :source => :transaction
has_many :debit_transactions, :through => :debit_amounts, :source => :transaction

validates_presence_of :type, :name
validates_uniqueness_of :name

# The credit balance for the account.
#
# @example
# >> asset.credits_balance
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
credit_amounts.balance
end

# The debit balance for the account.
#
# @example
# >> asset.debits_balance
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
debit_amounts.balance
end

# The trial balance of all accounts in the system. This should always equal zero,
# otherwise there is an error in the system.
#
Expand Down
17 changes: 17 additions & 0 deletions app/models/plutus/amounts_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Plutus
# Association extension for has_many :amounts relations. Internal.
module AmountsExtension
# Returns a sum of the referenced Amount objects.
def balance
balance = BigDecimal.new('0')
each do |amount_record|
if amount_record.amount
balance += amount_record.amount
else
balance = nil
end
end
return balance
end
end
end
30 changes: 0 additions & 30 deletions app/models/plutus/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,6 @@ module Plutus
# @author Michael Bulat
class Asset < Account

# The credit balance for the account.
#
# @example
# >> asset.credits_balance
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
credits_balance = BigDecimal.new('0')
credit_amounts.each do |credit_amount|
credits_balance += credit_amount.amount
end
return credits_balance
end

# The debit balance for the account.
#
# @example
# >> asset.debits_balance
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
debits_balance = BigDecimal.new('0')
debit_amounts.each do |debit_amount|
debits_balance += debit_amount.amount
end
return debits_balance
end

# The balance of the account.
#
# Assets have normal debit balances, so the credits are subtracted from the debits
Expand Down
31 changes: 0 additions & 31 deletions app/models/plutus/equity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,6 @@ module Plutus
# @author Michael Bulat
class Equity < Account

# The credit balance for the account.
#
# @example
# >> equity.credits_balance
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
credits_balance = BigDecimal.new('0')
credit_amounts.each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
end
return credits_balance
end

# The debit balance for the account.
#
# @example
# >> equity.debits_balance
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
debits_balance = BigDecimal.new('0')
debit_amounts.each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
end
return debits_balance
end


# The balance of the account.
#
# Equity accounts have normal credit balances, so the debits are subtracted from the credits
Expand Down
30 changes: 0 additions & 30 deletions app/models/plutus/expense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,6 @@ module Plutus
# @author Michael Bulat
class Expense < Account

# The credit balance for the account.
#
# @example
# >> expense.credits_balance
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
credits_balance = BigDecimal.new('0')
credit_amounts.each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
end
return credits_balance
end

# The debit balance for the account.
#
# @example
# >> expense.debits_balance
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
debits_balance = BigDecimal.new('0')
debit_amounts.each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
end
return debits_balance
end

# The balance of the account.
#
# Expenses have normal debit balances, so the credits are subtracted from the debits
Expand Down
30 changes: 0 additions & 30 deletions app/models/plutus/liability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,6 @@ module Plutus
# @author Michael Bulat
class Liability < Account

# The credit balance for the account.
#
# @example
# >> liability.credits_balance
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
credits_balance = BigDecimal.new('0')
credit_amounts.each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
end
return credits_balance
end

# The debit balance for the account.
#
# @example
# >> liability.debits_balance
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
debits_balance = BigDecimal.new('0')
debit_amounts.each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
end
return debits_balance
end

# The balance of the account.
#
# Liability accounts have normal credit balances, so the debits are subtracted from the credits
Expand Down
30 changes: 0 additions & 30 deletions app/models/plutus/revenue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,6 @@ module Plutus
# @author Michael Bulat
class Revenue < Account

# The credit balance for the account.
#
# @example
# >> revenue.credits_balance
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
credits_balance = BigDecimal.new('0')
credit_amounts.each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
end
return credits_balance
end

# The debit balance for the account.
#
# @example
# >> revenue.debits_balance
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
debits_balance = BigDecimal.new('0')
debit_amounts.each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
end
return debits_balance
end

# The balance of the account.
#
# Revenue accounts have normal credit balances, so the debits are subtracted from the credits
Expand Down
12 changes: 3 additions & 9 deletions app/models/plutus/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Transaction < ActiveRecord::Base
attr_accessible :description, :commercial_document

belongs_to :commercial_document, :polymorphic => true
has_many :credit_amounts
has_many :debit_amounts
has_many :credit_amounts, :extend => AmountsExtension
has_many :debit_amounts, :extend => AmountsExtension
has_many :credit_accounts, :through => :credit_amounts, :source => :account
has_many :debit_accounts, :through => :debit_amounts, :source => :account

Expand Down Expand Up @@ -71,13 +71,7 @@ def has_debit_amounts?
end

def amounts_cancel?
errors[:base] << "The credit and debit amounts are not equal" if difference_of_amounts != 0
end

def difference_of_amounts
credit_amount_total = credit_amounts.inject(0) {|sum, credit_amount| sum + credit_amount.amount.to_i}
debit_amount_total = debit_amounts.inject(0) {|sum, debit_amount| sum + debit_amount.amount.to_i}
credit_amount_total - debit_amount_total
errors[:base] << "The credit and debit amounts are not equal" if credit_amounts.balance != debit_amounts.balance
end
end
end
8 changes: 8 additions & 0 deletions spec/models/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ module Plutus
transaction.errors['base'].should == ["The credit and debit amounts are not equal"]
end

it "should require the debit and credit amounts to cancel even with fractions" do
transaction = FactoryGirl.build(:transaction)
transaction.credit_amounts << FactoryGirl.build(:credit_amount, :amount => 100.1, :transaction => transaction)
transaction.debit_amounts << FactoryGirl.build(:debit_amount, :amount => 100.2, :transaction => transaction)
transaction.should_not be_valid
transaction.errors['base'].should == ["The credit and debit amounts are not equal"]
end

it "should have a polymorphic commercial document associations" do
mock_document = FactoryGirl.create(:asset) # one would never do this, but it allows us to not require a migration for the test
transaction = FactoryGirl.build(:transaction_with_credit_and_debit, :commercial_document => mock_document)
Expand Down

0 comments on commit 9358431

Please sign in to comment.