Update model to support financial transactions #367
This change introduces two new data types to group the financial transactions. Now every transaction has a "type", which itself belongs to a "class". Types should be used add structured information to an transaction, instead of writing it into the notice textfield. E.g. this could be used to have different types depending on the source of money (cash vs. bank transfer). Classes are shown as different columns in the tables and will be uses to group transactions of specific types. They should be used if not the whole amount of ordergroup should be used to order food. E.g. if there is a deposit or membership fee, which is independent of the normal credit. This will allow us to implement additional features based on classes in the future. E.g. the sum of transactions in the "membership fee" class must be positive to allow food orders or show a big warning if it is bellow a certain value.
This commit is contained in:
parent
dc94e98138
commit
e7657b987f
21 changed files with 156 additions and 34 deletions
|
|
@ -4,6 +4,7 @@ class FinancialTransaction < ActiveRecord::Base
|
|||
belongs_to :ordergroup
|
||||
belongs_to :user
|
||||
belongs_to :financial_link
|
||||
belongs_to :financial_transaction_type
|
||||
|
||||
validates_presence_of :amount, :note, :user_id, :ordergroup_id
|
||||
validates_numericality_of :amount, greater_then: -100_000,
|
||||
|
|
@ -11,8 +12,18 @@ class FinancialTransaction < ActiveRecord::Base
|
|||
|
||||
localize_input_of :amount
|
||||
|
||||
after_initialize do
|
||||
initialize_financial_transaction_type
|
||||
end
|
||||
|
||||
# Use this save method instead of simple save and after callback
|
||||
def add_transaction!
|
||||
ordergroup.add_financial_transaction! amount, note, user
|
||||
ordergroup.add_financial_transaction! amount, note, user, financial_transaction_type
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def initialize_financial_transaction_type
|
||||
self.financial_transaction_type ||= FinancialTransactionType.default
|
||||
end
|
||||
end
|
||||
|
|
|
|||
6
app/models/financial_transaction_class.rb
Normal file
6
app/models/financial_transaction_class.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class FinancialTransactionClass < ActiveRecord::Base
|
||||
has_many :financial_transaction_types, dependent: :destroy
|
||||
|
||||
validates :name, presence: true
|
||||
validates_uniqueness_of :name
|
||||
end
|
||||
25
app/models/financial_transaction_type.rb
Normal file
25
app/models/financial_transaction_type.rb
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
class FinancialTransactionType < ActiveRecord::Base
|
||||
belongs_to :financial_transaction_class
|
||||
has_many :financial_transactions, dependent: :restrict_with_exception
|
||||
|
||||
validates :name, presence: true
|
||||
validates_uniqueness_of :name
|
||||
validates :financial_transaction_class, presence: true
|
||||
|
||||
before_destroy :restrict_deleting_last_financial_transaction_type
|
||||
|
||||
def self.default
|
||||
first
|
||||
end
|
||||
|
||||
def self.has_multiple_types
|
||||
self.count > 1
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# check if this is the last financial transaction type and deny
|
||||
def restrict_deleting_last_financial_transaction_type
|
||||
raise I18n.t('model.financial_transaction_type.no_delete_last') if FinancialTransactionType.count == 1
|
||||
end
|
||||
end
|
||||
|
|
@ -231,7 +231,7 @@ class Order < ActiveRecord::Base
|
|||
end
|
||||
|
||||
# Sets order.status to 'close' and updates all Ordergroup.account_balances
|
||||
def close!(user)
|
||||
def close!(user, transaction_type = nil)
|
||||
raise I18n.t('orders.model.error_closed') if closed?
|
||||
transaction_note = I18n.t('orders.model.notice_close', :name => name,
|
||||
:ends => ends.strftime(I18n.t('date.formats.default')))
|
||||
|
|
@ -243,7 +243,7 @@ class Order < ActiveRecord::Base
|
|||
for group_order in gos
|
||||
if group_order.ordergroup
|
||||
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, transaction_type)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -55,9 +55,9 @@ 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, link = nil)
|
||||
def add_financial_transaction!(amount, note, user, transaction_type, link = nil)
|
||||
transaction do
|
||||
t = FinancialTransaction.new(ordergroup: self, amount: amount, note: note, user: user, financial_link: link)
|
||||
t = FinancialTransaction.new(ordergroup: self, amount: amount, note: note, user: user, financial_transaction_type: transaction_type, financial_link: link)
|
||||
t.save!
|
||||
self.account_balance = financial_transactions.sum('amount')
|
||||
save!
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue