<?php
if(empty($fecha0)){$fecha0='2000-01-01';}

// echo '====='.date('z');

// echo '====='.$modu;
// echo '====='.$info;
// echo '====='.$docu;

$xround=6;
$xround2=2;
$xround6=6;
$fechao=$fecha1;
$fecha1o=$fecha1;

$doc18=1;
$doc18=0;

$docu=strtoupper($docu);

if(substr($docu,0,2)=='RB'){$rbco=1;}

$fecha1a=date('Y-m-01',strtotime($fecha2.'-365 days'));



$sql_set='set @numrow:=1;';
msql_query($sql_set,$link_data);
$sql_set='set @numbrk:=null;';
msql_query($sql_set,$link_data);



// $vndd_ok=1;
// $mrca_ok=1;
// $clor_ok=1;

$vndd_ok=0;
$rcdd_ok=0;
$mrca_ok=0;
$clor_ok=0;
$tlla_ok=0;


// if(1==2)
if(mb_stristr2('.vtas.clie',$modu))
{
     $mtok='vndd';
     $sql_mtok ='select distinct vndd from vtas1 ';
     $sql_mtok.='where ifnull(status,"") not in ("O","X") ';
     $sql_mtok.='and vndd*1>0 and ifnull(vndd,"")!="" ';
     $sql_mtok.='limit 5 ';
     $res_mtok=msql_query($sql_mtok,$link_data);
     if(msql_num_rows($res_mtok)>1){$vndd_ok=1;}
}



if(mb_stristr2('.cxco',$modu))
{
     $mtok='rcdd';
     $sql_mtok ='select distinct rcdd from vtas1 ';
     $sql_mtok.='where ifnull(status,"") not in ("O","X") ';
     $sql_mtok.='and rcdd*1>0 and ifnull(rcdd,"")!="" ';
     $sql_mtok.='limit 5 ';
//      $sql_mtok ='select distinct enti_rcdd from zsub0 ';
//      $sql_mtok.='where ifnull(zsub_status,"") not in (X") ';
//      $sql_mtok.='and zsub_rcdd*1>0 and ifnull(zsub_rcdd,"")!="" ';
//      $sql_mtok.='limit 5 ';
     $res_mtok=msql_query($sql_mtok,$link_data);
     if(msql_num_rows($res_mtok)>1){$rcdd_ok=1;}
}


// if(1==2)
// if(mb_stristr2('.vtas.clie.carr.inve..item.ibod.ivta.igas',$modu))
if(mb_stristr2('.vtas.clie.carr.inve.infi',$modu))
{
     $mtok='mrca';
     $sql_mtok ='select distinct item_mrca from '.$modu.'1  ';
     $sql_mtok.='left join '.$modu.'2 on idnum1=idnum2 ';
     $sql_mtok.='left join item0 on item=item_codigo ';
     $sql_mtok.='left join '.$mtok.'0 on item_'.$mtok.'='.$mtok.'_codigo ';
     $sql_mtok.='where ifnull(item_'.$mtok.',"")!="" ';
     if(!empty($fecha1))
     {$sql_mtok.='and fecha>="'.$fecha1.'" ';}
     if(!empty($fecha2))
     {$sql_mtok.='and fecha<="'.$fecha2.'" ';}
     $sql_mtok.='limit 5 ';
     $res_mtok=msql_query($sql_mtok,$link_data);
     if(msql_num_rows($res_mtok)>1){$mrca_ok=1;}
     
//      echo '==='.msql_num_rows($res_mtok);
     
     $mtok='clor';
     $sql_mtok ='select distinct item_clor from '.$modu.'1  ';
     $sql_mtok.='left join '.$modu.'2 on idnum1=idnum2 ';
     $sql_mtok.='left join item0 on item=item_codigo ';
     $sql_mtok.='left join '.$mtok.'0 on item_'.$mtok.'='.$mtok.'_codigo ';
     $sql_mtok.='where ifnull(item_'.$mtok.',"")!="" ';
     if(!empty($fecha1))
     {$sql_mtok.='and fecha>="'.$fecha1.'" ';}
     if(!empty($fecha2))
     {$sql_mtok.='and fecha<="'.$fecha2.'" ';}
     $sql_mtok.='limit 5 ';
     $res_mtok=msql_query($sql_mtok,$link_data);
     if(msql_num_rows($res_mtok)>1){$clor_ok=1;}
   
     $mtok='tlla';
     $sql_mtok ='select distinct item_clor from '.$modu.'1  ';
     $sql_mtok.='left join '.$modu.'2 on idnum1=idnum2 ';
     $sql_mtok.='left join item0 on item=item_codigo ';
     $sql_mtok.='left join '.$mtok.'0 on item_'.$mtok.'='.$mtok.'_codigo ';
     $sql_mtok.='where ifnull(item_'.$mtok.',"")!="" ';
     if(!empty($fecha1))
     {$sql_mtok.='and fecha>="'.$fecha1.'" ';}
     if(!empty($fecha2))
     {$sql_mtok.='and fecha<="'.$fecha2.'" ';}
     $sql_mtok.='limit 5 ';
     $res_mtok=msql_query($sql_mtok,$link_data);
     if(msql_num_rows($res_mtok)>1){$tlla_ok=1;}
   
}


if(mb_stristr2('.comp.prov',$modu))
{$vndd='';$rcdd='';}
// echo '*/*/*/'.$info;
// echo '*/*/*/*'.$fecha1a;

// vista diarios descuadrados
// select idnum1 from (
// select sum(ifnull(cant,0)) as difer,
// cont1.*,cont2.* 
// from cont1 
// left join cont2 on idnum1=idnum2
// where ifnull(status,"")not in ("O","X")
// and ctas in(select ctas_codigo from ctas0)
// group by idnum1
// ) as cont where abs(difer)>=0.01

// if($info=='bthist' and empty($fecha1)){$fecha1='2000-01-01';}
// if($info=='bthist'){$fecha1='2020-01-01';}
if($info=='bthist'){$fecha1=$fecha0;}

if($modu=='inve' and empty($orden)){$orden='Linea';}
if($modu=='infi' and empty($orden)){$orden='Linea';}

if(empty($numero))
{if(empty($fecha2)){$fecha2=date('Y-m-d');}}

if(!empty($numero))
{if(empty($fecha2)){$fecha2=date('Y-12-31');}}

if(!empty($impor)){$fecha1='2010-01-01'; $fecha2=date('Y-m-d'); $ctas='';$cair=''; $item='';}


$rgsn_ok=0;
if($cfg_empr=='hg01'){$rgsn_ok=1;}

// echo '*/*/*/'.$fecha1;
// echo '*/*/*/'.$fecha2;

if(strlen($fecha1)==7)
{
     $fecha1=date('Y-m-01', strtotime($fecha1));
     if(empty($fecha2))
     {$fecha2=date('Y-m-t', strtotime($fecha1));}
}

if(strlen($fecha2)==7)
{
     $fecha1=date('Y-m-01', strtotime($fecha1));
     $fecha2=date('Y-m-t', strtotime($fecha2));
}

if(strlen($fecha1)==8)
{
     $fecha1=date('Y-01-01', strtotime($fecha1));
     $fecha2=date('Y-12-31', strtotime($fecha1));
}


if(strlen($fecha1)==4)
{
     $fecha1=date('Y-01-01', strtotime($fecha1));
     $fecha2=date('Y-12-31', strtotime($fecha1));
}

if(substr($info,-2)=='mm')
{
     $fecha1=date('Y-m-01', strtotime($fecha1));
     $fecha2=date('Y-m-t', strtotime($fecha2));
}


if($modu=='soft')
{
     $fecha1=$fechao;
     $fecha2=$fechao;
}

if($info=='iicuxc')
{
  $cost='true';
}

if($info=='vtresu' and date('Y-m-d')<='2022-07-31')
{
//      $sql_query='select * from information_schema.columns where table_name="vtas1" and column_name="voucher"';
//      $res_query=msql_query($sql_query,$link_data);

//      if(msql_num_rows($res_query)==0)
//      {
//      $sql_query='alter table vtas1 add column voucher decimal(9,2)';
//      $res_query=msql_query($sql_query,$link_data);
//      }
}


//      view_maes('enti','pcnt');    

$saldo0=0.009999;

if($modu=='cont' and $dtr1==2)
{
     $sql_dtr1 ='select idnum1 from cont1 ';
     $sql_dtr1.='where ifnull(status,"")not in("O","X") and docu like "DTR%" and fecha<="'.$fecha2.'" ';
     if(mb_stristr2('.ttssff.ttrrii.ttsfcc.ttricc.ttssss',$info))
     {$sql_dtr1.='and fecha>="'.$fecha1.'" ';}
     $sql_dtr1.='order by fecha desc ';
     $nodtr=1;
}

if(@mb_stristr2('.mtch',$modu))
{
     $sql_mtch ='update mtch0 ';
     $sql_mtch.='left join team0 on mtch_team1=team_codigo ';
     $sql_mtch.='left join sqad0 on mtch_team2=sqad_codigo ';
     $sql_mtch.='set mtch_descri=concat(team_descri," - vs - ",sqad_descri) ';
     $res_mtch=msql_query($sql_mtch,$link_data);     
}

if(@mb_stristr2('.cxco.cxpa',$modu))
{
     $dias_anti=$_SESSION['dias_anti'];
     $saldo0=0.009999;
     if($cfg_empr=='imcp'){$saldo0=0.01;}
     if($cfg_empr=='imcp'){$saldo0=0.019999;}
//      if($cfg_empr=='pcs1'){$saldo0=0.05;}
     if($cfg_empr=='pcs1'){$saldo0=0.019999;}
}

if(@mb_stristr2('.opro',$modu))
{
     $col_ebode='0';
     $ebode=get_ebode('OP',$fecha1,$fecha2);
     foreach ($ebode as $key1=>$value1)
     {
          foreach($value1 as $key2=>$value2)
          if($key2=='ebode' and $value2>0){$col_ebode=(1/1000000); break 2; }
     }
//      var_dump($ebode);
}

if(mb_stristr2('.inve.',$modu))
{
     $col_pcli='0';
     $pclie=get_pcli($item,$enti,$fecha2);
     foreach ($pclie as $key1=>$value1)
     {
          foreach($value1 as $key2=>$value2)
          if($key2=='pcli' and $value2>0){$col_pcli=(1/1000000); break 2;}
     }
     
}


$date1 = new DateTime($fecha1);
$date2 = new DateTime($fecha2);
$ddiff = $date1->diff($date2);
//$tot_meses= floor($ddiff->days /30);

$tot_meses=(date('y',strtotime($fecha2))-date('y',strtotime($fecha1)))*12;
$tot_meses+=date('m',strtotime($fecha2))-date('m',strtotime($fecha1))+1;

// $tot_meses= round($ddiff->days /30,0);
// will output 2 days
// echo $diff->days . ' days ';
// echo $diff->days . ' days ';
// echo $diff->months. ' meses ';
// var_dump($diff);
// echo $tot_meses;

$sql_info='';
// if(empty($info)){return;}
// if(!empty($info))
// {
     if(empty($modu))
     {
          $docu0=data_maes('docu',$docu);
          $modu=$docu0['docu_tabla'];
     }
     $modu=strtolower($modu);

    
     if(!empty($modu))
     {
          $modu0=$modu.'0';     
          $modu1=$modu.'1';     
          $modu2=$modu.'2';     
          $modu3=$modu.'3';     
     }
     

$sql_busca_item='';
if(!empty($item) and mb_stristr2($item,' '))
{
     $busca_item=alltrim($item);
     $buscararray=explode(' ',$busca_item);
     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_busca_item.=' and (item_descri like "%'.$buscaidx.'%" or item_descri2 like "%'.$buscaidx.'%" or item_codigo like "%'.$buscaidx.'%" or item_codigov like "%'.$buscaidx.'%" or mrca_descri like "%'.$buscaidx.'%") ';
     }
     
}
     
if(mb_stristr2($info,'xano') or mb_stristr2($info,'anxm'))
{
     $fecha1=date('Y-01-01', strtotime($fecha1));
     $fecha2=date('Y-12-31', strtotime($fecha2));
}

if(mb_stristr2($info,'sem'))
{
     if($fecha1!=$week_start)
     {
          $week_day = date('w',strtotime($fecha1));
          $week_start = date('Y-m-d', strtotime($fecha1. '-'.($week_day-1).' days'));
     }
     if($fecha2!=$week_end)
     {
//                $week_day = date('w',strtotime($fecha2));
          $week_end = date('Y-m-d', strtotime($fecha2. ' this sunday'));          
     }
     $fecha1=$week_start;
     $fecha2=$week_end;
}
     

// if($info=='vtresu' or $info=='coresu')





     
// SQL_INFO MAIN     
//      echo '*/*/*/'.$info;
     if(substr($info,0,2)=='mt')
     {
          $sql_info='';
          $sql_info.='select * from '.$modu0.' ';
          
          $sql_info.='left join stat0 on '.$modu.'_status=stat_codigo ';

          $xwhere1 ='where ifnull('.$modu.'_status,"")!="X" ';
          $xwhere1.='and '.$modu.'_codigo!="" ';
          
          if(!empty($stat)){$xwhere1.='and '.$modu.'_status = "'.$stat.'" ';}

          if(mb_stristr2('.refx',$modu))
          {
               if(!empty($refx))
               {
                    $xwhere1.='and ( ';
                    $xwhere1.='   '.$modu.'_codigo  ="'.$refx.'" ';
                    $xwhere1.='or '.$modu.'_referx1 ="'.$refx.'" ';
                    $xwhere1.='or '.$modu.'_referx2 ="'.$refx.'" ';
                    $xwhere1.='or '.$modu.'_referx3 ="'.$refx.'" ';
                    $xwhere1.='or '.$modu.'_referx4 ="'.$refx.'" ';
                    $xwhere1.='or '.$modu.'_referx5 ="'.$refx.'" ';
                    $xwhere1.=')';
               }
          }
          
          if(mb_stristr2('.ciex',$modu))
          {
               if(!empty($letra)){$xwhere1.='and '.$modu.'_codigo like "'.$letra.'%"';}
          }
          
          if(mb_stristr2('.ctas',$modu))
          {
               if(!empty($grupo)){$xwhere1.='and '.$modu.'_codigo like "'.$grupo.'%"';}
          }
          
          if(mb_stristr2('.clac',$modu))
          {
               $xwhere1.='and '.$modu.'_tipo<=9 ';
               $xwhere1.='and '.$modu.'_fecha>="'.$fecha1.'" ';
               $xwhere1.='and '.$modu.'_fecha<="'.$fecha2.'" ';
               if(!empty($tcel)){$xwhere1.='and '.$modu.'_tipo = "'.$tcel.'"';}
               if(!empty($enti)){$xwhere1.='and ('.$modu.'_enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
               if($locaexte==1){$xwhere1.='and '.$modu.'_enti like "'.$empr0['empr_ruc'].'%"';}
               if($locaexte==2){$xwhere1.='and '.$modu.'_enti not like "'.$empr0['empr_ruc'].'%"';}
          }
          
          
//           if(mb_stristr2('.caja.ctab.bode.lsub.zsub.csub',$modu))
          if(mb_stristr2('.caja.ctab.bode.zsub.tpro.tcli',$modu))
          {
               $sql_info.='left join ctas0 on '.$modu.'_ctacont = ctas_codigo ';
          }
          
          if(mb_stristr2('.rpis.',$modu))
          {
               $sql_info.='left join segu0 on '.$modu.'_seguro = segu_codigo ';
          }
          
          if(mb_stristr2('.pk12.',$modu))
          {
               $sql_info.='left join enti0 on '.$modu.'_enti = enti_codigo ';
          }
          
          if(mb_stristr2('.ppdd.',$modu))
          {
               $sql_info.='left join enti0 on '.$modu.'_pptario1 = enti_codigo ';
               
//                if($empr0['empr_inmobil'])
//                {
//                     $sql_info.='left join prvc0 on '.$modu.'_prvc = prvc_codigo ';
//                     if($vtalq=='V'){$xwhere1.='and '.$modu.'_venta = "1" ';}
//                     if($vtalq=='A'){$xwhere1.='and '.$modu.'_alqui = "1" ';}
//                     if(!empty($prvc)){$xwhere1.='and '.$modu.'_prvc = "'.$prvc.'" ';}
//                }
               if(!empty($calle2)){$xwhere1.='and '.$modu.'_calle2 like "'.$calle2.'" ';}
               if(!empty($pisomz)){$xwhere1.='and '.$modu.'_pisomz like "'.$pisomz.'" ';}
               
          }
          
          if(mb_stristr2('.bbii.',$modu))
          {
               $sql_info.='left join enti0 on '.$modu.'_pptario = enti_codigo ';
               
//                if($empr0['empr_inmobil'])
               {
                    $sql_info.='left join prvc0 on '.$modu.'_prvc = prvc_codigo ';
                    if($vtalq=='V'){$xwhere1.='and '.$modu.'_venta = "1" ';}
                    if($vtalq=='A'){$xwhere1.='and '.$modu.'_alqui = "1" ';}
                    if(!empty($prvc)){$xwhere1.='and '.$modu.'_prvc = "'.$prvc.'" ';}
               }
               
          }
          
          
          
          
          if(mb_stristr2('.lsub',$modu))
          {
               if($empr0['empr_cont']==1)
               {
               $sql_info.='left join ctas0 on '.$modu.'_ctavtas = ctas_codigo ';
               $sql_info.='left join cta10 on '.$modu.'_ctainve = cta1_codigo ';
               $sql_info.='left join cta40 on '.$modu.'_ctavtas = cta4_codigo ';
               $sql_info.='left join cta50 on '.$modu.'_ctacvta = cta5_codigo ';
               }
          }
          
          if(mb_stristr2('.clac',$modu))
          {
               $sql_info.='left join enti0 on '.$modu.'_enti = enti_codigo ';
               $sql_info.='left join tcel0 on '.$modu.'_tipo = tcel_codigo ';
          }
          
//           if(mb_stristr2('.clie',$modu))
          if(mb_stristr2('.enti.clie',$modu))
          {
               $sql_info.='left join vndd0 on '.$modu.'_vndd = vndd_codigo ';
               $sql_info.='left join rcdd0 on '.$modu.'_rcdd = rcdd_codigo ';
               if(!empty($vndd)){$xwhere1.='and '.$modu.'_vndd like "'.$vndd.'%" ';}
               
          }
          
          if(mb_stristr2('.zsub',$modu))
          {
               $sql_info.='left join vndd0 on '.$modu.'_vndd = vndd_codigo ';
               $sql_info.='left join rcdd0 on '.$modu.'_rcdd = rcdd_codigo ';
          }
          
          
          if(mb_stristr2('.enti.clie.prov.pcnt,vndd',$modu))
          {
               $sql_info.='left join zsub0 on '.$modu.'_zsub = zsub_codigo ';
               $sql_info.='left join tcli0 on '.$modu.'_tcli = tcli_codigo ';
               $sql_info.='left join tpro0 on '.$modu.'_tpro = tpro_codigo ';
               $sql_info.='left join csub0 on '.$modu.'_csub = csub_codigo ';
               
               if(mb_stristr2('.clie',$modu))
               {$sql_info.='left join vtas1 on '.$modu.'_codigo = enti ';}
               
               if(mb_stristr2('.prov',$modu))
               {$sql_info.='left join comp1 on '.$modu.'_codigo = enti ';}
               
               
               $sql_info=calc_maes($modu,$sql_info);
               if(mb_stristr2('.clie.prov',$modu))
               {
                    $xwhere1.='and ifnull(fecha,"")>="'.$fecha1.'" and ifnull(fecha,"")<="'.$fecha2.'" ';
               }
               if(mb_stristr2('.pcnt.',$modu))
               {
                    if($empr0['empr_veterinaria']==1)
                    {
                         if(!empty($especie)){$xwhere1.='and '.$modu.'_especie like "%'.$especie.'%" ';}
                         if(!empty($raza)){$xwhere1.='and '.$modu.'_raza like "%'.$raza.'%" ';}
                    }
               }
//                $sql_info.='and '.$modu.'_codigo!="" ';
          }

//           echo '*/*/*'.$user0['user_codigo'];
          if(mb_stristr2('.rfrx',$modu))
          {
//                $sql_info.='left join enti0 on '.$modu.'_enti = enti_codigo ';
//                $sql_info.='left join item0 on '.$modu.'_item = item_codigo ';
//                $sql_info.='where ifnull('.$modu.'_status,"")!="X" ';
//                $sql_info.='and '.$modu.'_codigo!="" ';
//                if(!empty($liga)){$sql_info.='and '.$modu.'_liga="'.$liga.'" ';}
//                $sql_info=calc_maes($modu,$sql_info);
//                if(!empty($clase)){$xwhere1.='and '.$modu.'_clase like "%'.$clase.'%" ';}
//                if(!empty($mrca)){$xwhere1.='and '.$modu.'_mrca like "'.$mrca.'%" ';}
//                if(!empty($anio)){$xwhere1.='and '.$modu.'_anio like "'.$anio.'%" ';}
//                if(!empty($clor))
               {$xwhere1.='and '.$modu.'_referx = "'.$user0['user_codigo'].'" ';}
          }
       
          
          
          if(mb_stristr2('.seri',$modu))
          {
               $sql_info.='left join item0 on '.$modu.'_item = item_codigo ';
//                $sql_info=calc_maes($modu,$sql_info);
               if(!empty($item)){$xwhere1.='and '.$modu.'_item like "'.$item.'%" ';}
          }
       
          
          
          if(mb_stristr2('.item.ibod.ivta.igas.serv',$modu))
          {
               $sql_info.='left join lsub0 on '.$modu.'_lsub = lsub_codigo ';
               $sql_info.='left join mrca0 on '.$modu.'_mrca = mrca_codigo ';
               
               $sql_info.='left join clor0 on '.$modu.'_clor = clor_codigo ';
               $sql_info.='left join tlla0 on '.$modu.'_tlla = tlla_codigo ';
            
//                if($cfg_empr=='hg01')
               if($_SESSION['empr_farmacia']==1)
               {$sql_info.='left join rgsn0 on '.$modu.'_rgsn = rgsn_codigo ';}
               $sql_info=calc_maes($modu,$sql_info);
               
               
               if(!empty($lsub)){$xwhere1.='and '.$modu.'_lsub like "'.trim($lsub).'%" ';}
          }

          if(mb_stristr2('.pduc',$modu))
          {
               $sql_info.='left join proc0 on '.$modu.'_proc = proc_codigo ';
               
               if(!empty($proc)){$xwhere1.='and '.$modu.'_proc like "'.trim($proc).'%" ';}
          }

          if(mb_stristr2('.mdvh',$modu))
          {
//                $sql_info.='left join proc0 on '.$modu.'_proc = proc_codigo ';
               
//                if(!empty($proc)){$xwhere1.='and '.$modu.'_proc like "'.trim($proc).'%" ';}
               $sql_info=calc_maes($modu,$sql_info);
          }

          if(mb_stristr2('.vinn',$modu))
          {
               $sql_info.='left join plvh0 on '.$modu.'_plvh = plvh_codigo ';
               $sql_info.='left join vwmi0 on left('.$modu.'_codigo,3) = vwmi_codigo ';
               
//                if(!empty($proc)){$xwhere1.='and '.$modu.'_proc like "'.trim($proc).'%" ';}
          }

          if(mb_stristr2('.plvh',$modu))
          {
               $sql_info.='left join vinn0 on '.$modu.'_vinn = vinn_codigo ';
//                $sql_info.='left join vwmi0 on left('.$modu.'_codigo,3) = vwmi_codigo ';
//                $sql_info.='left join proc0 on '.$modu.'_proc = proc_codigo ';
               
//                if(!empty($proc)){$xwhere1.='and '.$modu.'_proc like "'.trim($proc).'%" ';}
          }

          if(mb_stristr2('.tcvh',$modu))
          {
               $sql_info.='left join clvh0 on '.$modu.'_clvh = clvh_codigo ';
               $sql_info.='left join item0 on '.$modu.'_item = item_codigo ';
               $sql_info.='left join lsub0 on item_lsub = lsub_codigo ';
               
               {$xwhere1.='and item_ttrf like "C%" ';}
               if(!empty($clvh)){$xwhere1.='and '.$modu.'_clvh like "'.trim($clvh).'" ';}
               if(!empty($item)){$xwhere1.='and '.$modu.'_item like "'.trim($item).'" ';}
//                if(!empty($mrca)){$xwhere1.='and mdvh_mrca like "'.trim($mrca).'" ';}
//                if(!empty($anio)){$xwhere1.='and mdvh_anio like "'.trim($anio).'" ';}
               
               
               
          }

          if(mb_stristr2('.mrca',$modu))
          {
               $sql_info.='left join pais0 on '.$modu.'_pais = pais_codigo ';
//                $sql_info.='left join item0 on '.$modu.'_item = item_codigo ';
          }

          if(mb_stristr2('.pmdo',$modu))
          {
               $sql_info.='left join clvh0 on '.$modu.'_clvh = clvh_codigo ';
//                $sql_info.='left join item0 on '.$modu.'_item = item_codigo ';
//                $sql_info.='left join lsub0 on item_lsub = lsub_codigo ';
               
//                {$xwhere1.='and item_ttrf like "T%" ';}
               if(!empty($clvh)){$xwhere1.='and '.$modu.'_clvh like "'.trim($clvh).'" ';}
//                if(!empty($item)){$xwhere1.='and '.$modu.'_item like "'.trim($item).'" ';}
               if(!empty($mrca)){$xwhere1.='and '.$modu.'_mrca like "'.trim($mrca).'" ';}
//                if(!empty($anio)){$xwhere1.='and mdvh_anio like "'.trim($anio).'" ';}
          }

          
          if(mb_stristr2('.tmpr',$modu))
          {
               $sql_info.='left join mdvh0 on '.$modu.'_mdvh = mdvh_codigo ';
               $sql_info.='left join item0 on '.$modu.'_item = item_codigo ';
               $sql_info.='left join lsub0 on item_lsub = lsub_codigo ';
               
               {$xwhere1.='and item_ttrf like "T%" ';}
               if(!empty($mdvh)){$xwhere1.='and '.$modu.'_mdvh like "'.trim($mdvh).'" ';}
               if(!empty($item)){$xwhere1.='and '.$modu.'_item like "'.trim($item).'" ';}
               if(!empty($mrca)){$xwhere1.='and mdvh_mrca like "'.trim($mrca).'" ';}
               if(!empty($anio)){$xwhere1.='and mdvh_anio like "'.trim($anio).'" ';}
          }

          
          
          if(mb_stristr2('.ppdd',$modu))
          {
          }

          if(mb_stristr2('.lreu',$modu))
          {
               $xwhere1.='and '.$modu.'_cong like "'.$_SESSION['cong_codigo'].'" ';
          }

          if(mb_stristr2('.trty',$modu))
          {
//                if(!empty($grupo))
               {$xwhere1.='and '.$modu.'_cong like "'.$_SESSION['cong_codigo'].'" ';}
               if(!empty($grupo)){$xwhere1.='and '.$modu.'_grupo like "'.$grupo.'" ';}
               if(!empty($sector)){$xwhere1.='and '.$modu.'_sector like "'.$sector.'%" ';}
          }

          if(mb_stristr2('.vhcl.plvh',$modu))
          {
               $sql_info.='left join enti0 on '.$modu.'_enti = enti_codigo ';
               $sql_info.='left join item0 on '.$modu.'_item = item_codigo ';
               $sql_info=calc_maes($modu,$sql_info);
               if(!empty($clase)){$xwhere1.='and '.$modu.'_clase like "%'.$clase.'%" ';}
               if(!empty($mrca)){$xwhere1.='and '.$modu.'_mrca like "'.$mrca.'%" ';}
               if(!empty($anio)){$xwhere1.='and '.$modu.'_anio like "'.$anio.'%" ';}
               if(!empty($clor)){$xwhere1.='and '.$modu.'_clor like "'.$clor.'%" ';}
          }
       
          if(mb_stristr2('.nrol',$modu))
          {
               $sql_info.='left join enti0 on '.$modu.'_enti = enti_codigo ';
               $sql_info.='left join curs0 on '.$modu.'_curs = curs_codigo ';
               $sql_info.='left join psco0 on curs_psco = psco_codigo ';
               if(!empty($enti)){$xwhere1.='and '.$modu.'_enti like "'.$enti.'%" ';}
               if(!empty($curs)){$xwhere1.='and '.$modu.'_curs like "'.$curs.'%" ';}
               if(!empty($psco)){$xwhere1.='and psco_codigo like "'.$psco.'" ';}
          }
          
          if(mb_stristr2('.curs',$modu))
          {
               $sql_info.='left join psco0 on '.$modu.'_psco = psco_codigo ';
//                $sql_info.='left join curs0 on '.$modu.'_curs = curs_codigo ';
          }
          
          
          if(mb_stristr2('.imgi',$modu))
          {
//                $sql_info.='left join liga0 on '.$modu.'_liga = liga_codigo ';
//                $sql_info.='where ifnull('.$modu.'_status,"")!="X" ';
//                $sql_info.='and '.$modu.'_codigo!="" ';
//                if(!empty($liga)){$sql_info.='and '.$modu.'_liga="'.$liga.'" ';}
               if(!empty($item)){$xwhere1.='and '.$modu.'_item like "'.$item.'%" ';}
          }
       
          
          if(mb_stristr2('.docu',$modu))
          {
//                $sql_info.='left join liga0 on '.$modu.'_liga = liga_codigo ';
//                $sql_info.='where ifnull('.$modu.'_status,"")!="X" ';
//                $sql_info.='and '.$modu.'_codigo!="" ';
//                if(!empty($liga)){$sql_info.='and '.$modu.'_liga="'.$liga.'" ';}
               if(!empty($dtabla)){$xwhere1.='and '.$modu.'_tabla like "'.$dtabla.'%" ';}
          }
       
          
          
          if(mb_stristr2('.mtch',$modu))
          {
               $sql_info.='left join liga0 on '.$modu.'_liga = liga_codigo ';
               $sql_info.='left join torn0 on '.$modu.'_torn = torn_codigo ';
               $sql_info.='left join team0 on '.$modu.'_team1 = team_codigo ';
               $sql_info.='left join sqad0 on '.$modu.'_team2 = sqad_codigo ';
//                $sql_info=calc_maes($modu,$sql_info);
//                $sql_info.='where '.$modu.'_ifnull(status,"")!="X" ';
//                $sql_info.='where ifnull('.$modu.'_status,"")!="X" ';
//                $sql_info.='and '.$modu.'_codigo!="" ';
//                if(!empty($fecha)){$sql_info.='and '.$modu.'_fecha="'.$fecha.'" ';}
               if(!empty($fecha1) and !empty($fecha2))
               {
//                $sql_info.='and '.$modu.'_fecha>="'.$fecha1.'" ';
//                $sql_info.='and '.$modu.'_fecha>="'.$fecha1.'" ';
               $xwhere1.='and '.$modu.'_fecha<="'.$fecha2.'" ';
               $xwhere1.='and '.$modu.'_fecha<="'.$fecha2.'" ';
               }
//                if(!empty($liga)){$sql_info.='and '.$modu.'_liga="'.$liga.'" ';}
//                if(!empty($torn)){$sql_info.='and '.$modu.'_torn="'.$torn.'" ';}
//                if(!empty($jornada)){$sql_info.='and '.$modu.'_jornada="'.$jornada.'" ';}
               if(!empty($liga)){$xwhere1.='and '.$modu.'_liga="'.$liga.'" ';}
               if(!empty($torn)){$xwhere1.='and '.$modu.'_torn="'.$torn.'" ';}
               if(!empty($jornada)){$xwhere1.='and '.$modu.'_jornada="'.$jornada.'" ';}
               
          }

          if(mb_stristr2('.rgsn',$modu))
          {
//                $sql_info.='left join liga0 on '.$modu.'_liga = liga_codigo ';
//                $sql_info.='where ifnull('.$modu.'_status,"")!="X" ';
//                $sql_info.='and '.$modu.'_codigo!="" ';
//                if(!empty($liga)){$sql_info.='and '.$modu.'_liga="'.$liga.'" ';}
               if(!empty($codpre)){$xwhere1.='and '.$modu.'_codpre like "%'.$codpre.'%" ';}
               if(!empty($pcom)){$xwhere1.='and '.$modu.'_codpre like "%'.$pcom.'%" ';}
               if(!empty($ffar)){$xwhere1.='and '.$modu.'_formaf like "%'.$ffar.'%" ';}
          }
       
          if(mb_stristr2('.torn',$modu))
          {
               $sql_info.='left join liga0 on '.$modu.'_liga = liga_codigo ';
//                $sql_info.='where ifnull('.$modu.'_status,"")!="X" ';
//                $sql_info.='and '.$modu.'_codigo!="" ';
//                if(!empty($liga)){$sql_info.='and '.$modu.'_liga="'.$liga.'" ';}
               if(!empty($liga)){$xwhere1.='and '.$modu.'_liga="'.$liga.'" ';}
          }
       
//           if(mb_stristr2('.liga.nive.team.posi',$modu))
          if(mb_stristr2('.liga.nive.team.posi',$modu))
          {
//                $sql_info.='left join liga0 on '.$modu.'_liga = liga_codigo ';
//                $sql_info.='where ifnull('.$modu.'_status,"")!="X" ';
//                $sql_info.='and '.$modu.'_codigo!="" ';
//                if(!empty($liga)){$sql_info.='and '.$modu.'_liga="'.$liga.'" ';}
               if(!empty($liga)){$xwhere1.='and '.$modu.'_liga="'.$liga.'" ';}
          }
       
          if(mb_stristr2('.adrs.user.usrt',$modu))
          {
//                $sql_info=calc_maes($modu,$sql_info);
//                $sql_info.='where ifnull('.$modu.'_status,"")!="X" ';
//                $sql_info.='and '.$modu.'_codigo!="" ';
//                if(!empty($enti)){$sql_info.='and '.$modu.'_enti like "'.substr($enti,0,10).'%" ';}
//                if(!empty($iuser)){$sql_info.='and '.$modu.'_iuser like "'.$iuser.'" ';}
               if(!empty($enti)){$xwhere1.='and '.$modu.'_enti like "'.substr($enti,0,10).'%" ';}
               if(!empty($iuser)){$xwhere1.='and '.$modu.'_iuser like "'.$iuser.'" ';}
          }


          if(!mb_stristr2($sql_info,'where'))
          {
//                $sql_info.='where ifnull('.$modu.'_status,"")!="X" ';
//                $sql_info.='and '.$modu.'_codigo!="" ';
          }
          
          if(!empty($codigo))
          {
//                echo '*/*/*/*/*'.$modu;
//                echo '*/*/*/*/*'.$codigo;
               if(mb_stristr2('.user',$modu) )
               {$xwhere1.='and '.$modu.'_codigo like "'.$codigo.'" ';}
               else
               {$xwhere1.='and '.$modu.'_codigo like "'.$codigo.'%" ';}
          }
               
          
//           {$sql_info.='and '.$modu.'_codigo like "'.$codigo.'%" ';}
          
          if(!empty($buscar))
          {
               $buscar=alltrim($buscar);
               $buscararray=explode(' ',$buscar);
//                var_dump2($buscararray);
               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);}
                    $xwhere1.='and (';
                    $xwhere1.=$modu.'_descri like"%'.$buscaidx.'%" ';
                    $xwhere1.=' or '.$modu.'_codigo like"%'.$buscaidx.'%" ';
                    
                    if(mb_stristr2('.enti.clie.prov.pcnt,vndd',$modu))
                    {
                         $xwhere1.=' or '.$modu.'_razsoc like"%'.$buscaidx.'%" ';
                         $xwhere1.=' or '.$modu.'_nomcom like"%'.$buscaidx.'%" ';
                    }
                    if(mb_stristr2('.item.prXod.serv.ivta',$modu))
                    {
//                          $xwhere1.=' or '.$modu.'_descri like"%'.$buscaidx.'%" ';
                         $xwhere1.=' or '.$modu.'_descri2 like"%'.$buscaidx.'%" ';
                         $xwhere1.=' or '.$modu.'_codigov like"%'.$buscaidx.'%" ';
                         $xwhere1.=' or '.$modu.'_marca like"%'.$buscaidx.'%" ';
                    }
                    if(mb_stristr2('.rgsn',$modu))
                    {
//                          $xwhere1.=' or '.$modu.'_descri like"%'.$buscaidx.'%" ';
                         $xwhere1.=' or '.$modu.'_codpre like"%'.$buscaidx.'%" ';
                         $xwhere1.=' or '.$modu.'_precom like"%'.$buscaidx.'%" ';
                         $xwhere1.=' or '.$modu.'_formaf like"%'.$buscaidx.'%" ';
                    }
                    $xwhere1.=')';
                    
//                     echo '==='.$xwhere1;
               }
               
               
               
          }
          
          
          $sql_info.=$xwhere1;
          $sql_info.=$xorderby1 ;
          
          if(!empty($codigo))
          {$sql_info.='order by '.$modu.'_codigo ';}
          

     }
     
     
     if(substr($info,0,2)!='mt')
     {
          
          
          $sql_info ='select * ';
          $sql_info.='from '.$modu1.' ';
          $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
          $sql_info.='where ifnull(status,"")!="X" ';
          $sql_info.='and ifnull(status2,"")!="X" ';
          $sql_info.='and ifnull(cant,0)!=0 ';
          
          $sql_info.='and fecha>="'.$fecha1.'" ';
          $sql_info.='and fecha<="'.$fecha2.'" ';
          
          if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
          if(!empty($item))
          {
               if(!empty($sql_busca_item))
               {$sql_info.=$sql_busca_item;}
               else
               {$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
          }
          
          
          
          
          if(mb_stristr2('.acce',$modu))
          {
               if(!empty($docd)){$modulo='';}
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join docd0 on docum=docd_codigo ';
               $sql_info.='left join user0 on numero=user_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               $sql_info.='where ifnull(status,"")!="X" ';
               $sql_info.='and user_status not in("I","X") ';
               $sql_info.='and docu_status not in("I","X") ';
               if(!empty($modulo)){$sql_info.='and (docd_tabla) like "'.($modulo).'%" ';}
               if(!empty($docd)){$sql_info.='and docum = "'.$docd.'" ';}
               if(!empty($usuario)){$sql_info.='and numero = "'.$usuario.'" ';}
//                if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or user_descri like "%'.($enti).'%") ';}
               if($info!='atblog')
               {
//                if(!empty($asub)){$sql_info.='and asub like "'.$asub.'%" ';}
               }
     //           if(!empty($zsub)){$sql_info.='and zsub like "'.$zsub.'%" ';}
     //           if(!empty($tpro)){$sql_info.='and enti_tpro like "'.$tpro.'%" ';}
     //           if(!empty($item)){$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
     //           if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}

               $xgroupby1='group by idnum1 ';
               if($info=='acdeta'){$xgroupby1='group by idnum1,iddet ';}
               $sql_info.=$xgroupby1;
               
               
               $xorderby1='orderby by idnum1 ';
               if($info=='acdeta'){$xorderby1='order by docd_tipo,docd_tabla, docum, idnum1 ';}
               $sql_info.=$xorderby1;
          }
          
          

          
          if(mb_stristr2('.btcr.avso',$modu))
          {
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; }
               if(!empty($docu) and !empty($ptoe) and !empty($numero))
               {
                    $numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; 
               }
               
//                if($info=='bthist'){$fecha1=date('Y-m-d',strtotime('-30 month')); $fecha2=date('Y-m-d',strtotime('-1 day'));}
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join user0 on enti=user_cedula ';
               $sql_info.='left join plvh0 on plvh=plvh_codigo ';    
               $sql_info.='left join enti0 on left(enti,10)=left(enti_codigo,10) ';    
               // propiedades
               $sql_info.='left join ppdd0 on left(ppdd,10)=left(ppdd_codigo,10) ';    
               $sql_info.='left join stat0 on status=stat_codigo ';
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               
//                if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
               if(!empty($enti) or !empty($plvh))
               {$sql_info.='and (enti like "'.substr($enti,0,10).'%" or plvh like "'.$plvh.'")';}
               
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               
               if(!empty($ptoe)){$sql_info.='and ptoe like "'.$ptoe.'%" ';}

               if($info!='btblog')
               {
                    if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
                    if(!empty($numero1)){$sql_info.='and numero*1 >= "'.(int)$numero1.'" ';}                    
                    if(!empty($numero2)){$sql_info.='and numero*1 <= "'.(int)$numero2.'" ';}          
               }
               


               $xgroupby1='group by idnum1 ';
//                if($info=='btblog'){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
               
//                     $xorderby1='';
//                     if($info=='bthist'){$xorderby1='order by fecha desc ';}
//                     $sql_info.=$xorderby1;
               
          }

          
          
          
          if(mb_stristr2('.plan',$modu))
          {
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; }
               if(!empty($docu) and !empty($ptoe) and !empty($numero))
               {
                    $numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; 
               }
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join user0 on enti=user_cedula ';
               $sql_info.='left join enti0 on left(enti,10)=left(enti_codigo,10) ';    
               $sql_info.='left join empl0 on empl=empl_codigo ';    
               $sql_info.='left join asub0 on asub=asub_codigo ';
               $sql_info.='left join lsub0 on asub_lsub=lsub_codigo ';
               $sql_info.='left join atvd0 on concat(left(asub,4),"-0000")=atvd_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
//                $sql_info.='and fecha>="'.$fecha1.'" ';
//                $sql_info.='and fecha<="'.$fecha2.'" ';
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
//                if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.($enti).'%") ';}
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
               
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               if(!empty($ptoe)){$sql_info.='and ptoe like "'.$ptoe.'%" ';}

               if(!empty($numero1)){$sql_info.='and numero*1 >= "'.(int)$numero1.'" ';}                    
               if(!empty($numero2)){$sql_info.='and numero*1 <= "'.(int)$numero2.'" ';}          
               
               if(!empty($asub)){$sql_info.='and asub like "'.substr($asub,0,4).'%" ';}
               if(!empty($empr1)){$sql_info.='and empr1 = "'.$empr1.'" ';}
               
               if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
               {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}
               
//                if($info!='plblog')
//                {
//                if(!empty($asub)){$sql_info.='and asub like "'.$asub.'%" ';}
//                }
     //           if(!empty($zsub)){$sql_info.='and zsub like "'.$zsub.'%" ';}
     //           if(!empty($tpro)){$sql_info.='and enti_tpro like "'.$tpro.'%" ';}
     //           if(!empty($item)){$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
     //           if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}


               $xgroupby1='group by idnum1 ';
               if($info=='atblog'){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
          }

          
          if(mb_stristr2('.atvd',$modu))
          {
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join user0 on enti=user_cedula ';
               $sql_info.='left join enti0 on left(clie,10)=left(enti_codigo,10) ';    
//                $sql_info.='left join enti0 on clie=enti_codigo ';    
               $sql_info.='left join asub0 on asub=asub_codigo ';
               $sql_info.='left join lsub0 on asub_lsub=lsub_codigo ';
               $sql_info.='left join atvd0 on concat(left(asub,4),"-0000")=atvd_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               $sql_info.='and fecha>="'.$fecha1.'" ';
               $sql_info.='and fecha<="'.$fecha2.'" ';
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or user_descri like "%'.($enti).'%") ';}
               if($info!='atblog')
               {
               if(!empty($asub)){$sql_info.='and asub like "'.$asub.'%" ';}
               }
     //           if(!empty($zsub)){$sql_info.='and zsub like "'.$zsub.'%" ';}
     //           if(!empty($tpro)){$sql_info.='and enti_tpro like "'.$tpro.'%" ';}
     //           if(!empty($item)){$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
     //           if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}

               $xgroupby1='group by idnum1 ';
               if($info=='atblog'){$xgroupby1='group by idnum1,iddet ';}
               $sql_info.=$xgroupby1;
          }



          if(mb_stristr2('.guia',$modu))
          {
//                if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';}
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';$ptoe='';}
               if(!empty($docu) and !empty($ptoe) and !empty($numero))
               {
                    $numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';
               }
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join enti0 on enti=enti_codigo ';
               $sql_info.='left join item0 on item=item_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               
//                $sql_info.='left join vhcl0 on vhcl=vhcl_codigo ';
               $sql_info.='left join plvh0 on plvh=plvh_codigo ';
               $sql_info.='left join codt0 on cedula1=codt_codigo ';
               
               
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
//                $sql_info.='and fecha>="'.$fecha1.'" ';
//                $sql_info.='and fecha<="'.$fecha2.'" ';
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
//                if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.($enti).'%") ';}
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
               
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               
               if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               if(!empty($ptoe)){$sql_info.='and ptoe like "'.$ptoe.'%" ';}

               if(!empty($numero1)){$sql_info.='and numero*1 >= "'.(int)$numero1.'" ';}                    
               if(!empty($numero2)){$sql_info.='and numero*1 <= "'.(int)$numero2.'" ';}          
   
               if(!empty($vhcl)){$sql_info.='and vhcl like "%'.$vhcl.'%" ';}
               if(!empty($plvh)){$sql_info.='and plvh like "%'.$plvh.'%" ';}
               if(!empty($cntn)){$sql_info.='and cntn like "%'.$cntn.'%" ';}
               if(!empty($codt)){$sql_info.='and cedula1 like "'.substr($codt,0,10).'%" ';}

               if($stce==1){$sql_info.='and ifnull(autorfec,"")!="" ';}
               if($stce==2){$sql_info.='and ifnull(autorfec,"")="" ';}
               
               
               if(!empty($stat)){$sql_info.='and status = "'.$stat.'" ';}

               if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
               {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}
               
               
//                if(!empty($asub)){$sql_info.='and asub like "'.substr($asub,0,4).'%" ';}
//                if(!empty($empr1)){$sql_info.='and empr1 = "'.$empr1.'" ';}

               $xgroupby1='group by idnum1 ';
               if($info=='gudeta'){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
          }


          if(mb_stristr2('.rmed',$modu))
          {
//                if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';}
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';$ptoe='';}
               if(!empty($docu) and !empty($ptoe) and !empty($numero))
               {
                    $numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';
               }
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join enti0 on enti=enti_codigo ';
               
               $sql_info.='left join ciex0 on ciex=ciex_codigo ';
               $sql_info.='left join cie20 on ciex2=cie2_codigo ';
               $sql_info.='left join cie30 on ciex3=cie3_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               
               
               
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
//                $sql_info.='and fecha>="'.$fecha1.'" ';
//                $sql_info.='and fecha<="'.$fecha2.'" ';
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
//                if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.($enti).'%") ';}
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
               if(!empty($pcnt)){$sql_info.='and (enti like "'.substr($pcnt,0,10).'%" or enti_descri like "%'.$pcnt.'%" or enti_razsoc like "%'.$pcnt.'%")';}
               
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               
               if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               if(!empty($ptoe)){$sql_info.='and ptoe like "'.$ptoe.'%" ';}

               if(!empty($numero1)){$sql_info.='and numero*1 >= "'.(int)$numero1.'" ';}                    
               if(!empty($numero2)){$sql_info.='and numero*1 <= "'.(int)$numero2.'" ';}          
   
//                if(!empty($codt)){$sql_info.='and cedula1 like "'.substr($codt,0,10).'%" ';}
               
               if(!empty($stat)){$sql_info.='and status = "'.$stat.'" ';}
               if(!empty($ciex)){$sql_info.='and ciex = "'.$ciex.'" ';}
               if(!empty($mdco)){$sql_info.='and mdco = "'.$mdco.'" ';}

               if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
               {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}

               
//                echo '*/*/*/*'.$clave;
               if($clave_print_ok!=1)
               if($user0['user_tipo']!='sist')
               {$sql_info.='and iuser = "'.$user0['user_codigo'].'" ';}
               
               
//                if(!empty($asub)){$sql_info.='and asub like "'.substr($asub,0,4).'%" ';}
//                if(!empty($empr1)){$sql_info.='and empr1 = "'.$empr1.'" ';}

               $xgroupby1='group by idnum1 ';
               if($info=='rmdeta'){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
          }


          

          if(mb_stristr2('.opro',$modu))
          {
//                if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';}
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; $ptoe='';}
               if(!empty($docu) and !empty($ptoe) and !empty($numero))
               {
                    $numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';
               }
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join enti0 on enti=enti_codigo ';
               $sql_info.='left join item0 on item=item_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               
               
               if($empr0['empr_inve']==1)
               {
                    if(mb_stristr2('.opresu.opdeta.opride.opdein',$info))     
                    {
                         $fecha1i=date('Y-m-01',strtotime($fecha1,'-2 month'));
                         $fecha2i=date('Y-m-t',strtotime($fecha2,'+4 month'));
                         
                         if(mb_stristr2('.opride',$info))
                         {
                              $fecha1i=date('2010-01-01');
                              $fecha2i=date('Y-m-d');
                         }
                         
                         $sql_infa ='create temporary table invefact ';
                         $sql_infa.='select  ';
                         $sql_infa.='idnum1 as inve_idnum1, ';
                         $sql_infa.='docu as inve_docu, ';
                         $sql_infa.='ptoe as inve_ptoe, ';
                         $sql_infa.='bode as inve_bode, ';
                         $sql_infa.='numero as inve_numero, ';
                         $sql_infa.='enti as inve_enti, ';
                         $sql_infa.='item as inve_item, ';
                         $sql_infa.='numfac, ';
                         $sql_infa.='sum(cant)as inve_cant, ';
                         $sql_infa.='sum(cant*puni)as inve_cost, ';
                         $sql_infa.='min(fecha) as inve_fecha ';
                         $sql_infa.='from inve1 left join inve2 on idnum1=idnum2 ';
                         $sql_infa.='where ifnull(status,"")not in("O","X") ';
                         $sql_infa.='and fecha>="'.$fecha1i.'" ';
                         $sql_infa.='and fecha<="'.$fecha2i.'" ';
                         $sql_infa.='and numfac*1>0 ';
                         
                         if((int)$enti!=0)
                         {$sql_infa.='and enti like "'.substr($enti,0,10).'%" ';}
                         
                         if(substr($info,0,2)=='OP')
                         {$sql_infa.='and docu in("EB19") ';}
                         
                         if(substr($info,2,4)=='resu')
                         {$sql_infa.='group by enti,numfac ';}
                         if(substr($info,2,4)=='deta')
                         {$sql_infa.='group by enti,item,numfac ';}
                         if(substr($info,2,4)=='ride')
                         {$sql_infa.='group by enti,item,numfac ';}
                         if(substr($info,2,4)=='dein')
                         {$sql_infa.='group by enti,item,numfac ';}
                         
                         $res_infa=msql_query($sql_infa,$link_data);
//                                    echo '=== '.$sql_infa;

                    }
                    
                    if(mb_stristr2('.opresu',$info))     
                    {$sql_info.='left join invefact on left(enti,10)=left(inve_enti,10) and numero*1=numfac*1 ';}
                    if(mb_stristr2('.opdeta',$info))     
                    {$sql_info.='left join invefact on left(enti,10)=left(inve_enti,10) and numero*1=numfac*1 and item=inve_item '; }
                    if(mb_stristr2('.opride.opdein',$info))     
                    {$sql_info.='left join invefact on left(enti,10)=left(inve_enti,10) and numero*1=numfac*1 and item=inve_item ';}
               }

               
               
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
//                $sql_info.='and fecha>="'.$fecha1.'" ';
//                $sql_info.='and fecha<="'.$fecha2.'" ';
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
//                if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.($enti).'%") ';}
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
               
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               if(!empty($ptoe)){$sql_info.='and ptoe like "'.$ptoe.'%" ';}

               if(!empty($numero1)){$sql_info.='and numero*1 >= "'.(int)$numero1.'" ';}                    
               if(!empty($numero2)){$sql_info.='and numero*1 <= "'.(int)$numero2.'" ';}          
   
//                if(!empty($vhcl)){$sql_info.='and vhcl like "'.$vhcl.'%" ';}
//                if(!empty($cntn)){$sql_info.='and cntn like "'.$cntn.'%" ';}
//                if(!empty($codt)){$sql_info.='and cedula1 like "'.substr($codt,0,10).'%" ';}
               if(!empty($csub1)){$sql_info.='and csub1 like "'.$csub1.'%" ';}
               
               if(!empty($stat)){$sql_info.='and status = "'.$stat.'" ';}
               
               if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
               {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}
               
//                if(!empty($asub)){$sql_info.='and asub like "'.substr($asub,0,4).'%" ';}
//                if(!empty($empr1)){$sql_info.='and empr1 = "'.$empr1.'" ';}

               $xgroupby1='group by idnum1 ';
               if($info=='opdeta'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='opitem'){$xgroupby1='group by item ';}
               if($info=='opdein'){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
          }

          if(mb_stristr2('.hcli',$modu))
          {
//                if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';}
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; $ptoe='';}
               if(!empty($docu) and !empty($ptoe) and !empty($numero))
               {
                    $numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';
               }
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join enti0 on enti=enti_codigo ';
               $sql_info.='left join item0 on item=item_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               
               $sql_info=calc_fields($modu,$sql_info);
//                $sql_info=calc_maes('enti',$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
//                if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
//                if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               
//                if(!mb_stristr2('.det1.det2.ride.blog',substr($info,2,4)))
               if(!mb_stristr2('.det1.det2.ride',substr($info,2,4)))
               {
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
               if(!empty($pcnt)){$sql_info.='and (enti like "'.substr($pcnt,0,10).'%" or enti_descri like "%'.$pcnt.'%" or enti_razsoc like "%'.$pcnt.'%" or enti_nomcom like "%'.$pcnt.'%")';}
               }
               if(!mb_stristr2('.blog',substr($info,2,4)))
               {
               if(!empty($numero1)){$sql_info.='and numero*1 >= "'.(int)$numero1.'" ';}                    
               if(!empty($numero2)){$sql_info.='and numero*1 <= "'.(int)$numero2.'" ';}          
               if(!empty($fase)){$sql_info.='and fase like "'.$fase.'%" ';}
               }

               
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               if(!empty($ptoe)){$sql_info.='and ptoe like "'.$ptoe.'%" ';}

   
               if(!empty($csub1)){$sql_info.='and csub1 like "'.$csub1.'%" ';}
               
               if(!empty($stat)){$sql_info.='and status = "'.$stat.'" ';}
               

               $xgroupby1='group by idnum1 ';
               if($info=='hcdeta'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='hchcli'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='hcitem'){$xgroupby1='group by item ';}
               if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
          }

          
          if(mb_stristr2('.otbj',$modu))
          {
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';}
//                if(!empty($docu) and !empty($ptoe) and !empty($numero))
               if(!empty($docu) and !empty($ptov) and !empty($numero))
               {
                    $numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';
               }
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join enti0 on enti=enti_codigo ';
               $sql_info.='left join plvh0 on plvh=plvh_codigo ';
               $sql_info.='left join vinn0 on vinn=vinn_codigo ';
               $sql_info.='left join item0 on item=item_codigo ';
               $sql_info.='left join mcnc0 on mcnc=mcnc_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(!mb_stristr2('.det1.det2.ride',substr($info,2,4)))
               {
                    if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
                    if(!empty($pcnt)){$sql_info.='and (enti like "'.substr($pcnt,0,10).'%" or enti_descri like "%'.$pcnt.'%" or enti_razsoc like "%'.$pcnt.'%" or enti_nomcom like "%'.$pcnt.'%")';}
               }
               
               if(!mb_stristr2('.blog',substr($info,2,4)))
               {
                    if(!empty($numero1)){$sql_info.='and numero*1 >= "'.(int)$numero1.'" ';}                    
                    if(!empty($numero2)){$sql_info.='and numero*1 <= "'.(int)$numero2.'" ';}          
//                     if(!empty($fase)){$sql_info.='and fase like "'.$fase.'%" ';}
               }

               
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               if(!empty($ptoe)){$sql_info.='and ptoe like "'.$ptoe.'%" ';}
               if(!empty($plvh)){$sql_info.='and plvh like "%'.$plvh.'%" ';}

   
               if(!empty($csub1)){$sql_info.='and csub1 like "'.$csub1.'%" ';}
               
               if(!empty($stat)){$sql_info.='and status = "'.$stat.'" ';}
               

               $xgroupby1='group by idnum1 ';
               if($info=='hcdeta'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='hchcli'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='hcitem'){$xgroupby1='group by item ';}
               if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
          }

          
          
          if(mb_stristr2('.jwib',$modu))
          {

//                if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';}
//                if(!empty($docu) and !empty($ptoe) and !empty($numero))
//                {
//                     $numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';
//                }
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
//                $sql_info.='left join enti0 on enti=enti_codigo ';
//                $sql_info.='left join item0 on item=item_codigo ';
               $sql_info.='left join cong0 on cong=cong_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               
               $sql_info=calc_fields($modu,$sql_info);
//                $sql_info=calc_maes('enti',$sql_info);
               $sql_info.='where ifnull(status,"") not in("O","X") ';
               $sql_info.='and cong = "'.$cong.'" ';
//                $sql_info.='and ifnull(nombre1,"")!="" ';
               $sql_info.='and length(ifnull(nombre1,""))>=5 ';
//                if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               
               if(substr($info,0,2)=='mm'){$sql_info.='and docu like "VM3%" ';}
               if(substr($info,0,2)=='vc'){$sql_info.='and docu like "VM%" and docu not like "VM3%" ';}
               if(substr($info,0,2)=='as'){$sql_info.='and docu like "AS1%" ';}
               

               
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               

   
               
               if(!empty($stat)){$sql_info.='and status = "'.$stat.'" ';}
               

               $xgroupby1='group by idnum1,iddet ';
//                if($info=='hcdeta'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='hchcli'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='hcitem'){$xgroupby1='group by item ';}
//                if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
          }

          if(mb_stristr2('.jwrv',$modu))
          {
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join cong0 on cong=cong_codigo ';
               $sql_info.='left join trty0 on trty=trty_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"") not in("O","X") ';
               
//                $sql_info.='and cong = "'.$cong.'" ';
               if(!empty($cong)){$sql_info.='and cong = "'.$cong.'" ';}
               
               $sql_info.='and length(ifnull(publicador,""))>=3 ';
               if(!empty($grupo)){$sql_info.='and trty_grupo = "'.$grupo.'" ';}
               if(!empty($trty)){$sql_info.='and trty like "'.$trty.'" ';}
               if(!empty($bloq)){$sql_info.='and bloq like "'.$bloq.'" ';}
               
//                if($info!='rvblog')
//                {if(!empty($ndpto)){$sql_info.='and ndpto like "'.$ndpto.'" ';}}
               
//                if($info!='rvblog')
               {if(!empty($ndpto)){$sql_info.='and ndpto like "'.$ndpto.'%" ';}}
               
               if($info=='rvblog')
               {$fecha1='2000-01-01';}
               
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               
               
               if(!empty($stat)){$sql_info.='and status = "'.$stat.'" ';}
               

               $xgroupby1='group by idnum1,iddet ';
//                if($info=='hcdeta'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='hchcli'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='hcitem'){$xgroupby1='group by item ';}
//                if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
          }

          if(mb_stristr2('.jwrt',$modu))
          {
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';$ptoe='';}
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join cong0 on cong=cong_codigo ';
               $sql_info.='left join trty0 on trty=trty_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"") not in("O","X") ';
               
               if(!empty($cong)){$sql_info.='and cong = "'.$cong.'" ';}
               
               $sql_info.='and length(ifnull(asignado,""))>=3 ';
               if(!empty($grupo)){$sql_info.='and trty_grupo = "'.$grupo.'" ';}
               if(!empty($sector)){$sql_info.='and trty_sector = "'.$sector.'" ';}
               if(!empty($trty)){$sql_info.='and trty like "'.$trty.'" ';}
//                if(!empty($bloq)){$sql_info.='and bloq like "'.$bloq.'" ';}
//                if($info!='atblog')
//                {if(!empty($ndpto)){$sql_info.='and ndpto like "'.$ndpto.'" ';}}
              
               
               
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               
               if(!empty($stat)){$sql_info.='and status = "'.$stat.'" ';}
               if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}
               

               $xgroupby1='group by idnum1,iddet ';
//                if($info=='hcdeta'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='hchcli'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='hcitem'){$xgroupby1='group by item ';}
//                if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
          }

          
          if(mb_stristr2('.jwmc',$modu))
          {

               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join cong0 on cong=cong_codigo ';
               $sql_info.='left join trty0 on trty=trty_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               
               $sql_info=calc_fields($modu,$sql_info);
//                $sql_info=calc_maes('enti',$sql_info);
               $sql_info.='where ifnull(status,"") not in("O","X") ';
               $sql_info.='and cong = "'.$cong.'" ';
//                if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               
               
               if(!empty($stat)){$sql_info.='and status = "'.$stat.'" ';}
               

               $xgroupby1='group by idnum1,iddet ';
//                $xgroupby1='group by idnum1 ';
               if($info=='jwmdc0'){$xgroupby1='group by idnum1 ';}
               if($info=='jwmdc1'){$xgroupby1='group by idnum1 ';}
//                if($info=='hchcli'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='hcitem'){$xgroupby1='group by item ';}
//                if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
//                if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}
               
               $sql_info.=$xgroupby1;
          }

          

          if(mb_stristr2('.vtas.clie.comp.prov.carr.lpro.dxco.dxpa',$modu))
          {
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; $ptoe='';}
               if(!empty($docu) and !empty($ptov) and !empty($numero)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; $vhcl=''; $plvh=''; $cntn=''; $stfin='';}
               if(!empty($docu) and !empty($ptoe) and !empty($numero))
               {
                    $numero1=$numero;$numero2=$numero;$fecha1='';$fecha2=''; $vhcl=''; $plvh=''; $cntn='';
                    if($modu=='guia'){$enti='';}
               }
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join enti0 on enti=enti_codigo ';
               $sql_info.='left join item0 on item=item_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               if(mb_stristr2('..comp.prov.lpro.',$modu))
               {$sql_info.='left join csub0 on csub=csub_codigo ';}
               
//                echo '<br>== '.date('H:i:s');
               if($inve=='true')
               if($empr0['empr_inve']==1)
               {
                    if(mb_stristr2('.vtresu.coresu.vtdeta.codeta.coride.vtride.vtdein.codein',$info))     
                    {
                         $sql_infa ='create view invefact1 as ';
                         $sql_infa.='select  ';
                         $sql_infa.='idnum1 as inve_idnum1, ';
                         $sql_infa.='docu as inve_docu, ';
                         $sql_infa.='ptoe as inve_ptoe, ';
                         $sql_infa.='bode as inve_bode, ';
                         $sql_infa.='numero as inve_numero, ';
                         $sql_infa.='enti as inve_enti, ';
                         $sql_infa.='item as inve_item, ';
                         $sql_infa.='numfac, ';
                         $sql_infa.='sum(cant)as inve_cant, ';
                         $sql_infa.='sum(cant*puni)as inve_cost, ';
                         $sql_infa.='min(fecha) as inve_fecha ';
                         $sql_infa.='from inve1 left join inve2 on idnum1=idnum2 ';
                         $sql_infa.='where ifnull(status,"")not in("O","X") ';
                         $sql_infa.='and numfac*1>0 ';
                         $sql_infa.='and docu in("IB10","IB15","EB12","EB10","EB15","IB12") ';
                         
                         $sql_infa.='group by enti,numfac ';
                         
                         
                         if($show_timei==1)
                         {echo '<br>== invefact =='.date('H:i:s');}
                         
                         if(mb_stristr2('.vtresu.coresu',$info))
                         {
                              $sql_drop='drop view invefact1';
                              $res_drop=msql_query($sql_drop,$link_data);
                              $res_infa=msql_query($sql_infa,$link_data);
                         }
                         else
                         {
                              $sql_infa.=',item ';
                              $sql_infa=str_replace('invefact1','invefact2',$sql_infa);
                              $sql_drop='drop view invefact2';
                              $res_drop=msql_query($sql_drop,$link_data);
                              $res_infa=msql_query($sql_infa,$link_data);
                         }
                         if($show_timei==1)
                         {echo '<br>== invefact =='.date('H:i:s');}
                         
                         
//                                    echo '=== '.$sql_infa;

                    }
                    
                    if(mb_stristr2('.vtresu.coresu',$info))     
                    {$sql_info.='left join invefact1 on left(enti,10)=left(inve_enti,10) and numero*1=numfac*1 ';}
//                     if(mb_stristr2('.vtdeta.codeta',$info))     
//                     {$sql_info.='left join invefact on left(enti,10)=left(inve_enti,10) and numero*1=numfac*1 and item=inve_item '; }
                    if(mb_stristr2('.vtride.coride.vtdein.codein',$info))     
                    {$sql_info.='left join invefact2 on left(enti,10)=left(inve_enti,10) and numero*1=numfac*1 and item=inve_item ';}
                    
                    
                    
               }
//                echo '<br>== '.date('H:i:s');

               if($modu=='carr')
               {$sql_info.='left join user0 on iuser=user_codigo ';}
               
               
               if($modu=='vtas' or $modu=='clie')
               {
                    $sql_info.='left join zsub0 on zsub=zsub_codigo ';
                    $sql_info.='left join ptov0 on ptov=ptov_codigo ';
                    $sql_info.='left join vndd0 on vndd=vndd_codigo ';
                    $sql_info.='left join tcli0 on enti_tcli=tcli_codigo ';
                    $sql_info.='left join lsub0 on item_lsub=lsub_codigo ';
                    // no habilitar - demora el query
//                     $sql_info.='left join rcdd0 on rcdd=rcdd_codigo ';
                    
                    if($mrca_ok==1)
                    {$sql_info.='left join mrca0 on item_mrca=mrca_codigo ';}
                    
                    if($clor_ok==1)
                    {$sql_info.='left join clor0 on item_clor=clor_codigo ';}
                    
                    if($tlla_ok==1)
                    {$sql_info.='left join tlla0 on item_tlla=tlla_codigo ';}
                    
//                     if($empr0['empr_automotriz']==1 or $empr0['empr_garage']==1)
//                     {$sql_info.='left join vhcl0 on vhcl=vhcl_codigo ';}
                    
                    if($empr0['empr_automotriz']==1 or $empr0['empr_garage']==1)
                    {$sql_info.='left join plvh0 on plvh=plvh_codigo ';}
                    
                    if($empr0['empr_farmacia']==1)
                    {$sql_info.='left join rgsn0 on item_rgsn=rgsn_codigo ';}
                    
//                     if($empr0['empr_garage']==1)
//                     {$sql_info.='left join vhcl0 on vhcl=vhcl_codigo ';}
                    
                    if($empr0['empr_veterinaria']==1)
                    {$sql_info.='left join pcnt0 on pcnt=pcnt_codigo ';}
                    
                    
               }
               
               if($rgsn_ok==2)
               if($info=='vtrpm1')
               {$sql_info.='left join rgsn0 on trim(item_rgsn)=trim(rgsn_codigo) ';}

               if($modu=='comp' or $modu=='prov' or $modu=='Xlpro')
               {
                    $sql_info.='left join fpag0 on fpag=fpag_codigo ';
                    $sql_info.='left join suco0 on suco=suco_codigo ';
                    $sql_info.='left join cair0 on cair=cair_codigo ';
                    $sql_info.='left join ctas0 on ctas=ctas_codigo ';
                    $sql_info.='left join tpro0 on enti_tpro=tpro_codigo ';
                    
                    if($empr0['empr_automotriz']==1 and $empr0['empr_impo']==1)
                    {$sql_info.='left join mdvh0 on mdvh=mdvh_codigo ';}
                    
               }

               
               

               if($modu=='dxco')
               {
//                     $sql_info.='left join zsub0 on enti_zsub=zsub_codigo ';
               }
               
               
               if($modu=='dxpa')
               {
//                     $sql_info.='left join enti0 on enti=enti_codigo ';
//                     $sql_info.='left join tpro0 on enti_tpro=tpro_codigo ';
               }
               
               
               
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info=calc_maes('enti',$sql_info);
               
//                if(($empr0['empr_automotriz']==1 or $empr0['empr_garage']==1) and mb_stristr2($sql_info,'vhcl0'))
//                {$sql_info=calc_maes('vhcl',$sql_info);}
               
               if(($empr0['empr_automotriz']==1 or $empr0['empr_garage']==1) and mb_stristr2($sql_info,'plvh0'))
               {$sql_info=calc_maes('plvh',$sql_info);}
               
               $sql_info.='where ifnull(status,"")!="X" ';
               if(!mb_stristr2('.resu.ride.res1.comi',substr($info,2,4)))
               {$sql_info.='and ifnull(status,"")!="O" ';}
               
//                if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               if(substr($info,-4)!='det2'){$sql_info.='and ifnull(cant,1)!=0 ';}
               
               if($info=='vtdet2')
               {
                    if($empr0['empr_ceuser']==1)
                    {$sql_info.='and vndd="'.$vndd.'" ';}
               }

               
               if($fautor=='true')
               {
               if(!empty($fecha1)){$sql_info.='and left(autorfec,10)>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and left(autorfec,10)<="'.$fecha2.'" ';}
               }
               else
               {
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               }               

               
               if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               if(!empty($ptov)){$sql_info.='and ptov like "'.$ptov.'%" ';}
               if(!empty($ptoe)){$sql_info.='and ptoe like "'.$ptoe.'%" ';}
               if(!empty($toenti)){$sql_info.='and toenti like "'.$toenti.'%" ';}
               if(!empty($fpag)){$sql_info.='and fpag = "'.$fpag.'" ';}
               if(!empty($suco)){$sql_info.='and suco = "'.$suco.'" ';}
               
               if(mb_stristr2('.comp.prov',$modu))
               {
//                if(!empty($enti)){$sql_info.='and enti like "'.$enti.'%" ';}
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}                    
               }

               
               
               if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
               {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}
               
               if(mb_stristr2('.vtas.clie.comp.prov',$modu))
               {
               if($tpago==1){$sql_info.='and ifnull(plazo,0)<=1 ';}
               if($tpago==2){$sql_info.='and ifnull(plazo,0)>=2 ';}
               }
               
               if($cchk==1){$sql_info.='and ifnull(cchk,0)=1 ';}
               if($cchk==2){$sql_info.='and ifnull(cchk,0)!=1 ';}
               
               
               if(!empty($pcom)){$sql_info.='and rgsn_codpre like "'.$pcom.'%" ';}

               
//                if(!empty($numero1)){$sql_info.='and numero*1 >= "'.(int)$numero1.'" ';}                    
//                if(!empty($numero2)){$sql_info.='and numero*1 <= "'.(int)$numero2.'" ';}          

               if($info=='prblog')
               {
                    if(!empty($enti)){$sql_info.='and enti like "'.substr($enti,0,10).'%" ';}
               }
               
               if($info=='lpblog')
               {
                    if(!empty($enti)){$sql_info.='and enti like "'.substr($enti,0,10).'%" ';}
               }
               
               
               
               if(!mb_stristr2('.blog',substr($info,2,4)))
               {
               if(!empty($numero1)){$sql_info.='and numero*1 >= "'.(int)$numero1.'" ';}                    
               if(!empty($numero2)){$sql_info.='and numero*1 <= "'.(int)$numero2.'" ';}
               }
               
               
//                if(!mb_stristr2($info,'blog'))
               if(!mb_stristr2('.det1.det2.ride.blog',substr($info,2,4)))
               {
                    if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
                    if(!empty($item)){$sql_info.='and (item like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
                    
                    if(!empty($vhcl)){$sql_info.='and vhcl like "%'.$vhcl.'%" ';}
                    if(!empty($plvh)){$sql_info.='and plvh like "%'.$plvh.'%" ';}
                    if(!empty($cntn)){$sql_info.='and cntn like "%'.$cntn.'%" ';}
                    
                    if(!empty($stfin)){$sql_info.='and ifnull(stfin,2) != "'.$stfin.'" ';}
                    
                    if(!empty($cair)){$sql_info.='and cair like "'.$cair.'%" ';}
//                     if(!empty($civa))
                    if(strlen(trim($civa))>0)
                    {
                         $civa0=data_maes('civa',$civa);
                         $sql_info.='and iva = '.$civa0['civa_descri']*(1).' ';
                    }
                    
                    if(!empty($ctas))
                    {
                         $sql_info.='and (ctas like "'.$ctas.'%" ';
                         if(substr($ctas,0,6)=='999999')
                         {$sql_info.='or ifnull(ctas,"")="" ';}
                         $sql_info.=') ';
//                          echo '===='.$sql_info;
                    }
               }
               
               if(!empty($vndd) and empty($numero1)){$sql_info.='and (vndd like "'.$vndd.'" or vndd_descri like "%'.$vndd.'%" ) ';}
               if($stce==1){$sql_info.='and ifnull(autorfec,"")!="" ';}
               if($stce==2){$sql_info.='and ifnull(autorfec,"")="" ';}
               if(!empty($impor)){$sql_info.='and impor like "'.$impor.'" ';}
               
               if(!empty($lsub)){$sql_info.='and item_lsub like "'.$lsub.'%" ';}
               if(!empty($zsub)){$sql_info.='and zsub like "'.$zsub.'%" ';}
               if(!empty($tcli)){$sql_info.='and enti_tcli like "'.$tcli.'%" ';}
               if(!empty($tpro)){$sql_info.='and enti_tpro like "'.$tpro.'%" ';}
               if(!empty($concep)){$sql_info.='and (concepto like "%'.$concep.'%" or detalle like "%'.$concep.'%") ';}
               
               if(!empty($mrca)){$sql_info.='and item_mrca = "'.$mrca.'" ';}
               if(!empty($clor)){$sql_info.='and item_clor = "'.$clor.'" ';}
               if(!empty($mdlo)){$sql_info.='and item_mdlo like "'.$mdlo.'%" ';}
//                if(!empty($vhcl)){$sql_info.='and vhcl like "'.$vhcl.'%" ';}
//                if(!empty($cntn)){$sql_info.='and cntn like "'.$cntn.'%" ';}
               
               if(!empty($stat)){$sql_info.='and status = "'.$stat.'" ';}
               
               if($info=='vtrpm1'){$sql_info.='and ifnull(iva,0)=0 ';}
               if($info=='vtrpm1'){$sql_info.='and item_lsub like "MEDI%" ';}

               $xgroupby1='group by idnum1 ';
               if($info=='vtenti'){$xgroupby1='group by left(enti,10) ';}
               if($info=='vtptov'){$xgroupby1='group by ptov ';}
               if($info=='vtxiva'){$xgroupby1='group by iva ';}
               if($info=='vtvndd'){$xgroupby1='group by left(vndd,10) ';}
               if($info=='coenti'){$xgroupby1='group by left(enti,10) ';}
//                if($info=='vtitem'){$xgroupby1='group by item ';}
               if($info=='vtitem'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='coitem'){$xgroupby1='group by item ';}
               if($info=='coitem'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='coctas'){$xgroupby1='group by ctas ';}
               if($info=='vtrpm1'){$xgroupby1='group by idnum1,item,puni ';}
               if($info=='cocair'){$xgroupby1='group by cair ';}
               if($info=='cociva'){$xgroupby1='group by iva ';}
               if($info=='ccitem'){$xgroupby1='group by item ';}
               if($info=='vtxano'){$xgroupby1='group by left(fecha,4) ';}
               if($info=='vtxmes'){$xgroupby1='group by left(fecha,7) ';}
               if($info=='vtxfec'){$xgroupby1='group by fecha ';}
//               if($info=='vtxsem'){$xgroupby1='group by date_format(fecha,"%x-%v"),left(fecha,10) ';}
               if($info=='vtxsem'){$xgroupby1='group by asema,fecha ';}
               //if($info=='vtxsem'){$xgroupby1='group by asema,substring(fecha,9,2) ';}
               if($info=='vtenxm'){$xgroupby1='group by left(enti,10),left(fecha,7) ';}
               if($info=='coenxm'){$xgroupby1='group by left(enti,10),left(fecha,7) ';}
               if($info=='vtvnxm'){$xgroupby1='group by left(vndd,10),left(fecha,7) ';}
               if($info=='vtanxm'){$xgroupby1='group by left(fecha,7) ';}
               if($info=='coanxm'){$xgroupby1='group by left(fecha,7) ';}
               if($info=='vtitxm'){$xgroupby1='group by item,left(fecha,7) ';}
               if($info=='vtlsxm'){$xgroupby1='group by item_lsub,left(fecha,7) ';}
               if($info=='cocaxm'){$xgroupby1='group by cair,left(fecha,7) ';}
               if($info=='coitxm'){$xgroupby1='group by item,left(fecha,7) ';}
               if($info=='coctxm'){$xgroupby1='group by ctas,left(fecha,7) ';}
               if($info=='coxmes'){$xgroupby1='group by left(fecha,7) ';}   
               
               if($info=='codein'){$xgroupby1='group by idnum1,iddet ';}   
               if($info=='opdein'){$xgroupby1='group by idnum1,iddet ';}   
               
               if(mb_stristr2($info,'rsol')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'osol')){$xgroupby1='group by idnum1,iddet ';}
               
               if(mb_stristr2($info,'blog')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'deta')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det1')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det2')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'det3')){$xgroupby1='group by idnum1,iddet ';}
               if(mb_stristr2($info,'ddbb')){$xgroupby1='group by idnum1,iddet ';}

//                echo $sql_info;
               $sql_info.=$xgroupby1;
          }

          if($modu=='inve' or $modu=='infi')
          {
//                if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; }
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; $bode='';}

               $sql_bode='select distinct bode_codigo from bode0 inner join inve1 on bode_codigo=bode ';
               $sql_bode.='where bode_status not in("I","X") ';
               if(!empty($bode)){$sql_bode.='and bode_codigo="'.$bode.'" ';}
               if(!empty($tbod)){$sql_bode.='and left(bode_tipo,1)="'.substr($tbod,0,1).'" ';}
               
//                if($info=='iiie13')
//                {$sql_bode='select distinct bode_codigo from bode0 ';}

               if(mb_stristr2('.iiie11.iiie13.iiie14',$info))
               {$sql_bode='select distinct bode_codigo from bode0 ';}

               if($info=='iietbb')
               {$sql_bode='select distinct bode_codigo from bode0 inner join inve1 on bode_codigo=bode2 ';}
               
               $sql_bode.='order by bode_tipo,bode_codigo ';

                    $sql_info ='select * ';
                    if($info=='iikrdx')
                    {
                         $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else idnum1 end as idnum1x ';
                         $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else iddet end as iddetx ';
                         $sql_info.=', case when fecha<"'.$fecha1.'" then max(fecha) end as fechax ';
          //                $sql_info.=', case when fecha<"'.$fecha1.'" then max(fecha) end as fechax ';
                    }
//                     $sql_info.='from inve1 ';
                    $sql_info.='from '.$modu1.' ';
                    $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
                    $sql_info.='left join docu0 on docu=docu_codigo ';
                    $sql_info.='left join item0 on item=item_codigo ';
//                     $sql_info.='left join iinv0 on cambio=iinv_codigo ';
                    if($modu=='inve')
                    {$sql_info.='left join ibod0 on cambio=ibod_codigo ';}
                    $sql_info.='left join bode0 on bode=bode_codigo ';
                    $sql_info.='left join enti0 on enti=enti_codigo ';
                    $sql_info.='left join lsub0 on item_lsub=lsub_codigo ';
                    $sql_info.='left join mrca0 on item_mrca=mrca_codigo ';
                    // if($clor_ok==1)
                    {$sql_info.='left join clor0 on item_clor=clor_codigo ';}
                    // if($tlla_ok==1)
                    {$sql_info.='left join tlla0 on item_tlla=tlla_codigo ';}
                    $sql_info.='left join stat0 on status=stat_codigo ';
               
               
                    if($pvar=='true' and 1==2)
                    {
                         $sql_cpro ='create temporary table invecpro1 ';
                         $sql_cpro.='select  ';
                         $sql_cpro.='item as cpro1_item, ';
                         $sql_cpro.='(fecha) as cpro1_fecha, ';
                         $sql_cpro.='puni as cpro1_cpro ';
                         $sql_cpro.='from inve1 ';
                         $sql_cpro.='left join inve2 on idnum1=idnum2 ';
                         $sql_cpro.='left join item0 on item=item_codigo ';
                         $sql_cpro.='where ifnull(status,"")not in("O","X") ';
                         $sql_cpro.='and cant*puni!=0 ';
                         $sql_cpro.='and fecha<"'.$fecha1.'"';
                         if(!empty($lsub))
                         {$sql_cpro.='and item_lsub like "'.$lsub.'%"';}
                         if(!empty($item))
                         {$sql_cpro.='and item like "'.$item.'%"';}
                         if(!empty($bode))
                         {$sql_cpro.='and bode like "'.$bode.'%"';}
                         $sql_cpro.='group by item ';
                         $sql_cpro.='order by  fecha desc ';
                         $res_cpro=msql_query($sql_cpro,$link_data);
                         
//                          echo '<br> === '.$sql_cpro;
                         
                         $sql_cpro ='create temporary table invecpro2 ';
                         $sql_cpro.='select  ';
                         $sql_cpro.='item as cpro2_item, ';
                         $sql_cpro.='(fecha) as cpro2_fecha, ';
                         $sql_cpro.='puni as cpro2_cpro ';
                         $sql_cpro.='from inve1 ';
                         $sql_cpro.='left join inve2 on idnum1=idnum2 ';
                         $sql_cpro.='left join item0 on item=item_codigo ';
                         $sql_cpro.='where ifnull(status,"")not in("O","X") ';
                         $sql_cpro.='and cant*puni!=0 ';
                         $sql_cpro.='and fecha<="'.$fecha2.'"';
                         if(!empty($lsub))
                         {$sql_cpro.='and item_lsub like "'.$lsub.'%"';}
                         if(!empty($item))
                         {$sql_cpro.='and item like "'.$item.'%"';}
                         if(!empty($bode))
                         {$sql_cpro.='and bode like "'.$bode.'%"';}
                         $sql_cpro.='group by item ';
                         $sql_cpro.='order by  fecha desc ';
                         $res_cpro=msql_query($sql_cpro,$link_data);

//                          echo '<br> === '.$sql_cpro;
                         
                         $sql_info.='left join invecpro1 on item=cpro1_item ';
                         $sql_info.='left join invecpro2 on item=cpro2_item ';
                         
                    }
               
               
               
          //           $sql_info=calc_maes('docu',$sql_info);
//                     $sql_info=calc_fields('inve',$sql_info);
                    $sql_info=calc_fields($modu,$sql_info);
                    $sql_info.='where ifnull(status,"")!="X" ';
               
                    if(!mb_stristr2('.resu.ride.deta.det2.blog.krdx',substr($info,2,4)))
                    {$sql_info.='and ifnull(status,"")!="O" ';}
               
               
//                     $sql_info.='where status not in("O","X") ';
                    if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               
                    if(!empty($tbod)){$sql_info.='and left(bode_tipo,1)="'.substr($tbod,0,1).'" ';}
               
                    if(!empty($fecha0)){$sql_info.='and fecha>="'.$fecha0.'" ';}

                    if($info=='iidet2')
                    {}
                    else
                    {if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}}
               
                    if(mb_stristr2('.iiresu.iideta',$info))
                    {
                         if($tsaldo==6  )
                         {
                              if($_SESSION['user_tipo'] !='sist')
                              {$sql_info.='and fecha>="2025-01-01"';};
                              $sql_info.='and status!="O"';
                              $sql_info.='and (puni)<"'.$saldo0.'"';
                         }
                         
                         if($tsaldo==8)
                         {
                              if($_SESSION['user_tipo'] !='sist')
                              {$sql_info.='and fecha>="2025-01-01"';};
                              $sql_info.='and status!="O"';
                              $sql_info.='and abs(puni)<="'.$saldo0.'"';
                         }
                         
                         if($tsaldo==68  )
                         {
                              if($_SESSION['user_tipo'] !='sist')
                              {$sql_info.='and fecha>="2025-01-01"';};
                              $sql_info.='and status!="O"';
                              $sql_info.='and (puni)<="'.$saldo0.'"';
                         }
                         
//                          {$sql_info.='and puni<="'.$saldo0.'"';}
                    }


//                     if($info=='iiie13')
                    if(mb_stristr2('.iiie11.iiie13.iiie14',$info))
                    {if(!empty($bode)){$sql_info.='and (bode like "'.$bode.'%" or bode2 like "'.$bode.'%" )';}}
                    else
                    {if(!empty($bode)){$sql_info.='and bode like "'.$bode.'%" ';}}
               
                    if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}
               
                    if(mb_stristr2('.iiresu.iideta.iidet1.iidet2',$info))
                    {if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}}
               
                    if(mb_stristr2('.ifresu.ifdeta.ifdet1.ifdet2',$info))
                    {if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}}
               
                    if(mb_stristr2('.iiddcc',$info))
                    {if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}}

                    if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
                    {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}
               
                    if(!empty($enti) and mb_stristr2('.resu.',substr($info,2,4)))
                    {$sql_info.='and enti like "'.substr($enti,0,10).'%" ';}
               
               
                    if($info=='iidet2' or $info=='ifdet2')
                    {
                         if(!empty($numero)){$sql_info.='and numero*1 = "'.(int)$numero.'" ';}
                    }
               
//                     if($info!='iiblog' and $info!='iidet2')
                    if($info!='iiblog' and $info!='iidet2' and $info!='ifblog' and $info!='ifdet2')
                    {
     //                     if(!empty($item)){$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
                         if(!empty($item))
                         {
                              if(empty($sql_busca_item))
                              {$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
                              else
                              {$sql_info.=$sql_busca_item;}
                         }
                         

                         if(!empty($lsub)){$sql_info.='and item_lsub like "'.$lsub.'%" '; }
                         if(!empty($lote)){$sql_info.='and lote like "'.$lote.'%" ';}
                         if(!empty($mrca)){$sql_info.='and item_mrca = "'.$mrca.'" ';}
                         if(!empty($clor)){$sql_info.='and item_clor = "'.$clor.'" ';}
                         if(!empty($mdlo)){$sql_info.='and item_mdlo like "'.$mdlo.'%" ';}
                    }

                    if($info=='iidcdd'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iidcmm'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iiresu'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iideta'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iidet1'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iiddbb'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iiblog'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='ifblog'){$sql_info.='and fecha>="'.$fecha1.'" ';}
     //                if($info=='iievta'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB10" ';}
     //                if($info=='iievta'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="IB12" ';}
                    if($info=='iievta'){$sql_info.='and fecha>="'.$fecha1.'" and (docu ="EB10" or docu="IB12") ';}
                    if($info=='iiecon'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB19" ';}
                    if($info=='iiicom'){$sql_info.='and fecha>="'.$fecha1.'" and docu in("IB10","IB15") ';}
                    if($info=='iiecxb'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB19" ';}
                    if($info=='iieclb'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB19" ';}
                    if($info=='iietxb'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB13" ';}
                    if($info=='iietbb'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB13" ';}
//                     if($info=='iietbb'){$sql_info.='and fecha>="'.$fecha1.'" and docu in("EB13","IB13") ';}
                    if($info=='iiie11'){$sql_info.='and fecha>="'.$fecha1.'" and docu like "%11" ';}
                    if($info=='iiie13'){$sql_info.='and fecha>="'.$fecha1.'" and docu in("EB13","IB13") ';}
                    if($info=='iiie14'){$sql_info.='and fecha>="'.$fecha1.'" and docu like "%14" ';}
                    if($info=='iicuxc'){$sql_info.='and fecha>="'.$fecha1.'" and docu in("IB10","IB15") ';}

                    if($info=='iillcc')          
                    {
                         $sql_info.='and item_codigo in(';
                         $sql_info.='select distinct item from inve1  ';
                         $sql_info.='left join inve2 on idnum1=idnum2 ';
                         $sql_info.='where status not in("O","X") and left(fecha,7)>="'.substr($fecha1,0,7).'"';
                         if(!empty($bode)){$sql_info.='and bode like "'.$bode.'%"';}
                         $sql_info.=')';
                    }


                    $xgroupby1='group by item ';
                    if($info=='iillcc'){$xgroupby1='group by item ';}
                    if($info=='iiiiee'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iiie11'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iiie13'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iiie14'){$xgroupby1='group by idnum1,iddet ';}
//                     if($info=='iiiiee'){$xgroupby1='group by item,docu,fecha,idnum1,iddet ';}
//                     if($info=='iiiiee'){$xgroupby1='group by item,fecha,docu ';}
//                     if($info=='iiiiee'){$xgroupby1='group by fecha,item,docu ';}
                    if($info=='iiddcc'){$xgroupby1='group by item,docu,fecha ';}
                    if($info=='iidcdd'){$xgroupby1='group by fecha,item,docu,fecha ';}
                    if($info=='iidcmm'){$xgroupby1='group by fecha,item,docu,fecha ';}
                    if($info=='iidocu'){$xgroupby1='group by item,docu,fecha ';}
                    if($info=='iiiecf'){$xgroupby1='group by enti,fecha,item,docu ';}
                    if($info=='iievta'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iiecon'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iiicom'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iiecxb'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iieclb'){$xgroupby1='group by item_lsub,bode ';}
                    if($info=='iietxb'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iietbb'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iisxlt'){$xgroupby1='group by item,idnum1,lote ';}
//                     if($info=='iisxlt'){$xgroupby1='group by idnum1,iddet ';}
//                     if($info=='iibode'){$xgroupby1='group by item,bode ';}
                    if($info=='iibode'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iillbb'){$xgroupby1='group by item_lsub,bode ';}
                    if($info=='iiresu'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iiddbb'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iikrdx'){$xgroupby1='group by idnum1x,iddetx,item ';}
                    if($info=='iiblog'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='ifblog'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iideta'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iidet1'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iidet2'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iicuxc'){$xgroupby1='group by idnum1,iddet ';}
//                     $xgroupby1='group by idnum1,iddet ';
               
               
                    if($info=='ifresu'){$xgroupby1='group by idnum1 ';}
                    if($info=='ifdeta'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='ifdet1'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='ifdet2'){$xgroupby1='group by idnum1,iddet ';}
               
               
               
                    $sql_info.=$xgroupby1;
               
                    $xorderby1='';
                    if($info=='iiiiee'){$xorderby1='order by fecha desc ';}
                    if($info=='iicuxc'){$xorderby1='order by item, fecha desc ';}
                    $sql_info.=$xorderby1;
               
          }

          if($modu=='pduc')
          {
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; $prcs='';}

               $sql_proc='select distinct proc_codigo from proc0 inner join pduc1 on proc_codigo=proc ';
               $sql_proc.='where proc_status not in("I","X") ';
               if(!empty($proc)){$sql_proc.='and proc_codigo="'.$proc.'" ';}
               if(!empty($tpro)){$sql_proc.='and left(proc_tipo,1)="'.substr($tpro,0,1).'" ';}
               

//                if(mb_stristr2('.iiie11.iiie13.iiie14',$info))
//                {$sql_bode='select distinct bode_codigo from bode0 ';}

//                if($info=='iietbb')
//                {$sql_bode='select distinct bode_codigo from bode0 inner join inve1 on bode_codigo=bode2 ';}
               
               $sql_proc.='order by proc_tipo,proc_codigo ';

                    $sql_info ='select * ';
                    if($info=='iikrdx')
                    {
                         $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else idnum1 end as idnum1x ';
                         $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else iddet end as iddetx ';
                         $sql_info.=', case when fecha<"'.$fecha1.'" then max(fecha) end as fechax ';
                    }
                    $sql_info.='from '.$modu1.' ';
                    $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
                    $sql_info.='left join docu0 on docu=docu_codigo ';
                    $sql_info.='left join item0 on item=item_codigo ';
//                     if($modu=='inve')
//                     {$sql_info.='left join ibod0 on cambio=ibod_codigo ';}
                    $sql_info.='left join proc0 on proc=proc_codigo ';
                    $sql_info.='left join enti0 on enti=enti_codigo ';
//                     $sql_info.='left join lsub0 on item_lsub=lsub_codigo ';
//                     $sql_info.='left join mrca0 on item_mrca=mrca_codigo ';
                    $sql_info.='left join stat0 on status=stat_codigo ';
               
               
               
                    $sql_info=calc_fields($modu,$sql_info);
                    $sql_info.='where ifnull(status,"")!="X" ';
               
                    if(!mb_stristr2('.resu.ride.deta.det2.blog.krdx',substr($info,2,4)))
                    {$sql_info.='and ifnull(status,"")!="O" ';}
               
               
                    if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               
//                     if(!empty($tpro)){$sql_info.='and left(proc_tipo,1)="'.substr($tpro,0,1).'" ';}
               
                    if(!empty($fecha0)){$sql_info.='and fecha>="'.$fecha0.'" ';}

                    if($info=='pddet2')
                    {}
                    else
                    {if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}}


//                     if(mb_stristr2('.iiie11.iiie13.iiie14',$info))
//                     {if(!empty($bode)){$sql_info.='and (bode like "'.$bode.'%" or bode2 like "'.$bode.'%" )';}}
//                     else
//                     {if(!empty($bode)){$sql_info.='and bode like "'.$bode.'%" ';}}
               
//                     if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}
               
//                     if(mb_stristr2('.iiresu.iideta.iidet1.iidet2',$info))
//                     {if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}}
               
//                     if(mb_stristr2('.ifresu.ifdeta.ifdet1.ifdet2',$info))
//                     {if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}}
               
//                     if(mb_stristr2('.iiddcc',$info))
//                     {if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}}

                    if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
                    {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}
               
                    if(!empty($enti) and mb_stristr2('.resu.',substr($info,2,4)))
                    {$sql_info.='and enti like "'.substr($enti,0,10).'%" ';}
               
               
                    if($info=='pddet2' )
                    {
                         if(!empty($numero)){$sql_info.='and numero*1 = "'.(int)$numero.'" ';}
                    }
               
                    if($info!='pdblog' and $info!='pddet2' )
                    {
                         if(!empty($item))
                         {
                              if(empty($sql_busca_item))
                              {$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
                              else
                              {$sql_info.=$sql_busca_item;}
                         }
                         

                         if(!empty($lsub)){$sql_info.='and item_lsub like "'.$lsub.'%" '; }
                         if(!empty($lote)){$sql_info.='and lote like "'.$lote.'%" ';}
                         if(!empty($mrca)){$sql_info.='and item_mrca = "'.$mrca.'" ';}
                         if(!empty($clor)){$sql_info.='and item_clor = "'.$clor.'" ';}
                         if(!empty($mdlo)){$sql_info.='and item_mdlo like "'.$mdlo.'%" ';}
                    }

                    if($info=='iidcdd'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iidcmm'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iiresu'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iideta'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iidet1'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iiddbb'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='iiblog'){$sql_info.='and fecha>="'.$fecha1.'" ';}
                    if($info=='ifblog'){$sql_info.='and fecha>="'.$fecha1.'" ';}
     //                if($info=='iievta'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB10" ';}
     //                if($info=='iievta'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="IB12" ';}
                    if($info=='iievta'){$sql_info.='and fecha>="'.$fecha1.'" and (docu ="EB10" or docu="IB12") ';}
                    if($info=='iiecon'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB19" ';}
                    if($info=='iiicom'){$sql_info.='and fecha>="'.$fecha1.'" and docu in("IB10","IB15") ';}
                    if($info=='iiecxb'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB19" ';}
                    if($info=='iieclb'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB19" ';}
                    if($info=='iietxb'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB13" ';}
                    if($info=='iietbb'){$sql_info.='and fecha>="'.$fecha1.'" and docu ="EB13" ';}
//                     if($info=='iietbb'){$sql_info.='and fecha>="'.$fecha1.'" and docu in("EB13","IB13") ';}
                    if($info=='iiie11'){$sql_info.='and fecha>="'.$fecha1.'" and docu like "%11" ';}
                    if($info=='iiie13'){$sql_info.='and fecha>="'.$fecha1.'" and docu in("EB13","IB13") ';}
                    if($info=='iiie14'){$sql_info.='and fecha>="'.$fecha1.'" and docu like "%14" ';}
                    if($info=='iicuxc'){$sql_info.='and fecha>="'.$fecha1.'" and docu in("IB10","IB15") ';}

                    if($info=='iillcc')          
                    {
                         $sql_info.='and item_codigo in(';
                         $sql_info.='select distinct item from inve1  ';
                         $sql_info.='left join inve2 on idnum1=idnum2 ';
                         $sql_info.='where status not in("O","X") and left(fecha,7)>="'.substr($fecha1,0,7).'"';
                         if(!empty($bode)){$sql_info.='and bode like "'.$bode.'%"';}
                         $sql_info.=')';
                    }


                    $xgroupby1='group by item ';
                    if($info=='iillcc'){$xgroupby1='group by item ';}
                    if($info=='iiiiee'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iiie11'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iiie13'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iiie14'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iiddcc'){$xgroupby1='group by item,docu,fecha ';}
                    if($info=='iidcdd'){$xgroupby1='group by fecha,item,docu,fecha ';}
                    if($info=='iidcmm'){$xgroupby1='group by fecha,item,docu,fecha ';}
                    if($info=='iidocu'){$xgroupby1='group by item,docu,fecha ';}
                    if($info=='iiiecf'){$xgroupby1='group by enti,fecha,item,docu ';}
                    if($info=='iievta'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iiecon'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iiicom'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iiecxb'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iieclb'){$xgroupby1='group by item_lsub,bode ';}
                    if($info=='iietxb'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iietbb'){$xgroupby1='group by item,idnum1 ';}
                    if($info=='iisxlt'){$xgroupby1='group by item,idnum1,lote ';}
//                     if($info=='iisxlt'){$xgroupby1='group by idnum1,iddet ';}
//                     if($info=='iibode'){$xgroupby1='group by item,bode ';}
                    if($info=='iibode'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iillbb'){$xgroupby1='group by item_lsub,bode ';}
                    if($info=='iiresu'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iiddbb'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iikrdx'){$xgroupby1='group by idnum1x,iddetx,item ';}
                    if($info=='iiblog'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='ifblog'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iideta'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iidet1'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='pddet2'){$xgroupby1='group by idnum1,iddet ';}
                    if($info=='iicuxc'){$xgroupby1='group by idnum1,iddet ';}
//                     $xgroupby1='group by idnum1,iddet ';
               
               
               
                    $sql_info.=$xgroupby1;
               
                    $xorderby1='';
                    if($info=='pddet2'){$xorderby1='order by hora1,item ';}
                    if($info=='iiiiee'){$xorderby1='order by fecha desc ';}
                    if($info=='iicuxc'){$xorderby1='order by item, fecha desc ';}
                    $sql_info.=$xorderby1;
               
          }

          if($modu=='caja')
          {
               $sql_info ='select * ';
               if($info=='cjmovi')
               {
                    $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else idnum1 end as idnum1x ';
                    $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else iddet end as iddetx ';
                    $sql_info.=', case when fecha<"'.$fecha1.'" then max(fecha) end as fechax ';
     //                $sql_info.=', case when fecha<"'.$fecha1.'" then max(fecha) end as fechax ';
               }
               $sql_info.='from caja1 ';
               $sql_info.='left join caja2 on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join docd0 on docum=docd_codigo ';
               $sql_info.='left join enti0 on enti=enti_codigo ';
               $sql_info.='left join caja0 on caja=caja_codigo ';
               $sql_info.='left join ctas0 on ctas=ctas_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               $sql_info=calc_fields('caja',$sql_info);
               $sql_info=calc_maes('enti',$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               if($info!='cjresu')
               {$sql_info.='and fecha>="'.$fecha0.'" ';}
               $sql_info.='and fecha<="'.$fecha2.'" ';

//                echo '=='.$info;
//                if($info!='cjresu' and $info!='cjdeta'  and $info!='cjdet2' and $info!='ride')
               if($info!='cjresu' and $info!='cjdeta'  and $info!='cjdet2' and $info!='ride' and $info!='ddbb')
               {$sql_info.='and left(docu,2) not in ("AC","AP") ';}
               
               
//               if(!empty($tbod)){$sql_info.='and left(bode_tipo,1)="'.substr($tbod,0,1).'" ';}
               if(!empty($caja)){$sql_info.='and caja like "'.$caja.'%" ';}

               if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}

               
               if($info=='cjdocu')
               {
                    if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
                    if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               }
               
               
               if($info=='cjresu')
               {
                    if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
                    if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               }
               
               
               if($info=='cjdet2')
               {
                    if(mb_stristr2('.ICH1.ECH1',$docu))
                    {
                    if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
                    }
                    
                    if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
                    if(!empty($numero)){$sql_info.='and numero*1 = "'.(int)$numero.'" ';}
               }
               
               
               
               if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
               {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}
               
               if($info!='cjblog' and $info!='cjdet2')
               {
//                     if(!empty($item))
//                     {
//                          if(empty($sql_busca_item))
//                          {$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
//                          else
//                          {$sql_info.=$sql_busca_item;}
//                     }
                    
//                     if(!empty($lsub)){$sql_info.='and item_lsub like "'.$lsub.'%" ';}
//                     if(!empty($lote)){$sql_info.='and lote like "'.$lote.'%" ';}
//                     if(!empty($mrca)){$sql_info.='and item_mrca = "'.$mrca.'" ';}
//                     if(!empty($clor)){$sql_info.='and item_clor = "'.$clor.'" ';}
//                     if(!empty($mdlo)){$sql_info.='and item_mdlo like "'.$mdlo.'%" ';}
               }
               

               if($info!='cjblog')
               {
//               if(!empty($lsub)){$sql_info.='and item_lsub like "'.$lsub.'%" ';}
//               if(!empty($item)){$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
//               if(!empty($lote)){$sql_info.='and lote like "'.$lote.'%" ';}
//               if(!empty($mrca)){$sql_info.='and item_mrca = "'.$mrca.'" ';}
//               if(!empty($clor)){$sql_info.='and item_clor = "'.$clor.'" ';}
//               if(!empty($mdlo)){$sql_info.='and item_mdlo like "'.$mdlo.'%" ';}
               }

               if($info=='cjdocu'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='cjdate'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='cjresu'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='cjddbb'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='cjblog'){$sql_info.='and fecha>="'.$fecha1.'" ';}


               
               $xgroupby1='group by caja ';
               if($info=='iillcc'){$xgroupby1='group by item ';}
               if($info=='cjiiee'){$xgroupby1='group by docu,fecha ';}
               if($info=='iiddcc'){$xgroupby1='group by lsub,docu,fecha ';}
               if($info=='iidcdd'){$xgroupby1='group by fecha,docu ';}
               if($info=='iidcmm'){$xgroupby1='group by fecha,docu ';}
               if($info=='cjblog'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='cjdet2'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='cjdocu'){$xgroupby1='group by idnum1 ';}
               if($info=='cjdate'){$xgroupby1='group by idnum1 ';}
               if($info=='cjcaja'){$xgroupby1='group by item,bode ';}
               if($info=='cjresu'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='cjdeta'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='iiddbb'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='cjmovi'){$xgroupby1='group by idnum1x,iddetx,caja ';}
               
//               echo '<br>'.$info;
//               echo '<br>'.$xgroupby1;
               
               $sql_info.=$xgroupby1;
          }

          if($modu=='bcos')
          {
               $sql_info ='select * ';
               
//                echo '*/*/*/*/* '.$fecha0;
               
               if($info=='bcmovi')
               {
                    $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else idnum1 end as idnum1x ';
                    $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else iddet end as iddetx ';
                    $sql_info.=', case when fecha<"'.$fecha1.'" then max(fecha) end as fechax ';
               }
               $sql_info.='from bcos1 ';
               $sql_info.='left join bcos2 on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join docd0 on docum=docd_codigo ';
               $sql_info.='left join enti0 on enti=enti_codigo ';
//                $sql_info.='left join bcos0 on bcos=bcos_codigo ';
               $sql_info.='left join ctab0 on ctab=ctab_codigo ';
               $sql_info.='left join ctas0 on ctas=ctas_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               $sql_info=calc_fields('bcos',$sql_info);
               $sql_info=calc_maes('enti',$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               
               if($info!='bcresu')
               {$sql_info.='and fecha>="'.$fecha0.'" ';}
               
               $sql_info.='and fecha<="'.$fecha2.'" ';
               $sql_info.='and left(docu,2) not in ("AC","AP") ';
//               if(!empty($tbod)){$sql_info.='and left(bode_tipo,1)="'.substr($tbod,0,1).'" ';}
               if(!empty($ctab)){$sql_info.='and ctab like "'.$ctab.'%" ';}
               
               if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}
               
               if($info=='bcresu')
               {
                    if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
                    if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               }
               if($info=='bcdet2')
               {
                    if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
                    if(!empty($numero)){$sql_info.='and numero*1 = "'.(int)$numero.'" ';}
               }
               
               if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
               {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}
               
               if($info!='bcblog' and $info!='bcdet2')
               {
//                     if(!empty($item))
//                     {
//                          if(empty($sql_busca_item))
//                          {$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
//                          else
//                          {$sql_info.=$sql_busca_item;}
//                     }
                    
//                     if(!empty($lsub)){$sql_info.='and item_lsub like "'.$lsub.'%" ';}
//                     if(!empty($lote)){$sql_info.='and lote like "'.$lote.'%" ';}
//                     if(!empty($mrca)){$sql_info.='and item_mrca = "'.$mrca.'" ';}
//                     if(!empty($clor)){$sql_info.='and item_clor = "'.$clor.'" ';}
//                     if(!empty($mdlo)){$sql_info.='and item_mdlo like "'.$mdlo.'%" ';}
               }
               
               if($info!='bcblog')
               {
//               if(!empty($lsub)){$sql_info.='and item_lsub like "'.$lsub.'%" ';}
//               if(!empty($item)){$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
//               if(!empty($lote)){$sql_info.='and lote like "'.$lote.'%" ';}
//               if(!empty($mrca)){$sql_info.='and item_mrca = "'.$mrca.'" ';}
//               if(!empty($clor)){$sql_info.='and item_clor = "'.$clor.'" ';}
//               if(!empty($mdlo)){$sql_info.='and item_mdlo like "'.$mdlo.'%" ';}
               }

               if($info=='bcdocu'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='bcdate'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='bcresu'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='bcddbb'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='bcblog'){$sql_info.='and fecha>="'.$fecha1.'" ';}

               
               
               
               $xgroupby1='group by ctab ';
               if($info=='iillcc'){$xgroupby1='group by item ';}
               if($info=='bciiee'){$xgroupby1='group by docu,fecha ';}
               if($info=='iiddcc'){$xgroupby1='group by lsub,docu,fecha ';}
               if($info=='iidcdd'){$xgroupby1='group by fecha,docu ';}
               if($info=='iidcmm'){$xgroupby1='group by fecha,docu ';}
               if($info=='bcblog'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='bcdet2'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='bcdocu'){$xgroupby1='group by idnum1 ';}
               if($info=='bcdate'){$xgroupby1='group by idnum1 ';}
               if($info=='bcctab'){$xgroupby1='group by ctab ';}
               if($info=='bcresu'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='bcdeta'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='iiddbb'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='bcmovi'){$xgroupby1='group by idnum1x,iddetx,ctab ';}
               
//               echo '<br>'.$info;
//               echo '<br>'.$xgroupby1;
               
               $sql_info.=$xgroupby1;
          }

          if($modu=='dxco' or $modu=='dxpa')
          {
               $sql_info ='select * ';
               if($info=='dcmovi')
               {
//                     $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else idnum1 end as idnum1x ';
//                     $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else iddet end as iddetx ';
//                     $sql_info.=', case when fecha<"'.$fecha1.'" then max(fecha) end as fechax ';
               }
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join docd0 on docum=docd_codigo ';
               $sql_info.='left join enti0 on enti=enti_codigo ';
//                $sql_info.='left join caja0 on caja=caja_codigo ';
               $sql_info.='left join ctas0 on ctas=ctas_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               
//                if($modu=='dxco')
//                {$sql_info.='left join zsub0 on enti_zsub=zsub_codigo ';}
//                if($modu=='dxpa')
//                {$sql_info.='left join zsub0 on enti_zsub=zsub_codigo ';}
               
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info=calc_maes('enti',$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
//                if($info!='cjresu')
//                {$sql_info.='and fecha>="'.$fecha0.'" ';}
               
               
//                $sql_info.='and fecha<="'.$fecha2.'" ';

//                if($info!='cjresu' and $info!='cjdeta' and $info!='caride')
//                {$sql_info.='and left(docu,2) not in ("AC","AP") ';}
               
//               if(!empty($tbod)){$sql_info.='and left(bode_tipo,1)="'.substr($tbod,0,1).'" ';}
//                if(!empty($caja)){$sql_info.='and caja like "'.$caja.'%" ';}

               if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}

               
               if($info=='cjdocu')
               {
                    if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
                    if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               }
               
               
               if($info=='cjresu')
               {
                    if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
                    if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               }
               
               
               if($info=='dcdet2')
               {
//                     if(mb_stristr2('.ICH1.ECH1',$docu))
//                     {
//                     if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
//                     }
                    
                    if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
                    if(!empty($numero)){$sql_info.='and numero*1 = "'.(int)$numero.'" ';}
               }
               
               
               
               if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
               {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}
               
               if($info!='cjblog' and $info!='cjdet2')
               {
//                     if(!empty($item))
//                     {
//                          if(empty($sql_busca_item))
//                          {$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
//                          else
//                          {$sql_info.=$sql_busca_item;}
//                     }
                    
//                     if(!empty($lsub)){$sql_info.='and item_lsub like "'.$lsub.'%" ';}
//                     if(!empty($lote)){$sql_info.='and lote like "'.$lote.'%" ';}
//                     if(!empty($mrca)){$sql_info.='and item_mrca = "'.$mrca.'" ';}
//                     if(!empty($clor)){$sql_info.='and item_clor = "'.$clor.'" ';}
//                     if(!empty($mdlo)){$sql_info.='and item_mdlo like "'.$mdlo.'%" ';}
               }
               

               if($info!='cjblog')
               {
//               if(!empty($lsub)){$sql_info.='and item_lsub like "'.$lsub.'%" ';}
//               if(!empty($item)){$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
//               if(!empty($lote)){$sql_info.='and lote like "'.$lote.'%" ';}
//               if(!empty($mrca)){$sql_info.='and item_mrca = "'.$mrca.'" ';}
//               if(!empty($clor)){$sql_info.='and item_clor = "'.$clor.'" ';}
//               if(!empty($mdlo)){$sql_info.='and item_mdlo like "'.$mdlo.'%" ';}
               }

               if($info=='cjdocu'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='cjdate'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='cjresu'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='cjddbb'){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if($info=='cjblog'){$sql_info.='and fecha>="'.$fecha1.'" ';}


               
               $xgroupby1='group by idnum1,iddet ';
//                if($info=='iillcc'){$xgroupby1='group by item ';}
//                if($info=='cjiiee'){$xgroupby1='group by docu,fecha ';}
//                if($info=='iiddcc'){$xgroupby1='group by lsub,docu,fecha ';}
//                if($info=='iidcdd'){$xgroupby1='group by fecha,docu ';}
//                if($info=='iidcmm'){$xgroupby1='group by fecha,docu ';}
//                if($info=='cjblog'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='cjdet2'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='cjdocu'){$xgroupby1='group by idnum1 ';}
//                if($info=='cjdate'){$xgroupby1='group by idnum1 ';}
//                if($info=='cjcaja'){$xgroupby1='group by item,bode ';}
//                if($info=='cjresu'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='cjdeta'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='iiddbb'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='cjmovi'){$xgroupby1='group by idnum1x,iddetx,caja ';}
               
//               echo '<br>'.$info;
//               echo '<br>'.$xgroupby1;
               
               $sql_info.=$xgroupby1;
          }

          
          
          
          if(mb_stristr2('.susa',$modu))
          {
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join user0 on enti=user_cedula ';
               $sql_info.='left join empl0 on left(enti,10)=left(empl_codigo,10) ';    
//                $sql_info.='left join enti0 on left(enti,10)=left(enti_codigo,10) ';    
//                $sql_info.='left join enti0 on left(enti,10)=(enti_codigo) ';    
//                $sql_info.='left join enti0 on clie=enti_codigo ';    
               $sql_info.='left join csub0 on csub=csub_codigo ';
//                $sql_info.='left join lsub0 on asub_lsub=lsub_codigo ';
//                $sql_info.='left join atvd0 on concat(left(asub,4),"-0000")=atvd_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
//                echo '*/*/*/'.$fecha0;
//                $sql_info.='and fecha>="'.$fecha1.'" ';
               $sql_info.='and fecha2<="'.$fecha2.'" ';
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               
               if(substr($info,2,4)!='blog')
               {if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or user_descri like "%'.($enti).'%") ';}}
               
               if(substr($info,2,4)=='blog')
               {if(!empty($csub)){$sql_info.='and csub like "'.$csub.'%" ';}}
               if(substr($info,2,4)=='deta')
               {if(!empty($csub)){$sql_info.='and csub like "'.$csub.'%" ';}}
//                if(!empty($empl)){$sql_info.='and (enti like "'.substr($empl,0,10).'%" or user_descri like "%'.($empl).'%") ';}
               
//                echo '*/*/*/'.$sql_info;
               if($info!='sublog')
               {
//                if(!empty($csub)){$sql_info.='and csub like "'.$asub.'%" ';}
               }
     //           if(!empty($zsub)){$sql_info.='and zsub like "'.$zsub.'%" ';}
     //           if(!empty($tpro)){$sql_info.='and enti_tpro like "'.$tpro.'%" ';}
     //           if(!empty($item)){$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
     //           if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}

               $xgroupby1='group by idnum1,iddet ';
               if($info=='sublog'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='sudeta'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='sudet2'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='suresu'){$xgroupby1='group by idnum1,iddet ';}
               $sql_info.=$xgroupby1;
               
               $xorderby1='';
               if($info=='suresu'){$xorderby1='order by enti,fecha2 desc ';}
               if($info=='sublog'){$xorderby1='order by enti,fecha2 desc ';}
               $sql_info.=$xorderby1;
               
               
          }
          
          if(mb_stristr2('.nomi',$modu))
          {
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
               $sql_info.='left join user0 on enti=user_cedula ';
               $sql_info.='left join empl0 on left(empl,10)=left(empl_codigo,10) ';    
               $sql_info.='left join nvdd0 on nvdd=nvdd_codigo ';
               $sql_info.='left join csub0 on csub=csub_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
               $sql_info=calc_fields($modu,$sql_info);
               $sql_info.='where ifnull(status,"")!="X" ';
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               $sql_info.='and fecha>="'.$fecha1.'" ';
               $sql_info.='and fecha<="'.$fecha2.'" ';
               if($info=='noblog')
               {$sql_info.='and fecha>="'.date('Y-m-01',strtotime($fecha2.' -1 month')).'" ';}
//                {$sql_info.='and idate2>="'.date('Y-m-01',strtotime($fecha2)).'" ';}
               
               if(!mb_stristr2('.blog',substr($info,2,4)))
               {if(!empty($numero)){$sql_info.='and numero*1 = '.(int)$numero.' ';}}
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or user_descri like "%'.($enti).'%") ';}
               
               if(substr($info,2,4)=='blog')
               {if(!empty($csub)){$sql_info.='and csub like "'.$csub.'%" ';}}
               if(substr($info,2,4)=='deta')
               {if(!empty($csub)){$sql_info.='and csub like "'.$csub.'%" ';}}
//                if(!empty($empl)){$sql_info.='and (enti like "'.substr($empl,0,10).'%" or user_descri like "%'.($empl).'%") ';}
               

               $xgroupby1='group by idnum1,iddet ';
               if($info=='noblog'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='nodeta'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='noresu'){$xgroupby1='group by idnum1,iddet ';}
               $sql_info.=$xgroupby1;
               
               $xorderby1='';
               if($info=='noresu'){$xorderby1='order by enti,fecha2 desc ';}
               if($info=='noblog'){$xorderby1='order by fecha desc, idnum1 desc, iddet desc ';}
               $sql_info.=$xorderby1;
               
               
          }
          
          

          if(mb_stristr2('.cxco.cxpa',$modu))
          {
               $sql_info='';
               $sql_info.='select * ';

               if($info=='ccmovi' or $info=='cpmovi' or $info=='ccanti' or $info=='cpanti')
               {
                    $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else idnum1 end as idnum1x ';
                    $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else iddet end as iddetx ';
                    $sql_info.=', case when fecha<"'.$fecha1.'" then max(fecha) end as fechax ';
                    $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else docu end as docux ';
     //                $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else mdfrom end as mdfromx ';
               }
               $sql_info.='from '.$modu1.' ';
               $sql_info.='where status not in("X") ';
               
               if($info=='ccdeta' or $info=='cpdeta')
               {$sql_info.='and status not in ("O") ';}
               
               if($info=='ccdeta' or $info=='cpdeta')
               {$sql_info.='and left(docum,4) not in ("AC10","AP10") ';}
               
               $sql_info.='and fecha>="'.$fecha0.'" ';
               
               if($doc18==0)
               {$sql_info.='and docu not in("CC18","XC18","CP18","XP18") ';}
               
               if(mb_stristr2('.ccanti.cpanti',$info))
               {$sql_info.='and docu like "A%" ';}
               
               if(mb_stristr2('.ccdcdd.cpdcdd.ccdcmm.cpdcmm',$info))
               {$sql_info.='and fecha>="'.$fecha1.'" ';}
               
//                if(mb_stristr2('.ccdeta.cpdeta',$info))
//                {$sql_info.='and fecha>="'.$fecha1.'" ';}
               
               
               
               $sql_info.='and fecha<="'.$fecha2.'" ';
               
               if(!empty($numero)){$sql_info.='and numer*1 = "'.$numero.'" ';}
               
               if(!empty($zsub)){$sql_info.='and enti_zsub like "'.$zsub.'%" ';}
               if(!empty($tcli)){$sql_info.='and enti_tcli like "'.$tcli.'%" ';}
               if(!empty($tpro)){$sql_info.='and enti_tpro like "'.$tpro.'%" ';}
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
//                if(!empty($vndd)){$sql_info.='and vndd_hide like "'.$vndd.'%" ';}
//                if(!empty($vndd)){$sql_info.='and vndd like "'.$vndd.'%" ';}
               
//                if($cfg_empr=='imcp')
//                {$sql_info.='and enti_tpro not like "ACCI%" ';}

     //           $sql_info=calc_fields($modu,$sql_info);
               $sql_info=sql_view($modu,$sql_info);
     //           echo '*//*'.$sql_info;

//                if($info=='ccsald'){$xgroupby1='group by left(enti,10),docum,numer ';}
//                if($info=='ccsald'){$xgroupby1='group by idnum1,iddet,mdfrom ';}
               if($info=='ccmovi'){$xgroupby1='group by idnum1,iddet,mdfrom ';}
               if($info=='cpmovi'){$xgroupby1='group by idnum1,iddet,mdfrom ';}
               if($info=='ccanti'){$xgroupby1='group by idnum1,iddet,mdfrom ';}
               if($info=='cpanti'){$xgroupby1='group by idnum1,iddet,mdfrom ';}

               $sql_info.=$xgroupby1;
//                echo $sql_info;
//                return;
          }     

          if(mb_stristr2('.retp.retc',$modu))
          {
//                if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti='';}
               if(!empty($docu) and !empty($idnum)){$numero1=$numero;$numero2=$numero;$fecha1='';$fecha2='';$enti=''; $ptoe='';}
               if(!empty($docu) and !empty($ptoe) and !empty($numero))
               {$numero1=$numero;$numero2=$numero;$enti='';$fecha1='';$fecha2='';}
               
//                echo '<br>====>'.$numero;
//                echo '<br>====>'.$numero1;
//                echo '<br>====>'.$numero2;
               
               $sql_info ='select * ';
               $sql_info.='from '.$modu1.' ';
               $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
               $sql_info.='left join docu0 on docu=docu_codigo ';
//                $sql_info.='left join tice0 on docu_tice=tice_codigo ';
               $sql_info.='left join tcel0 on docu_tcel=tcel_codigo ';
               $sql_info.='left join enti0 on enti=enti_codigo ';
               $sql_info.='left join cair0 on cair=cair_codigo ';
               $sql_info.='left join stat0 on status=stat_codigo ';
              
               $sql_info=calc_fields($modu,$sql_info);
     //           $sql_info=calc_maes('docu',$sql_info);
     //           $sql_info.='where status not in("O","X") ';
               $sql_info.='where ifnull(status,"")!="X" ';
               $sql_info.='and ifnull(cant,0)!=0 ';
//                if(!mb_stristr2('.rpresu.rcresu.rcride.rcsrid',$info))
//                {$sql_info.='and ifnull(status,"")!="O" ';}
               
               if(!mb_stristr2('.resu.ride.srid',substr($info,2,4)))
               {$sql_info.='and ifnull(status,"")!="O" ';}
               
               
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               
               if($rbco==1){$sql_info.='and docu like "RB%" ';}
               if($rbco!=1){$sql_info.='and docu not like "RB%" ';}
               
//                if($discrep=='true'){$sql_info.='and ifnull(errf,0)>0 ';}
               if($discrep=='true'){$sql_info.='and errf>0 ';}
               
               if(!empty($stat)){$sql_info.='and status="'.$stat.'" ';}
               if(!empty($fecha1)){$sql_info.='and fecha>="'.$fecha1.'" ';}
               if(!empty($fecha2)){$sql_info.='and fecha<="'.$fecha2.'" ';}
               if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}
               if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}
               if(!empty($ptoe)){$sql_info.='and ptoe like "'.$ptoe.'%" ';}
               if(!empty($cair)){$sql_info.='and cair like "'.$cair.'%" ';}
               if(!empty($riva)){$sql_info.='and puni = "'.$riva.'" ';}
               if(!empty($tpro)){$sql_info.='and enti_tpro like "'.$tpro.'%" ';}
//                echo '==='.$numero1.'-----'.$numero2;
               if(!empty($numero1)){$sql_info.='and numero*1 >= "'.(int)$numero1.'" ';}                    
               if(!empty($numero2)){$sql_info.='and numero*1 <= "'.(int)$numero2.'" ';}          

               if(!empty($toenti)){$sql_info.='and toenti like "'.$toenti.'%" ';}
               if($stce==1){$sql_info.='and ifnull(autorfec,"")!="" ';}
               if($stce==2){$sql_info.='and ifnull(autorfec,"")="" ';}
               
               if(mb_stristr2('.rprrta.rprrxm',$info)){$sql_info.='and cant*puni!=0 ';}
               if(mb_stristr2('.rpriva.rprixm',$info)){$sql_info.='and cant*puni!=0 ';}
               
               if(mb_stristr2('.rprrta.rprrxm',$info)){$sql_info.='and ifnull(cair,0)*1>300 ';}
               if(mb_stristr2('.rpriva.rprixm',$info)){$sql_info.='and ifnull(cair,0)*1<300 ';}
               
               if(mb_stristr2('.rcrrta.rcrrxm',$info)){$sql_info.='and cant*puni!=0 ';}
               if(mb_stristr2('.rcriva.rcrixm',$info)){$sql_info.='and cant*puni!=0 ';}
               
               if(mb_stristr2('.rcrrta.rcrrxm',$info)){$sql_info.='and ifnull(cair,0)*1>300 ';}
               if(mb_stristr2('.rcriva.rcrixm',$info)){$sql_info.='and ifnull(cair,0)*1<300 ';}
               
               if(!mb_stristr2($info,'blog'))
               {
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
//                if(!empty($item)){$sql_info.='and (item_codigo like "'.$item.'%" or item_ean like "'.$item.'%" or item_codigov like "'.$item.'%"  or item_descri like "%'.$item.'%")';}
               }


               $xgroupby1='group by idnum1,iddet ';
               if($info=='rcresu'){$xgroupby1='group by idnum1 ';}
               if($info=='rpresu'){$xgroupby1='group by idnum1 ';}

               if($info=='rprrta'){$xgroupby1='group by puni ';}
               if($info=='rpriva'){$xgroupby1='group by puni ';}
               if($info=='rcrrta'){$xgroupby1='group by puni ';}
               if($info=='rcriva'){$xgroupby1='group by puni ';}

               if($info=='rprrxm'){$xgroupby1='group by left(fecha,7),puni ';}
               if($info=='rprixm'){$xgroupby1='group by left(fecha,7),puni ';}
               if($info=='rcrrxm'){$xgroupby1='group by left(fecha,7),puni ';}
               if($info=='rcrixm'){$xgroupby1='group by left(fecha,7),puni ';}

               $sql_info.=$xgroupby1;
          }
          
          if($modu=='cont')
          {
               if(empty($nivel)){$nivel=$maxnivel;}
               if(empty($nivel)){$nivel=5;}
               $sql_info='';
               $sql_info ='select * ';
               if($info=='ttmovi' or $info=='ttssdd' or $info=='ttssmm')
               {
                    $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else idnum1 end as idnum1x ';
                    $sql_info.=', case when fecha<"'.$fecha1.'" then 0 else iddet end as iddetx ';
//                     $sql_info.=', case when fecha<"'.$fecha1.'" then max(fecha) else fecha end as fechax ';
                    $sql_info.=', case when fecha<"'.$fecha1.'" then  str_to_date("'.date('Y-m-d',strtotime($fecha1o.' -1 day')).'","%M %d %Y") else fecha end as fechax ';
                    $sql_info.=', case when fecha<"'.$fecha1.'" then  str_to_date("'.date('Y-m-d',strtotime($fecha1o.' -1 month')).'","%M %d") else left(fecha,7) end as mesx ';
//                     $sql_info.=', case when fecha<"'.$fecha1.'" then '.date('Y-m-d',strtotime($fecha1.'-1 month')).' else left(fecha,7) end as mesx ';
                    
                    
               }
               $sql_info.='from cont1 ';
               $sql_info.='left join cont2 on idnum1=idnum2 ';
               
               if(!mb_stristr2('.ttssff.ttrrii.ttssnn',$info))
               {
               }               
               
               if(mb_stristr2('.ttmovi.ttresu.ttdeta.ttdet2.ttride',$info))
               {
                    $sql_info.='left join docu0 on docu=docu_codigo ';
                    $sql_info.='left join ctas0 on ctas=ctas_codigo ';
                    $sql_info.='left join csub0 on csub=csub_codigo ';
                    $sql_info.='left join item0 on item=item_codigo ';
                    $sql_info.='left join enti0 on enti=enti_codigo ';
                    $sql_info.='left join stat0 on status=stat_codigo ';
               }
               
//                $sql_info.='left join stat0 on status=stat_codigo ';

//                $sql_info=calc_fields('cont',$sql_info);
               $sql_info=calc_fields($modu,$sql_info,$info);
               $sql_info.='where ifnull(status,"")!="X" ';
               
               if($info!='ttmovi')
               {$sql_info.='and ifnull(status,"")!="O" ';}
               
               if($info=='ttmovi' and empty($ctas))
               {$sql_info.='and ifnull(status,"")not in("O","X") ';}
               
               if(($info=='ttresu' or $info=='ttdeta') and $discrep=='true')
               {$sql_info.='and idnum1 in(select idnum2 from(select idnum2,sum(cant)as total from cont2 where ifnull(ctas,"99999")not like "99999%" group by idnum2) as difer where abs(total)>0.01) ';}
               
               
               if(substr($info,-4)!='det2'){$sql_info.='and cant!=0 ' ;}
               
               $sql_info.='and fecha>="'.$fecha0.'" ';
               
               
               if($nodtr==1)
               {$sql_info.='and idnum1 not in('.$sql_dtr1.')';}
               
               if($doc18==0)
               {$sql_info.='and docu not in("CC18","XC18","CP18","XP18") ';}
               
               
               $ctas_sf='"1","2","3"';
               $ctas_ri='"4","5","6"';
               if($empr0['empr_codigo']=='pcs1')
               {$ctas_ri='"4","5","6","7","8"';}
               
               if(mb_stristr2('.ttdet2',$info)){$ctas='';}
               if(mb_stristr2('.ttssff',$info)){$sql_info.='and left(ctas,1)in('.$ctas_sf.') '; $ctas='';}
               if(mb_stristr2('.ttsfcc',$info)){$sql_info.='and left(ctas,1)in('.$ctas_sf.') '; $ctas='';}
               
               if(mb_stristr2('.ttrrii',$info)){$sql_info.='and left(ctas,1) in('.$ctas_ri.') '; $ctas='';}
               if(mb_stristr2('.ttricc',$info)){$sql_info.='and left(ctas,1) in('.$ctas_ri.') '; $ctas='';}
               
               if(mb_stristr2('.ttresu.ttdeta',$info))
               {$sql_info.='and fecha>="'.$fecha1.'" ';}
               
               $sql_info.='and fecha<="'.$fecha2.'" ';
               
//                if(mb_stristr2('.ttresu.ttdeta',$info))
               if(mb_stristr2('.ttresu.ttdeta.ttdet2',$info))
               {if(!empty($docu)){$sql_info.='and docu like "'.$docu.'%" ';}}
               
                    
               if($cfg_empr!='posa')
               {if(!empty($ctas)){$sql_info.='and ctas like "'.$ctas.'%" ';}}
               
               if($cfg_empr=='posa')
               {if(!empty($ctas)){$sql_info.='and ctas like "'.$ctas.'" ';}}
               
//                if(!empty($ctas) and $info=='ttmovi'){$sql_info.='and trim(ctas) = "'.$ctas.'" ';}
               
               if(!empty($csub)){$sql_info.='and csub like "'.$csub.'%" ';}
//                if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" )';}
               if(!empty($enti)){$sql_info.='and (enti like "'.substr($enti,0,10).'%" or enti_descri like "%'.$enti.'%" or enti_razsoc like "%'.$enti.'%" or enti_nomcom like "%'.$enti.'%")';}
               
               if(!empty($item)){$sql_info.='and (item like "'.$item.'%" )';}
               
               if(!empty($idnum)){$sql_info.='and idnum1 = "'.$idnum.'" ';}
               
               if(!empty($mdfrom)){$sql_info.='and lower(mdfrom) = "'.strtolower($mdfrom).'" ';}
               
               if(!empty($concepto) and mb_stristr2('.resu.deta',substr($info,2,4)))
               {$sql_info.='and concat(ifnull(concepto,""),ifnull(detalle,"")) like "%'.$concepto.'%" ';}
               
               if($info=='ttdet2')
               {
                    if(!empty($numero)){$sql_info.='and numero*1 = "'.(int)$numero.'" ';}
               }

               $xgroupby1='group by ctas ';
               if($info=='ttsald'){$xgroupby1='group by ctas_codi'.$nivel.' ';}
               if($info=='ttsaln'){$xgroupby1='group by ctas_codi'.$nivel.' ';}
               if($info=='ttssss'){$xgroupby1='group by ctas_codi'.$nivel.',idnum1,iddet ';}
               
               if($info=='ttssnn'){$xgroupby1='group by ctas_codi'.$nivel.',idnum1,iddet ';}
               if($info=='ttsscc'){$xgroupby1='group by ctas_codi'.$nivel.',idnum1 ';}
               if($info=='ttssaa'){$xgroupby1='group by ctas_codi'.$nivel.',idnum1 ';}
               
//                if($info=='ttssff'){$xgroupby1='group by ctas_codi'.$nivel.',idnum1,iddet ';}
//                if($info=='ttrrii'){$xgroupby1='group by ctas_codi'.$nivel.',idnum1,iddet ';}
               if($info=='ttssff'){$xgroupby1='group by ctas_codi'.$nivel.' ';}
               if($info=='ttrrii'){$xgroupby1='group by ctas_codi'.$nivel.',idnum1 ';}
               
               if($info=='ttsfcc'){$xgroupby1='group by ctas_codi'.$nivel.',idnum1 ';}
               if($info=='ttricc'){$xgroupby1='group by ctas_codi'.$nivel.',idnum1 ';}
               
               if($info=='ttmovi'){$xgroupby1='group by idnum1x,iddetx,ctas ';}
//                if($info=='ttssdd'){$xgroupby1='group by idnum1x,iddetx,ctas ';}
//                if($info=='ttssdd'){$xgroupby1='group by idnum1,iddet,ctas ';}
//                if($info=='ttssmm'){$xgroupby1='group by idnum1,iddet,ctas ';}
               
               if($info=='ttssdd'){$xgroupby1='group by ctas,idnum1 ';}
               if($info=='ttssmm'){$xgroupby1='group by ctas,idnum1 ';}
               
               if($info=='ttresu'){$xgroupby1='group by idnum1 ';}
               if($info=='ttdeta'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='ttdet2'){$xgroupby1='group by idnum1,iddet ';}
               if($info=='ttride'){$xgroupby1='group by idnum1,iddet ';}
//                if($info=='ttride'){$xgroupby1='group by idnum1,ctas,iddet ';}
               $sql_info.=$xgroupby1;

               
               
               $xorderby1='';
//                if($info=='ttride'){$xorderby1='order by idnum2,ctas,iddet ';}
//                if($info=='sublog'){$xorderby1='order by enti,fecha2 desc ';}
               $sql_info.=$xorderby1;
               
               
               
     //           echo '<br>'.$sql_info;
     //           echo '<br>';

          }

          // Softball
          {
            if($modu=='soft')
              {
                  $sql_info ='select * ';
                  $sql_info.='from '.$modu1.' ';
                  $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
                  $sql_info.='left join docu0 on docu=docu_codigo ';
                  $sql_info.='left join liga0 on liga=liga_codigo ';
                  $sql_info.='left join torn0 on torn=torn_codigo ';    
                  $sql_info.='left join team0 on team1=team_codigo ';
                  $sql_info.='left join sqad0 on team2=sqad_codigo ';
                  $sql_info=calc_fields($modu,$sql_info);
                  $sql_info.='where ifnull(status,"")!="X" ';
                  $sql_info.='and tipo="'.$tipo.'" ';
                  if(!empty($jornada)){$sql_info.='and jornada="'.$jornada.'" ';}
                  if(!empty($equipo)){$sql_info.='and (team1="'.$equipo.'" or team2 = "'.$equipo.'") ';}
                  $sql_info.='and docu like "'.$docu.'%" ';
                  if(mb_stristr2('.sbresu.sbcale',$info)){$xgroupby1='group by idnum1 ';}
                  if(mb_stristr2('.sbresu.sbcale',$info)){$xgroupby1='group by idnum1 ';}
                  $sql_info.=$xgroupby1;
            
                  if(mb_stristr2('.sbresu.sbcale',$info)){$xorderby='order by jornada, fecha, idnum1 ';}
                  $sql_info.=$xorderby;

              }

            if($modu=='liup')
              {
                 $sql_info ='select * ';
                 $sql_info.='from '.$modu1.' ';
                 $sql_info.='left join '.$modu2.' on idnum1=idnum2 ';
                 $sql_info.='left join docu0 on docu=docu_codigo ';
//               $sql_info.='left join soft1 on soft1.idnum1=idsoft ';
                 $sql_info.='left join enti0 on jugador=enti_codigo ';
                 $sql_info.='left join team0 on team=team_codigo ';
                 $sql_info.='left join posi0 on posi=posi_codigo ';
                 $sql_info.='where ifnull(status,"")!="X" ';
                 $sql_info.='and docu like "'.$docu.'%" ';               
                 $sql_info.='and idsoft = '.$idsoft.' ';               
                 $sql_info.='and team = "'.$team.'" ';
                 if($info=='lidet2'){$xgroupby1='group by idnum1,iddet ';}
              }
          }
     }
  
     if($info=='iiblog'){$sql_info.='limit 50';}
     if($info=='noblog'){$sql_info.='limit 50';}


     $sql_info11='';
     $sql_info12='';
     if(mb_stristr2($info,'ride') or mb_stristr2($info,'ddbb'))
     {
          $sql_info11=substr($sql_info,0,strpos($sql_info,'group by'));
          $sql_info11.='group by idnum1 ';
          $sql_info12=substr($sql_info,0,strpos($sql_info,'group by')).' group by idnum1,iddet ';
          
          if($info=='ttride')
          {$sql_info12.='order by idnum1,ctas,iddet ';}
//           echo $sql_info12;
//           return;
     }




if($rete=='true')
if($info=='vtresu')
{
     $sql_drop='drop view reteclie';
     $res_drop=msql_query($sql_drop,$link_data);
     
     $sql_rete ='create view reteclie as ';
     $sql_rete.='select *, cant*puni as total from retc1 ';
     $sql_rete.='left join retc2 on idnum1=idnum2 ';
     $sql_rete.='where ifnull(status,"")not in("O","X") ';
     $sql_rete.='group by idnum1';
     $res_rete=msql_query($sql_rete,$link_data);
     
    //  $sql_rete='select info.*, rete.idnum1 as idn_rete, rete.numero as nume_rete from ';
     $sql_rete='select info.*, rete.idnum1 as idn_rete, rete.numero as nume_rete, rete.fecha as fec_rete from ';
     $sql_rete.='('.$sql_info.') as info ';
     $sql_rete.='left join reteclie rete on ';
     $sql_rete.='left(info.enti,10)= left(rete.enti,10) and info.numero*1=rete.numer*1 ';
     $sql_info=$sql_rete;
     
     
}




//      echo '===='.$rete;

if($info=='coresu')
{
     $sql_reteprov='select * from reteprov limit 1';
     $res_reteprov=msql_query($sql_reteprov,$link_data);
     if(msql_errno($link_data)>0){$rete='true';}
     
     
     if($rete=='true')
     {
          $sql_drop='drop view reteprov';
          $res_drop=msql_query($sql_drop,$link_data);

          $sql_rete ='create view reteprov as ';
          $sql_rete.='select *, cant*puni as total from retp1 ';
          $sql_rete.='left join retp2 on idnum1=idnum2 ';
          $sql_rete.='where ifnull(status,"")not in("O","X") ';
          $sql_rete.='group by idnum1';
          $res_rete=msql_query($sql_rete,$link_data);

          $sql_rete='select info.*, rete.idnum1 as idn_rete, rete.numero as nume_rete, rete.fecha as fec_rete from ';
          $sql_rete.='('.$sql_info.') as info ';
          $sql_rete.='left join reteprov rete on ';
//           $sql_rete.='left(info.enti,10)= left(rete.enti,10) and info.numero*1=rete.numer*1 ';
          $sql_rete.='left(info.enti,10)= left(rete.enti,10) and info.ptoe*1=rete.ptoem*1  and info.numero*1=rete.numer*1 ';
          $sql_info=$sql_rete;

     }
}




//           echo '<alert>';
//           echo '<consolelog>';
//           echo '====='.$sql_info;
//           echo '</consolelog>';
//           echo '</alert>';


//      echo '<br>==== '.$sql_info;
// die();
?>