From 40368aaa625e6aed05d83b45a0d3e69075136ee5 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 21 Apr 2021 10:47:03 +0300 Subject: [PATCH] added non canceled method for check paid invoices --- app/models/bank_transaction.rb | 10 ++++++++++ lib/tasks/invoices/process_payments.rake | 6 ++++-- test/tasks/invoices/process_payments_test.rb | 12 +++++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index 24bf51e0c..8fe0d86b5 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -27,6 +27,16 @@ class BankTransaction < ApplicationRecord .find_by(total: sum) end + def non_canceled? + paid_invoices = registrar.invoices + .order(created_at: :asc) + .non_cancelled + .where(total: sum) + return true if paid_invoices.any?(&:paid?) + + false + end + def registrar @registrar ||= Invoice.find_by(reference_no: parsed_ref_number)&.buyer end diff --git a/lib/tasks/invoices/process_payments.rake b/lib/tasks/invoices/process_payments.rake index edf6609b9..4ce9587f7 100644 --- a/lib/tasks/invoices/process_payments.rake +++ b/lib/tasks/invoices/process_payments.rake @@ -39,9 +39,11 @@ namespace :invoices do reference_no: incoming_transaction.payment_reference_number, description: incoming_transaction.payment_description } transaction = bank_statement.bank_transactions.create!(transaction_attributes) - Invoice.create_from_transaction!(transaction) unless transaction.autobindable? - transaction.autobind_invoice + unless transaction.non_canceled? + Invoice.create_from_transaction!(transaction) unless transaction.autobindable? + transaction.autobind_invoice + end end end else diff --git a/test/tasks/invoices/process_payments_test.rb b/test/tasks/invoices/process_payments_test.rb index 3f0cd49e6..2d993fd7a 100644 --- a/test/tasks/invoices/process_payments_test.rb +++ b/test/tasks/invoices/process_payments_test.rb @@ -14,6 +14,7 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase currency: @payment_currency, reference_no: @payment_reference_number) @account_activity = account_activities(:one) + @account = accounts(:cash) Setting.registry_iban = beneficiary_iban @@ -48,8 +49,13 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase assert_no_difference 'AccountActivity.count' do assert_no_difference 'Invoice.count' do - Invoice.create_from_transaction!(transaction) unless transaction.autobindable? - transaction.autobind_invoice + assert_no_difference -> {@account.balance} do + unless transaction.non_canceled? + Invoice.create_from_transaction!(transaction) unless transaction.autobindable? + transaction.autobind_invoice + @account.reload + end + end end end end @@ -114,7 +120,7 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase run_task end - assert_changes -> { registrar.invoices.count } do + assert_no_changes -> { registrar.invoices.count } do run_task end end