<?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 ctmeser where nparte1='".$_POST['setipvent']."'"));
			$tipoventa=$dt_tipven["np_desc1"];
		}else {
			$tipoventa="TODAS";
		}	
	if($_POST['setipven']!='T'){
			$dt_tipven = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from ptventa where nparte1='".$_POST['setipven']."'"));
			$pventa=$dt_tipven["np_desc1"];
		}else {
			$pventa="TODOS";
		}	
		
	
	    $this->Cell(190,3,'PROPINAS POR PAGAR',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,'MESERO:'.       $tipoventa.                           '',0,1,'C');		
	
	    //Salto de línea
	    $this->Ln(3);
		$this->SetFont('ARIAL','B',8);
		$this->SetFillColor(230,230,230);

		$this->Cell(15,3,'',0,0,'C',0);
		$this->Cell(29,3,'Fecha',0,0,'C',1);		
		$this->Cell(29,3,'Folio',0,0,'C',1);		
		//$this->Cell(24,3,'Mesero',1,0,'C',1);		
		$this->Cell(29,3,'Subtotal Cta.',0,0,'C',1);		
		$this->Cell(29,3,'Propina',0,0,'C',1);
		$this->Cell(20,3,'Servicio',0,0,'C',1);
		$this->Cell(20,3,'Porcentaje',0,0,'C',1);		
		$this->Cell(20,3,'Tip Pago',0,0,'C',1);	
		$this->Cell(25,3,'',0,1,'C',0);
	}
	//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[]=' ptventa.nparte1='.$_POST['setipven'].' ';
	}	
	
	if($_POST['setipvent']!='T') {
  		$arrBus[]=' dgpemema.cod_ven='.$_POST['setipvent'].' ';
	}else {
		$arrBus[]=' dgpemema.cod_ven!=0 ';
    }
	if($_POST['setippag']!='T') {
  		$arrBus[]=' dgger_cajerosd.tip_pag='.$_POST['setippag'].' ';
	}else {
		$arrBus[]=' dgger_cajerosd.tip_pag!=0 ';
    }
	
 	$WHERE="WHERE ".implode(" AND ",$arrBus);

 $sqlMes=mysqli_query($GLOBALS['con'] ,"SELECT distinct(cod_ven) FROM dgpemema
	LEFT JOIN ctmeser ON ctmeser.nparte1=dgpemema.cod_ven
	 where fec_ven between '$fecIni' and '$fecFin'
	 AND dgpemema.cancelado!=1 and dgpemema.cancela=0 and dgpemema.pagado='S'
	  order by np_desc1");
	while($mesero=mysqli_fetch_array($sqlMes)){

//CONCAT(dgcatven.nom_ven,' ',dgcatven.ap_pat_ven,' ',dgcatven.ap_mat_ven) as nomVen,			
	@$result = mysqli_query($GLOBALS['con'] ,"
		SELECT 
			dgpemema.fol_ven,
			dgpemema.con_ven,
			dgpemema.tip_ven,
			dgpemema.fec_ven,
			dgpemema.cod_ven,
			dgpemema.iva_ven,
			ctmeser.np_desc1 as nomVen,
			dgpemema.tot_ven,
			dgpemema.imp_pag,
			dgpemema.sub_ven,
			dgpemema.por_des as descuento,
			fpos.mesa as mesa,
			ctmesas.np_desc1 as dmesa,
			ctarea.np_desc1 as  darea,
			ptventa.tip_ven as dptvta,
			dgger_cajerosj.acc_hor as hora,
			fpropina.tip as propina,
			dgpemema.servicio as servicio,
			dgger_cajerosd.tip_pag as tipag,
			dgtippag.ini_pag as descpag,
			dgpemema.cancela as cancela,
				dgtippag.desc_pag as pagotip
		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 dgger_cajerosd ON dgger_cajerosd.n_rec=dgger_cajerosj.n_rec	
			LEFT JOIN  dgtippag ON dgtippag.tip_pag=dgger_cajerosd.tip_pag
			LEFT JOIN dgcatven ON dgpemema.cod_ven=dgcatven.cod_ven
		
		$WHERE and dgger_cajerosj.fol_unp=0 and dgpemema.cod_ven='".$mesero['cod_ven']."' 
		group by dgpemema.fol_ven
		ORDER BY 
			dgpemema.con_ven");
			$pdf->Cell(25,3,mysqli_error($GLOBALS['con']),2,0,'R',0);  
			$mesEr0=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT np_desc1 FROM ctmeser WHERE nparte1='".$mesero['cod_ven']."'"));
			$pdf->Cell(25,3,'',2,0,'R',0);  
	$pdf->Cell(30,3,$mesEr0['np_desc1'],2,1,'L',0);  
    
	$totp=0;
	$tots=0;
	$totc=0;
	
	while($reg = mysqli_fetch_array($result))
	{
	//------suma de productos
	@$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!=1 and dgpemema.cancela=0 and dgger_cajerosj.cancelado=0 and dgpememd.cancela=0");
		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 dgger_cajerosd.importe as sumat from dgger_cajerosj left join dgger_cajerosd on (dgger_cajerosd.n_rec=dgger_cajerosj.n_rec) where dgger_cajerosj.pedido='".$reg['fol_ven']."' and dgger_cajerosd.tip_pag=3");
	echo mysqli_error($GLOBALS['con']);
	$ccr = mysqli_fetch_array($cr);
	if ($ccr['sumat']!=0){
	$sumacr=$ccr["sumat"]+$reg['propina'];
	}else{
	$sumacr=$ccr["sumat"];
	}
////---------------------------------	
	$tip = $reg["fol_ven"];
	$iop = mysqli_query($GLOBALS['con'] ,"select * from fpropina where fol_ven = '$tip'");
	$qwe = mysqli_fetch_array($iop); 
	//if ($qwe['tip']>0 || $reg["servicio"]>0){
		$codigo = $reg["cod_ven"];
		$rsd = mysqli_query($GLOBALS['con'] ,"select * from ctmeser where nparte1 = '$codigo'");
		$djk = mysqli_fetch_array($rsd);
		$serv = $reg["servicio"];
$reg['descuento'];  //porcentaje descuento
	$desc=($reg['descuento']<80)?$suma*($reg['descuento']/100):$reg['descuento'];  //cantidad descuento descuento			
	$sumtot=(($suma-$desc))/1.16;			//saldo	
	
	
	
	
	$pdf->Cell(15,3,'',2,0,'R',0);  
	$pdf->Cell(29,3,chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,0,'C',0);  		
	$pdf->Cell(29,3,$reg['con_ven'],0,0,'C',0);  	
	//$pdf->Cell(54,3,$djk['np_desc1'],1,0,'C',0);  
	$pdf->Cell(29,3,'$'.number_format( $sumtot,2,'.',','),0,0,'R',0);  	
	$pdf->Cell(20,3,'$'.number_format($qwe["tip"],2,'.',','),0,0,'R',0); 
	$pdf->Cell(20,3,'$'.number_format($serv,2,'.',','),0,0,'R',0); 		
	$totv+=$sumtot;
		$totp+=$qwe['tip'];
		$tots+=$serv;
		$totc+=$sumtot;
		$porce = $sumtot;
		$taje = $qwe["tip"];
		$p = ($taje * 100);
		if ($porce==0)
		$o=0;
		else
		$o =$p / $porce;
	
	     $totsiva=$sumtot*1.16;
	      $totprop=$qwe['tip']+$reg["servicio"];
		  if ($totsiva==0)
		  {
			$por=0;
		  }else 
		  $por=($totprop*100)/$totsiva;
	
	
	$pdf->Cell(20,3,number_format($por,2,'.',',').'%',0,0,'R',0);  	
$pdf->Cell(30,3,$reg['pagotip'],0,0,'R',0);  	
	$pdf->Cell(25,3,'',0,1,'R',0);  		
	// }	
	}
	
	$pdf->SetFillColor(230,230,230);
		$pdf->Cell(15,3,'',0,0,'R',0);				
		$pdf->Cell(20,3,'',0,0,'L',1);
		$pdf->Cell(38,3,'TOTALES X MESERO',0,0,'L',1);
		$pdf->Cell(29,3,'$'.number_format($totc,2,'.',','),0,0,'R',1);
		$pdf->Cell(29,3,'$'.number_format($totp,2,'.',','),0,0,'R',1);
		$pdf->Cell(29,3,'$'.number_format($tots,2,'.',','),0,0,'R',1);
		$pdf->Cell(25,3,'',0,0,'R',1);						
		$pdf->Cell(25,3,'',0,1,'R',0);	
			
		$pdf->Cell(194,3,'',0,1,'R',0);
		
		$totpr+=$totp;
						$totse+=$tots;
						$totve+=$totc;
	
	}

$pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(230,230,230);
		$pdf->Cell(15,3,'',0,0,'R',0);
		$pdf->Cell(20,3,'',0,0,'L',1);				
		$pdf->Cell(38,3,'TOTALES',0,0,'L',1);
		$pdf->Cell(29,3,'$'.number_format($totve,2,'.',','),0,0,'R',1);
		$pdf->Cell(29,3,'$'.number_format($totpr,2,'.',','),0,0,'R',1);
		$pdf->Cell(29,3,'$'.number_format($totse,2,'.',','),0,0,'R',1);
		$pdf->Cell(25,3,'',0,0,'R',1);						
		$pdf->Cell(25,3,'',0,1,'R',0);				
		
$pdf->Output("Propinas_Por_Pagar.pdf","D");
?>


