<?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']!='T'){
			$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";
		}	
	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(230,3,'REPROTE DE PAGOS',0,1,'C');
		$this->SetFont('Arial','B',8);
		$this->Cell(230,3,'',0,1,'L');		
		$this->Cell(230,3,'PERIODO DEL '.$_POST['fecini'].' AL '.$_POST['fecfin'].'',0,1,'C');
		$this->Cell(230,3,'PUNTO DE VENTA:'.$pventa.                        '',0,1,'C');
		$this->Cell(230,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(20,3,'Fol. Vta.',1,0,'C',1);
		$this->Cell(25,3,'P. Vta./Mesa',1,0,'C',1);
		$this->Cell(15,3,'Hora',1,0,'C',1);
		$this->Cell(15,3,'Fecha',1,0,'C',1);		
		$this->Cell(35,3,'Mesero',1,0,'C',1);
		$this->Cell(35,3,'FP',1,0,'C',1);		
		$this->Cell(20,3,'Desc %',1,0,'C',1);		
		$this->Cell(15,3,'Desc $',1,0,'C',1);				
		$this->Cell(15,3,'Cortesia',1,0,'C',1);
		$this->Cell(15,3,'Propina',1,0,'C',1);		
		$this->Cell(15,3,'Subtotal',1,0,'C',1);
		$this->Cell(12,3,'Iva',1,0,'C',1);
		$this->Cell(15,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('L','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";	
	
	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.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,
			dgger_cajerosd.tip_pag as tipag,
			dgtippag.ini_pag as descpag,
			dgpemema.cancela as 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 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
		group by dgpemema.fol_ven
		ORDER BY 
			dgpemema.con_ven");
	echo mysqli_error($GLOBALS['con']);
	while($reg = mysqli_fetch_array($sql)){
//------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"];
	}
////---------------------------------
	//IMPRESION DE CUENTAS NO PAGADAS
	$pdf->SetFont('Arial','',7);
	 if (($reg["imp_pag"]==0.00) && ($reg["cancela"]==1)){
	$pdf->SetFillColor(230,230,230);
	$pdf->Cell(20,3,$reg["con_ven"],0,0,'C',1);
	$pdf->Cell(25,3,$reg["dptvta"].' / '.$reg["dmesa"],0,0,'L',1);
	$pdf->Cell(15,3,$reg["hora"],0,0,'C',1);
	$pdf->Cell(15,3,chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,0,'C',1);	
	$pdf->Cell(35,3,$reg["cod_ven"].' '.$reg["nomVen"],0,0,'L',1);					
	$pdf->Cell(35,3,'',0,0,'R',1);			
	$pdf->Cell(107,3,'CANCELADO',0,1,'C',1);			
//	$pdf->Cell(20,3,'',0,0,'R',1);			
	}else {
		$pdf->SetFont('ARIAL','',7);
	$pdf->SetFillColor(260,260,260);
	$pdf->Cell(20,3,$reg["con_ven"],0,0,'C',1);
//	$pdf->Cell(20,3,chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,0,'L',1);			
	$pdf->Cell(25,3,$reg["dptvta"].' / '.$reg["dmesa"],0,0,'L',1);
	$pdf->Cell(15,3,$reg["hora"],0,0,'C',1);
	$pdf->Cell(15,3,chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,0,'C',1);	
	$pdf->Cell(35,3,$reg["cod_ven"].' '.$reg["nomVen"],0,0,'L',1);	
	if ($reg['tipag']==''){				
	$pdf->Cell(142,3,'CUENTA ABIERTA',0,0,'L',1);			
	}else{
		$folio=mysqli_query($GLOBALS['con'] ,"select dgger_cajerosj.num_cli as cliente,dgger_cajerosd.tip_pag as tppag,dgtippag.ini_pag as pagoo from dgger_cajerosd inner join dgtippag on dgtippag.tip_pag=dgger_cajerosd.tip_pag inner join dgger_cajerosj on dgger_cajerosj.n_rec=dgger_cajerosd.n_rec
	   where dgger_cajerosj.pedido='".$reg['fol_ven']."'");
	
		$pdf->SetFont('ARIAL','',5);
		$a5=0;
		$cont=0;
		$_SESSION['clientec']=0;
		 while($rg=mysqli_fetch_array($folio)){
		 	if ($rg['tppag']==1){
			$cont++;
			$_SESSION['clientec']=$rg['cliente'];
			}
			$pdf->Cell(4,3,$rg['pagoo'].' | ',0,0,'C',0);
		$a5++; 
		}
		$dest=35-($a5*4);	
		 if ($GLOBALS['con']t>=1){	
	   	$sql_cli=mysqli_query($GLOBALS['con'] ,"select * from valeryus19 where n_reg=$_SESSION['clientec']");
	$reg_hb=mysqli_fetch_array($sql_cli);
		if ($reg_hb['fnumcto']!='')
		$hbit=$reg_hb['fnumcto'];
		else
		$hbit=$reg_hb['fmuncto'];
		$pdf->Cell($dest,3,'F.'.$reg_hb['n_reg'].'H.'.$hbit,0,0,'C',0);
		}else{
		$pdf->Cell($dest,3,'',0,0,'C',0);
		}		
	}	
		$pdf->SetFont('ARIAL','',7);
	//$pdf->Cell(17,3,$reg["tipag"].' '.$reg["descpag"],0,0,'L',1);			
	$pdf->Cell(20,3,number_format($reg['descuento'],2,'.',',').'%',2,0,'R',1);  //descuento
	$pdf->Cell(15,3,number_format($desc=$suma*($reg['descuento']/100),2,'.',','),0,0,'R',1);			//cortesia
	$pdf->Cell(15,3,number_format($sumacr,2,'.',','),0,0,'R',1);			//cortesia
	$pdf->Cell(15,3,number_format($reg["propina"],2,'.',','),0,0,'R',1);			//propina	
	$pdf->Cell(15,3,number_format($suma,2,'.',','),0,0,'R',1);			//subt
	$pdf->Cell(12,3,number_format($reg["iva_ven"],2,'.',','),0,0,'R',1);		//iva				
	$pdf->Cell(15,3,number_format($sumtot=(($suma-$desc-$sumacr)+$reg['propina']),2,'.',','),0,1,'R',1);			//total	
		$granDesVen +=$desc;  //descuento
		$granCorVen +=$sumacr;  //cortesia
		$granTotVen +=$suma;  //subtotal
		$granPagVen +=$reg["iva_ven"];  //iva
		if($reg["imp_pag"]!=0){
		 $granSalVen +=$sumtot;  //total
		}
		$granPro    +=($reg["propina"]);  //propina		
		$numVen++;
		}
	
		
}

//**************************************************pagos de cuentas totales*************************************************+*****//
	@$sql = 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,
			dgger_cajerosd.tip_pag as tipag,
			dgtippag.ini_pag as descpag,
			dgpemema.cancela as cancela,
			dgger_cajerosj.fol_unp as folp
		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_unp=dgger_cajerosj.fol_unp
			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
		GROUP BY dgger_cajerosj.fol_unp
		ORDER BY 
			dgpemema.con_ven");
	echo mysqli_error($GLOBALS['con']);
	while($reg = mysqli_fetch_array($sql)){
//------suma de productos
	@$s=mysqli_query($GLOBALS['con'] ,"select * from dgpemema where fol_ven IN (select pedido from dgger_cajerosj where fol_unp='".$reg['folp']."')");
		$sumare=0;
	while($r=mysqli_fetch_array($s)){
	
	$resu3=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select sum(pre_bas*can_ref) as summ from dgpememd where cons_ven = '".$r['fol_ven']."' and cancela=0 "));
	 $sumare+=$resu3['summ'];
	}
////---------------------------------	

//------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.fol_unp='".$reg['folp']."' and dgger_cajerosd.tip_pag=3");
	echo mysqli_error($GLOBALS['con']);
	$ccr = mysqli_fetch_array($cr);
	$sumacr=$ccr["sumat"];
	
	
	@$cr2=mysqli_query($GLOBALS['con'] ,"select dgger_cajerosj.imp_pag as sumat from dgger_cajerosj where dgger_cajerosj.fol_unp='".$reg['folp']."' group by dgger_cajerosj.fol_unp");
	echo mysqli_error($GLOBALS['con']);
	$ccr2= mysqli_fetch_array($cr2);
	$sumacr2=$ccr2["sumat"];	
////---------------------------------

	//IMPRESION DE CUENTAS NO PAGADAS
		//IMPRESION DE CUENTAS NO PAGADAS
	$pdf->SetFont('Arial','',7);
	 if (($reg["imp_pag"]==0.00) && ($reg["cancela"]==1)){
	$pdf->SetFillColor(230,230,230);
$folio=mysqli_query($GLOBALS['con'] ,"select dgpemema.tot_ven as importe,dgpemema.con_ven as folio, dgger_cajerosj.fol_unp as folpf from dgger_cajerosj inner join dgpemema on dgpemema.fol_ven=dgger_cajerosj.pedido
	   where dgger_cajerosj.fol_unp='".$reg['folp']."'");
	   $saldo=0;
	
		$pdf->SetFont('ARIAL','',5);
		$a5=0;
//		$saldo=0;
		 while($rg=mysqli_fetch_array($folio)){
	//	  $saldo+=$rg['importe'];
			$pdf->Cell(4,3,$rg['folio'].' | ',0,0,'C',1);
		$a5++; 
		}
		$dest=20-($a5*4);	
		$pdf->Cell($dest,3,'',0,0,'C',1);
		
//	$pdf->Cell(15,3,$reg["con_ven"],0,0,'C',1);
	$pdf->SetFont('Arial','',7);
	$pdf->Cell(25,3,$reg["dptvta"].' / '.$reg["dmesa"],0,0,'L',1);
	$pdf->Cell(15,3,$reg["hora"],0,0,'C',1);
	$pdf->Cell(15,3,chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,0,'C',1);	
	$pdf->Cell(35,3,$reg["cod_ven"].' '.$reg["nomVen"],0,0,'L',1);					
	$pdf->Cell(35,3,'',0,0,'R',1);			
	$pdf->Cell(107,3,'CANCELADO',0,1,'C',1);			
//	$pdf->Cell(20,3,'',0,0,'R',1);			
	}else {
	
	$pdf->SetFillColor(260,260,260);
		$folio=mysqli_query($GLOBALS['con'] ,"select dgpemema.tot_ven as importe,dgpemema.con_ven as folio, dgger_cajerosj.fol_unp as folpf from dgger_cajerosj inner join dgpemema on dgpemema.fol_ven=dgger_cajerosj.pedido
	   where dgger_cajerosj.fol_unp='".$reg['folp']."'");
	   $saldo=0;
	
		$pdf->SetFont('ARIAL','',5);
		$a5=0;
		$saldo=0;
		 while($rg=mysqli_fetch_array($folio)){
		 	$saldo+=$rg['importe'];
			$pdf->Cell(4,3,$rg['folio'].' | ',0,0,'C',0);
		$a5++; 
		}
		$dest=20-($a5*4);	
		$pdf->Cell($dest,3,'',0,0,'C',0);
		$pdf->SetFont('ARIAL','',7);
//	$pdf->Cell(15,3,$reg["con_ven"],0,0,'C',1);
//	$pdf->Cell(20,3,chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,0,'L',1);			
	$pdf->Cell(25,3,$reg["dptvta"].' / '.$reg["dmesa"],0,0,'L',1);
	$pdf->Cell(15,3,$reg["hora"],0,0,'C',1);
	$pdf->Cell(15,3,chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,0,'C',1);	
	$pdf->Cell(35,3,$reg["cod_ven"].' '.$reg["nomVen"],0,0,'L',1);	
	if ($reg['tipag']==''){				
	$pdf->Cell(147,3,'CUENTA ABIERTA',0,0,'L',1);			
	}else{
			$folio=mysqli_query($GLOBALS['con'] ,"select dgger_cajerosj.num_cli as cliente,dgger_cajerosd.tip_pag as tppag,dgtippag.ini_pag as pagoo from dgger_cajerosd inner join dgtippag on dgtippag.tip_pag=dgger_cajerosd.tip_pag inner join dgger_cajerosj on dgger_cajerosj.n_rec=dgger_cajerosd.n_rec
	   where dgger_cajerosj.pedido='".$reg['fol_ven']."'");
	
		$pdf->SetFont('ARIAL','',5);
		$a5=0;
		$cont=0;
		$_SESSION['clientec']=0;
		 while($rg=mysqli_fetch_array($folio)){
		 	if ($rg['tppag']==1){
			$cont++;
			$_SESSION['clientec']=$rg['cliente'];
			}
			$pdf->Cell(4,3,$rg['pagoo'].' | ',0,0,'C',0);
		$a5++; 
		}
		$dest=35-($a5*4);	
		if ($GLOBALS['con']t>=1){	
	   	$sql_cli=mysqli_query($GLOBALS['con'] ,"select * from valeryus19 where n_reg=$_SESSION['clientec']");
		$reg_hb=mysqli_fetch_array($sql_cli);
		if ($reg_hb['fnumcto']!='')
		$hbit=$reg_hb['fnumcto'];
		else
		$hbit=$reg_hb['fmuncto'];
		$pdf->Cell($dest,3,'F.'.$reg_hb['n_reg'].'H.'.$hbit,0,0,'C',0);
		}else{
		$pdf->Cell($dest,3,'',0,0,'C',0);
		}		
	}	
		$pdf->SetFont('ARIAL','',7);
$a1=($sumare+$reg['propina'])-$sumacr2;
$porDes=round($a1/$sumare*100,4);		
	//$pdf->Cell(17,3,$reg["tipag"].' '.$reg["descpag"],0,0,'L',1);			
	$pdf->Cell(20,3,number_format($porDes,2,'.',',').'%',2,0,'R',1);  //descuento
	$pdf->Cell(15,3,number_format($dsc=$a1,2,'.',','),0,0,'R',1);			//descuento en moneda
	$pdf->Cell(15,3,number_format($sumacr,2,'.',','),0,0,'R',1);			//cortesia
	$pdf->Cell(15,3,number_format($reg["propina"],2,'.',','),0,0,'R',1);			//propina	
	$pdf->Cell(15,3,number_format($sumare,2,'.',','),0,0,'R',1);			//subt
	$pdf->Cell(12,3,number_format($reg["iva_ven"],2,'.',','),0,0,'R',1);		//iva				
	$pdf->Cell(15,3,number_format($tot=(($sumare-$sumacr-$dsc)+$reg["propina"]),2,'.',','),0,1,'R',1);			//total			
			
		
			///--------------------rep cancelados----------
		$granDesVen +=$dsc;  //descuento
		$granCorVen +=$sumacr;  //cortesia
		$granTotVen +=$sumare;  //subtotal
		$granPagVen +=$reg["iva_ven"];  //iva
		if($reg["imp_pag"]!=0){
		$granSalVen +=$tot;  //total
		}
		$granPro    +=($reg["propina"]);  //propina		
		$numVen++;
		}
	}




$pdf->SetFont('ARIAL','B',9);
	$pdf->SetFillColor(230,230,230);
		$pdf->Cell(165,3,'TOTALES',0,0,'L',1);
		$pdf->Cell(15,3,number_format($granDesVen,2,'.',','),0,0,'R',1);
		$pdf->Cell(15,3,number_format($granCorVen,2,'.',','),0,0,'R',1);
		$pdf->Cell(15,3,number_format($granPro,2,'.',','),0,0,'R',1);
		$pdf->Cell(17,3,number_format($granTotVen,2,'.',','),0,0,'R',1);						
		$pdf->Cell(12,3,number_format($granPagVen,2,'.',','),0,0,'R',1);
		$pdf->Cell(14,3,number_format($granSalVen,2,'.',','),0,1,'R',1);				
		
$pdf->Output("REPORTE DE PAGOS.pdf","D");
?>


