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

<!-- Описание используемых стилей -->
<xsl:attribute-set name="title-style">
  <xsl:attribute name="style">margin:0cm; margin-bottom:.0001pt; font-size:12.0pt"; font-weight:bold; font-family:"Times New Roman";</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="line-style">
  <xsl:attribute name="style">margin:0cm; margin-bottom:.0001pt; font-size:11pt; font-family:"Times New Roman";</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="row-style">
  <xsl:attribute name="style">font-size:10pt; font-family:"Times New Roman";</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="data-style">
  <xsl:attribute name="style">font-size:10pt; font-family:"courier new,monospace"</xsl:attribute>
</xsl:attribute-set>

<xsl:decimal-format name="num-format" decimal-separator="." grouping-separator="," pattern-separator=";" />


<!-- Вывод даты в виде "dd.mm.yyyy tt" -->
<xsl:template name="FormatDateTime">
   <xsl:param name="DateTime"/>

   <xsl:variable name="date">
     <xsl:choose>
       <xsl:when test="contains($DateTime, 'T')">
         <xsl:value-of select="substring-before($DateTime, 'T')"/>
       </xsl:when>
       <xsl:otherwise>
         <xsl:value-of select="$DateTime"/>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:variable>

   <xsl:variable name="month_postfixYear" select="substring-after($date, '-')"/>
   <xsl:value-of select="substring-after($month_postfixYear, '-')"/> <xsl:text>.</xsl:text>
   <xsl:value-of select="substring-before($month_postfixYear, '-')"/> <xsl:text>.</xsl:text>
   <xsl:value-of select="substring-before($date, '-')"/> <xsl:text> </xsl:text>
   <xsl:value-of select="substring-after($DateTime, 'T')"/>
</xsl:template>

<!-- Вывод данных: число/строка -->
<xsl:template name="FormatData">
   <xsl:param name="Val"/>
   <xsl:param name="Fmt"/>
   <xsl:choose>
     <xsl:when test="$Fmt=''">
       <xsl:value-of select="$Val"/>
     </xsl:when>
     <xsl:when test="$Fmt='--'">
       <xsl:text>--</xsl:text>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="format-number($Val, $Fmt, 'num-format')"/>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<!-- Рамка для ячейки  -->
<xsl:template name="BorderStyle">
  <xsl:param name="Border"></xsl:param>
    <xsl:attribute name="style">

      <xsl:if test="$Border=''">border:solid 1.5pt;</xsl:if>

      <xsl:if test="$Border!=''">
        <xsl:text>border-top:</xsl:text>
        <xsl:choose>
          <xsl:when test="substring($Border, 1, 1)='2'">solid 1.5pt;</xsl:when>
          <xsl:when test="substring($Border, 1, 1)='1'">solid 0.5pt;</xsl:when>
          <xsl:otherwise>none;</xsl:otherwise>
        </xsl:choose>

        <xsl:text>border-bottom:</xsl:text>
        <xsl:choose>
          <xsl:when test="substring($Border, 2, 1)='2'">solid 1.5pt;</xsl:when>
          <xsl:when test="substring($Border, 2, 1)='1'">solid 0.5pt;</xsl:when>
          <xsl:otherwise>none;</xsl:otherwise>
        </xsl:choose>

        border-left:
        <xsl:choose>
          <xsl:when test="substring($Border, 3, 1)='2'">solid 1.5pt;</xsl:when>
          <xsl:when test="substring($Border, 3, 1)='1'">solid 0.5pt;</xsl:when>
          <xsl:otherwise>none;</xsl:otherwise>
        </xsl:choose>

        border-right:
        <xsl:choose>
          <xsl:when test="substring($Border, 4, 1)='2'">solid 1.5pt;</xsl:when>
          <xsl:when test="substring($Border, 4, 1)='1'">solid 0.5pt;</xsl:when>
          <xsl:otherwise>none;</xsl:otherwise>
        </xsl:choose>
      </xsl:if>
  </xsl:attribute>
</xsl:template>


<xsl:template match="/Reports">

  <xsl:for-each select="Report">
    <xsl:if test="position()>0">
      <p/> <p/>
    </xsl:if>

    <p xsl:use-attribute-sets="title-style"><b><xsl:value-of select="Title"/></b></p>
    <xsl:if test="@Type='ByReference'">
      <xsl:if test="Reference!=''">
        <p xsl:use-attribute-sets="line-style">Справочник: <xsl:value-of select="Reference"/></p>
      </xsl:if>
      <xsl:if test="Parameters[position()=2]!=''">
        <p xsl:use-attribute-sets="line-style">Параметры: <xsl:value-of select="Parameters"/></p>
      </xsl:if>
    </xsl:if>
    <xsl:if test="Period/@BegDate!=''">
      <p xsl:use-attribute-sets="line-style">За период c
        <xsl:call-template name="FormatDateTime">
          <xsl:with-param name="DateTime"><xsl:value-of select="Period/@BegDate"/></xsl:with-param>
        </xsl:call-template>
        по
        <xsl:call-template name="FormatDateTime">
          <xsl:with-param name="DateTime"><xsl:value-of select="Period/@EndDate"/></xsl:with-param>
        </xsl:call-template>
      </p>
    </xsl:if>

    <xsl:if test="Plan!=''">
      <p xsl:use-attribute-sets="line-style">План счетов: <xsl:value-of select="Plan"/></p>
    </xsl:if>
    <xsl:if test="Accounts!=''">
      <p xsl:use-attribute-sets="line-style">Cчета: <xsl:value-of select="Accounts"/></p>
    </xsl:if>

    <xsl:choose>
      <xsl:when test="@Type='ByReference' and Parameters[position()=2]!=''">
        <p xsl:use-attribute-sets="line-style">Параметры: <xsl:value-of select="Parameters[position()=2]"/></p>
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="Parameters!=''">
          <p xsl:use-attribute-sets="line-style">Параметры: <xsl:value-of select="Parameters"/></p>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>


    <xsl:for-each select="Table">
      <p></p>
      <xsl:if test="@Title!=''">
        <p xsl:use-attribute-sets="title-style"><xsl:value-of select="@Title"/></p>
      </xsl:if>

      <table cellSpacing="0" cellPadding="2" bordercolor="black" style="border-collapse:collapse;">

        <xsl:for-each select="Row">
          <xsl:variable name="CurrRow"><xsl:value-of select="@Type"/></xsl:variable>
          <xsl:variable name="IsHeaderRow" select="$CurrRow='SplitByCol' or $CurrRow='SumKind' or $CurrRow='DebCre' or $CurrRow='Indicator' "/>
          <xsl:variable name="IsTotalRow" select="contains($CurrRow,'Total')"/>

          <tr bgcolor="#F5F5F5" xsl:use-attribute-sets="row-style" >

            <xsl:if test="$IsHeaderRow or $IsTotalRow">
              <xsl:attribute name="bgcolor">#CCCCCC</xsl:attribute>
            </xsl:if>

            <xsl:for-each select="ColGroup">
              <xsl:variable name="CurrCol" select="@Type"/>

              <!-- колонки разбиения -->
              <xsl:if test="@Type='SplitByTab' or @Type='SplitByRow'">
                <xsl:for-each select="Cell">
                  <td>
                    <xsl:if test="not($IsHeaderRow or $IsTotalRow) or ($CurrCol='SplitByTab' and $CurrRow!='TotalReport' and not($IsHeaderRow))">
                      <xsl:attribute name="bgcolor">#FFFFE0</xsl:attribute>
                    </xsl:if>

                    <xsl:call-template name="BorderStyle">
                      <xsl:with-param name="Border"><xsl:value-of select="@Border"/></xsl:with-param>
                    </xsl:call-template>
                    <xsl:attribute name="align"><xsl:value-of select="@Align"/></xsl:attribute>

                    <xsl:choose>
                      <xsl:when test="@Span!=1">
                        <xsl:attribute name="colspan"><xsl:value-of select="@Span"/></xsl:attribute>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:attribute name="NOWRAP"/>
                      </xsl:otherwise>
                    </xsl:choose>
                    <xsl:value-of select="@Value"/>
                  </td>
                </xsl:for-each>
              </xsl:if>

              <!-- колонки данных -->
              <xsl:if test="@Type='Body'">
                <xsl:for-each select="Cell">
                  <td>
                    <!-- пример раскраски оборотов
                    <xsl:if test="not($IsHeaderRow or $IsTotalRow) and contains(@Type,'Turn')">
                      <xsl:attribute name="bgcolor">#FFFFE0</xsl:attribute>
                    </xsl:if>
                    -->

                    <xsl:call-template name="BorderStyle">
                      <xsl:with-param name="Border"><xsl:value-of select="@Border"/></xsl:with-param>
                    </xsl:call-template>
                    <xsl:attribute name="align"><xsl:value-of select="@Align"/></xsl:attribute>

                    <xsl:choose>
                      <xsl:when test="@Span!=1">
                        <xsl:attribute name="colspan"><xsl:value-of select="@Span"/></xsl:attribute>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:attribute name="NOWRAP"/>
                      </xsl:otherwise>
                    </xsl:choose>

                    <xsl:choose>
                      <xsl:when test="$IsHeaderRow">
                        <!-- шапка -->
                        <xsl:value-of select="@Value"/>
                      </xsl:when>
                      <xsl:when test="@Format!=''">
                        <!-- данные -->
                        <p xsl:use-attribute-sets="data-style">
                          <!-- <xsl:value-of select="format-number(@Value,@Format,'num-format')"/> -->
                          <xsl:call-template name="FormatData">
                            <xsl:with-param name="Val"><xsl:value-of select="@Value"/></xsl:with-param>
                            <xsl:with-param name="Fmt"><xsl:value-of select="@Format"/></xsl:with-param>
                          </xsl:call-template>
                        </p>
                      </xsl:when>
                      <xsl:otherwise>
                        <!-- данные -->
                        <p xsl:use-attribute-sets="data-style"><xsl:value-of select="@Value"/></p>
                      </xsl:otherwise>
                    </xsl:choose>
                  </td>
                </xsl:for-each> <!-- Cell -->
              </xsl:if>

            </xsl:for-each>  <!-- Column -->

          </tr>
        </xsl:for-each> <!-- Row -->

      </table>
    </xsl:for-each>   <!-- Table -->

  </xsl:for-each>   <!-- Report -->

</xsl:template>

</xsl:stylesheet>