<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:decimal-format name="european" decimal-separator="," grouping-separator="."/>

<xsl:output method="xml"
     doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
     doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

  <xsl:variable name="ascii"> !"#$%&amp;'()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:variable>
  <xsl:variable name="latin1"> ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆ&CCedil;ÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö&divid;øùúûüýþÿ</xsl:variable>

  <!-- Characters that usually don't need to be escaped -->
  <xsl:variable name="safe">!'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~</xsl:variable>

  <xsl:variable name="hex">0123456789ABCDEF</xsl:variable>
 
  <xsl:template match="invoice">

         <xsl:variable name="orderSubTotal">
            <xsl:call-template name="sumPositions">
              <xsl:with-param name="positions" select="positions"/>
              <xsl:with-param name="index" select="'1'"/>
              <xsl:with-param name="runningTotal" select="'0'"/>
            </xsl:call-template>
          </xsl:variable>

<html>
   <head>
     <title>Invoice Nr.: 2004-1100</title>
     <link rel="stylesheet" href="invoice.css" type="text/css" />
	 <style type="text/css">
        @media print {
            @page {
                size: <xsl:value-of select="orderer/media"/>;
                margin: 1cm;
            
            
                @bottom-center {
                  text-align: center;
                  font: 6pt Arial, Helvetica, sans-serif;
                  color: #7F7F7F;
                  content: "<xsl:value-of select="vendor/companyname"/> - <xsl:value-of select="vendor/register"/> - Geschaeftsführer: <xsl:value-of select="vendor/director"/>\A<xsl:value-of select="vendor/bankname"/> - BLZ: <xsl:value-of select="vendor/bankcode"/> - Kto.-Nr: <xsl:value-of select="vendor/bankaccno"/> - Swift Code: <xsl:value-of select="vendor/swiftbic"/> - USt.IdNr: <xsl:value-of select="vendor/vatid"/> - St-Nr: <xsl:value-of select="vendor/taxno"/>";
                  margin-bottom: 1cm;
                }
            
            }
        }
	</style>
    </head>
    <body>
    <div class="companyinformation">

<img>
  <xsl:attribute name="class">
    <xsl:value-of select="'logo'" />
  </xsl:attribute>
  <xsl:attribute name="alt">
    <xsl:value-of select="'Company Logo'" />
  </xsl:attribute>  
  <xsl:attribute name="src">
    <xsl:call-template name="url-encode">
      <xsl:with-param name="str" select="concat(vendor/companyname,'.png')"/>
    </xsl:call-template>
  </xsl:attribute>
</img>
     <br/>
     <xsl:value-of select="vendor/companyname"/><br />
     <xsl:value-of select="vendor/address"/><br />
     <xsl:value-of select="concat(vendor/zip,' ',vendor/city)"/><br />
     <xsl:value-of select="vendor/country"/><br />
     <br />
     Tel. <xsl:value-of select="vendor/tel"/><br />
     Fax  <xsl:value-of select="vendor/fax"/><br />
     E-Mail <xsl:value-of select="vendor/email"/><br />
    </div>

    <div class="addressline"> 
     <xsl:value-of select="vendor/companyname"/> - <xsl:value-of select="vendor/address"/> - <xsl:value-of select="concat(vendor/zip,' ',vendor/city)"/> - <xsl:value-of select="vendor/country"/>
    </div>

    <div class="recipient">
     <xsl:value-of select="orderer/companyname"/><br />
     Attn: <xsl:value-of select="orderer/personname"/><br />
         <xsl:for-each select="orderer/address">
           <xsl:value-of select="." /><br />
         </xsl:for-each>
     <xsl:value-of select="concat(orderer/zip,' ',orderer/city)"/><br />
     <xsl:value-of select="orderer/country"/><br />
    </div>

   <div class="descbox">
     <div class="descboxheader">
      Invoice
     </div>
     <table>
      <tr>
       <td>Process no.:</td> 
       <td><xsl:value-of select="voucher/@processno"/></td>
      </tr>
      <tr>
       <td>Voucher no.:</td> 
       <td><xsl:value-of select="voucher/@no"/></td>
      </tr>
      <tr>
        <td>Date:</td>
        <td><xsl:value-of select="voucher/@date"/></td>
     </tr>
      <tr>
       <td>Client reference no:</td>
       <td><xsl:value-of select="voucher/@clientrefid"/></td>
      </tr>
     </table>
    </div>

    <hr class="invisible" />
     <div class="infotable">
    <table class="infotable">
     <tr>
      <td>Valid until</td>
      <td></td>
      <td>Reference</td>
      <td><xsl:value-of select="voucher/@clientrefid"/></td>
      <td>Our VAT-ID no.</td>
      <td><xsl:value-of select="vendor/vatid"/></td>
     </tr>
     <tr>
      <td>Shipment</td>
      <td><xsl:value-of select="orderer/shipment"/></td>
      <td>Your sign</td>
      <td><xsl:value-of select="orderer/personname"/></td>
      <td>Our sign</td>
      <td><xsl:value-of select="vendor/sign"/></td>
     </tr>
     <tr>
      <td>Terms of shipment</td>
      <td>Standard</td>
      <td>Your voucher</td>
      <td><xsl:value-of select="voucher/@no"/></td>
      <td>Your VAT-ID no.</td>
      <td><xsl:value-of select="orderer/vatid"/></td>
     </tr>
    </table>
    </div>

     <table class="postable">
     <thead>
          <tr class="postheader">
           <td style="padding-right: 1.5mm">Pos.</td>
           <td style="padding-right: 1.5mm">Art.-No.</td>
           <td style="width:100%">Description</td>
           <td class="right">Qty. Unit</td>
           <td class="right">Unit Price</td>
           <td class="right">Total Price</td>
           <td style="text-align:right">TC</td>
          </tr>
      </thead>
      <tr class="puffer"><td colspan="7"></td></tr>
      <xsl:for-each select="positions/position">
      <tr>
       <td>
       <xsl:value-of select="position()"/>
       </td>
       <td><xsl:value-of select="khkid" /></td>
       <td>
         <xsl:value-of select="articletitle" />
         <xsl:for-each select="articledesc">
           <br /><xsl:value-of select="." />

         </xsl:for-each>
       </td>
       <td class="right"><xsl:value-of select="concat(qty,' ',unit)" /></td>
       <td class="right"><xsl:value-of select="format-number(unit_price, '#.###,00', 'european')" /></td>
       <td class="right"><xsl:value-of select="format-number((qty * unit_price), '#.###,00', 'european')" /></td>
       <td style="text-align:right">14</td>
      </tr>
      <tr class="puffer"><td colspan="7"></td></tr>
      </xsl:for-each>

      <tr class="postfooter">
       <td></td>
       <td></td>
       <td></td>
       <td class="right">Subtotal</td>
       <td class="right">EUR</td>
       <td class="subtotal">
          <xsl:value-of select="format-number($orderSubTotal, '#.###,00', 'european')"/>
       </td>
       <td style="text-align:right">TC</td>
      </tr>
      <tr class="puffer"><td colspan="7"></td></tr>
      <tr>
       <td></td>
       <td></td>
       <td>VAT with TC 14</td>
       <td class="right">16,00% of</td>
       <td class="right">
          <xsl:value-of select="format-number($orderSubTotal, '#.###,00', 'european')"/>
       </td>
       <td class="right"><xsl:value-of select="format-number(($orderSubTotal * 0.16), '#.###,00', 'european')"/></td>
       <td></td>
      </tr>
      <tr class="puffer"><td colspan="7"></td></tr>
      <tr class="postfooter">
       <td></td>
       <td></td>
       <td></td>
       <td class="right">Total</td>
       <td class="right">EUR</td>
       <td class="subtotal">
          <xsl:value-of select="format-number(($orderSubTotal * 1.16), '#.###,00', 'european')"/>
	  </td>
	  <td></td>
      </tr>
     </table>

     <p>The invoice amount must be paid with indication of the "Voucher no." by remittance to our bank account indicated below free of<br />
     bank charges for us. You may also send us a collection-only check in the currency mentioned above.</p>
     <p>Account Holder: <xsl:value-of select="vendor/companyname"/><br />
     Account Number: <xsl:value-of select="vendor/bankaccno"/><br />
     IBAN: <xsl:value-of select="vendor/iban"/><br />
     Bank Name: <xsl:value-of select="vendor/bankname"/><br />
     Bank Address: <xsl:value-of select="vendor/bankaddress"/><br />
     Bank SWIFT/BIC Code: <xsl:value-of select="vendor/swiftbic"/><br />
     Bank Code: <xsl:value-of select="vendor/bankcode"/></p>

     <p class="terms">Terms of payment:</p>
     <p>10 Days without deduction
          <xsl:value-of select="format-number(($orderSubTotal * 1.16), '#.###,00', 'european')"/>  EUR
	 </p>
   </body>
 </html>  
</xsl:template>
  
 <xsl:template name="sumPositions">
    <xsl:param name="positions"/>
    <xsl:param name="index" select="'1'"/>
    <xsl:param name="runningTotal" select="'0'"/>
    <xsl:variable name="currentPosition">
      <xsl:value-of select="$positions/position[$index]/qty * $positions/position[$index]/unit_price"/>
    </xsl:variable>
    <xsl:variable name="remainingpositions">
      <xsl:choose>
        <xsl:when test="$index=count($positions/position)">
          <xsl:text>0</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="sumPositions">
            <xsl:with-param name="positions" select="$positions"/>
            <xsl:with-param name="index" select="$index+1"/>
            <xsl:with-param name="runningTotal" select="$runningTotal+$currentPosition"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:value-of select="$currentPosition+$remainingpositions"/>
  </xsl:template>
  
  <xsl:template name="url-encode">
    <xsl:param name="str"/>   
    <xsl:if test="$str">
      <xsl:variable name="first-char" select="substring($str,1,1)"/>
      <xsl:choose>
        <xsl:when test="contains($safe,$first-char)">
          <xsl:value-of select="$first-char"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="codepoint">
            <xsl:choose>
              <xsl:when test="contains($ascii,$first-char)">
                <xsl:value-of select="string-length(substring-before($ascii,$first-char)) + 32"/>
              </xsl:when>
              <xsl:when test="contains($latin1,$first-char)">
                <xsl:value-of select="string-length(substring-before($latin1,$first-char)) + 160"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:message terminate="no">Warning: string contains a character that is out of range! Substituting "?".</xsl:message>
                <xsl:text>63</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
        <xsl:variable name="hex-digit1" select="substring($hex,floor($codepoint div 16) + 1,1)"/>
        <xsl:variable name="hex-digit2" select="substring($hex,$codepoint mod 16 + 1,1)"/>
        <xsl:value-of select="concat('%',$hex-digit1,$hex-digit2)"/>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="string-length($str) &gt; 1">
        <xsl:call-template name="url-encode">
          <xsl:with-param name="str" select="substring($str,2)"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:if>
  </xsl:template>
  
 </xsl:stylesheet>