<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Productos</title>
<?php
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;}
//++++++
$HW=V($_SESSION['SYS_HW']);
$IdUsu=vbval(V($_SESSION['SYS_USER_ID']));
//++++++++++++++
$StrGT="";
$SYS_MSG="";
$SYS_FOC="";
$OK=1;
//++++++++++++++
$StrId=V($_POST['TxtId']);
$StrCmd=V($_POST['TxtAc']);
$LStrPag=V($_POST['TxtPag']);
//++
include("ges_productos_fun.php");
//+++
$VP_RegXPag=50;
$LStrTR=gn("count(*)","vis_ges_productos","ID>0");
$LStrPag=PagGet($LStrPag,$StrCmd,$LStrTR,$VP_RegXPag);	
$TI=0;
?>

<!--INI_SCRIPT------------------------------------------------------------------------------>
<?php AddJs(); ?>
<script type="text/javascript" src="ja/funciones.js"></script>
<script type="text/javascript" src="ja/ges_productos5.js"></script>
<script src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.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">
.cF {height:700px;width:1250px;position:relative;top:0px}
.mM {height:34px;width:1250px;position:relative;top:0px;background-color:#09C; z-index:500;}

.tableFixHead          { overflow-y: auto; overflow:auto; border:solid 1px #363636;position: absolute; }
.tableFixHead thead th { position: sticky; top: 0; }
table  {border-collapse:collapse;width:100%;}
th, td {padding: 3px 3px;}
th     {background:#CCC}

.tzt{
	background-color:transparent; width:100%; font-size:11px; position:inherit!Important; border:none;
}
</style>
<script>
$(document).ready(function(){  
   Inicializar();
   //
   SYS_LOAD();
});


function exportarExcel() {
    const tabla = document.getElementById("TabRef");
    const filas = Array.from(tabla.rows);
    const datos = [];

    filas.forEach((fila, indexFila) => {
        const celdas = Array.from(fila.cells);
        const totalCols = celdas.length;

        // Excluir primera, penúltima y última columna
        const dataFiltrada = celdas
            .filter((_, idx) => idx !== 0 && idx !== totalCols - 1 && idx !== totalCols - 2)
            .map(celda => {
                // Si tiene textarea o input, obtener su valor
                const input = celda.querySelector('textarea, input');
                return input ? input.value : celda.textContent.trim();
            });

        datos.push(dataFiltrada);
    });

    const hoja = XLSX.utils.aoa_to_sheet(datos);
    const libro = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(libro, hoja, "Hoja1");
    XLSX.writeFile(libro, "referencias.xlsx");
}

</script>
</head>

<body >
<div align="center">

<div class="mM" align="left" style="font-size:12px">
<?php
echo GEN_MENU($IdUsu);
?>
</div>

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

<!--INI_TITULO--------------------------------->
<div id="DivT" class="dT" >
<img src="png/ges_prod.png"  class="iT" />
<div class="tT">PRODUCTOS</div>
</div>
<!--FIN_TITULO--------------------------------->

<div class="s1" style="left:35px; top: 105px; width: 170px;">
CODIGO / MODEL NUMBER</div>
<input name="Cod"  type="text" id="Cod"
class="lt" style="left: 35px; top: 125px; width: 158px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Cod;?>'/>


<div class="s1" style="left:202px; top: 105px; width: 68px;">
PARTIDA</div>
<input name="Partida"  type="text" id="Partida"
class="lt" style="left:202px; top: 125px; width: 68px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Partida;?>'/>



<div class="s1" style="left:284px; top: 105px; width: 197px;">
NOMBRE / DESCRIPCION</div>
<input name="Des"  type="text" id="Des"
class="lt" style="left: 282px; top: 125px; width: 244px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Des;?>'/>

<div class="s1" style="left:536px; top: 105px; width: 166px;">
MEASURES (Medidas)</div>
<input name="Mea"  type="text" id="Mea"
class="lt" style="left: 536px; top: 125px; width: 162px; text-transform:none"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Mea;?>'/>


<div class="s1" style="left:706px; top: 91px; width: 80px;">
PRECIO UNITARIO $
</div>
<input name="Pre"  type="text" id="Pre"
class="ndd" style="left:705px; top: 125px; width: 82px"
tabindex="<?php echo ++$TI;?>" maxlength="11"
value='<?php echo $Pre;?>'/>


<div class="s1" style="left:799px; top: 105px; width: 70px;"> UNIDAD
</div>
<select name="UndP" id="UndP" 
class="lt" style="left:797px; top: 125px; width: 85px"
tabindex="<?php echo ++$TI;?>">
<?php 
echo FCBO_("und_abr","ges_unidades","id_und>0",$UndP,"","S");
?>
</select>


<?php if(strlen($Hab)==0)$Hab="SI";?>
<div class="s1" style="left:893px; top: 105px; width: 82px;">
HABILITADO</div>
<select name="Hab" id="Hab" 
class="lt" style="left: 892px; top: 125px; width: 71px"
tabindex="<?php echo ++$TI;?>">
<?php 
echo FCBO_("aux_nom","auxiliar","aux_des='SN'",$Hab,"","S");
?>
</select>

<div class="s1" style="left:978px; top: 105px; width: 6px; background-color:#093; height:50px"></div>

<div class="s1" style="left:1001px; top: 83px; width: 227px;">
<b>Importar referencias desde excel</b></div>

<div class="s1" style="left:35px; top: 152px; width: 522px;">
<b>REFERENCIAS</b> (Para agregar una referencia previamente guardar/activar un producto)</div>

<?php
$BIS="";
if(vbval($StrId)==0)$BIS=' disabled ';
?>

<div class="s1" style="left: 35px; top: 192px; width: 104px;">
EMPRESAS</div>
<select name="Emp" id="Emp" <?php echo $BIS;?>
class="lt" style="left: 35px; top: 212px; width: 123px"
tabindex="<?php echo ++$TI;?>">
<?php 
echo FCBO_("emp_nom_cor","ges_empresas","id_emp>0",$Emp,"","S");
?>
</select>

<div class="s1" style="left: 166px; top: 179px; width: 75px;">
INVOICE FECHA</div>
<input name="InvFec"  type="text" id="InvFec" <?php echo $BIS;?>
class="fe tcal" style="left: 166px; top: 212px; width: 75px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $InFec;?>'/>

<div class="s1" style="left: 252px; top: 179px; width: 68px;">
INVOICE SERIE*</div>
<input name="InvSer"  type="text" id="InvSer" <?php echo $BIS;?>
class="lt" style="left: 250px; top: 212px; width: 70px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $InvSer;?>'/>

<div class="s1" style="left: 329px; top: 179px; width: 82px;">
INVOICE NUMERO*</div>
<input name="InvNum"  type="text" id="InvNum" <?php echo $BIS;?>
class="lt" style="left: 328px; top: 212px; width: 78px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $InvNum;?>'/>

<div class="s1" style="left: 414px; top: 179px; width: 58px;">
INVOICE ITEM*</div>
<input name="InvIte"  type="text" id="InvIte" <?php echo $BIS;?>
class="lt" style="left: 414px; top: 212px; width: 57px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $InvIte;?>'/>

<div class="s1" style="left: 480px; top: 192px; width: 55px;">
DUA
</div>
<input name="Dua"  type="text" id="Dua" <?php echo $BIS;?>
class="lt" style="left: 481px; top: 212px; width: 77px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Dua;?>'/>

<div class="s1" style="left: 569px; top: 192px; width: 67px;">
DUA ITEM
</div>
<input name="DuaIte"  type="text" id="DuaIte" <?php echo $BIS;?>
class="lt" style="left: 569px; top: 212px; width: 63px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $DuaIte;?>'/>

<div class="s1" style="left: 646px; top: 180px; width: 71px;">
PAIS ORIGEN</div>
<input name="PaiOri"  type="text" id="PaiOri" <?php echo $BIS;?>
class="lt" style="left: 643px; top: 212px; width: 72px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $PaiOri;?>'/>

<div class="s1" style="left: 728px; top: 179px; width: 98px;">
PAIS DESEMBARCO</div>
<input name="PaiDes"  type="text" id="PaiDes" <?php echo $BIS;?>
class="lt" style="left: 727px; top: 212px; width: 96px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $PaiDes;?>'/>

<div class="s1" style="left: 835px; top: 195px; width: 82px;"> PARTIDA
</div>
<input name="Par"  type="text" id="Par" <?php echo $BIS;?>
class="lt" style="left: 835px; top: 212px; width: 173px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Par;?>'/>

<div class="s1" style="left:1022px; top: 195px; width: 160px;">
NOMBRE COMERCIAL</div>
<input name="NomCom"  type="text" id="NomCom" <?php echo $BIS;?>
class="lt" style="left: 1021px; top: 212px; width: 206px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $NomCom;?>'/>

<div class="s1" style="left:34px; top: 244px; width: 59px;"> ESTADO
</div>
<input name="Est"  type="text" id="Est" <?php echo $BIS;?>
class="lt" style="left:34px; top: 261px; width: 74px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Est;?>'/>


<div class="s1" style="left:118px; top:244px; width: 59px;">
FOB USD
</div>
<input name="FobUsd"  type="text" id="FobUsd" <?php echo $BIS;?>
class="lt" style="left:118px; top: 261px; width: 61px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $FobUsd;?>'/>

<div class="s1" style="left:188px; top: 244px; width: 59px;">
FOB KG
</div>
<input name="FobKg"  type="text" id="FobKg" <?php echo $BIS;?>
class="lt" style="left:188px; top: 261px; width: 60px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $FobKg;?>'/>

<div class="s1" style="left: 258px; top: 244px; width: 57px;">
UNIDAD
</div>
<input name="Und"  type="text" id="Und" <?php echo $BIS;?>
class="lt" style="left: 257px; top: 261px; width: 52px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Und;?>'/>

<div class="s1" style="left: 318px; top: 244px; width: 80px;">
CANTIDAD
</div>
<input name="Can"  type="text" id="Can" <?php echo $BIS;?>
class="lt" style="left: 318px; top: 261px; width: 61px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Can;?>'/>

<div class="s1" style="left: 389px; top: 244px; width: 84px;">
MODELO
</div>
<input name="Mod"  type="text" id="Mod" <?php echo $BIS;?>
class="lt" style="left: 389px; top: 261px; width: 66px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Mod;?>'/>


<div class="s1" style="left: 463px; top: 244px; width: 117px;">
CLASE VARIEDAD
</div>
<input name="Cla"  type="text" id="Cla" <?php echo $BIS;?>
class="lt" style="left: 463px; top: 261px; width: 108px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Cla;?>'/>

<div class="s1" style="left: 580px; top: 244px; width: 268px;">
  OBSERVACION
</div>
<input name="Obs"  type="text" id="Obs" <?php echo $BIS;?>
class="lt" style="left: 580px; top: 261px; width: 339px"
tabindex="<?php echo ++$TI;?>"
value='<?php echo $Obs;?>'/>

<input name="CmdAdd" type="button" id="CmdAdd"  <?php echo $BIS;?>
value="Agregar" style="position: absolute; left: 927px; top: 260px; width: 87px; height: 23px;"
onClick="AddRef();" 
tabindex="<?php echo ++$TI;?>"/>


<input name="CmdXls1" type="button" id="CmdXls1"  
value="Exportar" style="position: absolute; left: 1167px; top: 260px; width: 64px; height: 23px;"
onClick="exportarExcel();" />


<?php
if(strlen($StrId)==0){
$ImpDis="disabled";
} 
?>

<input name="CmdImpRef" type="button" id="CmdImpRef" 
value="Correspondientes al registro activo" 
style="position: absolute; left: 995px; top: 106px; width: 231px; height: 23px;"
onClick="ImpEcxel();" <?php DIS($StrId);?> />

<input name="CmdMas" type="button" id="CmdMas" 
value="Referencias masivas" 
style="position: absolute; left: 995px; top: 136px; width: 231px; height: 23px;"
onClick="ImpEcxelMasivo();" />



<div class="tableFixHead" style="left:35px; top: 293px; width: 1195px; height: 119px;">
<table width="100%" id="TabRef" style="font-family:Arial, Helvetica, sans-serif; font-size:11px" >
<thead>
<tr>
<th width="6%">#</th>
<th width="6%" align="left">EMPRESA</th>
<th width="6%" align="left">INV.FEC</th>
<th width="6%" align="left">INV.SERIE</th>
<th width="6%" align="left">INV.NUM</th>
<th width="4%">ITEM</th>
<th width="8%" align="left">DUA</th>
<th width="4%" align="left">ITEM</th>

<th width="7%" align="left">PAIS ORI.</th>
<th width="6%" align="left">PAIS DES.</th>
<th width="7%" align="left">PARTIDA</th>
<th width="9%" align="left">NOM.COMERCIAL</th>
<th width="6%" align="left">ESTADO</th>
<th width="5%" align="left">FobUSS</th>
<th width="5%" align="left">FobKG</th>
<th width="5%" align="left">UNIDAD</th>
<th width="5%" >CANT.</th>
<th width="5%" align="left">MODELO</th>
<th width="8%" align="left">CLASE</th>

<th width="4%" align="left">OBSERVACION</th>

<th width="2%">&nbsp;</th>
<th width="2%">&nbsp;</th>
</tr>
</thead>
<tbody>
<?php
$PX=0;
$DET_PRO=vbval($StrId);
$s="SELECT * FROM vis_ges_productos_referencias 
	WHERE ID_PRO=$DET_PRO ORDER BY INV_FEC DESC ";	
$rs=grs($s);
while($r=$rs->fetch_assoc()){	
	$PX++;
	$CSS="";
	if($PX%2==0)$CSS=' style="background-color:#D4D4D4" ';
	$TXT_URL="URL_".$r['ID_REF'];
	$NEG="";
	if($r['R_INV_OK']==1)$NEG="font-weight:bold";
?>
    <tr  <?php echo $CSS;?>  onDblClick="PEdit('<?php echo $r['ID_REF'];?>');">
    <td align="center">
    <a href="javascript:void(0)" onClick="PEdit('<?php echo $r['ID_REF'];?>');">
    <img src="img/b_edit.png"  border="0"/></a></td>
    <!----------------------------------------------->
    <td><input type="text" class="tzt" readonly value="<?php echo $r['NC_EMP'];?>" style="width:100px;<?php echo $NEG;?>"></td>

    <td><input type="text" class="tzt" readonly value="<?php echo $r['INV_FEC_'];?>" style="width:100px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['INV_SER'];?>" style="width:100px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['INV_NUM'];?>" style="width:100px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['INV_ITE'];?>" style="width:35px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['DUA'];?>" style="width:100px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['DUA_ITEM'];?>" style="width:35px;<?php echo $NEG;?>"></td>
    
    <td><input type="text" class="tzt" readonly value="<?php echo $r['PAI_ORI'];?>" style="width:100px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['PAI_DES'];?>" style="width:100px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['PARTIDA'];?>" style="width:100px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['NOM_COM'];?>" style="width:150px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['ESTADO'];?>" style="width:60px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['FOB_USS'];?>" style="width:50px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['FOB_KG'];?>" style="width:50px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['UNIDAD'];?>" style="width:50px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['CANTIDAD'];?>" style="width:50px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['MODELO'];?>" style="width:100px;<?php echo $NEG;?>"></td>
    <td><input type="text" class="tzt" readonly value="<?php echo $r['CLASE'];?>" style="width:100px;<?php echo $NEG;?>"></td>
    
    
    <td><input type="text" class="tzt" readonly value="<?php echo $r['OBS'];?>" style="width:60px;<?php echo $NEG;?>"></td>
    <!----------------------------------------------->
    
    <td align="center">
    <a href="javascript:void(0)" onClick="PEdit('<?php echo $r['ID_REF'];?>');">
    <img src="img/b_edit.png"  border="0"/></a></td>
    
    <td align="center">
    <a href="javascript:void(0)" onClick="fEr(<?php echo $r['ID_REF'];?>);">
    <img src="img/eliminar.png"  border="0"/></a></td>
    </tr>
<?php
}
?>		  

</tbody>
</table>
</div>

<!--INI_BOTONES-------------------------------->
<div id="DivBtn" align="left" 
style="position:absolute; left: 33px; top: 71px; width:1000px; height: 26px;">
<input name="CmdB" type="button" id="CmdG" class="btm" 
<?php GOM($StrId);?>
onclick="fGuardar();"  
tabindex="<?php echo ++$TI;?>"/>

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

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

<input name="CmdB" type="button" id="CmdE" class="btm" 
value="Eliminar" onClick="fEliminar();"
<?php DIS($StrId);?>
tabindex="<?php echo ++$TI;?>" />
</div>
<!--FIN_BOTONES-------------------------------->
<div  style="position: absolute; left:35px; top: 422px; ; width: 1195px;height: ">
<table width="100%" style="font-family:Arial, Helvetica, sans-serif; font-size:11px">
<thead>
<tr>
<td colspan="10" height="18" align="center">
<?php echo PagBtn($LStrTR,$VP_RegXPag,$LStrPag);?>
</td>
</tr>
</thead>
</table>
</div>

<div class="tableFixHead" style="left:35px; top: 452px; width: 1195px; height: 207px;">
<table width="100%" style="font-family:Arial, Helvetica, sans-serif; font-size:11px" >
<thead>
<tr>
<th width="4%" align="left">CODIGO</th>
<th width="8%" align="left">PARTIDA</th>
<th width="33%" align="left">*NOMBRE / DESCRIPCION</th>
<th width="30%" align="left">MEDIDAS</th>
<th width="10%" align="left">PRECIO UNIT. $</th>
<th width="8%" align="left">UNIDAD</th>
<th width="3%" align="left">HAB</th>
<th width="4%">&nbsp;</th>
</tr>
</thead>
<tbody>
<?php
$PX=0;
$s="SELECT * FROM vis_ges_productos WHERE ID>0 ORDER BY DES ";	
$s=PagSql($s,$LStrPag,$VP_RegXPag,$LStrTR);
$rs=grs($s);
while($r=$rs->fetch_assoc()){	
$PX++;
$CSS="";
if($PX%2==0)$CSS=' style="background-color:#D4D4D4" ';
?>
<tr onDblClick="fAc(<?php echo $r['ID'];?>);" <?php echo $CSS;?>>
<!----------------------------------------------->
<td><?php echo $r['COD'];?></td>
<td><?php echo $r['PARTIDA'];?></td>
<td><?php echo $r['DES'];?></td>
<td><?php echo $r['MEDIDAS'];?></td>
<td><?php echo $r['PRE'];?></td>
<td><?php echo $r['UND'];?></td>
<td><?php echo $r['HAB'];?></td>
<!----------------------------------------------->
<td align="center">
<a href="javascript:void(0)" onClick="fAc(<?php echo $r['ID'];?>);">
<img src="img/b_edit.png"  border="0"/></a></td>
</tr>
<?php
}
?>		  

</tbody>
</table>
</div>

<input name="CmdRep" type="button" id="CmdRep"  
value="Ver codigos repetidos" style="position: absolute; left:35px; top: 666px; width: 227px; height: 23px;"
onClick="verRepetidos();" 
/>

<input name="CmdImportar" type="button" id="CmdImportar"  
value="Importar productos" style="position: absolute; left:278px; top: 666px; width: 227px; height: 23px;"
onClick="impProductos();" 
/>


<script>
function verRepetidos(){
	Ow("prod_cod_rep.php",570,620);
}
function impProductos(){
	Ow("ges_importar_productos.php",620,410);
}
</script>

<?php if(strlen($CodRep)==0)$CodRep="NO";?>
<div class="s1" style="left:893px; top: 105px; width: 82px;">
HABILITADO</div>
<select name="Hab" id="Hab" 
class="lt" style="left: 892px; top: 125px; width: 71px"
tabindex="<?php echo ++$TI;?>">
<?php 
echo FCBO_("aux_nom","auxiliar","aux_des='SN'",$Hab,"","S");
?>
</select>


<!--INI_OCULTOS-------------------------------->
<?php
include("ocultos.php");
?>
<!--INI_OCULTOS-------------------------------->
<input name="SYS_MSG" type="hidden" id="SYS_MSG" value="<?php echo $SYS_MSG;?>" />
<input name="SYS_FOC" type="hidden" id="SYS_FOC" value="<?php echo $SYS_FOC;?>" />
<input name="TXT_AUX" type="hidden" id="TXT_AUX" value="" />
<a href="http://www.google.com" id="LNK" target="_blank" style="display:none" >IR</a>
</form>
</div>
</div>
</body>
</html>
