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::Auth
include Concerns::Locale
include PathHelper
helper_method :current_user
helper_method :available_locales

View file

@ -2,7 +2,7 @@ class Finance::BaseController < ApplicationController
before_action :authenticate_finance
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)
@unpaid_invoices = Invoice.unpaid.includes(:supplier).limit(8)

View file

@ -16,7 +16,7 @@ class Finance::FinancialLinksController < Finance::BaseController
{
date: ft.created_on,
type: t('activerecord.models.financial_transaction'),
description: "#{ft.ordergroup.name}: #{ft.note}",
description: "#{ft.ordergroup_name}: #{ft.note}",
amount: ft.amount,
link_to: finance_ordergroup_transactions_path(ft.ordergroup),
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'
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,
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 :without_financial_link, -> { where(financial_link: nil) }
scope :with_ordergroup, -> { where.not(ordergroup: nil) }
localize_input_of :amount
@ -54,6 +55,10 @@ class FinancialTransaction < ApplicationRecord
reverts.present? || reverted_by.present?
end
def ordergroup_name
ordergroup ? ordergroup.name : I18n.t('model.financial_transaction.foodcoop_name')
end
# @todo rename in model, see #575
def created_at
created_on

View file

@ -14,7 +14,7 @@
- for t in @financial_transactions
%tr
%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
%td= h t.financial_transaction_type.name
%td= number_to_currency t.amount

View file

@ -34,7 +34,7 @@
- else
= format_time(t.created_on)
- 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)
- if FinancialTransactionType.has_multiple_types
%td= h t.financial_transaction_type.name

View file

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