add piece price field and options
This commit is contained in:
parent
6cb5b4b63c
commit
c0d85dd941
|
@ -2,35 +2,53 @@
|
||||||
require 'CSV'
|
require 'CSV'
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
require 'mysql2'
|
require 'mysql2'
|
||||||
|
require 'optparse'
|
||||||
|
|
||||||
load 'config.rb'
|
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)
|
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:
|
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
|
+45 81 73 02 02
|
||||||
|
|
||||||
På forhånd tak.
|
|
||||||
|
|
||||||
Med venlig hilsen
|
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
|
# Initialize product and member hashses
|
||||||
db.query("SELECT * FROM products").each do |product|
|
db.query("SELECT * FROM products").each do |product|
|
||||||
PRODUCTS[product['id']] = product['pretty_name']
|
id = product['id']
|
||||||
PRICES[product['id']] = product['price']
|
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
|
end
|
||||||
|
|
||||||
db.query("SELECT * FROM members").each do |member|
|
db.query("SELECT * FROM members").each do |member|
|
||||||
NAMES[member['id']] = member['name']
|
Names[member['id']] = member['name']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -72,32 +90,32 @@ def partition_rows(rows)
|
||||||
skyldnere
|
skyldnere
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_receipt(skyldnere)
|
def generate_receipt(skyldnere,draft)
|
||||||
counter = 0
|
counter = 0
|
||||||
skyldnere.each do |person, products|
|
skyldnere.each do |person, products|
|
||||||
counter += 1
|
counter += 1
|
||||||
yaml = YAML_BASE.clone
|
yaml = YAML_BASE.clone
|
||||||
yaml["to"] = NAMES[person]
|
yaml["to"] = Names[person]
|
||||||
products.each do |product, amount|
|
products.each do |product, amount|
|
||||||
yaml["invoice-nr"] = Time.now.strftime('%Y%W') + "-" + counter.to_s
|
yaml["invoice-nr"] = Time.now.strftime('%Y%W') + "-" + counter.to_s
|
||||||
yaml["service"] = Array.new unless yaml["service"]
|
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] }]
|
yaml["service"] << Hash[hash.map{ |k, v| [k.to_s, v] }]
|
||||||
end
|
end
|
||||||
|
|
||||||
unless Date.today.sunday?
|
if draft
|
||||||
yaml["draft"] = "true"
|
yaml["draft"] = "true"
|
||||||
yaml["drafttext"] = "Udkast"
|
yaml["drafttext"] = "Udkast"
|
||||||
end
|
end
|
||||||
|
|
||||||
output = Hash[yaml.map{ |k, v| [k.to_s, v] }].to_yaml
|
output = Hash[yaml.map{ |k, v| [k.to_s, v] }].to_yaml
|
||||||
output += "---\n"
|
output += "---\n"
|
||||||
file = File.open("./pandoc/details.yml","w")
|
File.open("./pandoc/details.yml","w") do |file|
|
||||||
file << output
|
file << output
|
||||||
file.close
|
end
|
||||||
`cd pandoc/; make -B`
|
`cd pandoc/; make -B`
|
||||||
`cp pandoc/output.pdf pdf/#{yaml["invoice-nr"]}.pdf`
|
`cp pandoc/output.pdf #{OUTPUT_PATH}/#{yaml["invoice-nr"]}.pdf`
|
||||||
puts "#{person}: #{yaml["invoice-nr"]}.pdf"
|
puts "#{person}: #{yaml["invoice-nr"]}.pdf" unless Options[:silent]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -116,4 +134,4 @@ def date_of_prev(day)
|
||||||
end
|
end
|
||||||
|
|
||||||
partition = partition_rows(read_db(db))
|
partition = partition_rows(read_db(db))
|
||||||
generate_receipt(partition)
|
generate_receipt(partition,Options[:draft])
|
|
@ -128,18 +128,18 @@ $endfor$
|
||||||
\STautoround*{2} % Get spreadtab to always display the decimal part
|
\STautoround*{2} % Get spreadtab to always display the decimal part
|
||||||
$if(commasep)$\STsetdecimalsep{,}$endif$ % Use comma as decimal separator
|
$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]
|
\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
|
$for(service)$ @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos
|
||||||
& @ $service.description$
|
& @ $service.description$
|
||||||
$if(service.details)$\newline \begin{itemize}
|
$if(service.details)$\newline \begin{itemize}
|
||||||
$for(service.details)$\scriptsize \item $service.details$
|
$for(service.details)$\scriptsize \item $service.details$
|
||||||
$endfor$ \end{itemize}
|
$endfor$ \end{itemize}
|
||||||
$endif$ & $service.amount$ & $service.price$\\$endfor$ \noalign{\vskip 2mm} \hline
|
$endif$ & $service.amount$ & $service.pieceprice$ & $service.price$\\$endfor$ \noalign{\vskip 2mm} \hline
|
||||||
@ & @ \multicolumn{2}{r}{Subtotal:} & :={sum(d1:[0,-1])} \\ \hhline{~~~-}
|
@ & @ \multicolumn{3}{r}{Subtotal:} & :={sum(e1:[0,-1])} \\ \hhline{~~~~-}
|
||||||
@ & @ \multicolumn{2}{r}{Rabat fra kantinekort (-10\%):} & -(100-90)/100*[0,-1] \\ \hhline{~~~-}
|
@ & @ \multicolumn{3}{r}{Rabat fra kantinekort (-10\%):} & -(100-90)/100*[0,-1] \\ \hhline{~~~~-}
|
||||||
@ & @ \multicolumn{2}{r}{\textbf{Total:}} & \textbf{:={[0,-2]+[0,-1]}} \\ \hhline{~~~-}
|
@ & @ \multicolumn{3}{r}{\textbf{Total:}} & \textbf{:={[0,-2]+[0,-1]}} \\ \hhline{~~~~-}
|
||||||
\end{spreadtab}
|
\end{spreadtab}
|
||||||
|
|
||||||
\vspace{15mm}
|
\vspace{15mm}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user