<?xml version="1.0" encoding="UTF-8"?>
<!--
  Stampa Fatture - foglio di stile base (MIT)
  Visualizza una fattura elettronica ordinaria (FPR12 / FPA12) o semplificata
  (FSM10) in HTML.
  Nel tracciato FatturaPA solo l'elemento radice è qualificato: i figli non
  hanno namespace, quindi si usa /* per la radice e nomi semplici per il resto.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <html lang="it">
      <head>
        <meta charset="UTF-8"/>
        <title>Fattura elettronica</title>
        <style>
          body { font-family: system-ui, Arial, sans-serif; font-size: 13px; color: #111; margin: 24px; }
          h1 { font-size: 18px; margin: 0 0 4px; }
          h2 { font-size: 14px; margin: 24px 0 8px; border-bottom: 1px solid #ccc; padding-bottom: 4px; }
          .parti { display: flex; gap: 24px; }
          .parte { flex: 1; border: 1px solid #ddd; border-radius: 6px; padding: 12px; }
          .etichetta { color: #666; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
          table { width: 100%; border-collapse: collapse; }
          th, td { border-bottom: 1px solid #eee; padding: 6px 4px; text-align: left; vertical-align: top; }
          th { background: #f5f5f5; font-size: 11px; text-transform: uppercase; }
          td.n, th.n { text-align: right; white-space: nowrap; }
          .totale { font-size: 16px; font-weight: bold; text-align: right; margin-top: 12px; }
          .lotto { page-break-after: always; }
          .lotto:last-child { page-break-after: auto; }
          @media print { body { margin: 0; } }
        </style>
      </head>
      <body>
        <xsl:for-each select="/*/FatturaElettronicaBody">
          <div class="lotto">
            <xsl:apply-templates select="/*/FatturaElettronicaHeader"/>
            <xsl:apply-templates select="."/>
          </div>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>

  <!--
    Anagrafica di cedente / cessionario.
    Ordinaria:   DatiAnagrafici/(IdFiscaleIVA, CodiceFiscale, Anagrafica/...), Sede
    Semplificata cedente:     IdFiscaleIVA, CodiceFiscale, Denominazione|Nome+Cognome, Sede
    Semplificata cessionario: IdentificativiFiscali/(IdFiscaleIVA, CodiceFiscale),
                              AltriDatiIdentificativi/(Denominazione|Nome+Cognome, Sede)
  -->
  <xsl:template name="soggetto">
    <xsl:param name="s"/>
    <xsl:variable name="ana" select="($s | $s/DatiAnagrafici/Anagrafica | $s/AltriDatiIdentificativi)[Denominazione or Nome or Cognome][1]"/>
    <xsl:variable name="ids" select="$s/DatiAnagrafici | $s/IdentificativiFiscali | $s"/>
    <div><strong>
      <xsl:choose>
        <xsl:when test="$ana/Denominazione">
          <xsl:value-of select="$ana/Denominazione"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat($ana/Nome, ' ', $ana/Cognome)"/>
        </xsl:otherwise>
      </xsl:choose>
    </strong></div>
    <xsl:for-each select="$ids/IdFiscaleIVA[1]">
      <div>P.IVA <xsl:value-of select="concat(IdPaese, IdCodice)"/></div>
    </xsl:for-each>
    <xsl:for-each select="$ids/CodiceFiscale[1]">
      <div>C.F. <xsl:value-of select="."/></div>
    </xsl:for-each>
    <xsl:for-each select="$s/Sede | $s/AltriDatiIdentificativi/Sede">
      <div>
        <xsl:value-of select="Indirizzo"/>
        <xsl:if test="NumeroCivico"><xsl:text> </xsl:text><xsl:value-of select="NumeroCivico"/></xsl:if>
      </div>
      <div>
        <xsl:value-of select="concat(CAP, ' ', Comune)"/>
        <xsl:if test="Provincia"> (<xsl:value-of select="Provincia"/>)</xsl:if>
        <xsl:if test="Nazione != 'IT'"><xsl:text> - </xsl:text><xsl:value-of select="Nazione"/></xsl:if>
      </div>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="FatturaElettronicaHeader">
    <div class="parti">
      <div class="parte">
        <div class="etichetta">Cedente / prestatore</div>
        <xsl:call-template name="soggetto"><xsl:with-param name="s" select="CedentePrestatore"/></xsl:call-template>
      </div>
      <div class="parte">
        <div class="etichetta">Cessionario / committente</div>
        <xsl:call-template name="soggetto"><xsl:with-param name="s" select="CessionarioCommittente"/></xsl:call-template>
        <div style="margin-top:8px">
          <span class="etichetta">Codice destinatario </span>
          <xsl:value-of select="DatiTrasmissione/CodiceDestinatario"/>
          <xsl:if test="DatiTrasmissione/PECDestinatario">
            <br/><span class="etichetta">PEC </span><xsl:value-of select="DatiTrasmissione/PECDestinatario"/>
          </xsl:if>
        </div>
      </div>
    </div>
  </xsl:template>

  <xsl:template match="FatturaElettronicaBody">
    <xsl:variable name="dg" select="DatiGenerali/DatiGeneraliDocumento"/>
    <h2>
      Documento <xsl:value-of select="$dg/TipoDocumento"/>
      n. <xsl:value-of select="$dg/Numero"/>
      del <xsl:value-of select="$dg/Data"/>
      <xsl:if test="$dg/Divisa != 'EUR'"> (<xsl:value-of select="$dg/Divisa"/>)</xsl:if>
    </h2>
    <xsl:for-each select="$dg/Causale"><p><xsl:value-of select="."/></p></xsl:for-each>

    <xsl:choose>
      <xsl:when test="local-name(/*) = 'FatturaElettronicaSemplificata'">
        <xsl:call-template name="corpo-semplificata"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="corpo-ordinaria"/>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:if test="Allegati">
      <h2>Allegati</h2>
      <ul>
        <xsl:for-each select="Allegati">
          <li><xsl:value-of select="NomeAttachment"/>
            <xsl:if test="DescrizioneAttachment"> - <xsl:value-of select="DescrizioneAttachment"/></xsl:if></li>
        </xsl:for-each>
      </ul>
    </xsl:if>
  </xsl:template>

  <!-- Fattura semplificata (FSM10): righe con importo IVA inclusa, nessun riepilogo/pagamento -->
  <xsl:template name="corpo-semplificata">
    <xsl:variable name="dg" select="DatiGenerali/DatiGeneraliDocumento"/>
    <xsl:for-each select="DatiGenerali/DatiFatturaRettificata">
      <p>
        Rettifica della fattura n. <xsl:value-of select="NumeroFR"/> del <xsl:value-of select="DataFR"/>
        <xsl:if test="ElementiRettificati">: <xsl:value-of select="ElementiRettificati"/></xsl:if>
      </p>
    </xsl:for-each>

    <h2>Dettaglio</h2>
    <table>
      <tr>
        <th>Descrizione</th><th class="n">IVA %</th><th class="n">Imposta</th><th class="n">Importo (IVA inclusa)</th>
      </tr>
      <xsl:for-each select="DatiBeniServizi">
        <tr>
          <td>
            <xsl:value-of select="Descrizione"/>
            <xsl:if test="Natura"><br/><small>Natura <xsl:value-of select="Natura"/></small></xsl:if>
            <xsl:if test="RiferimentoNormativo"><br/><small><xsl:value-of select="RiferimentoNormativo"/></small></xsl:if>
          </td>
          <td class="n"><xsl:value-of select="DatiIVA/Aliquota"/></td>
          <td class="n"><xsl:value-of select="DatiIVA/Imposta"/></td>
          <td class="n"><xsl:value-of select="Importo"/></td>
        </tr>
      </xsl:for-each>
    </table>

    <div class="totale">Totale documento: <xsl:value-of select="format-number(sum(DatiBeniServizi/Importo), '0.00')"/></div>
    <xsl:if test="$dg/BolloVirtuale = 'SI'">
      <p style="text-align:right">Bollo virtuale assolto</p>
    </xsl:if>
  </xsl:template>

  <!-- Fattura ordinaria (FPR12 / FPA12) -->
  <xsl:template name="corpo-ordinaria">
    <xsl:variable name="dg" select="DatiGenerali/DatiGeneraliDocumento"/>
    <h2>Dettaglio</h2>
    <table>
      <tr>
        <th>#</th><th>Descrizione</th><th class="n">Quantità</th><th class="n">Prezzo unitario</th>
        <th class="n">IVA %</th><th class="n">Importo</th>
      </tr>
      <xsl:for-each select="DatiBeniServizi/DettaglioLinee">
        <tr>
          <td><xsl:value-of select="NumeroLinea"/></td>
          <td>
            <xsl:value-of select="Descrizione"/>
            <xsl:if test="Natura"><br/><small>Natura <xsl:value-of select="Natura"/></small></xsl:if>
          </td>
          <td class="n"><xsl:value-of select="Quantita"/><xsl:text> </xsl:text><xsl:value-of select="UnitaMisura"/></td>
          <td class="n"><xsl:value-of select="PrezzoUnitario"/></td>
          <td class="n"><xsl:value-of select="AliquotaIVA"/></td>
          <td class="n"><xsl:value-of select="PrezzoTotale"/></td>
        </tr>
      </xsl:for-each>
    </table>

    <h2>Riepilogo IVA</h2>
    <table>
      <tr>
        <th class="n">Aliquota %</th><th>Natura</th><th class="n">Imponibile</th><th class="n">Imposta</th>
        <th>Esigibilità</th><th>Riferimento normativo</th>
      </tr>
      <xsl:for-each select="DatiBeniServizi/DatiRiepilogo">
        <tr>
          <td class="n"><xsl:value-of select="AliquotaIVA"/></td>
          <td><xsl:value-of select="Natura"/></td>
          <td class="n"><xsl:value-of select="ImponibileImporto"/></td>
          <td class="n"><xsl:value-of select="Imposta"/></td>
          <td><xsl:value-of select="EsigibilitaIVA"/></td>
          <td><xsl:value-of select="RiferimentoNormativo"/></td>
        </tr>
      </xsl:for-each>
    </table>

    <xsl:if test="$dg/ImportoTotaleDocumento">
      <div class="totale">Totale documento: <xsl:value-of select="$dg/ImportoTotaleDocumento"/></div>
    </xsl:if>

    <xsl:if test="DatiPagamento">
      <h2>Pagamento</h2>
      <table>
        <tr><th>Modalità</th><th>Scadenza</th><th>IBAN</th><th class="n">Importo</th></tr>
        <xsl:for-each select="DatiPagamento/DettaglioPagamento">
          <tr>
            <td><xsl:value-of select="ModalitaPagamento"/></td>
            <td><xsl:value-of select="DataScadenzaPagamento"/></td>
            <td><xsl:value-of select="IBAN"/></td>
            <td class="n"><xsl:value-of select="ImportoPagamento"/></td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
