<?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['setipvent']!='T'){
			$dt_tipven = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from ptventa where nparte1='".$_POST['setipvent']."'"));
			$pventa=$dt_tipven["np_desc1"];
		}else {
			$pventa="TODOS";
		}	
		if($_POST['secodven']!='T'){
			$dt_caj=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select CONCAT(dgcatven.nom_ven,' ',dgcatven.ap_pat_ven,' ',dgcatven.ap_mat_ven) as nomVen from dgcatven where cod_ven='".$_POST['secodven']."'"));
			$tipcaj=$dt_caj["nomVen"];	
		}
		else {
			$tipcaj="TODOS";
		}	
	    $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'].' ('.$pventa.')',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['setipvent']!='T') {
  		$ptventa=' ptventa.nparte1='.$_POST['setipvent'].' ';
	}else {
		$ptventa=' ptventa.nparte1!=0 ';
    }
	if($_POST['slcLinea']!='T') {
  		$dtlinea=' dglineasp.linea='.$_POST['slcLinea'].' ';
	}else {
		$dtlinea=' dglineasp.linea!=0 ';
    }
	if($_POST['secodven']!='T') {
		$arrBus="dgger_cajerosj.vendedor='".$_POST['secodven']."'";		
	}else {
		$arrBus="dgger_cajerosj.vendedor!='0'";		
	}
	
	if ($fecIni<'2014-05-01'){
	$posicion='fposres';
	$memaDet='dgpememd';
	//$mema='pvresmema';
	$table='dgger_cajerosj';
	}else{
	$posicion='fpos'; 
	$memaDet='dgpememd';
	//$mema='dgpemema';
	$table='dgger_cajerosj';
	}
	$mema='dgpemema';
	
	@$sql = mysqli_query($GLOBALS['con'] ,"
		SELECT 
			dginventp.nparte1 as idInvent,
			dginventp.np_desc1 as desProd,
			dginventp.linea as idLinea,
			dglineasp.des_linea as desLinea,
			SUM($memaDet.can_ref) as  canPop,
			$memaDet.comenta,
			SUM($memaDet.pre_bas*$memaDet.can_ref) as totPop
		FROM
			$memaDet
			INNER JOIN $mema ON($mema.fol_ven = $memaDet.cons_ven)
			INNER JOIN dginventp ON(dginventp.nparte1 = $memaDet.nparte1)
			INNER JOIN dglineasp ON(dginventp.linea = dglineasp.linea)
			LEFT JOIN $posicion ON $posicion.fol_ven=$mema.fol_ven
			LEFT JOIN ctmesas ON ctmesas.nparte1=$posicion.mesa
			LEFT JOIN ctarea ON ctarea.nparte1=ctmesas.area
			LEFT JOIN ctmeser  ON ctmeser.nparte1=$mema.cod_ven
			LEFT JOIN ptventa ON ptventa.nparte1=ctmesas.descripcion
		    INNER JOIN $table ON($mema.fol_ven = $table.pedido)
		WHERE
			$mema.fec_ven BETWEEN '$fecIni' AND '$fecFin' AND $mema.cancelado=0 and $mema.cancela=0  AND $mema.pagado='S' and $memaDet.cancela=0 and $ptventa and $arrBus and $dtlinea
		GROUP BY 
			$memaDet.nparte1,$memaDet.comenta
		ORDER BY 
			dginventp.np_desc1		
	");
	echo mysqli_error($GLOBALS['con']);
while($reg = mysqli_fetch_array($sql)){
	$pdf->Cell(20,3,$reg["idInvent"],0,0,'L');
	$pdf->Cell(90,3,$reg["desProd"].(($reg['comenta']!="")?(" | ".$reg['comenta']):""),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("Popularidad_De_Productos.pdf","D");
?>
