<?php
session_start();
if (!isset($_SESSION["user_id"]) || $_SESSION["user_id"]==null):
   print "<script>alert(\"Acceso Invalido!\");
   window.location='login.php';</script>";
endif;
if ($_SESSION["user_at"] < 2):
   print "<script>alert(\"Sin Acceso a Transaccion!\");
   window.location='vt000.php';</script>";
endif;
include_once ("lib/fexcel/classes/PHPExcel.php");
include_once ("lib/fpdf/funci.php");
include_once ("lib/fpdf/fpdf.php");
date_default_timezone_set('America/La_Paz');
$miusr = $_SESSION["user_id"];

// Conectar a la base de datos
$conex = conectar();
$feini = strip_tags($_POST["fecini"]);
$fefin = strip_tags($_POST["fecfin"]);
$xusua = strip_tags($_POST["xusua"]);
$fechi = date("Y-m-d",strtotime($feini));
$fechf = date("Y-m-d",strtotime($fefin));
$titul = "DEL ".$feini." AL ".$fefin;
$sqlid = mysql_query("select * from vtusr where vtusrusua = '$miusr'");
$sqrow = mysql_fetch_assoc($sqlid);
$xuser = $sqrow["vtusriden"];
$sqlve = mysql_query("select * from vtusr where vtusrusua = '$xusua'");
$sqven = mysql_fetch_assoc($sqlve);
$xsele = $sqven["vtusriden"];
if ($_SESSION["user_at"] < 4 and $xuser != $xsele):
      print "<script>alert(\"Reporte solo de su Usuario!\");
      window.location='vt000.php';</script>";
endif;
// Realiza la consulta
$resul = mysql_query("select vtcabntra,vtcabnfac,vtcabfech,vtcabncli,vtcabtven
                      from   vtcab
                      where  vtcabfech between '$fechi' and '$fechf'
                      and    vtcabusua = '$xusua'
                      and    vtcabstat = 0
                      order  by 1");
if (mysql_num_rows($resul) == 0):
      print "<script>alert(\"No Existen Ventas del Usuario!\");
      window.location='vt000.php';</script>";
endif;
class PDF extends FPDF{ // Cabecera de página
function Header(){
    global $titul,$xusua;
    // Logo
    $this->Image('images/cadena.jpg',10,8,33);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    $this->Ln(5);
    // Movernos a la derecha
    $this->Cell(84);
    $this->Cell(30,10,'RESUMEN DE VENTAS POR FECHA',0,0,'C');
    $this->SetFont('Arial','B',10);
    $this->Ln(6);
    $this->Cell(80);
    $this->Cell(40,10,$titul,0,0,'C');
    $this->Cell(36,10,"Usuario : ",0,0,'R');
    $this->Cell(10,10,$xusua,0,0,'L');
    $this->SetFont('Arial','B',8);
    $this->Ln(6);
    $this->Cell(45);
    $this->Cell(12,10,'TRANS',0,0,'C');
    $this->Cell(15,10,'FACTURA',0,0,'C');
    $this->Cell(16,10,'FECHA',0,0,'C');
    $this->Cell(50,10,'CLIENTE',0,0,'L');
    $this->Cell(15,10,'IMPORTE',0,0,'R');
    // Salto de línea10    
    $this->Ln(6);
}
// Pie de página
function Footer(){
    // Posición: a 1,5 cm del final
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Número de página
    $this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'C');
}
}
// Creación del objeto de la clase heredada
$pdf = new PDF("P","mm","letter");
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',8);
while ($mirow = mysql_fetch_assoc($resul)):
   $pdf->Cell(45);
   $pdf->Cell(12,5,$mirow["vtcabntra"],0,0,'C',0);
   $pdf->Cell(15,5,$mirow["vtcabnfac"],0,0,'C',0);
   $pdf->Cell(16,5,date("d-m-Y",strtotime($mirow["vtcabfech"])),0,0,'C',0);
   $pdf->Cell(50,5,utf8_decode($mirow["vtcabncli"]),0,0,'L',0);
   $pdf->Cell(15,5,number_format($mirow["vtcabtven"],2),0,0,'R',0);
   $toven += $mirow["vtcabtven"];
   $pdf->Ln(3);
endwhile;
$pdf->Cell(88);
$pdf->Cell(25,10,"Total Venta ----->",0,0,'L',0);
$pdf->Cell(40,10,number_format($toven,2),0,0,'R',0);
$pdf->Output();
mysql_free_result($resul);
mysql_close($conex);
?>
