added directo handler for interaction with billing system

This commit is contained in:
olegphenomenon 2022-02-01 13:35:11 +02:00
parent d9696014ff
commit 0259dd7fec
8 changed files with 286 additions and 1 deletions

View file

@ -0,0 +1,35 @@
class EisBilling::DirectoResponseController < EisBilling::BaseController
def update
response = params[:response]
xml_data = params[:xml_data]
@month = params.fetch(:month, false)
process_directo_response(xml_data, response)
render status: 200, json: { messege: 'Should return new directo number', status: :ok }
end
private
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)
end
directo_record.save!
end
end