From 689396126d6d728476e866eae64e1b540c3865a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Tue, 31 Jan 2017 15:38:08 +0100 Subject: [PATCH] use mysql backend --- receipt.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++++-------- update.rb | 16 ++++++++++++--- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/receipt.rb b/receipt.rb index 2d1bd51..8533b6c 100644 --- a/receipt.rb +++ b/receipt.rb @@ -1,8 +1,13 @@ # coding: utf-8 require 'CSV' require 'yaml' +require 'mysql2' -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: +load 'config.rb' + +db = Mysql2::Client.new(:host => DB_HOST, :username => DB_USER, :database => DB_DB) + +YAML_BASE = {"author": "Christoffer Müller Madsen", "city": "Aarhus", "from": ["Falstersgade 18, 4. th", "8000 Aarhus C"], 'currency': 'DKK', 'commasep': true, 'lang': 'danish', 'seriffont': 'Linux Libertine', 'sansfont': 'Linux Biolinum', '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: +45 81 73 02 02 @@ -12,10 +17,22 @@ Med venlig hilsen ] } PATH_BASE = "./pdf/" -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} -lines = {} + + +PRODUCTS = Hash.new +NAMES = Hash.new +PRICES = Hash.new + +# Initialize product and member hashses +db.query("SELECT * FROM products").each do |product| + PRODUCTS[product['id']] = product['pretty_name'] + PRICES[product['id']] = product['price'] +end + +db.query("SELECT * FROM members").each do |member| + NAMES[member['id']] = member['name'] +end + class LogItem def initialize(time,person,product,amount: 1) @@ -35,6 +52,16 @@ def read_file(file) rows end +def read_db(db) + rows = Array.new + db.query("SELECT * FROM transactions").each do |row| + if date_of_prev('monday').to_time < row["time"] + rows << LogItem.new(row["time"], row["buyer"], row["product"], amount: row["amount"]) + end + end + rows +end + def partition_rows(rows) skyldnere = Hash.new rows.each do |row| @@ -50,11 +77,11 @@ def generate_receipt(skyldnere) skyldnere.each do |person, products| counter += 1 yaml = YAML_BASE.clone - yaml["to"] = PRETTY_NAMES[person] + yaml["to"] = NAMES[person] products.each do |product, amount| yaml["invoice-nr"] = Time.now.strftime('%Y%W') + "-" + counter.to_s yaml["service"] = Array.new unless yaml["service"] - hash = {description: PRETTY_PRODUCTS[product], price: PRICES[product]*amount, amount: amount} + hash = {description: PRODUCTS[product], price: PRICES[product].to_i*amount, amount: amount} yaml["service"] << Hash[hash.map{ |k, v| [k.to_s, v] }] end output = Hash[yaml.map{ |k, v| [k.to_s, v] }].to_yaml @@ -68,5 +95,20 @@ def generate_receipt(skyldnere) end end -partition = partition_rows(read_file("./log/matkant-#{Time.now.strftime('%Y%W')}.log")) +# Utility + +def date_of_next(day) + date = Date.parse(day) + delta = date > Date.today ? 0 : 7 + date + delta +end + +def date_of_prev(day) + date = Date.parse(day) + delta = date < Date.today ? 0 : 7 + date - delta +end + +#partition = partition_rows(read_file("./log/matkant-#{Time.now.strftime('%Y%W')}.log")) +partition = partition_rows(read_db(db)) generate_receipt(partition) diff --git a/update.rb b/update.rb index 0250b5d..371d95d 100644 --- a/update.rb +++ b/update.rb @@ -1,3 +1,9 @@ +require 'mysql2' + +load 'config.rb' + +db = Mysql2::Client.new(:host => DB_HOST, :username => DB_USER, :database => DB_DB) + person = ARGV[0] product = ARGV[1] amount = ARGV[2] @@ -5,6 +11,10 @@ log_dir = "./log/" amount = 1.0 unless amount -output = File.open("#{log_dir}/matkant-#{Time.now.strftime('%Y%W')}.log","a+") -output << "#{Time.now.to_i};#{person};#{product};#{amount}\n" -output.close +statement = db.prepare("INSERT INTO transactions (buyer, product, amount) VALUES (?, ?, ?)") +result = statement.execute(person,product,amount) +puts result + +#output = File.open("#{log_dir}/matkant-#{Time.now.strftime('%Y%W')}.log","a+") +#output << "#{Time.now.to_i};#{person};#{product};#{amount}\n" +#output.close