Finishing up on this series we will now add a page total and a region that needs to show only on the last page of the document.
Page Totals - as many of you know we have direct support for page totals. You register a page total object in the template and then place it where its required to be rendered. The total is not actually calculated until the rendering engine starts to layout the data on the page, thats the only point at which the engine knows how many 'line' can fit on the page. In our case we know how many lines we are going to fit because we have fixed the number of rows before the rendering engine starts. We also already have those lines loaded into a variable 'invLines' so calculating a total is going to be relatively straightforward.
<?sum($invLines[(position()>=$start) and (position()<($start+$lpp))]/LINE_EXTENDED_AMOUNT)?>
Here's an XPATH expression again inside the '[ ]', this limits the number of rows we want to sum. it's like a 'where' clause on our data.
If we wanted the word 'Continued ...' to be shown we test the current record position against the total number of lines for the invoice. If they were not equal then we would show the 'Continued ...' string.
Last Page Only - again we now have native support for this when you do not know how much data is going to fit on a page with 5.6.2. In this case thou, we know how much data is going on the page and can ensure that a section on prints on the last page. Take a look a the template, there is a table to hold the summary/special instructions for the invoice at the end of the template. All we have done is wrap an if condition around that table:
<?if:count($invLines) < $start+$lpp?>
This checks to see if we have reached the end of the invoice lines section and if so then render the summary section i.e. on the last page. Then we close out the if condition. To get the table to appear at the bottom of the last page you'll have notived that we created a two celled table and put the line table in the top cell then the summary in the lower one and then drageed the cell border down the page to position the summary at the bottom of the page.
We now have an all singling all dancing invoice template, the features we added were:
1. A fixed number of rows per page,
2. 'Filler' space for lines, because that last page may only have 3 lines but you want to maintain the layout
3. Page totals or 'Continued' in place of a total
4. Last page only content
5. Header and page number resetting as we hit each new invoice in the batch.
The final template, data and output are available here.
We can now take some of those individual features and apply them to pretty much any other document whether than be a batch of purchase orders or a single standard letter showing outstanding invoices. Hope it's been helpful.
Comments (2)
This is fantastic. I very much appreciate this article. It has explained a lot. Question: How would this template be modified to place the Totals at the bottom of the page and allow any number of lines based on the remaining space between the Header and Section footer? All your help is greatly appreciated.
Regards,
Donn Shumway
Posted by Donn Shumway | April 10, 2007 1:49 PM
Posted on April 10, 2007 13:49
Good post. Any ideas on how you would combine this with grouping? For instance, if my invoice has a line type of PARTS and a line type of LABOR?
I want to use the template's features (especially the lines per page and last page only) but I also want to do a group by as I go through the lines.
For instance, I want to show all of the PARTS lines first, then all of the LABOR lines.
You used a for-each:$invLines.
In my example, I am wondering how I reconcile the $invLines to something like:
for-each-group: Lines; Expenditure_Category
Have you built a template that does some grouping as well?
Posted by Dave Bayard | April 10, 2007 3:23 PM
Posted on April 10, 2007 15:23