From a30d0d445374942e4a7589253bfa04597e1f2a56 Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Mon, 8 Feb 2021 02:47:05 +0100 Subject: [PATCH] Add option to create a new FinancialTransaction at a FinancialLink --- .../finance/financial_links_controller.rb | 27 +++++++++++++++++++ app/models/financial_link.rb | 4 +++ .../_new_financial_transaction.html.haml | 14 ++++++++++ .../new_financial_transaction.js.haml | 2 ++ .../finance/financial_links/show.html.haml | 7 ++++- config/locales/de.yml | 5 ++++ config/locales/en.yml | 5 ++++ config/routes.rb | 3 +++ 8 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 app/views/finance/financial_links/_new_financial_transaction.html.haml create mode 100644 app/views/finance/financial_links/new_financial_transaction.js.haml diff --git a/app/controllers/finance/financial_links_controller.rb b/app/controllers/finance/financial_links_controller.rb index cdb9b556..0392a800 100644 --- a/app/controllers/finance/financial_links_controller.rb +++ b/app/controllers/finance/financial_links_controller.rb @@ -64,6 +64,20 @@ class Finance::FinancialLinksController < Finance::BaseController redirect_to finance_link_url(@financial_link), notice: t('.notice') 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 @financial_transactions = FinancialTransaction.without_financial_link.includes(:financial_transaction_type, :ordergroup) end @@ -102,4 +116,17 @@ protected @financial_link = FinancialLink.find(params[:id]) 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 diff --git a/app/models/financial_link.rb b/app/models/financial_link.rb index 9a6d9714..1ad5214e 100644 --- a/app/models/financial_link.rb +++ b/app/models/financial_link.rb @@ -37,4 +37,8 @@ class FinancialLink < ApplicationRecord def self.first_unused_or_create unused.first || create end + + def amount + bank_transactions.sum(:amount) + invoices.sum(:amount) - financial_transactions.sum(:amount) + end end diff --git a/app/views/finance/financial_links/_new_financial_transaction.html.haml b/app/views/finance/financial_links/_new_financial_transaction.html.haml new file mode 100644 index 00000000..512bc5f7 --- /dev/null +++ b/app/views/finance/financial_links/_new_financial_transaction.html.haml @@ -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' diff --git a/app/views/finance/financial_links/new_financial_transaction.js.haml b/app/views/finance/financial_links/new_financial_transaction.js.haml new file mode 100644 index 00000000..830bcec9 --- /dev/null +++ b/app/views/finance/financial_links/new_financial_transaction.js.haml @@ -0,0 +1,2 @@ +$('#modalContainer').html('#{j(render("new_financial_transaction"))}'); +$('#modalContainer').modal(); diff --git a/app/views/finance/financial_links/show.html.haml b/app/views/finance/financial_links/show.html.haml index 88e186bd..e8a854bd 100644 --- a/app/views/finance/financial_links/show.html.haml +++ b/app/views/finance/financial_links/show.html.haml @@ -2,7 +2,12 @@ - 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_financial_transaction'), index_financial_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 '#', 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' %table.table.table-striped diff --git a/config/locales/de.yml b/config/locales/de.yml index 864de190..8ba97ac7 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -866,12 +866,16 @@ de: notice: Verlinkung wurde zu der Rechnung wurde hinzugefügt. create: notice: Ein neuer Finanzlink wurde erstellt. + create_financial_transaction: + notice: Neue Kontotransaktion wurde hinzugefügt. index_bank_transaction: title: Banktransaktion hinzufügen index_financial_transaction: title: Kontotransaktion hinzufügen index_invoice: title: Rechnung hinzufügen + new_financial_transaction: + title: Neue Kontotransaktion hinzufügen remove_bank_transaction: notice: Verlinkung wurde zu der Banktransaktion wurde entfernt. remove_financial_transaction: @@ -885,6 +889,7 @@ de: amount: Betrag date: Datum description: Beschreibung + new_financial_transaction: Neu Kontotransaktion hinzufügen title: Finanzlink %{number} type: Typ financial_transactions: diff --git a/config/locales/en.yml b/config/locales/en.yml index c82409e0..57ec4d3e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -867,12 +867,16 @@ en: notice: Link to the invoice has been added. create: notice: A new financial link has been created. + create_financial_transaction: + notice: Financial transaction has been added. index_bank_transaction: title: Add bank transaction index_financial_transaction: title: Add financial transaction index_invoice: title: Add invoice + new_financial_transaction: + title: Add financial transaction remove_bank_transaction: notice: Link to the bank transaction has been removed. remove_financial_transaction: @@ -886,6 +890,7 @@ en: amount: Amount date: Date description: Description + new_financial_transaction: New financial transaction title: Financial link %{number} type: Type financial_transactions: diff --git a/config/routes.rb b/config/routes.rb index ab475e42..f5d6505d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -190,6 +190,9 @@ Rails.application.routes.draw do get :index_invoice put 'invoices/:invoice', action: 'add_invoice', as: 'add_invoice' delete 'invoices/:invoice', action: 'remove_invoice', as: 'remove_invoice' + + get :new_financial_transaction + post :create_financial_transaction end end