{{script}}
from fixedpoint import FixedPoint as fp
VAT = lambda x, fp=fp: fp(x*fp(0.175,3),2)
FP = lambda x, fp=fp: fp(x,2)
INVOICES = data._namedChildren('invoice')
ITEMS = data.invoice.body._namedChildren('item')

TOTAL = FP(0)
for item in ITEMS:
	TOTAL = TOTAL + FP(item.price) * FP(item.units)
GROSS = TOTAL+VAT(TOTAL)

ROW_HEIGHT = 20
PAGE_WIDTH = 595
PAGE_HEIGHT = 842
TOP_SPACER = 60
BODY_SPACER = 40
BOTTOM_SPACER=60

{{endscript}}
<!DOCTYPE document SYSTEM "rml_1_0.dtd">
<document filename="invoice.pdf">

<template pageSize="(595, 842)" leftMargin="0" rightMargin="0" showBoundary="0">
	<pageTemplate id="main">
	<frame id="first" x1="20" y1="20" width="{{PAGE_WIDTH-40}}" height="{{PAGE_HEIGHT-40}}"/>
	</pageTemplate>
</template>

<stylesheet>
	<initialize>
		<alias id="style.normal" value="style.Normal"/>
	</initialize>
	<paraStyle name="normal" fontName="Helvetica" fontSize="10" spaceBefore="0" spaceAfter="0"/>
	<paraStyle name="bulletList" parent="style.normal" fontName="Helvetica" bulletFontName="ZapfDingbats" bulletFontSize="5" bulletIndent="20" leftIndent="35" spaceAfter="6"/>
	<paraStyle name="h1" fontName="Helvetica-BoldOblique" fontSize="12" leading="16.8" spaceBefore="0" spaceAfter="0"/>
	<paraStyle name="h2" fontName="Helvetica-BoldOblique" fontSize="12" textColor="red" spaceBefore="0" spaceAfter="0"/>
	<paraStyle name="h3" fontName="Helvetica-Bold" fontSize="10" textColor="gray" spaceBefore="0" spaceAfter="0"/>

	<blockTableStyle id="logoTableStyle">
	<blockAlignment value="CENTER"/>
	<blockValign value="MIDDLE"/>
	</blockTableStyle>

	<blockTableStyle id="holderTableStyle" >
	<!-- <blockTopPadding length="90" /> -->
	<blockFont name="Helvetica-BoldOblique" size="12"/>
	<blockTextColor colorName="black"/>
	<!--lineStyle kind="GRID" colorName="gray"/-->
	<blockValign value="BOTTOM"/>
	<blockBottomPadding length="0"/>
	<blockTopPadding length="0"/>
	<blockLeading length="16.8"/>
	</blockTableStyle>

	<blockTableStyle id="headerBodyTableStyle">
	<blockFont name="Helvetica-BoldOblique" size="12"/>
	<blockBottomPadding length="0"/>
	<blockAlignment value="RIGHT" start="-1,0" stop="-1,0"/>
	<blockTextColor colorName="black"/>
	<blockBackground colorName="cornflower"/>
	<lineStyle kind="GRID" colorName="gray"/>
	</blockTableStyle>

	<blockTableStyle id="rowBodyTableStyle">
	<blockFont name="Courier-Bold" size="12"/>
	<blockBottomPadding length="0"/>
	<blockAlignment value="RIGHT" start="-1,0" stop="-1,0"/>
	<blockTextColor colorName="black"/>
	<lineStyle kind="GRID" colorName="gray"/>
	</blockTableStyle>

	<blockTableStyle id="totalBodyTableStyle">
	<blockFont name="Courier-Bold" size="12"/>
	<blockBottomPadding length="0"/>
	<blockAlignment value="RIGHT"/>
	<blockTextColor colorName="black"/>
	<lineStyle kind="GRID" colorName="gray" start="1,0" stop="-1,-1"/>
	</blockTableStyle>
</stylesheet>


<story>
	{{for invoice in INVOICES}}

		<blockTable style="logoTableStyle" rowHeights="100" colWidths="180,150,180">
			<tr><td><pre style="h3">
ReportLab Europe Ltd.
Media House
3 Palmerston Road
Wimbledon
London SW19 1PG
Tel +44 (0)20 8545 1570
Fax +44 (0)20 8545 1571
VAT Reg No 707 3959 12</pre>
			</td>
			<td>
			<drawing module="rl_logo" function="RL_LOGO">
			<param name="height">{{86*0.9}}</param>
			<param name="width">{{130*0.9}}</param>
			</drawing>
			</td>
			<td>
			</td></tr>
		</blockTable>

		<spacer length="{{BODY_SPACER}}" />

		<blockTable style="holderTableStyle" rowHeights="20,20,20,20" colWidths="35,240,80,105">
		  <tr><td><para style="h1" textColor="cornflower">To:</para></td> <td><para style="h1">{{invoice.payee}}</para> </td><td><para style="h1" textColor="cornflower">Date: </para></td><td><para style="h1"> {{invoice.date}}</para></td></tr>
		  <tr><td> </td> <td><para style="h1">{{invoice.address.line1}}</para> </td> <td><para style="h1" textColor="cornflower">Number: </para></td><td><para style="h1">{{invoice.number}}</para> </td></tr>
		  <tr><td> </td> <td><para style="h1">{{invoice.address.line2}}</para> </td> <td><para style="h1" textColor="cornflower">Ref: </para></td><td><para style="h1"> {{invoice.ref}}</para> </td></tr>
		  <tr><td> </td> <td><para style="h1">{{invoice.address.line3}}</para> </td> <td><para style="h1" textColor="cornflower">Terms: </para></td><td><para style="h1">{{invoice.terms}}</para> </td></tr>
		</blockTable>

		<spacer length="{{BODY_SPACER}}" />

		<blockTable style="headerBodyTableStyle" rowHeights="20" colWidths="40,250,80,80">
			<tr><td>Units</td> <td>Description</td><td>Unit Price</td> <td>Price</td></tr>
		</blockTable>

			{{script}}
			curr_pos = 100+BODY_SPACER+(4*ROW_HEIGHT)+BODY_SPACER+ROW_HEIGHT
			runningTotal = 0.0
			{{endscript}}
		  {{for item in ITEMS}}
			{{if curr_pos + 2*ROW_HEIGHT < (PAGE_HEIGHT-BOTTOM_SPACER)}}
				<blockTable style="rowBodyTableStyle" rowHeights="20" colWidths="40,250,80,80">
					<tr><td>{{item.units}}</td> <td>{{curr_pos}} {{item.description}}</td> <td>{{item.price}}</td> <td>{{FP((FP(float(item.price))*FP(float(item.units))))}}</td></tr>
				</blockTable>
			{{else}}
				<blockTable style="totalBodyTableStyle" rowHeights="20" colWidths="67,287,93" >
				<tr><td></td> <td>Running Total</td> <td>{{runningTotal}}</td></tr>
				</blockTable>
				<blockTable style="holderTableStyle" colWidths="{{PAGE_WIDTH-80}}, 40">
					<tr><td></td><td>cont..</td></tr>
				</blockTable>
				<nextPage/>
				<nextFrame/>
				<spacer length="{{BODY_SPACER}}" />
				{{script}}curr_pos=BODY_SPACER{{endscript}}
				<blockTable style="headerBodyTableStyle" rowHeights="20" colWidths="40,250,80,80">
					<tr><td>Units</td> <td>Description</td><td>Unit Price</td> <td>Price</td></tr>
				</blockTable>
			{{endif}}
				{{script}}
				runningTotal+=float(item.price)
				curr_pos+=20
				{{endscript}}
		  {{endfor}}
		  {{if curr_pos + 4*ROW_HEIGHT >= (PAGE_HEIGHT-BOTTOM_SPACER)}}

			<blockTable style="totalBodyTableStyle" rowHeights="20" colWidths="67,287,93" >
				<tr><td></td> <td>Running Total</td> <td>{{runningTotal}}</td></tr>
			</blockTable>
			<blockTable style="holderTableStyle" colWidths="{{PAGE_WIDTH-80}}, 40">
				<tr><td></td><td>cont..</td></tr>
			</blockTable>
			<nextPage/>
			<nextFrame/>
			<spacer length="{{BODY_SPACER}}" />
			{{script}}curr_pos=BODY_SPACER{{endscript}}
			<blockTable style="headerBodyTableStyle" rowHeights="20" colWidths="40,250,80,80">
				<tr><td>..cont</td> <td>Description</td> <td>Unit Price</td><td>Price</td></tr>
			</blockTable>
		  {{endif}}

			<blockTable style="totalBodyTableStyle" rowHeights="20" colWidths="40,330,80" >
			<tr><td></td> <td>Net Total</td> <td>{{TOTAL}}</td></tr>
			</blockTable>
			<blockTable style="totalBodyTableStyle" rowHeights="20" colWidths="40,330,80" >
			<tr><td></td> <td>VAT (17.5%)</td> <td>{{VAT(TOTAL)}}</td></tr>
			</blockTable>
			<blockTable style="totalBodyTableStyle" rowHeights="20" colWidths="40,330,80" >
			<tr><td></td> <td>Gross</td> <td>{{GROSS}}</td></tr>
			</blockTable>

			{{script}}
			curr_pos+=3*ROW_HEIGHT
			{{endscript}}

			{{if curr_pos + BODY_SPACER + ROW_HEIGHT > (PAGE_HEIGHT-BOTTOM_SPACER)}}
				<blockTable style="holderTableStyle" colWidths="{{PAGE_WIDTH-80}}, 40">
					<tr><td></td><td>cont..</td></tr>
				</blockTable>
				<nextPage/>
				<nextFrame/>
				<spacer length="{{TOP_SPACER}}" />
				{{script}}
					curr_pos=TOP_SPACER
				{{endscript}}
			{{else}}
				<spacer length="{{BODY_SPACER}}" />
				{{script}}
					curr_pos+=BODY_SPACER
				{{endscript}}
			{{endif}}

		  {{for note in invoice.notes._namedChildren('note')}}
			{{if curr_pos + ROW_HEIGHT > (PAGE_HEIGHT-BOTTOM_SPACER)}}
				<blockTable style="holderTableStyle" colWidths="{{PAGE_WIDTH-80}}, 40">
					<tr><td></td><td>cont..</td></tr>
				</blockTable>
				<nextPage/>
				<nextFrame/>
				<spacer length="{{TOP_SPACER}}" />
				{{script}}
					curr_pos=TOP_SPACER
				{{endscript}}
			{{endif}}
			<blockTable style="holderTableStyle" colWidths="{{PAGE_WIDTH-40}}">
			<tr><td><para style="bulletList" bulletText="l">{{note}}</para></td></tr>
			</blockTable>
			{{script}}
				curr_pos+=ROW_HEIGHT
			{{endscript}}
		  {{endfor}}


		{{if invoice is not INVOICES[-1]}}
			<nextPage/>
			<nextFrame/>
		{{endif}}


	{{endfor}}
</story>
</document>
