mirror of
https://github.com/internetee/registry.git
synced 2025-07-27 21:16:12 +02:00
36 lines
1.1 KiB
Ruby
36 lines
1.1 KiB
Ruby
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: :ok, json: { messege: 'Should return new directo number' }
|
|
end
|
|
|
|
private
|
|
|
|
def process_directo_response(xml, req)
|
|
Rails.logger.info "[Directo] - Responded with body: #{xml}"
|
|
Nokogiri::XML(req).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
|