<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:n="http://www.xemelios.org/namespaces#ertn"
    version="2.0" 
    exclude-result-prefixes="xsl n">
    
    <xsl:output method="xhtml" encoding="ISO-8859-15" indent="yes" include-content-type="yes"/>
    <xsl:decimal-format name="decformat" decimal-separator="," grouping-separator=" " digit="#" pattern-separator=";" NaN="NaN" minus-sign="-"/>
    
    <xsl:template match="/n:Ertn/n:EtatErtn/n:LigneErtn">
        <html>
            <head>
                <title>Xemelios - Mandat national</title>
                <style type="text/css">
                    body {
                        font-family: Arial, Helvetica, sans-serif;
                        font-size: 14px;
                    }
                    span.lib {
                        font-weight: bold;
                        margin-right: 7px;
                    }
                    span.val {
                        font-style: italic;
                    }
                    div.emetteur {
                        top: 30px;
                        left: 0px;
                    }
                </style>
            </head>
            <body>
                <t1>Titre National</t1>
                <div class="emetteur">
                    <p>
                        <span class="lib">Comptable:</span><span class="val"><xsl:value-of select="n:LibelleComptable/@V"/> (<xsl:value-of select="n:Comptable/@V"/>)</span><br/>
                        <span class="lib">Ministère:</span><span class="val"><xsl:value-of select="n:Ministere/@LibelleMinistere"/> (<xsl:value-of select="n:Ministere/@V"/>)</span><br/>
                        <span class="lib">Ordonnateur:</span><span class="val"><xsl:value-of select="n:LibelleOrdonnateur/@V"/> (<xsl:value-of select="n:Ordo/@V"/>)</span>
                    </p>
                </div>
                <div class="budget">
                    <p>
                        <span class="lib">Programme:</span><span class="val"><xsl:value-of select="n:LibelleProgramme/@V"/> (<xsl:value-of select="n:ChapBudg/@V"/>)</span><br/>
                        <span class="lib">Action:</span><span class="val"><xsl:value-of select="n:LibelleAction/@V"/> (<xsl:value-of select="n:ArtBudgExec/@V"/>)</span><br/>
                        <span class="lib">Compte PCE:</span><span class="val"><xsl:value-of select="n:LibelleBudget/@V"/> (<xsl:value-of select="n:ParagBudgExec/@V"/>)</span>
                    </p>
                </div>
                <div class="general">
                    <p>
                        <span class="lib">Mandat:</span><span class="val"><xsl:value-of select="n:NumMandat/@V"/></span><br/>
                        <span class="lib">Date création:</span><span class="val"><xsl:call-template name="format-date"><xsl:with-param name="date" select="n:DateCreation[1]/@V"/></xsl:call-template></span><br/>
                        <span class="lib">Année budgétaire:</span><span class="val"><xsl:value-of select="n:AnneeBudget/@V"/></span><br/>
                        <span class="lib">Type évènement:</span><span class="val"><xsl:call-template name="typeEvt"><xsl:with-param name="evt" select="n:TypeEvt/@V"/></xsl:call-template></span><br/>
                        <span class="lib">Compte spécial du trésor:</span><span class="val"><xsl:value-of select="n:CompteSpeTresor/@V"/></span><br/>
                        <span class="lib">Opération d'investissement:</span><span class="val"><xsl:value-of select="n:NumOpeInvest/@V"/></span><br/>
                        <span class="lib">Année engagement:</span><span class="val"><xsl:value-of select="n:AnneeEngag/@V"/></span><br/>
                        <span class="lib">Engagement:</span><span class="val"><xsl:value-of select="n:NumEngag/@V"/></span><br/>
                        <span class="lib">Montant (€):</span><span class="val"><xsl:call-template name="number"><xsl:with-param name="num" select="n:MtPJ/@V"/></xsl:call-template></span>
                    </p>
                </div>
                <div class="liasses">
                    <p>
                        <span class="lib"><xsl:choose>
                            <xsl:when test="n:NumLiasseDeb/@V = n:NumLiasseFin/@V">Liasse locale:</xsl:when>
                            <xsl:otherwise>Liasses locales:</xsl:otherwise>
                        </xsl:choose></span>
                        <span class="val"><xsl:choose>
                            <xsl:when test="n:NumLiasseDeb/@V = n:NumLiasseFin/@V"><xsl:value-of select="n:NumLiasseDeb/@V"/></xsl:when>
                            <xsl:otherwise><xsl:value-of select="n:NumLiasseDeb/@V"/>..<xsl:value-of select="n:NumLiasseFin/@V"/></xsl:otherwise>
                        </xsl:choose></span><br/>
                        <span class="lib"><xsl:choose>
                            <xsl:when test="n:NumLiasseDeb/@V = n:NumLiasseFin/@V">Liasse nationale:</xsl:when>
                            <xsl:otherwise>Liasses nationales:</xsl:otherwise>
                        </xsl:choose></span>
                        <span class="val"><xsl:choose>
                            <xsl:when test="n:NumLiasseDeb/@V = n:NumLiasseFin/@V"><xsl:value-of select="n:NumLiasseNationale/n:Liasse[1]/@V"/></xsl:when>
                            <xsl:otherwise><ul><xsl:for-each select="n:NumLiasseNationale/n:Liasse"><li><xsl:value-of select="./@V"/></li></xsl:for-each></ul></xsl:otherwise>
                        </xsl:choose></span>
                    </p>
                </div>
            </body>
        </html>
        
    </xsl:template>
    
    <xsl:template name="format-date">
        <xsl:param name="date"/>
        <xsl:value-of select="substring($date,9)"/>/<xsl:value-of select="substring($date,6,2)"/>/<xsl:value-of select="substring($date,1,4)"/>
    </xsl:template>
    <xsl:template name="typeEvt">
        <xsl:param name="evt"/>
        <xsl:choose>
            <xsl:when test="$evt = '600'">Ordonnance de virement</xsl:when>
            <xsl:when test="$evt = '603'">DL ou mandat de régularisation</xsl:when>
            <xsl:when test="$evt = '605'">Ventilation budgétaire de la paye</xsl:when>
            <xsl:when test="$evt = '610'">DL ou mandat sur commande d'achat simple en DC, DL sur bon de com./ marché en DC, DL sur contrat d'achat en DC, DL direct en DC</xsl:when>
            <xsl:when test="$evt = '612'">Ordonnances de ré-imputation</xsl:when>
            <xsl:when test="$evt = '613'">Bordereau d'annulation (inclus les types 613, 618, 619 et 620)</xsl:when>
            <xsl:otherwise>Inconnu</xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="number">
        <xsl:param name="num"/>
        <xsl:choose>
            <xsl:when test="string-length(string($num)) = 0"/>
            <xsl:when test="number($num) = 0">
                <xsl:value-of select="format-number(0,'# ###,00;-# ###,00','decformat')"/>
            </xsl:when>
            <xsl:when test="string(number($num)) = 'NaN'"/>
            <xsl:otherwise>
                <xsl:value-of select="format-number($num,'# ###,00;-# ###,00','decformat')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>
