Created
May 8, 2015 13:59
-
-
Save duderman/dbbb2965c0f81408b40b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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