<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/tg_invoice">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

      <fo:layout-master-set>
        <!-- Define all page masters here -->
        <fo:page-sequence-master master-name="contents_sequence">
          <!-- Define sequence. First page wil be "cover", next page will be "continuation" -->
          <fo:repeatable-page-master-alternatives>
            <!-- List of page masters that will be selected accoding the result of the page position trait -->
            <fo:conditional-page-master-reference page-position="only" master-reference="single"/>
            <fo:conditional-page-master-reference page-position="first" master-reference="cover"/>
            <fo:conditional-page-master-reference page-position="rest" master-reference="continuation"/>
            <fo:conditional-page-master-reference page-position="last" master-reference="last"/>
          </fo:repeatable-page-master-alternatives>
        </fo:page-sequence-master>

        <!-- Layout for single page invoices -->
        <fo:simple-page-master master-name="single" page-height="11in" page-width="8.5in" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm">
          <fo:region-body margin-top="9cm" margin-bottom="3cm"/>
          <fo:region-before region-name="header_first" extent="9cm"/>
          <fo:region-after region-name="footer_last" extent="3cm"/>
        </fo:simple-page-master>
        <!-- End -->
        
        <!-- Layout for first page -->
        <fo:simple-page-master master-name="cover" page-height="11in" page-width="8.5in" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm">
          <fo:region-body margin-top="9cm" margin-bottom="1cm"/>
          <fo:region-before region-name="header_first" extent="9cm"/>
          <fo:region-after region-name="footer_rest" extent="1cm"/>
        </fo:simple-page-master>
        <!-- End -->
        
        <!-- Layout for subsequent pages -->
        <fo:simple-page-master master-name="continuation" page-height="11in" page-width="8.5in" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm">
          <fo:region-body margin-top="3cm" margin-bottom="1cm"/>
          <fo:region-before region-name="header_rest" extent="3cm"/>
          <fo:region-after region-name="footer_rest" extent="1cm"/>
        </fo:simple-page-master>
        <!-- End -->
        
        <!-- Layout for last page -->
        <fo:simple-page-master master-name="last" page-height="11in" page-width="8.5in" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm">
          <fo:region-body margin-top="3cm" margin-bottom="3cm"/>
          <fo:region-before region-name="header_rest" extent="2cm"/>
          <fo:region-after region-name="footer_last" extent="3cm"/>
        </fo:simple-page-master>
        <!-- End -->       
      </fo:layout-master-set>
      <!-- End page masters -->
      
      <fo:page-sequence master-reference="contents_sequence" font-family="sans-serif" font-size="10pt">
        <!-- Page Content -->
        <fo:static-content flow-name="header_first">
          <!-- Content for first page header -->
          <fo:block font-size="18pt" font-weight="bold" text-align="center" letter-spacing="5pt">INVOICE</fo:block>
          <fo:block-container absolute-position="absolute" top="1cm" left="1cm" width="5cm" text-align="center">
            <fo:block>
              <fo:external-graphic src="url(Logo.gif)" content-width="5cm"/>
              <fo:block space-before="5pt">1234 Golf Rd,</fo:block>
              <fo:block>Chicago, IL 60601</fo:block>
              <fo:block>Tel: (847) 263 4000</fo:block>
              <fo:block>Fax: (847) 263 4100</fo:block>
            </fo:block>
          </fo:block-container>
          <fo:block-container absolute-position="absolute" top="1cm" left="13cm" width="5cm" text-align="center">
            <fo:block>
              <fo:table>
                <fo:table-column column-width="3cm"/>
                <fo:table-column column-width="2cm"/>
                <fo:table-body>
                  <fo:table-row>
                    <fo:table-cell>
                      <fo:block text-align="right">Invoice No&#160;-&#160;</fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                      <fo:block text-align="left"><xsl:value-of select="invoice_backing_sheet/invoice_no"/></fo:block>
                    </fo:table-cell>
                  </fo:table-row>
                  <fo:table-row>
                    <fo:table-cell>
                      <fo:block text-align="right">Invoice Date&#160;-&#160;</fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                      <fo:block text-align="left"><xsl:value-of select="invoice_backing_sheet/invoice_date"/></fo:block>
                    </fo:table-cell>
                  </fo:table-row>
                </fo:table-body>
              </fo:table>
            </fo:block>
          </fo:block-container>
          <!-- Bill To and Ship To -->
          <fo:block-container absolute-position="absolute" top="6.5cm" left="3cm" width="4cm" text-align="left">
            <fo:block>
              <fo:block font-weight="bold">Bill To:</fo:block>
              <fo:block>Realty Today</fo:block>
              <fo:block>123 Main St</fo:block>
              <fo:block>New York 12345</fo:block>
            </fo:block>
          </fo:block-container>
          <fo:block-container absolute-position="absolute" top="6.5cm" left="10cm" width="4cm" text-align="left">
            <fo:block>
              <fo:block font-weight="bold">Sold To:</fo:block>
              <fo:block>Realty Today</fo:block>
              <fo:block>123 Main St</fo:block>
              <fo:block>New York 12345</fo:block>
            </fo:block>
          </fo:block-container>
        </fo:static-content>
        <!-- Continuation Page Header -->
        <fo:static-content flow-name="header_rest">
          <fo:block-container absolute-position="absolute" top="0cm" left="1cm" width="5cm" text-align="center">
            <fo:block>
              <fo:external-graphic src="url(Logo.gif)" content-height="2cm"/>
            </fo:block>
          </fo:block-container>
          <!-- End content for first page header -->
        </fo:static-content>
        
        <fo:static-content flow-name="footer_rest" >
          <!-- Content for First Page Footer -->
          <fo:block font-size="8pt" margin-top="10px" text-align="right"> Page <fo:page-number/> of <fo:page-number-citation ref-id="last-page"/>
          </fo:block>
          <!-- End content for First Page Footer -->
        </fo:static-content>
        <fo:static-content flow-name="footer_last">
          <!-- Content for Last Page Footer -->
          <fo:table border-width="1pt" border-style="solid">
            <fo:table-column column-width="16%"/>
            <fo:table-column column-width="16%"/>
            <fo:table-column column-width="26.5%"/>
            <fo:table-column column-width="26.5%" border-left-style="solid" border-left-width="1pt"/>
            <fo:table-column column-width="15%" border-right-style="solid" border-right-width="1pt"/>
            <fo:table-header>
              <fo:table-cell height="20px" number-columns-spanned="5">
                <fo:block>&#160;</fo:block>
              </fo:table-cell>
            </fo:table-header>
            <!-- Totals and late payment tersms -->
            <fo:table-footer>
              <fo:table-row>
                <fo:table-cell number-rows-spanned="3" number-columns-spanned="3" padding="3pt">
                  <fo:block font-size="8pt">Open balances not paid within 30 days are subject to a 1 1/2% interest charge per month. In addition, purchaser shall be responsible for all costs of collections including reasonable attorney's fees and court costs</fo:block>
                </fo:table-cell>
                <fo:table-cell padding="3pt" font-weight="bold" border-top-width="1pt" border-top-style="solid" border-right-width="1pt" border-right-style="solid">
                  <fo:block>Total</fo:block>
                </fo:table-cell>
                <fo:table-cell padding="3pt" text-align="right" border-top-width="1pt" border-top-style="solid">
                  <fo:block>
                    <xsl:value-of select="format-number(sum(invoice_backing_sheet/invoice_line_item/line_total[text()]),'$#,###.##')"/>
                  </fo:block>
                </fo:table-cell>
              </fo:table-row>
              <fo:table-row>
                <fo:table-cell padding="3pt" border-top-width="1pt" border-top-style="solid" border-right-width="1pt" border-right-style="solid" border-left-width="1pt" border-left-style="solid">
                  <fo:block>PMT. REC</fo:block>
                </fo:table-cell>
                <fo:table-cell padding="3pt" text-align="right" border-top-width="1pt" border-top-style="solid">
                  <fo:block>
                    <xsl:value-of select="format-number(0,'$#,##0.00')"/>
                  </fo:block>
                </fo:table-cell>
              </fo:table-row>
              <fo:table-row>
                <fo:table-cell padding="3pt" font-weight="bold" border-top-width="1pt" border-top-style="solid" border-right-width="1pt" border-right-style="solid" border-left-width="1pt" border-left-style="solid">
                  <fo:block>BAL. DUE</fo:block>
                </fo:table-cell>
                <fo:table-cell padding="3pt" text-align="right" border-top-width="1pt" border-top-style="solid">
                  <fo:block>
                    <xsl:value-of select="format-number(sum(invoice_backing_sheet/invoice_line_item/line_total[text()]),'$#,###.##')"/>
                  </fo:block>
                </fo:table-cell>
              </fo:table-row>
            </fo:table-footer>
            <fo:table-body>
              <fo:table-cell>
                <fo:block/>
              </fo:table-cell>
              <fo:table-cell number-columns-spanned="3">
                <fo:block/>
              </fo:table-cell>
              <fo:table-cell>
                <fo:block/>
              </fo:table-cell>
            </fo:table-body>
          </fo:table>
          <fo:block font-size="8pt" margin-top="10px" text-align="right"> Page <fo:page-number/> of <fo:page-number-citation ref-id="last-page"/>
          </fo:block>
          <!-- End Content for Last Page Footer -->
        </fo:static-content>
        <!-- End header and footer -->

        <!-- Page Content -->
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates select="invoice_backing_sheet"/>
          <fo:block id="last-page"/>
        </fo:flow>
        <!-- End Page content -->
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="invoice_backing_sheet">
    <fo:block font-family="sans-serif" font-size="10pt" text-align="left" space-before="10px" space-after="10px" >
      <fo:table border-width="1pt" border-style="solid" border-collapse="collapse">
        <fo:table-column column-width="16%" border-right-width="1pt" border-right-style="solid"/>
        <fo:table-column column-width="16%" border-right-width="1pt" border-right-style="solid"/>
        <fo:table-column column-width="26.5%" border-right-width="1pt" border-right-style="solid"/>
        <fo:table-column column-width="26.5%" border-right-width="1pt" border-right-style="solid"/>
        <fo:table-column column-width="15%" border-right-width="1pt" border-right-style="solid"/>
        <fo:table-body>
          <fo:table-row font-weight="bold">
            <fo:table-cell padding="3pt">
              <fo:block>Job #</fo:block>
            </fo:table-cell>
            <fo:table-cell padding="3pt" number-columns-spanned="2">
              <fo:block>Title</fo:block>
            </fo:table-cell>
            <fo:table-cell padding="3pt">
              <fo:block>Terms</fo:block>
            </fo:table-cell>
            <fo:table-cell padding="3pt">
              <fo:block>PO Number</fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row border-top-width="1pt" border-top-style="solid">
            <fo:table-cell padding="3pt">
              <fo:block><xsl:value-of select="job_no"/></fo:block>
            </fo:table-cell>
            <fo:table-cell padding="3pt" number-columns-spanned="2">
              <fo:block><xsl:value-of select="job_title"/></fo:block>
            </fo:table-cell>
            <fo:table-cell padding="3pt">
              <fo:block><xsl:value-of select="cust_payment_terms"/></fo:block>
            </fo:table-cell>
            <fo:table-cell padding="3pt">
              <fo:block><xsl:value-of select="po_reference"/>
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
        </fo:table-body>
      </fo:table>

      <fo:table border-width="1pt" border-style="solid"  border-collapse="collapse">
        <!-- Order details and payment terms -->
        <fo:table-column column-width="16%" border-right-width="1pt" border-right-style="solid"/>
        <fo:table-column column-width="16%" border-right-width="1pt" border-right-style="solid"/>
        <fo:table-column column-width="26.5%" border-right-width="1pt" border-right-style="solid"/>
        <fo:table-column column-width="26.5%" border-right-width="1pt" border-right-style="solid"/>
        <fo:table-column column-width="15%" border-right-width="1pt" border-right-style="solid"/>
        <fo:table-header border-bottom-style="solid" border-bottom-width="1pt">
          <fo:table-cell padding="3pt" text-align="right">
            <fo:block>Qty</fo:block>
          </fo:table-cell>
          <fo:table-cell padding="3pt" number-columns-spanned="3">
            <fo:block>Description</fo:block>
          </fo:table-cell>
          <fo:table-cell padding="3pt" text-align="right">
             <fo:block>Price</fo:block>
          </fo:table-cell>
        </fo:table-header>
        <fo:table-footer>
          <fo:table-cell> 
            <fo:block/>
          </fo:table-cell>
          <fo:table-cell number-columns-spanned="3">
            <fo:block/>
          </fo:table-cell>
          <fo:table-cell>
            <fo:block/>
          </fo:table-cell>
        </fo:table-footer>
        <!-- Line Items -->
        <fo:table-body>
          <xsl:apply-templates select="invoice_line_item"/>
        </fo:table-body>
      </fo:table>
    </fo:block>
  </xsl:template>

  <xsl:template match="invoice_line_item">

    <fo:table-row>
      <fo:table-cell padding="3pt">
        <fo:block text-align="right">
          <xsl:value-of select="format-number(quantity,'#,###.##')"/>
        </fo:block>
      </fo:table-cell>
      <fo:table-cell padding="3pt" number-columns-spanned="3" >
        <fo:block text-align="left">
          <xsl:value-of select="description"/>
        </fo:block>
      </fo:table-cell>
      <fo:table-cell padding="3pt">
        <fo:block text-align="right">
          <xsl:value-of select="format-number(line_total, '#,###.00')"/>
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
  </xsl:template>
</xsl:stylesheet>
