matkant_invoice/receipt.rb

73 lines
2.4 KiB
Ruby
Raw Normal View History

2017-01-30 13:06:38 +00:00
# coding: utf-8
require 'CSV'
require 'yaml'
2017-01-30 14:23:50 +00:00
YAML_BASE = {"author": "Christoffer Müller Madsen", "city": "Aarhus", "from": ["Falstersgade 18, 4. th", "8000 Aarhus C"], '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[Overfør venligst det anførte beløb via MobilePay til følgende telefonnummer i løbet af de næste 14 dage:
2017-01-30 13:06:38 +00:00
2017-01-30 14:23:50 +00:00
+45 81 73 02 02
2017-01-30 13:06:38 +00:00
2017-01-30 14:23:50 +00:00
forhånd tak.
2017-01-30 13:06:38 +00:00
2017-01-30 14:23:50 +00:00
Med venlig hilsen
2017-01-30 13:06:38 +00:00
] }
PATH_BASE = "./pdf/"
2017-01-30 14:23:50 +00:00
PRETTY_PRODUCTS = {"papkaffe" => "Papkrus kaffe (0.35 L)", "kage" => "Kage", "spandaur" => "Spandaur", "kaffekrus" => "Kaffe i krus (0.25 L)"}
PRETTY_NAMES = {"Alexander" => "Alexander Munch-Hansen", "Jon" => "Jon Michael Aanes"}
PRICES = {'papkaffe' => 7, "kage" => 11, "kaffekrus" => 5}
2017-01-30 13:06:38 +00:00
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
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)
2017-01-30 14:23:50 +00:00
counter = 0
2017-01-30 13:06:38 +00:00
skyldnere.each do |person, products|
2017-01-30 14:23:50 +00:00
counter += 1
2017-01-30 13:06:38 +00:00
yaml = YAML_BASE.clone
2017-01-30 14:23:50 +00:00
yaml["to"] = PRETTY_NAMES[person]
2017-01-30 13:06:38 +00:00
products.each do |product, amount|
2017-01-30 14:23:50 +00:00
yaml["invoice-nr"] = Time.now.strftime('%Y%W') + "-" + counter.to_s
2017-01-30 13:06:38 +00:00
yaml["service"] = Array.new unless yaml["service"]
2017-01-30 14:23:50 +00:00
hash = {description: PRETTY_PRODUCTS[product], price: PRICES[product]*amount, amount: amount}
2017-01-30 13:06:38 +00:00
yaml["service"] << Hash[hash.map{ |k, v| [k.to_s, v] }]
end
2017-01-30 14:23:50 +00:00
output = Hash[yaml.map{ |k, v| [k.to_s, v] }].to_yaml
output += "---\n"
file = File.open("./pandoc/details.yml","w")
file << output
file.close
`cd pandoc/; make -B`
`cp pandoc/output.pdf pdf/#{yaml["invoice-nr"]}.pdf`
puts "#{person}: #{yaml["invoice-nr"]}.pdf"
2017-01-30 13:06:38 +00:00
end
end
2017-01-30 14:23:50 +00:00
partition = partition_rows(read_file("./log/matkant-#{Time.now.strftime('%Y%W')}.log"))
2017-01-30 13:06:38 +00:00
generate_receipt(partition)