# coding: utf-8 require 'CSV' require 'yaml' YAML_BASE = {"invoice-nr" => "2017-01-31", "author": "Christoffer Müller Madsen", 'currency': 'DKK', 'commasep': true, 'lang': 'danish', 'seriffont': 'Hoefler Text', 'sansfont': 'Helvetica Neue', 'fontsize': '10pt', 'geometry': 'a4paper, left=43mm, right=43mm, top=51mm, bottom=17mm', 'closingnote': %Q[Please transfer the due amount to the following bank account within the next 14 days: Mustermann GmbH Kreditinstitut: Deutsche Postbank AG IBAN: DE18 3601 0043 9999 9999 99 BIC: PBNKDEFF We really appreciate your business and look forward to future projects together. Best regards, ] } PATH_BASE = "./pdf/" PRETTY_NAMES = {"papkaffe" => "Papkrus kaffe (0.35 L)", "kage" => "Kage", "spandaur" => "Spandaur"} PRICES = {'papkaffe' => 7, "kage" => 11} lines = {} class LogItem def initialize(time,person,product,amount: 1) @time = time @person = person @product = product @amount = amount end attr_accessor :time, :person, :product, :amount end class LineItem def initialize(type) @type = type end attr_accessor(:type) end def read_file(file) rows = Array.new CSV.foreach(file, col_sep: ';', converters: :float) do |row| rows << LogItem.new(row[0],row[1],row[2],amount: row[3]) end rows end def partition_rows(rows) skyldnere = Hash.new rows.each do |row| skyldnere[row.person] = Hash.new unless skyldnere[row.person] skyldnere[row.person][row.product] = 0 unless skyldnere[row.person][row.product] skyldnere[row.person][row.product] += row.amount end skyldnere end def generate_receipt(skyldnere) skyldnere.each do |person, products| yaml = YAML_BASE.clone yaml["to"] = person products.each do |product, amount| yaml["service"] = Array.new unless yaml["service"] hash = {description: PRETTY_NAMES[product], price: PRICES[product]*amount, amount: amount} yaml["service"] << Hash[hash.map{ |k, v| [k.to_s, v] }] end puts Hash[yaml.map{ |k, v| [k.to_s, v] }].to_yaml end # pdf_path = PATH_BASE + "temp.pdf" # return pdf_path end partition = partition_rows(read_file("./log/matkant.log")) puts partition.inspect generate_receipt(partition)