Add FinancialLinksController#incomplete

This commit is contained in:
Patrick Gansterer 2019-11-19 12:51:08 +01:00
parent 057f3ec8ea
commit 645c142bf6
4 changed files with 47 additions and 1 deletions

View file

@ -1,5 +1,5 @@
class Finance::FinancialLinksController < Finance::BaseController
before_action :find_financial_link, except: [:create]
before_action :find_financial_link, except: [:create, :incomplete]
def show
@items = @financial_link.bank_transactions.map do |bt|
@ -44,6 +44,10 @@ class Finance::FinancialLinksController < Finance::BaseController
redirect_to finance_link_url(@financial_link), notice: t('.notice')
end
def incomplete
@financial_links = FinancialLink.incomplete
end
def index_bank_transaction
@bank_transactions = BankTransaction.without_financial_link
end

View file

@ -2,4 +2,29 @@ class FinancialLink < ApplicationRecord
has_many :bank_transactions
has_many :financial_transactions
has_many :invoices
scope :incomplete, -> { with_full_sum.where.not('full_sums.full_sum' => 0) }
scope :with_full_sum, -> {
select(:id, :note, :full_sum).joins(<<-SQL)
LEFT JOIN (
SELECT id, COALESCE(bt_sum, 0) - COALESCE(ft_sum, 0) + COALESCE(i_sum, 0) AS full_sum
FROM financial_links fl
LEFT JOIN (
SELECT financial_link_id, SUM(amount) AS bt_sum
FROM bank_transactions
GROUP BY financial_link_id
) bt ON bt.financial_link_id = fl.id
LEFT JOIN (
SELECT financial_link_id, SUM(amount) AS ft_sum
FROM financial_transactions
GROUP BY financial_link_id
) ft ON ft.financial_link_id = fl.id
LEFT JOIN (
SELECT financial_link_id, SUM(amount) AS i_sum
FROM invoices
GROUP BY financial_link_id
) i ON i.financial_link_id = fl.id
) full_sums ON full_sums.id = financial_links.id
SQL
}
end

View file

@ -0,0 +1,14 @@
- title t('.title')
%table.table.table-striped
%thead
%tr
%th= heading_helper FinancialLink, :id
%th= heading_helper FinancialLink, :note
%th= heading_helper FinancialTransaction, :amount
%tbody
- for fl in @financial_links
%tr
%td= link_to fl.id, finance_link_path(fl)
%td= fl.note
%td= format_currency fl.full_sum

View file

@ -173,6 +173,9 @@ Foodsoft::Application.routes.draw do
end
resources :links, controller: 'financial_links', only: [:create, :show] do
collection do
get :incomplete
end
member do
get :index_bank_transaction
put 'bank_transactions/:bank_transaction', action: 'add_bank_transaction', as: 'add_bank_transaction'