mirror of
https://github.com/internetee/registry.git
synced 2025-07-27 21:16:12 +02:00
implemented method which get invoice number from billins side and updated tests
This commit is contained in:
parent
390d693b13
commit
9f4942e522
8 changed files with 191 additions and 10 deletions
|
@ -25,6 +25,7 @@ class Registrar
|
|||
def send_invoice_data_to_billing_system
|
||||
add_invoice_instance = EisBilling::AddDeposits.new(@invoice)
|
||||
result = add_invoice_instance.send_invoice
|
||||
|
||||
link = JSON.parse(result.body)['everypay_link']
|
||||
|
||||
@invoice.update(payment_link: link)
|
||||
|
|
|
@ -41,21 +41,30 @@ class Invoice < ApplicationRecord
|
|||
attribute :vat_rate, ::Type::VatRate.new
|
||||
|
||||
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
|
||||
self.number = last_no + 1
|
||||
else
|
||||
self.number = Setting.invoice_number_min.to_i
|
||||
end
|
||||
# if last_no && last_no >= Setting.invoice_number_min.to_i
|
||||
# self.number = last_no + 1
|
||||
# else
|
||||
# self.number = Setting.invoice_number_min.to_i
|
||||
# end
|
||||
|
||||
return if number <= Setting.invoice_number_max.to_i
|
||||
# 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
|
||||
|
||||
self.number = JSON.parse(result.body)['invoice_number'].to_i
|
||||
end
|
||||
|
||||
def to_s
|
||||
I18n.t('invoice_no', no: number)
|
||||
end
|
||||
|
|
27
app/services/eis_billing/get_invoice_number.rb
Normal file
27
app/services/eis_billing/get_invoice_number.rb
Normal 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
|
|
@ -24,6 +24,17 @@ class AdminAreaInvoicesIntegrationTest < ApplicationIntegrationTest
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
assert_text 'Create new invoice'
|
||||
|
|
|
@ -16,6 +16,17 @@ class BankTransactionTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
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')
|
||||
another_invoice = @invoice.dup
|
||||
another_invoice.save(validate: false)
|
||||
|
@ -40,6 +51,17 @@ class BankTransactionTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
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')
|
||||
another_invoice = @invoice.dup
|
||||
another_invoice.save(validate: false)
|
||||
|
@ -61,6 +83,17 @@ class BankTransactionTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
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')
|
||||
another_invoice = @invoice.dup
|
||||
another_invoice.save(validate: false)
|
||||
|
|
|
@ -79,6 +79,10 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
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.vat_no = 'US1234'
|
||||
invoice = @invoice.dup
|
||||
|
@ -118,13 +122,49 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
transaction.reference_no = registrar.reference_no
|
||||
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)
|
||||
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
|
||||
invoice = Invoice.create_from_transaction!(transaction)
|
||||
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
|
||||
invoice = Invoice.create_from_transaction!(transaction)
|
||||
assert_equal 0.99, invoice.total
|
||||
|
@ -136,6 +176,10 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
transaction.reference_no = registrar.reference_no
|
||||
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
|
||||
Invoice.create_from_transaction!(transaction)
|
||||
end
|
||||
|
|
|
@ -144,6 +144,18 @@ class RegistrarTest < ActiveJob::TestCase
|
|||
end
|
||||
|
||||
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')
|
||||
Setting.days_to_keep_invoices_active = 10
|
||||
|
||||
|
@ -154,6 +166,17 @@ class RegistrarTest < ActiveJob::TestCase
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
perform_enqueued_jobs do
|
||||
|
|
|
@ -77,6 +77,17 @@ class ProcessPaymentsTaskTest < ActiveJob::TestCase
|
|||
end
|
||||
|
||||
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?
|
||||
|
||||
@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
|
||||
|
||||
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)
|
||||
|
||||
assert_changes -> { registrar.accounts.first.balance } do
|
||||
|
@ -163,6 +185,17 @@ class ProcessPaymentsTaskTest < ActiveJob::TestCase
|
|||
end
|
||||
|
||||
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)
|
||||
@invoice.payment_orders.destroy_all
|
||||
@invoice.destroy
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue