Skip to content

Commit

Permalink
Merge pull request #2433 from internetee/remove-feature-toggle
Browse files Browse the repository at this point in the history
remove eis-billing feature toggle
  • Loading branch information
vohmar authored Sep 13, 2022
2 parents e3a5f15 + ce677ce commit f77883b
Show file tree
Hide file tree
Showing 29 changed files with 472 additions and 596 deletions.
7 changes: 0 additions & 7 deletions app/controllers/eis_billing/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class BaseController < ApplicationController
protect_from_forgery with: :null_session
skip_authorization_check # Temporary solution
# skip_before_action :verify_authenticity_token # Temporary solution
before_action :persistent
before_action :authorized

INITIATOR = 'billing'.freeze
Expand Down Expand Up @@ -49,11 +48,5 @@ def billing_secret_key
def logger
Rails.logger
end

def persistent
return true if Feature.billing_system_integrated?

render json: { message: "We don't work yet!" }, status: :unauthorized
end
end
end
83 changes: 21 additions & 62 deletions app/jobs/directo_invoice_forward_job.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,31 @@
class DirectoInvoiceForwardJob < ApplicationJob
def perform(monthly: false, dry: false)
@dry = dry
(@month = Time.zone.now - 1.month) if monthly
data = nil

@client = new_directo_client
monthly ? send_monthly_invoices : send_receipts
end
if monthly
@month = Time.zone.now - 1.month
data = collect_monthly_data
else
data = collect_receipts_data
end

def new_directo_client
DirectoApi::Client.new(ENV['directo_invoice_url'], Setting.directo_sales_agent,
Setting.directo_receipt_payment_term)
EisBilling::SendDataToDirecto.send_request(object_data: data, monthly: monthly, dry: dry)
end

def send_receipts
def collect_receipts_data
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|
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)
collected_data << invoice.as_directo_json
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
collected_data
end

def valid_invoice_conditions?(invoice)
Expand All @@ -68,29 +39,17 @@ def valid_invoice_conditions?(invoice)
true
end

def sync_with_directo
assign_monthly_numbers if @month
def collect_monthly_data
registrars_data = []

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
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:)
Expand Down
125 changes: 125 additions & 0 deletions app/jobs/directo_invoice_forward_legacy_job.rb
Original file line number Diff line number Diff line change
@@ -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
84 changes: 0 additions & 84 deletions app/jobs/directo_invoice_forward_two_job.rb

This file was deleted.

19 changes: 6 additions & 13 deletions app/jobs/send_e_invoice_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ def perform(invoice_id, payable: true)
invoice = Invoice.find_by(id: invoice_id)
return unless need_to_process_invoice?(invoice: invoice, payable: payable)

process(invoice: invoice, payable: payable)
send_invoice_to_eis_billing(invoice: invoice, payable: payable)
invoice.update(e_invoice_sent_at: Time.zone.now)
rescue StandardError => e
log_error(invoice: invoice, error: e)
raise e
Expand All @@ -16,23 +17,15 @@ def perform(invoice_id, payable: true)

def need_to_process_invoice?(invoice:, payable:)
logger.info "Checking if need to process e-invoice #{invoice}, payable: #{payable}"
unprocessable = invoice.do_not_send_e_invoice? && (invoice.monthly_invoice ? true : payable)
return false if invoice.blank?
return false if unprocessable
return false if invoice.do_not_send_e_invoice? && payable

true
end

def process(invoice:, payable:)
invoice.to_e_invoice(payable: payable).deliver unless Rails.env.development?
invoice.update(e_invoice_sent_at: Time.zone.now)
log_success(invoice)
end

def log_success(invoice)
id = invoice.try(:id) || invoice
message = "E-Invoice for an invoice with ID # #{id} was sent successfully"
logger.info message
def send_invoice_to_eis_billing(invoice:, payable:)
result = EisBilling::SendEInvoice.send_request(invoice: invoice, payable: payable)
logger.info result.body
end

def log_error(invoice:, error:)
Expand Down
Loading

0 comments on commit f77883b

Please sign in to comment.