<?php
//Incluir Archivos Necesarios
require('../../../pdf/fpdf.php');
date_default_timezone_set('America/Caracas'); 
$hora_local = date('H:i:s');
class PDF extends FPDF
{ }
//Incluir Archivos Necesarios
require('../../../Clases/db.php');
   

  $fecha1 = $_GET['txtfecha1'];
 $fecha2 = $_GET['txtfecha2'];
 $verEliminados = $_GET['vereli'];
$mostrar = new conn ();
$conex = $mostrar->crearconn();
    $pdf = new PDF();
$pdf->SetX(4);

$pdf->SetMargins(17,17,17);
  $pdf->AddPage();
$pdf->Image('../../../Imagenes/logo.png',165,12,35,35,'PNG');
  $pdf->SetFont('Arial','B',16);
  $pdf->SetTextColor(32,100,210);
  $pdf->Cell(150,10,iconv("UTF-8", "ISO-8859-1",strtoupper("Emperadora Internet y Mas")),0,0,'L');

  $pdf->Ln(9);

  $pdf->SetFont('Arial','',10);
  $pdf->SetTextColor(39,39,51);
  $pdf->Cell(150,9,iconv("UTF-8", "ISO-8859-1","RIF: J-29966140-6"),0,0,'L');

  $pdf->Ln(5);

  $pdf->Cell(150,9,iconv("UTF-8", "ISO-8859-1","Valencia Barrio Monumental III Av principal"),0,0,'L');

  $pdf->Ln(5);

  $pdf->Cell(150,9,iconv("UTF-8", "ISO-8859-1","Teléfono: 0241-8480889"),0,0,'L');

  $pdf->Ln(5);

  $pdf->Cell(150,9,iconv("UTF-8", "ISO-8859-1","Email: laemperadorainternetymas@gmail.com"),0,0,'L');

  $pdf->Ln(10);

  $pdf->SetFont('Arial','',10);
  $pdf->Cell(30,7,iconv("UTF-8", "ISO-8859-1","Fecha de emisión:"),0,0);
  $pdf->SetTextColor(97,97,97);
  $pdf->Cell(116,7,iconv ("UTF-8", "ISO-8859-1",date("d/m/Y", strtotime($hora_local))." ".date($hora_local)),0,0,'L');
  $pdf->SetFont('Arial','B',10);
  $pdf->SetTextColor(39,39,51);
  $pdf->Cell(26,7,iconv("UTF-8", "ISO-8859-1",strtoupper("Reporte Total de Ventas")),0,0,'C');

  

  $pdf->Ln(9);
  # Tabla de productos #
  $pdf->SetFont('Arial','',8);
  $pdf->SetFillColor(23,83,201);
  $pdf->SetDrawColor(23,83,201);
  $pdf->SetTextColor(255,255,255);
   $pdf->Cell(14,8,iconv("UTF-8", "ISO-8859-1","N°"),1,0,'C',true);
    $pdf->Cell(25,8,iconv("UTF-8", "ISO-8859-1","Nombre Cliente"),1,0,'C',true);
     $pdf->Cell(25,8,iconv("UTF-8", "ISO-8859-1","Cedula Cliente"),1,0,'C',true);
    $pdf->Cell(25,8,iconv("UTF-8", "ISO-8859-1","Vendedor"),1,0,'C',true);
      $pdf->Cell(30,8,iconv("UTF-8", "ISO-8859-1","Fecha"),1,0,'C',true);
       $pdf->Cell(14,8,iconv("UTF-8", "ISO-8859-1","Total"),1,0,'C',true);
  $pdf->Cell(50,8,iconv("UTF-8", "ISO-8859-1","Descripcion"),1,0,'C',true);
  
 

  $pdf->Ln(8);

  
  $pdf->SetTextColor(39,39,51);
    
   /*----------  Detalles de la tabla  ----------*/

       require('../../../Clases/Venta.php');
        $obj = new ventas();
        $result = $obj->getVentasConDetalles($fecha1,$fecha2,$verEliminados);
  

    
$totalp= 0;
$pdf->SetAutoPageBreak(false);
$startingX = $pdf->getX();
foreach ($result as $rows) {
    $pdf->Cell(14,7,iconv("UTF-8", "ISO-8859-1",$rows['N_Factura']),'LRB',0,'C');
    $pdf->Cell(25,7,iconv("UTF-8", "ISO-8859-1",$rows['Nombre_cliente']),'LRB',0,'C');
    $pdf->Cell(25,7,iconv("UTF-8", "ISO-8859-1",$rows['Cedula_cliente']),'LRB',0,'C');
    $pdf->Cell(25,7,iconv("UTF-8", "ISO-8859-1",$rows['User_Vendedor']),'LRB',0,'C');
    $pdf->Cell(30,7,iconv("UTF-8", "ISO-8859-1",$rows['Fecha']),'LRB',0,'C');

    $pdf->Cell(14,7,iconv("UTF-8", "ISO-8859-1",$rows['Total']),'LRB',0,'C');
    // Concatenar detalles de productos y cantidades
    $detalles = '';
    foreach ($rows['detalles'] as $detalle) {
        $detalles .= $detalle['Nombre_Producto'] . ' ' . $detalle['Cantidad'] . ', ';
    }

   

    $pdf->SetFillColor(255, 255, 255); // Cambiar color de fondo a azul claro
       $pdf->MultiCell(50, 7, iconv("UTF-8", "ISO-8859-1", $detalles), 'LRB', 'C', true);
  // Draw horizontal lines at the same position as the MultiCell
    $xPos = $pdf->getX();
    $yPos = $pdf->getY();
    $pdf->SetXY($xPos, $yPos - 7); // Adjust Y position to match the Multicell
    $pdf->Cell(183, 0, '', 'T'); // Draw a horizontal line spanning the width of the cells
    // Restaurar la posición de la celda


    

    $pdf->Ln(7);
    /*----------  Fin Detalles de la tabla  ----------*/
}



$pdf->SetFont('Arial','B',9);
$pdf->Cell(100,7,iconv("UTF-8", "ISO-8859-1",''),'TB',0,'C');
    $pdf->Cell(20,7,iconv("UTF-8", "ISO-8859-1",""),'TB',0,'C');
   $pdf->Cell(20,7,iconv("UTF-8", "ISO-8859-1",""),'TB',0,'C');
    $pdf->Cell(20,7,iconv("UTF-8", "ISO-8859-1",""),'TB',0,'C');
     
    $pdf->Cell(2,7,iconv("UTF-8", "ISO-8859-1",""),'TB',0,'C');
 $pdf->Cell(2,7,iconv("UTF-8", "ISO-8859-1",""),'TB',0,'C');
  $pdf->Ln(7);
$pdf->Output("I",true);