Skip to content

Instantly share code, notes, and snippets.

@kohgpat
Created July 3, 2012 08:10
Show Gist options
  • Save kohgpat/3038403 to your computer and use it in GitHub Desktop.
Save kohgpat/3038403 to your computer and use it in GitHub Desktop.
Fines...
require 'nokogiri'
require 'dbf'
require 'ostruct'
class Dictionary
def initialize
@doc = Nokogiri::XML(File.open("dictionaries/F014.xml"))
end
def find_kod_by_osn(osn)
@doc.xpath("//item").each do |i|
return i.attr("Kod") if i.attr("Osn") == osn
end
end
end
class Fines
def self.start(xml, dbf)
@xml = Nokogiri::XML(File.open(xml))
@dbf = DBF::Table.new(dbf)
@dictionary = Dictionary.new
set_default_values
find_records
set_fines
end
private
def self.set_default_values
@xml.xpath("//SCHET").xpath("SENK_EKMP")[0].content = 0.0
@xml.xpath("//SCHET").xpath("SENK_MEE")[0].content = 0.0
@xml.xpath("//SCHET").xpath("SENK_MEK")[0].content = 0.0
@xml.xpath("//ZAP").each do |z|
z.xpath("SLUCH").xpath("SUMP")[0].content = z.xpath("SLUCH").xpath("SUMV")[0].content
z.xpath("SLUCH").xpath("SENK_MEE")[0].content = 0.0
z.xpath("SLUCH").xpath("SENK_EKMP")[0].content = 0.0
z.xpath("SLUCH").xpath("SENK_MEK")[0].content = 0.0
end
end
def self.find_records
@records =[]
@dbf.each do |r|
@records << OpenStruct.new(polis: r.sn_pol, reason: r.name_pp, sum: r.r_result) if r.r_result != 0.0
end
end
def self.set_fines
sank_mek = 0.0
sank_mee = 0.0
sank_ekmp = 0.0
@records.each do |r|
@xml.xpath("//ZAP").each do |z|
spolis = z.xpath("PACIENT").xpath("SPOLIS").text
npolis = z.xpath("PACIENT").xpath("NPOLIS").text
spolis.empty? ? polis = npolis : polis = "#{spolis} #{npolis}"
if polis == r.polis
sumv = z.xpath("SLUCH").xpath("SUMV").text.to_f
sump = (sumv - r.sum).round(2)
refreason = @dictionary.find_kod_by_osn(r.reason)
z.xpath("SLUCH").xpath("SUMP")[0].content = sump
z.xpath("SLUCH").xpath("REFREASON")[0].content = refreason
if r.reason.start_with?("3")
z.xpath("SLUCH").xpath("SENK_EKMP")[0].content = r.sum
sank_ekmp += r.sum
end
if r.reason.start_with?("4")
z.xpath("SLUCH").xpath("SENK_MEE")[0].content = r.sum
sank_mee += r.sum
end
end
end
end
@xml.xpath("//SCHET").xpath("SENK_EKMP")[0].content = sank_ekmp
@xml.xpath("//SCHET").xpath("SENK_MEE")[0].content = sank_mee
@xml.xpath("//SCHET").xpath("SENK_MEK")[0].content = sank_mek
File.open("out.xml", 'w') { |f| f.write(@xml.to_xml) }
end
end
Fines.start(ARGV[0], ARGV[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment