<?xml version="1.0" encoding="Shift_JIS" ?>

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
>
<xsl:output encoding="Shift_JIS"/>


<xsl:template match="/">
<fo:root>
<xsl:apply-templates select="invoice" />
</fo:root>
</xsl:template>


<xsl:template match="invoice">
<fo:layout-master-set>
<fo:simple-page-master master-name="simple"
  page-height="297mm" 
  page-width="210mm"
  margin-top="1cm" 
  margin-bottom="2cm" 
  margin-left="2.5cm" 
  margin-right="2.5cm">
<fo:region-body margin-top="2cm" />
<fo:region-before extent="2cm" />
<fo:region-after extent="1.5cm" />
</fo:simple-page-master>
</fo:layout-master-set>


<fo:page-sequence master-reference="simple">
<fo:flow flow-name="xsl-region-body">

<fo:block
  font-size="18pt"
  font-family="Mincho"
  line-height="20pt"
  space-after.optimum="15pt"
  text-align="center"
  border-after-style="solid"
  border-after-width="medium"
  letter-spacing="1.5mm"
  padding="1mm">
請求書
</fo:block>


<fo:block-container
  position="absolute"
  top="18mm"
  left="0mm"
  width="85mm"
  height="60mm"
>

<fo:block 
  font-size="14pt"
  font-family="Mincho"
  border-after-style="solid"
  border-after-width="0.5pt"
><xsl:value-of select="to"/> 御中
</fo:block>

<fo:block
  font-size="12pt"
  font-family="Mincho"
  border-after-style="solid"
  border-after-width="0.5pt"
  space-before="5mm"
>
件名：<xsl:value-of select="subject"/>
</fo:block>

<fo:block
  font-size="10pt"
  font-family="Mincho"
  space-before="10mm"
>
下記の通り請求させていただきます。
</fo:block>

<fo:block
  space-before="5mm"
>
<fo:table
  table-layout="fixed"
  width="60mm"
>
<fo:table-column column-number="1" column-width="45mm" /> 
<fo:table-body>

<fo:table-row>
<fo:table-cell
  border-style="solid"
  border-width="0.5pt">
<fo:block
  font-size="10pt"
  font-family="Mincho"
  text-align="center"
  padding="1mm"
>請求金額</fo:block> 
</fo:table-cell>
</fo:table-row>

<fo:table-row>
<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="15pt"
  font-family="Gothic"
  text-align="center"
  padding="1mm"
>￥<xsl:call-template name="sumfee" />-</fo:block> 
</fo:table-cell>
</fo:table-row>

</fo:table-body>
</fo:table>
</fo:block>
</fo:block-container>



<fo:block-container
  position="absolute"
  text-align="right"
  top="15mm"
  left="0mm"
  width="162mm"
  height="60mm">
<fo:block
  font-size="10pt"
  font-family="Mincho">
<xsl:apply-templates select="date"/>
</fo:block>


<fo:block
  font-size="13pt"
  font-family="Mincho"
  margin-top="2cm"
  space-before="25mm"
>
名前
</fo:block>

<fo:block
  font-size="10pt"
  font-family="Mincho"
  space-before="2mm"
>
東京都渋谷区渋谷1-1-1
</fo:block>

<fo:block
  font-size="10pt"
  font-family="Mincho">
渋谷タワー111
</fo:block>

<fo:block
  font-size="9pt"
  font-family="Mincho"
  space-before="2mm"
>
Tel: 01-2345-6789
</fo:block>

<fo:block
  font-size="9pt"
  font-family="Mincho"
>
Mail: mail@address.com
</fo:block>

</fo:block-container>


<fo:block-container
  position="absolute"
  top="85mm"
  left="0mm"
  width="170mm"
  height="180mm"
>

<fo:block
  space-before="5mm"
>
<fo:table
  table-layout="fixed"
>
<fo:table-column column-number="1" column-width="15mm" />
<fo:table-column column-number="2" column-width="70mm" />
<fo:table-column column-number="3" column-width="30mm" />
<fo:table-column column-number="4" column-width="15mm" />
<fo:table-column column-number="5" column-width="30mm" />
<fo:table-body>

<fo:table-row>

<fo:table-cell
  border-style="solid"
  border-width="0.5pt">
<fo:block
  font-size="10pt"
  font-family="Mincho"
  text-align="center"
  padding="1mm"
>No.</fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt">
<fo:block
  font-size="10pt"
  font-family="Mincho"
  text-align="center"
  padding="1mm"
>項目</fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt">
<fo:block
  font-size="10pt"
  font-family="Mincho"
  text-align="center"
  padding="1mm"
>単価</fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt">
<fo:block
  font-size="10pt"
  font-family="Mincho"
  text-align="center"
  padding="1mm"
>数量</fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt">
<fo:block
  font-size="10pt"
  font-family="Mincho"
  text-align="center"
  padding="1mm"
>金額</fo:block> 
</fo:table-cell>


</fo:table-row>

<xsl:call-template name="items-loop">
<xsl:with-param name="i" select="15" />
<xsl:with-param name="m" select="15" />
</xsl:call-template>


<fo:table-row>

<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="10pt"
  font-family="Gothic"
  text-align="center"
  padding="1mm"
>計</fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="10pt"
  font-family="Mincho"
  text-align="center"
  padding="1mm"
></fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="10pt"
  font-family="Gothic"
  text-align="center"
  padding="1mm"
></fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="10pt"
  font-family="Gothic"
  text-align="center"
  padding="1mm"
></fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="10pt"
  font-family="Gothic"
  text-align="right"
  padding="1mm"
  margin-right="2mm"
><xsl:call-template name="sumfee" /></fo:block> 
</fo:table-cell>


</fo:table-row>

</fo:table-body>
</fo:table>

</fo:block>
</fo:block-container>



<fo:block-container position="absolute"
  top="210mm" left="10mm" width="140mm" height="100mm"
>
<fo:block border-style="solid" border-width="thin"
  font-family="Gothic" font-size="10pt" padding="3mm" width="100mm"
>
<fo:block>お振込先</fo:block> 
<fo:block>子供銀行 渋谷支店（店番号123）</fo:block>
<fo:block>普通 12345678 苗字名前</fo:block>
</fo:block>
</fo:block-container>


    </fo:flow>
  </fo:page-sequence>

</xsl:template>


<xsl:template match="date">
<xsl:value-of select="@year" />年<xsl:value-of select="@month" />月<xsl:value-of select="@date" />日
</xsl:template>


<xsl:template name="items-loop">
<xsl:param name="i" />
<xsl:param name="m" />
<xsl:param name="pos" select="$m - $i + 1"/>
<xsl:if test="$i &gt; 0">

<fo:table-row>
<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="10pt"
  font-family="Gothic"
  text-align="center"
  padding="1mm"
><xsl:value-of select="$pos" /></fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="10pt"
  font-family="Mincho"
  text-align="left"
  padding="1mm"
  margin-left="2mm"
><xsl:value-of select="item[$pos]/name" /></fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="10pt"
  font-family="Gothic"
  text-align="right"
  padding="1mm"
  margin-right="2mm"
>
<xsl:if test="item[$pos]">
<xsl:value-of select="format-number(item[$pos]/fee, '#,##0')" />
</xsl:if>
</fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="10pt"
  font-family="Gothic"
  text-align="right"
  padding="1mm"
  margin-right="2mm"
><xsl:value-of select="item[$pos]/amount" /></fo:block> 
</fo:table-cell>


<fo:table-cell
  border-style="solid"
  border-width="0.5pt"
>
<fo:block
  font-size="10pt"
  font-family="Gothic"
  text-align="right"
  padding="1mm"
  margin-right="2mm"
>
<xsl:if test="item[$pos]">
<xsl:value-of select="format-number(item[$pos]/amount * item[$pos]/fee, '#,##0')" />
</xsl:if>
</fo:block> 
</fo:table-cell>

</fo:table-row>

<xsl:call-template name="items-loop">
<xsl:with-param name="i" select="$i - 1" />
<xsl:with-param name="m" select="$m" />
</xsl:call-template>
</xsl:if>
</xsl:template>


<xsl:template name="sumfee">
  <xsl:param name="items" select="item" />
  <xsl:param name="subtotal" select="0" />
  <xsl:choose>
    <xsl:when test="$items">
      <xsl:call-template name="sumfee">
        <xsl:with-param name="items"
                        select="$items[position() > 1]" />
        <xsl:with-param name="subtotal"
          select="$subtotal + $items[1]/amount * $items[1]/fee" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="format-number($subtotal, '#,##0')" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
  

</xsl:transform>
