Skip to content

Commit

Permalink
Add autobind fix if invoice with this sum is already present
Browse files Browse the repository at this point in the history
  • Loading branch information
yulgolem committed Jan 29, 2020
1 parent 3821c76 commit 19cd5ec
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/bank_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions test/models/bank_transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 19cd5ec

Please sign in to comment.