<?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 adusr where adusrusua = '$miusr'");
$sqrow = mysql_fetch_assoc($sqlid);
$xuser = $sqrow["adusriden"];
$sqlve = mysql_query("select * from adusr where adusrusua = '$xusua'");
$sqven = mysql_fetch_assoc($sqlve);
$xsele = $sqven["adusriden"];
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 vedetntra,vecabncli,vedetcart,vedetcant,
                             vedetpven,vedettven,vecabfech
                      from   vecab,vedet
                      where  vecabntra = vedetntra
                      and    vecabusua = '$xusua'
                      and    vecabfech between '$fechi' and '$fechf'
                      and    vecabstat = 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,'DETALLE 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(35,10,"Usuario : ",0,0,'R');
    $this->Cell(10,10,$xusua,0,0,'L');
    $this->SetFont('Arial','B',8);
    $this->Ln(6);
    $this->Cell(10);
    $this->Cell(12,10,'TRANS',0,0,'C');
    $this->Cell(16,10,'FECHA',0,0,'C');
    $this->Cell(50,10,'CLIENTE',0,0,'L');
    $this->Cell(50,10,'PRODUCTO',0,0,'L');
    $this->Cell(15,10,'CANTIDAD',0,0,'R');
    $this->Cell(15,10,'PRECIO',0,0,'R');
    $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 = new PDF("L","mm",array(216,140));
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',8);
$paso = 0;
while ($mirow = mysql_fetch_assoc($resul)):
   if ($paso == 0):
      $trans = $mirow["vedetntra"];
      $paso = 1;
   endif;
   if ($trans != $mirow["vedetntra"]):
      $trans = $mirow["vedetntra"];
      $pdf->AddPage();
   endif;
   $pdf->Cell(12,5,$mirow["vedetntra"],0,0,'C',0);
   $pdf->Cell(16,5,date("d-m-Y",strtotime($mirow["vecabfech"])),0,0,'C',0);
   $pdf->Cell(50,5,utf8_decode($mirow["vecabncli"]),0,0,'L',0);
   $pdf->Cell(50,5,utf8_decode($mirow["vedetcart"]),0,0,'L',0);
   $pdf->Cell(15,5,number_format($mirow["vedetcant"],2),0,0,'R',0);
   $pdf->Cell(15,5,number_format($mirow["vedetpven"],2),0,0,'R',0);
   $pdf->Cell(15,5,number_format($mirow["vedettven"],2),0,0,'R',0);
   //$pdf->Cell(15,5,$miusr,0,0,'R',0);
   $toven += $mirow["vedettven"];
   $pdf->Ln(3);
endwhile;
$pdf->Cell(88);
$pdf->Cell(25,10,"Total Venta ----->",0,0,'L',0);
$pdf->Cell(70,10,number_format($toven,2),0,0,'R',0);
$pdf->Output();
mysql_free_result($resul);
mysql_close($conex);
?>
