From aa2d9547a4e1cbb47e5ab3759abc975312cc8011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 7 Sep 2020 12:55:42 +0300 Subject: [PATCH 1/6] Remove bank statement import functionality --- .../admin/bank_statements_controller.rb | 25 +-------- app/models/bank_statement.rb | 53 ------------------- app/views/admin/bank_statements/import.haml | 20 ------- app/views/admin/bank_statements/index.haml | 1 - app/views/admin/bank_statements/show.haml | 4 -- config/locales/en.yml | 2 - config/routes.rb | 6 --- test/system/admin_area/bank_statement_test.rb | 16 ------ 8 files changed, 2 insertions(+), 125 deletions(-) delete mode 100644 app/views/admin/bank_statements/import.haml delete mode 100644 test/system/admin_area/bank_statement_test.rb diff --git a/app/controllers/admin/bank_statements_controller.rb b/app/controllers/admin/bank_statements_controller.rb index 1e3b31bf5..d507f359c 100644 --- a/app/controllers/admin/bank_statements_controller.rb +++ b/app/controllers/admin/bank_statements_controller.rb @@ -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 diff --git a/app/models/bank_statement.rb b/app/models/bank_statement.rb index c73e6bb44..763506989 100644 --- a/app/models/bank_statement.rb +++ b/app/models/bank_statement.rb @@ -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 diff --git a/app/views/admin/bank_statements/import.haml b/app/views/admin/bank_statements/import.haml deleted file mode 100644 index e432abdf1..000000000 --- a/app/views/admin/bank_statements/import.haml +++ /dev/null @@ -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') diff --git a/app/views/admin/bank_statements/index.haml b/app/views/admin/bank_statements/index.haml index 2c604c035..f04557b0b 100644 --- a/app/views/admin/bank_statements/index.haml +++ b/app/views/admin/bank_statements/index.haml @@ -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 diff --git a/app/views/admin/bank_statements/show.haml b/app/views/admin/bank_statements/show.haml index 9797c59bf..ff139012b 100644 --- a/app/views/admin/bank_statements/show.haml +++ b/app/views/admin/bank_statements/show.haml @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 27299072e..c9faca1eb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -467,7 +467,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' @@ -584,7 +583,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:
ref number
payment amount
invoice number (the first numerical value in comment field)
.' create_bank_statement: 'Create bank statement' create_bank_transaction: 'Create bank transaction' create_new_invoice: 'Create new invoice' diff --git a/config/routes.rb b/config/routes.rb index 2f341866b..223cf3171 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -213,13 +213,7 @@ Rails.application.routes.draw do 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 diff --git a/test/system/admin_area/bank_statement_test.rb b/test/system/admin_area/bank_statement_test.rb deleted file mode 100644 index 53cbcc177..000000000 --- a/test/system/admin_area/bank_statement_test.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'application_system_test_case' - -class AdminAreaBankStatementTest < ApplicationSystemTestCase - setup do - sign_in users(:admin) - 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 - end -end From b981c616715d6f4d248a36e038830fa8479823e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 7 Sep 2020 13:02:41 +0300 Subject: [PATCH 2/6] Fix CC issues --- app/controllers/admin/bank_statements_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/admin/bank_statements_controller.rb b/app/controllers/admin/bank_statements_controller.rb index d507f359c..4295c155f 100644 --- a/app/controllers/admin/bank_statements_controller.rb +++ b/app/controllers/admin/bank_statements_controller.rb @@ -2,7 +2,7 @@ module Admin class BankStatementsController < BaseController load_and_authorize_resource - before_action :set_bank_statement, only: [:show, :bind_invoices] + before_action :set_bank_statement, only: %i[show bind_invoices] def index @q = BankStatement.search(params[:q]) From b5668ea49485066d2452d5021c2afb32da4f5278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 7 Sep 2020 13:11:40 +0300 Subject: [PATCH 3/6] Fix some CC issues --- app/models/bank_statement.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/bank_statement.rb b/app/models/bank_statement.rb index 763506989..d57f3e4d7 100644 --- a/app/models/bank_statement.rb +++ b/app/models/bank_statement.rb @@ -6,15 +6,15 @@ class BankStatement < ApplicationRecord validates :bank_code, :iban, presence: true - FULLY_BINDED = 'fully_binded' - PARTIALLY_BINDED = 'partially_binded' - NOT_BINDED = 'not_binded' + 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 From 2d1402f6bffa1548896a5b74a26fa4950bae7924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 7 Sep 2020 13:21:32 +0300 Subject: [PATCH 4/6] Increase test coverage for bank statements --- test/system/admin_area/bank_statement_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 test/system/admin_area/bank_statement_test.rb diff --git a/test/system/admin_area/bank_statement_test.rb b/test/system/admin_area/bank_statement_test.rb new file mode 100644 index 000000000..b4a482f3b --- /dev/null +++ b/test/system/admin_area/bank_statement_test.rb @@ -0,0 +1,15 @@ +require 'application_system_test_case' + +class AdminAreaBankStatementTest < ApplicationSystemTestCase + setup do + sign_in users(:admin) + travel_to Time.zone.parse('2010-07-05 00:30:00') + end + + def test_can_create_statement_manually + visit admin_bank_statements_path + click_link_or_button 'Add' + click_link_or_button 'Save' + assert_text 'Record created' + end +end From fbef572dfc646a0dcd6cdeb41c3c724e4e187848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 7 Sep 2020 15:10:13 +0300 Subject: [PATCH 5/6] Include tests for admin area statement autobinding --- test/system/admin_area/bank_statement_test.rb | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/test/system/admin_area/bank_statement_test.rb b/test/system/admin_area/bank_statement_test.rb index b4a482f3b..6de21b1c3 100644 --- a/test/system/admin_area/bank_statement_test.rb +++ b/test/system/admin_area/bank_statement_test.rb @@ -7,9 +7,47 @@ class AdminAreaBankStatementTest < ApplicationSystemTestCase end def test_can_create_statement_manually - visit admin_bank_statements_path + 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 From 04377d8997ce9415721b5c441061ca7dd736001a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 8 Sep 2020 16:26:32 +0300 Subject: [PATCH 6/6] Remove 'import_file_path' from bank_statements model --- config/application.yml.sample | 1 - ...31554_remove_import_file_path_from_bank_statements.rb | 9 +++++++++ db/structure.sql | 6 ++++-- 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20200908131554_remove_import_file_path_from_bank_statements.rb diff --git a/config/application.yml.sample b/config/application.yml.sample index d5753adb6..1b6c40951 100644 --- a/config/application.yml.sample +++ b/config/application.yml.sample @@ -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 diff --git a/db/migrate/20200908131554_remove_import_file_path_from_bank_statements.rb b/db/migrate/20200908131554_remove_import_file_path_from_bank_statements.rb new file mode 100644 index 000000000..a80a1e5a5 --- /dev/null +++ b/db/migrate/20200908131554_remove_import_file_path_from_bank_statements.rb @@ -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 diff --git a/db/structure.sql b/db/structure.sql index 6224671ad..1e32bf318 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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, @@ -4850,4 +4849,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200807110611'), ('20200811074839'), ('20200812090409'), -('20200812125810'); +('20200812125810'), +('20200908131554'); + +