<?xml version="1.0" encoding="windows-1251" ?>
<xsl:stylesheet version="1.0" 
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:pfr="http://schema.pfr.ru">

<!-- Âûâîä â xml-ôàéë ñ WIN-êîäèðîâêîé -->
<xsl:output method="xml" version="1.0" encoding="windows-1251" />
<xsl:decimal-format decimal-separator="."/>
<xsl:param name = "ReestrSource" select = "'ReportParams'"/>

<!-- Êëþ÷ äëÿ ïåðèîäà: Ãîä+Êâàðòàë -->
<xsl:key name="KeyPeriod" match="InfoAboutCorr" use="concat(pfr:Ãîä,pfr:Êâàðòàë)"/>
<!-- Êëþ÷ äëÿ ïåðèîäà: Ãîä+Êâàðòàë (èç àòðèáóòîâ) -->
<xsl:key name="KeyPeriodA" match="pfr:ÐååñòðÏîÐàçäåëàì6_6" use="concat(pfr:Period/@Year,pfr:Period/@Cod)"/>

<!-- Âõîäíàÿ èíôîðìàöèÿ î ôàéëàõ ÏÔÐ ïðèâîäèòñÿ ê åäèíîìó ôîðìàòó è ñîõðàíÿåòñÿ â ïåðåìåííîé
     Reports --> 
<xsl:variable name="Reports">
   <xsl:element name="ReportList">
	<xsl:choose>
		<xsl:when test="$ReestrSource='ReportParams'">
      <!--   <xsl:for-each select="document('db_main.xml')//ReportList/anyType">-->
         <xsl:for-each select=".//Ôàéë">
<!--            <xsl:if test="@ÒèïÔîðìû!='Rsv1_15'">-->
               <xsl:element name="File">
                  <xsl:attribute name="FullName">
                     <xsl:value-of select="@ÏîëíîåÈìÿ"/>
                  </xsl:attribute>
<!--
                  <xsl:attribute name="Name">
                     <xsl:value-of select="@Èìÿ"/>
                  </xsl:attribute>
-->
                  <xsl:attribute name="Month">
                     <xsl:value-of select="@ÊîäÏåðèîäà"/>
                  </xsl:attribute>
                  <xsl:attribute name="Year">
                     <xsl:value-of select="@Ãîä"/>
                  </xsl:attribute>
                  <xsl:attribute name="Type">
                     <xsl:value-of select="@ÒèïÑâåäåíèé"/>
                  </xsl:attribute>
               </xsl:element>
<!--            </xsl:if> -->
         </xsl:for-each>
		</xsl:when>
		<xsl:otherwise>
         <xsl:for-each select=".//Ôàéë">
               <xsl:element name="File">
                  <xsl:attribute name="FullName">
                     <xsl:value-of select="@ÏîëíîåÈìÿ"/>
                  </xsl:attribute>
<!--
                  <xsl:attribute name="Name">
                     <xsl:value-of select="@Èìÿ"/>
                  </xsl:attribute>
-->
                   <xsl:for-each select="document(@ÏîëíîåÈìÿ)/pfr:ÔàéëÏÔÐ/pfr:Ïà÷êàÂõîäÿùèõÄîêóìåíòîâ">
                      <xsl:attribute name="Month">
                         <xsl:value-of select=".//pfr:Îò÷åòíûéÏåðèîä/pfr:Êâàðòàë"/>
                      </xsl:attribute>
                      <xsl:attribute name="Year">
                         <xsl:value-of select=".//pfr:Îò÷åòíûéÏåðèîä/pfr:Ãîä"/>
                      </xsl:attribute>
                      <xsl:attribute name="Type">
                         <xsl:value-of select=".//pfr:ÒèïÑâåäåíèé"/>
                      </xsl:attribute>
                   </xsl:for-each>
                </xsl:element>
         </xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
   </xsl:element>
</xsl:variable>
<!-- Â ïåðåìåííóþ AllOriginal66 ñíà÷àëà ñ÷èòûâàþòñÿ âñå ýëåìåíòû
     "ÑâåäåíèÿÎêîððåêòèðîâêàõ" èç âñåõ ôàéëîâ ñ èñõîäíûìè ñâåäåíèÿìè
     + ñîðòèðóþòñÿ ïî Ãîä+Êâàðòàë
-->
<xsl:variable name="AllOriginal66">
   <xsl:for-each select="document(msxsl:node-set($Reports)//ReportList/File[@Type='ÈÑÕÎÄÍÀß']/@FullName)/pfr:ÔàéëÏÔÐ//pfr:ÑÂÅÄÅÍÈß_Î_ÑÓÌÌÅ_ÂÛÏËÀÒ_È_ÑÒÐÀÕÎÂÎÌ_ÑÒÀÆÅ_ÇË//pfr:ÑâåäåíèÿÎêîððåêòèðîâêàõ[pfr:ÒèïÑòðîêè='ÌÅÑÖ']">
<!--      <xsl:sort select="concat(Ãîä,Êâàðòàë)" />-->
      <xsl:element name="InfoAboutCorr">
         <xsl:copy-of select="ancestor-or-self::*/pfr:ÑòðàõîâîéÍîìåð" />
         <xsl:copy-of select="*"/>
      </xsl:element>
   </xsl:for-each>
</xsl:variable>

<!-- Â ïåðåìåííóþ Reestr66 äåëàåòñÿ âûáîðêà âñåõ èìåþùèõñÿ ðàçäåëîâ 6.6 ñ ãðóïïèðîâêîé ïî 
     êîððåêòèðóåìûì ïåðèîäàì
-->
<xsl:variable name="Reestr66">
      <!-- Áåæèì ïî âñåì ýëåìåíòàì ÿâëÿþùèõñÿ "ïåðâûìè" ñ òî÷êè çðåíèÿ êëþ÷à KeyPeriod  -->
      <xsl:for-each select="msxsl:node-set($AllOriginal66)//InfoAboutCorr[generate-id()=generate-id(key('KeyPeriod',concat(pfr:Ãîä,pfr:Êâàðòàë))[1])]">
         <xsl:element name="Period">
            <xsl:attribute name="Cod">
               <xsl:value-of select="normalize-space(pfr:Êâàðòàë)"/>
            </xsl:attribute>
            <xsl:attribute name="Year">
               <xsl:value-of select="normalize-space(pfr:Ãîä)"/>
            </xsl:attribute>
            <!-- Âûáèðàåì âñå ýëåìåíòû ñ òàêèì æå Ãîäîì è Êâàðòàëîì 
                 È îñóùåñòâëÿåì èõ ïåðåáîð (îòñîðòèðîâàâ ïî ñòðàõîâîìó íîìåðó)
            -->
            <xsl:variable name="PeriodsCurrentYear"
                 select="key('KeyPeriod',concat(pfr:Ãîä,pfr:Êâàðòàë))"/>
            <xsl:for-each select="$PeriodsCurrentYear">
               <xsl:sort select=".//pfr:ÑòðàõîâîéÍîìåð" />
               <xsl:element name="Person">
                     <xsl:element name="Snils">
                        <xsl:value-of select=".//pfr:ÑòðàõîâîéÍîìåð" />
                     </xsl:element>
                     <xsl:if test=".//pfr:ÑóììàÄîíà÷èñëåííûõÂçíîñîâÎÏÑ">
                        <xsl:element name="DeltaOPS">
                           <xsl:value-of select=".//pfr:ÑóììàÄîíà÷èñëåííûõÂçíîñîâÎÏÑ" />
                        </xsl:element>
                     </xsl:if>
                     <xsl:if test=".//pfr:ÑóììàÄîíà÷èñëåííûõÂçíîñîâÍàÑòðàõîâóþ">
                        <xsl:element name="DeltaS">
                           <xsl:value-of select=".//pfr:ÑóììàÄîíà÷èñëåííûõÂçíîñîâÍàÑòðàõîâóþ"/>
                        </xsl:element>
                     </xsl:if>
                     <xsl:if test=".//pfr:ÑóììàÄîíà÷èñëåííûõÂçíîñîâÍàÍàêîïèòåëüíóþ">
                        <xsl:element name="DeltaN">
                           <xsl:value-of select=".//pfr:ÑóììàÄîíà÷èñëåííûõÂçíîñîâÍàÍàêîïèòåëüíóþ"/>
                        </xsl:element>
                     </xsl:if>
               </xsl:element>
           </xsl:for-each>
         </xsl:element>
      </xsl:for-each>
</xsl:variable>

<!-- Â ïåðåìåííóþ ReestrCorrective ñ÷èòûâàþòñÿ âñå ñòðàõîâûå íîìåðà, êîòîðûå ãðóïïèðóþòñÿ
     ïî èìåíè ôàéëà êîððåêòèðóþùèõ ñâåäåíèé
-->
<xsl:variable name="ReestrCorrective">
   <xsl:for-each select="document(msxsl:node-set($Reports)//ReportList/File[@Type='ÊÎÐÐÅÊÒÈÐÓÞÙÀß' or @Type='ÎÒÌÅÍßÞÙÀß']/@FullName)//pfr:ÔàéëÏÔÐ">
      <xsl:sort select=".//pfr:ÈìÿÔàéëà" />
      <xsl:element name="File">
         <xsl:attribute name="Name">
            <xsl:value-of select="normalize-space(.//pfr:ÈìÿÔàéëà)"/>
         </xsl:attribute>
         <xsl:attribute name="CountPerson">
      	   <xsl:choose>
      		   <xsl:when test=".//pfr:ÂÕÎÄßÙÀß_ÎÏÈÑÜ_ÏÎ_ÑÓÌÌÀÌ_ÂÛÏËÀÒ_È_ÏÎ_ÑÒÐÀÕÎÂÛÌ_ÂÇÍÎÑÀÌ//pfr:ÑîñòàâÄîêóìåíòîâ//pfr:Íàëè÷èåÄîêóìåíòîâ//pfr:Êîëè÷åñòâî">
                  <xsl:value-of select=".//pfr:ÂÕÎÄßÙÀß_ÎÏÈÑÜ_ÏÎ_ÑÓÌÌÀÌ_ÂÛÏËÀÒ_È_ÏÎ_ÑÒÐÀÕÎÂÛÌ_ÂÇÍÎÑÀÌ//pfr:ÑîñòàâÄîêóìåíòîâ//pfr:Íàëè÷èåÄîêóìåíòîâ//pfr:Êîëè÷åñòâî"/>
      		   </xsl:when>
      		   <xsl:otherwise>
                  <xsl:value-of select=".//pfr:ÑÂÅÄÅÍÈß_ÏÎ_ÏÀ×ÊÅ_ÄÎÊÓÌÅÍÒÎÂ_ÐÀÇÄÅËÀ_6//pfr:ÑîñòàâÄîêóìåíòîâ//pfr:Íàëè÷èåÄîêóìåíòîâ//pfr:Êîëè÷åñòâî"/>
      		   </xsl:otherwise>
			   </xsl:choose>
         </xsl:attribute>
         <xsl:attribute name="Cod">
            <xsl:value-of select="normalize-space(.//pfr:ÊîððåêòèðóåìûéÎò÷åòíûéÏåðèîä/pfr:Êâàðòàë)"/>
         </xsl:attribute>
         <xsl:attribute name="Year">
            <xsl:value-of select="normalize-space(.//pfr:ÊîððåêòèðóåìûéÎò÷åòíûéÏåðèîä/pfr:Ãîä)"/>
         </xsl:attribute>
         <xsl:variable name="TypeCorr">
            <xsl:value-of select="normalize-space(.//pfr:ÒèïÑâåäåíèé)"/>
         </xsl:variable>
         <xsl:attribute name="Type">
            <xsl:value-of select="$TypeCorr"/>
         </xsl:attribute>
         <xsl:if test="$TypeCorr!='ÎÒÌÅÍßÞÙÀß'">
        	   <xsl:choose>
        		   <xsl:when test=".//pfr:ÑÂÅÄÅÍÈß_Î_ÑÓÌÌÅ_ÂÛÏËÀÒ_Î_ÑÒÐÀÕÎÂÛÕ_ÂÇÍÎÑÀÕ_È_ÑÒÐÀÕÎÂÎÌ_ÑÒÀÆÅ_ÇË">
                  <xsl:for-each select=".//pfr:ÑÂÅÄÅÍÈß_Î_ÑÓÌÌÅ_ÂÛÏËÀÒ_Î_ÑÒÐÀÕÎÂÛÕ_ÂÇÍÎÑÀÕ_È_ÑÒÐÀÕÎÂÎÌ_ÑÒÀÆÅ_ÇË">
                     <xsl:element name="Snils">
                        <xsl:value-of select=".//pfr:ÑòðàõîâîéÍîìåð"/>
                     </xsl:element>
                  </xsl:for-each>
        		   </xsl:when>
        		   <xsl:otherwise>
                  <xsl:for-each select=".//pfr:ÑÂÅÄÅÍÈß_Î_ÑÓÌÌÅ_ÂÛÏËÀÒ_È_ÑÒÐÀÕÎÂÎÌ_ÑÒÀÆÅ_ÇË">
                     <xsl:element name="Snils">
                        <xsl:value-of select=".//pfr:ÑòðàõîâîéÍîìåð"/>
                     </xsl:element>
                  </xsl:for-each>
        		   </xsl:otherwise>
     		   </xsl:choose>
         </xsl:if>
      </xsl:element>
   </xsl:for-each>
</xsl:variable>

<xsl:template match="/">

<xsl:element name="Part_2_5">
<!-- 
<xsl:element name="Debug1">
   <xsl:element name="VarInput">
      <xsl:copy-of select="."/>
</xsl:element>
   <xsl:element name="VarReestrSource">
      <xsl:copy-of select="$ReestrSource"/>
</xsl:element>
   <xsl:element name="VarReports">
      <xsl:copy-of select="$Reports"/>
</xsl:element>
   <xsl:element name="VarReestrCorrective">
      <xsl:copy-of select="$ReestrCorrective"/>
</xsl:element>
   <xsl:element name="VarAllOriginal66">
      <xsl:copy-of select="$AllOriginal66"/>
</xsl:element>
   <xsl:element name="VarReestr66">
      <xsl:copy-of select="$Reestr66"/>
</xsl:element>
</xsl:element>
-->
   <!-- Äëÿ íà÷àëà ñâîäíàÿ èíôîðìàöèÿ ïî èñõîäíûì ôàéëàì -->
   <xsl:element name="Part_2_5_1">
   <xsl:for-each select="document(msxsl:node-set($Reports)//ReportList/File[@Type='ÈÑÕÎÄÍÀß']/@FullName)//pfr:ÔàéëÏÔÐ">
      <xsl:sort select=".//pfr:ÈìÿÔàéëà" />
      <xsl:element name="FilePFR">
         <xsl:attribute name="Name">
            <xsl:value-of select=".//pfr:ÈìÿÔàéëà"/>
         </xsl:attribute>
         <xsl:attribute name="CountPerson">
            <xsl:value-of select=".//pfr:ÑÂÅÄÅÍÈß_ÏÎ_ÏÀ×ÊÅ_ÄÎÊÓÌÅÍÒÎÂ_ÐÀÇÄÅËÀ_6//pfr:ÑîñòàâÄîêóìåíòîâ//pfr:Íàëè÷èåÄîêóìåíòîâ//pfr:Êîëè÷åñòâî"/>
         </xsl:attribute>
         <xsl:attribute name="SumOPS">
            <xsl:value-of select="format-number(.//pfr:ÑÂÅÄÅÍÈß_ÏÎ_ÏÀ×ÊÅ_ÄÎÊÓÌÅÍÒÎÂ_ÐÀÇÄÅËÀ_6//pfr:ÑòðàõîâûõÂçíîñîâÎÏÑ,'0.00')"/>
         </xsl:attribute>
         <xsl:attribute name="SumBase">
            <xsl:value-of select="format-number(.//pfr:ÑÂÅÄÅÍÈß_ÏÎ_ÏÀ×ÊÅ_ÄÎÊÓÌÅÍÒÎÂ_ÐÀÇÄÅËÀ_6//pfr:ÁàçàÄëÿÍà÷èñëåíèÿÑòðàõîâûõÂçíîñîâÍåÏðåâûøàþùàÿÏðåäåëüíóþ,'0.00')"/>
         </xsl:attribute>
      </xsl:element>
   </xsl:for-each>
   </xsl:element>
   <!-- Ñâîäíàÿ èíôîðìàöèÿ ïî êîððåêòèðóþùèì ôàéëàì -->
   <xsl:element name="Part_2_5_2">
   <!-- Ïðîõîäèì ïî âñåì ôàéëàì êîððåêòèðóþùèõ ñâåäåíèé -->
   <xsl:for-each select="msxsl:node-set($ReestrCorrective)/File">
      <xsl:element name="FilePFR">
         <!-- èìÿ ôàéëà + êîëè÷åñòâî çàñòðàõîâàííûõ ëèö â ôàéëå -->
         <xsl:copy-of select="@Name"/>
         <xsl:copy-of select="@CountPerson"/>
         <xsl:copy-of select="@Cod"/>
         <xsl:copy-of select="@Year"/>
         <xsl:copy-of select="@Type"/>

         <xsl:if test="@Type!='ÎÒÌÅÍßÞÙÀß'">
            <!-- Êëþ÷ ïåðèîäà äëÿ òåêóùåãî ôàéëà êîðð.ñâåäåíèé -->
            <xsl:variable name="KeyP" select="concat(@Year,@Cod)"/>
            <!-- Íàõîäèì ñîîòâåñòâóþùèé ïåðèîä ñðåäè ðàçäåëîâ 6.6 èñõîäíûõ ñâåäåíèé -->
            <xsl:variable name="AppropriatePeriod66">
               <xsl:for-each select="msxsl:node-set($Reestr66)/Period">
                  <xsl:if test="concat(./@Year,./@Cod)=$KeyP">
                     <xsl:copy-of select="."/>
                  </xsl:if>
               </xsl:for-each>
            </xsl:variable>

<!-- Ïðîõîäèì ïî âñåì ñòðàõîâûì íîìåðàì òåêóùåãî ôàéëà êîððåêòèðóþùèõ ñâåäåíèé
                 è äëÿ êàæäîãî èç íèõ íàõîäèì Person èç ðàçäåëà 6.6 -->
            <xsl:variable name="AppropriatePerson66">
               <xsl:for-each select=".//Snils">
                  <xsl:variable name="CurStrNum" select="."/>
                  <xsl:for-each select="msxsl:node-set($AppropriatePeriod66)//Person[Snils=$CurStrNum]">
                        <xsl:copy-of select="."/>
                  </xsl:for-each>
               </xsl:for-each>
            </xsl:variable>

   			<xsl:call-template name="sumCorrective">
   				<xsl:with-param name="CorrectiveItems" select="msxsl:node-set($AppropriatePerson66)/Person" />
   			</xsl:call-template>
<!--
<xsl:element name="Debug2">
   <xsl:element name="VarAppropriatePeriod66">
      <xsl:copy-of select="$AppropriatePeriod66"/>
   </xsl:element>
   <xsl:element name="VarAppropriatePerson66">
      <xsl:copy-of select="$AppropriatePerson66"/>
   </xsl:element>
</xsl:element>
-->
 <!--      <xsl:value-of select="format-number($ReportTotal, '$#,###.00')"/>-->
         </xsl:if>
      </xsl:element>
   </xsl:for-each>
   </xsl:element>
</xsl:element>
</xsl:template>

<!-- Ïîäâåäåíèå èòîãîâ ïî ñòðàõîâûì âçíîñàì -->
<xsl:template name="sumCorrective">
   <xsl:param name="CorrectiveItems" />    
   <xsl:param name="TotalOPS" select="0"/>    
   <xsl:param name="TotalStr" select="0"/>    
   <xsl:param name="TotalNak" select="0"/>    

	<xsl:choose>
		<xsl:when test="$CorrectiveItems">
			<xsl:variable name="firstOPS">
      	   <xsl:choose>
			   <xsl:when test="$CorrectiveItems[1]//DeltaOPS">
			      <xsl:value-of select="$CorrectiveItems[1]//DeltaOPS"/>
			   </xsl:when>
			   <xsl:otherwise>0</xsl:otherwise>
			   </xsl:choose>
			</xsl:variable>
			<xsl:variable name="firstStr">
      	   <xsl:choose>
			   <xsl:when test="$CorrectiveItems[1]//DeltaS">
			      <xsl:value-of select="$CorrectiveItems[1]//DeltaS"/>
			   </xsl:when>
			   <xsl:otherwise>0</xsl:otherwise>
			   </xsl:choose>
			</xsl:variable>
			<xsl:variable name="firstNak">
      	   <xsl:choose>
			   <xsl:when test="$CorrectiveItems[1]//DeltaN">
			      <xsl:value-of select="$CorrectiveItems[1]//DeltaN"/>
			   </xsl:when>
			   <xsl:otherwise>0</xsl:otherwise>
			   </xsl:choose>
			</xsl:variable>
			<xsl:call-template name="sumCorrective">
				<xsl:with-param name="CorrectiveItems" select="$CorrectiveItems[position() > 1]" />
				<xsl:with-param name="TotalOPS" select="$TotalOPS + $firstOPS" />
				<xsl:with-param name="TotalStr" select="$TotalStr + $firstStr" />
				<xsl:with-param name="TotalNak" select="$TotalNak + $firstNak" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
		   <xsl:attribute name="SumNak">
				<xsl:value-of select="format-number($TotalNak,'0.00')" />
		   </xsl:attribute>
		   <xsl:attribute name="SumStr">
				<xsl:value-of select="format-number($TotalStr,'0.00')" />
		   </xsl:attribute>
		   <xsl:attribute name="SumOPS">
				<xsl:value-of select="format-number($TotalOPS,'0.00')" />
		   </xsl:attribute>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>