mirror of
https://github.com/internetee/registry.git
synced 2025-07-29 22:16:19 +02:00
rename directo job
This commit is contained in:
parent
cd81e58ff6
commit
ce677ce69c
4 changed files with 155 additions and 155 deletions
|
@ -1,60 +1,31 @@
|
||||||
class DirectoInvoiceForwardJob < ApplicationJob
|
class DirectoInvoiceForwardJob < ApplicationJob
|
||||||
def perform(monthly: false, dry: false)
|
def perform(monthly: false, dry: false)
|
||||||
@dry = dry
|
data = nil
|
||||||
(@month = Time.zone.now - 1.month) if monthly
|
|
||||||
|
|
||||||
@client = new_directo_client
|
if monthly
|
||||||
monthly ? send_monthly_invoices : send_receipts
|
@month = Time.zone.now - 1.month
|
||||||
|
data = collect_monthly_data
|
||||||
|
else
|
||||||
|
data = collect_receipts_data
|
||||||
|
end
|
||||||
|
|
||||||
|
EisBilling::SendDataToDirecto.send_request(object_data: data, monthly: monthly, dry: dry)
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_directo_client
|
def collect_receipts_data
|
||||||
DirectoApi::Client.new(ENV['directo_invoice_url'], Setting.directo_sales_agent,
|
|
||||||
Setting.directo_receipt_payment_term)
|
|
||||||
end
|
|
||||||
|
|
||||||
def send_receipts
|
|
||||||
unsent_invoices = Invoice.where(in_directo: false).non_cancelled
|
unsent_invoices = Invoice.where(in_directo: false).non_cancelled
|
||||||
|
collected_data = []
|
||||||
|
|
||||||
Rails.logger.info("[DIRECTO] Trying to send #{unsent_invoices.count} prepayment invoices")
|
|
||||||
unsent_invoices.each do |invoice|
|
unsent_invoices.each do |invoice|
|
||||||
unless valid_invoice_conditions?(invoice)
|
unless valid_invoice_conditions?(invoice)
|
||||||
Rails.logger.info "[DIRECTO] Invoice #{invoice.number} has been skipped"
|
Rails.logger.info "[DIRECTO] Invoice #{invoice.number} has been skipped"
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
@client.invoices.add_with_schema(invoice: invoice.as_directo_json, schema: 'prepayment')
|
collected_data << invoice.as_directo_json
|
||||||
end
|
end
|
||||||
|
|
||||||
sync_with_directo
|
collected_data
|
||||||
end
|
|
||||||
|
|
||||||
def send_monthly_invoices
|
|
||||||
Registrar.where.not(test_registrar: true).find_each do |registrar|
|
|
||||||
next unless registrar.cash_account
|
|
||||||
|
|
||||||
@client = new_directo_client
|
|
||||||
send_invoice_for_registrar(registrar)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def send_invoice_for_registrar(registrar)
|
|
||||||
summary = registrar.monthly_summary(month: @month)
|
|
||||||
@client.invoices.add_with_schema(invoice: summary, schema: 'summary') unless summary.nil?
|
|
||||||
|
|
||||||
sync_with_directo if @client.invoices.count.positive?
|
|
||||||
end
|
|
||||||
|
|
||||||
def assign_monthly_numbers
|
|
||||||
raise 'Directo Counter is going to be out of period!' if directo_counter_exceedable?(@client.invoices.count)
|
|
||||||
|
|
||||||
min_directo = Setting.directo_monthly_number_min.presence.try(:to_i)
|
|
||||||
directo_number = [Setting.directo_monthly_number_last.presence.try(:to_i),
|
|
||||||
min_directo].compact.max || 0
|
|
||||||
|
|
||||||
@client.invoices.each do |inv|
|
|
||||||
directo_number += 1
|
|
||||||
inv.number = directo_number
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid_invoice_conditions?(invoice)
|
def valid_invoice_conditions?(invoice)
|
||||||
|
@ -68,29 +39,17 @@ class DirectoInvoiceForwardJob < ApplicationJob
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def sync_with_directo
|
def collect_monthly_data
|
||||||
assign_monthly_numbers if @month
|
registrars_data = []
|
||||||
|
|
||||||
Rails.logger.info("[Directo] - attempting to send following XML:\n #{@client.invoices.as_xml}")
|
Registrar.where.not(test_registrar: true).find_each do |registrar|
|
||||||
return if @dry
|
registrars_data << {
|
||||||
|
registrar: registrar,
|
||||||
res = @client.invoices.deliver(ssl_verify: false)
|
registrar_summery: registrar.monthly_summary(month: @month),
|
||||||
process_directo_response(res.body, @client.invoices.as_xml)
|
}
|
||||||
rescue SocketError, Errno::ECONNREFUSED, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
|
|
||||||
EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError
|
|
||||||
Rails.logger.info('[Directo] Failed to communicate via API')
|
|
||||||
end
|
|
||||||
|
|
||||||
def process_directo_response(xml, req)
|
|
||||||
Rails.logger.info "[Directo] - Responded with body: #{xml}"
|
|
||||||
Nokogiri::XML(xml).css('Result').each do |res|
|
|
||||||
if @month
|
|
||||||
mark_invoice_as_sent(res: res, req: req)
|
|
||||||
else
|
|
||||||
inv = Invoice.find_by(number: res.attributes['docid'].value.to_i)
|
|
||||||
mark_invoice_as_sent(invoice: inv, res: res, req: req)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
registrars_data
|
||||||
end
|
end
|
||||||
|
|
||||||
def mark_invoice_as_sent(invoice: nil, res:, req:)
|
def mark_invoice_as_sent(invoice: nil, res:, req:)
|
||||||
|
|
125
app/jobs/directo_invoice_forward_legacy_job.rb
Normal file
125
app/jobs/directo_invoice_forward_legacy_job.rb
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
class DirectoInvoiceForwardLegacyJob < ApplicationJob
|
||||||
|
def perform(monthly: false, dry: false)
|
||||||
|
@dry = dry
|
||||||
|
(@month = Time.zone.now - 1.month) if monthly
|
||||||
|
|
||||||
|
@client = new_directo_client
|
||||||
|
monthly ? send_monthly_invoices : send_receipts
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_directo_client
|
||||||
|
DirectoApi::Client.new(ENV['directo_invoice_url'], Setting.directo_sales_agent,
|
||||||
|
Setting.directo_receipt_payment_term)
|
||||||
|
end
|
||||||
|
|
||||||
|
def send_receipts
|
||||||
|
unsent_invoices = Invoice.where(in_directo: false).non_cancelled
|
||||||
|
|
||||||
|
Rails.logger.info("[DIRECTO] Trying to send #{unsent_invoices.count} prepayment invoices")
|
||||||
|
unsent_invoices.each do |invoice|
|
||||||
|
unless valid_invoice_conditions?(invoice)
|
||||||
|
Rails.logger.info "[DIRECTO] Invoice #{invoice.number} has been skipped"
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
@client.invoices.add_with_schema(invoice: invoice.as_directo_json, schema: 'prepayment')
|
||||||
|
end
|
||||||
|
|
||||||
|
sync_with_directo
|
||||||
|
end
|
||||||
|
|
||||||
|
def send_monthly_invoices
|
||||||
|
Registrar.where.not(test_registrar: true).find_each do |registrar|
|
||||||
|
next unless registrar.cash_account
|
||||||
|
|
||||||
|
@client = new_directo_client
|
||||||
|
send_invoice_for_registrar(registrar)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def send_invoice_for_registrar(registrar)
|
||||||
|
summary = registrar.monthly_summary(month: @month)
|
||||||
|
@client.invoices.add_with_schema(invoice: summary, schema: 'summary') unless summary.nil?
|
||||||
|
|
||||||
|
sync_with_directo if @client.invoices.count.positive?
|
||||||
|
end
|
||||||
|
|
||||||
|
def assign_monthly_numbers
|
||||||
|
raise 'Directo Counter is going to be out of period!' if directo_counter_exceedable?(@client.invoices.count)
|
||||||
|
|
||||||
|
min_directo = Setting.directo_monthly_number_min.presence.try(:to_i)
|
||||||
|
directo_number = [Setting.directo_monthly_number_last.presence.try(:to_i),
|
||||||
|
min_directo].compact.max || 0
|
||||||
|
|
||||||
|
@client.invoices.each do |inv|
|
||||||
|
directo_number += 1
|
||||||
|
inv.number = directo_number
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def valid_invoice_conditions?(invoice)
|
||||||
|
if invoice.account_activity.nil? || invoice.account_activity.bank_transaction.nil? ||
|
||||||
|
invoice.account_activity.bank_transaction.sum.nil? ||
|
||||||
|
invoice.account_activity.bank_transaction.sum != invoice.total
|
||||||
|
return false
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def sync_with_directo
|
||||||
|
assign_monthly_numbers if @month
|
||||||
|
|
||||||
|
Rails.logger.info("[Directo] - attempting to send following XML:\n #{@client.invoices.as_xml}")
|
||||||
|
return if @dry
|
||||||
|
|
||||||
|
res = @client.invoices.deliver(ssl_verify: false)
|
||||||
|
process_directo_response(res.body, @client.invoices.as_xml)
|
||||||
|
rescue SocketError, Errno::ECONNREFUSED, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
|
||||||
|
EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError
|
||||||
|
Rails.logger.info('[Directo] Failed to communicate via API')
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_directo_response(xml, req)
|
||||||
|
Rails.logger.info "[Directo] - Responded with body: #{xml}"
|
||||||
|
Nokogiri::XML(xml).css('Result').each do |res|
|
||||||
|
if @month
|
||||||
|
mark_invoice_as_sent(res: res, req: req)
|
||||||
|
else
|
||||||
|
inv = Invoice.find_by(number: res.attributes['docid'].value.to_i)
|
||||||
|
mark_invoice_as_sent(invoice: inv, res: res, req: req)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def mark_invoice_as_sent(invoice: nil, res:, req:)
|
||||||
|
directo_record = Directo.new(response: res.as_json.to_h,
|
||||||
|
request: req, invoice_number: res.attributes['docid'].value.to_i)
|
||||||
|
if invoice
|
||||||
|
directo_record.item = invoice
|
||||||
|
invoice.update(in_directo: true)
|
||||||
|
else
|
||||||
|
update_directo_number(num: directo_record.invoice_number)
|
||||||
|
end
|
||||||
|
|
||||||
|
directo_record.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_directo_number(num:)
|
||||||
|
return unless num.to_i > Setting.directo_monthly_number_last.to_i
|
||||||
|
|
||||||
|
Setting.directo_monthly_number_last = num.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
def directo_counter_exceedable?(invoice_count)
|
||||||
|
min_directo = Setting.directo_monthly_number_min.presence.try(:to_i)
|
||||||
|
max_directo = Setting.directo_monthly_number_max.presence.try(:to_i)
|
||||||
|
last_directo = [Setting.directo_monthly_number_last.presence.try(:to_i),
|
||||||
|
min_directo].compact.max || 0
|
||||||
|
|
||||||
|
return true if max_directo && max_directo < (last_directo + invoice_count)
|
||||||
|
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,84 +0,0 @@
|
||||||
class DirectoInvoiceForwardTwoJob < ApplicationJob
|
|
||||||
def perform(monthly: false, dry: false)
|
|
||||||
data = nil
|
|
||||||
|
|
||||||
if monthly
|
|
||||||
@month = Time.zone.now - 1.month
|
|
||||||
data = collect_monthly_data
|
|
||||||
else
|
|
||||||
data = collect_receipts_data
|
|
||||||
end
|
|
||||||
|
|
||||||
EisBilling::SendDataToDirecto.send_request(object_data: data, monthly: monthly, dry: dry)
|
|
||||||
end
|
|
||||||
|
|
||||||
def collect_receipts_data
|
|
||||||
unsent_invoices = Invoice.where(in_directo: false).non_cancelled
|
|
||||||
collected_data = []
|
|
||||||
|
|
||||||
unsent_invoices.each do |invoice|
|
|
||||||
unless valid_invoice_conditions?(invoice)
|
|
||||||
Rails.logger.info "[DIRECTO] Invoice #{invoice.number} has been skipped"
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
collected_data << invoice.as_directo_json
|
|
||||||
end
|
|
||||||
|
|
||||||
collected_data
|
|
||||||
end
|
|
||||||
|
|
||||||
def valid_invoice_conditions?(invoice)
|
|
||||||
if invoice.account_activity.nil? || invoice.account_activity.bank_transaction.nil? ||
|
|
||||||
invoice.account_activity.bank_transaction.sum.nil? ||
|
|
||||||
invoice.account_activity.bank_transaction.sum != invoice.total
|
|
||||||
return false
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def collect_monthly_data
|
|
||||||
registrars_data = []
|
|
||||||
|
|
||||||
Registrar.where.not(test_registrar: true).find_each do |registrar|
|
|
||||||
registrars_data << {
|
|
||||||
registrar: registrar,
|
|
||||||
registrar_summery: registrar.monthly_summary(month: @month),
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
registrars_data
|
|
||||||
end
|
|
||||||
|
|
||||||
def mark_invoice_as_sent(invoice: nil, res:, req:)
|
|
||||||
directo_record = Directo.new(response: res.as_json.to_h,
|
|
||||||
request: req, invoice_number: res.attributes['docid'].value.to_i)
|
|
||||||
if invoice
|
|
||||||
directo_record.item = invoice
|
|
||||||
invoice.update(in_directo: true)
|
|
||||||
else
|
|
||||||
update_directo_number(num: directo_record.invoice_number)
|
|
||||||
end
|
|
||||||
|
|
||||||
directo_record.save!
|
|
||||||
end
|
|
||||||
|
|
||||||
def update_directo_number(num:)
|
|
||||||
return unless num.to_i > Setting.directo_monthly_number_last.to_i
|
|
||||||
|
|
||||||
Setting.directo_monthly_number_last = num.to_i
|
|
||||||
end
|
|
||||||
|
|
||||||
def directo_counter_exceedable?(invoice_count)
|
|
||||||
min_directo = Setting.directo_monthly_number_min.presence.try(:to_i)
|
|
||||||
max_directo = Setting.directo_monthly_number_max.presence.try(:to_i)
|
|
||||||
last_directo = [Setting.directo_monthly_number_last.presence.try(:to_i),
|
|
||||||
min_directo].compact.max || 0
|
|
||||||
|
|
||||||
return true if max_directo && max_directo < (last_directo + invoice_count)
|
|
||||||
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,6 +1,6 @@
|
||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
||||||
class DirectoInvoiceForwardJobTest < ActiveSupport::TestCase
|
class DirectoInvoiceForwardLegacyJobTest < ActiveSupport::TestCase
|
||||||
setup do
|
setup do
|
||||||
@invoice = invoices(:one)
|
@invoice = invoices(:one)
|
||||||
@user = registrars(:bestnames)
|
@user = registrars(:bestnames)
|
||||||
|
@ -38,7 +38,7 @@ class DirectoInvoiceForwardJobTest < ActiveSupport::TestCase
|
||||||
end.to_return(status: 200, body: response)
|
end.to_return(status: 200, body: response)
|
||||||
|
|
||||||
assert_nothing_raised do
|
assert_nothing_raised do
|
||||||
DirectoInvoiceForwardJob.perform_now(monthly: false, dry: false)
|
DirectoInvoiceForwardLegacyJob.perform_now(monthly: false, dry: false)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_not_empty @invoice.directo_records.first.request
|
assert_not_empty @invoice.directo_records.first.request
|
||||||
|
@ -52,7 +52,7 @@ class DirectoInvoiceForwardJobTest < ActiveSupport::TestCase
|
||||||
Setting.directo_monthly_number_max = 30_991
|
Setting.directo_monthly_number_max = 30_991
|
||||||
|
|
||||||
assert_raises 'RuntimeError' do
|
assert_raises 'RuntimeError' do
|
||||||
DirectoInvoiceForwardJob.perform_now(monthly: true, dry: false)
|
DirectoInvoiceForwardLegacyJob.perform_now(monthly: true, dry: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ class DirectoInvoiceForwardJobTest < ActiveSupport::TestCase
|
||||||
end.to_return(status: 200, body: response)
|
end.to_return(status: 200, body: response)
|
||||||
|
|
||||||
assert_difference 'Setting.directo_monthly_number_last' do
|
assert_difference 'Setting.directo_monthly_number_last' do
|
||||||
DirectoInvoiceForwardJob.perform_now(monthly: true, dry: false)
|
DirectoInvoiceForwardLegacyJob.perform_now(monthly: true, dry: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -103,7 +103,7 @@ class DirectoInvoiceForwardJobTest < ActiveSupport::TestCase
|
||||||
end.to_return(status: 200, body: response)
|
end.to_return(status: 200, body: response)
|
||||||
|
|
||||||
assert_difference 'Setting.directo_monthly_number_last' do
|
assert_difference 'Setting.directo_monthly_number_last' do
|
||||||
DirectoInvoiceForwardJob.perform_now(monthly: true, dry: false)
|
DirectoInvoiceForwardLegacyJob.perform_now(monthly: true, dry: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -126,7 +126,7 @@ class DirectoInvoiceForwardJobTest < ActiveSupport::TestCase
|
||||||
end.to_return(status: 200, body: response)
|
end.to_return(status: 200, body: response)
|
||||||
|
|
||||||
assert_difference 'Setting.directo_monthly_number_last' do
|
assert_difference 'Setting.directo_monthly_number_last' do
|
||||||
DirectoInvoiceForwardJob.perform_now(monthly: true, dry: false)
|
DirectoInvoiceForwardLegacyJob.perform_now(monthly: true, dry: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ class DirectoInvoiceForwardJobTest < ActiveSupport::TestCase
|
||||||
end.to_return(status: 200, body: response)
|
end.to_return(status: 200, body: response)
|
||||||
|
|
||||||
assert_difference 'Setting.directo_monthly_number_last' do
|
assert_difference 'Setting.directo_monthly_number_last' do
|
||||||
DirectoInvoiceForwardJob.perform_now(monthly: true, dry: false)
|
DirectoInvoiceForwardLegacyJob.perform_now(monthly: true, dry: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -186,7 +186,7 @@ class DirectoInvoiceForwardJobTest < ActiveSupport::TestCase
|
||||||
(body.include? 'StartDate') && (body.include? 'EndDate') && (body.include? 'goodnames')
|
(body.include? 'StartDate') && (body.include? 'EndDate') && (body.include? 'goodnames')
|
||||||
end.to_return(status: 200, body: response)
|
end.to_return(status: 200, body: response)
|
||||||
|
|
||||||
DirectoInvoiceForwardJob.perform_now(monthly: true, dry: false)
|
DirectoInvoiceForwardLegacyJob.perform_now(monthly: true, dry: false)
|
||||||
|
|
||||||
assert_requested first_registrar_stub
|
assert_requested first_registrar_stub
|
||||||
assert_requested second_registrar_stub
|
assert_requested second_registrar_stub
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue