<?php
 
session_cache_limiter('private');
session_start();
session_start();
include("DMMYSQLG.php");
include("DSA1000.php");
include("DMMYSQLG.php");
//include("DF30000.inc");
include("DF80000.php");

$name="Reporte Tipo de Pago.xls"; 
if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
	header('Content-Type: application/force-download');
else
	header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="'.$name.'"');

?>
<table  width="100%">
    	<tr>
        	<th colspan="6">
          	<div align="left">
            	REPORTE:  TIPO DE PAGO			
          	</div>			</th>
      	</tr>
    	<tr>
    	  <th colspan="6" align="justify"><?php
 if ($_POST["setipvent"]=="T"){
					echo "PUNTO DE VENTA: ";
					echo "TODAS"; 
					} else 
					{
					@$bus = mysqli_query($con ,"SELECT * FROM ptventa where nparte1 = '".$_POST['setipvent']."'");
					$re = mysqli_fetch_array($bus);
					echo "PUNTO DE VENTA: ";
					echo $re['np_desc1'];
					}			
				 ?></th>
  	  </tr>
    	<tr>
    	  <th colspan="6" align="justify"><?php
 if ($_POST["setipven"]=="T"){
					echo "TIP. PAGO.: ";
					echo "TODAS"; 
					} else 
					{
					@$bus = mysqli_query($con ,"SELECT * FROM dgtippag WHERE tip_pag = '".$_POST['setipven']."'");
					$re = mysqli_fetch_array($bus);
					echo "TIP. PAGO.: ";
					echo $re['desc_pag'];
					}			
				 ?></th>
  	  </tr>
    	<tr>
    	  <th colspan="6" ><?php
 if ($_POST["secodven"]=="T"){
					echo "CAJERO: ";
					echo "TODOS"; 
					} else 
					{
					@$bus1 = mysqli_query($con ,"SELECT * FROM dgcatven WHERE cod_ven = '".$_POST['secodven']."'");
					$re1 = mysqli_fetch_array($bus1);
					echo "CAJERO: ";
					echo $re1['nom_ven'];
					echo ' ';
					echo $re1['ap_pat_ven']; 
					echo ' ';
					echo $re1['ap_mat_ven'];
					}			
				 ?></th>
  	  </tr>


<?php


	$fecIni=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
	$fecFin=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d');
	
	
	if($_POST['secodven']!='T') {
		$arrBus[]="dgger_cajerosj.vendedor='".$_POST['secodven']."'";		
	}else {
		$arrBus[]="dgger_cajerosj.vendedor!='0'";		
	}
	if($_POST['setipven']!='T') 
	{
	 	$arrBus[]="dgtippag.tip_pag='".$_POST['setipven']."'";
	}
	if($_POST['setipvent']!='T') {
  		$arrBus[]=' ptventa.nparte1='.$_POST['setipvent'].' ';
  }else {
		$arrBus[]=' ptventa.nparte1!=0 ';
  }
	$arrBus[]=" dgger_cajerosj.cancelado=0 AND (dgger_cajerosj.fec_rec between '".$fecIni."' and '".$fecFin."') ";	
   	$WHERE="WHERE ".implode(" AND ",$arrBus);
	
	@$sql = mysqli_query($con ,"
		SELECT 
			dgger_cajerosj.tip_mov as tipag,
			dgger_cajerosd.tip_pag as tipo_pago,
			dgtippag.tip_pag as idpag,
			dgtippag.desc_pag as descpag,
			dgger_cajerosj.vendedor as vendedor
		FROM 
			dgger_cajerosj
			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 fpos ON fpos.fol_ven=dgger_cajerosj.pedido
			LEFT JOIN ctmesas ON ctmesas.nparte1=fpos.mesa
			LEFT JOIN ptventa ON ptventa.nparte1=ctmesas.descripcion
		$WHERE
		GROUP BY dgtippag.tip_pag
		ORDER BY 
			dgtippag.desc_pag");
	echo mysqli_error($con);
	$a=0;
	while($reg = mysqli_fetch_array($sql))
	{
	//ENCABEZADO DE CADA PAGO 

	echo "<tr>
			  <td class=\"estiloDet\">'".$reg['idpag']."'</td>
			  <td class=\"estiloDet\" colspan=\"5\">'".$reg['descpag']."'</td>
          </tr>";
///---------------------------------------PAGOS Y PROPINA DE FORMA INDIVIDUAL----------------------------------------------------------//		
if($_POST['secodven']!='T') {
  $vendedor=' dgger_cajerosj.vendedor='.$reg['vendedor'].' ';
  }else {
  $vendedor=' dgger_cajerosj.vendedor!=0 ';
  }

if($_POST['setipvent']!='T') {
  $ptvta=' ptventa.nparte1='.$_POST['setipvent'].' ';
  }else {
  $ptvta=' ptventa.nparte1!=0 ';
  }

	//ENCABEZADO PARA LOS DETALLES 
	echo '<tr class="typeagre">
	 	  <td><div align="center">Folio Orden</div></td>
		  <td><div align="center">Cajero</div></td>
		  <td><div align="center">Referencia</div></td>
 		  <td><div align="center">Autoriza</div></td>
		  <td><div align="center">Importe</div></td>
  		  <td><div align="center">Punto de Venta</div></td>
	 	</tr>';
	@$sql_pg = mysqli_query($con ,"
		SELECT 
			dgpemema.con_ven as fol_ven,
			CONCAT(dgcatven.nom_ven,' ',dgcatven.ap_pat_ven,' ',dgcatven.ap_mat_ven) as nomVen,	
			dgger_cajerosd.importe as saldo,	
			dgger_cajerosd.ref as referencia,			
			dgger_cajerosj.pedido as pedido,
			dgger_cajerosj.num_cli as cliente,	
			fpropina.tip as propina,
			ptventa.np_desc1 as dptvta,
			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 ptventa ON ptventa.nparte1=ctmesas.descripcion
			INNER 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 dgger_cajerosj.vendedor=dgcatven.cod_ven
			
		WHERE dgtippag.tip_pag='".$reg['idpag']."' and dgger_cajerosj.fol_unp=0 and dgger_cajerosj.cancelado=0 AND dgpemema.cancela=0 and dgpemema.cancelado=0 AND $vendedor AND $ptvta AND (dgpemema.fec_ven between '".$fecIni."' and '".$fecFin."')
			ORDER BY 
			dgpemema.con_ven");
	echo mysqli_error($con);
	$a=0;
				$b=0;	
	$granTot=0;
	while($rgp = mysqli_fetch_array($sql_pg)){
	if ($reg['idpag']==1){
	$sql_cli=mysqli_query($con ,"select * from valeryus19 where n_reg='".$rgp['cliente']."'");
	$reg_hb=mysqli_fetch_array($sql_cli);
	if ($reg_hb['fnumcto']!='')
	$hbit=$reg_hb['fnumcto'];
	else
	$hbit=$reg_hb['fmuncto'];
	
		echo '<tr class="detFacTr">';
		echo '<td class="detFacC">'.$rgp["fol_ven"].'&nbsp;</td>'; 
		echo '<td class="detFacL">'.$rgp['nomVen'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FOLIO:  '.$reg_hb['n_reg'].' &nbsp;&nbsp;&nbsp;&nbsp;HABITACION:'.$hbit.'</td>';  
		echo '<td class="detFacL">'.$rgp['referencia'].'&nbsp;</td>';  
		echo '<td class="detFacL">'.$rgp['autoriza'].'&nbsp;</td>';  			
		echo '<td class="detFacR">$'.number_format(($rgp["saldo"]+$rgp['propina']),2,'.',',').'</td>'; 
		echo '<td class="detFacL"> &nbsp;&nbsp;'.$rgp['dptvta'].'</td>'; 		 
		echo '</tr>';	
	}else{
		echo '<tr class="detFacTr">';
		echo '<td class="detFacC">'.$rgp["fol_ven"].'&nbsp;</td>'; 
		echo '<td class="detFacL">'.$rgp['nomVen'].'&nbsp;</td>';  
		echo '<td class="detFacL">'.$rgp['referencia'].'&nbsp;</td>';  
		echo '<td class="detFacL">'.$rgp['autoriza'].'&nbsp;</td>';  			

		
		echo '<td class="detFacR">$'.number_format(($rgp["saldo"]+$rgp['propina']),2,'.',',').'</td>'; 

		echo '<td class="detFacL"> &nbsp;&nbsp;'.$rgp['dptvta'].'</td>'; 		 
		echo '</tr>';	
	}	$numVen ++;
		$a ++;
		$granTot +=$rgp["saldo"]+$rgp['propina'];  //suma saldo individual
		$granTot2 +=$rgp["saldo"]+$rgp['propina'];  //suma saldo				
		} //cierra el detalle de pago INDIVIDUAL + PROPINA
///------------------------------------------------------------------------------------------------------------------------------------//		
//****************************************FORMASD DE PAGO PAGADAS DE FORMA general***************************************+*////

	@$sql_pro = mysqli_query($con ,"
		SELECT 
			dgger_cajerosj.pedido as fol_ven,
			CONCAT(dgcatven.nom_ven,' ',dgcatven.ap_pat_ven,' ',dgcatven.ap_mat_ven) as nomVen,	
			dgger_cajerosd.importe as saldo,
			dgger_cajerosd.ref as referencia,	
			dgger_cajerosj.pedido as pedido,
			dgger_cajerosj.num_cli as cliente,	
			fpropina.tip as propina,
			ptventa.np_desc1 as dptvta,
			dgger_cajerosj.fol_unp as folp
		FROM 
			dgger_cajerosj
			LEFT JOIN fpos ON fpos.fol_ven=dgger_cajerosj.pedido
			LEFT JOIN ctmesas ON ctmesas.nparte1=fpos.mesa
			LEFT JOIN ptventa ON ptventa.nparte1=ctmesas.descripcion		
			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 dgger_cajerosj.vendedor=dgcatven.cod_ven
			
		WHERE dgtippag.tip_pag='".$reg['idpag']."' and dgger_cajerosj.fol_unp!=0 and dgger_cajerosj.cancelado=0 AND $vendedor AND $ptvta AND (dgger_cajerosj.fec_rec between '".$fecIni."' and '".$fecFin."')
		group by dgger_cajerosj.fol_unp
		ORDER BY 
			dgger_cajerosj.pedido");
	echo mysqli_error($con);
	if (mysqli_num_rows($sql_pro)!=0){
		//ENCABEZADO PARA LA PROPINA
/*	echo "<tr>
			  <td class=\"estiloDet\"></td>
			  <td class=\"estiloDet\" colspan=\"5\">PROPINAS PAGADAS DE FORMA GENERAL </td>
          </tr>";*/
	}
	$granTotpro=0;
	while($rgpro = mysqli_fetch_array($sql_pro)){
	if ($reg['idpag']==1){
	$sql_cli=mysqli_query($con ,"select * from valeryus19 where n_reg='".$rgpro['cliente']."'");
	$reg_hb=mysqli_fetch_array($sql_cli);
	if ($reg_hb['fnumcto']!='')
	$hbit=$reg_hb['fnumcto'];
	else
	$hbit=$reg_hb['fmuncto'];
	
		echo '<tr class="detFacTr">';
	$folio=mysqli_query($con ,"select 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='".$rgpro['folp']."'");
		echo '<td class="detFacC">&nbsp;';
			   while($rg=mysqli_fetch_array($folio)){
	   echo $rg['folio'];
	   echo ' | ';
	   }
		echo '</td>'; 
	
		echo '<td class="detFacL">'.$rgpro['nomVen'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FOLIO:  '.$reg_hb['n_reg'].' &nbsp;&nbsp;&nbsp;&nbsp;HABITACION:'.$hbit.'</td>';  
		echo '<td class="detFacL">'.$rgpro['referencia'].'&nbsp;</td>';  
		echo '<td class="detFacL">'.$rgpro['autoriza'].'&nbsp;</td>';  			
		echo '<td class="detFacR">$'.number_format(($rgpro["saldo"]),2,'.',',').'</td>'; 
		echo '<td class="detFacL"> &nbsp;&nbsp;'.$rgpro['dptvta'].'</td>'; 		 
		echo '</tr>';	
	}else{
		echo '<tr class="detFacTr">';
		$folio=mysqli_query($con ,"select dgpemema.con_ven as folio, dgger_cajerosj.fol_unp as folpf from dgger_cajerosj left join dgpemema on dgpemema.fol_ven=dgger_cajerosj.pedido
	   where dgger_cajerosj.fol_unp='".$rgpro['folp']."'");
		echo '<td class="detFacC">&nbsp;';
		while($rg=mysqli_fetch_array($folio)){
	   echo $rg['folio'];
	   echo ' | ';
	   }
		echo '</td>'; 
		echo '<td class="detFacL">'.$rgpro['nomVen'].'&nbsp;</td>';  
		echo '<td class="detFacL">'.$rgpro['referencia'].'&nbsp;</td>';  
		echo '<td class="detFacL">'.$rgpro['autoriza'].'&nbsp;</td>';  			

		
		echo '<td class="detFacR">$'.number_format($rgpro['saldo'],2,'.',',').'</td>'; 

		echo '<td class="detFacL"> &nbsp;&nbsp;'.$rgpro['dptvta'].'</td>'; 		 
		echo '</tr>';	
	}		
		$granTotpro +=$rgpro['saldo'];  //suma propina individual
		$granTotpro2 +=$rgpro['saldo'];  //suma propina 
			$a ++;
				$numVen ++; 
							
		} //cierra el detalle de pago  PROPINA
//************************************************************************************************************************************////		
//||||||||||||||||||||||||||||||||||||||||||||||||||CANTIDAD NETA DE CADA FORMA DE PAGO |||||||||||||||||||||||||||||||||||||||||||||||//						
		echo "<tr> 
			<td class=\"granTotL\"  colspan=\"4\"> Numero Ventas: $a</td>  						
			<td class=\"granTotR\"></td>  	
			<td class=\"granTotR\">".number_format(($granTot+$granTotpro),2,'.',',')."</td> 						
		</tr>";
			echo "<tr>
			  <td></td>
			  <td colspan=\"5\">&nbsp;&nbsp;</td>
          </tr>";
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||//		
		
	} //cierra el tipo de pago


echo "<tr> 
			<td class=\"estiloSubTotal\" colspan=\"4\"> Numero Ventas Totales: $numVen</td>  						
			<td class=\"estiloSubTotal\"></td>  	
			<td class=\"estiloSubTotal\" align=\"right\">".number_format(($granTotpro2+$granTot2),2,'.',',')."</td> 						
		</tr>";
?>	
    </table>