From 19cd5ecd95d3665e6b82d0ce772b14897eef1b4d Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Wed, 29 Jan 2020 20:19:00 +0500 Subject: [PATCH] Add autobind fix if invoice with this sum is already present --- app/models/bank_transaction.rb | 2 +- test/models/bank_transaction_test.rb | 51 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index 6612a4dbd1..c04a98e90b 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -17,7 +17,7 @@ def binded_invoice end def invoice - @invoice ||= registrar.invoices.find_by(total: sum) if registrar + @invoice ||= registrar.invoices.order(created_at: :asc).unpaid.find_by(total: sum) if registrar end def registrar diff --git a/test/models/bank_transaction_test.rb b/test/models/bank_transaction_test.rb index 37ff4041a5..e4cce24879 100644 --- a/test/models/bank_transaction_test.rb +++ b/test/models/bank_transaction_test.rb @@ -15,6 +15,57 @@ def test_matches_against_invoice_nubmber_and_reference_number end end + def test_binds_if_this_sum_invoice_already_present + create_payable_invoice(number: '2222', total: 10, reference_no: '1234567') + another_invoice = @invoice.dup + another_invoice.save(validate: false) + another_invoice.update(reference_no: '7654321', number: '2221') + + another_item = @invoice.items.first.dup + another_item.invoice = another_invoice + another_item.save + another_invoice.reload + + first_transaction = BankTransaction.new(description: 'invoice #2221', + sum: 10, + description: 'Order nr 1 from registrar 1234567 second number 2345678') + + first_transaction.create_activity(another_invoice.buyer, another_invoice) + + transaction = BankTransaction.new(description: 'invoice #2222', + sum: 10, + description: 'Order nr 1 from registrar 1234567 second number 2345678') + + assert_difference 'AccountActivity.count' do + transaction.autobind_invoice + end + end + + def test_marks_the_first_one_as_paid_if_same_sum + create_payable_invoice(number: '2222', total: 10, reference_no: '1234567') + another_invoice = @invoice.dup + another_invoice.save(validate: false) + another_invoice.update(reference_no: '7654321', number: '2221') + + another_item = @invoice.items.first.dup + another_item.invoice = another_invoice + another_item.save + another_invoice.reload + + transaction = BankTransaction.new(description: 'invoice #2222', + sum: 10, + description: 'Order nr 1 from registrar 1234567 second number 2345678') + + assert_difference 'AccountActivity.count' do + transaction.autobind_invoice + end + + @invoice.reload + another_invoice.reload + assert(@invoice.paid?) + assert_not(another_invoice.paid?) + end + def test_matches_against_invoice_nubmber_and_reference_number_in_description create_payable_invoice(number: '2222', total: 10, reference_no: '1234567') transaction = BankTransaction.new(description: 'invoice #2222',