add piece price field and options

This commit is contained in:
Christoffer Müller Madsen 2017-02-01 11:18:38 +01:00
parent 6cb5b4b63c
commit c0d85dd941
2 changed files with 45 additions and 27 deletions

View File

@ -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
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.open("./pandoc/details.yml","w") do |file|
file << output
file.close
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])

View File

@ -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}