<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:inv="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
  xmlns:cn="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"
  xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
  xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
  xmlns:ccts="urn:un:unece:uncefact:documentation:2"
  xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
  exclude-result-prefixes="inv cn cac cbc ccts ext">

  <xsl:output method="html" encoding="UTF-8" indent="yes" doctype-system="about:legacy-compat"/>

  <!-- ================================================================== -->
  <!-- Root template                                                       -->
  <!-- ================================================================== -->
  <xsl:template match="/">
    <html>
      <head>
        <meta charset="UTF-8"/>
        <title>
          <xsl:choose>
            <xsl:when test="cn:CreditNote">
              <xsl:text>Nota de credit </xsl:text>
              <xsl:value-of select="cn:CreditNote/cbc:ID"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>Factura </xsl:text>
              <xsl:value-of select="inv:Invoice/cbc:ID"/>
            </xsl:otherwise>
          </xsl:choose>
        </title>
        <style>
          * { box-sizing: border-box; margin: 0; padding: 0; }
          body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-size: 12px; color: #333; padding: 20px; background: #fff; }
          .invoice-container { max-width: 210mm; margin: 0 auto; }

          /* Header */
          .invoice-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 20px; padding-bottom: 15px; border-bottom: 3px solid #2c3e50; }
          .invoice-title { font-size: 28px; font-weight: bold; color: #2c3e50; }
          .credit-note .invoice-title { color: #c0392b; }
          .credit-note .invoice-header { border-bottom-color: #c0392b; }
          .invoice-meta { text-align: right; }
          .invoice-meta table { margin-left: auto; }
          .invoice-meta td { padding: 2px 6px; }
          .invoice-meta td:first-child { font-weight: bold; text-align: right; color: #555; }
          .invoice-meta td:last-child { text-align: left; }

          /* Party blocks */
          .parties { display: flex; justify-content: space-between; gap: 20px; margin-bottom: 20px; }
          .party { flex: 1; border: 1px solid #ddd; border-radius: 4px; padding: 12px; background: #fafafa; }
          .party-label { font-size: 11px; font-weight: bold; text-transform: uppercase; color: #888; margin-bottom: 6px; letter-spacing: 0.5px; }
          .party-name { font-size: 16px; font-weight: bold; color: #2c3e50; margin-bottom: 6px; }
          .party-detail { font-size: 11px; color: #555; line-height: 1.6; }
          .party-detail span { display: block; }
          .party-detail .label { font-weight: 600; color: #444; display: inline; }

          /* Payment / delivery info */
          .info-section { margin-bottom: 20px; }
          .info-section h3 { font-size: 13px; font-weight: bold; color: #2c3e50; margin-bottom: 8px; padding-bottom: 4px; border-bottom: 1px solid #ddd; }
          .info-grid { display: flex; flex-wrap: wrap; gap: 8px 24px; }
          .info-item { font-size: 11px; }
          .info-item .label { font-weight: 600; color: #555; }

          /* Lines table */
          .lines-table { width: 100%; border-collapse: collapse; margin-bottom: 20px; font-size: 11px; }
          .lines-table thead th { background: #2c3e50; color: #fff; padding: 8px 6px; text-align: left; font-weight: 600; font-size: 10px; text-transform: uppercase; letter-spacing: 0.3px; }
          .credit-note .lines-table thead th { background: #c0392b; }
          .lines-table thead th.num { text-align: right; }
          .lines-table tbody td { padding: 6px; border-bottom: 1px solid #eee; vertical-align: top; }
          .lines-table tbody td.num { text-align: right; font-family: 'Consolas', 'Courier New', monospace; }
          .lines-table tbody tr:nth-child(even) { background: #f9f9f9; }
          .lines-table tbody tr:hover { background: #f0f4f8; }
          .item-name { font-weight: 600; }
          .item-desc { font-size: 10px; color: #777; margin-top: 2px; }
          .item-id { font-size: 10px; color: #999; }

          /* Tax summary */
          .tax-table { width: 50%; margin-left: auto; border-collapse: collapse; margin-bottom: 20px; font-size: 11px; }
          .tax-table th { background: #ecf0f1; padding: 6px; text-align: left; font-weight: 600; }
          .tax-table th.num { text-align: right; }
          .tax-table td { padding: 6px; border-bottom: 1px solid #eee; }
          .tax-table td.num { text-align: right; font-family: 'Consolas', 'Courier New', monospace; }

          /* Totals */
          .totals { width: 50%; margin-left: auto; margin-bottom: 20px; }
          .totals table { width: 100%; border-collapse: collapse; }
          .totals td { padding: 6px; font-size: 12px; }
          .totals td:first-child { text-align: right; color: #555; font-weight: 600; }
          .totals td:last-child { text-align: right; font-family: 'Consolas', 'Courier New', monospace; }
          .totals tr.total-payable { border-top: 2px solid #2c3e50; }
          .totals tr.total-payable td { font-size: 16px; font-weight: bold; color: #2c3e50; padding-top: 10px; }
          .credit-note .totals tr.total-payable td { color: #c0392b; }
          .credit-note .totals tr.total-payable { border-top-color: #c0392b; }

          /* Notes */
          .notes { margin-bottom: 20px; padding: 10px; background: #fffde7; border-left: 3px solid #f9a825; font-size: 11px; }
          .notes h3 { font-size: 12px; margin-bottom: 4px; color: #f57f17; }

          /* Allowance/Charge */
          .allowance-charge { margin-bottom: 15px; }
          .allowance-charge table { width: 60%; margin-left: auto; border-collapse: collapse; font-size: 11px; }
          .allowance-charge td { padding: 4px 6px; border-bottom: 1px solid #eee; }
          .allowance-charge td:first-child { color: #555; }
          .allowance-charge td:last-child { text-align: right; font-family: 'Consolas', 'Courier New', monospace; }
          .charge { color: #c0392b; }
          .allowance { color: #27ae60; }

          /* Footer */
          .footer { margin-top: 30px; padding-top: 10px; border-top: 1px solid #ddd; font-size: 10px; color: #999; text-align: center; }

          /* Print */
          @media print {
            body { padding: 0; font-size: 10px; }
            .invoice-container { max-width: 100%; }
            .lines-table thead th { background: #2c3e50 !important; color: #fff !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
            .credit-note .lines-table thead th { background: #c0392b !important; }
            .party { page-break-inside: avoid; }
          }
        </style>
      </head>
      <body>
        <div>
          <xsl:attribute name="class">
            <xsl:text>invoice-container</xsl:text>
            <xsl:if test="cn:CreditNote"> credit-note</xsl:if>
          </xsl:attribute>
          <xsl:apply-templates select="inv:Invoice"/>
          <xsl:apply-templates select="cn:CreditNote"/>
        </div>
      </body>
    </html>
  </xsl:template>

  <!-- ================================================================== -->
  <!-- Invoice                                                             -->
  <!-- ================================================================== -->
  <xsl:template match="inv:Invoice">
    <xsl:call-template name="RenderDocument">
      <xsl:with-param name="docTitle">FACTURA</xsl:with-param>
      <xsl:with-param name="typeCode" select="cbc:InvoiceTypeCode"/>
      <xsl:with-param name="lineNodeName">InvoiceLine</xsl:with-param>
      <xsl:with-param name="qtyNodeName">InvoicedQuantity</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <!-- ================================================================== -->
  <!-- CreditNote                                                          -->
  <!-- ================================================================== -->
  <xsl:template match="cn:CreditNote">
    <xsl:call-template name="RenderDocument">
      <xsl:with-param name="docTitle">NOTA DE CREDIT</xsl:with-param>
      <xsl:with-param name="typeCode" select="cbc:CreditNoteTypeCode"/>
      <xsl:with-param name="lineNodeName">CreditNoteLine</xsl:with-param>
      <xsl:with-param name="qtyNodeName">CreditedQuantity</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <!-- ================================================================== -->
  <!-- Shared document rendering                                           -->
  <!-- ================================================================== -->
  <xsl:template name="RenderDocument">
    <xsl:param name="docTitle"/>
    <xsl:param name="typeCode"/>
    <xsl:param name="lineNodeName"/>
    <xsl:param name="qtyNodeName"/>

    <!-- HEADER -->
    <div class="invoice-header">
      <div>
        <div class="invoice-title"><xsl:value-of select="$docTitle"/></div>
        <xsl:if test="$typeCode">
          <div style="font-size:11px; color:#888; margin-top:4px;">
            <xsl:text>Tip: </xsl:text>
            <xsl:call-template name="InvoiceTypeName">
              <xsl:with-param name="code" select="$typeCode"/>
            </xsl:call-template>
            <xsl:text> (</xsl:text>
            <xsl:value-of select="$typeCode"/>
            <xsl:text>)</xsl:text>
          </div>
        </xsl:if>
      </div>
      <div class="invoice-meta">
        <table>
          <tr>
            <td>Numar:</td>
            <td><strong><xsl:value-of select="cbc:ID"/></strong></td>
          </tr>
          <tr>
            <td>Data emitere:</td>
            <td><xsl:call-template name="FormatDate"><xsl:with-param name="date" select="cbc:IssueDate"/></xsl:call-template></td>
          </tr>
          <xsl:if test="cbc:DueDate">
            <tr>
              <td>Data scadenta:</td>
              <td><xsl:call-template name="FormatDate"><xsl:with-param name="date" select="cbc:DueDate"/></xsl:call-template></td>
            </tr>
          </xsl:if>
          <xsl:if test="cbc:TaxPointDate">
            <tr>
              <td>Data TVA:</td>
              <td><xsl:call-template name="FormatDate"><xsl:with-param name="date" select="cbc:TaxPointDate"/></xsl:call-template></td>
            </tr>
          </xsl:if>
          <tr>
            <td>Moneda:</td>
            <td><xsl:value-of select="cbc:DocumentCurrencyCode"/></td>
          </tr>
          <xsl:if test="cbc:TaxCurrencyCode and cbc:TaxCurrencyCode != cbc:DocumentCurrencyCode">
            <tr>
              <td>Moneda TVA:</td>
              <td><xsl:value-of select="cbc:TaxCurrencyCode"/></td>
            </tr>
          </xsl:if>
          <xsl:if test="cbc:AccountingCost">
            <tr>
              <td>Ref. contabila:</td>
              <td><xsl:value-of select="cbc:AccountingCost"/></td>
            </tr>
          </xsl:if>
          <xsl:if test="cbc:BuyerReference">
            <tr>
              <td>Ref. cumparator:</td>
              <td><xsl:value-of select="cbc:BuyerReference"/></td>
            </tr>
          </xsl:if>
        </table>
      </div>
    </div>

    <!-- NOTES -->
    <xsl:if test="cbc:Note">
      <div class="notes">
        <h3>Note</h3>
        <xsl:for-each select="cbc:Note">
          <div><xsl:value-of select="."/></div>
        </xsl:for-each>
      </div>
    </xsl:if>

    <!-- REFERENCES -->
    <xsl:if test="cac:InvoicePeriod or cac:OrderReference or cac:BillingReference or cac:DespatchDocumentReference or cac:ReceiptDocumentReference or cac:ContractDocumentReference or cac:OriginatorDocumentReference or cac:ProjectReference">
      <div class="info-section">
        <h3>Referinte document</h3>
        <div class="info-grid">
          <xsl:if test="cac:InvoicePeriod">
            <div class="info-item">
              <span class="label">Perioada facturare: </span>
              <xsl:call-template name="FormatDate"><xsl:with-param name="date" select="cac:InvoicePeriod/cbc:StartDate"/></xsl:call-template>
              <xsl:text> - </xsl:text>
              <xsl:call-template name="FormatDate"><xsl:with-param name="date" select="cac:InvoicePeriod/cbc:EndDate"/></xsl:call-template>
            </div>
          </xsl:if>
          <xsl:if test="cac:OrderReference/cbc:ID">
            <div class="info-item">
              <span class="label">Comanda: </span>
              <xsl:value-of select="cac:OrderReference/cbc:ID"/>
            </div>
          </xsl:if>
          <xsl:if test="cac:OrderReference/cbc:SalesOrderID">
            <div class="info-item">
              <span class="label">Comanda vanzare: </span>
              <xsl:value-of select="cac:OrderReference/cbc:SalesOrderID"/>
            </div>
          </xsl:if>
          <xsl:for-each select="cac:BillingReference/cac:InvoiceDocumentReference">
            <div class="info-item">
              <span class="label">Factura precedenta: </span>
              <xsl:value-of select="cbc:ID"/>
              <xsl:if test="cbc:IssueDate">
                <xsl:text> din </xsl:text>
                <xsl:call-template name="FormatDate"><xsl:with-param name="date" select="cbc:IssueDate"/></xsl:call-template>
              </xsl:if>
            </div>
          </xsl:for-each>
          <xsl:if test="cac:DespatchDocumentReference/cbc:ID">
            <div class="info-item">
              <span class="label">Aviz expeditie: </span>
              <xsl:value-of select="cac:DespatchDocumentReference/cbc:ID"/>
            </div>
          </xsl:if>
          <xsl:if test="cac:ReceiptDocumentReference/cbc:ID">
            <div class="info-item">
              <span class="label">Aviz receptie: </span>
              <xsl:value-of select="cac:ReceiptDocumentReference/cbc:ID"/>
            </div>
          </xsl:if>
          <xsl:if test="cac:ContractDocumentReference/cbc:ID">
            <div class="info-item">
              <span class="label">Contract: </span>
              <xsl:value-of select="cac:ContractDocumentReference/cbc:ID"/>
            </div>
          </xsl:if>
          <xsl:if test="cac:ProjectReference/cbc:ID">
            <div class="info-item">
              <span class="label">Proiect: </span>
              <xsl:value-of select="cac:ProjectReference/cbc:ID"/>
            </div>
          </xsl:if>
        </div>
      </div>
    </xsl:if>

    <!-- PARTIES -->
    <div class="parties">
      <!-- Supplier -->
      <div class="party">
        <div class="party-label">Furnizor</div>
        <div class="party-name">
          <xsl:value-of select="cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName"/>
        </div>
        <xsl:variable name="sCIF" select="cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID"/>
        <xsl:variable name="sLegalID" select="cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID"/>
        <xsl:variable name="sPartyID" select="cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID"/>
        <div class="party-detail">
          <!-- CIF/CUI: PartyTaxScheme/CompanyID, sau PartyLegalEntity/CompanyID daca NU incepe cu J -->
          <xsl:choose>
            <xsl:when test="$sCIF and string-length($sCIF) &gt; 0">
              <span><span class="label">CIF/CUI: </span><xsl:value-of select="$sCIF"/></span>
            </xsl:when>
            <xsl:when test="$sLegalID and string-length($sLegalID) &gt; 0 and not(starts-with($sLegalID, 'J'))">
              <span><span class="label">CIF/CUI: </span><xsl:value-of select="$sLegalID"/></span>
            </xsl:when>
          </xsl:choose>
          <!-- Reg. Com.: PartyLegalEntity/CompanyID doar daca incepe cu J, altfel PartyIdentification/ID daca incepe cu J -->
          <xsl:choose>
            <xsl:when test="$sLegalID and starts-with($sLegalID, 'J')">
              <span><span class="label">Reg. Com.: </span><xsl:value-of select="$sLegalID"/></span>
            </xsl:when>
            <xsl:when test="$sPartyID and starts-with($sPartyID, 'J')">
              <span><span class="label">Reg. Com.: </span><xsl:value-of select="$sPartyID"/></span>
            </xsl:when>
          </xsl:choose>
          <xsl:if test="cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyLegalForm">
            <span><span class="label">Capital social: </span><xsl:value-of select="cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyLegalForm"/></span>
          </xsl:if>
          <xsl:apply-templates select="cac:AccountingSupplierParty/cac:Party/cac:PostalAddress"/>
          <xsl:apply-templates select="cac:AccountingSupplierParty/cac:Party/cac:Contact"/>
        </div>
      </div>
      <!-- Customer -->
      <div class="party">
        <div class="party-label">Client</div>
        <div class="party-name">
          <xsl:value-of select="cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName"/>
        </div>
        <xsl:variable name="cCIF" select="cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID"/>
        <xsl:variable name="cLegalID" select="cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID"/>
        <xsl:variable name="cPartyID" select="cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID"/>
        <div class="party-detail">
          <!-- CIF/CUI: PartyTaxScheme/CompanyID, sau PartyLegalEntity/CompanyID daca NU incepe cu J -->
          <xsl:choose>
            <xsl:when test="$cCIF and string-length($cCIF) &gt; 0">
              <span><span class="label">CIF/CUI: </span><xsl:value-of select="$cCIF"/></span>
            </xsl:when>
            <xsl:when test="$cLegalID and string-length($cLegalID) &gt; 0 and not(starts-with($cLegalID, 'J'))">
              <span><span class="label">CIF/CUI: </span><xsl:value-of select="$cLegalID"/></span>
            </xsl:when>
          </xsl:choose>
          <!-- Reg. Com.: PartyLegalEntity/CompanyID doar daca incepe cu J, altfel PartyIdentification/ID daca incepe cu J -->
          <xsl:choose>
            <xsl:when test="$cLegalID and starts-with($cLegalID, 'J')">
              <span><span class="label">Reg. Com.: </span><xsl:value-of select="$cLegalID"/></span>
            </xsl:when>
            <xsl:when test="$cPartyID and starts-with($cPartyID, 'J')">
              <span><span class="label">Reg. Com.: </span><xsl:value-of select="$cPartyID"/></span>
            </xsl:when>
          </xsl:choose>
          <xsl:apply-templates select="cac:AccountingCustomerParty/cac:Party/cac:PostalAddress"/>
          <xsl:apply-templates select="cac:AccountingCustomerParty/cac:Party/cac:Contact"/>
        </div>
      </div>
    </div>

    <!-- DELIVERY -->
    <xsl:if test="cac:Delivery">
      <div class="info-section">
        <h3>Livrare</h3>
        <div class="info-grid">
          <xsl:if test="cac:Delivery/cbc:ActualDeliveryDate">
            <div class="info-item">
              <span class="label">Data livrare: </span>
              <xsl:call-template name="FormatDate"><xsl:with-param name="date" select="cac:Delivery/cbc:ActualDeliveryDate"/></xsl:call-template>
            </div>
          </xsl:if>
          <xsl:if test="cac:Delivery/cac:DeliveryLocation/cbc:ID">
            <div class="info-item">
              <span class="label">Locatie: </span>
              <xsl:value-of select="cac:Delivery/cac:DeliveryLocation/cbc:ID"/>
            </div>
          </xsl:if>
          <xsl:if test="cac:Delivery/cac:DeliveryLocation/cac:Address">
            <div class="info-item">
              <span class="label">Adresa livrare: </span>
              <xsl:call-template name="FormatAddress">
                <xsl:with-param name="addr" select="cac:Delivery/cac:DeliveryLocation/cac:Address"/>
              </xsl:call-template>
            </div>
          </xsl:if>
          <xsl:if test="cac:Delivery/cac:DeliveryParty/cac:PartyName/cbc:Name">
            <div class="info-item">
              <span class="label">Destinatar: </span>
              <xsl:value-of select="cac:Delivery/cac:DeliveryParty/cac:PartyName/cbc:Name"/>
            </div>
          </xsl:if>
        </div>
      </div>
    </xsl:if>

    <!-- PAYMENT MEANS -->
    <xsl:if test="cac:PaymentMeans">
      <div class="info-section">
        <h3>Modalitate de plata</h3>
        <div class="info-grid">
          <xsl:for-each select="cac:PaymentMeans">
            <div class="info-item">
              <span class="label">Cod plata: </span>
              <xsl:call-template name="PaymentMeansName">
                <xsl:with-param name="code" select="cbc:PaymentMeansCode"/>
              </xsl:call-template>
              <xsl:text> (</xsl:text><xsl:value-of select="cbc:PaymentMeansCode"/><xsl:text>)</xsl:text>
            </div>
            <xsl:if test="cbc:PaymentID">
              <div class="info-item">
                <span class="label">Ref. plata: </span>
                <xsl:value-of select="cbc:PaymentID"/>
              </div>
            </xsl:if>
            <xsl:if test="cac:PayeeFinancialAccount/cbc:ID">
              <div class="info-item">
                <span class="label">IBAN: </span>
                <xsl:value-of select="cac:PayeeFinancialAccount/cbc:ID"/>
              </div>
            </xsl:if>
            <xsl:if test="cac:PayeeFinancialAccount/cbc:Name">
              <div class="info-item">
                <span class="label">Titular cont: </span>
                <xsl:value-of select="cac:PayeeFinancialAccount/cbc:Name"/>
              </div>
            </xsl:if>
            <xsl:if test="cac:PayeeFinancialAccount/cac:FinancialInstitutionBranch/cbc:ID">
              <div class="info-item">
                <span class="label">BIC: </span>
                <xsl:value-of select="cac:PayeeFinancialAccount/cac:FinancialInstitutionBranch/cbc:ID"/>
              </div>
            </xsl:if>
          </xsl:for-each>
        </div>
      </div>
    </xsl:if>

    <!-- PAYMENT TERMS -->
    <xsl:if test="cac:PaymentTerms/cbc:Note">
      <div class="info-section">
        <h3>Termeni de plata</h3>
        <div style="font-size:11px;">
          <xsl:value-of select="cac:PaymentTerms/cbc:Note"/>
        </div>
      </div>
    </xsl:if>

    <!-- DOCUMENT-LEVEL ALLOWANCES/CHARGES -->
    <xsl:if test="cac:AllowanceCharge">
      <div class="allowance-charge">
        <div class="info-section">
          <h3>Reduceri / Majorari la nivel de document</h3>
        </div>
        <table>
          <xsl:for-each select="cac:AllowanceCharge">
            <tr>
              <td>
                <xsl:choose>
                  <xsl:when test="cbc:ChargeIndicator = 'true'">
                    <span class="charge">Majorare</span>
                  </xsl:when>
                  <xsl:otherwise>
                    <span class="allowance">Reducere</span>
                  </xsl:otherwise>
                </xsl:choose>
                <xsl:if test="cbc:AllowanceChargeReason">
                  <xsl:text> - </xsl:text>
                  <xsl:value-of select="cbc:AllowanceChargeReason"/>
                </xsl:if>
                <xsl:if test="cbc:AllowanceChargeReasonCode">
                  <xsl:text> (cod: </xsl:text>
                  <xsl:value-of select="cbc:AllowanceChargeReasonCode"/>
                  <xsl:text>)</xsl:text>
                </xsl:if>
              </td>
              <td>
                <xsl:if test="cbc:MultiplierFactorNumeric">
                  <xsl:value-of select="cbc:MultiplierFactorNumeric"/>
                  <xsl:text>% = </xsl:text>
                </xsl:if>
                <xsl:value-of select="format-number(cbc:Amount, '#,##0.00')"/>
                <xsl:text> </xsl:text>
                <xsl:value-of select="cbc:Amount/@currencyID"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </div>
    </xsl:if>

    <!-- INVOICE / CREDIT NOTE LINES -->
    <table class="lines-table">
      <thead>
        <tr>
          <th style="width:30px;">#</th>
          <th>Descriere</th>
          <th style="width:60px;">UM</th>
          <th class="num" style="width:70px;">Cantitate</th>
          <th class="num" style="width:90px;">Pret unitar</th>
          <xsl:if test="cac:InvoiceLine/cac:AllowanceCharge or cac:CreditNoteLine/cac:AllowanceCharge">
            <th class="num" style="width:80px;">Reducere</th>
          </xsl:if>
          <th class="num" style="width:50px;">TVA %</th>
          <th class="num" style="width:100px;">Valoare</th>
        </tr>
      </thead>
      <tbody>
        <!-- InvoiceLine -->
        <xsl:for-each select="cac:InvoiceLine">
          <xsl:call-template name="RenderLine">
            <xsl:with-param name="qtyNode" select="cbc:InvoicedQuantity"/>
            <xsl:with-param name="hasAnyDiscount" select="boolean(../cac:InvoiceLine/cac:AllowanceCharge)"/>
          </xsl:call-template>
        </xsl:for-each>
        <!-- CreditNoteLine -->
        <xsl:for-each select="cac:CreditNoteLine">
          <xsl:call-template name="RenderLine">
            <xsl:with-param name="qtyNode" select="cbc:CreditedQuantity"/>
            <xsl:with-param name="hasAnyDiscount" select="boolean(../cac:CreditNoteLine/cac:AllowanceCharge)"/>
          </xsl:call-template>
        </xsl:for-each>
      </tbody>
    </table>

    <!-- TAX BREAKDOWN -->
    <xsl:if test="cac:TaxTotal/cac:TaxSubtotal">
      <table class="tax-table">
        <thead>
          <tr>
            <th>Categorie TVA</th>
            <th class="num">Baza impozabila</th>
            <th class="num">Cota TVA</th>
            <th class="num">Valoare TVA</th>
          </tr>
        </thead>
        <tbody>
          <xsl:for-each select="cac:TaxTotal[1]/cac:TaxSubtotal">
            <tr>
              <td>
                <xsl:value-of select="cac:TaxCategory/cbc:ID"/>
                <xsl:if test="cac:TaxCategory/cbc:TaxExemptionReason">
                  <xsl:text> - </xsl:text>
                  <xsl:value-of select="cac:TaxCategory/cbc:TaxExemptionReason"/>
                </xsl:if>
              </td>
              <td class="num"><xsl:value-of select="format-number(cbc:TaxableAmount, '#,##0.00')"/></td>
              <td class="num">
                <xsl:choose>
                  <xsl:when test="cac:TaxCategory/cbc:Percent">
                    <xsl:value-of select="cac:TaxCategory/cbc:Percent"/>%
                  </xsl:when>
                  <xsl:otherwise>-</xsl:otherwise>
                </xsl:choose>
              </td>
              <td class="num"><xsl:value-of select="format-number(cbc:TaxAmount, '#,##0.00')"/></td>
            </tr>
          </xsl:for-each>
        </tbody>
      </table>
    </xsl:if>

    <!-- TOTALS -->
    <div class="totals">
      <table>
        <xsl:if test="cac:LegalMonetaryTotal/cbc:LineExtensionAmount">
          <tr>
            <td>Total linii (fara TVA):</td>
            <td>
              <xsl:value-of select="format-number(cac:LegalMonetaryTotal/cbc:LineExtensionAmount, '#,##0.00')"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="cbc:DocumentCurrencyCode"/>
            </td>
          </tr>
        </xsl:if>
        <xsl:if test="cac:LegalMonetaryTotal/cbc:AllowanceTotalAmount">
          <tr>
            <td>Total reduceri:</td>
            <td>
              <xsl:text>-</xsl:text>
              <xsl:value-of select="format-number(cac:LegalMonetaryTotal/cbc:AllowanceTotalAmount, '#,##0.00')"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="cbc:DocumentCurrencyCode"/>
            </td>
          </tr>
        </xsl:if>
        <xsl:if test="cac:LegalMonetaryTotal/cbc:ChargeTotalAmount">
          <tr>
            <td>Total majorari:</td>
            <td>
              <xsl:text>+</xsl:text>
              <xsl:value-of select="format-number(cac:LegalMonetaryTotal/cbc:ChargeTotalAmount, '#,##0.00')"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="cbc:DocumentCurrencyCode"/>
            </td>
          </tr>
        </xsl:if>
        <xsl:if test="cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount">
          <tr>
            <td>Total fara TVA:</td>
            <td>
              <xsl:value-of select="format-number(cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount, '#,##0.00')"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="cbc:DocumentCurrencyCode"/>
            </td>
          </tr>
        </xsl:if>
        <xsl:if test="cac:TaxTotal/cbc:TaxAmount">
          <tr>
            <td>Total TVA:</td>
            <td>
              <xsl:value-of select="format-number(cac:TaxTotal[1]/cbc:TaxAmount, '#,##0.00')"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="cac:TaxTotal[1]/cbc:TaxAmount/@currencyID"/>
            </td>
          </tr>
        </xsl:if>
        <xsl:if test="cac:TaxTotal[2]/cbc:TaxAmount">
          <tr>
            <td>Total TVA (moneda TVA):</td>
            <td>
              <xsl:value-of select="format-number(cac:TaxTotal[2]/cbc:TaxAmount, '#,##0.00')"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="cac:TaxTotal[2]/cbc:TaxAmount/@currencyID"/>
            </td>
          </tr>
        </xsl:if>
        <xsl:if test="cac:LegalMonetaryTotal/cbc:TaxInclusiveAmount">
          <tr>
            <td>Total cu TVA:</td>
            <td>
              <xsl:value-of select="format-number(cac:LegalMonetaryTotal/cbc:TaxInclusiveAmount, '#,##0.00')"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="cbc:DocumentCurrencyCode"/>
            </td>
          </tr>
        </xsl:if>
        <xsl:if test="cac:LegalMonetaryTotal/cbc:PrepaidAmount">
          <tr>
            <td>Platit in avans:</td>
            <td>
              <xsl:text>-</xsl:text>
              <xsl:value-of select="format-number(cac:LegalMonetaryTotal/cbc:PrepaidAmount, '#,##0.00')"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="cbc:DocumentCurrencyCode"/>
            </td>
          </tr>
        </xsl:if>
        <xsl:if test="cac:LegalMonetaryTotal/cbc:PayableRoundingAmount">
          <tr>
            <td>Rotunjire:</td>
            <td>
              <xsl:value-of select="format-number(cac:LegalMonetaryTotal/cbc:PayableRoundingAmount, '#,##0.00')"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="cbc:DocumentCurrencyCode"/>
            </td>
          </tr>
        </xsl:if>
        <tr class="total-payable">
          <td>TOTAL DE PLATA:</td>
          <td>
            <xsl:value-of select="format-number(cac:LegalMonetaryTotal/cbc:PayableAmount, '#,##0.00')"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="cbc:DocumentCurrencyCode"/>
          </td>
        </tr>
      </table>
    </div>

    <!-- ADDITIONAL DOCUMENTS -->
    <xsl:if test="cac:AdditionalDocumentReference">
      <div class="info-section">
        <h3>Documente atasate</h3>
        <div class="info-grid">
          <xsl:for-each select="cac:AdditionalDocumentReference">
            <div class="info-item">
              <span class="label"><xsl:value-of select="cbc:ID"/>: </span>
              <xsl:value-of select="cbc:DocumentDescription"/>
              <xsl:if test="cac:Attachment/cbc:EmbeddedDocumentBinaryObject/@filename">
                <xsl:text> [</xsl:text>
                <xsl:value-of select="cac:Attachment/cbc:EmbeddedDocumentBinaryObject/@filename"/>
                <xsl:text>]</xsl:text>
              </xsl:if>
              <xsl:if test="cac:Attachment/cac:ExternalReference/cbc:URI">
                <xsl:text> (</xsl:text>
                <xsl:value-of select="cac:Attachment/cac:ExternalReference/cbc:URI"/>
                <xsl:text>)</xsl:text>
              </xsl:if>
            </div>
          </xsl:for-each>
        </div>
      </div>
    </xsl:if>

    <!-- FOOTER -->
    <div class="footer">
      <xsl:text>Document generat din fisier UBL 2.1</xsl:text>
      <xsl:if test="cbc:UBLVersionID">
        <xsl:text> | UBL </xsl:text>
        <xsl:value-of select="cbc:UBLVersionID"/>
      </xsl:if>
      <xsl:if test="cbc:CustomizationID">
        <xsl:text> | </xsl:text>
        <xsl:value-of select="cbc:CustomizationID"/>
      </xsl:if>
    </div>

  </xsl:template>

  <!-- ================================================================== -->
  <!-- Render a single line (Invoice or CreditNote)                        -->
  <!-- ================================================================== -->
  <xsl:template name="RenderLine">
    <xsl:param name="qtyNode"/>
    <xsl:param name="hasAnyDiscount"/>
    <tr>
      <td><xsl:value-of select="cbc:ID"/></td>
      <td>
        <div class="item-name">
          <xsl:value-of select="cac:Item/cbc:Name"/>
        </div>
        <xsl:if test="cac:Item/cbc:Description">
          <div class="item-desc"><xsl:value-of select="cac:Item/cbc:Description"/></div>
        </xsl:if>
        <xsl:if test="cac:Item/cac:SellersItemIdentification/cbc:ID">
          <div class="item-id">
            <xsl:text>Cod furnizor: </xsl:text>
            <xsl:value-of select="cac:Item/cac:SellersItemIdentification/cbc:ID"/>
          </div>
        </xsl:if>
        <xsl:if test="cac:Item/cac:BuyersItemIdentification/cbc:ID">
          <div class="item-id">
            <xsl:text>Cod client: </xsl:text>
            <xsl:value-of select="cac:Item/cac:BuyersItemIdentification/cbc:ID"/>
          </div>
        </xsl:if>
        <xsl:if test="cac:Item/cac:StandardItemIdentification/cbc:ID">
          <div class="item-id">
            <xsl:text>Cod standard: </xsl:text>
            <xsl:value-of select="cac:Item/cac:StandardItemIdentification/cbc:ID"/>
            <xsl:if test="cac:Item/cac:StandardItemIdentification/cbc:ID/@schemeID">
              <xsl:text> (</xsl:text>
              <xsl:value-of select="cac:Item/cac:StandardItemIdentification/cbc:ID/@schemeID"/>
              <xsl:text>)</xsl:text>
            </xsl:if>
          </div>
        </xsl:if>
        <xsl:if test="cac:Item/cac:CommodityClassification/cbc:ItemClassificationCode">
          <div class="item-id">
            <xsl:text>Clasificare: </xsl:text>
            <xsl:value-of select="cac:Item/cac:CommodityClassification/cbc:ItemClassificationCode"/>
            <xsl:if test="cac:Item/cac:CommodityClassification/cbc:ItemClassificationCode/@listID">
              <xsl:text> (</xsl:text>
              <xsl:value-of select="cac:Item/cac:CommodityClassification/cbc:ItemClassificationCode/@listID"/>
              <xsl:text>)</xsl:text>
            </xsl:if>
          </div>
        </xsl:if>
        <xsl:if test="cbc:Note">
          <div class="item-desc" style="color:#666; font-style:italic;">
            <xsl:value-of select="cbc:Note"/>
          </div>
        </xsl:if>
      </td>
      <td>
        <xsl:value-of select="$qtyNode/@unitCode"/>
      </td>
      <td class="num">
        <xsl:value-of select="format-number($qtyNode, '#,##0.####')"/>
      </td>
      <td class="num">
        <xsl:value-of select="format-number(cac:Price/cbc:PriceAmount, '#,##0.####')"/>
        <xsl:if test="cac:Price/cac:AllowanceCharge/cbc:BaseAmount">
          <div class="item-desc">
            <xsl:text>Baza: </xsl:text>
            <xsl:value-of select="format-number(cac:Price/cac:AllowanceCharge/cbc:BaseAmount, '#,##0.####')"/>
          </div>
        </xsl:if>
      </td>
      <xsl:if test="$hasAnyDiscount">
        <td class="num">
          <xsl:for-each select="cac:AllowanceCharge">
            <div>
              <xsl:choose>
                <xsl:when test="cbc:ChargeIndicator = 'true'">
                  <span class="charge">+<xsl:value-of select="format-number(cbc:Amount, '#,##0.00')"/></span>
                </xsl:when>
                <xsl:otherwise>
                  <span class="allowance">-<xsl:value-of select="format-number(cbc:Amount, '#,##0.00')"/></span>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:if test="cbc:AllowanceChargeReason">
                <div class="item-desc"><xsl:value-of select="cbc:AllowanceChargeReason"/></div>
              </xsl:if>
            </div>
          </xsl:for-each>
        </td>
      </xsl:if>
      <td class="num">
        <xsl:choose>
          <xsl:when test="cac:Item/cac:ClassifiedTaxCategory/cbc:Percent">
            <xsl:value-of select="cac:Item/cac:ClassifiedTaxCategory/cbc:Percent"/>%
          </xsl:when>
          <xsl:when test="cac:Item/cac:ClassifiedTaxCategory/cbc:ID">
            <xsl:value-of select="cac:Item/cac:ClassifiedTaxCategory/cbc:ID"/>
          </xsl:when>
        </xsl:choose>
      </td>
      <td class="num">
        <strong><xsl:value-of select="format-number(cbc:LineExtensionAmount, '#,##0.00')"/></strong>
      </td>
    </tr>
  </xsl:template>

  <!-- ================================================================== -->
  <!-- Address template                                                    -->
  <!-- ================================================================== -->
  <xsl:template match="cac:PostalAddress">
    <xsl:if test="cbc:StreetName or cbc:AdditionalStreetName or cbc:CityName">
      <span>
        <span class="label">Adresa: </span>
        <xsl:value-of select="cbc:StreetName"/>
        <xsl:if test="cbc:AdditionalStreetName">
          <xsl:text>, </xsl:text>
          <xsl:value-of select="cbc:AdditionalStreetName"/>
        </xsl:if>
      </span>
      <span>
        <xsl:if test="cbc:PostalZone">
          <xsl:value-of select="cbc:PostalZone"/>
          <xsl:text> </xsl:text>
        </xsl:if>
        <xsl:value-of select="cbc:CityName"/>
        <xsl:if test="cbc:CountrySubentity">
          <xsl:text>, </xsl:text>
          <xsl:value-of select="cbc:CountrySubentity"/>
        </xsl:if>
        <xsl:if test="cac:Country/cbc:IdentificationCode">
          <xsl:text>, </xsl:text>
          <xsl:value-of select="cac:Country/cbc:IdentificationCode"/>
        </xsl:if>
      </span>
    </xsl:if>
  </xsl:template>

  <!-- ================================================================== -->
  <!-- Contact template                                                    -->
  <!-- ================================================================== -->
  <xsl:template match="cac:Contact">
    <xsl:if test="cbc:Name">
      <span><span class="label">Contact: </span><xsl:value-of select="cbc:Name"/></span>
    </xsl:if>
    <xsl:if test="cbc:Telephone">
      <span><span class="label">Telefon: </span><xsl:value-of select="cbc:Telephone"/></span>
    </xsl:if>
    <xsl:if test="cbc:ElectronicMail">
      <span><span class="label">Email: </span><xsl:value-of select="cbc:ElectronicMail"/></span>
    </xsl:if>
  </xsl:template>

  <!-- ================================================================== -->
  <!-- Format address inline                                               -->
  <!-- ================================================================== -->
  <xsl:template name="FormatAddress">
    <xsl:param name="addr"/>
    <xsl:if test="$addr/cbc:CountrySubentity">
      <xsl:value-of select="$addr/cbc:CountrySubentity"/>
      <xsl:text>, </xsl:text>
    </xsl:if>
    <xsl:if test="$addr/cbc:CityName">
      <xsl:value-of select="$addr/cbc:CityName"/>
      <xsl:text>, </xsl:text>
    </xsl:if>
    <xsl:if test="$addr/cbc:StreetName">
      <xsl:text>Str. </xsl:text>
      <xsl:value-of select="$addr/cbc:StreetName"/>
    </xsl:if>
    <xsl:if test="$addr/cbc:AdditionalStreetName">
      <xsl:text>, </xsl:text>
      <xsl:value-of select="$addr/cbc:AdditionalStreetName"/>
    </xsl:if>
    <xsl:if test="$addr/cbc:PostalZone">
      <xsl:text>, </xsl:text>
      <xsl:value-of select="$addr/cbc:PostalZone"/>
    </xsl:if>
    <xsl:if test="$addr/cac:Country/cbc:IdentificationCode">
      <xsl:text>, </xsl:text>
      <xsl:value-of select="$addr/cac:Country/cbc:IdentificationCode"/>
    </xsl:if>
  </xsl:template>

  <!-- ================================================================== -->
  <!-- Format date dd.mm.yyyy                                              -->
  <!-- ================================================================== -->
  <xsl:template name="FormatDate">
    <xsl:param name="date"/>
    <xsl:if test="$date and string-length($date) >= 10">
      <xsl:value-of select="substring($date, 9, 2)"/>
      <xsl:text>.</xsl:text>
      <xsl:value-of select="substring($date, 6, 2)"/>
      <xsl:text>.</xsl:text>
      <xsl:value-of select="substring($date, 1, 4)"/>
    </xsl:if>
  </xsl:template>

  <!-- ================================================================== -->
  <!-- Invoice type code to name                                           -->
  <!-- ================================================================== -->
  <xsl:template name="InvoiceTypeName">
    <xsl:param name="code"/>
    <xsl:choose>
      <xsl:when test="$code = '380'">Factura comerciala</xsl:when>
      <xsl:when test="$code = '381'">Nota de credit</xsl:when>
      <xsl:when test="$code = '383'">Nota de debit</xsl:when>
      <xsl:when test="$code = '384'">Factura corectata</xsl:when>
      <xsl:when test="$code = '386'">Factura proforma</xsl:when>
      <xsl:when test="$code = '389'">Autofactura</xsl:when>
      <xsl:when test="$code = '751'">Factura - informatii in scopuri contabile</xsl:when>
      <xsl:otherwise>Tip <xsl:value-of select="$code"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- ================================================================== -->
  <!-- Payment means code to name                                          -->
  <!-- ================================================================== -->
  <xsl:template name="PaymentMeansName">
    <xsl:param name="code"/>
    <xsl:choose>
      <xsl:when test="$code = '1'">Instrument nespecificat</xsl:when>
      <xsl:when test="$code = '10'">Numerar</xsl:when>
      <xsl:when test="$code = '20'">Cec</xsl:when>
      <xsl:when test="$code = '30'">Transfer credit</xsl:when>
      <xsl:when test="$code = '31'">Debit direct</xsl:when>
      <xsl:when test="$code = '42'">Transfer bancar</xsl:when>
      <xsl:when test="$code = '48'">Card bancar</xsl:when>
      <xsl:when test="$code = '49'">Debit direct</xsl:when>
      <xsl:when test="$code = '54'">Card de credit</xsl:when>
      <xsl:when test="$code = '55'">Card de debit</xsl:when>
      <xsl:when test="$code = '57'">Standing agreement</xsl:when>
      <xsl:when test="$code = '58'">Transfer SEPA</xsl:when>
      <xsl:when test="$code = '59'">Debit direct SEPA</xsl:when>
      <xsl:when test="$code = '97'">Compensare</xsl:when>
      <xsl:when test="$code = 'ZZZ'">Convenit reciproc</xsl:when>
      <xsl:otherwise>Cod <xsl:value-of select="$code"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
