[NTG-context] Learning LuaTeX : Invoice

Henning Hraban Ramm hraban at fiee.net
Sun Feb 20 14:29:56 CET 2011


Am 2011-02-20 um 13:58 schrieb Henning Hraban Ramm:

> That seems to work so far, but invoice.hours and invoice.sum are  
> always doubled!
> In my letter setup, it’s even quadrupled!
> I guess my functions gets called several times, but how can I avoid  
> that?

I found a better approach, but with the letter module involved, my  
"RegisterItem" is called twice!

\starttext
\usemodule[letter]

\startluacode
userdata = userdata or {}

userdata.invoice = { amount = 0, hours = 0, perhour = 100, items = {} }

function userdata.numberformat(amount)
	context(string.gsub(string.format("\%.2f", amount), "%.", ","))
end

function userdata.InvoiceLine(text, hours)
	context.NC()
	context(text)
	context.NC()
	userdata.numberformat(hours)
	context("\\,h")
	context.NC()
	context.NC()
	userdata.numberformat(hours * userdata.invoice.perhour)
	context("\\,€")
	context.NC()
	context.NR()
end

function userdata.InvoiceSumLine(text)
	context.HL()
	context.NC()
	context(text)
	context.NC()
	userdata.numberformat(userdata.invoice.hours)
	context("\\,h")
	context.NC()
	context("à ")
	userdata.numberformat(userdata.invoice.perhour)
	context("\\,€/h")
	context.NC()
	userdata.numberformat(userdata.invoice.amount)
	context("\\,€")
	context.NC()
	context.NR()
end

function userdata.RegisterItem(text, hours)
	for no, item in ipairs(userdata.invoice.items) do
		-- we need to check for double registering due to some call logic of  
ConTeXt and the letter module
		if item.text == text then
			--return -- commented to show double calling
		end
	end
	table.insert(userdata.invoice.items, {text=text, hours=hours})
end

function userdata.Invoice()
	local amountsum, hoursum = 0,0
	context("\\starttabulate[|lw(8cm)|rg(,)w(2cm)|rg(,)w(2cm)| 
rg(,)w(3cm)|]")
	for no, item in ipairs(userdata.invoice.items) do
		userdata.InvoiceLine(item.text, item.hours)
		hoursum = hoursum + item.hours
		amountsum = amountsum + item.hours * userdata.invoice.perhour
	end
	userdata.invoice.hours = hoursum
	userdata.invoice.amount = amountsum
	userdata.InvoiceSumLine("Sum")
	context.stoptabulate()
end

\stopluacode

\startletter[subject={INVOICE}]

\startluacode

userdata.RegisterItem("Do some \\CONTEXT ", 0.5)
userdata.RegisterItem("Do some \\LUATEX ", 1.5)

userdata.Invoice()

\stopluacode

\stopletter

\stoptext



Greetlings from Lake Constance!
Hraban
---
http://www.fiee.net/texnique/
http://wiki.contextgarden.net
https://www.cacert.org (I'm an assurer)



More information about the ntg-context mailing list