Skip to content

Instantly share code, notes, and snippets.

@duderman
Created May 8, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duderman/dbbb2965c0f81408b40b to your computer and use it in GitHub Desktop.
Save duderman/dbbb2965c0f81408b40b to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'nokogiri'
invoices = Invoice.where(contract_id: nil)
invoices.each do |invoice|
next if invoice.reload.contract
begin
xml_file = open(invoice.archive_file.file_url)
xml = Nokogiri::XML(xml_file)
pharmacy_chain_id = invoice.pharmacy.pharmacy_chain_id
contract_number = xml.xpath('//Документ//ЗаголовокДокумента//НомерДоговора').text
contract = invoice.supplier.contractor.contracts.where(pharmacy_chain_id: pharmacy_chain_id, number: contract_number).first
if contract
invoice.update_column(:contract_id, contract.id)
invoice.add_correct_attributes('contract_id')
invoice.estimate! if invoice.check_correct! && invoice.draft?
else
puts "Unknown contract with number #{contract_number} for invoice #{invoice.number}"
end
rescue => e
puts e
ensure
xml_file.close
xml_file.unlink
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment