<?php 
session_start();
foreach($_GET as $campo => $valor){${$campo}=trim($valor);} 
foreach($_POST as $campo => $valor){${$campo}=trim($valor);} 

$user_codigo=$_SESSION['user_codigo'];

include_once('lib_main.php');
if(!empty($empr)){$link_data=msql_connect('','','',$empr);}

$user0=data_maes('user',$user_codigo);




$precio='pvp1';     
if(mb_stristr('gmny.gnya',$cfg_empr)){$solo_ctlg=1;}
if(mb_stristr('gmny.gnya',$cfg_empr)){$ver_tallas=2;}



// SELECT SQL
{
$sql_item ='select *,';
$sql_item.='count(item_tlla) as item_ctalla ';
$sql_item.='from item0 ';
$sql_item.='left join lsub0 on item_lsub=lsub_codigo ';
$sql_item.='left join mrca0 on item_mrca=mrca_codigo ';
$sql_item.='left join clor0 on item_clor=clor_codigo ';
$sql_item.='left join tlla0 on item_tlla=tlla_codigo ';
$sql_item.='left join kldd0 on item_kldd=kldd_codigo ';
$sql_item.='where item_status not in ("I","X") ';
$sql_item.='and item_descri!="" ';
$sql_item.='and item_codigo not like "99%" ';
$sql_item.='and item_'.$precio.'>0 ';
if(!empty($buscar))
{
     $buscar=alltrim($buscar);
     $buscararray=explode(' ',$buscar);
     for($idx=0; $idx<count($buscararray); $idx++)
     {
          $buscaidx=$buscararray[$idx];
          if(strtolower(substr($buscaidx,-2,2))=='es'){$buscaidx=substr($buscaidx,0,-2);}
          if(strtolower(substr($buscaidx,-1,1))=='s'){$buscaidx=substr($buscaidx,0,-1);}
//           $sql_item.=' and (item_descri like "%'.$buscaidx.'%" or item_codigo like "%'.$buscaidx.'%" or item_codigov like "%'.$buscaidx.'%") ';
          $sql_item.=' and (item_descri like "%'.$buscaidx.'%" or item_codigo like "%'.$buscaidx.'%" or item_codigov like "%'.$buscaidx.'%" or mrca_descri like "%'.$buscaidx.'%") ';
     }
}
     
// if($sn_stock!=''){$sn_stock=1;}     
// if($bode!=''){$sn_stock=1;}     
if($lsub!=''){$sql_item.='and (item_lsub like "'.$lsub.'%") ';}
if($mrca!=''){$sql_item.='and (item_mrca = "'.$mrca.'") ';}
if($item!=''){$sql_item.='and (item_codigo like "'.$item.'%") ';}
if($mdlo!=''){$sql_item.='and (item_mdlo like "'.$mdlo.'%") ';}
if($clor!='')
{
     $clor_tipo=r_fiel('clor',$clor,'tipo');
     $sql_item.=' and (clor_tipo like "'.$clor_tipo.'%")';
}
if($solo_ctlg==1)
{$sql_item.=' and item_ctlg=1 ';}

if(mb_stristr('gnya.gmny',$cfg_empr))
{$sql_item.=' and item_codigo!=item_rfer ';}

if(mb_stristr('gnya.gmny',$cfg_empr) and !empty($categoria))
{
     $categoriax=$categoria;
     if(strtolower(substr($categoriax,-2,2))=='es'){$categoriax=substr($categoriax,0,-2);}
     if(strtolower(substr($categoriax,-1,1))=='s'){$categoriax=substr($categoriax,0,-1);}
     $sql_item.=' and item_descri like "%'.$categoriax.'%" ';
}

if(!empty($sn_foto))     
{
     $sql_item.='and left(item_codigo,10) ';     
     if($sn_foto==2)
     {$sql_item.='not ';}
     $sql_item.='in(select left(imgi_item,10) from imgi0 where imgi_status!="X" and imgi_item is not null) ';     
}
          
if(!empty($sn_stock))     
{
     $sql_item.='and left(item_codigo,10) ';
     if($sn_stock==2)
     {$sql_item.='not ';}
     $sql_item.='in(select left(item,10) ';
     $sql_item.='from (select item, sum(case when left(docu,1)="I" then cant else cant*(-1) end) as stock ';
     $sql_item.='from inve1 left join inve2 on idnum1=idnum2 ';
     $sql_item.='where status not in ("O","X") ';
     
//      filtrar bodega para cuando sea punto de venta o bodega en linea
//      $sql_item.='and bode ';
     if($sn_stock==1 and !empty($bode))
     {$sql_item.='and bode="'.$bode.'" ';}
     
     $sql_item.='group by item) as qstock ';
     $sql_item.='where stock>0) ';
}
     
     
     
$xgroupby='group by item_codigo ';
// $xorderby='order by item_descri,item_codigo ';                                 
// $xorderby='order by item_codigo, item_descri ';                                 
$xorderby='order by item_descri, item_codigo ';                                 
if(empty($buscar) and empty($lsub) and empty($mrca) and empty($clor))
{$xlimit=1000;}

     
if(mb_stristr('gmny.gnya',$cfg_empr)){$xgroupby='group by item_mdlo,item_raiz ';}
if(mb_stristr('gmny.gnya',$cfg_empr)){$xorderby='order by item_idate, item_codigo ';}
// if(mb_stristr('orel',$cfg_empr)){$xorderby='order by item_idate, item_descri ';}
     
// if(mb_stristr($orden,'Cod')){$xorderby='order by item_codigo ';}
if(mb_stristr($orden,'A-Z')){$xorderby='order by item_descri,item_codigo ';}
if(mb_stristr($orden,'Z-A')){$xorderby='order by item_descri desc,item_codigo ';}
if(mb_stristr($orden,'<')){$xorderby='order by item_pvp1,item_descri,item_codigo ';}
if(mb_stristr($orden,'>')){$xorderby='order by item_pvp1 desc,item_descri,item_codigo ';}

     
     
     
$sql_item.=$xgroupby;                                 
$sql_item.=$xorderby;

// if(empty($lsub) and empty($mrca) and empty($clor)){$sql_item.='desc ';}

if(!empty($xlimit)){$sql_item.='limit '.$xlimit;}
     
$res_item=msql_query($sql_item,$link_data);
// echo $sql_item;

}




$ptitulo='FILTROS'; include('ele_title.php');


if(@mb_stristr('sist.admi',$user0['user_tipo']))
{
$pid='bode'; $pdefa=$bode; $ptodo=1;
$ptabla='bode';
$plabel='Bodega';
include('ele_select.php');

$pid='sn_stock'; $pdefa=$sn_stock;
$plabel='Stock';
$poptions='0,Todos;1,SI;2,NO';
$ponchange='{buscar_item();}';
include('ele_select.php');

$pid='sn_foto'; $pdefa=$sn_foto;
$plabel='Foto';
$poptions='0,Todos;1,SI;2,NO';
$ponchange='{buscar_item();}';
include('ele_select.php');

include('ele_break.php');
}


if(mb_stristr('gmny.gnya',$cfg_empr))
{
     $pdefa=$categoria; $plabel='Categorías'; $pid='categoria'; $ptodo=1;
     $ponchange='{buscar_item();}';
     $poptions='Blusas;Vestidos;Faldas;Enterizos;Shorts;Leggins;Camisas;Camisetas;Pantalones;Pijamas;Niñ,Niñ@s;';
     include('ele_select.php');
//      echo '<span style="float:left;width:2em;">.</span>';
}


$pid='lsub'; $pdefa=$lsub; $ptodo=1;
$ptabla='lsub';
$plabel='Linea';
// $ponchange='{buscar_item();}';
if(mb_stristr('gmny.gnya',$cfg_empr)){$phide=1;}
include('ele_select.php');
// echo '<span style="float:left;width:2em;">.</span>';


// $span=$dom->appendChild($dom->createElement("span","."));
//      $span->setAttribute("style","float:left;width:2em;");

$pid='mrca'; $pdefa=$mrca; $ptodo=1;
$ptabla='mrca';
$plabel='Marca';
$pwhere='(mrca_codigo in(select distinct item_mrca from inve1 left join inve2 on idnum1=idnum2 left join item0 on item=item_codigo))';
include('ele_select.php');


$pid='orden'; $pdefa=$orden;
$plabel='Ordenar X';
$poptions='Código;Nombre (A-Z);Nombre (Z-A);Precio <;Precio >';
$ponchange='{buscar_item();}';
// $palign='right';
include('ele_select.php');


// echo '<div></div>';

$msgres='';
$msgres.=msql_num_rows($res_item).' Resultado(s)...';
{if(!empty($buscar)){$msgres.='para "'.$buscar.'"';}}


$simi_buscar=trim($buscar);
if(mb_stristr($simi_buscar,' '))
{$simi_buscar=substr($simi_buscar,0,strpos($simi_buscar,' '));}

if(msql_num_rows($res_item)<=1){$buscar='';}
// else
// {if(!empty($buscar)){$msgres.='para "'.$buscar.'"';}}

// if(is_numeric($buscar)){$buscar='';}

$pid='buscar'; 
$plabel='Texto';
$pvalue=$buscar;
// $pdefa=$buscar;
$pplace='buscar';
// $palign='right';
$ponenter='{buscar_item();}';
include('ele_input.php');


$pid='btn_busca'; 
$plabel='Buscar';
$pplace='buscar';
$palign='right';
$ponclick='{buscar_item();}';
include('ele_button.php');

// catalogo
{    
echo '<div style="float:left;white-space: nowrap;">';     
echo $msgres;
echo '</div>';     
     
include('ele_break.php');
// echo '<div style ="display:block;height:3em;"></div>';     

$ptitulo='CATALOGO';include('ele_title.php');
// include('ele_break.php');
     
     
msql_data_seek($res_item,0);     
while($aso_item=msql_fetch_assoc($res_item))
{
     $pid=$aso_item['item_codigo'];
     $ptabla='item';
     $pprecio='pvp1';
//      $pcodigo=$aso_item['item_codigo'];
     $pcodigo=$aso_item['item_rfer'];
     if(empty($pcodigo)){$pcodigo=$aso_item['item_mdlo'];}
     if(empty($pcodigo)){$pcodigo=$aso_item['item_codigo'];}
     $pstockv=3;
     if(mb_stristr('gnya.gmny',$cfg_empr) and !esMovil()){$pobj=4;}
//      if(mb_stristr('gnya.gmny',$cfg_empr) and !esMovil()){$pobj=5; $psize=0;}
     if(mb_stristr('gnya.gmny',$cfg_empr)){$pstockv=0;}
     $pcarr=1;
     
     include('ele_image.php');
     $simi_codi=$pcodigo;
     $same_codi=trim($aso_item['item_codigo']);
     $same_desc=trim($aso_item['item_descri']);
     
}
     
if(msql_num_rows($res_item)==1)
{
     include('ele_break.php');
//      $simi_desc=trim(substr($simi_desc,0,10));
     $same_desc=trim($same_desc);
     $same_desc=str_replace('  ',' ',$same_desc);
     $simi_desc=$same_desc;
     
          
     if(mb_stristr('gmny.gnya',$cfg_empr)){$same_codi=substr($same_codi,0,10);}
     
     if(mb_stristr('gmny.gnya',$cfg_empr)){$simi_desc=substr($same_codi,0,8);}  

     $sql_item=str_replace('where ','where item_codigo not like "'.$same_codi.'%" and ',$sql_item);
     
     $sql_simi=$sql_item;
     $sql_simi=str_replace('%'.$simi_codi.'%','%'.$simi_desc.'%',$sql_simi);
     $res_simi=msql_query($sql_simi,$link_data);

     if(msql_num_rows($res_simi)==0)
     {
          if(mb_stristr($simi_desc,' '))
          {
               $words=explode(' ',$same_desc);
               for($idx=count($words)-1; $idx>=0; $idx-- )
               {
                    $simi_desc='';
                    for($idx2=0; $idx2<=$idx; $idx2++)
                    {$simi_desc.=$words[$idx2].' ';}
                    $simi_desc=trim($simi_desc);
                    
                    $sql_simi=$sql_item;
                    $sql_simi=str_replace('%'.$simi_buscar.'%','%'.$simi_desc.'%',$sql_simi);
                    $res_simi=msql_query($sql_simi,$link_data);
//                     if(msql_num_rows($res_simi)!=0){break;}
                    if(msql_num_rows($res_simi)>1){break;}
               }
          }
     }     
     
     
     
     if(msql_num_rows($res_simi)==0)
     {
          $sql_simi=$sql_item;
          $sql_simi=str_replace('%'.$simi_buscar.'%','%'.$simi_desc.'%',$sql_simi);
          $res_simi=msql_query($sql_simi,$link_data);
     }

     if(msql_num_rows($res_simi)==0)
     {
          $sql_simi=$sql_item;
          $sql_simi=str_replace('%'.$simi_buscar.'%','%'.$same_desc.'%',$sql_simi);
          $res_simi=msql_query($sql_simi,$link_data);
     }
     
     if(msql_num_rows($res_simi)==0)
     {
          if(mb_stristr($same_desc,' ')){$same_desc=substr($same_desc,0,strpos($same_desc,' '));}
          $sql_simi=$sql_item;
          $sql_simi=str_replace('%'.$simi_buscar.'%','%'.$same_desc.'%',$sql_simi);
          $res_simi=msql_query($sql_simi,$link_data);
     }

     
     if(msql_num_rows($res_simi)!=0)
     {
          $ptitulo='SIMILARES';include('ele_title.php');
//           echo msql_num_rows($res_simi).' Resultado(s)...';
     }
     
     
     while($aso_item=msql_fetch_assoc($res_simi))
     {
          $pid=$aso_item['item_codigo'];
          $pcodigo=$aso_item['item_rfer'];
          if(empty($pcodigo)){$pcodigo=$aso_item['item_mdlo'];}
          if(empty($pcodigo)){$pcodigo=$aso_item['item_codigo'];}
//           $pstockv=3;
//           if(mb_stristr('gnya.gmny',$cfg_empr) and !esMovil()){$pobj=4;}
//           if(mb_stristr('gnya.gmny',$cfg_empr)){$pstockv=0;}
//           $pcarr=1;
          include('ele_image.php');
     }

}
     
}

// echo '</div>';

?>


<script>
function buscar_item()
{
     get_para();
     var xhref='';
     xhref=xhref+'?scri=shop_item2';
     xvalue=document.getElementById("buscar").value; xhref=xhref+'&buscar='+xvalue;
     if(document.getElementById("categoria")){xvalue=document.getElementById("categoria").value;xhref=xhref+'&categoria='+xvalue;}
     if(document.getElementById("lsub")){xvalue=document.getElementById("lsub").value;xhref=xhref+'&lsub='+xvalue;}
     if(document.getElementById("mrca")){xvalue=document.getElementById("mrca").value;xhref=xhref+'&mrca='+xvalue;}
     if(document.getElementById("bode")){xvalue=document.getElementById("bode").value;xhref=xhref+'&bode='+xvalue;}
     if(document.getElementById("orden")){xvalue=document.getElementById("orden").value;xhref=xhref+'&orden='+xvalue;}
     if(document.getElementById("sn_foto")){xvalue=document.getElementById("sn_foto").value;xhref=xhref+'&sn_foto='+xvalue;}
     if(document.getElementById("sn_stock")){xvalue=document.getElementById("sn_stock").value;xhref=xhref+'&sn_stock='+xvalue;}
     location.href=xhref;          
}
</script>

<script>
function save_carr(item,cant)
{
     var data=new FormData();
     data.append('script','save_carr');
     data.append('item',item);
     data.append('cant',cant);
     exe_ajax(data);
}
</script>



<script>document.title="<?php echo ucfirst($cfg_descri).' - Catálogo';?>"</script>
