<?php

session_start();
include("DMMYSQLG.php");
require_once("DF80000.php");
if ($_SESSION['butimp']==0){
  $_REQUEST['bot']=$_SESSION['folioimp'];
	$_REQUEST['but']=$_SESSION['butimp'];
}	

//Consulta Datos Empresa
$dt_empres = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT * FROM dxempres where cla_emp = '".$_SESSION['empresa']."'"));		
$cvdm = intval($dt_empres['cve_delmun']);
$cves = intval($dt_empres['cve_estado']);
//Consulta Datos Del/Mun
$sqld_encabezado = mysqli_query($GLOBALS['con'] ,"SELECT * FROM dxdelmun where cve_delmun ='$cvdm'");
@$rowd_encabezado = mysqli_fetch_array($sqld_encabezado);
//Consulta Datos Estado 
$sqle_encabezado = mysqli_query($GLOBALS['con'] ,"SELECT * FROM dxestado where cve_estado = '$cves'");
@$rowe_encabezado = mysqli_fetch_array($sqle_encabezado);
@$resu1=mysqli_query($GLOBALS['con'] ,"select * from dgpemema where fol_ven = '".$_REQUEST['bot']."'");
@$rego1=mysqli_fetch_array($resu1);
$dt_ger_cajerosj=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT * FROM dgger_cajerosj where pedido='".$rego1['fol_ven']."'"));
?>
<html>
<head>
<title><? echo "CORTE DE CAJA";?></title>
<!-- MeadCo Security Manager -->
<!-- ********************************************************************************* -->
<!-- * DO NOT copy this codebase - it can change at any time                         * -->
<!-- * download the (free) ScriptX Resource Kit and store the cab on your own server * -->
<!-- ********************************************************************************* -->
<!-- MeadCo ScriptX -->
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	font-family:Arial, Helvetica, sans-serif;
	font-size:10px;
}
.tit{
font-family:Arial, Helvetica, sans-serif;
font-size:10px;
}
.tot{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
font-weight:bold;
}

.dom{
font-family:Arial, Helvetica, sans-serif;
font-size:10px;
}
td{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
.titOrd{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
font-weight:bolder;
}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><object id=factory style="display:none"
  classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
  codebase="archivos/smsx.caab#Version=6,1,400,50">
</object>
<script>
function printWindow() {
factory.printing.header = "";
factory.printing.footer = "";
factory.printing.portrait = true;
factory.printing.leftMargin = 0;
factory.printing.topMargin = 0;
factory.printing.rightMargin = 0;
factory.printing.bottomMargin = 0;
//factory.printing.copies = 2;
window.print();
}
</script>
 </head> 
 <body onLoad="printWindow()">
<form name="form1" method="post" action="">
  <table  border="0" cellspacing="0">
    <tr>
      <td colspan="3" scope="col" align="center" class="titOrd"><?php echo $dt_empres['nom_emp'];
	  				$_REQUEST['bot'];
					$_GET['bot'];
					$_POST['bot'];
					$_REQUEST['control'];
					$_GET['control'];
					$_POST['control']; ?></td>
    </tr>
    <tr>
      <td colspan="3" class="dom" align="justify"><?php  echo 'CALLE: '.$dt_empres['calle']." ".$dt_empres['no_ext'];?></td>
    </tr>
    <tr>
      <td colspan="3" class="dom" align="justify"><?php echo 'COL. '.$dt_empres['des_col'].", ".$rowd_encabezado['des_delmun']." ".$rowe_encabezado['des_estado'];?></td>
    </tr>
    <tr>       <td colspan="4" class="dom" align="justify"><? echo 'C.P.'.$dt_empres['cod_pos'].' TEL: '.$dt_empres['telefono01'];?></td>
    </tr>
    <tr>
      <td colspan="3"><div align="center"><? echo 'RFC:'.$dt_empres['rfc'];?></div></td>
    </tr>
    <tr>
      <td colspan="3" class="titOrd"><? echo 'FECHA:&nbsp;&nbsp;'.chfecha($dt_empres['fec_alta'],'Y-m-d','d/m/Y').'&nbsp;&nbsp;&nbsp; HORA:&nbsp;'.date('H:i:s');?></td>
    </tr>
    <tr>
	<td colspan="3"  class="titOrd" align="center">CORTE DE CAJA</td>
	</tr>
<?php
 
//formas de pago *************************************************************************************************/////	
	
	$fecIni=chfecha($_GET['bot'],'d/m/Y','Y-m-d');
	$fecFin=chfecha($_GET['but'],'d/m/Y','Y-m-d');
	if($_GET['ven']!='T') {
		 $arrBus[]='dgger_cajerosj.vendedor='.$_GET['ven'].'';		
	}
	if($_GET['pven']!='T') {
  		$arrBus[]=' ptventa.nparte1='.$_GET['pven'].' ';
	}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($GLOBALS['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($GLOBALS['con']);
	$a=0;
	//if (mysqli_num_rows($sql)!=0){
	echo "<tr class='titOrd' align='center'> 
			<td class=\"titOrd\" colspan=\"3\">PAGOS</td>  						
		</tr>";

//	}

	while($reg = mysqli_fetch_array($sql))
	{
///---------------------------------------PAGOS DE FORMA INDIVIDUAL----------------------------------------------------------//		
$fecIni=chfecha($_GET['bot'],'d/m/Y','Y-m-d');
	$fecFin=chfecha($_GET['but'],'d/m/Y','Y-m-d');
	if($_GET['ven']!='T') {
		 $vendedor="dgger_cajerosj.vendedor='".$_GET['ven']."'";		
	}else{
	 $vendedor=' dgger_cajerosj.vendedor!=0 ';
	}
	
	if($_GET['pven']!='T') {
  		$ptvta=' ptventa.nparte1='.$_GET['pven'].' ';
	}else {
		$ptvta=' ptventa.nparte1!=0 ';
    }
@$sql_pg = mysqli_query($GLOBALS['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($GLOBALS['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
		
//////////////***********************************************///
@$sql_pro = mysqli_query($GLOBALS['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($GLOBALS['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  align=\"right\">$".number_format($granTotpro+$granTot,2,'.',',')."&nbsp;&nbsp;&nbsp;&nbsp;</td>  
          </tr>";
			if ($reg['idpag']==3){
		$granCorVen+=$granTotpro+$granTot;
		}
}
	
///------------------------------------------------------------------------------------------------------------------------------------/

//sacar propinas y descuentos*************************************************+
////////////////individual
	$fecIni=chfecha($_GET['bot'],'d/m/Y','Y-m-d');
	$fecFin=chfecha($_GET['but'],'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($_GET['ven']!='T') {
		 $arrBus[]='dgger_cajerosj.vendedor='.$_GET['ven'].'';		
	}
	if($_GET['pven']!='T') {
  		$arrBus[]=' ptventa.nparte1='.$_GET['pven'].' ';
	}else {
		$arrBus[]=' ptventa.nparte1!=0 ';
    }
 	$WHERE="WHERE ".implode(" AND ",$arrBus);

	@$sql = mysqli_query($GLOBALS['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($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"];
		}

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

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

////pagados de forma general 
	@$sql = mysqli_query($GLOBALS['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($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'];
	}
	
	@$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"];	


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

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


?>
		<td colspan="3"  class="titOrd" align="center">PRODUCTOS</td>
	 	<tr class="titOrd">
		  <td>Cantidad</td>
		  <td><div align="center">Productos</div></td>
  		  <td><div align="center">Precio</div></td>
		</tr>
		

<?php

 $_GET['bot'];
 $_GET['but'];
 $_GET['ven'];
 $_GET['pven'];
	$fecIni=chfecha($_GET['bot'],'d/m/Y','Y-m-d');
	$fecFin=chfecha($_GET['but'],'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($_GET['ven']!='T') {
		 $arrBus[]='dgger_cajerosj.vendedor='.$_GET['ven'].'';		
	}
	if($_GET['pven']!='T') {
  		$arrBus[]=' ptventa.nparte1='.$_GET['pven'].' ';
	}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,
			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($GLOBALS['con']);
	 mysqli_num_rows($sql);
while($reg = mysqli_fetch_array($sql)){	
			echo '<tr class="detFacTr">';
			echo '<td class="detFacL">'.$reg["can_ref"].'&nbsp;</td>'; //cantidad
@$al = mysqli_query($GLOBALS['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 align="right">'.number_format(($reg["pre_bas"]*$reg["can_ref"]),2,'.',',').'&nbsp;&nbsp;&nbsp;&nbsp;</td>';  //precio unit
			echo '</tr>';		
			$ttoct+=$reg["pre_bas"]*$reg["can_ref"];	
	
}
	$TOTVTA=$ttoct;
	
		echo "<tr class='typeagre'> 
			<td colspan=\"3\" class=\"titOrd\">TOTALES </td>  						
		</tr>
		<tr>	
			<td colspan=\"2\" class=\"titOrd\">DESCUENTOS</td>  						
			<td class='detFacR'> $".number_format($granDesVen,2,'.',',')."</td>  	
		</tr>
		
		<tr>	
			<td colspan=\"2\" class=\"titOrd\">CORTESIAS</td>  						
			<td class='detFacR'> $".number_format($granCorVen,2,'.',',')."</td>  	
		</tr>
		<tr>	
			<td colspan=\"2\" class=\"titOrd\">PROPINAS</td>  						
			<td class='detFacR'> $".number_format($granPro,2,'.',',')."</td> 						
		</tr>	
		<tr>	
			<td colspan=\"2\" class=\"titOrd\">TOTAL  </td>  						
			<td class=\"titOrd\"> $".number_format($TOTVTA-$granDesVen-$granCorVen+$granPro,2,'.',',')."</td>												
		</tr>	";



		
	
		
?>	
    </table>
</form>
 


</body>
</html>

