Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove bank statement (TH6) import functionality #1674

Merged
merged 7 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions app/controllers/admin/bank_statements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: %i[show bind_invoices]

def index
@q = BankStatement.search(params[:q])
Expand Down Expand Up @@ -43,22 +43,6 @@ def create
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)

Expand All @@ -69,19 +53,14 @@ def bind_invoices
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
@bank_statement = BankStatement.find(params[:id])
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
Expand Down
61 changes: 4 additions & 57 deletions app/models/bank_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,17 @@ 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
FULLY_BINDED = 'fully_binded'.freeze
PARTIALLY_BINDED = 'partially_binded'.freeze
NOT_BINDED = 'not_binded'.freeze

# TODO: Cache this to database so it can be used for searching
def status
if bank_transactions.unbinded.count == bank_transactions.count
NOT_BINDED
elsif bank_transactions.unbinded.count == 0
elsif bank_transactions.unbinded.count.zero?
FULLY_BINDED
else
PARTIALLY_BINDED
Expand Down
20 changes: 0 additions & 20 deletions app/views/admin/bank_statements/import.haml

This file was deleted.

1 change: 0 additions & 1 deletion app/views/admin/bank_statements/index.haml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 0 additions & 4 deletions app/views/admin/bank_statements/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion config/application.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ smtp_authentication: 'plain' # 'plain', 'login', 'cram_md5'
#
app_name: '.EE Registry'
zonefile_export_dir: 'export/zonefiles'
bank_statement_import_dir: 'import/bank_statements'
legal_documents_dir: 'import/legal_documents'
time_zone: 'Tallinn' # more zones by rake time:zones:all

Expand Down
2 changes: 0 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ en:
paid_at: 'Paid at'
invoice: 'Invoice'
bank_statements: 'Bank statements'
import_th6_bank_statement: 'Import TH6 bank statement'
back_to_bank_statements: 'Back to bank statements'
back_to_bank_statement: 'Back to bank statement'
back_to_billing: 'Back to billing'
Expand Down Expand Up @@ -583,7 +582,6 @@ en:
valid: Valid
object_is_not_eligible_for_renewal: 'Object is not eligible for renewal'
object_is_not_holded: 'Object is not holded'
bank_statement_desc: 'Import file row will match only when matching following attributes: <b><br>ref number<br>payment amount<br>invoice number (the first numerical value in comment field)</b>.'
create_bank_statement: 'Create bank statement'
create_bank_transaction: 'Create bank transaction'
create_new_invoice: 'Create new invoice'
Expand Down
6 changes: 0 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,7 @@

resources :bank_statements do
resources :bank_transactions
collection do
get 'import'
post 'create_from_import'
end

post 'bind_invoices', on: :member
get 'download_import_file', on: :member
end

resources :bank_transactions do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RemoveImportFilePathFromBankStatements < ActiveRecord::Migration[6.0]
def up
remove_column :bank_statements, :import_file_path
end

def down
add_column :bank_statements, :import_file_path, :string
end
end
6 changes: 4 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ CREATE TABLE public.bank_statements (
id integer NOT NULL,
bank_code character varying,
iban character varying,
import_file_path character varying,
queried_at timestamp without time zone,
created_at timestamp without time zone,
updated_at timestamp without time zone,
Expand Down Expand Up @@ -4850,4 +4849,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20200807110611'),
('20200811074839'),
('20200812090409'),
('20200812125810');
('20200812125810'),
('20200908131554');


49 changes: 43 additions & 6 deletions test/system/admin_area/bank_statement_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,48 @@ class AdminAreaBankStatementTest < ApplicationSystemTestCase
travel_to Time.zone.parse('2010-07-05 00:30:00')
end

def test_import_statement
assert_difference 'BankStatement.count', 1 do
visit import_admin_bank_statements_path
attach_file 'Th6 file', Rails.root.join('test', 'fixtures', 'files', 'bank_statement_test.txt').to_s
click_link_or_button 'Save'
end
def test_can_create_statement_manually
create_bank_statement
assert_text 'Record created'
end

def test_can_add_transaction_to_statement_manually
create_bank_statement
click_link_or_button 'Add'
assert_text 'Create bank transaction'

fill_in 'Description', with: 'Invoice with id 123'
fill_in 'Reference number', with: '1232'
fill_in 'Sum', with: '500'
fill_in 'Paid at', with: Time.zone.today.to_s
click_link_or_button 'Save'
assert_text 'Record created'
end

def test_can_bind_statement_transactions
registrar = registrars(:bestnames)
registrar.issue_prepayment_invoice(amount: 500)
invoice = registrar.invoices.last

create_bank_statement
click_link_or_button 'Add'
assert_text 'Create bank transaction'

fill_in 'Description', with: "Invoice with id #{invoice.number}"
fill_in 'Reference number', with: invoice.reference_no
fill_in 'Sum', with: invoice.total
fill_in 'Paid at', with: Time.zone.today.to_s
click_link_or_button 'Save'

click_link_or_button 'Back to bank statement'
click_link_or_button 'Bind invoices'

assert_text 'Invoices were fully binded'
end

def create_bank_statement
visit admin_bank_statements_path
click_link_or_button 'Add'
click_link_or_button 'Save'
end
end