diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index 6fbf7ce592..6612a4dbd1 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -16,22 +16,12 @@ def binded_invoice account_activity.invoice end - - def invoice_num - return @invoice_no if defined?(@invoice_no) - - match = description.match(/^[^\d]*(\d+)/) - return unless match - - @invoice_no = match[1].try(:to_i) - end - def invoice - @invoice ||= registrar.invoices.find_by(number: invoice_num) if registrar + @invoice ||= registrar.invoices.find_by(total: sum) if registrar end def registrar - @registrar ||= Invoice.find_by(reference_no: reference_no)&.buyer + @registrar ||= Invoice.find_by(reference_no: parsed_ref_number)&.buyer end @@ -39,11 +29,9 @@ def registrar def autobind_invoice return if binded? return unless registrar - return unless invoice_num return unless invoice return unless invoice.payable? - return if invoice.total != sum create_activity(registrar, invoice) end @@ -98,4 +86,12 @@ def reset_pending_registrar_balance_reload registrar.settings['balance_auto_reload'].delete('pending') registrar.save! end + + def parsed_ref_number + reference_no || ref_number_from_description + end + + def ref_number_from_description + /(\d{7})/.match(description)[0] + end end diff --git a/test/fixtures/bank_transactions.yml b/test/fixtures/bank_transactions.yml index ada80981d4..70b3b6651b 100644 --- a/test/fixtures/bank_transactions.yml +++ b/test/fixtures/bank_transactions.yml @@ -1,5 +1,5 @@ one: sum: 1 currency: EUR - description: Order nr. 1 - iban: US75512108001245126199 \ No newline at end of file + description: Order nr 1 from registrar 1234567 second number 2345678 + iban: US75512108001245126199 diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index 3fe18b4b2b..bc9fa29003 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -22,4 +22,5 @@ one: vat_rate: 0.1 total: 16.50 reference_no: 13 - number: 1 \ No newline at end of file + number: 1 + description: Order nr 1 from registrar 1234567 second number 2345678 diff --git a/test/integration/registrar_area/invoices/payment_callback_test.rb b/test/integration/registrar_area/invoices/payment_callback_test.rb index 2303158a7a..23db55e844 100644 --- a/test/integration/registrar_area/invoices/payment_callback_test.rb +++ b/test/integration/registrar_area/invoices/payment_callback_test.rb @@ -28,7 +28,7 @@ def payable_invoice def assert_matching_bank_transaction_exists(invoice) assert BankTransaction.find_by( - description: invoice.order, + description: invoice.description, currency: invoice.currency, iban: invoice.seller_iban ), 'Matching bank transaction should exist' @@ -56,4 +56,4 @@ def every_pay_request_params payment_method: "every_pay" } end -end \ No newline at end of file +end diff --git a/test/models/bank_transaction_test.rb b/test/models/bank_transaction_test.rb index 0e4d88bd6b..37ff4041a5 100644 --- a/test/models/bank_transaction_test.rb +++ b/test/models/bank_transaction_test.rb @@ -6,9 +6,20 @@ class BankTransactionTest < ActiveSupport::TestCase @invoice = invoices(:one) end - def test_matches_against_invoice_number_and_reference_number - create_payable_invoice(number: '2222', total: 10, reference_no: '1111') - transaction = BankTransaction.new(description: 'invoice #2222', sum: 10, reference_no: '1111') + def test_matches_against_invoice_nubmber_and_reference_number + create_payable_invoice(number: '2222', total: 10, reference_no: '1234567') + transaction = BankTransaction.new(description: 'invoice #2222', sum: 10, reference_no: '1234567') + + assert_difference 'AccountActivity.count' do + transaction.autobind_invoice + end + 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', + sum: 10, + description: 'Order nr 1 from registrar 1234567 second number 2345678') assert_difference 'AccountActivity.count' do transaction.autobind_invoice