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
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
if @current_user.update_attributes(user_params)
@current_user.ordergroup.update_attributes(ordergroup_params) if ordergroup_params

View file

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

View file

@ -6,6 +6,9 @@
= f.input :name
= f.association :financial_transaction_class, :include_blank => false
= f.input :name_short
- if BankAccount.count > 1
= f.association :bank_account
%small= t '.name_short_desc'
.modal-footer
= link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'}
= 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
%li= link_to t('.profile'), my_profile_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{class: ('disabled' if FoodsoftConfig[:homepage].blank?)}
= link_to FoodsoftConfig[:name], FoodsoftConfig[:homepage]