Merge pull request #1499 from internetee/1496-fix-invoice-autobinding

Add non_cancelled scope to autobind invoices
This commit is contained in:
Timo Võhmar 2020-01-29 20:10:36 +02:00 committed by GitHub
commit cbbfbae99e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View file

@ -17,7 +17,13 @@ class BankTransaction < ApplicationRecord
end
def invoice
@invoice ||= registrar.invoices.order(created_at: :asc).unpaid.find_by(total: sum) if registrar
return unless registrar
@invoice ||= registrar.invoices
.order(created_at: :asc)
.unpaid
.non_cancelled
.find_by(total: sum)
end
def registrar

View file

@ -41,6 +41,28 @@ class BankTransactionTest < ActiveSupport::TestCase
end
end
def test_binds_if_this_sum_cancelled_invoice_already_present
create_payable_invoice(number: '2222', total: 10, reference_no: '1234567')
another_invoice = @invoice.dup
another_invoice.save(validate: false)
another_item = @invoice.items.first.dup
another_item.invoice = another_invoice
another_item.save
another_invoice.reload
another_invoice.update(reference_no: '1234567', number: '2221', cancelled_at: Time.zone.now)
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