<?php

 @session_start();

if(!isset($_SESSION['PCOSESS_SesionAbierta'])) {
	echo '<head><title>Error</title><style type="text/css"> body { background-color: #000000; color: #7f7f7f; font-family: sans-serif,helvetica; } </style></head><body><table width="100%" height="100%" border=0><tr><td align=center>&#9827; Acceso no autorizado !</td></tr></table></body>';
	die();
}
/*include_once '../../core/configuracion.php';
include_once '../../core/conexiones.php';
include_once '../../inc/practico/def_basedatos.php';
include_once '../../core/comunes.php';*/

$saldoFavor = $factura["saldo_pendiente"] < 0 ? -$factura["saldo_pendiente"] : 0;
$nombreFactura = $factura["id_contrato"] . '-' .$factura["nombres"] . ' ' . $factura["apellidos"];
$saldoPendienteValor = $factura["saldo_pendiente"] > 0 ? $factura["saldo_pendiente"] : 0;
$saldoPendiente = number_format($saldoPendienteValor, 0, ',', '.');
$totalValor =  number_format($factura["valor_sin_recargo"] + $saldoPendienteValor - $saldoFavor, 0, ',', '.');
$total = $totalValor > 0 ? $totalValor : 0;

$fechaSinRecargoBr = str_replace("-", "", $factura["fecha_sin_recargo"]);
$totalValor =  number_format($factura["valor_sin_recargo"] + $saldoPendienteValor - $saldoFavor, 0, ',', '.');
$total = $totalValor > 0 ? $totalValor : 0;
$totalBr = str_replace(".", "", $total);
$codigobarra = $factura["codigo_gs1"]; // "7709998678439";
$BarcodeParent = "(415)$codigobarra(8020)" . $factura["id"] . "(3900)$totalBr(96)$fechaSinRecargoBr";

$descargar = 0;
if ($PCO_Accion == "FACTURAS_descargarFactura")
    $descargar = 1;

$html = '
<html>

<head>
   <meta charset="utf-8">
   <title>Factura</title>
   <style>
      * {
         border: 0;
         box-sizing: content-box;
         color: inherit;
         font-family: Verdana;
         font-size: inherit;
         font-style: inherit;
         font-weight: inherit;
         line-height: inherit;
         list-style: none;
         margin: 0;
         padding: 0;
         text-decoration: none;
         vertical-align: top;
      }

      /* heading */

      h1 {
         font: bold 100% sans-serif;
         letter-spacing: 0.5em;
         text-align: center;
         text-transform: uppercase;
      }

      /* table */

      table {
         font-size: 75%;
         table-layout: fixed;
         width: 100%;
      }

      table {
         border-collapse: separate;
         border-spacing: 3px;
      }

      th,
      td {
         border-width: 1px;
         padding: 0.5em;
         position: relative;
         text-align: center;
      }

      th,
      td {
         border-radius: 0.25em;
         border-style: solid;
      }

      th {
         background: #1f60a0;
         color: #fff;
         border-color: #BBB;
      }

      td {
         border-color: #DDD;
      }

      /* page */

      html {
         font: 16px/1 "Open Sans", sans-serif;
         overflow: auto;
         padding: 0.5in;
      }

      html {
         background: #999;
         cursor: default;
      }

      body {
         box-sizing: border-box;
         height: 11in;
         margin: 0 auto;
         overflow: hidden;
         padding: 0.5in;
         width: 8.5in;
      }

      body {
         background: #FFF;
         border-radius: 1px;
         box-shadow: 0 0 1in -0.25in rgba(0, 0, 0, 0.5);
      }

      /* header */

      header {
         margin: 0 0 3em;
      }

      header:after {
         clear: both;
         content: "";
         display: table;
      }

      header h1 {
         background: #000;
         border-radius: 0.25em;
         color: #FFF;
         margin: 0 0 1em;
         padding: 0.5em 0;
      }

      header address {
         float: left;
         font-size: 75%;
         font-style: normal;
         line-height: 1.25;
         margin: 0 1em 1em 0;
      }

      header address p {
         margin: 0 0 0.25em;
      }

      header span,
      header img {
         display: block;
         float: right;
      }

      header span {
         margin: 0 0 1em 1em;
         max-height: 25%;
         max-width: 60%;
         position: relative;
      }

      header img {
         max-height: 100%;
         max-width: 100%;
      }

      header input {
         cursor: pointer;
         -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
         height: 100%;
         left: 0;
         opacity: 0;
         position: absolute;
         top: 0;
         width: 100%;
      }

      /* article */

      article,
      article address,
      table.meta,
      table.inventory {
         margin: 0 0 1.3em;
      }

      article:after {
         clear: both;
         content: "";
         display: table;
      }

      article h1 {
         clip: rect(0 0 0 0);
         position: absolute;
      }

      article address {
         float: left;
         font-size: 125%;
      }

      /* table meta & balance */

      table.meta,
      table.balance {
         float: right;
         width: 36%;
      }

      table.meta:after,
      table.balance:after {
         clear: both;
         content: "";
         display: table;
      }

      /* table meta */

      table.meta th {
         width: 40%;
      }

      table.meta td {
         width: 60%;
      }

      /* table items */

      table.inventory {
         clear: both;
         width: 100%;
      }

      table.inventory th {
         font-weight: bold;
         text-align: center;
      }

      table.inventory td:nth-child(1) {
         width: 26%;
      }

      table.inventory td:nth-child(2) {
         width: 38%;
      }

      table.inventory td:nth-child(3) {
         text-align: center;
         width: 12%;
      }

      table.inventory td:nth-child(4) {
         text-align: center;
         width: 12%;
      }

      table.inventory td:nth-child(5) {
         text-align: center;
         width: 12%;
      }

      /* table balance */

      table.balance th {
         width: 40%;
         text-align: center;
      }

      table.balance td {
         width: 60%;
         text-align: center;
      }

      /* aside */

      aside h1 {
         border: none;
         border-width: 0 0 1px;
         margin: 0 0 1em;
      }

      aside h1 {
         border-color: #999;
         border-bottom-style: solid;
      }

      /* javascript */

      @media print {
         * {
            -webkit-print-color-adjust: exact;
         }

         html {
            background: none;
            padding: 0;
         }

         body {
            box-shadow: none;
            margin: 0;
         }

         span:empty {
            display: none;
         }
      }

      @page {
         margin: 0;
      }
   </style>
    <link rel="stylesheet" href="inc/pdfLib/all.min.css">
    <script type="text/javascript" src="inc/pdfLib/html2pdf.bundle.min.js"></script>
    <script type="text/javascript" src="inc/bwip/bwip-js-min.js"></script>
    
            <!--<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">-->
            <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"
              integrity="sha512-GsLlZN/3F2ErC5ifS5QtgpiJtWd43JWSuIgh7mbzZ8zBps+dvLusV+eNQATqgA/HdeKFVgA5v3S/cIrLF7QnIg=="
              crossorigin="anonymous" referrerpolicy="no-referrer"></script>-->
</head>
<body id="Invoices">

   <article>
      <BR>
      <BR>
      <BR>
      <address>
         <p style="color:#14406b; font-size: 18px; font-weight: bold;">AGUAS DE SAN JERONIMO E.S.P</p>
         <p style="font-size: 12px;">NIT: 811018300-6</p>
         <p style="font-size: 12px;">CLL 21 # 8-78 San Jerónimo, Ant</p>
         <BR>
         <p style="font-size: 14px;"><span style="font-weight: bold;">Contrato N° </span> ' . $factura["id_contrato"] . ' <br>
            <span style="font-weight: bold;">Nombre: </span>' . $factura["nombres"] . ' ' . $factura["apellidos"] . ' <br>
            <!--<span style="font-weight: bold;">Estrato: </span>' . $factura["estrato"] . '<br>-->
            <span style="font-weight: bold;"> Vereda: </span>' . $factura["zona"] . ' <br>
            <span style="font-weight: bold;">Dirección: </span>' . $factura["direccion"] . ' <br>
         </p>
      </address>

      <table class="meta">
         <tr>
            <th style="width: 120px !important;"><span>Factura</span></th>
            <td><span>' . $factura["id"] . '</span></td>
         </tr>
         <tr>
            <th><span>Fecha sin recargo</span></th>
            <td><span>' . $factura["fecha_sin_recargo"] . '</span></td>
         </tr>
         <tr>
            <th><span>Fecha con recargo</span></th>
            <td><span>' . $factura["fecha_con_recargo"] . '</span></td>
         </tr>
      </table>
      <table class="inventory">
         <thead>
            <tr>
               <th><span>Descripción</span></th>
               <th><span>Cantidad</span></th>
               <th style="background-color: #14406b;"><span>Precio</span></th>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td><span>Consumo de agua m3</span></td>
               <td><span>1</span></td>
               <td style="text-align: center !important;"><span data-prefix>$</span><span>' . number_format($factura["valor_sin_recargo"], 0, ',', '.') . '</span></td>
            </tr>
         </tbody>
      </table>

      <table class="balance" style="font-size: 11px;">
         <tr>
            <td><span>SUBTOTAL</span></td>
            <th><span data-prefix>$</span><span>' . number_format($factura["valor_sin_recargo"], 0, ',', '.') . '</span></th>
         </tr>
         <tr>
            <td><span>CUENTAS VENCIDAS</span></td>
            <th><span data-prefix>$</span><span>' . $saldoPendiente . '</span></th>
         </tr>
         <tr>
            <td><span>SALDO A FAVOR</span></td>
            <th><span data-prefix>$</span><span>' . number_format($saldoFavor, 0, ',', '.') . '</span></th>
         </tr>
         <tr>
            <td><span>OTROS</span></td>
            <th><span data-prefix>$</span><span>0</span></th>
         </tr>
         <tr style="color: #fff; font-size: 16px;">
            <td style="background-color: #1f60a0; font-weight: bold;"><span>TOTAL</span></td>
            <th style="background-color: #14406b;"><span data-prefix>$</span><span>' . $total . '</span></th>
         </tr>
       
            
        </tr>
         </tr>
      </table>
      <address style="width: 62%;">
        <table>
            <tr>
               <td style="border-radius: 0 !important; border-style: none !important;">
                  <p style="font-size: 13px; vertical-align: middle; line-height: normal">
                     <span style="font-weight: bold;"> Medios y Canales de Pago</span>
                     <br>
                     <br>
                     <span style="font-weight: bold;">BANCOLOMBIA:</span>
                     <br>
                     <span style="font-weight: ">Corresponsal Bancario (PAC): </span>
                     <br>
                     <span style="font-weight: bold;"> '.$factura['convenio_presencial'].'</span>
                    <br>
                     <span style="font-weight: ">Pago virtual (APP): </span>
                      <br>
                    <span style="font-weight: bold;"> '.$factura['convenio_virtual'].'</span>
                     <br>
                     <span style="font-weight: ">Referencia de pago:</span>
                     <br>
                     <span style="font-weight: bold;">   NUMERO DE CONTRATO</span>
                  </p>
               </td> 
               <td style="border-radius: 0 !important; border-style: none !important;">
                    <img src="data:image/png;base64,[encoded data, 21620 characters]" data-filename="Firma Ignacio Digital.png"
                    data-filename="Firma Ignacio Digital.png" style="width: 220px; height: 70px;">
                    <p style="font-size: 10.3px; font-weight: bold;">JORGE IGNACIO AREIZA CASTRILLON <BR> GERENTE</p>
               </td>
            </tr>
         </table> 
      </address>
      </div>
        
         </br>
         </br>
         </br>
        <table style="border-collapse: collapse; border:none; border-width: 0px; style="vertical-align: middle !important;">
            <tr style="float: middle; !important;">
                <td style="border-radius: 0 !important; border-style: none !important; vertical-align: middle !important; width:30%">
                    <img id="barcodeTarget" width="500" />
                    <canvas id="mycanvas"></canvas>
                </td>
            </tr>
        </table>
   </article>
 
       <script>
        const options = {
            bcid:        "gs1-128",       // Barcode type
            text:        "' . $BarcodeParent. '",    // Text to encode
            scale:       2,               // 3x scaling factor
            height:      8,              // Bar height, in millimeters
            includetext: true,            // Show human-readable text
            textxalign:  "center",        // Always good to set this
        };

        const hola3 = () => {
            const canvas = document.createElement("canvas");
            try {
                bwipjs.toCanvas(canvas, options);
                document.getElementById("barcodeTarget").src = canvas.toDataURL("image/png");
            } catch (e) { console.log({e}); }
        };
        //addEventListener("DOMContentLoaded", () => {
            hola3();
        //});

	</script>
	

	
	
      <script>
      document.addEventListener("DOMContentLoaded", function (event) {
        //setTimeout(function(){ hola(); }, 1000);
        var descargar = ' . $descargar . ';
        if(descargar == 1) {
             var element = document.getElementById("Invoices");
             
             var opt = {
                filename: "' . str_replace(' ', '', $nombreFactura) . '.pdf",
                image: { type: "jpeg", quality: 0.98 },
                html2canvas: { scale: 2, useCORS: true, letterRendering: true },
                jsPDF: { unit: "mm", format: "letter", orientation: "portrait" }
             };
    		 html2pdf(element, opt);
    		 setTimeout(function(){ window.close(); }, 1650);
        }
        });
        
        function hola(){
            var element = document.getElementById("Invoices");
             
             var opt = {
                filename: "' . $factura["contrato"] . '.pdf",
                image: { type: "jpeg", quality: 0.98 },
                html2canvas: { scale: 2, useCORS: true, letterRendering: true },
                jsPDF: { unit: "mm", format: "letter", orientation: "portrait" }
             };
    		 html2pdf(element, opt);
        }
   </script>
</body>
</html>';

echo $html;
die();


?>