diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 87f27af36..adaad87d6 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -117,10 +117,10 @@ class Invoice < ApplicationRecord return unless registrar_user vat = VatRateCalculator.new(registrar: registrar_user).calculate - wo_vat = transaction.sum / (1 + (vat / 100)) - registrar_user.issue_prepayment_invoice(amount: wo_vat, - description: 'Direct top-up via bank transfer', - payable: false) + wo_vat = (transaction.sum / (1 + (vat / 100))) + + registrar_user.issue_prepayment_invoice(wo_vat, 'Direct top-up via bank transfer', + payable: false, total: transaction.sum) end private diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 86ff5ef4d..42fbc2b78 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -54,7 +54,7 @@ class Registrar < ApplicationRecord end end - def issue_prepayment_invoice(amount, description = nil, payable: true) + def issue_prepayment_invoice(amount, description = nil, payable: true, total: nil) vat_rate = ::Invoice::VatRateCalculator.new(registrar: self).calculate invoice = invoices.create!( @@ -90,6 +90,7 @@ class Registrar < ApplicationRecord buyer_email: email, reference_no: reference_no, vat_rate: vat_rate, + total: total, items_attributes: [ { description: 'prepayment', diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 9c1c45610..6ee2dc85c 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -109,4 +109,16 @@ class InvoiceTest < ActiveSupport::TestCase seller_zip: nil) assert_equal 'street, city, state', invoice.seller_address end + + def test_assumes_correct_sum_amount_when_created_by_transaction + registrar = registrars(:bestnames) + + bank_transaction = bank_transactions(:one).dup + bank_transaction.reference_no = registrar.reference_no + bank_transaction.sum = 5 + bank_transaction.save + + invoice = Invoice.create_from_transaction!(bank_transaction) + assert_equal 5, invoice.total + end end diff --git a/test/tasks/invoices/process_payments_test.rb b/test/tasks/invoices/process_payments_test.rb index 38c2e8fdc..eeaf411cc 100644 --- a/test/tasks/invoices/process_payments_test.rb +++ b/test/tasks/invoices/process_payments_test.rb @@ -94,6 +94,13 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase end end + def test_topup_creates_invoice_with_total_of_transactioned_amount + registrar = registrars(:bestnames) + run_task + + assert_equal 0.1, registrar.invoices.last.total + end + def test_output assert_output "Transactions processed: 1\n" do run_task