Add option to create a new FinancialTransaction at a FinancialLink

This commit is contained in:
Patrick Gansterer 2021-02-08 02:47:05 +01:00
parent 0e77a0e77c
commit a30d0d4453
8 changed files with 66 additions and 1 deletions

View file

@ -64,6 +64,20 @@ class Finance::FinancialLinksController < Finance::BaseController
redirect_to finance_link_url(@financial_link), notice: t('.notice') redirect_to finance_link_url(@financial_link), notice: t('.notice')
end end
def new_financial_transaction
@financial_transaction = FinancialTransaction.new(financial_link: @financial_link)
@financial_transaction.amount = @financial_link.amount
@financial_transaction.ordergroup_id = find_best_fitting_ordergroup_id_for_financial_link(@financial_link.id)
end
def create_financial_transaction
financial_transaction = FinancialTransaction.new(financial_transaction_params)
financial_transaction.ordergroup.add_financial_transaction! financial_transaction.amount, financial_transaction.note, current_user, financial_transaction.financial_transaction_type, @financial_link
redirect_to finance_link_url(@financial_link), notice: t('.notice')
rescue => error
redirect_to finance_link_url(@financial_link), alert: t('errors.general_msg', msg: error)
end
def index_financial_transaction def index_financial_transaction
@financial_transactions = FinancialTransaction.without_financial_link.includes(:financial_transaction_type, :ordergroup) @financial_transactions = FinancialTransaction.without_financial_link.includes(:financial_transaction_type, :ordergroup)
end end
@ -102,4 +116,17 @@ protected
@financial_link = FinancialLink.find(params[:id]) @financial_link = FinancialLink.find(params[:id])
end end
private
def financial_transaction_params
params.require(:financial_transaction).permit(:financial_transaction_type_id, :ordergroup_id, :amount, :note)
end
def find_best_fitting_ordergroup_id_for_financial_link(financial_link_id)
FinancialTransaction.joins(<<-SQL).order(created_on: :desc).pluck(:ordergroup_id).first
JOIN bank_transactions a ON financial_transactions.financial_link_id = a.financial_link_id
JOIN bank_transactions b ON a.iban = b.iban AND b.financial_link_id = #{financial_link_id.to_i}
SQL
end
end end

View file

@ -37,4 +37,8 @@ class FinancialLink < ApplicationRecord
def self.first_unused_or_create def self.first_unused_or_create
unused.first || create unused.first || create
end end
def amount
bank_transactions.sum(:amount) + invoices.sum(:amount) - financial_transactions.sum(:amount)
end
end end

View file

@ -0,0 +1,14 @@
= simple_form_for @financial_transaction, url: create_financial_transaction_finance_link_path(@financial_link),
:validate => true do |f|
.modal-header
= close_button :modal
%h3= t('.title')
.modal-body
- if FinancialTransactionType.has_multiple_types
= f.association :financial_transaction_type, include_blank: false
= f.input :amount
= f.association :ordergroup, collection: Ordergroup.undeleted.order(:name)
= f.input :note, :as => :text
.modal-footer
= link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'}
= f.submit class: 'btn btn-primary'

View file

@ -0,0 +1,2 @@
$('#modalContainer').html('#{j(render("new_financial_transaction"))}');
$('#modalContainer').modal();

View file

@ -2,7 +2,12 @@
- content_for :actionbar do - content_for :actionbar do
= link_to t('.add_bank_transaction'), index_bank_transaction_finance_link_path(@financial_link), remote: true, class: 'btn' = link_to t('.add_bank_transaction'), index_bank_transaction_finance_link_path(@financial_link), remote: true, class: 'btn'
.btn-group
= link_to t('.add_financial_transaction'), index_financial_transaction_finance_link_path(@financial_link), remote: true, class: 'btn' = link_to t('.add_financial_transaction'), index_financial_transaction_finance_link_path(@financial_link), remote: true, class: 'btn'
= link_to '#', data: {toggle: 'dropdown'}, class: 'btn dropdown-toggle' do
%span.caret
%ul.dropdown-menu
%li= link_to t('.new_financial_transaction'), new_financial_transaction_finance_link_path(@financial_link), remote: true
= link_to t('.add_invoice'), index_invoice_finance_link_path(@financial_link), remote: true, class: 'btn' = link_to t('.add_invoice'), index_invoice_finance_link_path(@financial_link), remote: true, class: 'btn'
%table.table.table-striped %table.table.table-striped

View file

@ -866,12 +866,16 @@ de:
notice: Verlinkung wurde zu der Rechnung wurde hinzugefügt. notice: Verlinkung wurde zu der Rechnung wurde hinzugefügt.
create: create:
notice: Ein neuer Finanzlink wurde erstellt. notice: Ein neuer Finanzlink wurde erstellt.
create_financial_transaction:
notice: Neue Kontotransaktion wurde hinzugefügt.
index_bank_transaction: index_bank_transaction:
title: Banktransaktion hinzufügen title: Banktransaktion hinzufügen
index_financial_transaction: index_financial_transaction:
title: Kontotransaktion hinzufügen title: Kontotransaktion hinzufügen
index_invoice: index_invoice:
title: Rechnung hinzufügen title: Rechnung hinzufügen
new_financial_transaction:
title: Neue Kontotransaktion hinzufügen
remove_bank_transaction: remove_bank_transaction:
notice: Verlinkung wurde zu der Banktransaktion wurde entfernt. notice: Verlinkung wurde zu der Banktransaktion wurde entfernt.
remove_financial_transaction: remove_financial_transaction:
@ -885,6 +889,7 @@ de:
amount: Betrag amount: Betrag
date: Datum date: Datum
description: Beschreibung description: Beschreibung
new_financial_transaction: Neu Kontotransaktion hinzufügen
title: Finanzlink %{number} title: Finanzlink %{number}
type: Typ type: Typ
financial_transactions: financial_transactions:

View file

@ -867,12 +867,16 @@ en:
notice: Link to the invoice has been added. notice: Link to the invoice has been added.
create: create:
notice: A new financial link has been created. notice: A new financial link has been created.
create_financial_transaction:
notice: Financial transaction has been added.
index_bank_transaction: index_bank_transaction:
title: Add bank transaction title: Add bank transaction
index_financial_transaction: index_financial_transaction:
title: Add financial transaction title: Add financial transaction
index_invoice: index_invoice:
title: Add invoice title: Add invoice
new_financial_transaction:
title: Add financial transaction
remove_bank_transaction: remove_bank_transaction:
notice: Link to the bank transaction has been removed. notice: Link to the bank transaction has been removed.
remove_financial_transaction: remove_financial_transaction:
@ -886,6 +890,7 @@ en:
amount: Amount amount: Amount
date: Date date: Date
description: Description description: Description
new_financial_transaction: New financial transaction
title: Financial link %{number} title: Financial link %{number}
type: Type type: Type
financial_transactions: financial_transactions:

View file

@ -190,6 +190,9 @@ Rails.application.routes.draw do
get :index_invoice get :index_invoice
put 'invoices/:invoice', action: 'add_invoice', as: 'add_invoice' put 'invoices/:invoice', action: 'add_invoice', as: 'add_invoice'
delete 'invoices/:invoice', action: 'remove_invoice', as: 'remove_invoice' delete 'invoices/:invoice', action: 'remove_invoice', as: 'remove_invoice'
get :new_financial_transaction
post :create_financial_transaction
end end
end end