<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#160;">
<!ENTITY deg "&#176;">
<!ENTITY eacute "&#233;">
]>
<xsl:stylesheet xmlns:n="http://www.minefi.gouv.fr/cp/helios/pes/versement/1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:decimal-format name="decformat" decimal-separator="," grouping-separator=" " digit="#" pattern-separator=";" NaN="NaN" minus-sign="-"/>    
   <xsl:template match="n:EtatVersement">
        <html>
            <head>
                <style>
                    body { background-color: #FFFFE8; }
                    div, td { font-family: verdana, sans-serif; font-size: 10px;} 
                    h1 { font-size: 18px; font-weight:bold; } 
                    table thead th { text-align: center; font-size: 11px; vertical-align: top;}
                    table tbody tr td { font-size: 10px; border: 0px; vertical-align: top; } 
                    table { border-spacing: 10px 20px; }
                    tr { page-break-inside: avoid; }
                    p#siret { left: 8px; }
                    .fonce { background-color: #FFFFB9;}
                    .clair { background-color: #FFFFD1;}
                    .entete { background-color: #003356; color: #FFFFFF;}
                    .enteteCol { background-color: #004456; color: #FFFFFF;}
                </style>
            </head>
            <body>
                <p style="border: solid #000000 0;"></p>
                <table>
                        <tr>
                            <td class="fonce" style="border: solid #000000 0;">
                                <h2 class="entete">Emetteur</h2>
                                <h3><xsl:value-of select="Emetteur/Nom/@V"/></h3>
                                <xsl:if test="string-length(Emetteur/Adresse/Adr1/@V) &gt; 0"><xsl:value-of select="Emetteur/Adresse/Adr1/@V"/><br/></xsl:if>
                                <xsl:if test="string-length(Emetteur/Adresse/Adr2/@V) &gt; 0"><xsl:value-of select="Emetteur/Adresse/Adr2/@V"/><br/></xsl:if>
                                <xsl:if test="string-length(Emetteur/Adresse/Adr3/@V) &gt; 0"><xsl:value-of select="Emetteur/Adresse/Adr3/@V"/><br/></xsl:if>
                                <xsl:value-of select="Emetteur/Adresse/CP/@V"/>&nbsp;<xsl:value-of select="Emetteur/Adresse/Ville/@V"/><br/><b>SIRET :</b>&#160;<xsl:value-of select="Emetteur/Siret/@V"/>
                            </td>
                        </tr>
                    </table>
                <br/>
                <!--p>
                    <xsl:if test="RepartitionParNature"><a href="#repartNat">Répartition par Nature</a></xsl:if>
                    <xsl:if test="Nomencaltures"><a href="#nomencl">Nomenclatures</a></xsl:if>
                </p-->
                <table width="100%" style="border-collapse: separate;">
                    <colgroup>
                        <col width="25%"/>
                        <col width="25%"/>
                        <col width="25%"/>
                        <col width="25%"/>
                    </colgroup>
                    <thead>
                        <tr>
                            <td class="enteteCol"><h3><center>Versement</center></h3></td>
                            <td class="enteteCol"><h3><center>Bénéficiaire</center></h3></td>
                            <td class="enteteCol"><h3><center>Destinataire</center></h3></td>
                            <td class="enteteCol"><h3><center>Décision</center></h3></td>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:for-each select="DonneesVersement/Versement">
                            <xsl:sort select="concat(NumMandat/@V,'|',Destinataire/InfoTiers/Nom/@V)" data-type="text" order="ascending"/>
                            <xsl:call-template name="Versement"/>
                        </xsl:for-each>
                    </tbody>
                </table>
                <br/>
                <h2>Totaux par mandat</h2>
                <table>
                    <thead>
                        <tr>
                            <td class="enteteCol"><h3><center>Mandat</center></h3></td><td class="enteteCol"><h3><center>Montant</center></h3></td>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:variable name="distinct.mandats" select="//Versement[not(preceding-sibling::node()/NumMandat/@V = NumMandat/@V)]/NumMandat/@V"/>
                        <xsl:for-each select="$distinct.mandats">
                            <xsl:sort select="."/>
                            <xsl:variable name="numMandat" select="."/>
                        <xsl:element name="tr">
                            <xsl:choose>
                                <xsl:when test="position() mod 2">
                                    <xsl:attribute name="class">fonce</xsl:attribute>
                                </xsl:when>
                                <xsl:otherwise><xsl:attribute name="class">clair</xsl:attribute></xsl:otherwise>
                            </xsl:choose>
                            <td><xsl:value-of select="$numMandat"/></td>
                            <td align="right"><xsl:value-of select="format-number(sum(//Versement[NumMandat/@V=$numMandat]/Mt/@V),'#####0,00;-######0,00','decformat')"></xsl:value-of></td>
                        </xsl:element>
                        </xsl:for-each>
                    </tbody>
                    
                </table>
                <br/>
                <table>
                    <tr>
                        <td class="entete" style="border: solid #000000 0;">
                            <h2><a name="nomencl"/>Nomenclatures</h2>
                        </td>
                    </tr>
                </table>
                <br/>
                <!--xsl:choose-->
                    <xsl:if test="Nomenclatures/NomenclaturePrestation">
                        <b>Nomenclature de Prestation : </b><xsl:value-of select="Nomenclatures/NomenclaturePrestation/Description/@V"/>
                        <br/><br/>
                        <table width="100%" style="border-collapse: separate;page-break-inside: avoid;">
                            <colgroup>
                                <col width="50%"/>
                                <col width="50%"/>
                            </colgroup>
                            <thead>
                                <tr>
                                    <td class="enteteCol"><h3><center>Libelle</center></h3></td>
                                    <td class="enteteCol"><h3><center>Code</center></h3></td>
                                </tr>
                            </thead>
                            <tbody>
                                <xsl:for-each select="Nomenclatures/NomenclaturePrestation/Correspondance">
                                    <xsl:sort select="Libelle/@V" data-type="text" order="ascending"/>
                                    <xsl:call-template name="correspondance"/>                            
                                </xsl:for-each>
                            </tbody>
                        </table>
                    </xsl:if>
                    <xsl:if test="Nomenclatures/NomenclatureNature">
                        <b>Nomenclature de Nature : </b><xsl:value-of select="Nomenclatures/NomenclatureNature/Description/@V"/>
                        <br/><br/>
                        <table width="100%" style="border-collapse: separate;page-break-inside: avoid;">
                            <colgroup>
                                <col width="50%"/>
                                <col width="50%"/>
                            </colgroup>
                            <thead>
                                <tr>
                                    <td class="enteteCol"><h3><center>Libelle</center></h3></td>
                                    <td class="enteteCol"><h3><center>Code</center></h3></td>
                                </tr>
                            </thead>
                            <tbody>
                                <xsl:for-each select="Nomenclatures/NomenclatureNature/Correspondance">
                                    <xsl:sort select="Libelle/@V" data-type="text" order="ascending"/>
                                    <xsl:call-template name="correspondance"/>                            
                                </xsl:for-each>
                            </tbody>
                        </table>
                    </xsl:if>
                    <xsl:if test="Nomenclatures/NomenclatureBudget">
                        <b>Nomenclature de Budget : </b><xsl:value-of select="Nomenclatures/NomenclatureBudget/Description/@V"/>
                        <br/><br/>
                        <table width="100%" style="border-collapse: separate;page-break-inside: avoid;">
                            <colgroup>
                                <col width="50%"/>
                                <col width="50%"/>
                            </colgroup>
                            <thead>
                                <tr>
                                    <td class="enteteCol"><h3><center>Libelle</center></h3></td>
                                    <td class="enteteCol"><h3><center>Code</center></h3></td>
                                </tr>
                            </thead>
                            <tbody>
                                <xsl:for-each select="Nomenclatures/NomenclatureBudget/Correspondance">
                                    <xsl:sort select="Libelle/@V" data-type="text" order="ascending"/>
                                    <xsl:call-template name="correspondance"/>                            
                                </xsl:for-each>
                            </tbody>
                        </table>
                    </xsl:if>
                <!--/xsl:choose-->
                <xsl:choose>
                    <xsl:when test="RepartitionParNature">
                        <br/><br/>
                        <table>
                            <tr>
                                <td class="entete" style="border: solid #000000 0;">
                                    <h2><a name="repartNat"/>Répartition par Nature</h2>
                                </td>
                            </tr>
                        </table>
                        <br/>
                                <table width="100%" style="border-collapse: separate;page-break-inside: avoid;">
                                    <colgroup>
                                        <col width="20%"/>
                                        <col width="20%"/>
                                        <col width="20%"/>
                                        <col width="20%"/>
                                        <col width="20%"/>
                                    </colgroup>
                                    <thead>
                                        <tr>
                                            <td class="enteteCol"><h3><center>Année</center></h3></td>
                                            <td class="enteteCol"><h3><center>Libéllé</center></h3></td>
                                            <td class="enteteCol"><h3><center>Code</center></h3></td>
                                            <td class="enteteCol"><h3><center>Montant</center></h3></td>
                                            <td class="enteteCol"><h3><center>Code Budget</center></h3></td>
                                            <td class="enteteCol"><h3><center>CodeFonction</center></h3></td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <xsl:for-each select="RepartitionParNature/Repartition">
                                            <xsl:sort select="Libelle/@V" data-type="text" order="ascending"/>
                                            <xsl:call-template name="repartition"/>                            
                                        </xsl:for-each>
                                    </tbody>
                                </table>
                    </xsl:when>
                </xsl:choose>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="Versement">
        <xsl:param name="thisvers"/>
        <xsl:variable name="classTr">
            <xsl:choose>
                <xsl:when test="position() mod 2 = 1">fonce</xsl:when>
                <xsl:otherwise>clair</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:element name="tr">
            <xsl:attribute name="class"><xsl:value-of select="$classTr"/></xsl:attribute>
            <xsl:element name="td">
                <b>Dossier : </b><xsl:value-of select="IdDossier/@V"/><br/>
                <b>Id : </b><xsl:value-of select="IdVerst/@V"/><br/>
                <b>Date: </b><xsl:value-of select="Date/@V"/><br/>
                <b>Service : </b><xsl:value-of select="Service/@V"/><br/>
                <br/>
                <b>Mandat : </b><xsl:value-of select="NumMandat/@V"/><br/>
                <b>Code Nature : </b><xsl:value-of select="CodeNature/@V"/><br/>
                <b>Code Budget : </b><xsl:value-of select="CodeBudget/@V"/><br/>
                <b>Code Fonction : </b><xsl:value-of select="CodeFonction/@V"/><br/>
                <b>Commentaire : </b><xsl:value-of select="Commentaire/@V"/><br/>
                <br/>
                <b>Année/Séquence: </b><xsl:value-of select="substring(Date/@V,1,4)"/>&#160;/&#160;<xsl:value-of select="Sequence/NumOrdre/@V"/>&#160;/&#160;<xsl:value-of select="Sequence/NbTotal/@V"/><br/>
                <b>Prestation Principale: </b><xsl:value-of select="../../Prestation/Libelle/@V"/>&#160;/&#160;<xsl:value-of select="../../Prestation/Code/@V"/><br/>
                <b>Prestation: </b><xsl:value-of select="Prestation/@V"/><br/>
                <b>Montant à verser : <xsl:value-of select="Mt/@V"/>&#160;€</b>
            </xsl:element>
            <xsl:element name="td">
                <xsl:for-each select="Beneficiaire">
                    <xsl:value-of select="./InfoTiers/Civilite/@V"/>&#160;<b><xsl:value-of select="./InfoTiers/Nom/@V"/></b>&#160;<xsl:value-of select="./InfoTiers/Prenom/@V"/><br/>
                    <xsl:if test="string-length(./Adresse/Adr1/@V) &gt; 0"><xsl:value-of select="./Adresse/Adr1/@V"/><br/></xsl:if>
                    <xsl:if test="string-length(./Adresse/Adr2/@V) &gt; 0"><xsl:value-of select="./Adresse/Adr2/@V"/><br/></xsl:if>
                    <xsl:if test="string-length(./Adresse/Adr3/@V) &gt; 0"><xsl:value-of select="./Adresse/Adr3/@V"/><br/></xsl:if>
                    <xsl:value-of select="Beneficiaire/Adresse/CP/@V"/>&#160;<xsl:value-of select="./Adresse/Ville/@V"/><br/>
                    <xsl:if test="./DateNaissance"><br/><b>Date de naissance : </b><xsl:call-template name="slash-date"><xsl:with-param name="datebrute" select="./DateNaissance/@V"/></xsl:call-template></xsl:if>
                    <xsl:if test="not(position() = last())"><br/></xsl:if>
                </xsl:for-each>
            </xsl:element>
            <xsl:element name="td">
                <xsl:value-of select="Destinataire/InfoTiers/Civilite/@V"/>&#160;<b><xsl:value-of select="Destinataire/InfoTiers/Nom/@V"/></b>&#160;<xsl:value-of select="Destinataire/InfoTiers/Prenom/@V"/><br/>
                <xsl:if test="string-length(Destinataire/Adresse/Adr1/@V) &gt; 0"><xsl:value-of select="Destinataire/Adresse/Adr1/@V"/><br/></xsl:if>
                <xsl:if test="string-length(Destinataire/Adresse/Adr2/@V) &gt; 0"><xsl:value-of select="Destinataire/Adresse/Adr2/@V"/><br/></xsl:if>
                <xsl:if test="string-length(Destinataire/Adresse/Adr3/@V) &gt; 0"><xsl:value-of select="Destinataire/Adresse/Adr3/@V"/><br/></xsl:if>
                <xsl:value-of select="Destinataire/Adresse/CP/@V"/>&#160;<xsl:value-of select="Destinataire/Adresse/Ville/@V"/><br/>
                <b>RIB : </b><xsl:value-of select="Destinataire/CpteBancaire/CodeEtab/@V"/>&#160;<xsl:value-of select="Destinataire/CpteBancaire/CodeGuic/@V"/>&#160;<xsl:value-of select="Destinataire/CpteBancaire/IdCpte/@V"/>&#160;<xsl:value-of select="Destinataire/CleRib/CodeEtab/@V"/><br/>
                <xsl:value-of select="Destinataire/CpteBancaire/LibBanc/@V"/>&#160;<xsl:value-of select="Destinataire/CpteBancaire/TitCpte/@V"/>
            </xsl:element>
            <xsl:element name="td">
                <b>Date : </b><xsl:value-of select="Decision/Date/@V"/><br/>
                <b>Date Effet : </b><xsl:value-of select="Decision/DateEffet/@V"/><br/>
                <b>Date Fin : </b><xsl:value-of select="Decision/DateFin/@V"/><br/>
                <b>Nature : </b><xsl:value-of select="Decision/Nature/@V"/><br/>
                <b>Identifiant : </b><xsl:value-of select="Decision/IdDecision/@V"/><br/>
                <b>Périodicité : </b><xsl:value-of select="Decision/Periodicite/@V"/><br/>
                <b>Montant : </b><xsl:value-of select="Decision/Mt/@V"/>&#160;€
            </xsl:element>
        </xsl:element>
    </xsl:template>

<xsl:template name="correspondance">
    <xsl:variable name="classTr">
        <xsl:choose>
            <xsl:when test="position() mod 2 = 1">fonce</xsl:when>
            <xsl:otherwise>clair</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:element name="tr">
        <xsl:attribute name="class"><xsl:value-of select="$classTr"/></xsl:attribute>
        <xsl:element name="td">
            <xsl:value-of select="Libelle/@V"/>
        </xsl:element>
        <xsl:element name="td">
            <xsl:value-of select="Code/@V"/>
        </xsl:element>
    </xsl:element>
</xsl:template>

<xsl:template name="repartition">
    <xsl:variable name="classTr">
        <xsl:choose>
            <xsl:when test="position() mod 2 = 1">fonce</xsl:when>
            <xsl:otherwise>clair</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:element name="tr">
        <xsl:attribute name="class"><xsl:value-of select="$classTr"/></xsl:attribute>
        <xsl:element name="td">
            <xsl:value-of select="../../Annee/@V"/>
        </xsl:element>
        <xsl:element name="td">
            <xsl:value-of select="Libelle/@V"/>
        </xsl:element>
        <xsl:element name="td">
            <xsl:value-of select="Code/@V"/>
        </xsl:element>
        <xsl:element name="td">
            <xsl:value-of select="Mt/@V"/>
        </xsl:element>
        <xsl:element name="td">
            <xsl:value-of select="CodeBudget/@V"/>
        </xsl:element>
        <xsl:element name="td">
            <xsl:value-of select="CodeFonction/@V"/>
        </xsl:element>
    </xsl:element>
</xsl:template>

    <xsl:template name="slash-date">
        <xsl:param name="datebrute"/>
            <xsl:value-of select="substring($datebrute, 9, 2)"/>
            <xsl:text>/</xsl:text>
            <xsl:value-of select="substring($datebrute, 6, 2)"/>
            <xsl:text>/</xsl:text>
            <xsl:value-of select="substring($datebrute, 1, 4)"/>
    </xsl:template>
    
</xsl:stylesheet>
