mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 09:57:23 +02:00
60 lines
2.6 KiB
Ruby
60 lines
2.6 KiB
Ruby
class Directo < ActiveRecord::Base
|
|
belongs_to :item, polymorphic: true
|
|
|
|
def self.send_receipts
|
|
new_trans = Invoice.where(invoice_type: "DEB", in_directo: false).where(cancelled_at: nil)
|
|
total = new_trans.count
|
|
counter = 0
|
|
Rails.logger.info("[DIRECTO] Will try to send #{total} invoices")
|
|
|
|
new_trans.find_in_batches(batch_size: 10).each do |group|
|
|
mappers = {} # need them as no direct connection between invoice
|
|
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
|
xml.invoices {
|
|
group.each do |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.sum_cache
|
|
Rails.logger.info("[DIRECTO] Invoice #{invoice.number} has been skipped")
|
|
next
|
|
end
|
|
counter += 1
|
|
|
|
num = invoice.number
|
|
mappers[num] = invoice
|
|
xml.invoice(
|
|
"SalesAgent" => Setting.directo_sales_agent,
|
|
"Number" => num,
|
|
"InvoiceDate" => invoice.created_at.strftime("%Y-%m-%dT%H:%M:%S"),
|
|
"PaymentTerm" => Setting.directo_receipt_payment_term,
|
|
"Currency" => invoice.currency,
|
|
"CustomerCode"=> invoice.buyer.try(:directo_handle)
|
|
){
|
|
xml.line(
|
|
"ProductID" => Setting.directo_receipt_product_name,
|
|
"Quantity" => 1,
|
|
"UnitPriceWoVAT" => ActionController::Base.helpers.number_with_precision(invoice.sum_cache/(1+invoice.vat_prc), precision: 2, separator: "."),
|
|
"ProductName" => invoice.description
|
|
)
|
|
}
|
|
end
|
|
}
|
|
end
|
|
|
|
data = builder.to_xml.gsub("\n",'')
|
|
response = RestClient::Request.execute(url: ENV['directo_invoice_url'], method: :post, payload: {put: "1", what: "invoice", xmldata: data}, verify_ssl: false).to_s
|
|
dump_result_to_db(mappers, response)
|
|
end
|
|
|
|
STDOUT << "#{Time.zone.now.utc} - Directo receipts sending finished. #{counter} of #{total} are sent\n"
|
|
end
|
|
|
|
def self.dump_result_to_db mappers, xml
|
|
Nokogiri::XML(xml).css("Result").each do |res|
|
|
obj = mappers[res.attributes["docid"].value.to_i]
|
|
obj.directo_records.create!(response: res.as_json.to_h)
|
|
obj.update_columns(in_directo: true)
|
|
Rails.logger.info("[DIRECTO] Invoice #{res.attributes["docid"].value} was pushed and return is #{res.as_json.to_h.inspect}")
|
|
end
|
|
end
|
|
end
|