Add reference_calculator

This commit is contained in:
Patrick Gansterer 2019-11-11 12:09:18 +01:00
parent d75b881318
commit e16f03eebf
11 changed files with 133 additions and 1 deletions

View file

@ -14,6 +14,15 @@ class HomeController < ApplicationController
def profile def profile
end end
def reference_calculator
if current_user.ordergroup
@types = FinancialTransactionType.with_name_short.order(:name)
@bank_accounts = @types.includes(:bank_account).map(&:bank_account).uniq.compact
else
redirect_to root_url, alert: I18n.t('group_orders.errors.no_member')
end
end
def update_profile def update_profile
if @current_user.update_attributes(user_params) if @current_user.update_attributes(user_params)
@current_user.ordergroup.update_attributes(ordergroup_params) if ordergroup_params @current_user.ordergroup.update_attributes(ordergroup_params) if ordergroup_params

View file

@ -1,5 +1,6 @@
class FinancialTransactionType < ApplicationRecord class FinancialTransactionType < ApplicationRecord
belongs_to :financial_transaction_class belongs_to :financial_transaction_class
belongs_to :bank_account
has_many :financial_transactions, dependent: :restrict_with_exception has_many :financial_transactions, dependent: :restrict_with_exception
validates :name, presence: true validates :name, presence: true
@ -10,6 +11,8 @@ class FinancialTransactionType < ApplicationRecord
before_destroy :restrict_deleting_last_financial_transaction_type before_destroy :restrict_deleting_last_financial_transaction_type
scope :with_name_short, -> { where.not(name_short: [nil, '']) }
def self.default def self.default
first first
end end

View file

@ -6,6 +6,9 @@
= f.input :name = f.input :name
= f.association :financial_transaction_class, :include_blank => false = f.association :financial_transaction_class, :include_blank => false
= f.input :name_short = f.input :name_short
- if BankAccount.count > 1
= f.association :bank_account
%small= t '.name_short_desc'
.modal-footer .modal-footer
= link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'} = link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'}
= f.submit class: 'btn btn-primary' = f.submit class: 'btn btn-primary'

View file

@ -0,0 +1,76 @@
- title t('.title')
- content_for :javascript do
:javascript
$(function() {
var default_bank_account = $('p[data-text]').attr('data-text');
function update() {
var items = {};
var hide_placeholder = false;
$('input[data-has-name-short]').each(function() {
var bank_account = $(this).attr('data-bank-account') || default_bank_account;
var name = $(this).attr('name');
var value = Math.ceil(parseFloat($(this).val()) * 100) / 100;
if (value > 0) {
var item = items[bank_account];
if (!item) {
items[bank_account] = item = { amount: 0, values: {} };
}
item.amount += value;
item.values[name] = value;
hide_placeholder = true;
}
});
$('p[data-text]').each(function() {
var ele = $(this);
var bank_account = ele.attr('data-text');
var item = items[bank_account];
console.dir(item);
if (item) {
ele.show();
ele.find('.amount').text(item.amount);
ele.find('.reference').text((#{BankTransactionReference.js_code_for_user(@current_user)})(item.values));
} else {
ele.hide();
}
});
if (hide_placeholder) {
$('#placeholder').hide();
} else {
$('#placeholder').show();
}
}
$(document).on('input', 'input[data-has-name-short]', update);
update();
});
= form_tag finance_create_transaction_collection_path do
%p
%table#ordergroups
%thead
%tr
%th= heading_helper FinancialTransactionType, :name
%th= heading_helper FinancialTransaction, :amount
%tbody
- for t in @types
%tr
%td= "#{t.name} (#{t.name_short}):"
%td= text_field_tag t.name_short, nil, class: 'input-small',
'data-has-name-short' => true, 'data-bank-account' => t.bank_account.try(:id),
step: 0.01, type: :number
%p#placeholder
= t('.placeholder')
- for ba in @bank_accounts
%p{'data-text' => ba.id}
= t('.text0')
%b.amount= '?'
= t('.text1')
%b.reference= '?'
= t('.text2')
%b= ba.iban

View file

@ -9,6 +9,8 @@
%ul.dropdown-menu %ul.dropdown-menu
%li= link_to t('.profile'), my_profile_path %li= link_to t('.profile'), my_profile_path
%li= link_to t('.ordergroup'), my_ordergroup_path %li= link_to t('.ordergroup'), my_ordergroup_path
- if BankAccount.any? && FinancialTransactionType.with_name_short.any?
%li= link_to t('.reference_calculator'), home_reference_calculator_path
%li= link_to t('.logout'), logout_path %li= link_to t('.logout'), logout_path
%li{class: ('disabled' if FoodsoftConfig[:homepage].blank?)} %li{class: ('disabled' if FoodsoftConfig[:homepage].blank?)}
= link_to FoodsoftConfig[:name], FoodsoftConfig[:homepage] = link_to FoodsoftConfig[:name], FoodsoftConfig[:homepage]

View file

@ -61,6 +61,7 @@ de:
ordergroup: Bestellgruppe ordergroup: Bestellgruppe
user: Eingetragen von user: Eingetragen von
financial_transaction_type: financial_transaction_type:
bank_account: Bankkonto
financial_transaction_class: Kontotransaktionsklasse financial_transaction_class: Kontotransaktionsklasse
name_short: Kurzname name_short: Kurzname
group_order: group_order:
@ -298,6 +299,7 @@ de:
title_new: Kontotransaktionsklasse anlegen title_new: Kontotransaktionsklasse anlegen
financial_transaction_types: financial_transaction_types:
form: form:
name_short_desc: Der Kurzname ist notwendig, falls der Kontotransaktionstyp in Banktransaktionen automatisch erkannt werden soll. Falls es mehrere Bankkonten gibt, kann das gewünschte Zielkonto für Überweisungen ausgewählt werden.
title_edit: Kontotransaktionstyp bearbeiten title_edit: Kontotransaktionstyp bearbeiten
title_new: Kontotransaktionstyp anlegen title_new: Kontotransaktionstyp anlegen
mail_delivery_status: mail_delivery_status:
@ -1088,6 +1090,12 @@ de:
title: Mein Profil title: Mein Profil
user: user:
since: "(Mitglied seit %{when})" since: "(Mitglied seit %{when})"
reference_calculator:
placeholder: Bitte fülle zuerst die einzelnen Felder mit den gewünschten Beträgen für die Überweisung aus, um die Zahlungsreferenz für diese Transaktion anzuzeigen.
text0: Bitte überweise
text1: mit der Zahlungsreferenz
text2: auf das Bankkonto
title: Zahlungsreferenz-Rechner
start_nav: start_nav:
finances: finances:
accounts: Konten aktualisieren accounts: Konten aktualisieren
@ -1132,6 +1140,7 @@ de:
logout: Abmelden logout: Abmelden
ordergroup: Meine Bestellgruppe ordergroup: Meine Bestellgruppe
profile: Profil bearbeiten profile: Profil bearbeiten
reference_calculator: Zahlungsreferenz-Rechner
lib: lib:
render_pdf: render_pdf:
page: Seite %{number} von %{count} page: Seite %{number} von %{count}

View file

@ -67,6 +67,7 @@ en:
financial_transaction_class: financial_transaction_class:
name: Name name: Name
financial_transaction_type: financial_transaction_type:
bank_account: Bank Account
name: Name name: Name
financial_transaction_class: Financial transaction class financial_transaction_class: Financial transaction class
name_short: Short Name name_short: Short Name
@ -314,6 +315,7 @@ en:
title_new: Add new financial transaction class title_new: Add new financial transaction class
financial_transaction_types: financial_transaction_types:
form: form:
name_short_desc: The short name is mandatory for financial transaction types which should be automatically assignable in bank transactions. If there are multiple bank accounts, the preferred target account for bank transfers can be selected.
title_edit: Edit financial transaction type title_edit: Edit financial transaction type
title_new: Add new financial transaction type title_new: Add new financial transaction type
mail_delivery_status: mail_delivery_status:
@ -1116,6 +1118,12 @@ en:
user: user:
since: "(member for %{when})" since: "(member for %{when})"
title: "%{user}" title: "%{user}"
reference_calculator:
placeholder: Please enter the amounts you want to tranfser in every field first, to see the reference you should use for that transaction.
text0: Please transfer
text1: with the reference
text2: to the bank account
title: Reference Calculator
start_nav: start_nav:
admin: Administration admin: Administration
finances: finances:
@ -1167,6 +1175,7 @@ en:
logout: Logout logout: Logout
ordergroup: My ordergroup ordergroup: My ordergroup
profile: Edit profile profile: Edit profile
reference_calculator: Reference Calculator
logo: "<span>food</span>soft" logo: "<span>food</span>soft"
lib: lib:
render_pdf: render_pdf:

View file

@ -31,6 +31,7 @@ Foodsoft::Application.routes.draw do
########### User specific ########### User specific
get '/home/profile', as: 'my_profile' get '/home/profile', as: 'my_profile'
get '/home/reference_calculator'
patch '/home/update_profile', as: 'update_profile' patch '/home/update_profile', as: 'update_profile'
get '/home/ordergroup' => 'home#ordergroup', as: 'my_ordergroup' get '/home/ordergroup' => 'home#ordergroup', as: 'my_ordergroup'
post '/home/cancel_membership' => 'home#cancel_membership', as: 'cancel_membership' post '/home/cancel_membership' => 'home#cancel_membership', as: 'cancel_membership'

View file

@ -0,0 +1,5 @@
class AddBankAccountToFinancialTransactionType < ActiveRecord::Migration
def change
add_reference :financial_transaction_types, :bank_account
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20181205000000) do ActiveRecord::Schema.define(version: 20181205010000) do
create_table "article_categories", force: :cascade do |t| create_table "article_categories", force: :cascade do |t|
t.string "name", limit: 255, default: "", null: false t.string "name", limit: 255, default: "", null: false
@ -124,6 +124,7 @@ ActiveRecord::Schema.define(version: 20181205000000) do
t.string "name", limit: 255, null: false t.string "name", limit: 255, null: false
t.integer "financial_transaction_class_id", limit: 4, null: false t.integer "financial_transaction_class_id", limit: 4, null: false
t.string "name_short", limit: 255 t.string "name_short", limit: 255
t.integer "bank_account_id", limit: 4
end end
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

View file

@ -17,4 +17,18 @@ class BankTransactionReference
return ret return ret
end end
def self.js_code_for_user(user)
%{
function(items) {
var ret = "FS#{user.ordergroup.id}.#{user.id}";
for (var key in items) {
if (items.hasOwnProperty(key)) {
ret += key + items[key];
}
}
return ret;
}
}
end
end end