Skip to content

Instantly share code, notes, and snippets.

@ysnerdem
Created January 19, 2019 18:38
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 ysnerdem/b09cdc776d1303fed68f2c97d3a1b88f to your computer and use it in GitHub Desktop.
Save ysnerdem/b09cdc776d1303fed68f2c97d3a1b88f to your computer and use it in GitHub Desktop.
doc = File.open("/home/yasin/İndirilenler/Satıs_Faturaları.XML")
count = 0
# doc.each do |line|
# count += 1 if line.include?("<INVOICE DBOP=\"INS\" >")
# break if count == 5001
# File.open("1.xml", "a") { |f| f.write(line.to_s) }
# end
#
# File.open("1.xml", "a") { |f| f.write("</SALES_INVOICES>") }
# ****** 2
# File.open("2.xml", "a") { |f| f.write("<?xml version=\"1.0\" encoding=\"ISO-8859-9\"?>\n") }
# File.open("2.xml", "a") { |f| f.write("<SALES_INVOICES>\n") }
#
# doc.each do |line|
# count += 1 if line.include?("<INVOICE DBOP=\"INS\" >")
# break if count == 10001
# if count >= 5001
# File.open("2.xml", "a") { |f| f.write(line.to_s) }
# end
# end
#
# File.open("2.xml", "a") { |f| f.write("</SALES_INVOICES>") }
# ***** 3
# File.open("3.xml", "a") { |f| f.write("<?xml version=\"1.0\" encoding=\"ISO-8859-9\"?>\n") }
# File.open("3.xml", "a") { |f| f.write("<SALES_INVOICES>\n") }
#
# doc.each do |line|
# count += 1 if line.include?("<INVOICE DBOP=\"INS\" >")
# break if count == 15001
# if count >= 10001
# File.open("3.xml", "a") { |f| f.write(line.to_s) }
# end
# end
#
# File.open("3.xml", "a") { |f| f.write("</SALES_INVOICES>") }
# ***** 4
# File.open("4.xml", "a") { |f| f.write("<?xml version=\"1.0\" encoding=\"ISO-8859-9\"?>\n") }
# File.open("4.xml", "a") { |f| f.write("<SALES_INVOICES>\n") }
#
# doc.each do |line|
# count += 1 if line.include?("<INVOICE DBOP=\"INS\" >")
# break if count == 20001
# if count >= 15001
# File.open("4.xml", "a") { |f| f.write(line.to_s) }
# end
# end
#
# File.open("4.xml", "a") { |f| f.write("</SALES_INVOICES>") }
# ***** 5
File.open("5.xml", "a") { |f| f.write("<?xml version=\"1.0\" encoding=\"ISO-8859-9\"?>\n") }
File.open("5.xml", "a") { |f| f.write("<SALES_INVOICES>\n") }
doc.each do |line|
count += 1 if line.include?("<INVOICE DBOP=\"INS\" >")
break if count == 21278
if count >= 20001
File.open("5.xml", "a") { |f| f.write(line.to_s) }
end
end
doc.
@canozel
Copy link

canozel commented Jan 19, 2019

#!/usr/bin/env ruby

@rules = [
    {"<TYPE>": "TYPE"},
    {"<NUMBER>": "FATURO_NO"},
    {"<DATE>": "FATURA_TARIHI"},
    {"<ARP_CODE>": "MUSTERI_KODU"},
    {"<DATE_CREATED>": "SIPARIS_TARIHI"},
    {"<SALESMAN_CODE>": "NOTLAR"},
    {"<DATE>": "SEVK_TARIHI"},
    {"<TYPE>": "TYPE"},
    {"<MASTER_CODE>": "VARYANT_STOK_KODU"},
    {"<QUANTITY>": "ADET"},
    {"<PRICE>": "FIYAT"},
    {"<RC_XRATE>": "DOVIZ_KURU"},
    {"<VAT_RATE>": "KDV_ORANI"},
    {"<VAT_BASE>": "VAT_BASE"},
    {"<EDT_CURR>": "EDT_CURR"},
    {"<EDT_PRICE>": "EDT_PRICE"},
    {"<MASTER_DEF>": "MASTER_DEF"},
    {"<NOTES>": "NOTLAR"}
]

@rule_counter = 0

def xml_tag_length(doc, tag_name)
  file = File.open("#{doc}", "r")
  count = 0
  file.each { |l| count += 1 if l.include?("</#{tag_name}>") }
  count
end

def invs_xml_split(path, n)
  doc = File.open(path, encoding: 'ISO-8859-1')

  limit = xml_tag_length("#{path}", "INVOICE")

  count = 0
  new_doc_name = 0
  @arr = []

  x = 0
  y = n
  doc.each do |line|
    if line.include?("<INVOICE DBOP=\"INS\" >")
      count += 1
      filter_tag(line)
    elsif line.include?("</INVOICE>") && (count == y || count == limit)
      filter_tag(line)
      first = true
      File.open("#{new_doc_name}.XML", "a") do |f|
        if first
          f.puts('<?xml version="1.0" encoding="ISO-8859-9"?>')
          f.puts('<SALES_INVOICES>')
          first = false
        end
        @arr.each { |element| f.write(element.to_s) }
        f.puts('</SALES_INVOICES>')
      end
      @arr = []
      new_doc_name += 1
      x += n
      y += n
    elsif count >= (x + 1) # || count == 0
      if line.include?("<NOTES")
        puts "---------------"
        puts "#line: #{line}"
        puts "#line_strip: #{line.strip.scan(/<(.*?)>./).flatten.first}"
        @arr << line.gsub(line.strip.scan(/<(.*?)>./).flatten.first, "NOTLAR")
      elsif line.include?("<DISCOUNT_DISTR>")
        @arr << line.gsub(line.strip.scan(/<(.*?)>./).flatten.first, "INDIRIM_MIKTARI")
      elsif line.include?("<BASE_AMOUNT>")
        @arr << line
      elsif line.include?("<DISCOUNT_RATE>")
        @arr << line.gsub(line.strip.scan(/<(.*?)>./).flatten.first, "INDIRIM_ORANI")
      end
      unless blacklist?(line)
        filter_tag(line)
      end
    end
  end
  doc.close
end

def filter_tag(line)
  if parent?(line)
    @arr << line
  elsif tag_closed?(line)
    @arr << line
  else
    rule = read_rule(line)
    if rule
      @arr << line.gsub(rule.keys[0].to_s[1..-2], rule.values[0].to_s)
    end
  end
end

def blacklist?(line)
  blacklist = false
  %w(OKCINFO PREACCLINES INTEL DEFNFLDSLIST LIST DETAIL INFO PAYMENT).each do |word|
     if line.include?(word.to_s)
       blacklist = true
       break
     end
  end
  blacklist
end

def tag_closed?(line)
  if line.include?("</INVOICE>")
    @rule_counter = 0
  end
  line.strip.to_s[0..1] == '</'
end

def parent?(line)
  if line.include?("<TRANSACTION>")
    @rule_counter = 7
  end
  !tag_closed?(line) && line.strip.scan(/<(.*?)>./).flatten.first.nil?
end

def read_rule(line)
  if line.include?(@rules[@rule_counter].first.first.to_s)
    @rule_counter += 1
    @rules[@rule_counter - 1]
  end
end

invs_xml_split("/home/canozel/Downloads/Satıs_Faturaları.XML", 5000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment