<?php
session_start();
include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
include("phpmnu_fun.php");
if(strlen($_SESSION['SYS_USER_ID'])==0){JsDir("login.php");	return;}
//
$StrHWND=$_SESSION['SYS_HW'];
$StrGT="";$LStrMsg="";$LStrFoc="";$LStrMM="";
$LStrPag=V($_POST['TxtPag']);
//
include("compras_fun.php");
//
$VP_RegXPag=10;
$LStrTR=gn("count(*)","temp_compras","temc_index>0 AND cod_temc='$StrHWND'");
$LStrPag=PagGet($LStrPag,$StrCmd,$LStrTR,$VP_RegXPag);	
$TI=0;
//
$IdUsu=vbval(V($_SESSION['SYS_USER_ID']));
$AT=gs("at","usuarios","id_usuario=$IdUsu");
?>

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Compras</title>
<!--INI_SCRIPT------------------------------------------------------------------------------>
<?php AddJs(); ?>
<script type="text/javascript" src="ja/compraz.js"></script>
<script type="text/javascript" src="ja/funciones.js"></script>
<!--FIN_SCRIPT------------------------------------->
<link rel="stylesheet" type="text/css" href="css/estiloss.css"  />
<link rel="stylesheet" type="text/css" href="css/basic.css"  />
<link rel="stylesheet" type="text/css" href="css/mnu.css"  />

<style type="text/css">
.tableFixHead          { overflow-y: auto; }
.tableFixHead thead th { position: sticky; top: 0; }
table  {border-collapse:collapse;width:100%;}
th, td {padding: 3px 3px;}
th     {background:#666;}


.cF {height:600px;width:1250px;position:relative;top:0px}
.mM {height:34px;width:1250px;position:relative;top:0px;background-color:#09C; z-index:500;}
</style>
<script>
$(document).ready(function(){
   Inicializar();
});
//----------------------------------------------------------
var FOCO=0;
var myVar=window.setInterval(function(){myTimer()},500);

function myTimer(){
	t=GetHora();
	tt=document.getElementById("TxtHora").value;
	if(tt>0 && document.getElementById("ChN_BAR").checked==false){	
		if(t-tt>=1){
			document.getElementById("TxtHora").value=0;
			fGetTabla();		
		}
	}
}


function myStopFunction(){
	clearInterval(myVar);
}

$(document).keydown(function(e){
	var AT=gv("TxtAT");
	
	if(e.which==71 && e.altKey){		
		if(AT=="1"){	
		document.getElementById("CmdG").click();
		return;
		}
	}
	
	if(e.which==78 && e.altKey){
		if(AT=="1"){
		document.getElementById("CmdC").click();
		return;
		}
	}
	
	if(e.which==75 && e.altKey){
		if(AT=="1"){	
		document.getElementById("CmdE").click();
		return;
		}
	}

	if(e.which==66 && e.altKey){
		if(AT=="1"){	
		document.getElementById("CmdB").click();
		return;
		}
	}

	if(e.which==77 && e.altKey){
		if(AT=="1"){
		document.getElementById("CmdZ").click();
		return;
		}
	}
  
});

function cCod(e){	
	var k;
	k=(document.all) ? e.keyCode :e.which;
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	if(k==40){
		if(DivDatos.style.visibility!="hidden"){
			document.getElementById("z1").focus();
			event.preventDefault();
			
		}
		return true;
	}
	if(k==13){
		var Cod=gv("TxtCod");
		
		if(Cod==""){
			jF("TxtDes");
			return;
		}
		
		if(k==13){
			GetCod(Cod);
		}
	}
}

function GetCod(Cod){	
	svv("Pro_TxtId,TxtDes,TxtPre,TxtCant,TxtImp");
	svv("Pro_TxtCod,Pro_TxtDes");
	
	R=AX("Z_COMPRAS",Cod);

	S=R.split("|");
	IdP=S[0];
	Cod=S[1];
	Des=S[2];
	Pre=S[3];
	//
	if(jL(IdP)>0){
		sv("Pro_TxtId",IdP);
		sv("TxtCod",Cod);
		sv("TxtDes",Des);
		sv("TxtPre",Pre);
		sv("TxtCant","1");
		//	
		sv("Pro_TxtCod",Cod);
		sv("Pro_TxtDes",Des);
		//
		if(document.getElementById("ChN_AUT").checked==true){
			fAgregar();
		}else{
			jF("TxtPre");
		}
	}else{
		//alert("Codigo del producto no existe");
		jF("TxtDes");	
	}
}

function cCodUP(e){	
	var k;
	k=(document.all) ? e.keyCode :e.which;	
	if(k==40 || k==27 || k==13){
		
	}else{
		sv('TxtHora',GetHora());
	}
}
</script>

</head>

<body  <?php echo $LStrMsg;?>  >
<div align="center">

<div class="mM" align="left" style="font-size:12px">
<?php
//$HTM=file_get_contents("http://inversionessounduniversal.com/ventas/phpmnu.php?USER_ID=$IdUsu");
echo GEN_MENU($IdUsu);
?>
</div>


<div Id="DivFRM"  class="cF" style="background-color:#FFFFFF;" >
<form id="Frm" name="Frm" method="post" action="compras.php" autocomplete="off" style="margin:0px; padding:0px; display:inline;" onSubmit="return false;">

<!--TITULO-------------------------------->
<div id="DivT" class="dT" >
<img src="img/compra.png" width="54" height="53" class="iT" />
<div class="tT">COMPRAS</div>
</div>


<!--TEXTOS-------------------------------->
<div class="s1" style="left: 35px; top: 138px; width: 79px;">ALMACEN *</div>
<select name="CboAlm" id="CboAlm" 
class="lt" style="left: 35px; top: 157px; width: 256px" 
tabindex="<?php echo ++$TI;?>">
<?php 
	echo ListALM($StrAlm);
?>
</select>

<div class="s1" style="left: 303px; top: 123px; width: 54px;">FECHA *</div>
<div class="s1" style="left: 303px; top: 138px; width: 71px;">(dd-mm-yy)</div>
<input name="TxtFec"  type="text" id="TxtFec"
class="fe tcal" style="left: 301px; top: 157px; width: 91px"
tabindex="<?php echo ++$TI;?>"
value="<?php echo $StrFec;?>" />

<div class="s1" style="left: 403px; top: 138px; width: 109px;">COMPROBANTE *</div>
<select name="CboCom" id="CboCom" 
class="lt" style="left: 401px; top: 157px; width: 150px" 
tabindex="<?php echo ++$TI;?>">
<?php 
	echo FCBO_("com_nombre",
			   "comprobantes",
			   "id_comprobante>0 AND com_nombre IN ('FACTURA','BOLETA','NOTA DE VENTA','GUIA','OTROS')",$StrCom,"","S");
?>
</select>

<div class="s1" style="left: 573px; top: 138px; width: 76px;">NUMERO *</div>
<input name="TxtNum"  type="text" id="TxtNum"
class="lt" style="left: 571px; top: 157px; width: 128px" 
tabindex="<?php echo ++$TI;?>" maxlength="15"
value="<?php echo $StrNum;?>" />

<div class="s1" style="left: 705px; top: 138px; width: 72px;">MONEDA *</div>
<select name="CboMon" id="CboMon" 
class="lt" style="left: 705px; top: 157px; width: 105px" 
tabindex="<?php echo ++$TI;?>">
<?php echo FCBOO_("mon_nombre","monedas","id_moneda<>'X'",$StrMon,"N","N","mon_nombre DESC")?> 

</select>

<div class="s1" style="left: 823px; top: 138px; width: 72px;">T. CAMBIO</div>
<input name="TxtTC"  type="text" id="TxtTC"
class="rod" style="left: 822px; top: 157px; width: 65px;" 
tabindex="<?php echo ++$TI;?>"
value="<?php echo $StrTC;?>" />

<input name="FPro" type="hidden" id="FPro" onClick="fProveedores();" />

<div class="s1" onClick="InsPro()" 
style="left: 896px; top: 138px; width: 111px; cursor: pointer; color: #00F">PROVEEDOR</div>
<select name="CboPro" id="CboPro" 
class="lt" style="left: 896px; top: 157px; width: 321px" onChange="fGetRuc(this.value,'TxtRuc');" 
tabindex="<?php echo ++$TI;?>">
<?php echo FCBO_("NOMBRE","vproveedores","ID>0",$StrPro,"","S")?> 
</select>

<div class="s1" style="left: 35px; top: 188px; width: 100px;">RUC	</div>
<input name="TxtRuc"  type="text" id="TxtRuc"
class="nei" style="left: 35px; top: 208px; width: 144px" 
onBlur="fGetProveedor(this.value,'CboPro');" maxlength="11"
tabindex="<?php echo ++$TI;?>"
value="<?php echo $StrRuc;?>" />

<?php
if(strlen($StrTdp)==0)$StrTdp="CONTADO";
?>
<div class="s1" style="left: 189px; top: 188px; width: 97px;">TIPO DE PAGO *</div>
<select name="CboTdp" id="CboTdp" 
class="lt" style="left: 188px; top: 208px; width: 105px"
tabindex="<?php echo ++$TI;?>">
<?php echo FCBO_("tipp_nombre","tipos_pagos","",$StrTdp,"","S")?> 
</select>

<!--CHECKS-------------------------------->
<div class="s1" style="left: 328px; top: 213px; width: 230px;" >
<label for="ChN_INC">EL PRECIO NO INCLUYE IGV</label></div>
<input name="ChN_INC" type="checkbox" id="ChN_INC" 
class="lt" style="left: 308px; top: 210px;"
value="1"  onclick="CalcularIMPORTE();"
<?php if($StrN_INC=="1"){echo "checked=\"checked\"";}?> 
/>

<div class="s1" style="left: 54px; top: 248px; width: 137px;">
<label for="ChN_BAR">CODIGO DE BARRAS</label></div>
<input name="ChN_BAR" type="checkbox" id="ChN_BAR" 
class="lt" style="left:35px; top: 245px;"
value="1"   onchange="ChN_BAR_OnChange();"
<?php if($StrN_BAR=="1"){echo "checked=\"checked\"";}?>
/>

<div class="s1" style="left: 227px; top: 248px; width: 156px;">
<label for="ChN_AUT" >AUTO AGREGAR</label></div>
<input name="ChN_AUT" type="checkbox" id="ChN_AUT" 
class="lt" style="left: 206px; top: 245px;" value="1"  onchange="ChN_BAR_OnChange();"
<?php if($StrN_AUT=="1"){echo "checked=\"checked\"";}?>>

<!----------------------------------------------->
<div class="s1" style="left: 35px; top: 275px; width: 54px;">CODIGO</div>
<input name="TxtCod"  type="text" id="TxtCod"
style="left: 35px; top: 296px; width:124px"
tabindex="<?php echo ++$TI;?>" 
onKeyDown="return cCod(event);" 
onKeyUp="return cCodUP(event);" 
onFocus="FOCO=0;"
value="<?php echo $StrCod;?>" />

<div class="s1" style="left: 167px; top: 275px; width: 96px;">DESCRIPCION</div>
<input name="TxtDes"  type="text" id="TxtDes"
class="lt" style="left: 167px; top: 296px; width: 705px" 
value="<?php echo $StrDes;?>" 
onKeyDown="return cDes(event);" 
onKeyUp="return cDesUP(event);" 
onFocus="FOCO=1;"
tabindex="<?php echo ++$TI;?>"/>

<div class="s1" style="left: 881px; top: 275px; width: 54px;">PRECIO</div>
<input name="TxtPre"  type="text" id="TxtPre"
class="ndd" style="left: 881px; top: 296px; width: 60px;" 
value="<?php echo $StrPre;?>" onKeyUp="fImporte();"
onfocus="selecciona_value(this);"
tabindex="<?php echo ++$TI;?>"/>

<div class="s1" style="left: 949px; top: 275px; width: 38px;">CANTIDAD</div>
<input name="TxtCant"  type="text" id="TxtCant"
class="ned" style="left: 950px; top: 296px; width: 61px;"
value="<?php echo $StrCant;?>" onKeyUp="fImporte();"
onfocus="selecciona_value(this);"
tabindex="<?php echo ++$TI;?>"/>

<div class="s1" style="left: 1021px; top: 275px; width: 54px;">IMPORTE</div>
<input name="TxtImp"  type="text" id="TxtImp"
class="rod" style="left: 1020px; top: 296px; width: 89px;"
value="<?php echo $StrImp;?>"/>

<input name="CmdAdd" type="button" id="CmdAdd" 
value="Agregar" style="position: absolute; left: 1117px; top: 294px; width: 62px; height: 25px;"
onClick="fAgregar();" 
tabindex="<?php echo ++$TI;?>"/>

<input name="CmdCls" type="button" id="CmdCls" 
value="X" style="position: absolute; left: 1185px; top: 294px; width: 32px; height: 25px;"
onClick="fCls();" 
tabindex="<?php echo ++$TI;?>"/>

<!--BOTONES-------------------------------->
<div class="s1" style="left: 35px; top: 85px; width: 672px; height: 26px;">
<input name="CmdB" type="button" id="CmdG" class="btm"
onclick="fGuardar();" 
<?php
if(strlen($StrId)==0){
	echo "value=\"Guardar\"";
}else{			
	echo "value=\"Modificar\"";

	$StrAnu=gs("anulado","compras","id_compra=$StrId");
	if($StrAnu=="S"){
	echo " disabled=\"disabled\"";
	}
}
?>
tabindex="<?php echo ++$TI;?>" />

<input name="CmdB" type="button" id="CmdC"  class="btm"
value="Nuevo" onClick="fCancelarPHP();"  
tabindex="<?php echo ++$TI;?>" />

<input name="CmdB" type="button" id="CmdA" class="btm"
value="Anular" onClick="fAnular();"
<?php
if(strlen($StrId)==0){
	echo "disabled=\"disabled\"";
}else{
	$StrAnu=gs("anulado","compras","id_compra=$StrId");
	if($StrAnu=="S"){
	echo "disabled=\"disabled\"";
	}
}
?>
tabindex="<?php echo ++$TI;?>" />

<input name="CmdB" type="button" id="CmdE" class="btm"
value="Eliminar" onClick="fEliminar();"
<?php
if(strlen($StrId)==0){
echo "disabled=\"disabled\"";
}else{
$StrAnu=gs("anulado","compras","id_compra=$StrId");
if($StrAnu=="S"){
echo "disabled=\"disabled\"";
}
}
?>
tabindex="<?php echo ++$TI;?>" />

<input name="CmdB" type="button" id="CmdB" class="btm"
value="Buscar" onClick="fBuscar('compras_buscar.php');"  
tabindex="<?php echo ++$TI;?>" />

<input name="CmdB" type="button" id="CmdB" class="btm"
value="Buscar.Det" onClick="fBuscar('compras_buscar_det.php');"  
tabindex="<?php echo ++$TI;?>" />
</div>


<!--GRIDD-------------------------------->
<div class="tableFixHead" style="position: absolute; left: 35px; top: 327px; width: 1180px; height: 223px; overflow: auto; border:solid 1px #A9A9A9">
<table width="100%" style="font-family:Arial, Helvetica, sans-serif; font-size:11px">
<thead>

<tr>
<th width="2%">#</th>
<th width="9%">CODIGO</th>
<th width="49%">DESCRIPCION</th>
<th width="18%">MARCA</th>
<th width="6%">PRECIO</th>
<th width="7%">CANT</th>
<th width="8%">IMPORTE</th>
<th width="3%">&nbsp;</th>
</tr>

</thead>
<tbody>

<?php
	$ZX=0;
	$s="SELECT * FROM temp_compras WHERE cod_temc='$StrHWND' ORDER BY temc_index";
	$result = grs($s);
	$X_IMPORTE=0;
	$X_ST=0;
	$X_IG=0;
	$X_TO=0;	
	while ($row = $result->fetch_row()){
		$ZX++;
		$CSS="";
		if($ZX%2==0)$CSS=" style=\"background-color:#CCC\" ";
		//
		$X_ID=$row[1];
		$X_CO=VV(gs("COD","vproductos","ID=$X_ID"));
		$X_DE=VV(gs("DESCRIPCION","vproductos","ID=$X_ID"));
		$X_MAR=VV(gs("MARCA","vproductos","ID=$X_ID"));
		$X_CA=$row[2];	
		$X_PR=NF($row[3],2);	
		$X_IM=$row[4];	
		$X_IMPORTE=$X_IMPORTE+$X_IM;
		$X_IM=NF($X_IM,2);	

		?>
		<tr <?php echo $CSS;?> >
		<!----------------------------------------------->
        <td ><?php echo $ZX;?></td>
		<td ><?php echo $X_CO;?></td>
		<td ><?php echo $X_DE;?></td>
		<td ><?php echo $X_MAR;?></td>
        <td style="text-align:right"><?php echo $X_PR;?></td>
		<td style="text-align:right"><?php echo $X_CA;?></td>		
		<td style="text-align:right"><?php echo $X_IM;?></td>
		<!----------------------------------------------->
		<td align="center">
		<a href="javascript:void(0)" onClick="fKiller(<?php echo $row[5];?>);">
		<img src="img/eliminar.png"  border="0"/></a></td>
		</tr>
		<?php	
	}	
	
	if($StrN_INC=="1"){
		$X_ST=$X_IMPORTE;
		$X_IG=round($X_ST * 0.18,2);
		$X_TO=$X_ST + $X_IG;		
	}else{
		$X_UNO=$X_IMPORTE / 118;
		$X_IG=round($X_UNO * 18,2);
		$X_ST=$X_IMPORTE - $X_IG;
		$X_TO=$X_ST + $X_IG;	
}
?>
</tbody>
</table>
</div>

<div class="s1" style="left: 807px; top: 562px; width: 75px;">
<b>SUB TOTAL</b></div>
<input name="T_ST" type="text" id="T_ST" 
class="rod" style="left: 886px; top: 562px; width:75px; height:25px; border:none; padding-right:5px"
value="<?php echo number_format($X_ST,2,'.','');?>" />

<div class="s1" style="left: 974px; top: 562px; width: 28px;">
<b>IGV</b></div>
<input name="T_IG" type="text" id="T_IG" 
class="rod" style="left: 1004px; top: 562px; width:70px; height:25px;border:none; padding-right:5px"
value="<?php echo number_format($X_IG,2,'.','');?>" />

<div class="s1" style="left: 1088px; top: 562px; width: 48px;">
<b>TOTAL</b></div>
<input name="T_TO" type="text" id="T_TO" 
class="rod" style="left: 1135px; top: 562px; width: 80px; height:25px; border:none; padding-right:5px"
value="<?php echo number_format($X_TO,2,'.','');?>" />

<style type="text/css">
.gr1{
	visibility: hidden; 
	overflow-x: hidden; 
	overflow-y: scroll; 
	color: #0FF; 
	background-color: #FFFFFF; 
	layer-background-color: #CCCCCC; 
	border: 1px none #000000;
}
.gr2{
	visibility:hidden; 	
	color: #0FF; 
	overflow-y: scroll; 
	background-color: #CCCCCC; 
	layer-background-color: #CCCCCC; 
	border: 1px none #000000;	
}
.gr3{
	padding-left:15px; 	
	background-color: #FFCC00; 
	layer-background-color: #FFCC00; 
	border: 1px none #000000;
	padding-top:2px;
	border-radius: 0.25rem;
	
}
</style>

<div id="TAB_AJAX" class="datagrid gr1" 
style="height: 202px; width: 1180px; position: absolute; left: 36px; top: 341px;display::none;" align="left"></div>

<div id="TAB_CAPTION" class="datagrid gr2" style=" display::none; height:20px; width:1180px; position:absolute; left: 36px; top: 327px;" align="left">
    <table width="1080" border="1" cellpadding="0" cellspacing="0" bgcolor="#d4e3e5" >   
    <tr height="10 px">
    <td width="128" bgcolor="#CCCCCC">CODIGO</td>
    <td width="142" bgcolor="#CCCCCC">LINEA</td>
    <td width="555" bgcolor="#CCCCCC">DESCRIPCION</td>
    <td width="56" bgcolor="#CCCCCC">STOCK</td>
    <td width="56" align="right" bgcolor="#CCCCCC">PC.SOL</td>
    <td width="56" align="right" bgcolor="#CCCCCC">PC.DOL</td>
    <td width="40" align="right" bgcolor="#CCCCCC">&nbsp;</td>
    </tr> 
    <div style=" position:inherit; top:0px; left:1114px"><input name="CmdClose" type="button" value="Cerrar" onClick="CerrarDIV();" style="background-color:#F90" /></div>
    </table>
  
</div>

<div class="s1 gr3" 
style="position: absolute; left: 264px; top: 274px; width: 126px; height:17px;">
<a target="_blank" href="productos.php">NUEVO PRODUCTO</a></div>

<!--INI_OCULTOS-------------------------------->
<?php include("ocultos.php");?>
<div>
<input name="TxtHora" type="hidden" id="TxtHora" value="0" />
<input name="TxtRuc_" type="hidden" id="TxtRuc_"  />       
</div>
    
<div id="DivDatosProd">
<input name="Pro_TxtId" type="hidden" id="Pro_TxtId" value=""/>
<input name="Pro_TxtDes" type="hidden" id="Pro_TxtDes" value=""/>
<input name="Pro_TxtPre" type="hidden" id="Pro_TxtPre" value=""/>
<input name="Pro_TxtCod" type="hidden" id="Pro_TxtCod" value=""/>
<input name="Pro_TxtMarca" type="hidden" id="Pro_TxtMarca" value=""/>
<input name="T_Importe" type="hidden" id="T_Importe" value=""/>
</div>
<!--FIN_OCULTOS-------------------------------->
	
</form>
</div>
</div>
</body>
</html>

