<?php
include('pdf/fpdf.php');
session_cache_limiter('private');
session_start();
//ini_set("display_errors", 1);
//ini_set("track_errors", 1);
//ini_set("html_errors", 1);
//error_reporting(E_ALL);
$com = "I";
include("DMMYSQLG.php");
//include("DSA3000.php");
include("DMMYSQLG.php");
include('DF80000.php');
 $user=0;
class PDF extends FPDF{
	//Cabecera de Página
	function Header(){
	    //Logotipo
	    include("DYIM_VER.php");
	    //Arial Bold (14)
	    $this->SetFont('Arial','B',12);
	    //Espacio
		
		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, usuario from dgcatven where cod_ven='".$_POST['secodven']."'"));
			$tipcaj=$dt_caj["nomVen"];	
			$user=$dt_caj["usuario"];	
		}
		else {
			$tipcaj="TODOS";
		}	
	
	    $this->Cell(190,3,'LISTADO DE CUENTAS CANCELADAS POR LOS CAJEROS',0,1,'C');
		$this->SetFont('Arial','B',8);
		$this->Cell(190,3,'',0,1,'L');		
		$this->Cell(190,3,'PERIODO DEL '.$_POST['fecini'].' AL '.$_POST['fecfin'].'',0,1,'C');
		$this->Cell(190,3,'PUNTO DE VENTA:'.$pventa.                        '',0,1,'C');
		$this->Cell(190,3,'CAJERO:'.       $tipcaj.                           '',0,1,'C');		
	
	    //Salto de línea
	    $this->Ln(3);
		$this->SetFont('ARIAL','B',8);
		$this->SetFillColor(230,230,230);
		$this->Cell(15,3,'No. Ven.',1,0,'C',1);
		$this->Cell(15,3,'FECHA',1,0,'C',1);
		$this->Cell(40,3,'Punto venta',1,0,'C',1);		
		
		$this->Cell(15,3,'Total',1,0,'C',1);
		$this->Cell(90,3,'Motivo',1,0,'C',1);
		$this->Cell(20,3,'Cheques Imp.',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');
	$arrBus[]="(dgpemema.fec_ven between '$fecIni' and '$fecFin') AND dgpemema.cancela=1  ";	
	
	if($_POST['secodven']!='T') {
		 
$dt_caj=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select usuario from dgcatven where cod_ven='".$_POST['secodven']."'"));
			
			$user=$dt_caj["usuario"];			 
$arrBus[]="dgpemema.usuario='".$user."'";		 
	}
	if($_POST['setipven']!='T') 
	{
	 	$arrBus[]="dgpemema.tip_ven='".$_POST['setipven']."'";
	}	
	if($_POST['setipvent']!='T') {
  		$arrBus[]=' ptventa.nparte1='.$_POST['setipvent'].' ';
	}else {
		//$arrBus[]=' ptventa.nparte1!=0 ';
    }
	
 	$WHERE="WHERE ".implode(" AND ",$arrBus);
//CONCAT(dgcatven.nom_ven,' ',dgcatven.ap_pat_ven,' ',dgcatven.ap_mat_ven) as nomVen,			
	@$sql = mysqli_query($GLOBALS['con'] ,"
		SELECT 
			dgpemema.fol_ven,
			dgpemema.con_ven,
			dgpemema.tip_ven,
			dgpemema.fec_ven,
			dgpemema.hor_ven,
			dgpemema.cod_ven,
			dgpemema.iva_ven,
			ctmeser.np_desc1 as nomVen,
			dgpemema.tot_ven,
			dgpemema.imp_pag,
			dgpemema.sub_ven,
			fpos.mesa as mesa,
			ctmesas.np_desc1 as dmesa,
			ctarea.np_desc1 as  darea,
			ptventa.np_desc1 as dptvta,
			dgger_cajerosj.acc_hor as hora,
			fpropina.tip as propina,
			dgger_cajerosj.tip_mov as tipag,
			dgtippag.desc_pag as descpag,
			dgpemema.cancela as cancela
			,fimpresion.num_imp,
			dgpemema.mt_cancela

		FROM 
			dgpemema
			LEFT JOIN fpos ON fpos.fol_ven=dgpemema.fol_ven
			LEFT JOIN ctmesas ON ctmesas.nparte1=fpos.mesa
			LEFT JOIN ctarea ON ctarea.nparte1=ctmesas.area
			LEFT JOIN ctmeser  ON ctmeser.nparte1=dgpemema.cod_ven
			LEFT JOIN ptventa ON ptventa.nparte1=ctmesas.descripcion
			LEFT JOIN dgger_cajerosj ON dgger_cajerosj.pedido=dgpemema.fol_ven		
			LEFT JOIN  fpropina ON fpropina.fol_ven=dgpemema.fol_ven	
			LEFT JOIN  dgtippag ON dgtippag.tip_pag=dgger_cajerosj.tip_mov								
			left JOIN dgcatven ON dgpemema.cod_ven=dgcatven.cod_ven
			left join fimpresion on dgpemema.cod_ven=fimpresion.fol_ven
			left join dxopemaex on dgpemema.usuario=dxopemaex.oper
		$WHERE
		ORDER BY 
			dgpemema.con_ven");
	echo mysqli_error($GLOBALS['con']);
//	".$dt_empres['fec_alta']."

while($reg = mysqli_fetch_array($sql)){
	@$s = mysqli_query($GLOBALS['con'] ,"SELECT SUM(dgpememd.pre_bas*dgpememd.can_ref) as sumat FROM dgpememd, dgger_cajerosj, dgpemema WHERE dgpememd.cons_ven='".$reg['fol_ven']."' and dgger_cajerosj.pedido='".$reg['fol_ven']."' and dgpememd.cons_ven=dgpemema.fol_Ven ");
		echo mysqli_error($GLOBALS['con']);
		while($r = mysqli_fetch_array($s)){
		$suma=$r["sumat"];
		}
////---------------------------------	

//------suma de productos solo cortesia
	@$cr = mysqli_query($GLOBALS['con'] ,"SELECT SUM(dgpememd.pre_bas*dgpememd.can_ref) as sumat FROM dgpememd, dgger_cajerosj, dgger_cajerosd, dgpemema WHERE dgpememd.cons_ven='".$reg['fol_ven']."' and dgpememd.cons_ven=dgger_cajerosj.pedido and dgger_cajerosj.n_rec=dgger_cajerosd.n_rec and dgger_cajerosd.tip_pag=3 ");
		echo mysqli_error($GLOBALS['con']); 
		while($ccr = mysqli_fetch_array($cr)){
		$sumacr=$ccr["sumat"];
		}
////---------------------------------
		@$sq = mysqli_query($GLOBALS['con'] ,"SELECT dgpememd.*,dginventp.np_desc1 FROM dgpememd INNER JOIN dginventp ON dgpememd.nparte1 = dginventp.nparte1 WHERE dgpememd.cons_ven='".$reg['fol_ven']."'  order by dginventp.np_desc1");
		echo mysqli_error($GLOBALS['con']);
	while($rg = mysqli_fetch_array($sq)){
		$des=$rg["por_des"];					
		$iva +=$rg["iva_pre"];
		$total +=$rg["total_cd"];
	}

	$pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(250,250,250);
		
	$pdf->Cell(15,3,$reg["con_ven"],0,0,'C',1);
	$pdf->Cell(15,3,chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,0,'L',1);			
	$pdf->Cell(40,3,$reg['dptvta'],0,0,'L',1);					
	
	$pdf->Cell(15,3,$reg["sub_ven"],0,0,'R',1);	
	$pdf->Cell(90,3,$reg["mt_cancela"],0,0,'L',1);
	$pdf->Cell(20,3,$reg["num_imp"],0,1,'C',1);
		$granCorVen +=$sumacr;  //cortesia
		$granTotVen +=$reg["sub_ven"];  //subtotal
		$granPagVen +=$reg["iva_ven"];  //iva
		$granSalVen +=($reg["sub_ven"]);  //total
		$granPro    +=($reg["propina"]);  //propina				
		$numVen++;
		}

$pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(230,230,230); 	
		$pdf->Cell(127,3,'NUMERO DE VENTAS: '.$numVen.'',0,0,'L',1);
		$pdf->Cell(15,3,number_format($granTotVen-$granCorVen,2,'.',','),0,0,'R',1);
						
//number_format((($granSalVen-$granCorVen)+$granPro),2,'.',',')
$pdf->Output("CUENTAS CANCELADAS POR LOS CAJEROS.pdf","D");
?>


