<?php

include('pdf/fpdf.php');
session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
include('DF80000.php');
 
class PDF extends FPDF{
	//Cabecera de Página
	function Header(){
	    //Logotipo
	    include("DYIM_VER.php");
	    //Arial Bold (14)
	    $this->SetFont('Arial','B',12);
	    //Espacio
		
	    //Título
		if($_POST['setipven']!='TODAS'){
			$dt_tipven = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dgtipven where tip_ven='".$_POST['setipven']."'"));
			$tipoventa=$dt_tipven["des_tipven"];
		}
		else
			$tipoventa="TODAS";
	    $this->Cell(190,3,'POPULARIDAD DE PRODUCTOS',0,1,'C');
		$this->SetFont('Arial','B',8);
		$this->Cell(190,3,'Del '.$_POST['fecini'].' al '.$_POST['fecfin'].' ('.$tipoventa.')',0,1,'C');
	    //Salto de línea
	    $this->Ln(3);
		$this->SetFont('ARIAL','B',8);
		$this->SetFillColor(220,220,220);
		$this->Cell(20,3,'Clave',1,0,'C',1);	
		$this->Cell(90,3,'Articulo',1,0,'C',1);	
		$this->Cell(30,3,'Cantidad',1,0,'C',1);	
		$this->Cell(30,3,'Total',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().'/{nb}',0,0,'C');
	}
}
$pdf;

$pdf=new PDF('P','mm','Letter');
$pdf->AddPage();
$pdf->AliasNbPages();
$binit=-1;
$pdf->SetFillColor(240,240,240);
$pdf->SetFont('Arial','',8);
$fecIni=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
$fecFin=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d');
if($_POST['setipven']!='TODAS') $tipVenAnd="dgpemema.tip_ven='".$_POST['setipven']."' and";	
@$sql = mysqli_query($GLOBALS['con'] ,"
	SELECT 
		dginvent.nparte1 as idInvent,
		dginvent.np_desc1 as desProd,
		dginvent.linea as idLinea,
		dglineas.des_linea as desLinea,
		SUM(dgpememd.can_ref) as  canPop,
		SUM(dgpememd.total_cd) as totPop
	FROM
		dgpememd
		INNER JOIN dgpemema ON(dgpemema.fol_ven = dgpememd.cons_ven)
		INNER JOIN dginvent ON(dginvent.nparte1 = dgpememd.nparte1)
		INNER JOIN dglineas ON(dginvent.linea = dglineas.linea)
	WHERE
		dgpemema.fec_ven BETWEEN '$fecIni' AND '$fecFin' AND dgpemema.cancelado=0
	GROUP BY 
		dgpememd.nparte1
	ORDER BY 
		dginvent.np_desc1		
");

while($reg = mysqli_fetch_array($sql)){
	$pdf->Cell(20,3,$reg["idInvent"],0,0,'L');
	$pdf->Cell(90,3,$reg["desProd"],0,0,'L');			
	$pdf->Cell(30,3,$reg['canPop'],0,0,'R');
	$pdf->Cell(30,3,number_format($reg["totPop"],2,'.',','),0,1,'R');
	$total +=$reg["totPop"];
	$arrSumLin[$reg['idLinea']]['desLin']=$reg['desLinea'];
	$arrSumLin[$reg['idLinea']]['totLin'] +=$reg['totPop'];
	$arrSumLin[$reg['idLinea']]['canLin'] +=$reg['canPop'];				
	$canArt +=$reg['canPop'];
	$numVen++;
}
$pdf->SetFont('Arial','I',8);
$pdf->SetFillColor(249);
if(count($arrSumLin)>0)foreach($arrSumLin as $idLin=>$val){
	$pdf->Cell(20,3,'',0,0,'L',1);
	$pdf->Cell(90,3,$val['desLin'],0,0,'L',1);			
	$pdf->Cell(30,3,$val['canLin'],0,0,'R',1);
	$pdf->Cell(30,3,number_format($val['totLin'],2,'.',','),0,1,'R',1);
}
$pdf->SetFillColor(245);
$pdf->SetFont('Arial','B',8);
$pdf->Cell(110,3,"TOTAL:",0,0,'R',1);
$pdf->Cell(30,3,number_format($canArt,2,'.',','),0,0,'R',1);
$pdf->Cell(30,3,number_format($total,2,'.',','),0,1,'R',1);
$pdf->Output("Listado_de_Ventas_Diarias.pdf","D");
?>