<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
	              xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:n1="http://www.sat.gob.mx/cfd/3"
	              xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital"
	              xmlns:ine="http://www.sat.gob.mx/ine"
	              xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	              xmlns:barcode="org.krysalis.barcode4j.xalan.BarcodeExt"
	              xmlns:bc="http://barcode4j.krysalis.org/ns"
	              xmlns:common="http://exslt.org/common"
	              xmlns:xalan="http://xml.apache.org"
	              exclude-result-prefixes="barcode common xalan">

  <xsl:param name="totalLetra" select="'N/A'"/>
  <xsl:param name="urlReferenciaQr"/>
  <xsl:param name="textoValidez"/>
  <xsl:param name="telefonoReceptor"/>
  <xsl:param name="documento"/>

  <xsl:variable name="LugarExpedicion">
    <xsl:for-each select="n1:Comprobante">
      <xsl:value-of select="@LugarExpedicion" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="serieFolioFiscal">
    <xsl:for-each select="n1:Comprobante">
      <xsl:value-of select="concat(@serie,' ',@folio)" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="subTotal">
    <xsl:for-each select="n1:Comprobante">
      <xsl:value-of select="format-number(number(string(@subTotal)), '###,##0.00')" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="total">
    <xsl:for-each select="n1:Comprobante">
      <xsl:value-of select="format-number(number(string(@total)), '###,##0.00')" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="metodoPago">
    <xsl:for-each select="n1:Comprobante">
      <xsl:value-of select="@metodoDePago" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="numeroCuentaPago">
    <xsl:for-each select="n1:Comprobante">
      <xsl:value-of select="@NumCtaPago" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="condicionesDePagoComprobante">
    <xsl:for-each select="n1:Comprobante">
      <xsl:value-of select="@condicionesDePago" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="formaPagoComprobante">
    <xsl:for-each select="n1:Comprobante">
      <xsl:value-of select="@formaDePago" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="iva">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Impuestos">
        <xsl:for-each select="n1:Traslados">
          <xsl:for-each select="n1:Traslado">
            <xsl:value-of select="format-number(number(string(@importe)), '###,##0.00')" />
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="fecha">
    <xsl:for-each select="n1:Comprobante">
      <xsl:value-of select="@fecha" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="emisorNombre">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Emisor">
        <xsl:value-of select="@nombre" />
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="emisorRfc">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Emisor">
        <xsl:value-of select="@rfc" />
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="regimenFiscalEmisor">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Emisor">
        <xsl:for-each select="n1:RegimenFiscal">
          <xsl:value-of select="@Regimen" />
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="emisorDomicilioFiscal">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Emisor">
        <xsl:for-each select="n1:DomicilioFiscal">
          <xsl:value-of select="concat(@calle,' ',@noExterior,', ',@colonia,', ',@municipio,', CP. ',@codigoPostal,', ',@estado,', ',@pais)" />
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="receptorNombre">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Receptor">
        <xsl:value-of select="@nombre" />
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="receptorRfc">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Receptor">
        <xsl:value-of select="@rfc" />
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="receptorCalle">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Receptor">
        <xsl:for-each select="n1:Domicilio">
          <xsl:value-of select="@calle" />
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="receptorColonia">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Receptor">
        <xsl:for-each select="n1:Domicilio">
          <xsl:value-of select="@colonia" />
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="receptorCodigoPostal">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Receptor">
        <xsl:for-each select="n1:Domicilio">
          <xsl:value-of select="@codigoPostal" />
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="receptorCiudad">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Receptor">
        <xsl:for-each select="n1:Domicilio">
          <xsl:value-of select="@municipio" />
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="receptorEstado">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Receptor">
        <xsl:for-each select="n1:Domicilio">
          <xsl:value-of select="@estado" />
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="receptorPais">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Receptor">
        <xsl:for-each select="n1:Domicilio">
          <xsl:value-of select="@pais" />
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="cadenaOriginalComplemento">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Complemento">
        <xsl:for-each select="tfd:TimbreFiscalDigital">
          <fo:inline>
            <xsl:value-of select="concat('||',@version,'|',@UUID,'|',@FechaTimbrado,'|',@selloCFD,'|',@noCertificadoSAT,'||')" />
          </fo:inline>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="urlImageQR">
    <xsl:for-each select="n1:Comprobante">
      <xsl:for-each select="n1:Complemento">
        <xsl:for-each select="tfd:TimbreFiscalDigital">
          <fo:inline>
            <xsl:value-of select="concat('C:\Timbres\',@UUID,'.jpg')" />
          </fo:inline>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>

  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm">
          <fo:region-body margin-top="7.0cm" margin-bottom="0.5cm" />
          <fo:region-before extent="7.0cm"/>
          <fo:region-after extent="7.5cm" />
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="simple" initial-page-number="1">
        <fo:static-content flow-name="xsl-region-before" >
          <fo:block font-size="13.0pt" font-family="serif" padding-after="0.5pt" space-before="0.5pt" text-align="right" border-top-style="solid" border-top-width="1.0pt" border-left-width="1.0pt">
            <fo:table width="100%">
              <fo:table-column column-width="6.0cm" />
              <!--Tamaño de las filas de los datos de la factura-->
              <fo:table-body>
                <fo:table-row keep-together.within-page="always">
                  <fo:table-cell display-align="center">
                    <fo:block font-size="8pt" font-family="sans-serif" color="white" text-align="center" padding-top="1pt" height="3mm">
                      <fo:external-graphic src='url("C:\facturas\recursos\logoCuchillaRojo.png")' content-width="5.0cm" content-height="scale-to-fit" />
                    </fo:block>
                  </fo:table-cell>



                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <fo:table width="100%">
                        <!--Tabla para los datos de la Factura-->
                        <fo:table-column column-width="3.5cm" />
                        <fo:table-column column-width="8.5cm" />
                        <fo:table-body>
                          <fo:table-row keep-together.within-page="always">
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                                <fo:leader />
                                <!--Rellenar una celda con un espacio en blanco-->
                              </fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="1mm">
                                <fo:leader />
                                <!--Rellenar una celda con un espacio en blanco-->
                              </fo:block>
                            </fo:table-cell>
                          </fo:table-row>
                          <fo:table-row keep-together.within-page="always" background-color="lightgrey">
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="right" padding-top="1pt" height="3mm" font-weight="bold">
                                SERIE FOLIO:<fo:leader />
                              </fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                                <xsl:value-of select="$serieFolioFiscal" />
                              </fo:block>
                            </fo:table-cell>
                          </fo:table-row>
                          <fo:table-row keep-together.within-page="always">
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="right" padding-top="1pt" height="3mm" font-weight="bold">
                                FECHA:<fo:leader />
                              </fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                                <xsl:value-of select="$fecha" />
                              </fo:block>
                            </fo:table-cell>
                          </fo:table-row>
                          <fo:table-row keep-together.within-page="always">
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="right" padding-top="1pt" height="3mm" font-weight="bold">
                                DOCUMENTO:<fo:leader />
                              </fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                                <xsl:value-of select="$documento" />
                                <!--Numero de pedido, Credito, Nota o Ticket-->
                              </fo:block>
                            </fo:table-cell>
                          </fo:table-row>
                          <fo:table-row keep-together.within-page="always">
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                                <fo:leader />
                                <!--Rellenar una celda con un espacio en blanco-->
                              </fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="1mm">
                                <fo:leader />
                                <!--Rellenar una celda con un espacio en blanco-->
                              </fo:block>
                            </fo:table-cell>
                          </fo:table-row>
                          <fo:table-row keep-together.within-page="always" display-align="center" >
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="right" padding-top="1pt" height="3mm" font-weight="bold">
                                LUGAR DE EXPEDICION:<fo:leader />
                              </fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                                <xsl:value-of select="$LugarExpedicion" />
                              </fo:block>
                            </fo:table-cell>
                          </fo:table-row>
                          <fo:table-row keep-together.within-page="always">
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" color="white" text-align="left" padding-top="1pt" height="3mm">
                                <fo:leader />
                                <!--Rellenar una celda con un espacio en blanco-->
                              </fo:block>
                            </fo:table-cell>
                            <fo:table-cell>
                              <fo:block font-size="7pt" font-family="sans-serif" color="white" text-align="left" padding-top="1pt" height="1mm">
                                <fo:leader />
                                <!--Rellenar una celda con un espacio en blanco-->
                              </fo:block>
                            </fo:table-cell>
                          </fo:table-row>
                        </fo:table-body>
                      </fo:table>

                      <fo:table width="100%">
                        <fo:table-body>
                          <fo:table-row keep-together.within-page="always">
                            <fo:table-cell border-style="solid">
                              <fo:block font-size="7pt" font-weight="bold" background-color="lightgrey" text-align="center" height="3mm">
                                <xsl:value-of select="$textoValidez" />
                              </fo:block>
                            </fo:table-cell>
                          </fo:table-row>
                        </fo:table-body>
                      </fo:table>
                    </fo:block>
                  </fo:table-cell>

                </fo:table-row>
                <fo:table-row keep-together.within-page="always">
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <fo:leader />
                      <!--Rellenar una celda con un espacio en blanco-->
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <fo:leader />
                      <!--Rellenar una celda con un espacio en blanco-->
                    </fo:block>
                  </fo:table-cell>
                </fo:table-row>
              </fo:table-body>
            </fo:table>

            <!--tabla para el nombre de la empresa y los datos de su direccion-->
            <fo:table width="100%" border-style="solid">
              <fo:table-column column-width="5.0cm" />
              <!--Tamaño de las filas de la empresa y su direccion-->
              <fo:table-body>
                <fo:table-row keep-together.within-page="always" background-color="lightgrey">
                  <fo:table-cell display-align="center" font-weight="bold">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="center" padding-top="1pt">
                      <xsl:value-of select="$emisorNombre" />
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <xsl:value-of select="concat($emisorRfc,' ',$emisorDomicilioFiscal)" />
                    </fo:block>
                  </fo:table-cell>
                </fo:table-row>
              </fo:table-body>
            </fo:table>

            <!--Empieza la seccion de los datos del cliente-->
            <fo:leader />
            <fo:table width="100%" display-align="center" border-style="solid">
              <fo:table-column column-width="2.0cm" />
              <fo:table-column column-width="4.0cm" />
              <fo:table-column column-width="2.0cm" />
              <fo:table-column column-width="8.0cm" />
              <fo:table-column column-width="1.0cm" />
              <fo:table-body>
                <fo:table-row keep-together.within-page="always">
                  <fo:table-cell font-weight="bold">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt">
                      CLIENTE:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell number-columns-spanned="5">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <xsl:value-of select="$receptorNombre" />
                    </fo:block>
                  </fo:table-cell>
                </fo:table-row>
                <fo:table-row keep-together.within-page="always">
                  <fo:table-cell font-weight="bold">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt">
                      RFC:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell  number-columns-spanned="5">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <xsl:value-of select="$receptorRfc" />
                    </fo:block>
                  </fo:table-cell>
                </fo:table-row>
                <fo:table-row keep-together.within-page="always">
                  <fo:table-cell font-weight="bold">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt">
                      DOMICILIO:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell number-columns-spanned="5">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <xsl:value-of select="$receptorCalle" />
                    </fo:block>
                  </fo:table-cell>
                </fo:table-row>
                <fo:table-row keep-together.within-page="always">
                  <fo:table-cell font-weight="bold">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt">
                      TELEFONO:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <xsl:value-of select="$telefonoReceptor" />
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell font-weight="bold">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt">
                      COLONIA:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <xsl:value-of select="$receptorColonia" />
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell font-weight="bold">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt">
                      C.P.:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <xsl:value-of select="$receptorCodigoPostal" />
                    </fo:block>
                  </fo:table-cell>
                </fo:table-row>
                <fo:table-row keep-together.within-page="always">
                  <fo:table-cell font-weight="bold">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt">
                      CIUDAD:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <xsl:value-of select="$receptorCiudad" />
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell font-weight="bold">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt">
                      ESTADO:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <xsl:value-of select="$receptorEstado" />
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell font-weight="bold">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt">
                      PAIS:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" height="3mm">
                      <xsl:value-of select="$receptorPais" />
                    </fo:block>
                  </fo:table-cell>
                </fo:table-row>
              </fo:table-body>
            </fo:table>
          </fo:block>
        </fo:static-content>
        <fo:static-content flow-name="xsl-region-after">
          <fo:block font-size="12.0pt" font-family="sans-serif" padding-bottom="0.5cm" text-align="center" border-bottom-style="solid" border-bottom-width="1.0pt">


            <!-- INICIA APARTADO PARA LOS TOTALES Y EL IMPORTE CON LETRA-->
            <fo:table width="100%">
              <fo:table-column column-width="12.0cm" />
              <!--CodigoInterno-->
              <fo:table-column column-width="1.0cm" />
              <!--CodigoInterno-->
              <fo:table-column column-width="2.cm" />
              <!--CodigoInterno-->
              <fo:table-column column-width="4.0cm" />
              <!--CodigoInterno-->
              <fo:table-body>
                <fo:table-row keep-together.within-page="always">
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="center" padding-top="1pt" background-color="lightgrey" font-weight="bold">
                      IMPORTE CON LETRA
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" padding-top="1pt">
                      <fo:leader />
                      <!--Rellenar una celda con un espacio en blanco-->
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell border-bottom-style="solid">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="right"  padding-top="1pt" font-weight="bold">
                      SUBTOTAL:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell border-bottom-style="solid">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="right" padding-top="1pt">
                      <xsl:value-of select="$subTotal" />
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="right" padding-top="1pt"></fo:block>
                  </fo:table-cell>
                </fo:table-row>
                <fo:table-row keep-together.within-page="always">
                  <fo:table-cell>
                    <fo:block font-size="6pt" font-family="sans-serif" text-align="center" padding-top="1pt">
                      <xsl:value-of select="$totalLetra" />
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" padding-top="1pt" >
                      <fo:leader />
                      <!--Rellenar una celda con un espacio en blanco-->
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell border-bottom-style="solid">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="right"  padding-top="1pt" font-weight="bold" >
                      IVA:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell border-bottom-style="solid">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="right" padding-top="1pt">
                      <xsl:value-of select="$iva" />
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="right" padding-top="1pt"></fo:block>
                  </fo:table-cell>
                </fo:table-row>
                <fo:table-row keep-together.within-page="always">
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="center" padding-top="1pt">
                      <fo:leader />
                      <!--Rellenar una celda con un espacio en blanco-->
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell>
                    <fo:block font-size="8pt" font-family="sans-serif" padding-top="1pt">
                      <fo:leader />
                      <!--Rellenar una celda con un espacio en blanco-->
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell border-bottom-style="solid">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="right"  padding-top="1pt" font-weight="bold">
                      TOTAL:
                    </fo:block>
                  </fo:table-cell>
                  <fo:table-cell border-bottom-style="solid">
                    <fo:block font-size="8pt" font-family="sans-serif" text-align="right" padding-top="1pt">
                      <xsl:value-of select="$total" />
                    </fo:block>
                  </fo:table-cell>
                </fo:table-row>
              </fo:table-body>
            </fo:table>
            <!-- INICIA APARTADO PARA LOS TOTALES Y EL IMPORTE CON LETRA-->

            <!-- Se inserta un espacio en blanco-->
            <fo:leader />




            <xsl:apply-templates select="n1:Comprobante/n1:Complemento/tfd:TimbreFiscalDigital"/>
          </fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates select="n1:Comprobante"/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="n1:Comprobante">
    <fo:block text-align="center">
      <fo:table width="100%">
        <fo:table-column column-width="2.0cm" />
        <!--CodigoInterno-->
        <fo:table-column column-width="9.0cm" />
        <!--descripcion-->
        <fo:table-column column-width="2.0cm" />
        <!--precio Unitario-->
        <fo:table-column column-width="2.0cm" />
        <!--Cantidad-->
        <fo:table-column column-width="2.0cm" />
        <!--unidad-->
        <fo:table-column column-width="2.0cm" />
        <!--Importe-->
        <fo:table-body>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" background-color="lightgrey" text-align="left" padding-top="1pt" height="3mm">
                Codigo
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" background-color="lightgrey" text-align="left" padding-top="1pt">
                Producto
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" background-color="lightgrey" text-align="center" padding-top="1pt">
                P. Unitario
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" background-color="lightgrey" text-align="center" padding-top="1pt">
                Cantidad
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" background-color="lightgrey" text-align="center" padding-top="1pt">
                Unidad
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" background-color="lightgrey" text-align="center" padding-top="1pt">
                Importe
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
        </fo:table-body>
      </fo:table>
      <fo:table table-layout="fixed" width="100%">
        <fo:table-column column-width="2.0cm" />
        <!--CodigoInterno-->
        <fo:table-column column-width="9.0cm" />
        <!--descripcion-->
        <fo:table-column column-width="2.0cm" />
        <!--precio Unitario-->
        <fo:table-column column-width="2.0cm" />
        <!--Cantidad-->
        <fo:table-column column-width="2.0cm" />
        <!--unidad-->
        <fo:table-column column-width="2.0cm" />
        <!--Importe-->
        <fo:table-body>
          <xsl:apply-templates select="n1:Conceptos/n1:Concepto"/>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="center" padding-top="1pt" font-weight="bold">
                <fo:leader />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" padding-top="1pt" >
                <fo:leader />
                <!--Rellenar una celda con un espacio en blanco-->
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="right"  padding-top="1pt" font-weight="bold">
                <fo:leader />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="right" padding-top="1pt">
                <fo:leader />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="right" padding-top="1pt"></fo:block>
            </fo:table-cell>
          </fo:table-row>



        </fo:table-body>
      </fo:table>
    </fo:block>
  </xsl:template>

  <xsl:template match="tfd:TimbreFiscalDigital">
    <fo:block border-style="solid">
      <fo:table display-align="center" width="100%">
        <fo:table-column column-width="4.5cm" />
        <!--espacioEnBlanco-->
        <fo:table-column column-width="5.5cm" />
        <!--espacioEnBlanco-->
        <fo:table-column column-width="5.0cm" />
        <!--espacioEnBlanco-->
        <fo:table-column column-width="4.0cm" />
        <!--espacioEnBlanco-->
        <fo:table-body>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" font-weight="bold">
                Folio Fiscal:
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" >
                <xsl:value-of select="@UUID" />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" font-weight="bold">
                No de Serie del Certificado del SAT:
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" >
                <xsl:value-of select="@noCertificadoSAT" />
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" font-weight="bold">
                Fecha y Hora de Certificacion:
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" >
                <xsl:value-of select="@FechaTimbrado" />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" font-weight="bold">
                Metodo de Pago:
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" >
                <xsl:value-of select="$metodoPago" />
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" font-weight="bold">
                Numero de Cuenta de Pago:
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" >
                <xsl:value-of select="$numeroCuentaPago" />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" font-weight="bold">
                Condiciones de Pago:
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" >
                <xsl:value-of select="$condicionesDePagoComprobante" />
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" font-weight="bold">
                Forma de Pago:
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" >
                <xsl:value-of select="$formaPagoComprobante" />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" font-weight="bold">
                Regimen Fiscal:
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="left" padding-top="1pt" >
                <xsl:value-of select="$regimenFiscalEmisor" />
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
        </fo:table-body>
      </fo:table>
    </fo:block>

    <fo:table display-align="center" width="100%" background-color="grey" border-style="solid">
      <fo:table-body>
        <fo:table-row keep-together.within-page="always">
          <fo:table-cell>
            <fo:block font-size="8pt" font-family="sans-serif" text-align="center" padding-top="1pt" font-weight="bold">
              Este documento es una representación impresa de un CFDI
            </fo:block>
          </fo:table-cell>
        </fo:table-row>
      </fo:table-body>
    </fo:table>

    <fo:block border-style="solid">
      <fo:table display-align="center" width="100%">
        <fo:table-column column-width="3.5cm" />
        <fo:table-body>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell number-rows-spanned="6">
              <fo:block>
                <!--<fo:external-graphic src='url(concat("C:\Timbres\",@UUID,".jpg"))' content-width="7cm" content-height="scale-to-fit" />-->
                <fo:external-graphic src="{$urlReferenciaQr}" content-width="3.0cm" content-height="scale-to-fit" />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="center" padding-top="1pt" background-color="lightgrey">
                Sello Digital del CFDI
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="6pt" font-family="sans-serif" text-align="left" padding-top="1pt" >
                <!--<xsl:value-of select="@selloCFD" />-->
                <fo:inline font-family="arial, sans-serif" font-size="6pt" >
                  <xsl:value-of select="substring(@selloCFD,1,120)" />
                  <fo:block></fo:block>
                  <fo:inline>
                    <xsl:value-of select="substring(@selloCFD,121,120)" />
                  </fo:inline>
                  <fo:block></fo:block>
                  <fo:inline>
                    <xsl:value-of select="substring(@selloCFD,241,120)" />
                  </fo:inline>
                </fo:inline>
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="center" padding-top="1pt" background-color="lightgrey">
                Sello del SAT
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="6pt" font-family="sans-serif" text-align="left" padding-top="1pt">
                <!--<xsl:value-of select="@selloSAT" />-->
                <fo:inline font-family="arial, sans-serif" font-size="6pt" >
                  <xsl:value-of select="substring(@selloSAT,1,120)" />
                  <fo:block></fo:block>
                  <fo:inline>
                    <xsl:value-of select="substring(@selloSAT,121,120)" />
                  </fo:inline>
                  <fo:block></fo:block>
                  <fo:inline>
                    <xsl:value-of select="substring(@selloSAT,241,120)" />
                  </fo:inline>
                  <fo:block></fo:block>
                  <fo:inline>
                    <xsl:value-of select="substring(@selloSAT,361,120)" />
                  </fo:inline>
                </fo:inline>
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="8pt" font-family="sans-serif" text-align="center" padding-top="1pt" background-color="lightgrey">
                Cadena original del complemento de certificacion digital del SAT
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row keep-together.within-page="always">
            <fo:table-cell>
              <fo:block font-size="6pt" font-family="sans-serif" text-align="left" padding-top="1pt" >
                <fo:inline font-family="arial, sans-serif" font-size="6pt" >
                  <xsl:value-of select="substring($cadenaOriginalComplemento,1,120)" />
                  <fo:block></fo:block>
                  <fo:inline>
                    <xsl:value-of select="substring($cadenaOriginalComplemento,121,120)" />
                  </fo:inline>
                  <fo:block></fo:block>
                  <fo:inline>
                    <xsl:value-of select="substring($cadenaOriginalComplemento,241,120)" />
                  </fo:inline>
                </fo:inline>
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
        </fo:table-body>
      </fo:table>
    </fo:block>
  </xsl:template>

  <xsl:template match="n1:Concepto">
    <fo:table-row keep-together.within-page="always">
      <fo:table-cell>
        <fo:block font-size="7pt" font-family="sans-serif" text-align="left" height="3mm">
          <xsl:value-of select="@noIdentificacion" />
        </fo:block>
      </fo:table-cell>
      <fo:table-cell>
        <fo:block font-size="7pt" font-family="sans-serif" text-align="left" padding-top="1pt">
          <xsl:value-of select="@descripcion" />
        </fo:block>
      </fo:table-cell>
      <fo:table-cell>
        <fo:block font-size="7pt" font-family="sans-serif" text-align="right" padding-top="1pt">
          <xsl:value-of select="format-number(number(string(@valorUnitario)), '###,##0.00')" />
        </fo:block>
      </fo:table-cell>
      <fo:table-cell>
        <fo:block font-size="7pt" font-family="sans-serif" text-align="right" padding-top="1pt">
          <xsl:value-of select="format-number(number(string(@cantidad)), '###,##0.00')" />
        </fo:block>
      </fo:table-cell>
      <fo:table-cell>
        <fo:block font-size="7pt" font-family="sans-serif" text-align="center" padding-top="1pt">
          <xsl:value-of select="@unidad" />
        </fo:block>
      </fo:table-cell>
      <fo:table-cell>
        <fo:block font-size="7pt" font-family="sans-serif" text-align="right" padding-top="1pt">
          <xsl:value-of select="format-number(number(string(@importe)), '###,##0.00')" />
        </fo:block>
      </fo:table-cell>
      <fo:table-cell>
        <fo:block font-size="7pt" font-family="sans-serif" text-align="right" padding-top="1pt"></fo:block>
      </fo:table-cell>
    </fo:table-row>
  </xsl:template>

</xsl:stylesheet>