Skip to content

Commit

Permalink
Dates dates dates =(
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioelia committed Jan 26, 2013
1 parent cde2a8b commit 1da65de
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 71 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ test.sqlite3
spec/debug.log
.DS_Store
.yardoc/
pkg/
pkg/
*~
48 changes: 34 additions & 14 deletions app/models/plutus/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ class Asset < Account
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
def credits_balance(start_date = nil, end_date = nil )
credits_balance = BigDecimal.new('0')
credit_amounts.each do |credit_amount|
credits_balance += credit_amount.amount
if start_date || end_date
credits_by_date(start_date, end_date).each do |credit_amount|
credits_balance += credit_amount.amount
end
else
credit_amounts.each do |credit_amount|
credits_balance += credit_amount.amount
end
end


return credits_balance
end

Expand All @@ -31,10 +39,16 @@ def credits_balance
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
def debits_balance(start_date = nil, end_date = nil )
debits_balance = BigDecimal.new('0')
debit_amounts.each do |debit_amount|
debits_balance += debit_amount.amount
if start_date || end_date
debits_by_date(start_date, end_date).each do |debit_amount|
debits_balance += debit_amount.amount
end
else
debit_amounts.each do |debit_amount|
debits_balance += debit_amount.amount
end
end
return debits_balance
end
Expand All @@ -49,11 +63,15 @@ def debits_balance
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
def balance(start_date = nil, end_date = nil)
unless contra
debits_balance - credits_balance
(start_date.nil? && end_date.nil?) ?
debits_balance - credits_balance :
debits_balance(start_date,end_date) - credits_balance(start_date,end_date)
else
credits_balance - debits_balance
(start_date.nil? && end_date.nil?) ?
credits_balance - debits_balance :
credits_balance(start_date,end_date) - debits_balance(start_date,end_date)
end
end

Expand All @@ -67,16 +85,18 @@ def balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
#
# @return [BigDecimal] The decimal value balance
def self.balance( startDate = nil, endDate = nil )
def self.balance( start_date = nil, end_date = nil )
accounts_balance = BigDecimal.new('0')
accounts = self.find(:all)
accounts.each do |asset|
asset.start_date = startDate if startDate
asset.end_date = endDate if endDate
unless asset.contra
accounts_balance += asset.balance
(start_date.nil? && end_date.nil?) ?
accounts_balance += asset.balance :
accounts_balance += asset.balance( start_date, end_date )
else
accounts_balance -= asset.balance
(start_date.nil? && end_date.nil?) ?
accounts_balance -= asset.balance :
accounts_balance -= asset.balance( start_date, end_date )
end
end
accounts_balance
Expand Down
50 changes: 36 additions & 14 deletions app/models/plutus/equity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ class Equity < Account
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
def credits_balance( start_date = nil, end_date = nil )
credits_balance = BigDecimal.new('0')
credit_amounts.each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
if start_date || end_date
credits_by_date( start_date, end_date ).each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
end
else
credit_amounts.each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
end
end
return credits_balance
end
Expand All @@ -31,10 +37,16 @@ def credits_balance
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
def debits_balance( start_date = nil, end_date = nil )
debits_balance = BigDecimal.new('0')
debit_amounts.each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
if start_date || end_date
debits_by_date( start_date, end_date ).each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
end
else
debit_amounts.each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
end
end
return debits_balance
end
Expand All @@ -50,11 +62,15 @@ def debits_balance
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
def balance(start_date = nil, end_date = nil)
unless contra
credits_balance - debits_balance
(start_date.nil? && end_date.nil?) ?
credits_balance - debits_balance :
credits_balance - debits_balance( start_date, end_date )
else
debits_balance - credits_balance
(start_date.nil? && end_date.nil?) ?
debits_balance - credits_balance :
debits_balance - credits_balance( start_date, end_date )
end
end

Expand All @@ -68,16 +84,22 @@ def balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
#
# @return [BigDecimal] The decimal value balance
def self.balance( startDate = nil, endDate = nil )
def self.balance( start_date = nil, end_date = nil )
accounts_balance = BigDecimal.new('0')
accounts = self.find(:all)
accounts.each do |equity|
equity.start_date = startDate if startDate
equity.end_date = endDate if endDate

unless equity.contra
accounts_balance += equity.balance
(start_date.nil? && end_date.nil?) ?
accounts_balance += equity.balance :
accounts_balance += equity.balance( start_date, end_date )

else
accounts_balance -= equity.balance

(start_date.nil? && end_date.nil?) ?
accounts_balance -= equity.balance :
accounts_balance -= equity.balance( start_date, end_date )

end
end
accounts_balance
Expand Down
46 changes: 32 additions & 14 deletions app/models/plutus/expense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ class Expense < Account
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
def credits_balance( start_date = nil, end_date = nil )
credits_balance = BigDecimal.new('0')
credit_amounts.each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
if start_date || end_date
credits_by_date( start_date, end_date ).each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
end
else
credit_amounts.each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
end
end
return credits_balance
end
Expand All @@ -31,10 +37,16 @@ def credits_balance
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
def debits_balance( start_date = nil, end_date = nil )
debits_balance = BigDecimal.new('0')
debit_amounts.each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
if start_date || end_date
debits_by_date( start_date, end_date ).each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
end
else
debit_amounts.each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
end
end
return debits_balance
end
Expand All @@ -49,11 +61,15 @@ def debits_balance
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
def balance( start_date = nil, end_date = nil )
unless contra
debits_balance - credits_balance
(start_date.nil? && end_date.nil?) ?
debits_balance - credits_balance :
debits_balance( start_date, end_date ) - credits_balance( start_date, end_date )
else
credits_balance - debits_balance
(start_date.nil? && end_date.nil?) ?
credits_balance - debits_balance :
credits_balance( start_date, end_date ) - debits_balance( start_date, end_date )
end
end

Expand All @@ -67,16 +83,18 @@ def balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
#
# @return [BigDecimal] The decimal value balance
def self.balance( startDate = nil, endDate = nil )
def self.balance( start_date = nil, end_date = nil )
accounts_balance = BigDecimal.new('0')
accounts = self.find(:all)
accounts.each do |expense|
expense.start_date = startDate if startDate
expense.end_date = endDate if endDate
unless expense.contra
accounts_balance += expense.balance
(start_date.nil? && end_date.nil?) ?
accounts_balance += expense.balance :
accounts_balance += expense.balance( start_date, end_date )
else
accounts_balance -= expense.balance
(start_date.nil? && end_date.nil?) ?
accounts_balance -= expense.balance :
accounts_balance -= expense.balance( start_date, end_date )
end
end
accounts_balance
Expand Down
46 changes: 32 additions & 14 deletions app/models/plutus/liability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ class Liability < Account
# => #<BigDecimal:103259bb8,'0.3E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def credits_balance
def credits_balance( start_date = nil, end_date = nil )
credits_balance = BigDecimal.new('0')
credit_amounts.each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
if start_date || end_date
credits_by_date( start_date, end_date ).each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
end
else
credit_amounts.each do |credit_amount|
credits_balance = credits_balance + credit_amount.amount
end
end
return credits_balance
end
Expand All @@ -31,10 +37,16 @@ def credits_balance
# => #<BigDecimal:103259bb8,'0.1E4',4(12)>
#
# @return [BigDecimal] The decimal value credit balance
def debits_balance
def debits_balance( start_date = nil, end_date = nil )
debits_balance = BigDecimal.new('0')
debit_amounts.each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
if start_date || end_date
debits_by_date( start_date, end_date ).each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
end
else
debit_amounts.each do |debit_amount|
debits_balance = debits_balance + debit_amount.amount
end
end
return debits_balance
end
Expand All @@ -49,11 +61,15 @@ def debits_balance
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
def balance( start_date = nil, end_date = nil )
unless contra
credits_balance - debits_balance
(start_date.nil? && end_date.nil?) ?
credits_balance - debits_balance :
credits_balance - debits_balance( start_date, end_date )
else
debits_balance - credits_balance
(start_date.nil? && end_date.nil?) ?
debits_balance - credits_balance :
debits_balance - credits_balance( start_date, end_date )
end
end

Expand All @@ -62,16 +78,18 @@ def balance
# @example
# >> Plutus::Liability.balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
def self.balance( startDate = nil, endDate = nil )
def self.balance( start_date = nil, end_date = nil )
accounts_balance = BigDecimal.new('0')
accounts = self.find(:all)
accounts.each do |liability|
liability.startDate = startDate if startDate
liability.endDate = endDate if endDate
unless liability.contra
accounts_balance += liability.balance
(start_date.nil? && end_date.nil?) ?
accounts_balance += liability.balance :
accounts_balance += liability.balance( start_date, end_date
else
accounts_balance -= liability.balance
(start_date.nil? && end_date.nil?) ?
accounts_balance -= liability.balance :
accounts_balance -= liability.balance( start_date, end_date )
end
end
accounts_balance
Expand Down
Loading

0 comments on commit 1da65de

Please sign in to comment.