class Oddb2xml::Builder

def build_calc

def build_calc
  prepare_calc_items
  a_builder = Nokogiri::XML::Builder.new(encoding: "utf-8") do |xml|
    xml.doc.tag_suffix = @tag_suffix
    xml.ARTICLES(XML_OPTIONS) do
      @calc_items.each do |ean13, info|
        if info&.compositions
          xml.ARTICLE do
            xml.GTIN ean13
            xml.NAME info.column_c
            xml.PKG_SIZE info.pkg_size
            xml.SELLING_UNITS info.selling_units
            xml.MEASURE info.measure # Nur wenn Lösung wen Spalte M ml, Spritze
            if info.galenic_form.is_a?(String)
              xml.GALENIC_FORM info.galenic_form
              xml.GALENIC_GROUP "Unknown"
            else
              xml.GALENIC_FORM info.galenic_form.description
              xml.GALENIC_GROUP info.galenic_group ? info.galenic_group.description : "Unknown"
            end
            xml.COMPOSITIONS do
              info.compositions.each do |composition|
                xml.COMPOSITION do
                  xml.EXCIPIENS { emit_substance(xml, composition.excipiens) } if composition.excipiens
                  xml.LABEL composition.label if composition.label
                  xml.LABEL_DESCRIPTION composition.label_description if composition.label_description
                  xml.CORRESP composition.corresp if composition.corresp
                  if composition.substances && (composition.substances.size > 0)
                    xml.SUBSTANCES do
                      composition.substances.each { |substance| xml.SUBSTANCE { emit_substance(xml, substance, true) } }
                    end
                  end
                end
              end
            end
          end
        end
      end
    end
  end
  csv_name = File.join(WORK_DIR, "oddb_calc.csv")
  CSV.open(csv_name, "w+", col_sep: ";") do |csv|
    csv << ["gtin"] + @calc_items.values.first.headers
    @calc_items.each do |key, value|
      if value&.to_array
        csv << [key] + value.to_array
      else
        puts "key #{key.inspect} WITHOUT #{value.inspect}"
      end
    end
  end
  Oddb2xml.add_hash(a_builder.to_xml)
end