Refactored finance/ordergroups|transactions module.

This commit is contained in:
benni 2011-06-09 21:35:05 +02:00
parent fc1d130113
commit ea6348bc5c
38 changed files with 967 additions and 443 deletions

View file

@ -4,14 +4,15 @@ class FinancialTransaction < ActiveRecord::Base
belongs_to :ordergroup
belongs_to :user
validates_presence_of :note, :user_id, :ordergroup_id
validates_presence_of :amount, :note, :user_id, :ordergroup_id
validates_numericality_of :amount
# Custom attribute setter that accepts decimal numbers using localized decimal separator.
def amount=(amount)
self[:amount] = String.delocalized_decimal(amount)
end
localize_input_of :amount
# Use this save method instead of simple save and after callback
def add_transaction!
ordergroup.add_financial_transaction! amount, note, user
end
end
# == Schema Information

View file

@ -178,7 +178,7 @@ class Order < ActiveRecord::Base
transaction do # Start updating account balances
for group_order in gos
price = group_order.price * -1 # decrease! account balance
group_order.ordergroup.add_financial_transaction(price, transaction_note, user)
group_order.ordergroup.add_financial_transaction!(price, transaction_note, user)
end
if stockit? # Decreases the quantity of stock_articles

View file

@ -38,11 +38,11 @@ class Ordergroup < Group
# Creates a new FinancialTransaction for this Ordergroup and updates the account_balance accordingly.
# Throws an exception if it fails.
def add_financial_transaction(amount, note, user)
def add_financial_transaction!(amount, note, user)
transaction do
trans = FinancialTransaction.new(:ordergroup => self, :amount => amount, :note => note, :user => user)
trans.save!
self.account_balance += trans.amount
self.account_balance = financial_transactions.sum('amount')
self.account_updated = trans.created_on
save!
notify_negative_balance(trans)