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:
Patrick Gansterer 2017-03-04 14:15:18 +01:00
parent dc94e98138
commit e7657b987f
21 changed files with 156 additions and 34 deletions

View file

@ -68,7 +68,8 @@ class Finance::BalancingController < Finance::BaseController
# Balances the Order, Update of the Ordergroup.account_balances
def close
@order = Order.find(params[:id])
@order.close!(@current_user)
@type = FinancialTransactionType.find_by_id(params.permit(:type))
@order.close!(@current_user, @type)
redirect_to finance_order_index_url, notice: t('finance.balancing.close.notice')
rescue => error

View file

@ -55,10 +55,11 @@ class Finance::FinancialTransactionsController < ApplicationController
def create_collection
raise I18n.t('finance.financial_transactions.controller.create_collection.error_note_required') if params[:note].blank?
type = FinancialTransactionType.find_by_id(params.permit(:type))
params[:financial_transactions].each do |trans|
# ignore empty amount fields ...
unless trans[:amount].blank?
Ordergroup.find(trans[:ordergroup_id]).add_financial_transaction!(trans[:amount], params[:note], @current_user)
Ordergroup.find(trans[:ordergroup_id]).add_financial_transaction!(trans[:amount], params[:note], @current_user, type)
end
end
redirect_to finance_ordergroups_url, notice: I18n.t('finance.financial_transactions.controller.create_collection.notice')

View file

@ -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

View 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

View 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

View file

@ -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

View file

@ -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!

View file

@ -1,10 +1,14 @@
-title t('.title')
%p!= t('.first_paragraph')
%table.table.table-striped{:style => "width:35em"}
- for group_order in @order.group_orders
%tr{:class => cycle('even', 'odd')}
%td= group_order.ordergroup_name
%td.numeric= number_to_currency(group_order.price)
.form-actions
= link_to t('.clear'), close_finance_order_path(@order), method: :patch, class: 'btn btn-primary'
= link_to t('.or_cancel'), new_finance_order_path(order_id: @order.id)
= form_tag close_finance_order_path(@order) do
%p!= t('.first_paragraph')
- if FinancialTransactionType.has_multiple_types
%p
%b= heading_helper FinancialTransaction, :financial_transaction_type
= select_tag :type, options_for_select(FinancialTransactionType.order(:name).map { |t| [ t.name, t.id ] })
%table.table.table-striped{:style => "width:35em"}
- for group_order in @order.group_orders
%tr{:class => cycle('even', 'odd')}
%td= group_order.ordergroup_name
%td.numeric= number_to_currency(group_order.price)
.form-actions
= submit_tag t('.clear'), class: 'btn btn-primary'
= link_to t('.or_cancel'), new_finance_order_path(order_id: @order.id)

View file

@ -5,6 +5,8 @@
= simple_form_for @financial_transaction, :url => finance_ordergroup_transactions_path(@ordergroup),
:validate => true do |f|
= f.hidden_field :ordergroup_id
- if FinancialTransactionType.has_multiple_types
= f.association :financial_transaction_type, :as => :radio_buttons
= f.input :amount
= f.input :note, :as => :text
.form-actions

View file

@ -34,6 +34,10 @@
.well.well-small= t('.sidebar')
= form_tag finance_create_transaction_collection_path do
- if FinancialTransactionType.has_multiple_types
%p
%b= heading_helper FinancialTransaction, :financial_transaction_type
= select_tag :type, options_for_select(FinancialTransactionType.order(:name).map { |t| [ t.name, t.id ] })
%p
%b= heading_helper FinancialTransaction, :note
= text_field_tag :note, params[:note], class: 'input-xlarge', required: 'required'