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

$nivel = $_GET["nivel"];
$nombreuser =$_GET["nombreuser"];
$fechainicio=$_GET["fechainicio"];
$fechatermino=$_GET["fechatermino"];
$sucu=$_GET["sucu"];
//nombre suscursal
	   $query_nom="SELECT * FROM sucu WHERE codsucu='$sucu'";
	   $res_nom=mysql_query($query_nom);
	   $nom_sucu=mysql_result($res_nom,0,"nombre");
	   
$rut=$_GET["rut"];

$sel_cliente="SELECT * FROM articulos  WHERE referencia='$rut' and sucu='$sucu'"; 
   $rs_cliente=mysql_query($sel_cliente);
   $nombrecliente=mysql_result($rs_cliente,0,"descripcion");
   $art=mysql_result($rs_cliente,0,"referencia");
    
	//saldo entrada
	$sel_saldo_entrada="SELECT sum(entrada) AS total FROM cartola_historica where art='$art' AND fecha < '$fechainicio' and sucu='$sucu';"; 
    $rs_saldo_entrada=mysql_query($sel_saldo_entrada);
	$saldo_entrada=mysql_result($rs_saldo_entrada,0,"total");
	
	//saldo salida
	$sel_saldo_salida="SELECT sum(salida) AS total FROM cartola_historica where art='$art' AND fecha < '$fechainicio' and sucu='$sucu';"; 
    $rs_saldo_salida=mysql_query($sel_saldo_salida);
	$saldo_salida=mysql_result($rs_saldo_salida,0,"total");
	
	$total_saldo=$saldo_entrada-$saldo_salida;
         
?>
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
  

    <meta charset="utf-8">
    <title>INVENTARIO</title>
    <meta name="viewport" content="initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no, width = device-width">

    <!--[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>
		
		var cursor;
		if (document.all) {
		// Está utilizando EXPLORER
		cursor='hand';
		} else {
		// Está utilizando MOZILLA/NETSCAPE
		cursor='pointer';
		}
		
		 function imprimir() {
			location.href="../informe.php";	
		 }
		</script>
        <script language="javascript">
function imprimir() {
	window.print();
	window.close();
	
}
</script>
<script language="javascript">
function excel() {
	window.open("rejillaxml.php?fechainicio=<? echo $fechainicio ?>&fechatermino=<? echo $fechatermino ?>&rut=<? echo $rut ?>");
	window.history.back()
	
}
function imprimir() {
	window.open("rejilla_imprime.php?fechainicio=<? echo $fechainicio ?>&fechatermino=<? echo $fechatermino ?>&rut=<? echo $rut ?>&sucu=<? echo $sucu ?>");
	window.history.back()
	
}
function cerrar() {
	window.close();	
}
</script>
</head>
<body onload=inicio()>
<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 >
            <table width="818" border=0 cellpadding=3 cellspacing=0 class="fuente8">					
					  <tr>
						  <td>CARTOLA DE EXITENCIA | SUCURSAL: <? echo $nom_sucu ?></td>
						  
					  </tr>
					  <tr>
					    <td>PRODUCTO: <? echo $nombrecliente ?></td>
		      </tr>
					  <tr>
					    <td>CODIGO DE BUSQUEDA : <? echo $rut ?></td>
		      </tr>
					  <tr>
					    <td>FECHA DESDE : <? echo $fechainicio?> --  HASTA: <? echo $fechatermino?></td>
		      </tr>
					  <tr>
					    <td align="right"><input type="button" id="imprime" name="Imprimir" value="IMPRIMIR" 
                        class="art-button" onClick="imprimir()">
				        <input type="button" id="cerrar" name="cerrar" value="CERRAR" class="art-button" onClick="cerrar()"></td>
				      </tr>
					  <tr>
					    <td> DETALLES DE CARTOLA </td>
      </tr>
                      </table>
                     <table width="975">
					  <tr>
					    <td colspan="5">&nbsp;</td>
</tr>
					  <tr>
					    <td colspan="4" class="fuente8" >&nbsp;</td>
					  </tr>
					  <tr>
					    <td colspan="5"><tr>
					      <td><tr>
					        <td><tr>
					          <td colspan="4">&nbsp;</td>
					        </tr>
<tr>
					          <td>
                             
                                  <? 
								  

$sel_articulos="SELECT * FROM cartola_historica WHERE art='$art' AND fecha BETWEEN '$fechainicio' AND '$fechatermino' 
and sucu='$sucu' ORDER BY fecha ASC  ";
$rs_articulos=mysql_query($sel_articulos);
$contador=0;
$totalfinal=0;
$iva=19;
$numero_articulos=mysql_num_rows($rs_articulos);
       
	   
           ?>
                             
                                <table width="821" border="0">
                                  <tr class="cabeceraTabla2">
                                    <td width="66" bgcolor="#0066FF">FECHA</td>
                                    <td width="108" bgcolor="#0066FF">TIPO DOC</td>
                                    <td width="115" bgcolor="#0066FF">N° DOC</td>
                                    <td width="191" bgcolor="#0066FF">GLOSA</td>
                                    <td width="91" align="right" bgcolor="#0066FF">ENTRADA </td>
                                    <td width="107" align="right" bgcolor="#0066FF">SALIDA</td>
                                    <td width="113" align="right" bgcolor="#0066FF">SALDO </td>
                                  </tr>
                                  
                                  <tr class="cabeceraTabla2">
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td align="left"><h6>Saldo anterior</h6></td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td align="right" class="fuente8"><?
									 if($total_saldo>0)
									{ 
									  $saldo_ant=$total_saldo; echo $saldo_ant; 
									}
									 else{$saldo_ant=0; echo $saldo_ant;
									} ?></td>
                                  </tr>
                                  <? if ($numero_articulos>0) { 
								  while ($contador < mysql_num_rows($rs_articulos)) {
								  	  
								             
								 ?>
                                  <tr class="fuente8">
                                    <td><?  $factura=mysql_result($rs_articulos,$contador,"fecha"); echo $factura;?></td>
                                    <td><?  $doc=mysql_result($rs_articulos,$contador,"tipo_doc");
									 switch ($doc) {
				   //factura electronica
                   case 1:
                         $nombretipo="FACTURA";
					break;
				   //factura manual
				   case 2:
                         $nombretipo="BOLETA";
					break;
				   //guia de despacho electronica 
				   case 3:
				         $nombretipo="GUIA. ELECT";
                   break;
				   //guia de descpacho manual
				   case 4:
				         $nombretipo="GUIA. MANUAL";
                   break;
				   //boleta elcetronica
				   case 5:
				         $nombretipo="BOLETA. ELECT";
                   break;
				   //boleta manual 
				   case 6:
				         $nombretipo="BOLETA. MANUAL";
                   break;
				   //entrada directa
				   case 7:
				         $nombretipo="OTRA. ENT";
                   break;
				   case 8:
				         $nombretipo="OTRA. SAL";
                   break;
				   
            } echo $nombretipo;
			
			
									
									?></td>
                                    <td align="left"><? echo mysql_result($rs_articulos,$contador,"n_doc");?></td>
                                    <td align="left"><? echo mysql_result($rs_articulos,$contador,"glosa");  ?></td>
                                    <td align="right"><? $entradas=mysql_result($rs_articulos,$contador,"entrada"); echo mysql_result($rs_articulos,$contador,"entrada"); ?></td>
                                    <td align="right"><? $salidas=mysql_result($rs_articulos,$contador,"salida"); echo mysql_result($rs_articulos,$contador,"salida");?></td>
                                    <td align="right"><? 
									//suma saldo
									$saldosuma=$saldosuma+$entradas;
									//resta saldo
								    $saldoresta=$saldoresta+$salidas;
									//saldos disponibles
									
									$saldos_disp=$saldosuma-$saldoresta+$saldo_ant; echo $saldos_disp
									  ?></td>
                                  </tr>
                                  
                                  <? $contador++;}?>
                                  <tr class="fuente8">
                                    <td colspan="7"><hr></td>
                                  </tr>
                                  
                                  <tr class="itemImparTabla">
                                    <td colspan="4" align="right" bgcolor="#FFFFFF" class="fuente8">TOTALES</td>
                                    <td align="right" valign="middle" bgcolor="#FFFFFF" class="fuente8"><?
       	$sel_total_ent="SELECT sum(entrada) AS total_entradas FROM cartola_historica WHERE art='$art' AND fecha BETWEEN '$fechainicio' AND '$fechatermino'";
$rs_total_ent=mysql_query($sel_total_ent);
$total_entradas=mysql_result($rs_total_ent,0,"total_entradas");  $total_entradas=$total_entradas+$saldo_ant;echo $total_entradas;                             
									
									
									?></td>
                                    <td align="right" bgcolor="#FFFFFF" class="fuente8" ><?
                                    	$sel_total_sal="SELECT sum(salida) AS total_salidas FROM cartola_historica WHERE art='$art' AND fecha BETWEEN '$fechainicio' AND '$fechatermino' and sucu='$sucu'";
$rs_total_sal=mysql_query($sel_total_sal);
$total_salida=mysql_result($rs_total_sal,0,"total_salidas"); echo $total_salida;
									
									?></td>
                                    <td align="right" bgcolor="#FFFFFF" class="fuente8" ><? $total_final=$total_entradas-$total_salida; echo $total_final; ?> </td>
                                  </tr>
                                  <? }else {?>
                                
                                  <? }?>
                                </table>
    
     </p>
   
     <p align="left">  
     <div id="resultado">
  </div></p>
    </body></html>