Add BankAccountConnector to implement bank import methods in plugins

This commit is contained in:
Patrick Gansterer 2020-02-24 14:22:58 +01:00
parent d476993321
commit 5d84156bd8
11 changed files with 281 additions and 22 deletions

View file

@ -15,16 +15,36 @@ class Finance::BankAccountsController < Finance::BaseController
def import
@bank_account = BankAccount.find(params[:id])
import_method = @bank_account.find_import_method
if import_method
count = import_method.call(@bank_account)
redirect_to finance_bank_account_transactions_url(@bank_account), notice: t('.notice', count: count)
importer = @bank_account.find_connector
if importer
importer.load params[:state] && YAML.load(params[:state])
ok = importer.import params[:controls]
importer.finish if ok
flash.notice = t('.notice', count: importer.count) if ok
@auto_submit = importer.auto_submit
@controls = importer.controls
#TODO: encrypt state
@state = YAML.dump importer.dump
else
# @todo add import for csv files as fallback
redirect_to finance_bank_account_transactions_url(@bank_account), alert: t('.no_import_method')
ok = true
flash.alert = t('.no_import_method')
end
needs_redirect = ok
rescue => error
redirect_to finance_bank_account_transactions_url(@bank_account), alert: t('errors.general_msg', msg: error.message)
flash.alert = t('errors.general_msg', msg: error.message)
needs_redirect = true
ensure
return unless needs_redirect
redirect_path = finance_bank_account_transactions_url(@bank_account)
if request.post?
@js_redirect = redirect_path
else
redirect_to redirect_path
end
end
end

View file

@ -10,7 +10,9 @@ class BankAccount < ApplicationRecord
validates_numericality_of :balance, :message => I18n.t('bank_account.model.invalid_balance')
# @return [Function] Method wich can be called to import transaction from a bank or nil if unsupported
def find_import_method
def find_connector
klass = BankAccountConnector.find iban
return klass.new self if klass
end
def assign_unlinked_transactions

View file

@ -0,0 +1,33 @@
= form_tag import_finance_bank_account_path(@bank_account), class: 'form-horizontal',
data: { auto_submit: @auto_submit}, id: 'import_form', method: :post, remote: true do
= hidden_field_tag :import_uid, @import_uid
= hidden_field_tag :state, @state
- for control in @controls
- name = control.name
.control-group
- if name
- if control.type == :hidden
= hidden_field_tag "controls[#{control.name}]", control.value
- else
%label(for=name class='control-label')
= control.label + ':'
.controls
- if control.type == :password
= password_field_tag "controls[#{control.name}]", control.value
-else
= text_field_tag "controls[#{control.name}]", control.value
- else
= control.text
- if @auto_submit
:javascript
var form = $('#import_form');
setTimeout(function() {
form.submit();
}, form.data('auto-submit'));
- else
.control-group
.controls
= submit_tag t('.submit'), class: 'btn btn-primary'

View file

@ -1,12 +1,4 @@
- title t('.title', name: @bank_account.name)
= form_for :bank_accounts, :url => parse_upload_finance_bank_account_path(@bank_account),
:html => { multipart: true, class: "form-horizontal" } do |f|
.control-group
%label(for="bank_transactions_file")= t '.file_label'
= f.file_field "file"
.form-actions
= submit_tag t('.submit'), class: 'btn btn-primary'
= link_to t('ui.or_cancel'), finance_bank_account_transactions_path(@bank_account)
#import
= render "import"

View file

@ -0,0 +1,4 @@
- if @js_redirect
document.location.replace('#{escape_javascript(@js_redirect)}');
- else
$('#import').html('#{escape_javascript(render("import"))}');