bug 9505 refactor loops in invoices.pl