<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:n="fr:gouv:finances:xemelios:internal"
      xmlns:added="http://projets.admisource.gouv.fr/xemelios/namespaces#added"
      exclude-result-prefixes="xs"
      
      version="2.0">
    
    
    <xsl:character-map name="accents">
        <xsl:output-character character="à" string="&amp;#224;"/>
        <xsl:output-character character="é" string="&amp;#233;"/>
        <xsl:output-character character="è" string="&amp;#232;"/>
        <xsl:output-character character="ê" string="&amp;#234;"/>
        <xsl:output-character character="ë" string="&amp;#235;"/>
        <xsl:output-character character="î" string="&amp;#238;"/>
        <xsl:output-character character="ï" string="&amp;#239;"/>
        <xsl:output-character character="ô" string="&amp;#244;"/>
        <xsl:output-character character="ù" string="&amp;#249;"/>
    </xsl:character-map>

    <xsl:output encoding="UTF-8" method="xhtml" use-character-maps="accents"/>
    <xsl:decimal-format name="decformat" decimal-separator="," grouping-separator=" " digit="#" pattern-separator=";" NaN="NaN" minus-sign="-"> </xsl:decimal-format>
    
    <xsl:param name="numLigne"/>
    
    <xsl:variable name="DATE_FORMAT" select="'[D,2]/[M,2]/[Y]'"/>
    <xsl:variable name="MOIS_FORMAT" select="'[M,2]/[Y]'"/>
    <xsl:variable name="TIME_FORMAT" select="'[H01]:[m01]:[s01]'"/>
    
    <xsl:template match="/">
        <html>
            <head>
                <title>Demande de RSA</title>
                <style media="all">
                    body { font-family: verdana, sans-serif; font-size: 10px; }
                    table.data { width: 100%; border-style: none; border-width: 0px; cellpadding: 0px; cellspacing: 0px; }
                    table.detail { background-color: black; }
                    table { background-color: black; cell-spacing: 1px; cell-padding: 1px; margin: 5px 5px 5px 5px; border-collapse: collapse; }
                    th { background-color: #cccccc; border-style: solid; border-width: 1px; }
                    td { background-color: white; border-style: solid; border-width: 1px; }
                    
                    .bold { font-weight: bold; }
                    td { vertical-align: baseline; background-color: white;}
                    td.mt { white-space: nowrap; }
                    tr.bordered { border-color: black; border-width: 1px; border-color: #666666; border-style: solid; }
                    tr.total td { background-color: #cccccc; font-weight: bold; cellspacing-bottom: 3px;}
                    theader tr th { font-weight: bold; background-color: #CCCCCC; }
                    tr.highlighted td { background-color: #FFFF66; }
                </style>
                <style type="text/css" media="print">
                    @page { size: 210mm 297mm; @bottom-center { content: "Page " counter(page) " / " counter(pages); } }
                    body { font-family: verdana, sans-serif; font-size: 10px; width: 210mm; margin: 0px; }
                    table { page-break-inside: avoid }
                    tr { page-break-inside: avoid; }
                    table#enteteGauche { width: 110mm; }
                    table#enteteDoite { width: 70mm; }
                    table.maxWidth { width: 180mm; }
                </style>
            </head>
            <body>
                <table id="flux">
                    <colgroup>
                        <col width="50%"/>
                        <col width="50%"/>
                    </colgroup>
                    <theader>
                        <tr><th colspan="2">Flux</th></tr>
                    </theader>
                    <tbody>
                        <tr>
                            <td class="bold">Application émettrice</td>
                            <td><xsl:value-of select="/n:Racine/n:IdentificationFlux/n:APPLIEME/text()"/> - <xsl:value-of select="/n:Racine/n:IdentificationFlux/n:NUMVERSIONAPPLIEME/text()"/></td>
                        </tr>
                        <tr>
                            <td class="bold">Créé le</td>
                            <td><xsl:value-of select="format-date(/n:Racine/n:IdentificationFlux/n:DTCREAFLUX/text(),$DATE_FORMAT)"/> - <xsl:value-of select="format-time(/n:Racine/n:IdentificationFlux/n:HEUCREAFLUX/text(),$TIME_FORMAT)"></xsl:value-of></td>
                        </tr>
                        <tr>
                            <td class="bold">Mois de référence</td>
                            <td><xsl:value-of select="format-date(/n:Racine/n:IdentificationFlux/n:DTREF/text(),'[M,2]/[Y]')"/></td>
                        </tr>
                    </tbody>
                </table>
                <xsl:apply-templates/>            
            </body>
        </html>
    </xsl:template>
    <xsl:template match="*"></xsl:template>
    <xsl:template match="n:Racine">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="n:DemandesRSA">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="n:InfosFinancieresFoyerRSA">
        <table id="allocataire">
            <colgroup>
                <col width="30%"/>
                <col width="70%"/>
            </colgroup>
            <theader>
                <tr><th colspan="2">Allocataire</th></tr>
            </theader>
            <tbody>
                <xsl:variable name="matricule" select="./n:IdentificationRSA/n:Organisme/n:MATRICULE/text()"/>
                    <xsl:for-each select="n:Personne">
                        <tr>
                            <td colspan="2"><b><xsl:value-of select="./n:Identification/n:QUAL/text()"/>&#160;<xsl:value-of select="./n:Identification/n:PRENOM/text()"/>&#160;<xsl:value-of select="./n:Identification/n:NOMNAI/text()"/></b><xsl:if test="not( ./n:Identification/n:NOMNAI/text() eq ./n:Identification/n:NOM/text())">&#160;épou<xsl:choose>
                                    <xsl:when test="./n:Identification/n:SEXE/text()=1">x</xsl:when><xsl:otherwise>se</xsl:otherwise>
                                </xsl:choose>&#160;<b><xsl:value-of select="./n:Identification/n:NOM/text()"/></b></xsl:if>
                            </td>
                        </tr>
                        <tr>
                            <td>Né<xsl:choose><xsl:when test="./n:Identification/n:SEXE/text() eq '1'"></xsl:when><xsl:otherwise>e</xsl:otherwise></xsl:choose> le</td>
                            <xsl:variable name="lieu.naiss.length" select="string-length(./n:Identification/n:NOMCOMNAI/text())" as="xs:integer"/>
                            <td class="bold"><xsl:value-of select="format-date(./n:Identification/n:DTNAI/text(),$DATE_FORMAT)"/><xsl:if test="./n:Identification/n:NOMCOMNAI and ($lieu.naiss.length gt 0)"> à <xsl:value-of select="./n:Identification/n:NOMCOMNAI/text()"/></xsl:if></td>
                        </tr>
                        <tr>
                            <td>Matricule <xsl:value-of select="../n:IdentificationRSA/n:Organisme/n:FONORG/text()"/></td>
                            <td class="bold"><xsl:value-of select="$matricule"/></td>
                        </tr>
                    </xsl:for-each>
                    <xsl:for-each select="n:DonneesAdministratives/n:Adresse">
                        <tr>
                            <td>Adresse</td>
                            <td class="bold">
                                <xsl:if test="string-length(./n:COMPLIDEADR/text()) gt 0"><xsl:value-of select="n:COMPLIDEADR/text()"/><br/></xsl:if>
                                <xsl:value-of select="n:NUMVOIE/text()"/>&#160;<xsl:value-of select="n:TYPEVOIE/text()"/>&#160;<xsl:value-of select="n:NOMVOIE/text()"/><br/>
                                <xsl:if test="string-length(./n:COMPLADR/text()) gt 0"><xsl:value-of select="n:COMPLADR/text()"/><br/></xsl:if>
                                <xsl:if test="string-length(./n:LIEUDIST/text()) gt 0"><xsl:value-of select="n:LIEUDIST/text()"/><br/></xsl:if>
                                <xsl:value-of select="n:CODEPOS/text()"/>&#160;<xsl:value-of select="n:LOCAADR/text()"/>
                            </td>
                        </tr>
                    </xsl:for-each>
            </tbody>
        </table>
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="n:AcompteRSA">
        <hr/>
        <h2>Acompte RSA du mois <xsl:value-of select="format-date(n:GeneraliteAcompteRSA/n:MOISMOUCOMPTA/text(),$MOIS_FORMAT)"/></h2>
        <p><b>Montant total:</b>&#160;<xsl:call-template name="number"><xsl:with-param name="num" select="n:DetailAcompteRSA/@added:somme"/></xsl:call-template></p>
        <table class="detail">
            <theader>
                <tr>
                    <th>Nature prestation ou créance</th>
                    <th>Type opération comptable</th>
                    <th>Rang prestation ou créance</th>
                    <th>Sens mouvement</th>
                    <th>Montant</th>
                    <th>Date traitement</th>
                </tr>
            </theader>
            <tbody>
                <xsl:apply-templates/>
            </tbody>
        </table>
    </xsl:template>
    <xsl:template match="n:DetailAcompteRSA">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="n:AllocationsComptabilisees | n:RemisesIndus | n:IndusConstates | n:IndusTransferesCG | n:AnnulationsFaibleMontant | n:AutresAnnulations">
        <tr>
            <td colspan="4" style="text-align:center; background-color: #CCCCCC;">
                <xsl:choose>
                    <xsl:when test="local-name() = 'AllocationsComptabilisees'">Allocations comptabilisées</xsl:when>
                    <xsl:when test="local-name() = 'RemisesIndus'">Remises indus</xsl:when>
                    <xsl:when test="local-name() = 'IndusConstates'">Indus contatés</xsl:when>
                    <xsl:when test="local-name() = 'IndusTransferesCG'">Indus transférés CG</xsl:when>
                    <xsl:when test="local-name() = 'AnnulationsFaibleMontant'">Annulations faibles montants</xsl:when>
                    <xsl:when test="local-name() = 'AutresAnnulations'">Autres annulations</xsl:when>
            </xsl:choose>
            </td>
            <td class="mt" style="text-align: right; background-color: #CCCCCC; font-weight: bold; "><xsl:call-template name="number"><xsl:with-param name="num" select="./@added:somme"/></xsl:call-template></td>
            <td style="background-color: #CCCCCC;"> </td>
        </tr>
        <xsl:apply-templates/>
        <!--tr class='total'>
            <td colspan="4">Total</td>
            <td style="text-align:right;"><xsl:call-template name="number"><xsl:with-param name="num" select="./@added:somme"/></xsl:call-template></td>
            <td>&#160;</td>
        </tr-->
    </xsl:template>
    
    <xsl:template match="n:DetailAllocationComptabilisee | n:DetailRemiseIndu | n:DetailInduConstate | n:DetailInduTransfereCG | n:DetailAnnulationFaibleMontant | n:DetailAutreAnnulation">
        <xsl:element name="tr">
            <xsl:if test="$numLigne = ./@added:generated-id"><xsl:attribute name="class">highlighted</xsl:attribute></xsl:if>
            <td><xsl:value-of select="n:NATPFCRE/text()"/> - <xsl:value-of select="n:NATPFCRE/@added:nature"/></td>
            <td><xsl:value-of select="n:TYPEOPECOMPTA/text()"/> - <xsl:value-of select="n:TYPEOPECOMPTA/@added:ope"/></td>
            <td class="mt" style="text-align:right;">
                <xsl:choose>
                    <xsl:when test="n:RGCRE"><xsl:value-of select="format-number(n:RGCRE/text(),'# ###','decformat')"/></xsl:when>
                    <xsl:otherwise>&#160;</xsl:otherwise>
                </xsl:choose>
            </td>
            <td style="text-align:right;"><xsl:choose>
                <xsl:when test="n:SENSOPECOMPTA/text() = 'AJ'">+</xsl:when>
                <xsl:otherwise>-</xsl:otherwise>
            </xsl:choose>
            </td>
            <td style="text-align:right;"><xsl:call-template name="number"><xsl:with-param name="num" select="n:MTMOUCOMPTA/text()"/></xsl:call-template></td>
            <td><xsl:value-of select="format-date(n:DTTRAIMOUCOMPTA/text(),$DATE_FORMAT)"/></td>
        </xsl:element>
    </xsl:template>
    
    <xsl:template name="number">
        <xsl:param name="num"/>
        <xsl:choose>
            <xsl:when test="string-length($num) = 0"/>
            <xsl:when test="number($num) = 0"/>
            <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>
