implemented method which get invoice number from billins side and updated tests

This commit is contained in:
olegphenomenon 2022-01-26 13:15:40 +02:00
parent 390d693b13
commit 9f4942e522
8 changed files with 191 additions and 10 deletions

View file

@ -25,6 +25,7 @@ class Registrar
def send_invoice_data_to_billing_system def send_invoice_data_to_billing_system
add_invoice_instance = EisBilling::AddDeposits.new(@invoice) add_invoice_instance = EisBilling::AddDeposits.new(@invoice)
result = add_invoice_instance.send_invoice result = add_invoice_instance.send_invoice
link = JSON.parse(result.body)['everypay_link'] link = JSON.parse(result.body)['everypay_link']
@invoice.update(payment_link: link) @invoice.update(payment_link: link)

View file

@ -41,19 +41,28 @@ class Invoice < ApplicationRecord
attribute :vat_rate, ::Type::VatRate.new attribute :vat_rate, ::Type::VatRate.new
def set_invoice_number def set_invoice_number
last_no = Invoice.order(number: :desc).limit(1).pick(:number) # last_no = Invoice.order(number: :desc).limit(1).pick(:number)
if last_no && last_no >= Setting.invoice_number_min.to_i # if last_no && last_no >= Setting.invoice_number_min.to_i
self.number = last_no + 1 # self.number = last_no + 1
else # else
self.number = Setting.invoice_number_min.to_i # self.number = Setting.invoice_number_min.to_i
# end
# return if number <= Setting.invoice_number_max.to_i
# errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached'))
# logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE')
# throw(:abort)
result = EisBilling::GetInvoiceNumber.send_invoice
if JSON.parse(result.body)['error'] == 'out of range'
errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached'))
logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE')
throw(:abort)
end end
return if number <= Setting.invoice_number_max.to_i self.number = JSON.parse(result.body)['invoice_number'].to_i
errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached'))
logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE')
throw(:abort)
end end
def to_s def to_s

View file

@ -0,0 +1,27 @@
module EisBilling
class GetInvoiceNumber < EisBilling::Base
attr_reader :invoice
def self.send_invoice
base_request
end
private
def self.base_request
uri = URI(invoice_generator_url)
http = Net::HTTP.new(uri.host, uri.port)
headers = {
'Authorization' => 'Bearer foobar',
'Content-Type' => 'application/json',
'Accept' => TOKEN
}
http.post(invoice_generator_url, nil, headers)
end
def self.invoice_generator_url
"#{BASE_URL}/api/v1/invoice_generator/invoice_number_generator"
end
end
end

View file

@ -24,6 +24,17 @@ class AdminAreaInvoicesIntegrationTest < ApplicationIntegrationTest
end end
def test_create_new_invoice def test_create_new_invoice
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
visit new_admin_invoice_path visit new_admin_invoice_path
assert_text 'Create new invoice' assert_text 'Create new invoice'

View file

@ -16,6 +16,17 @@ class BankTransactionTest < ActiveSupport::TestCase
end end
def test_binds_if_this_sum_invoice_already_present def test_binds_if_this_sum_invoice_already_present
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
create_payable_invoice(number: '2222', total: 10, reference_no: '1234567') create_payable_invoice(number: '2222', total: 10, reference_no: '1234567')
another_invoice = @invoice.dup another_invoice = @invoice.dup
another_invoice.save(validate: false) another_invoice.save(validate: false)
@ -40,6 +51,17 @@ class BankTransactionTest < ActiveSupport::TestCase
end end
def test_binds_if_this_sum_cancelled_invoice_already_present def test_binds_if_this_sum_cancelled_invoice_already_present
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
create_payable_invoice(number: '2222', total: 10, reference_no: '1234567') create_payable_invoice(number: '2222', total: 10, reference_no: '1234567')
another_invoice = @invoice.dup another_invoice = @invoice.dup
another_invoice.save(validate: false) another_invoice.save(validate: false)
@ -61,6 +83,17 @@ class BankTransactionTest < ActiveSupport::TestCase
end end
def test_marks_the_first_one_as_paid_if_same_sum def test_marks_the_first_one_as_paid_if_same_sum
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
create_payable_invoice(number: '2222', total: 10, reference_no: '1234567') create_payable_invoice(number: '2222', total: 10, reference_no: '1234567')
another_invoice = @invoice.dup another_invoice = @invoice.dup
another_invoice.save(validate: false) another_invoice.save(validate: false)

View file

@ -79,6 +79,10 @@ class InvoiceTest < ActiveSupport::TestCase
end end
def test_buyer_vat_no_is_taken_from_registrar_by_default def test_buyer_vat_no_is_taken_from_registrar_by_default
invoice_n = Invoice.order(number: :desc).last.number
response = OpenStruct.new(body: "{\"invoice_number\":\"#{invoice_n + 3}\"}")
Spy.on(EisBilling::GetInvoiceNumber, :send_invoice).and_return(response)
registrar = registrars(:bestnames) registrar = registrars(:bestnames)
registrar.vat_no = 'US1234' registrar.vat_no = 'US1234'
invoice = @invoice.dup invoice = @invoice.dup
@ -118,13 +122,49 @@ class InvoiceTest < ActiveSupport::TestCase
transaction.reference_no = registrar.reference_no transaction.reference_no = registrar.reference_no
transaction.sum = 250 transaction.sum = 250
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
invoice = Invoice.create_from_transaction!(transaction) invoice = Invoice.create_from_transaction!(transaction)
assert_equal 250, invoice.total assert_equal 250, invoice.total
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 4}\"}", headers: {})
transaction.sum = 146.88 transaction.sum = 146.88
invoice = Invoice.create_from_transaction!(transaction) invoice = Invoice.create_from_transaction!(transaction)
assert_equal 146.88, invoice.total assert_equal 146.88, invoice.total
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 5}\"}", headers: {})
transaction.sum = 0.99 transaction.sum = 0.99
invoice = Invoice.create_from_transaction!(transaction) invoice = Invoice.create_from_transaction!(transaction)
assert_equal 0.99, invoice.total assert_equal 0.99, invoice.total
@ -136,6 +176,10 @@ class InvoiceTest < ActiveSupport::TestCase
transaction.reference_no = registrar.reference_no transaction.reference_no = registrar.reference_no
transaction.sum = 250 transaction.sum = 250
invoice_n = Invoice.order(number: :desc).last.number
response = OpenStruct.new(body: "{\"invoice_number\":\"#{invoice_n + 3}\"}")
Spy.on(EisBilling::GetInvoiceNumber, :send_invoice).and_return(response)
assert_emails 1 do assert_emails 1 do
Invoice.create_from_transaction!(transaction) Invoice.create_from_transaction!(transaction)
end end

View file

@ -144,6 +144,18 @@ class RegistrarTest < ActiveJob::TestCase
end end
def test_issues_new_invoice def test_issues_new_invoice
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
travel_to Time.zone.parse('2010-07-05') travel_to Time.zone.parse('2010-07-05')
Setting.days_to_keep_invoices_active = 10 Setting.days_to_keep_invoices_active = 10
@ -154,6 +166,17 @@ class RegistrarTest < ActiveJob::TestCase
end end
def test_issues_e_invoice_along_with_invoice def test_issues_e_invoice_along_with_invoice
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
EInvoice::Providers::TestProvider.deliveries.clear EInvoice::Providers::TestProvider.deliveries.clear
perform_enqueued_jobs do perform_enqueued_jobs do

View file

@ -77,6 +77,17 @@ class ProcessPaymentsTaskTest < ActiveJob::TestCase
end end
def test_if_invoice_is_overdue_than_48_hours def test_if_invoice_is_overdue_than_48_hours
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
assert_not @invoice.paid? assert_not @invoice.paid?
@account_activity.update(activity_type: "add_credit", bank_transaction: nil, created_at: Time.zone.today - 3.days, creator_str: 'AdminUser') @account_activity.update(activity_type: "add_credit", bank_transaction: nil, created_at: Time.zone.today - 3.days, creator_str: 'AdminUser')
@ -144,6 +155,17 @@ class ProcessPaymentsTaskTest < ActiveJob::TestCase
end end
def test_credits_registrar_account_without_invoice_beforehand def test_credits_registrar_account_without_invoice_beforehand
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
registrar = registrars(:bestnames) registrar = registrars(:bestnames)
assert_changes -> { registrar.accounts.first.balance } do assert_changes -> { registrar.accounts.first.balance } do
@ -163,6 +185,17 @@ class ProcessPaymentsTaskTest < ActiveJob::TestCase
end end
def test_topup_creates_invoice_and_send_it_as_paid def test_topup_creates_invoice_and_send_it_as_paid
invoice_n = Invoice.order(number: :desc).last.number
stub_request(:post, "http://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
with(
headers: {
'Accept'=>'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer foobar',
'Content-Type'=>'application/json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
registrar = registrars(:bestnames) registrar = registrars(:bestnames)
@invoice.payment_orders.destroy_all @invoice.payment_orders.destroy_all
@invoice.destroy @invoice.destroy