Adopt model to reference GroupOrder in generated FinancialTransaction

This commit is contained in:
Patrick Gansterer 2019-11-04 04:33:43 +01:00
parent b6542fb711
commit 17059a8104
7 changed files with 17 additions and 5 deletions

View File

@ -5,6 +5,7 @@ class FinancialTransaction < ApplicationRecord
belongs_to :user
belongs_to :financial_link
belongs_to :financial_transaction_type
belongs_to :group_order
belongs_to :reverts, class_name: 'FinancialTransaction', foreign_key: 'reverts_id'
has_one :reverted_by, class_name: 'FinancialTransaction', foreign_key: 'reverts_id'

View File

@ -8,6 +8,7 @@ class GroupOrder < ApplicationRecord
belongs_to :ordergroup
has_many :group_order_articles, :dependent => :destroy
has_many :order_articles, :through => :group_order_articles
has_one :financial_transaction
belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by_user_id"
validates_presence_of :order_id

View File

@ -244,7 +244,7 @@ class Order < ApplicationRecord
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, transaction_type)
group_order.ordergroup.add_financial_transaction!(price, transaction_note, user, transaction_type, nil, group_order)
end
end

View File

@ -80,9 +80,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, transaction_type, link = nil)
def add_financial_transaction!(amount, note, user, transaction_type, link = nil, group_order = nil)
transaction do
t = FinancialTransaction.new(ordergroup: self, amount: amount, note: note, user: user, financial_transaction_type: transaction_type, financial_link: link)
t = FinancialTransaction.new(ordergroup: self, amount: amount, note: note, user: user, financial_transaction_type: transaction_type, financial_link: link, group_order: group_order)
t.save!
update_balance!
# Notify only when order group had a positive balance before the last transaction:

View File

@ -38,7 +38,11 @@
%td= h show_user(t.user)
- if FinancialTransactionType.has_multiple_types
%td= h t.financial_transaction_type.name
%td= h t.note
%td
- if t.group_order
= link_to t.note, t.group_order
- else
= t.note
- FinancialTransactionClass.sorted.each do |c|
%td.numeric{style: 'width:5em'}
- if t.financial_transaction_type.financial_transaction_class == c

View File

@ -0,0 +1,5 @@
class AddGroupOrderToFinancialTransaction < ActiveRecord::Migration
def change
add_reference :financial_transactions, :group_order
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20181201000200) do
ActiveRecord::Schema.define(version: 20181201000210) do
create_table "article_categories", force: :cascade do |t|
t.string "name", limit: 255, default: "", null: false
@ -137,6 +137,7 @@ ActiveRecord::Schema.define(version: 20181201000200) do
t.integer "financial_transaction_type_id", limit: 4, null: false
t.integer "financial_link_id", limit: 4
t.integer "reverts_id", limit: 4
t.integer "group_order_id"
end
add_index "financial_transactions", ["ordergroup_id"], name: "index_financial_transactions_on_ordergroup_id", using: :btree