From c0d85dd941729b48996571709973b1f5a302abd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Wed, 1 Feb 2017 11:18:38 +0100 Subject: [PATCH] add piece price field and options --- receipt.rb => invoice.rb | 60 ++++++++++++++++++++++++++-------------- pandoc/template.tex | 12 ++++---- 2 files changed, 45 insertions(+), 27 deletions(-) rename receipt.rb => invoice.rb (67%) diff --git a/receipt.rb b/invoice.rb similarity index 67% rename from receipt.rb rename to invoice.rb index ead3fbb..79018db 100644 --- a/receipt.rb +++ b/invoice.rb @@ -2,35 +2,53 @@ require 'CSV' 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! + 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 -På forhånd tak. - Med venlig hilsen ] } -PATH_BASE = "./pdf/" - - -PRODUCTS = Hash.new -NAMES = Hash.new -PRICES = Hash.new +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'] + 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 end db.query("SELECT * FROM members").each do |member| - NAMES[member['id']] = member['name'] + Names[member['id']] = member['name'] end @@ -72,32 +90,32 @@ def partition_rows(rows) skyldnere end -def generate_receipt(skyldnere) +def generate_receipt(skyldnere,draft) counter = 0 skyldnere.each do |person, products| counter += 1 yaml = YAML_BASE.clone - yaml["to"] = 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: PRODUCTS[product], price: PRICES[product].to_i*amount, amount: amount} + hash = {description: Products[product], pieceprice: Prices[product].to_i, price: Prices[product].to_i*amount, amount: amount} yaml["service"] << Hash[hash.map{ |k, v| [k.to_s, v] }] end - unless Date.today.sunday? + if draft yaml["draft"] = "true" yaml["drafttext"] = "Udkast" end 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 + File.open("./pandoc/details.yml","w") do |file| + file << output + end `cd pandoc/; make -B` - `cp pandoc/output.pdf pdf/#{yaml["invoice-nr"]}.pdf` - puts "#{person}: #{yaml["invoice-nr"]}.pdf" + `cp pandoc/output.pdf #{OUTPUT_PATH}/#{yaml["invoice-nr"]}.pdf` + puts "#{person}: #{yaml["invoice-nr"]}.pdf" unless Options[:silent] end end @@ -116,4 +134,4 @@ def date_of_prev(day) end partition = partition_rows(read_db(db)) -generate_receipt(partition) +generate_receipt(partition,Options[:draft]) diff --git a/pandoc/template.tex b/pandoc/template.tex index 0d6f06d..ab3e980 100644 --- a/pandoc/template.tex +++ b/pandoc/template.tex @@ -128,18 +128,18 @@ $endfor$ \STautoround*{2} % Get spreadtab to always display the decimal part $if(commasep)$\STsetdecimalsep{,}$endif$ % Use comma as decimal separator -\begin{spreadtab}{{tabular}[t t t t]{lp{7.2cm}cr}} +\begin{spreadtab}{{tabular}[t t t t t]{lp{7.2cm}ccr}} \hdashline[1pt/1pt] - @ \noalign{\vskip 2mm} \textbf{\#} & @ \textbf{Produkt} & @ \textbf{Antal} & @ \textbf{Priser i $currency$} \\ \hline + @ \noalign{\vskip 2mm} \textbf{\#} & @ \textbf{Produkt} & @ \textbf{Antal} & @ \textbf{Pris pr. stk.} & @ \textbf{Samlet pris i $currency$} \\ \hline $for(service)$ @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos & @ $service.description$ $if(service.details)$\newline \begin{itemize} $for(service.details)$\scriptsize \item $service.details$ $endfor$ \end{itemize} - $endif$ & $service.amount$ & $service.price$\\$endfor$ \noalign{\vskip 2mm} \hline - @ & @ \multicolumn{2}{r}{Subtotal:} & :={sum(d1:[0,-1])} \\ \hhline{~~~-} - @ & @ \multicolumn{2}{r}{Rabat fra kantinekort (-10\%):} & -(100-90)/100*[0,-1] \\ \hhline{~~~-} - @ & @ \multicolumn{2}{r}{\textbf{Total:}} & \textbf{:={[0,-2]+[0,-1]}} \\ \hhline{~~~-} + $endif$ & $service.amount$ & $service.pieceprice$ & $service.price$\\$endfor$ \noalign{\vskip 2mm} \hline + @ & @ \multicolumn{3}{r}{Subtotal:} & :={sum(e1:[0,-1])} \\ \hhline{~~~~-} + @ & @ \multicolumn{3}{r}{Rabat fra kantinekort (-10\%):} & -(100-90)/100*[0,-1] \\ \hhline{~~~~-} + @ & @ \multicolumn{3}{r}{\textbf{Total:}} & \textbf{:={[0,-2]+[0,-1]}} \\ \hhline{~~~~-} \end{spreadtab} \vspace{15mm}