<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#160;">
]><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:n="http://www.minefi.gouv.fr/cp/helios/pes_v2/facture"
    xmlns:added="http://projets.admisource.gouv.fr/xemelios/namespaces#added"
    xmlns:ano="http://projets.admisource.gouv.fr/xemelios/namespaces#anomally"
    xmlns:data="data.uri"
    version="2.0">
    <xsl:param name="elementId"/> <!-- 'PESFacture' pour afficher le releve, vide sinon -->
    <xsl:param name="numeroFacture"/>  <!-- dans le cas de l'affichage d'une FactureIndiv FactureIndiv/NumeroFacture/@V -->
    <xsl:param name="idLigne" select="concat('','')"/>
    <xsl:param name="browser-destination" />
    <xsl:param name="anoId"/>    <!-- id de l'anomalie a mettre en surbrillance -->
    <xsl:output encoding="ISO-8859-1" method="xhtml" exclude-result-prefixes="n added ano data" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" include-content-type="no" indent="yes"/>
    <xsl:decimal-format name="decformat" decimal-separator="." grouping-separator=" "/>               
    
    <xsl:template name="slash-date">
        <xsl:param name="datebrute"/>
        <date xmlns:data="data.uri" xsl:exclude-result-prefixes="data">
            <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)"/>
        </date>
    </xsl:template>
    
    <xsl:template match="/n:DOC_PES_Facture">
        <html style="page: landscape">
            <head>
                <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"/>
                <title>Quittance <xsl:value-of select="./n:PESFacture/n:FactureIndiv[n:NumeroFacture/@V = $numeroFacture]/n:Debiteur/n:InfoTiers/n:Nom/@V"/></title>
                <style title="styleFacturePes" type="text/css" media="all">
                    body {
                        font-family: verdana, sans-serif;
                        font-size: 10px;
                        background-color: #FFFFFF;
                    }
                    div.emetteur {
                        position: relative;
                        left: 12cm;
                        width: 10cm;
                        border: solid 1px black;
                        text-align: left;
                        padding: 0.2cm 0.2cm 0.2cm 0.2cm;
                    }
                    table.facture {
                        font-family: verdana;
                        font-size: 10px;
                        margin: 0;
                        border-style: none;
                        border-width: 0px;
                        width: 100%;
                        text-align: left;
                        padding: 0;
                        page-break-inside: avoid;
                    }
                    .entete {
                    font-weight: bold;
                    text-align: center;
                    font-size: 12px;
                    background-color: rgb(202, 202, 202);
                    border-top: solid 1px black;
                    border-bottom: solid 1px black;
                    }
                    .total_entete {
                    font-weight: normal;
                    text-align: right;
                    font-size: 12px;                    
                    }
                    .total_valeur {
                    font-weight: bold;
                    text-align: right;
                    font-size: 12px;                    
                    }
                    td.colonne1 {
                    border-width: 0px;
                    text-align: left;
                    border-bottom: solid 1px #EFEFEF;
                    }
                    td.colonne2 {
                    width: 2cm;
                    border-width: 0px;
                    text-align: right;
                    border-bottom: solid 1px #EFEFEF;
                    }
                    td.colonne3 {
                    width: 1.5cm;
                    border-width: 0px;
                    text-align: right;
                    border-bottom: solid 1px #EFEFEF;
                    }
                    td.colonne4 {
                    width: 2cm;
                    border-width: 0px;
                    text-align: right;
                    border-bottom: solid 1px #EFEFEF;
                    }
                    td.colonne5 {
                    width: 1.5cm;
                    border-width: 0px;
                    text-align: right;
                    border-bottom: solid 1px #EFEFEF;
                    }
                    td.colonne6 {
                    width: 2cm;
                    border-width: 0px;
                    text-align: right;
                    border-bottom: solid 1px #EFEFEF;
                    }
                    td.colonne7 {
                    width: 2cm;
                    border-width: 0px;
                    text-align: right;
                    border-bottom: solid 1px #EFEFEF;
                    }
                    tr.highlighted { background-color: #FFFF66; }
                </style>
                <style media="print">
                    @page landscape {
                        size: 297mm 210mm;
                        margin: 1cm 1cm 1cm 1cm;
                    }
                    body { 
                        font-family: verdana, sans-serif;
                        font-size: 10px;
                        width: 210mm;
                        margin: 0px; }
                    a { text-decoration: none; color: black;}
    	    </style>
                
            </head>
            <body>
                <xsl:for-each select="./n:PESFacture/n:FactureIndiv[n:NumeroFacture/@V = $numeroFacture]">
                    <table class="facture">
                        <tbody>
                            <tr>
                                <td colspan="7">Le&nbsp;
                                    <xsl:call-template name="slash-date" >
                                        <xsl:with-param name="datebrute" select="n:DateEmission/@V" />
                                    </xsl:call-template>	
                                </td>
                            </tr>      
                            <tr>
                                <td colspan="7"><xsl:value-of select="n:Objet/@V"/></td>
                            </tr>
<!-- DEBITEUR -->                                    
                            <tr>
                                <td colspan="7">
                                    <div class="emetteur">
                                        <xsl:if test="string-length(n:Debiteur/n:InfoTiers/n:Civilite/@V)>0">
                                            <xsl:value-of select="n:Debiteur/n:InfoTiers/n:Civilite/@V"/>&nbsp;
                                        </xsl:if>
                                        <xsl:if test="string-length(n:Debiteur/n:InfoTiers/n:Prenom/@V)>0">
                                            <xsl:value-of select="n:Debiteur/n:InfoTiers/n:Prenom/@V"/>&nbsp;
                                        </xsl:if>
                                        <xsl:value-of select="n:Debiteur/n:InfoTiers/n:Nom/@V"/>
                                        <br/>
                                        <xsl:choose>
                                            <xsl:when test="n:Debiteur/n:Adresse/n:Adr1/@V">
                                                <xsl:value-of select="n:Debiteur/n:Adresse/n:Adr1/@V"/>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:choose>
                                                    <xsl:when test="n:Debiteur/n:Adresse/n:Adr2/@V">
                                                        <xsl:value-of select="n:Debiteur/n:Adresse/n:Adr2/@V"/>
                                                    </xsl:when>
                                                    <xsl:otherwise>
                                                        <xsl:value-of select="n:Debiteur/n:Adresse/n:Adr3/@V"/>
                                                    </xsl:otherwise>
                                                </xsl:choose>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                        <br/>
                                        <xsl:value-of select="n:Debiteur/n:Adresse/n:CP/@V"/>&nbsp;<xsl:value-of select="n:Debiteur/n:Adresse/n:Ville/@V"/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                            <td colspan="7">Facture N&#176;&nbsp;<xsl:value-of select="n:NumeroFacture/@V"/></td>
                            </tr>
                            <tr>
                                <td class="colonne1"></td>
                                <td class="entete colonne2">Mt unitaire</td>
                                <td class="entete colonne3">Qte</td>
                                <td class="entete colonne4">Mt HT</td>
                                <td class="entete colonne5">Tx Tva</td>
                                <td class="entete colonne6">Mt Tva</td>
                                <td class="entete colonne7">Mt TTC</td>
                            </tr>
                            <xsl:for-each select="n:LigneFacture">
                                <xsl:element name="tr">
                                    <xsl:if test="./@added:generated-id = $idLigne">
                                        <xsl:attribute name="class">highlighted</xsl:attribute>
                                    </xsl:if>
                                    <td class="colonne1"><xsl:value-of select="n:Libelle/@V"/>&nbsp;
                                        <xsl:choose>
                                            <xsl:when test="n:Date">&nbsp;le&nbsp;
                                                <xsl:call-template name="slash-date" >
                                                    <xsl:with-param name="datebrute" select="n:Date/@V" />
                                                </xsl:call-template>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                &nbsp;du&nbsp;
                                                <xsl:call-template name="slash-date" >
                                                    <xsl:with-param name="datebrute" select="n:DateDebut/@V" />
                                                </xsl:call-template>
                                                &nbsp;au&nbsp;
                                                <xsl:call-template name="slash-date" >
                                                    <xsl:with-param name="datebrute" select="n:DateFin/@V" />
                                                </xsl:call-template>
                                            </xsl:otherwise> 
                                        </xsl:choose>
                                    </td>
                                    <td class="colonne2">
                                        <xsl:call-template name="number">
                                            <xsl:with-param name="num" select="n:MtUnitaire/@V"/>
                                            <xsl:with-param name="monetaire" select="1"/>
                                        </xsl:call-template>&nbsp;</td>
                                    <td class="colonne2">
                                        <xsl:call-template name="number">
                                            <xsl:with-param name="num" select="n:Quantite/@V"/>
                                        </xsl:call-template>&nbsp;
                                    </td>
                                    <td class="colonne4">
                                        <xsl:call-template name="number">
                                            <xsl:with-param name="num" select="n:MtHT/@V"/>
                                            <xsl:with-param name="monetaire" select="1"/>
                                        </xsl:call-template>&nbsp;</td>
                                    <td class="colonne5">
                                        <xsl:if test="n:TauxTVA/@V != ''">
                                            <xsl:if test="n:TauxTVA/@V != 0">
                                                <xsl:call-template name="number">
                                                    <xsl:with-param name="num" select="n:TauxTVA/@V"/>
                                                </xsl:call-template>&nbsp;
                                            </xsl:if>
                                            <xsl:if test="n:TauxTVA/@V = 0">
                                                &nbsp;
                                            </xsl:if>
                                        </xsl:if>
                                        <xsl:if test="n:TauxTVA/@V = ''">
                                            &nbsp;
                                        </xsl:if>
                                    </td>
                                    <td class="colonne6">
                                        <xsl:if test="n:MtTVA/@V != 0">
                                            <xsl:call-template name="number">
                                                <xsl:with-param name="num" select="n:MtTVA/@V"/>
                                                <xsl:with-param name="monetaire" select="1"/>
                                            </xsl:call-template>&nbsp;
                                        </xsl:if>
                                        <xsl:if test="n:MtTVA/@V = 0">
                                            &nbsp;
                                        </xsl:if>
                                    </td>
                                    <td class="colonne7">
                                        <xsl:call-template name="number">
                                            <xsl:with-param name="num" select="n:MtTTC/@V"/>
                                            <xsl:with-param name="monetaire" select="1"/>
                                        </xsl:call-template>&nbsp;</td>
                                </xsl:element>
                            </xsl:for-each>

                    <xsl:variable name="lignes_5_50" select="n:LigneFacture[number(n:TauxTVA/@V)=5.5]"></xsl:variable>
                    <xsl:variable name="lignes_19_60" select="n:LigneFacture[number(n:TauxTVA/@V)=19.6]"></xsl:variable>
                    <xsl:if test="$lignes_5_50!='' or $lignes_19_60!=''">
                        <tr>
                            <td colspan="7"/>
                        </tr>
                        <tr>
                            <td colspan="3"/>
                            <td class="entete" colspan="4">Totaux T.V.A par Taux</td>
                        </tr>
                        <xsl:if test="$lignes_5_50!=''">
                            <tr>
                                <td colspan="3"/>
                                <td colspan="2" class="total_entete">5.5&nbsp;%</td>
                                <td colspan="2" class="total_valeur">
                                    <xsl:apply-templates select="$lignes_5_50"/>
                                    <xsl:call-template name="number">
                                        <xsl:with-param name="num" select="sum($lignes_5_50/n:MtTVA/@V)"/>
                                        <xsl:with-param name="monetaire" select="1"/>
                                    </xsl:call-template>
                                </td>
                            </tr>
                        </xsl:if>
                        <xsl:if test="$lignes_19_60!=''">
                            <tr>
                                <td colspan="3"/>
                                <td colspan="2" class="total_entete">19.6&nbsp;%</td>
                                <td colspan="2" class="total_valeur">
                                    <xsl:apply-templates select="$lignes_19_60"/>
                                    <xsl:call-template name="number">
                                        <xsl:with-param name="num" select="sum($lignes_19_60/n:MtTVA/@V)"/>
                                        <xsl:with-param name="monetaire" select="1"/>
                                    </xsl:call-template>
                                </td>
                            </tr>
                        </xsl:if>
                    </xsl:if>
                            <tr>
                                <td colspan="7"/>
                            </tr>
                            <tr>
                                <td colspan="3"/>
                                <td class="entete" colspan="4">Total</td>
                            </tr>
                            <tr>
                                <td colspan="3">Mode de r&#233;glement : 
                                    <xsl:value-of select="ModalitesReglement/@V"/>
                                </td>
                                <td colspan="2" class="total_entete">Montant HT</td>
                                <td colspan="2" class="total_valeur">
                                    <xsl:call-template name="number">
                                        <xsl:with-param name="num" select="n:TotalAPayer/n:MtTotalHT/@V"/>
                                        <xsl:with-param name="monetaire" select="1"/>
                                    </xsl:call-template>&nbsp;</td>
                            </tr>
                            <tr>
                                <td colspan="3">Conditions g&#233;n&#233;rales :
                                    <xsl:value-of select="ConditionsGen/@V"/>
                                </td>
                                <td colspan="2" class="total_entete">Montant TVA</td>
                                <td colspan="2" class="total_valeur">
                                    <xsl:call-template name="number">
                                        <xsl:with-param name="num" select="n:TotalAPayer/n:MtTotalTVA/@V"/>
                                        <xsl:with-param name="monetaire" select="1"/>
                                    </xsl:call-template>&nbsp;</td>
                            </tr>
                            <tr>
                                <td colspan="3"/>
                                <td colspan="2" class="total_entete">Montant TTC</td>
                                <td colspan="2" class="total_valeur">
                                    <xsl:call-template name="number">
                                        <xsl:with-param name="num" select="n:TotalAPayer/n:MtTotalTTC/@V"/>
                                        <xsl:with-param name="monetaire" select="1"/>
                                    </xsl:call-template>&nbsp;</td>
                            </tr>
                            </tbody>
                        </table>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template name="number">
        <xsl:param name="num"/>
        <xsl:param name="monetaire"/>
        <xsl:choose>
            <xsl:when test="$monetaire">
                <xsl:choose>
                    <xsl:when test="string-length(string($num)) = 0"/>
                    <xsl:when test="string(number($num)) = 'NaN'"/>
                    <xsl:otherwise>
                        <xsl:value-of select="format-number($num,'### ##0.00;-### ##0.00','decformat')"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="string-length($num) = 0"/>
                    <xsl:when test="string(number($num)) = 'NaN'"/>
                    <xsl:otherwise>
                        <xsl:value-of select="number($num)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
        
        
    </xsl:template>

</xsl:stylesheet>