matkant_invoice/invoice.rb

166 lines
4.1 KiB
Ruby

# coding: utf-8
require 'yaml'
require 'mysql2'
require 'optparse'
load 'config.rb'
Options = {}
OptionParser.new do |opts|
opts.banner = "Usage: invoice.rb [options]"
opts.on("-d", "--draft", "Generate invoices with 'DRAFT' watermark") do |d|
Options[:draft] = d
end
opts.on("-v", "--verbose", "Run verbosely") do |v|
Options[:verbose] = v
end
opts.on("-s", "--silent", "Run silently (overrules -v)") do |s|
Options[:silent] = true
Options[:verbose] = false
end
end.parse!
class Transaction
def initialize(time, person, product, amount: 1)
@time = time
@person = person
@product = product
@amount = amount
end
attr_accessor :time, :person, :product, :amount
end
class Product
def initialize(id, description, price)
@id = id
@description = description
@price = price
end
def to_s
"#{@id} => #{@description}, #{@price}"
end
attr_accessor :id, :description, :price
end
def read_file(file)
rows = Array.new
CSV.foreach(file, col_sep: ';', converters: :float) do |row|
rows << Transaction.new(row[0],row[1],row[2],amount: row[3])
end
rows
end
def read_db(db)
transactions = Array.new
db.query("SELECT * FROM Transactions").each do |trans|
if date_of_prev('monday').to_time < trans["time"]
transactions << Transaction.new(trans["time"], trans["buyer"],
trans["product"], amount: trans["amount"])
end
end
return transactions
end
# Partition transactions into the persons who they belong to
def partition_transactions(transactions)
persons = Hash.new
transactions.each do |trans|
# Initialize data structures if not already done
if !(persons[trans.person])
persons[trans.person] = {}
end
if !(persons[trans.person][trans.product])
persons[trans.person][trans.product] = 0
end
persons[trans.person][trans.product] += trans.amount
end
return persons
end
def generate_receipt(persons,draft)
counter = 0
# Iterate through persons who have purchased something in the given timeframe
persons.each do |person, products|
counter += 1
yaml = YAML_BASE.clone
yaml["to"] = Names[person]
yaml["invoice-nr"] = Time.now.strftime('%Y%W') + "-" + counter.to_s
# Iterate through products purchased and write data to yaml hash
products.each do |id, amount|
yaml["service"] = Array.new unless yaml["service"]
hash = {description: Products[id].description,
pieceprice: Products[id].price.to_i,
price: Products[id].price.to_i*amount, amount: amount}
# Convert Symbols to Strings to ensure compatibility with pandoc
yaml["service"] << Hash[hash.map{ |k, v| [k.to_s, v] }]
end
# Write draft watermark on invoice if marked as draft
if draft
yaml["draft"] = "true"
yaml["drafttext"] = "Udkast"
end
# Convert Symbols to Strings to ensure compatibility with pandoc
output = Hash[yaml.map{ |k, v| [k.to_s, v] }].to_yaml
output += "---\n"
# Write output to file
File.open("./pandoc/details.yml","w") do |file|
file << output
end
# Generate PDF using pandoc
`cd pandoc/; make -B`
`cp pandoc/output.pdf #{OUTPUT_PATH}/#{yaml["invoice-nr"]}.pdf`
puts "#{person}: #{yaml["invoice-nr"]}.pdf" unless Options[:silent]
end
end
# 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
db = Mysql2::Client.new(:host => DB_HOST, :username => DB_USER, :database => DB_DB)
# Initialize product and member hashses
Products = Hash.new
Names = Hash.new
Prices = Hash.new
db.query("SELECT * FROM Products").each do |p|
product = Product.new(p['id'], p['description'], p['price'])
Products[p['id']] = product
if Options[:verbose]
puts "Found product: #{product}"
end
end
db.query("SELECT * FROM Persons").each do |member|
Names[member['id']] = member['name']
end
partition = partition_transactions(read_db(db))
generate_receipt(partition,Options[:draft])