<? 
$nivel = $_GET["nivel"];
$nombreuser =$_POST["nombreuser"];
$sucu =$_POST["sucu"];
 

$fecha =$_POST["fecha"];
$hora=date("H:i:s");
$fecha_ft=date('d-m-Y',strtotime($fecha));


require_once('../../conf/connection.php');

//suma de cierre de caja 
$query_cierre="SELECT sum(monto) AS base FROM cierre_caja  WHERE fecha='$fecha'";
$rs_query_cierre=mysql_query($query_cierre);
$base=mysql_result($rs_query_cierre,0,"base");
if($base==""){$base="0";}else{}


//suma de costos 
$query_costo="SELECT sum(costo) AS t_costo FROM detalles_pedido  WHERE fecha='$fecha'";
$rs_query_costo=mysql_query($query_costo);
$costo=mysql_result($rs_query_costo,0,"t_costo");
if($costo==""){$costo="0";}else{}

//suma total de ventas boletas = "1" y faturas="2" 

$query="SELECT sum(valor_total) AS boletas FROM tp_doc  WHERE tipo=1 AND sucu='$sucu' AND fecha='$fecha'";
$rs_query=mysql_query($query);
$boletas=mysql_result($rs_query,0,"boletas");
if($boletas==""){$boletas="0";}else{}

$query="SELECT sum(valor_total) AS facturas FROM tp_doc  WHERE tipo=10 AND sucu='$sucu' AND fecha='$fecha'";
$rs_query=mysql_query($query);
$facturas=mysql_result($rs_query,0,"facturas");
if($facturas==""){$facturas="0";}else{}

$totalventa=$boletas+$facturas;

$query2="SELECT * FROM tp_doc WHERE fecha='$fecha' AND sucu='$sucu'";
$rs_query2=mysql_query($query2);

//SUMA TOTAL POR FORMA DE PAGO

// efectivo=1 | transbank (credito)=2 | transbank(debito)=3 | cuenta_cliente=4

$query="SELECT sum(valor_total) AS efectivo FROM tp_doc  WHERE forma='1' AND sucu='$sucu' AND fecha='$fecha'";
$rs_query=mysql_query($query);
$efectivo=mysql_result($rs_query,0,"efectivo");
if($efectivo==""){$efectivo="0";}else{}

$query="SELECT sum(valor_total) AS credito FROM tp_doc  WHERE forma='2' AND sucu='$sucu' AND fecha='$fecha'";
$rs_query=mysql_query($query);
$credito=mysql_result($rs_query,0,"credito");
if($credito==""){$credito="0";}else{}

$query="SELECT sum(valor_total) AS debito FROM tp_doc  WHERE forma='3' AND sucu='$sucu' AND fecha='$fecha'";
$rs_query=mysql_query($query);
$debito=mysql_result($rs_query,0,"debito");
if($debito==""){$debito="0";}else{}

$query="SELECT sum(valor_total) AS cuenta FROM tp_doc  WHERE forma='4' AND sucu='$sucu' AND fecha='$fecha'";
$rs_query=mysql_query($query);
$cuenta=mysql_result($rs_query,0,"cuenta");
if($cuenta==""){$cuenta="0";}else{}

$totalventa_pago=$efectivo+$debito+$credito+$cuenta;

$totalventa_cierre=$totalventa_pago+$base;

//fin 

$nr_ide2=mysql_num_rows($rs_query2);

if($nr_ide2==0){ 

    ?><script>alert("ERROR: No existen registros, verifique los datos ingresados.");window.history.back(); </script>  <? 

}else{	
 
  $query3="SELECT * FROM cierre_caja WHERE fecha='$fecha' AND sucu='$sucu'";
   $rs_query3=mysql_query($query3);
   $nr_ide3=mysql_num_rows($rs_query3);
   if($nr_ide3==0){ ?><script>alert("ERROR: No Existe monto Previo Registrado en caja, verifique los datos ingresados.");window.history.back(); </script> <?  }else{


?>
<!DOCTYPE html>
<html dir="ltr" lang="en-US"><head>
    <meta charset="utf-8">
    <title>mod_venta</title>
    <meta name="viewport" content="initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no, width = device-width">
    <script type='text/javascript' src='../jquery-1.7.1.min.js'></script>
    <!--[if lt IE 9]><script src="https://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
    <link rel="stylesheet" href="../../style.css" media="screen">
    <!--[if lte IE 7]><link rel="stylesheet" href="style.ie7.css" media="screen" /><![endif]-->
    <link rel="stylesheet" href="../../style.responsive.css" media="all">

    <script src="../../jquery.js"></script>
    <script src="../../script.js"></script>
    <script src="../../script.responsive.js"></script>



<style>.art-content .art-postcontent-0 .layout-item-0 { border-top-width:1px;border-top-style:solid;border-top-color:#B1C9C7;margin-top: 10px;margin-bottom: 10px;  }
.art-content .art-postcontent-0 .layout-item-1 { border-right-style:solid;border-right-width:1px;border-right-color:#B1C9C7; padding-right: 10px;padding-left: 10px;  }
.ie7 .art-post .art-layout-cell {border:none !important; padding:0 !important; }
.ie6 .art-post .art-layout-cell {border:none !important; padding:0 !important; }

</style>
<script>

function cerrar() {
			window.close();
		}
		
</script>  
<script language="javascript">

function imprimir() {
	window.open("rejilla_imprime.php?fecha=<? echo $fecha ?>&sucu=<? echo $sucu ?>&nombreuser=<? echo $nombreuser ?>");
	window.history.back()
	
}
function cerrar() {
	window.close();	
}

function ver(numero){
			miPopup = window.open("ver.php?numero="+ numero +"","residente","width=700,height=380,scrollbars=yes");
			miPopup.focus();
		}
</script>
       

</head>
<body>
<div id="art-main">

<div class="art-sheet clearfix">
            <div class="art-layout-wrapper">
                <div class="art-content-layout">
                    <div class="art-content-layout-row">
                        <div class="art-layout-cell art-content"><article class="art-post art-article">
                                
                                                
                                <div class="art-postcontent art-postcontent-0 clearfix"><div class="art-content-layout">
    <div class="art-content-layout-row">
    <div class="art-layout-cell layout-item-1" style="width: 100%" >
       
        <p>
      
      <h3>INFORME DE CIERRE DE CAJA</h3>
     

      <p><br>
        <br>
        
        Fecha: <? echo $fecha_ft ?> Hora: <? echo $hora ?></p>
      
      <table width="277" border="0" class="table">
  <tr>
    <td width="145" bgcolor="#69BFE0">Monto Efectivo en Caja:</td>
    <td width="122" align="right" bgcolor="#69BFE0"><? echo $base ?></td>
    </tr>
      </table>
      <p>&nbsp;</p>
      <table width="200" border="0" class="table">
        <tr>
    <td bgcolor="#69BFE0">Docuentos</td>
    <td bgcolor="#69BFE0">Total </td>
    </tr>
  <tr>
    <td width="86">Sin Doc:</td>
    <td width="98" align="right"><? echo $facturas ?></td>
  </tr>
  <tr>
    <td>Boletas: </td>
    <td align="right"><? echo $boletas ?></td>
  </tr>
  <tr>
    <td bgcolor="#69BFE0">Total</td>
    <td align="right" bgcolor="#69BFE0"><? echo $totalventa ?></td>
  </tr>
</table><br>

<table width="200" border="0" class="table">
  <tr>
    <td colspan="2" bgcolor="#69BFE0">
    Utilidad
    </td>
    </tr>
  <tr>
    <td bgcolor="#69BFE0">Descripción</td>
    <td bgcolor="#69BFE0">Valor</td>
  </tr>
  <tr>
    <td width="86">Venta:</td>
    <td width="98" align="right"><? echo $totalventa ?></td>
  </tr>
  <tr>
    <td>Cotos: </td>
    <td align="right"><? echo $costo  ?></td>
  </tr>
  <tr>
    <td bgcolor="#69BFE0">Utilidad:</td>
    <td align="right" bgcolor="#69BFE0"><? $utilidad= $totalventa-$costo; echo $utilidad; ?></td>
  </tr>
</table><br>

<table width="200" border="0">
          <tr>
            <td bgcolor="#69BFE0">Tipos de pago</td>
            <td bgcolor="#69BFE0">Total </td>
          </tr>
          <tr>
            <td width="121">Efectivo:</td>
            <td width="69" align="right"><? echo $efectivo ?></td>
          </tr>
          <tr>
            <td>Transbank (Credito):</td>
            <td align="right"><? echo $credito ?></td>
          </tr>
          <tr>
            <td>Transbank (Debito):</td>
            <td align="right"><? echo $debito ?></td>
          </tr>
          <tr>
            <td>Cuenta Cliente: </td>
            <td align="right"><? echo $cuenta ?></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td align="right"><hr></td>
          </tr>
          <tr>
            <td bgcolor="#69BFE0">Total</td>
            <td align="right" bgcolor="#69BFE0"><? echo $totalventa_pago ?></td>
          </tr>
          <tr>
            <td bgcolor="#69BFE0">Total + Efectivo</td>
            <td align="right" bgcolor="#69BFE0"><? echo $totalventa_cierre ?></td>
          </tr>
        </table>

        
         <input type="button" class="art-button" id="imprime" name="Imprimir" value="Guardar + Imprimir" onClick="imprimir()">
       
         <input type="button" class="art-button" id="cerrar" name="cerrar" value="Cerrar ventana" onClick="cerrar()">
         <br>
         <br>
        
        <?
        $buscar='select * from tp_doc where tipo<>3 and sucu like \'%'.$sucu.'%\' and fecha like \'%'.$fecha.'%\' Order by numero';
		   $r=mysql_query($buscar,$conn) or die("Error en el query: ".mysql_error());
		   $n=mysql_num_rows($r);
		    //si hay registros los procesamos
			if($n>0)
			{
			?>
				<table width="383" >
				<tr bgcolor="#69BFE0" >
				<td width="109">Documento</td>
				<td width="62">Tipo doc</td>
				<td width="62">Obs</td>
				<td width="90">Total </td>
				<td width="38">&nbsp;</td>
				</tr>
				<?
				while($row=mysql_fetch_assoc($r))
				{ //Procesamos cada registro como una fila de tabla
					?>
					<tr><?
					//$row["nomCampo"] estos nomCampo son los campos devueltos por el
					//query en la variable $r que procesa mysql_fetch_assoc
					?>
					 <td height="56"><? echo  $row["numero"] ?></td>
					 
					 <td align="left"><? $tipo=$row["tipo"];if($tipo==1){echo "Boleta";}else{echo "Sin doc";} ?></td>
					 <td align="left"><? echo  $row["est"] ?></td>
					 <td align="right"><? echo $row["valor_total"] ?></td>
					 <td align="right">
                     <img src="../../images/modificar.png" width="16" height="16"
                     onClick="ver('<? echo  $row["numero"] ?>')"
                     >
                     </td>
				    </tr>
				<? }?>
				</table><br>
			
			<? } 
			else
			{
				echo "No hubieron coincidencias!";
     
			}}} ?>
        </p>
    </div>
    </div>
</div>
<div class="art-content-layout-br layout-item-0">
</div></div>


</article></div>
                    </div>
                </div>
            </div>

    </div>
    
</div>


</body></html>
