Remove bank statement import functionality

This commit is contained in:
Karl Erik Õunapuu 2020-09-07 12:55:42 +03:00
parent bfa60a9c17
commit aa2d9547a4
No known key found for this signature in database
GPG key ID: C9DD647298A34764
8 changed files with 2 additions and 125 deletions

View file

@ -2,7 +2,7 @@ module Admin
class BankStatementsController < BaseController
load_and_authorize_resource
before_action :set_bank_statement, only: [:show, :download_import_file, :bind_invoices]
before_action :set_bank_statement, only: [:show, :bind_invoices]
def index
@q = BankStatement.search(params[:q])
@ -43,22 +43,6 @@ module Admin
end
end
def import
@bank_statement = BankStatement.new
end
def create_from_import
@bank_statement = BankStatement.new(bank_statement_params)
if @bank_statement.import
flash[:notice] = I18n.t('record_created')
redirect_to [:admin, @bank_statement]
else
flash.now[:alert] = I18n.t('failed_to_create_record')
render 'new'
end
end
def bind_invoices
@bank_statement.bind_invoices(manual: true)
@ -69,11 +53,6 @@ module Admin
redirect_to [:admin, @bank_statement]
end
def download_import_file
filename = @bank_statement.import_file_path.split('/').last
send_data File.open(@bank_statement.import_file_path, 'r').read, filename: filename
end
private
def set_bank_statement
@ -81,7 +60,7 @@ module Admin
end
def bank_statement_params
params.require(:bank_statement).permit(:th6_file, :bank_code, :iban, bank_transactions_attributes: [
params.require(:bank_statement).permit(:bank_code, :iban, bank_transactions_attributes: [
:description, :sum, :currency, :reference_no, :paid_at
])
end

View file

@ -4,65 +4,12 @@ class BankStatement < ApplicationRecord
accepts_nested_attributes_for :bank_transactions
attr_accessor :th6_file
validates :bank_code, :iban, presence: true
FULLY_BINDED = 'fully_binded'
PARTIALLY_BINDED = 'partially_binded'
NOT_BINDED = 'not_binded'
def import
import_th6_file && save
end
def import_th6_file
return false unless th6_file
th6_file.open.each_line do |row|
bt_params = parse_th6_row(row)
next unless bt_params
bank_transactions.build(bt_params)
end
prepare_dir
self.import_file_path = "#{ENV['bank_statement_import_dir']}/#{Time.zone.now.to_formatted_s(:number)}.txt"
File.open(import_file_path, 'w') { |f| f.write(th6_file.open.read) }
end
def prepare_dir
dirname = ENV['bank_statement_import_dir']
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
end
def parse_th6_row(row)
return parse_th6_header(row) if row[4, 3].strip == '000'
return if row[4, 3].strip == '999' # skip footer
return unless row[4, 1].strip == '1' # import only transactions
return unless row[266, 2].strip == 'C' # import only Credit transactions
{
paid_at: DateTime.strptime(row[5, 8].strip, '%Y%m%d'),
bank_reference: row[5, 16].strip,
iban: row[25, 20].strip,
currency: row[45, 3].strip,
buyer_bank_code: row[48, 3].strip,
buyer_iban: row[51, 32].strip,
buyer_name: row[83, 35].strip,
document_no: row[118, 8].strip,
description: row[126, 140].strip,
sum: BigDecimal(row[268, 12].strip) / BigDecimal('100.0'),
reference_no: row[280, 35].strip
}
end
def parse_th6_header(row)
self.bank_code = row[7, 3].strip
self.iban = row[10, 20].strip
self.queried_at = DateTime.strptime(row[30, 10].strip, '%y%m%d%H%M')
nil
end
# TODO: Cache this to database so it can be used for searching
def status
if bank_transactions.unbinded.count == bank_transactions.count

View file

@ -1,20 +0,0 @@
- content_for :actions do
= link_to(t(:back_to_bank_statements), admin_bank_statements_path, class: 'btn btn-default')
= render 'shared/title', name: t(:import_th6_bank_statement)
= form_for(@bank_statement, url: { action: :create_from_import }, multipart: true) do |f|
= render 'shared/full_errors', object: @bank_statement
.row
.col-md-8
.form-group
.col-md-4.control-label
= f.label :th6_file
.col-md-8
= f.file_field :th6_file
.col-md-4
%p= t(:bank_statement_desc).html_safe
%hr
.row
.col-md-8.text-right
= button_tag(t(:save), class: 'btn btn-primary')

View file

@ -1,6 +1,5 @@
- content_for :actions do
= link_to(t(:add), new_admin_bank_statement_path, class: 'btn btn-primary')
= link_to(t('.import_btn'), import_admin_bank_statements_path, class: 'btn btn-primary')
= render 'shared/title', name: t(:bank_statements)
.row

View file

@ -30,10 +30,6 @@
%dt= t(:created_at)
%dd= l(@bank_statement.created_at)
- if @bank_statement.import_file_path
%dt= t(:import_file)
%dd= link_to(t(:download), download_import_file_admin_bank_statement_path(@bank_statement))
.row
.col-sm-6
%h3.text-center-xs