Adopt model to allow FinancialTransaction without an ordergroup

This will allow us to add accounting for the foodcoop itself, to support
listing for spendings independent of the order (e.g. rent, electricity).
This commit is contained in:
Patrick Gansterer 2019-11-01 14:07:23 +01:00
parent 1053b3883d
commit 1a49bee42d
11 changed files with 22 additions and 7 deletions

View file

@ -3,6 +3,7 @@ class ApplicationController < ActionController::Base
include Concerns::FoodcoopScope include Concerns::FoodcoopScope
include Concerns::Auth include Concerns::Auth
include Concerns::Locale include Concerns::Locale
include PathHelper
helper_method :current_user helper_method :current_user
helper_method :available_locales helper_method :available_locales

View file

@ -2,7 +2,7 @@ class Finance::BaseController < ApplicationController
before_action :authenticate_finance before_action :authenticate_finance
def index def index
@financial_transactions = FinancialTransaction.includes(:ordergroup).order('created_on DESC').limit(8) @financial_transactions = FinancialTransaction.with_ordergroup.includes(:ordergroup).order(created_on: :desc).limit(8)
@orders = Order.finished_not_closed.includes(:supplier).limit(8) @orders = Order.finished_not_closed.includes(:supplier).limit(8)
@unpaid_invoices = Invoice.unpaid.includes(:supplier).limit(8) @unpaid_invoices = Invoice.unpaid.includes(:supplier).limit(8)

View file

@ -16,7 +16,7 @@ class Finance::FinancialLinksController < Finance::BaseController
{ {
date: ft.created_on, date: ft.created_on,
type: t('activerecord.models.financial_transaction'), type: t('activerecord.models.financial_transaction'),
description: "#{ft.ordergroup.name}: #{ft.note}", description: "#{ft.ordergroup_name}: #{ft.note}",
amount: ft.amount, amount: ft.amount,
link_to: finance_ordergroup_transactions_path(ft.ordergroup), link_to: finance_ordergroup_transactions_path(ft.ordergroup),
remove_path: remove_financial_transaction_finance_link_path(@financial_link, ft) remove_path: remove_financial_transaction_finance_link_path(@financial_link, ft)

View file

@ -9,12 +9,13 @@ class FinancialTransaction < ApplicationRecord
belongs_to :reverts, class_name: 'FinancialTransaction', foreign_key: 'reverts_id' belongs_to :reverts, class_name: 'FinancialTransaction', foreign_key: 'reverts_id'
has_one :reverted_by, class_name: 'FinancialTransaction', foreign_key: 'reverts_id' has_one :reverted_by, class_name: 'FinancialTransaction', foreign_key: 'reverts_id'
validates_presence_of :amount, :note, :user_id, :ordergroup_id validates_presence_of :amount, :note, :user_id
validates_numericality_of :amount, greater_then: -100_000, validates_numericality_of :amount, greater_then: -100_000,
less_than: 100_000 less_than: 100_000
scope :visible, -> { joins('LEFT JOIN financial_transactions r ON financial_transactions.id = r.reverts_id').where('r.id IS NULL').where(reverts: nil) } scope :visible, -> { joins('LEFT JOIN financial_transactions r ON financial_transactions.id = r.reverts_id').where('r.id IS NULL').where(reverts: nil) }
scope :without_financial_link, -> { where(financial_link: nil) } scope :without_financial_link, -> { where(financial_link: nil) }
scope :with_ordergroup, -> { where.not(ordergroup: nil) }
localize_input_of :amount localize_input_of :amount
@ -54,6 +55,10 @@ class FinancialTransaction < ApplicationRecord
reverts.present? || reverted_by.present? reverts.present? || reverted_by.present?
end end
def ordergroup_name
ordergroup ? ordergroup.name : I18n.t('model.financial_transaction.foodcoop_name')
end
# @todo rename in model, see #575 # @todo rename in model, see #575
def created_at def created_at
created_on created_on

View file

@ -14,7 +14,7 @@
- for t in @financial_transactions - for t in @financial_transactions
%tr %tr
%td= link_to format_time(t.created_on), add_financial_transaction_finance_link_path(@financial_link, financial_transaction: t.id), method: :put %td= link_to format_time(t.created_on), add_financial_transaction_finance_link_path(@financial_link, financial_transaction: t.id), method: :put
%td= t.ordergroup.name %td= t.ordergroup_name
- if FinancialTransactionType.has_multiple_types - if FinancialTransactionType.has_multiple_types
%td= h t.financial_transaction_type.name %td= h t.financial_transaction_type.name
%td= number_to_currency t.amount %td= number_to_currency t.amount

View file

@ -34,7 +34,7 @@
- else - else
= format_time(t.created_on) = format_time(t.created_on)
- if with_ordergroup - if with_ordergroup
%td= h link_to t.ordergroup.name, finance_ordergroup_transactions_path(t.ordergroup) %td= h link_to t.ordergroup_name, finance_ordergroup_transactions_path(t.ordergroup)
%td= h show_user(t.user) %td= h show_user(t.user)
- if FinancialTransactionType.has_multiple_types - if FinancialTransactionType.has_multiple_types
%td= h t.financial_transaction_type.name %td= h t.financial_transaction_type.name

View file

@ -34,7 +34,7 @@
- @financial_transactions.each do |ft| - @financial_transactions.each do |ft|
%tr %tr
%td= format_date(ft.created_on) %td= format_date(ft.created_on)
%td= ft.ordergroup.name %td= ft.ordergroup_name
%td= ft.note %td= ft.note
%td.numeric= format_currency ft.amount %td.numeric= format_currency ft.amount
.span6 .span6

View file

@ -1272,6 +1272,8 @@ de:
model: model:
delivery: delivery:
each_stock_article_must_be_unique: Lieferung darf jeden Lagerartikel höchstens einmal auflisten. each_stock_article_must_be_unique: Lieferung darf jeden Lagerartikel höchstens einmal auflisten.
financial_transaction:
foodcoop_name: Foodcoop
financial_transaction_type: financial_transaction_type:
no_delete_last: Es muss mindestens ein Kontotransaktionstyp existieren. no_delete_last: Es muss mindestens ein Kontotransaktionstyp existieren.
group_order: group_order:

View file

@ -1311,6 +1311,8 @@ en:
model: model:
delivery: delivery:
each_stock_article_must_be_unique: Each stock article must not be listed more than once. each_stock_article_must_be_unique: Each stock article must not be listed more than once.
financial_transaction:
foodcoop_name: Foodcoop
financial_transaction_type: financial_transaction_type:
no_delete_last: At least one financial transaction type must exist. no_delete_last: At least one financial transaction type must exist.
group_order: group_order:

View file

@ -0,0 +1,5 @@
class AllowEmtpyOrdergroupInFinancialTransaction < ActiveRecord::Migration
def change
change_column_null :financial_transactions, :ordergroup_id, true
end
end

View file

@ -130,7 +130,7 @@ ActiveRecord::Schema.define(version: 20181205010000) do
add_index "financial_transaction_types", ["name_short"], name: "index_financial_transaction_types_on_name_short", using: :btree add_index "financial_transaction_types", ["name_short"], name: "index_financial_transaction_types_on_name_short", using: :btree
create_table "financial_transactions", force: :cascade do |t| create_table "financial_transactions", force: :cascade do |t|
t.integer "ordergroup_id", limit: 4, default: 0, null: false t.integer "ordergroup_id", limit: 4
t.decimal "amount", precision: 8, scale: 2, default: 0, null: false t.decimal "amount", precision: 8, scale: 2, default: 0, null: false
t.text "note", limit: 65535, null: false t.text "note", limit: 65535, null: false
t.integer "user_id", limit: 4, default: 0, null: false t.integer "user_id", limit: 4, default: 0, null: false