<?php
/**
 * Uriel
 */
require_once('../../../controllers/facturacion.controller.php');
require_once('../../../models/facturacion.model.php');
require_once('../../../controllers/productos.controller.php');
require_once('../../../models/productos.model.php');
require_once('tcpdf_include.php');
class APISEPDFFACTURA extends TCPDF
{
    //Page header
    public function Header() {
        // Logo
        $image_file = K_PATH_IMAGES.'logo-apise-bloque.fw.png';
        $this->Image($image_file, 10, 10, 15, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
        // Set font
        $this->SetFont('helvetica', 'B', 20);
        // Title
        /*$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');*/
    }
    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 5, "", 0, 1, 'L', 0, '', 0, false, 'M', 'M');
        //$this->Cell(0, 8, "Report generated with Open-LIMS (" . date("dS M Y H:i") . ")", 0, 1, 'L', 0, '', 0, false, 'M', 'M');
        //$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}
#imprimirCotizacion
class imprimirFactura{
      #public $idCotiMaster;
      public $idFacturaTimbrada;
  #public function traerImpresionCotiza(){

  public function traerImpresionFactura(){

        // TRAER INFORMACIN
        $item  = 'fac_id';
        $valor = $this -> idFacturaTimbrada;
          $respuestaFactura = ControladorFacturacion::ctrMostrarFacturaConTimbre($item,$valor);
          //var_dump($respuestaFactura);exit;

          #$fecha = substr($respuestaFactura["coti_fechacreado"],0,-8);
          #<Fecha y hora en que es creada la factura> Se agrega el prefijo 'T' a la fecha de la factura
          $splitFecha = explode(" ", $respuestaFactura['fac_creado_fecha']);
          $fecha = $splitFecha[0] ."T".$splitFecha[1];
          $folio = $respuestaFactura["fac_folio"];
          $empresa = $respuestaFactura["emp_nombre"];
          $RFC = $respuestaFactura["emp_rfc"];

          #Datos del timbradoPor
          $SerieTimbre = $respuestaFactura["timbre_certificado"];
          #==Fecha y hora en eque es timbrada la factura== Se agrega el prefijo 'T' a la fecha de la factura
          $splitFechaTimbrado = explode(" ", $respuestaFactura['fecha_timbrado']);
          $fechaTimbrado = $splitFechaTimbrado[0] ."T".$splitFechaTimbrado[1];
          $folioUUID = $respuestaFactura["timbre_UUID"];

          #datos generales del timbrado
          $metodoDePago =  $respuestaFactura["metP_Desc"];
          $formaDePago =  $respuestaFactura["formaPago_desc"];
          #falta evaluar el como obtener este resultado
          $condicionDePago = "Aun Pendiente";
          $coti_moneda =  $respuestaFactura["divisa_abreviatura"];
          $totalEnLetra = null; #pendiente por ahora.

          $usoDeCFDI =  $respuestaFactura["uso_codigo"] . ' ' . $respuestaFactura['uso_descripcion'];
          $CodigoPostal =  $respuestaFactura["fac_lugarExpedicion"];
          $tipoComprobante = $respuestaFactura["tipComprobante_SAT_Id"] . ' ' . $respuestaFactura['tipComprobante_Desc'];

    #      $cot_subtotal= '$ '.$respuestaFactura["coti_subTotal"];
    #      $cot_iva= '$ '.$respuestaFactura["coti_iva"];
    #      $cot_total= '$ '.$respuestaFactura["coti_total"];
          //var_dump($fecha);

        #  $item = 'fac_id';
          #$valor = $this -> idCotiMaster;

          #  $resItem = ControladorProductos::ctrMostrarFacturaConTimbre($item, $valor);
              //var_dump($resItem);exit;
              //$descripcion = $resItem["detalle_descripcion"];


        //TCPDF
        $pdf = new APISEPDFFACTURA(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        $pdf->setFontSubsetting(true);

        $pdf->AddPage();

        $pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));


$bloque1 = <<<EOF

  <table>
    <tr>
    <td style="width:230; line-height:20px;"><img src="images/logo-apise-bloque2.fw.png"></td>

    <td style="background-color:white; width:120">
      <div style="font-size:8.5px; text-align:center; line-height:20px">

          Calle 16 de Septiembre
        <br>
          Colonia: Satelite C.P. 88795
        <br>
          Ciudad: Reynosa,Tamps.
        <br>
          Telfono: 00000000
        <br>
          e-mail: ventas@apise.com.mx
      </div>
    </td>

    <td style="background-color:white; width:190;">
    <div style="text-align:right; line-height:15px">
    <table style="padding:2px;">
      <tr>
        <td colspan="2"><span style="color: #C03;font-size: 12px; font-weight: bold;">FACTURA</span></td>
      </tr>
      <tr style="font-size: 8.5px; text-align:center;">
        <td style="border: 1px solid #0070C0;background-color:#0070C0;color:white;">
          Folio:
        </td>
      </tr>
      <tr style="font-size: 8.5px;">
      <td style="text-align:center;border-style: 2px solid #0070C0;border-color:#0070C0;">
        $folio
      </td>
      </tr>

      <tr style="font-size: 8.5px;">
            <td colspan="2" style="font-size:8.5px; text-align:center; line-height:1px"></td>
      </tr>

      <tr style="font-size: 8.5px;">
          <td style="border:  1px solid #0070C0;background-color:#0070C0;color:white; text-align:center;">
            Tipo de Comprobante:
          </td>

      </tr>
      <tr style="font-size: 8.5px;">
          <td style="text-align:center;border-style: 2px solid #0070C0;border-color:#0070C0;">
            $tipoComprobante
          </td>
      </tr>

      <tr style="font-size: 8.5px;">
            <td colspan="2" style="font-size:8.5px; text-align:center; line-height:1px"></td>
        </tr>
      <tr style="font-size: 8.5px;">
        <td style="border: 1px solid #0070C0;background-color:#0070C0;color:white;">
          Numero de Serie del Certificado del CSD:
        </td>
        <td style="text-align:center;border-style: 2px solid #0070C0;border-color:#0070C0;">
           Es la llave y certificado que es proporcionado al cliente
        </td>
      </tr>
      <tr style="font-size: 8.5px;">
            <td colspan="2" style="font-size:8.5px; text-align:center; line-height:1px"></td>
      </tr>
      <tr style="font-size: 8.5px;">
        <td style="border: 1px solid #0070C0;background-color:#0070C0;color:white;">
          Lugar, fecha y hora de emisin:
        </td>
        <td style="border-left: 1px solid #0070C0;text-align:center;border-style: 1px solid #0070C0;border-color:#0070C0">
          #$CodigoPostal <br/> $fecha
        </td>
      </tr>
      <tr style="font-size: 8.5px;">
            <td colspan="2" style="font-size:8.5px; text-align:center; line-height:1px"></td>
      </tr>
      <tr style="font-size: 8.5px;">
        <td style="border: 1px solid #0070C0;background-color:#0070C0;color:white;">
          Folio Fiscal
        </td>
        <td style="border-left: 1px solid #0070C0;text-align:center;border-style: 1px solid #0070C0;border-color:#0070C0">
          $folioUUID
        </td>
      </tr>
      <tr style="font-size: 8.5px;">
            <td colspan="2" style="font-size:8.5px; text-align:center; line-height:1px"></td>
      </tr>
      <tr style="font-size: 8.5px;">
        <td style="border: 1px solid #0070C0;background-color:#0070C0;color:white;">
          No. de Serie del Certificado del SAT
        </td>
        <td style="border-left: 1px solid #0070C0;text-align:center;border-style: 1px solid #0070C0;border-color:#0070C0">
          $SerieTimbre
        </td>
      </tr>
      <tr style="font-size: 8.5px;">
            <td colspan="2" style="font-size:8.5px; text-align:center; line-height:1px"></td>
      </tr>
      <tr style="font-size: 8.5px;">
        <td style="border: 1px solid #0070C0;background-color:#0070C0;color:white;">
          Fecha y hora del certificacin
        </td>
        <td style="border-left: 1px solid #0070C0;text-align:center;border-style: 1px solid #0070C0;border-color:#0070C0">
          $fechaTimbrado
        </td>
      </tr>
    </table>
    </div>

    </td>

    </tr>
  </table>

EOF;

$pdf->writeHTML($bloque1, false, false, false, false, '');


$bloque2 = <<<EOF
  <img src="images/backFact2.jpg">
  <table width="540px" height="16"
  style="font-size:10px;
  padding:5px 10px;
  border: 2px solid #0070C0;
  background-color:white;
  border-radius: 25px;">
    <tr>
      <td width="15%"><strong>Empresa:</strong></td>
      <td width="85%">$empresa</td>
    </tr>
    <tr>
      <td><strong>RFC:</strong></td>
        <td>$RFC</td>
    </tr>
    <tr>
      <td><strong>Uso CFDI:</strong></td>
        <td>$usoDeCFDI</td>
    </tr>
  </table>

EOF;

$pdf->writeHTML($bloque2, false, false, false, false, '');

$bloque3 = <<<EOF
  <table>
    <tr>
      <td></td>
    </tr>
  </table>

  <table width="540px" height="16"
  style="font-size:8px;
  padding:5px 5px;
  background-color:#5B9BD5;
  border-radius: 25px;
  color:white;">
    <tr>
      <td style="border: 1px solid #0070C0;" width="12%"><strong>SERVICIO</strong></td>
      <td style="border: 1px solid #0070C0;" width="12%"><strong>CANTIDAD</strong></td>
      <td style="border: 1px solid #0070C0;" width="52%"><strong>DESCRIPCIN</strong></td>
      <td style="border: 1px solid #0070C0;" width="12%"><strong>IMPORTE</strong></td>
      <td style="border: 1px solid #0070C0;" width="12%"><strong>TOTAL</strong></td>
    </tr>
  </table>

EOF;

$pdf->writeHTML($bloque3, false, false, false, false, '');

/*
foreach ($resItem as $key => $value) {

$servicio = $value['detalle_folioServicio'];
$cantidad = $value['detalle_cantidad'];
$descripcion = $value['detalle_descripcion'];
$importe = '$ '.$value['detalle_importe'];
$total = '$ '.$value['detalle_total'];


$bloque4 = <<<EOF
  <table width="540px" height="500px"
  style="font-size:8px;
  padding:5px 5px;
  background-color:white;
  border-radius: 25px;">
    <tr>
      <td style="border: 1px solid #0070C0;" width="12%">$servicio</td>
      <td style="text-align:center; border: 1px solid #0070C0;" width="12%">$cantidad</td>
      <td style="border: 1px solid #0070C0;" width="52%">$descripcion</td>
      <td style="text-align:right;border: 1px solid #0070C0;" width="12%">$importe</td>
      <td style="text-align:right;border: 1px solid #0070C0;" width="12%">$total</td>
    </tr>
  </table>

EOF;

$pdf->writeHTML($bloque4, false, false, false, false, '');

}*/

$bloqueTotales1 = <<<EOF


<table>
  <tr>
    <td></td>
  </tr>
</table>

<table width="540px" height="500px"
style="font-size:8px;
padding:5px 5px;
background-color:white;">
  <tr>
    <td style="background-color:#5B9BD5;
    border-radius: 25px;color:white;text-align:center;border: 1px solid #0070C0;" width="100%"><strong>TERMINOS Y CONDICIONES</strong></td>
  </tr>
  <tr>
    <td style="text-align:center;border: 1px solid #0070C0;" width="100%"><strong></strong></td>
  </tr>
</table>


  <table>
    <tr>
      <td></td>
    </tr>
  </table>

  <table width="540px" height="500px"
  style="font-size:8px;
  padding:5px 5px;
  background-color:white;">
    <tr>
      <td style="background-color:#5B9BD5;
      border-radius: 25px;color:white;text-align:center;border: 1px solid #0070C0;" width="12%"><strong>MONEDA</strong></td>
      <td style="background-color:#5B9BD5;border-radius: 25px;color:white;text-align:center;border: 1px solid #0070C0;" width="12%"><strong>VIGENCIA</strong></td>
      <td style="background-color:#5B9BD5;border-radius: 25px;color:white;text-align:center;border: 1px solid #0070C0;" width="52%"><strong>TIEMPO DE ENTREGA</strong></td>
      <td style="background-color:#5B9BD5;
      border-radius: 25px;
      color:white;text-align:right;border: 1px solid #0070C0;" width="12%">Subtotal</td>
      <td style="text-align:right;border: 1px solid #0070C0;" width="12%"></td>
    </tr>
    <tr>
      <td style="text-align:center;border: 1px solid #0070C0;" width="12%"><strong>MX</strong></td>
      <td style="text-align:center;border: 1px solid #0070C0;" width="12%"><strong></strong></td>
      <td style="text-align:center;border: 1px solid #0070C0;" width="52%"><strong></strong></td>
      <td style="background-color:#5B9BD5;
      border-radius: 25px;
      color:white;text-align:right;border: 1px solid #0070C0;" width="12%">IVA</td>
      <td style="text-align:right;border: 1px solid #0070C0;" width="12%"></td>
    </tr>
    <tr>
      <td colspan="3" style="text-align:center;border-right: 1px solid #0070C0;" width="76%"></td>
      <td style="background-color:#5B9BD5;
      border-radius: 25px;color:white;text-align:right;border: 1px solid #0070C0;" width="12%">Total</td>
      <td style="text-align:right;border: 1px solid #0070C0;" width="12%"></td>
    </tr>
    <tr>
      <td colspan="5" style="text-align:center;" width="100%"></td>

    </tr>
    <tr>
      <td colspan="5" style="text-align:center;" width="100%"><strong>GRACIAS POR DARME LA OPORTUNIDAD DE COTIZAR</strong></td>

    </tr>
  </table>

EOF;

  $pdf->writeHTML($bloqueTotales1, false, false, false, false, '');
  $pdf->Output('../APISE-Factura.pdf', 'I');
  }
}
$factura = new imprimirFactura();
$factura -> idFacturaTimbrada =$_GET["fac_id"];
$factura -> traerImpresionFactura();

 ?>
