<?php
 
session_start();
include("DMMYSQLG.php");
include("DSA1000.php");
include("DF30000.inc");
include("DF80000.php");
include("JQUERY.inc");
$ttoct=0;
$granDesVen=0;
$granCorVen=0;
$granPro=0;
?>
<html>
<head>
<link rel="stylesheet" href="css/estilo1.css" type="text/css"/>
<script type="text/javascript" language="JavaScript1.2" src="DSSCRIPT.js"></script>

<script language="javascript">

$(document).ready(function(){

	$('#timer').hide(0);
    $('#timer').css('display','none');
    var elegido3=new Array();
	$("input[name='con_ven[]']").change(function () {
     var elegido2 = new Array();
     $("input[name='con_ven[]']:checked").each(function () {
      elegido2.push($(this).val());
      elegido3=elegido2;
      if (elegido2 != null) {
       document.getElementById('traspasar').style.display = 'block';
      }else{
       document.getElementById('traspasar').style.display = 'none';
      }
    });
   });



	$('#traspasar').click(function(){
   	$("#dialogo").dialog({
      modal: true,
      title: "Traspasar",
      width: 550,
      minWidth: 400,
      maxWidth: 650,
      show: "fold",
      hide: "scale"
   });
});
	$('#enviar').click(function() { 
	var timeSlide = 1000;
	$('#timer').fadeIn(300);
    $('.box-success, .box-error').slideUp(timeSlide);
	elegido=document.getElementById("cajero").value;
	psw=document.getElementById("pasw").value;

		if (elegido != null && psw != "") {
			
			$.ajax({
                    type: 'POST',
                    url: 'ven_corte_pr_ajax.php',
                    data: { cajero: elegido, pass: psw, ch: elegido3},

                    success:function(msj){
                        if ( msj == 1 ){
                            $('#alertBoxes').html('<br><div class="box-success"></div>');
                            $('.box-success').hide(0).html('Espera un momento…');
                            $('.box-success').slideDown(timeSlide);
                            setTimeout(function(){
                                openpage('ven_corte_PR.php','_self');
                            },(timeSlide + 500));
                        } else{
                            $('#alertBoxes').html('<br><div class="box-error"></div>');
                            $('.box-error').hide(0).html('Lo sentimos, pero los datos son incorrectos');
                            $('.box-error').slideDown(timeSlide);
                        }
                        $('#timer').fadeOut(300);
                    }

                });
			
			

		}else {
							
			alert("por favor ingrese la clave");
			return false;
		}
    

});
});

function abrir2popup(val1,val2) 
{

a= window.open("ven_corte_IM.php?bot=<?php
 echo $_POST['fecini']; ?>&but=<?php
 echo $_POST['fecfin']; ?>&ven=<?php
 echo $_POST['secodven']; ?>&pven=<?php
 echo $_POST['setipvent']; ?>","",
"scrollbars=yes, resizable=no, width=300,height=700, top=85, left=140");
}


</script>
</head>
<body>

<?php
 

$sq_tipven = mysqli_query($con ,"select * from dgtipven");//Datos para combo setipven 
$sq_pvta = mysqli_query($con ,"select * from ptventa");//Datos para combo punto de venta 


?>
<div align="center">
<form name="frm1" id="frm1" method="post" action="">
  <table border="0" align="center" cellpadding="0" cellspacing="0">
    	<tr>
    	  <th colspan="2">CORTE/CUENTAS ABIERTAS</th>
		 </tr>
  	<tr>
     	  <td><span class="Estilo20">Punto de Venta:</span></td>
        	<td>
            	<div align="left">
            	  <select name="setipvent" id="setipvent">
            	    <option value="T">------ TODOS ------</option>
            	    <?php
 
						while ($dt_tippag = mysqli_fetch_array($sq_pvta)){
							if($dt_tippag['nparte1'] == $_POST['setipvent']) 
								echo '<option value="'.$dt_tippag['nparte1'].'" selected>'.$dt_tippag['np_desc1'].'</option>';
							else
								echo '<option value="'.$dt_tippag['nparte1'].'">'.$dt_tippag['np_desc1'].'</option>';
						}
					?>
          	    </select>
       	        </div></td>
   	  </tr>		
      	<tr>
      	  <td><span class="Estilo20">Cajero:</span></td>
      	  <td><select name="secodven" id="secodven">
			<option value="T">---- TODOS ----</option>
            <?php
 
			$sql = mysqli_query($con ,"SELECT * FROM dgcatven order by CONCAT(nom_ven,' ',ap_pat_ven,' ',ap_mat_ven) asc");
			while($reg=mysqli_fetch_array($sql)){
				if($reg['cod_ven'] == $_POST['secodven']){
					echo '<option value="' . $reg['cod_ven'] . '" selected>' . $reg['nom_ven'] . ' ' . $reg['ap_pat_ven'] . ' ' . $reg['ap_mat_ven'] .'</option>';
				}else{
					echo '<option value="' . $reg['cod_ven'] . '">' . $reg['nom_ven'] . ' ' . $reg['ap_pat_ven'] . ' ' . $reg['ap_mat_ven'] . '</option>';
				}
			
			}
			?>
            </select>&nbsp;</td>
   	  </tr>
		 
		 <tr>
      	  <td colspan="2" nowrap>
		    <div align="center" class="estiloFecha">

			  <span class="Estilo16">Del</span> 
                          <?php $fecf=(isset($_POST['fecini']))?$_POST['fecini']:date('d/m/Y');?>
			  <input name="fecini" type="text" class="Estilo3 datefrom" id="fecini" size="15" value="<?=$fecf?>"
                                 <?php
					if(isset($_POST['fecini']))
						echo 'value="'.$_POST['fecini'].'"'; 
				  	else
				    	echo 'value="'.date('d/m/Y').'"'; 
				?>>		      
	      	
	      	  <span class="Estilo16">al </span> 
	         

	          <input name="fecfin" type="text" class="Estilo3 dateto" id="fecfin" size="15" 
			<?php
 
				if(isset($_POST['fecfin']))
			  		echo 'value="'.$_POST['fecfin'].'"'; 
			  	else
			  		echo 'value="'.date('d/m/Y').'"'; 
			?>>
	         
	       
          </div></td>
       	</tr>

     	<tr style="display:none">
     	  <td width="44"><span class="Estilo16">Tipo Venta:</span></td>
        	<td width="191">
            	<div align="left">
            	  <select name="setipven" id="setipven">
            	    <option value="T">---- TODAS ----</option>
            	    <?php
 
						while ($dt_tipven = mysqli_fetch_array($sq_tipven)){
							if($dt_tipven['tip_ven'] == $_POST['setipven']) 
								echo '<option value="'.$dt_tipven['tip_ven'].'" selected>'.$dt_tipven['des_tipven'].'</option>';
							else
								echo '<option value="'.$dt_tipven['tip_ven'].'">'.$dt_tipven['des_tipven'].'</option>';
						}
					?>
          	    </select>
       	        </div></td>
      	</tr>
      	<tr>
      	  <td colspan="2"><div align="center">
       		  <input name="boproc" type="submit" id="boproc" value="Traspaso" onClick="openpage('ven_corte_PR.php','_self')"/>
       		  <input type="button" name="bot4" value="Cuentas Abiertas" onClick="openpage('ven_corte_IMM.php','_self')">
          	
       	  </div></td>
		</tr>
      	<tr style="display:none">
      	  <td colspan="2"><div align="center">
       		  <input name="boproc" type="submit" id="boproc" value="Procesar" onClick="openpage('ven_corte_PR.php','_self')" />
       	  </div></td>
       	</tr>
	</table>

  <table border="1" cellpadding="0" cellspacing="0">
  
 <?php
  if ( isset($_POST["boproc"])){

	$fecIni2=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
	$fecFin2=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d');

	$arrBus2[]="(dgpemema.fec_ven between '$fecIni2' and '$fecFin2') AND dgpemema.cancelado!=1 and cancela!=1 and dgpemema.pagado!='S'";	
	
	if($_POST['setipven']!='T') 
	{
	 	$arrBus2[]="dgpemema.tip_ven='".$_POST['setipven']."'";
	}
	if($_POST['setipvent']!='T') {
  		$arrBus2[]=' ptventa.nparte1='.$_POST['setipvent'].' ';
	}else {
		$arrBus2[]=' ptventa.nparte1!=0 ';
    }
    
    if($_POST['secodven']!='T') 
	{
	 	$arrBus2[]="dgger_cajerosj.cod_ven='".$_POST['secodven']."'";
	}
	
 	$WHERE2="WHERE ".implode(" AND ",$arrBus2);
	@$sql2 = mysqli_query($con ,"
		SELECT 
			dgpemema.fol_ven 
		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
				
		$WHERE2
		ORDER BY 
			dgpemema.con_ven");
		
	echo mysqli_error($con);
if (mysqli_num_rows($sql2)!=0)
{?>
    	<tr>
        	<th colspan="16">
          	<div align="left">
            	</div>			</th>
      	</tr>

	 	<tr class="typeagre">
	 	  <td colspan="11" class="granTotCan"><div align="center" style="background:#FFFFFF">¡¡¡¡¡¡EXISTEN CUENTAS ABIERTAS!!!!!!</div></td>
	 	</tr>
	 	<tr>
	 		<th>No. Ven</th>
	 		<th>Fecha</th>
	 		<th>Mesero</th>
	 		<th>Punto de venta</th>
	 		<th>Mesa</th>
	 		<th>Total</th>
	 	</tr>

	 	<?php 

	 	$fecIni=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
	$fecFin=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d');
	//$arrBus[]="dgpemema.fec_ven='".$dt_empres['fec_alta']."' AND dgpemema.cancelado!=1 and cancela!=1 and pagado!='S'";	
	$arrBus[]=" dgpemema.fec_ven between '$fecIni' and '$fecFin' AND dgpemema.cancelado!=1 and dgpemema.cancela!=1 and dgpemema.pagado!='S'";
	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($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,
			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
		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
			
		$WHERE
		ORDER BY 
			dgpemema.con_ven");
	echo mysqli_error($con);
//	".$dt_empres['fec_alta']."

while($reg = mysqli_fetch_array($sql)){
	@$s = mysqli_query($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($con);
		while($r = mysqli_fetch_array($s)){
		$suma=$r["sumat"];
		}
////---------------------------------	

//------suma de productos solo cortesia
	@$cr = mysqli_query($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($con);
		while($ccr = mysqli_fetch_array($cr)){
		$sumacr=$ccr["sumat"];
		}
////---------------------------------
		@$sq = mysqli_query($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=0 order by dginventp.np_desc1");
		echo mysqli_error($con);
		?>

	<tr>	
	<td><div align="center"><input type="checkbox" name="con_ven[]" value="<?php echo $reg["fol_ven"]?>"><?php echo $reg["fol_ven"]?></div></td>
	<td >
		<div align="center"><?php echo chfecha($reg['fec_ven'],'Y-m-d','d/m/Y')?>
		</div>	
	</td>	
	<td >
		<div align="center"><?php echo $reg["nomVen"]?>
		</div>	
	</td>			
				
	<td ><div align="center"><?php echo $reg['dptvta']	?></div></td>	
	
			
	<td><div align="center"><?php echo $reg['dmesa']?></div></td>
	<td><div align="center"><?php echo round($reg['tot_ven'],2)?></div></td>
	</tr>
	<?php
	while($rg = mysqli_fetch_array($sq)){
		?>
		 <!-- <tr>	
		<td><div align="center"><?php echo $reg["con_ven"]?></div></td>
		<td><div align="center"><?php echo $rg['np_desc1']?></div></td>		
		<td><div align="center"><?php echo  number_format(($rg['pre_bas']*$rg['can_ref']),2,'.',',')?></div></td>		
		<td><div align="center"><?php echo number_format(($rg["total_cd"]-$rg["iva_pre"]),2,'.',',')?></div></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>		
		</tr>  -->
	
		<?php
		$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++;
		}
		?>
		<tr>
		<td><div align="center"><?php echo 'NUMERO DE VENTAS: '.$numVen ?></div></td>
		<td></td>
		
		<td><div align="center"><?php echo number_format($granCorVen,2,'.',',') ?></div></td>
		<td><div align="center"><?php echo number_format($granPro,2,'.',',') ?></div></td>
		<td><div align="center"><?php echo number_format($granTotVen-$granCorVen,2,'.',',')?></div></td>				
		<td><div align="center"><?php echo number_format($granPagVen,2,'.',',') ?></div></td>
		</tr>
		
		
		<tr>
		<td><input style="display: none" id="traspasar" name="Submit" type="button" class="original"  value="Traspasar"/></td>
		
		</tr>

<?php
	
} else{
?>
    	<tr class="typeagre">
        	<td colspan="3">
          	<div align="left">
            	<input type="button" name="bot4" value="Imprimir" onClick="abrir2popup('javascript:document.frm1.fecini.value','javascript:document.frm1.fecfin.value');">
          	</div></td>
			
      	</tr>

<?php

//************************************************************formas de pago *************************************************************************************************/////	
	
		$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 (dgpemema.fec_ven 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,
			dgtippag.ini_pag as inipag,
			dgger_cajerosj.vendedor as vendedor
		FROM 
			dgger_cajerosj
			INNER JOIN dgpemema ON dgpemema.fol_ven=dgger_cajerosj.pedido	
			INNER JOIN dgger_cajerosd ON dgger_cajerosd.n_rec=dgger_cajerosj.n_rec	
			INNER JOIN  dgtippag ON dgtippag.tip_pag=dgger_cajerosd.tip_pag											
			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
		$WHERE
		GROUP BY dgtippag.tip_pag
		ORDER BY 
			dgtippag.desc_pag");
	echo mysqli_error($con);
	$a=0;
	//if (mysqli_num_rows($sql)!=0){
	echo "<tr class='typeagre'> 
			<td colspan=\"3\" align=\"center\">PAGOS</td>  						
		</tr>";
//	}

	while($reg = mysqli_fetch_array($sql))
	{
	
///---------------------------------------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 ';
  }
@$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,	
			sum(dgger_cajerosd.importe) as saldo,	
			dgger_cajerosj.pedido as pedido,	
			sum(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."')
		group by dgtippag.tip_pag
			ORDER BY 
			dgpemema.con_ven");
	echo mysqli_error($con);
	$a=0;
	$b=0;	
	$granTot=0;
	while($rgp = mysqli_fetch_array($sql_pg)){
	//ENCABEZADO DE CADA PAGO 

	

		$numVen ++;
		$a ++;
		$granTot +=($rgp["saldo"]+$rgp["propina"]);  //suma saldo individual
		} //cierra el detalle de pago INDIVIDUAL + PROPINA
		
//////////////***********************************************///

	$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=0 and dgpemema.cancela=0 and dgpemema.pagado='S'";	
	
	if($_POST['secodven']!='T') {
		 $vendedor="dgger_cajerosj.vendedor='".$_POST['secodven']."'";		
	}
	if($_POST['setipven']!='T') 
	{
	 	$vendedor="dgpemema.tip_ven='".$_POST['setipven']."'";
	}
	if($_POST['setipvent']!='T') {
  		$ptventa=' ptventa.nparte1='.$_POST['setipvent'].' ';
	}else {
		$ptventa=' ptventa.nparte1!=0 ';
    }
		

	@$sql_pro = 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_cajerosj.pedido as pedido,	
			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_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 dgpemema.cancela=0 and dgpemema.cancelado=0 AND $vendedor AND $ptvta AND (dgpemema.fec_ven between '".$fecIni."' and '".$fecFin."')
		group by dgger_cajerosj.fol_unp,dgtippag.tip_pag
		ORDER BY 
			dgpemema.con_ven");
	echo mysqli_error($con);
	$granTotpro=0;
	while($rgpro = mysqli_fetch_array($sql_pro)){
		
	$numVen ++;
		$a ++;
		
	$granTotpro +=$rgpro["saldo"];  //suma saldo individual		
		}
		
			//ENCABEZADO DE CADA PAGO 
echo "<tr>
			  <td class=\"estiloDet\">".$reg['inipag']."</td>
			  <td class=\"estiloDet\">".$reg['descpag']."</td>
			  <td class=\"detFacR\">$".number_format($granTotpro+$granTot,2,'.',',')."</td>  
          </tr>";
		if ($reg['idpag']==3){
		$granCorVen+=$granTotpro+$granTot;
		}

		
		}
	
///------------------------------------------------------------------------------------------------------------------------------------/

//sacar propinas y descuentos*************************************************+
////////////////individual
$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);

	@$sql = mysqli_query($con ,"
		SELECT 
			dgpemema.fol_ven,
			dgpemema.por_des as descuento,
			fpropina.tip as propina
		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($con);
	while($reg = mysqli_fetch_array($sql)){
//------suma de productos
	@$s = mysqli_query($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($con);
		while($r = mysqli_fetch_array($s)){
		$suma=$r["sumat"];
		}

			$desc=$suma*($reg['descuento']/100);

		$granDesVen +=$desc;  //descuento
		$granPro    +=($reg["propina"]);  //propina		
////---------------------------------
}

////pagados de forma general 
	@$sql = mysqli_query($con ,"
		SELECT 
			dgpemema.fol_ven,
			sum(dgpemema.por_des) as descuentog,
			dgpemema.por_des as descuento,
			fpropina.tip as propina,
			sum(dgger_cajerosd.importe) as impdg,
			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($con);

	while($reg = mysqli_fetch_array($sql)){
	//------suma de productos
	@$s=mysqli_query($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($con ,"select sum(pre_bas*can_ref) as summ from dgpememd where cons_ven = '".$r['fol_ven']."' and cancela=0 "));
	 $sumare+=$resu3['summ'];
	}
	
	@$cr2=mysqli_query($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($con);
	$ccr2= mysqli_fetch_array($cr2);
	$sumacr2=$ccr2["sumat"];	


	$a1=($sumare+$reg['propina'])-$sumacr2;
	$porDes=round($a1/$sumare*100,4);
	$dsc=$a1;
			///--------------------rep cancelados----------
		$granDesVen +=$dsc;  //descuento
		$granPro    +=($reg["propina"]);  //propina		

}
//******************************************

?>
   <tr style="display:none">
    	  <th colspan="16" align="justify"><?php
 if ($_POST["setipvent"]=="T"){
					echo "TIP. VTA.: ";
					echo "TODAS"; 
					} else 
					{
					@$bus = mysqli_query($con ,"SELECT * FROM ptventa WHERE nparte1= '".$_POST['setipvent']."'");
					$re = mysqli_fetch_array($bus);
					echo "TIP. VTA.: ";
					echo $re['np_desc1'];
					}			
				 ?></th>
  	  </tr>
    	<tr style="display:none">
    	  <th colspan="16"><?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>
	  <tr>
	  <td colspan="3">
			<div align="center">
            	PRODUCTOS
          	</div></td>
	  </tr>	
	 	<tr class="typeagre">
		  <td>Cantidad</td>
		  <td><div align="center">Productos</div></td>
  		  <td><div align="center">Precio</div></td>
		</tr>
		

<?php

	$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=0 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($con ,"
		SELECT 
			dgpemema.fol_ven,
			dgpememd.nparte1,
			sum(dgpememd.can_ref) as can_ref,
			dgpememd.pre_bas,
			dgger_cajerosj.fol_unp as folp
		FROM 
			dgpememd
			LEFT join dgpemema on dgpemema.fol_ven=dgpememd.cons_ven
			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
			
		$WHERE and dgpememd.cancela=0
		GROUP BY dgpememd.nparte1 
		ORDER BY 
			dgpemema.con_ven");
	echo mysqli_error($con);
	 mysqli_num_rows($sql);
while($reg = mysqli_fetch_array($sql)){	
////--------------------------------- PRODUCTOS 
			echo '<tr class="detFacTr">';
//			echo '<td class="detFacL">'.$reg["con_ven"].'&nbsp;</td>'; //cantidad
			echo '<td class="detFacL">'.$reg["can_ref"].'&nbsp;</td>'; //cantidad
		@$al = mysqli_query($con ,"SELECT * from dginventp where nparte1='".$reg['nparte1']."'"); //productos
		$al1 = mysqli_fetch_array($al);
			echo '<td class="detFacL">'.$al1['np_desc1'].'&nbsp;</td>';  //desc alim
			echo '<td class="detFacR">'.number_format(($reg["pre_bas"]*$reg["can_ref"]),2,'.',',').'</td>';  //precio unit
			echo '</tr>';		
			$ttoct+=$reg["pre_bas"]*$reg["can_ref"];	
	
}
	$TOTVTA=$ttoct;

	echo "<tr class='typeagre'> 
			<td colspan=\"3\">TOTALES </td>  						
		</tr>
		<tr>	
			<td colspan=\"2\">DESCUENTOS</td>  						
			<td class='detFacR'> $".number_format($granDesVen,2,'.',',')."</td>  	
		</tr>
		
		<tr>	
			<td colspan=\"2\">CORTESIAS</td>  						
			<td class='detFacR'> $".number_format($granCorVen,2,'.',',')."</td>  	
		</tr>
		<tr>	
			<td colspan=\"2\">PROPINAS</td>  						
			<td class='detFacR'> $".number_format($granPro,2,'.',',')."</td> 						
		</tr>	
		<tr>	
			<td class=\"granTotR\" colspan=\"2\">TOTAL  </td>  						
			<td class=\"granTotR\"> $".number_format($TOTVTA-$granDesVen-$granCorVen+$granPro,2,'.',',')."</td>												
		</tr>	";
		


}
		}
	
		
?>	

    </table>
    
  </form>
  <div style="display: none;" id="dialogo">
   			
			<div id="alertBoxes"></div>
			<br>
			Cajero:
			<br>
   			<select name="cajero" id="cajero">
            <?php
 
			$sql = mysqli_query($con ,"SELECT * FROM dgcatven order by CONCAT(nom_ven,' ',ap_pat_ven,' ',ap_mat_ven) asc");
			while($reg=mysqli_fetch_array($sql)){
				if($reg['cod_ven'] == $_POST['cajero']){
					echo '<option value="' . $reg['cod_ven'] . '" selected>' . $reg['nom_ven'] . ' ' . $reg['ap_pat_ven'] . ' ' . $reg['ap_mat_ven'] .'</option>';
				}else{
					echo '<option value="' . $reg['cod_ven'] . '">' . $reg['nom_ven'] . ' ' . $reg['ap_pat_ven'] . ' ' . $reg['ap_mat_ven'] . '</option>';
				}
			
			}
			?>
            </select>
            <br>
            Clave<br>
            <input type="text" id="pasw" name="pasw" value=""><br><br>
			<span class="timer" id="timer"></span><input type="submit" name="enviar" id="enviar" value="Traspasar"   class="bot_RjR">
			
		</div>
</div>
</body>
</html>
