<?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
		
		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,'LISTADO DE CUENTAS Y PRODUCTOS ELIMINADOS',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(32,3,'No. Ven.',1,0,'C',1);
		$this->Cell(60,3,'Descripcion',1,0,'C',1);
		$this->Cell(20,3,'Precio U.',1,0,'C',1);
		$this->Cell(15,3,'P.U. C/Desc.',1,0,'C',1);
		$this->Cell(15,3,'Hora',1,0,'C',1);
		$this->Cell(60,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.cancelado!=1 and dgpemema.cancela=0 and dgpemema.pagado='S' ";	
	
	if($_POST['secodven']!='T') {
		 $arrBus[]="dgger_cajerosj.vendedor='".$_POST['secodven']."'";		
	}
	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
		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
		$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 and dgpemema.cancelado=0 and dgger_cajerosj.cancelado=0 and dgpemema.cancelado!=1");
		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 and dgger_cajerosj.cancelado=0 and dgpemema.cancelado!=1");
		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']."' and dgpememd.cancela=1 order by dginventp.np_desc1");
		echo mysqli_error($GLOBALS['con']);
	while($rg = mysqli_fetch_array($sq)){
		$pdf->SetFillColor(240,240,240);
		$pdf->SetFont('Arial','',8);
		$pdf->Cell(32,3,$reg["fol_ven"],0,0,'C',0);
		$pdf->Cell(60,3,$rg['np_desc1'],0,0,'L',0);					
		$pdf->Cell(10,3, number_format(($rg['pre_bas']*$rg['can_ref']),2,'.',','),0,0,'L',0);					
		$pdf->Cell(15,3,number_format(($rg["total_cd"]-$rg["iva_pre"]),2,'.',','),0,0,'R',0); //desceunto
		$pdf->Cell(15,3,$reg["hor_ven"],0,0,'R',0); //desceunto
		$pdf->Cell(60,3,$reg["num_imp"],0,1,'C',0); //cheques	
		$des=$rg["por_des"];					
		$iva +=$rg["iva_pre"];
		$total +=$rg["total_cd"];
	}

		$granCorVen +=$sumacr;  //cortesia
		$granTotVen +=$suma;  //subtotal
		$granPagVen +=$reg["iva_ven"];  //iva
		$granSalVen +=($reg["tot_ven"]);  //total
		$granPro    +=($reg["propina"]);  //propina				
		$numVen++;
		}

					
//number_format((($granSalVen-$granCorVen)+$granPro),2,'.',',')
$pdf->Output("productos eliminados.pdf","D");
?>


