BastliPOSPrinter

git clone git://xatko.vsos.ethz.ch/BastliPOSPrinter.git
Log | Files | Refs | README

10_JSON_to_GROFF.lua (1201B)


      1 #!/usr/bin/lua
      2 
      3 local json  = require("json")
      4 
      5 s = io.read("*a")
      6 
      7 local j=json.decode(s)
      8 
      9 local tpl=assert(io.open(os.getenv("GROFF_TEMPLATE"), "r"))
     10 
     11 function getPositions()
     12 	local str="===\nlrxn\n.\n"
     13 	local str2=".TS\n"
     14 	local total=0
     15 	for _,a in pairs(j["articles"]) do
     16 		local escname=a["name"]
     17 		escname=string.gsub(escname,"\\","\\\\")
     18 		escname=string.gsub(escname,"\n","\n\\&")
     19 		local subtot=a["price"]*a["amount"]
     20 		total=total+subtot
     21 		str=str..string.format("%d\tT{\n%s\nT}\t%.2f CHF\n", a["amount"], escname, subtot)
     22 		str2=str2.."llxn\n"
     23 	end
     24 	str=str..string.format("\n\tTOTAL\t%.2f CHF\n", total)
     25 	return str2..str..".TE";
     26 end
     27 
     28 local fctbl={
     29 	["sale_id"]=function()
     30 		return j["sale_id"]
     31 	end,
     32 	["positions"]=function()
     33 		return getPositions()
     34 	end,
     35 	["today"]=function()
     36 		return os.date("%Y-%m-%d %H:%M")
     37 	end,
     38 }
     39 
     40 function processLine(t)
     41 	a,b,name= string.find(t, "{{([^}]+)}}")
     42 	if name then
     43 		io.write(string.sub(t,1,a-1))
     44 		if fctbl[name] then
     45 			io.write(fctbl[name]())
     46 		else
     47 			io.write("{{"..name.."}}")
     48 		end
     49 		processLine(string.sub(t,b+1))
     50 	else
     51 		io.write(t)
     52 	end
     53 end
     54 
     55 while true do
     56 	t=tpl:read("*line")
     57 	if not t then
     58 		break
     59 	end
     60 	processLine(t.."\n")
     61 end
     62 
     63 tpl:close()