2017-01-30 13:06:38 +00:00
# coding: utf-8
require 'CSV'
require 'yaml'
2017-01-31 14:38:08 +00:00
require 'mysql2'
2017-02-01 10:18:38 +00:00
require 'optparse'
2017-01-30 13:06:38 +00:00
2017-01-31 14:38:08 +00:00
load 'config.rb'
2017-02-01 10:18:38 +00:00
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!
2017-01-31 14:38:08 +00:00
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:
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
Med venlig hilsen
2017-01-30 13:06:38 +00:00
] }
2017-02-01 10:18:38 +00:00
Products = Hash . new
Names = Hash . new
Prices = Hash . new
2017-01-31 14:38:08 +00:00
# Initialize product and member hashses
db . query ( " SELECT * FROM products " ) . each do | product |
2017-02-01 10:18:38 +00:00
id = product [ 'id' ]
pretty_name = product [ 'pretty_name' ]
price = product [ 'price' ]
if Options [ :verbose ]
puts " Found product: #{ id } => #{ pretty_name } , #{ price } "
end
Products [ id ] = pretty_name
Prices [ id ] = price
2017-01-31 14:38:08 +00:00
end
db . query ( " SELECT * FROM members " ) . each do | member |
2017-02-01 10:18:38 +00:00
Names [ member [ 'id' ] ] = member [ 'name' ]
2017-01-31 14:38:08 +00:00
end
2017-01-30 13:06:38 +00:00
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
2017-01-31 14:38:08 +00:00
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
2017-01-30 13:06:38 +00:00
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
2017-02-01 10:18:38 +00:00
def generate_receipt ( skyldnere , draft )
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-02-01 10:18:38 +00:00
yaml [ " to " ] = 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-02-01 10:18:38 +00:00
hash = { description : Products [ product ] , pieceprice : Prices [ product ] . to_i , price : Prices [ product ] . to_i * 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-31 15:13:26 +00:00
2017-02-01 10:18:38 +00:00
if draft
2017-01-31 15:13:26 +00:00
yaml [ " draft " ] = " true "
yaml [ " drafttext " ] = " Udkast "
end
2017-01-30 14:23:50 +00:00
output = Hash [ yaml . map { | k , v | [ k . to_s , v ] } ] . to_yaml
output += " --- \n "
2017-02-01 10:18:38 +00:00
File . open ( " ./pandoc/details.yml " , " w " ) do | file |
file << output
end
2017-01-30 14:23:50 +00:00
` cd pandoc/; make -B `
2017-02-01 10:18:38 +00:00
` cp pandoc/output.pdf #{ OUTPUT_PATH } / #{ yaml [ " invoice-nr " ] } .pdf `
puts " #{ person } : #{ yaml [ " invoice-nr " ] } .pdf " unless Options [ :silent ]
2017-01-30 13:06:38 +00:00
end
end
2017-01-31 14:38:08 +00:00
# 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_db ( db ) )
2017-02-01 10:18:38 +00:00
generate_receipt ( partition , Options [ :draft ] )