<?php

include('pdf/fpdf.php');
session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
require_once("DF80000.php");
class PDF extends FPDF
{
	//Cabecera de Página
	function Header()
	{
	    //Logotipo
	         include("DYIM_VER.php");
	    //Arial Bold (14)
	    $this->SetFont('Arial','B',11);
	    //Espacio
		$fec = $_POST['botf1'];
        include('DF20000.php');
        $fecha_inicial = $f_m;
        //$pdf->Cell(15,5,$fecha_inicial,1,0,'C');
        $fec = $_POST['botf2'];
        include('DF20000.php');
        $fecha_final = $f_m;
        //$pdf->Cell(15,5,$fecha_final,1,0,'C');
	    //Título
	    $this->Cell(250,3,'LISTADO DE FACTURAS',0,1,'C');
	    //Salto de línea
	    $this->Ln(3);
		$this->SetFont('Times','',7);
		$this->SetFillColor(192,192, 192);
		$this->Cell(8,5,'',0,0,'C');
		$this->Cell(12,5,'Fol.Fac.',1,0,'C',1);
		$this->Cell(20,5,'Tip.Vent.',1,0,'C',1);
		$this->Cell(5,5,'T.F.',1,0,'C',1);
		$this->Cell(18,5,'Fec.Fac',1,0,'C',1);
		$this->Cell(20,5,'Num.Fac',1,0,'C',1);
		$this->Cell(17,5,'Fec.Ven.',1,0,'C',1);
		$this->Cell(17,5,'Fol.Ven',1,0,'C',1);
		$this->Cell(17,5,'Cod.cli',1,0,'C',1);
		$this->Cell(60,5,'Nom.Cli',1,0,'C',1);
		$this->Cell(10,5,'Por.Des',1,0,'C',1);
		$this->Cell(17,5,'Venta',1,0,'C',1);
		$this->Cell(17,5,'Sub.Tot.',1,0,'C',1);
		$this->Cell(17,5,'Iva',1,0,'C',1);
		$this->Cell(10,5,'Cancela',1,1,'C',1);
	}
	//Pie de Página
	function Footer()
	{
	    //Posición: 1,5 cm del Final
	    $this->SetY(-15);
	    //Arial Italic 8
	    $this->SetFont('Arial','I',8);
	    //Número de página
	    $this->Cell(0,0, $this->PageNo(),0,0,'C');
	}
}

$pdf=new PDF('L','mm','A4');
$pdf->AddPage();
$fec = $_POST['botf1'];
include('DF20000.php');
$fecha_inicial = $f_m;
//$pdf->Cell(15,5,$fecha_inicial,1,0,'C');
$fec = $_POST['botf2'];
include('DF20000.php');
$fecha_final = $f_m;
$tipFac=$_POST['setipfac'];
//$pdf->Cell(15,5,$fecha_final,1,0,'C');
$sq_factur=mysqli_query($GLOBALS['con'] ,"SELECT * FROM dgfactur where tip_fac='$tipFac' AND fec_fac between '$fecha_inicial' AND '$fecha_final'");
while($reg = mysqli_fetch_array($sq_factur)){
    $pdf->SetFillColor(254,254,254);
	$pdf->Cell(8,5,'',1,0,'C',0;
	$pdf->Cell(12,5, $reg['fol_fac'],0,'0','C',1);
	$dt_tipven=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT * FROM dgtipven where  tip_ven = '".$reg['tip_ven']."'"));
	
	$pdf->Cell(20,5,substr($dt_tipven['des_tipven'],0,10),0,'0','C',1);
	$pdf->Cell(5,5, $reg['tip_fac'],0,'0','C',1);
	$pdf->Cell(18,5, chfecha($reg['fec_fac'],'Y-m-d','d/m/Y'),0,'0','C',1);
	$pdf->Cell(20,5, $reg['num_fac'],0,'0','C',1);
	$pdf->Cell(17,5, chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,'0','C',1);
	$pdf->Cell(17,5, $reg['fol_ven'],0,'0','C',1);
	$pdf->Cell(17,5, $reg['cod_cli'],0,'0','C',1);
	$dt_catcli=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT * FROM dgcatcli where  cod_cli = ' $reg['cod_cli']'"));
	$pdf->Cell(60,5,substr($dt_catcli["ap_pat_cli"]." ".$dt_catcli["ap_mat_cli"]." ".$dt_catcli["nom_cli"],1,35) ,0,'0','L',1);
	$pdf->Cell(10,5, $reg['por_des'],0,'0','L',1);
	$dt_almacen=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT * FROM dgalmacen where almacen = '".$reg['alm_gen']."'"));
	$pdf->Cell(17,5, $dt_almacen['desc_almacen'],0,'0','C',1);
	$pdf->Cell(17,5,number_format($reg['sub_ven'],2,'.',','),0,'0','R',1);
	$pdf->Cell(17,5,number_format($reg['iva_ven'],2,'.',','),0,'0','R',1);
	if($reg['cancelado']=="1" || $reg['cancelado']=="S") $cancelado = "SI";
	else $cancelado = "NO";
	$pdf-> Cell(10,5, $cancelado,0,1,'C',1);
	$sq_factud=mysqli_query($GLOBALS['con'] ,"SELECT * FROM dgfactud WHERE  cons_fac='".$reg['fol_fac']."'");
	while($dt_factud=mysqli_fetch_array($sq_factud)){
		$pdf->Cell(8,5,'',0,0,'C');
		$pdf->Cell(30,3, chfecha($dt_factud['fec_fac'],'Y-m-d','d/m/Y'),0,'0','C');
		$pdf->Cell(30,3, $dt_factud['num_fac'],0,'0','L');
		$pdf->Cell(30,3, $dt_factud['tip_fac'],0,'0','L');
		$pdf->Cell(30,3, $dt_factud['nparte1'],0,'0','L');
		$dt_desc=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT * FROM dginvent WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   nparte1 = '".$dt_factud['nparte1']."'"));
		$pdf->Cell(30,3, $dt_desc['np_desc1'],0,'1','L');
		
	}
	$pdf->Ln(3);
	$sumSubVen =$sumSubVen+$reg['sub_ven'];
	$sumIvaVen +=$reg['iva_ven'];
	
}
$pdf->Cell(265,5,"TOTAL: ".number_format($sumSubVen + $sumIvaVen,2,'.',','),0,'0','R',1);

$pdf->Output("Listado_Facturas.pdf","D");
?>