<?php
// return;
     // SQL_INFO FINSISH     


     if(!empty($fields))
     {
//           $fields=str_replace(' then sum(saldo) else 0 end ',' then sum(saldo) end ',$fields);
//           echo '<br><br> ====> '.$fields;
//           echo '<br><br> ====> '.$sql_info;
          
          $sql_info='select '.$fields.' from ('.$sql_info.') ';
          $sql_info.='as maes0 ';
     }


     if($info=='iicuxc')
     {
          $rowtop=3;
          $fldbrk='item';
     }
     if($info=='suresu')
     {
          $rowtop=1;
          $fldbrk='enti';
     }

     if($info=='sublog')
     {
          $rowtop=1;
          $fldbrk='enti';
     }

     
     if(!empty($rowtop))
     {
     $sql_info='select *, if('.$fldbrk.'=@numbrk,@numrow:=@numrow+1,@numrow:=1) as numrow_hide, @numbrk:='.$fldbrk.' as numbrk_hide from ('.$sql_info.') as modu12';
     $sql_info='select * from ('.$sql_info.') as modu123 where numrow_hide<="'.$rowtop.'"';
     }
     


     if(mb_stristr2('.vtenti.',$info))
     {
//                $sql_info='select * from ('.$sql_info.') ';
//                $sql_info='select maes1.*, (total/_c_)as prome from ('.$sql_info.') ';
//                $sql_info.='as maes1 ';
     }


     if(mb_stristr2('.otresu.',$info))
     {
//                $sql_infox=$sql_info;
//                $sql_info ='select maes1.*, maes1.fase as ufase from ('.$sql_infox.') ';
//                $sql_info.='as maes1 ';
//                $sql_info.='where concat(maes1.fecha1_hide,maes1.hora1_hide)=';
//                $sql_info.='(select max(concat(maes2.fecha1_hide,maes2.hora1_hide)) from ('.$sql_infox.') as maes2 where maes2.fecha1_hide=maes1.fecha1_hide)';
     }



     // GROUP BY
     
     if(substr($info,0,2)=='mt')
     {
          $sql_info.='group by '.$modu.'_codigo ';
     }


     
     // ORDER BY
     if(substr($info,0,2)=='mt')
     {
          if(mb_stristr2($orden,'Código')){$xorderby='order by '.$modu.'_codigo ';}
          if(mb_stristr2($orden,'Descripción')){$xorderby='order by '.$modu.'_descri,'.$modu.'_codigo ';}
          if(mb_stristr2($orden,'Tipo')){$xorderby='order by '.$modu.'_tipo,'.$modu.'_codigo ';}
          if(mb_stristr2($orden,'Zona')){$xorderby='order by '.$modu.'_zsub,'.$modu.'_codigo ';}
          if(mb_stristr2($orden,'Linea')){$xorderby='order by '.$modu.'_lsub,'.$modu.'_codigo ';}
          
//           if($modu=='refx')
//           {$xorderby='order by '.$modu.'_refx0,'.$modu.'_codigo ';}
//           {$xorderby='order by '.$modu.'_referx5,'.$modu.'_referx4,'.$modu.'_referx3,'.$modu.'_referx2,'.$modu.'_referx1,'.$modu.'_codigo ';}
          
          if($modu=='tmpr')
          {$xorderby='order by '.$modu.'_mdvh, lsub_codigo , '.$modu.'_codigo ';}
          
          $sql_info.=$xorderby;
//           echo $orden.'****'.$xorderby;
     }




     if(substr($info,0,2)!='mt')
     {

     if(mb_stristr2('.jwib',$modu))
     {
          $xgroupby2='group by idnum1,iddet ';
          if($info=='mmresu'){$xgroupby2='group by nombre ';}
          if($info=='asresu'){$xgroupby2='group by nombre ';}
          if($info=='vcresu'){$xgroupby2='group by nombre ';}
          $sql_info.=$xgroupby2;
     }
          
          
     if(mb_stristr2('.jwrv.jwrt',$modu))
     {
          $xgroupby2='group by idnum1,iddet ';
//           if($info=='asresu'){$xgroupby2='group by nombre ';}
//           if($info=='vcresu'){$xgroupby2='group by nombre ';}
          $sql_info.=$xgroupby2;
     }
          
          
     if(mb_stristr2('.jwmc',$modu))
     {
          $xgroupby2='group by idnum1,iddet ';
          if($info=='mcprog'){$xgroupby2='group by sema,hora,lugar ';}
//           if($info=='vcresu'){$xgroupby2='group by nombre ';}
          $sql_info.=$xgroupby2;
     }
          
     if($modu=='plan')
     {
          $xgroupby2='group by asub ';
          if($info=='plxsem'){$xgroupby2='group by asema,usuario ';}
          if($info=='plblog'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='plresu'){$xgroupby2='group by idnum1 ';}
          if($info=='pldet2'){$xgroupby2='group by idnum1,iddet ';}
          $sql_info.=$xgroupby2;
     }
          
     if($modu=='atvd')
     {
          $xgroupby2='group by item ';
          if($info=='atxsem'){$xgroupby2='group by asema,usuario ';}
          if($info=='atblog'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='atresu'){$xgroupby2='group by idnum1 ';}
          $sql_info.=$xgroupby2;
     }
          
     if($modu=='cont')
     {
          $xgroupby2='group by ctas ';
          if($info=='ttsald'){$xgroupby2='group by ctas_codi'.$nivel.' ';}
          if($info=='ttsaln'){$xgroupby2='group by ctas_codi'.$nivel.' ';}
          if($info=='ttssss'){$xgroupby2='group by ctas_codi'.$nivel.' ';}
//           if($info=='ttssdd'){$xgroupby2='group by ctas_codi'.$nivel.',fecha ';}
          if($info=='ttssdd'){$xgroupby2='group by ctas_codi'.$nivel.',fechax ';}
          if($info=='ttssmm'){$xgroupby2='group by ctas_codi'.$nivel.',mesx ';}
          if($info=='ttssnn'){$xgroupby2='group by ctas_codi'.$nivel.' ';}
          if($info=='ttsscc'){$xgroupby2='group by ctas_codi'.$nivel.' ';}
          if($info=='ttssaa'){$xgroupby2='group by ctas_codi'.$nivel.' ';}
          if($info=='ttssff'){$xgroupby2='group by ctas_codi'.$nivel.' ';}
          if($info=='ttrrii'){$xgroupby2='group by ctas_codi'.$nivel.' ';}
          if($info=='ttsfcc'){$xgroupby2='group by ctas_codi'.$nivel.' ';}
          if($info=='ttricc'){$xgroupby2='group by ctas_codi'.$nivel.' ';}
          if($info=='ttmovi'){$xgroupby2='group by ctas,fecha,idnum1x,iddetx ';}
          
//           if($empr0['empr_codigo']=='posa')
//           {if($info=='ttmovi'){$xgroupby2='group by ctas,fecha,idnum1x ';}}
          
          if($info=='ttresu'){$xgroupby2='group by idnum1 ';}
          if($info=='ttdeta'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='ttdet2'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='ttride'){$xgroupby2='group by idnum1,iddet ';}
//           if($info=='ttride'){$xgroupby2='group by idnum1,ctas,iddet ';}
          $sql_info.=$xgroupby2;
     }
     
     if(mb_stristr2('.vtas.comp.clie.prov.carr.guia.opro',$modu))
     {
          $xgroupby2='group by idnum1,iddet ';
          if(mb_stristr2('.vtenti.coenti.clenti.prenti',$info)){$xgroupby2='group by left(enti,10) ';}
          if(mb_stristr2('.vtptov.',$info)){$xgroupby2='group by ptoe ';}
          if(mb_stristr2('.vtvndd.clvndd.',$info)){$xgroupby2='group by left(vndd,10) ';}
          if(mb_stristr2('.vtresu.coresu.clresu.prresu.caresu',$info)){$xgroupby2='group by idnum1 ';}
          if($info=='vtxano'){$xgroupby2='group by left(fecha,4) ';}
          if($info=='vtxmes'){$xgroupby2='group by left(fecha,7) ';}
          if($info=='vtxfec'){$xgroupby2='group by fecha ';}
          if($info=='vtxsem'){$xgroupby2='group by asema ';}
          if($info=='vtenti'){$xgroupby2='group by left(enti,10) ';}
          if($info=='vtenxm'){$xgroupby2='group by left(enti,10) ';}
          if($info=='vtvnxm'){$xgroupby2='group by left(vndd,10) ';}
          if($info=='vtanxm'){$xgroupby2='group by left(fecha,4) ';}
          if($info=='vtitxm'){$xgroupby2='group by item ';}
          if($info=='vtlsxm'){$xgroupby2='group by item_lsub ';}
          
          if($info=='coanxm'){$xgroupby2='group by left(fecha,4) ';}
          if($info=='coxmes'){$xgroupby2='group by left(fecha,7) ';}
          if($info=='cocair'){$xgroupby2='group by cair ';}
          if($info=='cociva'){$xgroupby2='group by iva ';}
          if($info=='cocaxm'){$xgroupby2='group by cair ';}
          if($info=='coitxm'){$xgroupby2='group by item ';}
          if($info=='coenxm'){$xgroupby2='group by left(enti,10) ';}
          if($info=='coctxm'){$xgroupby2='group by ctas ';}
          if(mb_stristr2('.vtitem.coitem.clitem.vtrpm1',$info)){$xgroupby2='group by item ';}
          
          if($info=='codein'){$xgroupby2='group by idnum1,iddet ';}
          $sql_info.=$xgroupby2;
     }

     if($modu=='caja')
     {
          $xgroupby2='group by caja ';
          if($info=='cjdocu'){$xgroupby2='group by idnum1 ';}
          if($info=='cjresu'){$xgroupby2='group by idnum1 ';}
          if($info=='cjdate'){$xgroupby2='group by caja,fecha ';}
          if($info=='cjiiee'){$xgroupby2='group by caja,fecha ';}
          if($info=='iiresu'){$xgroupby2='group by idnum1 ';}
          if($info=='iiddbb'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='cjmovi'){$xgroupby2='group by caja,fecha,signo,idnum1x ';}
          if($info=='cjblog'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='cjdet2'){$xgroupby2='group by idnum1,iddet ';}
          $sql_info.=$xgroupby2;
     }
     
     if($modu=='bcos')
     {
          $xgroupby2='group by ctab ';
          if($info=='bcdocu'){$xgroupby2='group by idnum1 ';}
          if($info=='bcresu'){$xgroupby2='group by idnum1 ';}
          if($info=='bcdate'){$xgroupby2='group by ctab,fecha ';}
          if($info=='bciiee'){$xgroupby2='group by ctab,fecha ';}
          if($info=='iiresu'){$xgroupby2='group by idnum1 ';}
          if($info=='iiddbb'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='bcmovi'){$xgroupby2='group by ctab,fecha,signo,idnum1x ';}
          if($info=='bcblog'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='bcdet2'){$xgroupby2='group by idnum1,iddet ';}
          $sql_info.=$xgroupby2;
     }
          
     if($modu=='retc' or $modu=='retp')
     {
          $xgroupby2='group by idnum1,iddet ';
          if($info=='rprrxm'){$xgroupby2='group by left(fecha,4),cair ';}
          if($info=='rprixm'){$xgroupby2='group by left(fecha,4),porc_iva ';}
          if($info=='rcrrxm'){$xgroupby2='group by left(fecha,4),cair ';}
          if($info=='rcrixm'){$xgroupby2='group by left(fecha,4),porc_iva ';}
          $sql_info.=$xgroupby2;
     }
          
          
     if($modu=='inve' or $modu=='infi')
     {
//           echo '====='.$info;
          $xgroupby2='group by item ';
          if($info=='iiiiee'){$xgroupby2='group by item ';}
          if($info=='iiddcc'){$xgroupby2='group by item_lsub ';}
          if($info=='iidcdd'){$xgroupby2='group by fecha ';}
          if($info=='iidcmm'){$xgroupby2='group by left(fecha,7) ';}
          if($info=='iiresu'){$xgroupby2='group by idnum1 ';}
          if($info=='iisxlt'){$xgroupby2='group by item,lote ';}
          if($info=='iiddbb'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='iikrdx'){$xgroupby2='group by item,fecha,signo,idnum1x,iddetx ';}
          if($info=='iiblog'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='ifblog'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='iisxlt'){$xgroupby2='group by item,lote ';}
          if($info=='iiiecf'){$xgroupby2='group by enti,fecha ';}
          if($info=='iideta'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='iidet1'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='iidet2'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='iillbb'){$xgroupby2='group by item_lsub ';}
          if($info=='iieclb'){$xgroupby2='group by item_lsub ';}
          if($info=='iicuxc'){$xgroupby2='';}

          if($info=='ifresu'){$xgroupby2='group by idnum1 ';}
          if($info=='ifdeta'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='ifdet1'){$xgroupby2='group by idnum1,iddet ';}
          if($info=='ifdet2'){$xgroupby2='group by idnum1,iddet ';}
          
          
          $sql_info.=$xgroupby2;
     }
          
     if(mb_stristr2('.susa.nomi',$modu))
     {
//           if($info=='sublog'){$xgroupby2='group by idnum1,iddet ';}          
     }
          
     if(mb_stristr2('.cxco.cxpa',$modu))
     {
          if($info=='ccresu'){$xwhere='where docum like "F%" or docum like "PR%" or docum like "AC%" or docum like "%00"';}
          if($info=='ccdeta'){$xwhere='where docum like "F%" or docum like "PR%" or docum like "AC%" or docum like "%00"';}
          
          if($info=='cccred'){$xwhere='where docum like "F%" or docum like "PR%" or docum like "AC%" or docum like "%00"';}
          
          if($info=='cpresu'){$xwhere='where docum like "F%" ';}
          if($info=='cpdeta'){$xwhere='where docum like "F%" ';}
          
          if($info=='ccsald'){$xwhere='where 1=1 ';}
          if($info=='cpsald'){$xwhere='where 1=1 ';}
          if($info=='ccsacc'){$xwhere='where 1=1 ';}
          if($info=='cpsacc'){$xwhere='where 1=1 ';}
          if($info=='ccddcc'){$xwhere='where 1=1 ';}
          if($info=='cpddcc'){$xwhere='where 1=1 ';}
          
//           if(mb_stristr2('.ccsald.cpsald',$info))
//           if(!empty($vndd)){$xwhere.='and vndd_hide like "'.$vndd.'%" ';}
          
//           if(mb_stristr2('.ccsald.cpsald',$info))
//           if(!empty($vndd)){$xwhere.='and enti_vndd_hide like "'.$vndd.'%" ';}
          
          $sql_info.=$xwhere;
          $xgroupby2='group by left(enti,10) ';
          
          
//           if($info=='ccsald' and $anti=='true'){$xgroupby2='group by left(enti,10) ';}
          if($info=='ccsald' and $anti=='true'){$xgroupby2='group by docum,ptoem,numer ';}
//           if($info=='cpsald' and $anti=='true'){$xgroupby2='group by docum,ptoem,numer ';}
          
          if($info=='ccresu'){$xgroupby2='group by docum,ptoem,numer ';}
          if($info=='ccresu' and ($tsaldo==9 or $tsaldo==98) ){$xgroupby2='group by docum,numer ';}
          
          if($info=='cccred'){$xgroupby2='group by docum,ptoem,numer ';}
//           if($info=='cccred' and ($tsaldo==9 or $tsaldo==98) ){$xgroupby2='group by docum,numer ';}
          
          if($info=='ccdeta'){$xgroupby2='group by docum,ptoem,numer,idnum1 ';}
          if($info=='cpdeta'){$xgroupby2='group by docum,ptoem,numer,idnum1 ';}
          
          if($info=='cpresu'){$xgroupby2='group by docum,ptoem,numer ';}
          if($info=='cpresu' and ($tsaldo==9 or $tsaldo==98) ){$xgroupby2='group by docum,numer ';}
          
          if($info=='ccmovi'){$xgroupby2='group by idnum1x,docux ';}
          if($info=='cpmovi'){$xgroupby2='group by idnum1x,docux ';}
          if($info=='ccanti'){$xgroupby2='group by idnum1x,docux ';}
          if($info=='cpanti'){$xgroupby2='group by idnum1x,docux ';}
          if($info=='ccdcdd'){$xgroupby2='group by fecha ';}
          if($info=='cpdcdd'){$xgroupby2='group by fecha ';}
          if($info=='ccdcmm'){$xgroupby2='group by left(fecha,7) ';}
          if($info=='cpdcmm'){$xgroupby2='group by left(fecha,7) ';}
          $sql_info.=$xgroupby2;
          
          
//           $xorderby2='';
//           $xorderby2='order by fecha  ';     
//           $sql_info.=$xorderby2;
          
          
     }
          
//      }
  
     // ORDER BY
     
     if(substr($info,0,2)!='mt')
     {
          
          
     if($modu=='btcr')
     {
          $xorderby='order by fecha,idnum1 ';
          if($info=='btblog'){$xorderby='order by idn*1 desc';}
          if($info=='btdet2'){$xorderby='order by idn*1 desc';}
          if($info=='bthist'){$xorderby='order by fecha desc, idn desc';}
          $sql_info.=$xorderby;
          
     }
     
     if($modu=='plan')
     {
          $xorderby='order by fecha,idnum1 ';
          if($info=='plblog'){$xorderby='order by fecha,idnum1 ';}
          if($info=='plxsem'){$xorderby='order by usuario,asema ';}
          $sql_info.=$xorderby;
     }
     
     if($modu=='atvd')
     {
          $xorderby='';
          if($info=='atblog'){$xorderby='order by fecha,idnum1 ';}
          if($info=='atxsem'){$xorderby='order by usuario,asema ';}
          $sql_info.=$xorderby;
     }
     
          
     if($modu=='hcli')
     {
          $xorderby='';
//           if($info=='hcblog'){$xorderby='order by fecha,idnum1 ';}
          if($info=='hcblog'){$xorderby='order by fecha,idnum1 ';}
          if($info=='hcresu'){$xorderby='order by docu,ptoe,numero ';}
          if($info=='hcdeta'){$xorderby='order by docu,ptoe,numero ';}
          if($info=='hchcli'){$xorderby='order by docu,ptoe,numero ';}
          $sql_info.=$xorderby;
     }
     
     
     if($modu=='otbj')
     {
          $xorderby='';
          if($info=='otblog'){$xorderby='order by  ';}
          if($info=='otresu'){$xorderby='order by docu,ptoe,numero ';}
          if($info=='otdeta'){$xorderby='order by idnum1,iddet ';}
          if($info=='otdet2'){$xorderby='order by idnum1,iddet ';}
          if($info=='othtec'){$xorderby='order by docu,ptoe,numero,fecha1,hora1 ';}
          $sql_info.=$xorderby;
          
          
     }
     
     
     if($modu=='cont')
     {
          $xorderby='';
          if($info=='ttmovi'){$xorderby='order by ctas,fecha,idnum1x ';}
          if($info=='ttresu'){$xorderby='order by fecha,mdfrom,idnum1 ';}
          if($info=='ttdeta'){$xorderby='order by fecha,mdfrom,idnum1,iddet ';}
          $sql_info.=$xorderby;
     }
     
     if($modu=='guia' or $modu=='opro')
     {
          $xorderby='order by enti_descri ';
          if(mb_stristr2($orden,'Nombre')){$xorderby='order by enti_descri,enti ';}
          if(mb_stristr2($orden,'Item')){$xorderby='order by item_descri,item ';}
          if(mb_stristr2($orden,'Zona')){$xorderby='order by enti_zsub,enti_descri,enti ';}
          if(mb_stristr2($orden,'Linea')){$xorderby='order by item_lsub,item_descri,item_codigo ';}
          if($info=='guresu'){$xorderby='order by docu,ptoe,numero ';}
          if($info=='opresu'){$xorderby='order by docu,ptoe,numero ';}
          
          if(mb_stristr2($info,'det1')){$xorderby='order by idnum1,iddet ';}
          if(mb_stristr2($info,'det2')){$xorderby='order by idnum1,iddet ';}
          if(mb_stristr2($info,'det3')){$xorderby='order by idnum1,iddet ';}
          $sql_info.=$xorderby;
     }
     
     if($modu=='vtas' or $modu=='clie')
     {
          $xorderby='order by enti_descri ';
//           if(mb_stristr2($orden,'Nombre')){$xorderby='order by enti_descri,enti ';}
//           if(mb_stristr2($orden,'Item')){$xorderby='order by item_descri,item ';}
//           if(mb_stristr2($orden,'Zona')){$xorderby='order by enti_zsub,enti_descri,enti ';}
//           if(mb_stristr2($orden,'Linea')){$xorderby='order by item_lsub,item_descri,item_codigo ';}
          if($info=='vtresu'){$xorderby='order by docu,ptoe,numero ';}
          if($info=='vtres1'){$xorderby='order by docu,ptoe,numero ';}
          if($info=='clresu'){$xorderby='order by docu,ptoe,numero ';}
          if($info=='caresu'){$xorderby='order by docu,ptoe,numero ';}
          
          
          if(mb_stristr2($orden,'Nombre')){$xorderby=str_replace('order by', 'order by enti_descri,enti,',$xorderby)  ;}
          if(mb_stristr2($orden,'Item')){$xorderby=str_replace('order by', 'order by item_descri,item,',$xorderby)  ;}
          if(mb_stristr2($orden,'Zona')){$xorderby=str_replace('order by', 'order by enti_zsub,zsub,',$xorderby)  ;}
          if(mb_stristr2($orden,'Linea')){$xorderby=str_replace('order by', 'order by item_lsub,item_descri,item_codigo,',$xorderby)  ;}
          
          
          if($info=='vtdeta'){$xorderby='order by docu,ptoe,numero ';}
          
          if($info=='vtenti'){$xorderby='order by enti ';}
          
          if($info=='vtxano'){$xorderby='order by fecha ';}
          if($info=='vtxmes'){$xorderby='order by fecha ';}
          if($info=='vtxfec'){$xorderby='order by fecha ';}
          if($info=='vtxsem'){$xorderby='order by asema ';}
          
          if($info=='vtxiva'){$xorderby='order by iva ';}
          if($info=='vtptov'){$xorderby='order by ptov ';}
          if($info=='vtvndd'){$xorderby='order by vndd_descri,vndd ';}
          if($info=='vtenxm'){$xorderby='order by enti_descri,fecha ';}
          if($info=='vtvnxm'){$xorderby='order by vndd_descri,fecha ';}
          if($info=='vtanxm'){$xorderby='order by fecha ';}
          if($info=='vtitxm'){$xorderby='order by item_lsub,item_descri,fecha ';}
          if($info=='vtlsxm'){$xorderby='order by lsub_descri,fecha ';}
//           if($info=='vtrpm1'){$xorderby='order by item_descri ';}
//           if($info=='vtrpm1'){$xorderby='order by item_rgsn,item_descri ';}
          if(mb_stristr2($info,'det1')){$xorderby='order by idnum1,iddet ';}
          if(mb_stristr2($info,'det2')){$xorderby='order by idnum1,iddet ';}
          if(mb_stristr2($info,'det3')){$xorderby='order by idnum1,iddet ';}
          $sql_info.=$xorderby;
          
     }
     
     if($modu=='comp' or $modu=='prov' or $modu=='lpro')
     {
          $xorderby='order by enti_descri, fecha ';
          if(mb_stristr2($orden,'Código')){$xorderby='order by item_codigo ';}
          if(mb_stristr2($orden,'Nombre')){$xorderby='order by enti_descri,enti ';}
          if($info=='iiddbb'){$xorderby='order by fecha,idnum1,iddet ';}
          if($info=='coresu'){$xorderby='order by fecha,docu,ptoe,numero ';}
          if($info=='codeta'){$xorderby='order by enti_descri,fecha,docu,ptoe,numero ';}
          if($info=='cores1'){$xorderby='order by fecha,docu,ptoe,numero ';}
          if($info=='coxano'){$xorderby='order by fecha ';}
          if($info=='coanxm'){$xorderby='order by fecha ';}
          if($info=='coxano'){$xorderby='order by fecha ';}
          if($info=='coxmes'){$xorderby='order by fecha ';}
          
          if($info=='coctas'){$xorderby='order by ctas ';}
          if($info=='cocair'){$xorderby='order by cair ';}
          if($info=='cociva'){$xorderby='order by iva*1 ';}
          if($info=='cocaxm'){$xorderby='order by cair ';}
          if($info=='coitxm'){$xorderby='order by item ';}
          if($info=='coenxm'){$xorderby='order by enti ';}
          if($info=='coctxm'){$xorderby='order by ctas ';}

          
//           if($info=='prresu'){$xorderby='order by fecha,docu,ptoe,numero ';}
          
          
          if($info=='prblog'){$xorderby='order by fecha, idnum1_hide desc, iddet_hide desc ';}
          if($info=='lpblog'){$xorderby='order by fecha, idnum1_hide desc, iddet_hide desc ';}
          
//           echo '<br>==== ';
//           echo '<br>==== ';
//           echo '<br>==== '.$sql_info;
//           echo '<br>==== ';
//           echo '<br>==== ';
          
          
          $sql_info.=$xorderby;
     }
     
     if($modu=='caja')
     {
          if(mb_stristr2($orden,'Código')){$xorderby='order by caja_codigo ';}
          if(mb_stristr2($orden,'Nombre')){$xorderby='order by caja_descri,caja_codigo ';}
          if($info=='cjdocu'){$xorderby='order by caja,fecha,signo,docu,numero ';}
          if($info=='cjdate'){$xorderby='order by caja,fecha ';}
          if($info=='cjblog'){$xorderby='order by fecha,idnum1 desc ';}
          if($info=='cjddbb'){$xorderby='order by fecha,idnum1,iddet ';}
          if($info=='cjmovi'){$xorderby='order by caja,fecha,signo desc,idnum1x ';}
          if($info=='cjblog'){$xorderby='order by idnum1,iddet ';}
          if($info=='cjresu'){$xorderby='order by fecha, docu ';}
          $sql_info.=$xorderby;
     }
          
     if($modu=='bcos')
     {
          if(mb_stristr2($orden,'Código')){$xorderby='order by ctab_codigo ';}
          if(mb_stristr2($orden,'Nombre')){$xorderby='order by ctab_descri,ctab_codigo ';}
          if($info=='bcdocu'){$xorderby='order by ctab,fecha,signo,docu,numero ';}
          if($info=='bcdate'){$xorderby='order by ctab,fecha ';}
          if($info=='bcblog'){$xorderby='order by fecha,idnum1 desc ';}
          if($info=='bcddbb'){$xorderby='order by fecha,idnum1,iddet ';}
          if($info=='bcmovi'){$xorderby='order by ctab,fecha,signo desc,idnum1x ';}
          if($info=='bcblog'){$xorderby='order by idnum1,iddet ';}
          if($info=='bcresu'){$xorderby='order by fecha, docu ';}
          $sql_info.=$xorderby;
     }
          
     
     if($modu=='inve' or $modu=='infi')
     {
          if(mb_stristr2($orden,'Código')){$xorderby='order by item_codigo ';}
          if(mb_stristr2($orden,'Nombre')){$xorderby='order by item_descri,item_codigo ';}
          if(mb_stristr2($orden,'Linea')){$xorderby='order by item_lsub,item_descri,item_codigo ';}
          if($info=='iiddcc'){$xorderby='order by item_lsub ';}
          if($info=='iidcdd'){$xorderby='order by fecha ';}
          if($info=='iidcmm'){$xorderby='order by mes ';}
          if($info=='iiresu'){$xorderby='order by fecha,bode,docu,numero ';}
          if($info=='iideta'){$xorderby='order by fecha,bode,docu,numero ';}
          if($info=='iiddbb'){$xorderby='order by fecha,idnum1,iddet ';}
          if($info=='iikrdx'){$xorderby='order by item,fecha,signo desc,idnum1x ';}
          if($info=='iiblog'){$xorderby='order by idnum1,iddet ';}
          if($info=='ifblog'){$xorderby='order by idnum1,iddet ';}
          if($info=='iisxlt'){$xorderby='order by item_lsub,item,lote ';}
          if($info=='iiie11'){$xorderby='order by fecha, docu desc ';}
          if($info=='iiiecf'){$xorderby='order by cliente,fecha ';}
          if($info=='iidet1'){$xorderby='order by fecha desc,enti,item ';}
          if($info=='iietbb'){$xorderby='order by bode,item_lsub,item_descri ';}
          if($info=='iicuxc'){$xorderby='order by item_lsub, item, fecha desc ';}
          $sql_info.=$xorderby;
     }
          
     
     if(mb_stristr2('.cxco.cxpa',$modu))
     {
          
          $xorderby='order by fecha ';
          if(mb_stristr2($orden,'Fecha')){$xorderby='order by fecha,pto,numero ';}
          if(mb_stristr2($orden,'Código')){$xorderby='order by enti,fecha ';}
          if(mb_stristr2($orden,'Nombre')){$xorderby='order by enti_descri,enti,fecha ';}
          if(mb_stristr2($orden,'Zona')){$xorderby='order by enti_zsub,enti_descri,enti,fecha ';}
          if(mb_stristr2($orden,'T.Prov.')){$xorderby='order by enti_tpro,enti_descri,enti,fecha ';}
//           if($info=='ccddcc'){$xorderby='order by idnum1, iddet ';}
          
//           if(empty($orden))
          {
//                $xorderby='';
               if($info=='ccmovi'){$xorderby='order by fecha,idate,headet,mdfrom,idate ';}
               if($info=='cpmovi'){$xorderby='order by fecha,headet,mdfrom,idate ';}
               
               if($info=='ccanti'){$xorderby='order by fecha,headet,mdfrom,idate ';}
               if($info=='cpanti'){$xorderby='order by fecha,headet,mdfrom,idate ';}
               
               if($info=='ccdeta'){$xorderby='order by docu1_hide,headet,fecha ';}
               if($info=='cpdeta'){$xorderby='order by docu1_hide,headet,fecha ';}
               
//                if($info=='cpdcdd'){$xorderby='order by fecha ';}
//                if($info=='ccdcdd'){$xorderby='order by fecha ';}
               if($info=='cpdcmm'){$xorderby='order by left(fecha,7) ';}
               if($info=='ccdcmm'){$xorderby='order by left(fecha,7) ';}
          }
          
//           if(empty($orden))
          {
//                if($info=='ccmovi'){$xorderby.='idate,headet,mdfrom,idate ';}
//                if($info=='cpmovi'){$xorderby.='headet,mdfrom,idate ';}
//                if($info=='ccanti'){$xorderby.='headet,mdfrom,idate ';}
//                if($info=='cpanti'){$xorderby.='headet,mdfrom,idate ';}
//                if($info=='ccresu'){$xorderby.='ptoe,numero ';}
//                if($info=='cpresu'){$xorderby.='ptoe,numero ';}
//                if($info=='cpdcdd'){$xorderby.='fecha ';}
//                if($info=='ccdcdd'){$xorderby.='fecha ';}
//                if($info=='cpdcmm'){$xorderby.='left(fecha,7) ';}
//                if($info=='ccdcmm'){$xorderby.='left(fecha,7) ';}
          }
          
          
          
//           if($info=='iikrdx'){$xorderby='order by item,fecha,signo desc,idnum1x ';}
          $sql_info.=$xorderby;
//           echo '===='.$xorderby;
     }

          
     if(mb_stristr2('.retp.retc',$modu))
     {
          if($info=='rpresu'){$xorderby='order by fecha,ptoe,numero ';}
          if($info=='rcresu'){$xorderby='order by fecha,ptoe,numero ';}
          
          if($info=='rprrta'){$xorderby='order by cair ';}
          if($info=='rpriva'){$xorderby='order by porc_iva ';}
          if($info=='rcrrta'){$xorderby='order by cair ';}
          if($info=='rcriva'){$xorderby='order by porc_iva ';}
          
          if($info=='rprrxm'){$xorderby='order by left(fecha,7),cair ';}
          if($info=='rprixm'){$xorderby='order by left(fecha,7),porc_iva ';}
          if($info=='rcrrxm'){$xorderby='order by left(fecha,7),cair ';}
          if($info=='rcrixm'){$xorderby='order by left(fecha,7),porc_iva ';}
          
//           if($info=='iikrdx'){$xorderby='order by item,fecha,signo desc,idnum1x ';}
          $sql_info.=$xorderby;
     }

     if(mb_stristr2('.susa.nomi',$modu))
     {
          if($info=='sublog'){$xorderby='order by fecha,idnum1_hide desc, iddet_hide desc ';}
          if($info=='sudeta'){$xorderby='order by fecha desc,idnum1 desc, iddet desc ';}
          if($info=='sudet2'){$xorderby='order by fecha desc,idnum1 desc, iddet desc ';}
//           if($info=='rcresu'){$xorderby='order by fecha,ptoe,numero ';}
//           if($info=='iikrdx'){$xorderby='order by item,fecha,signo desc,idnum1x ';}
          $sql_info.=$xorderby;
     }

     if(mb_stristr2('.jwib',$modu))
     {
          $xorderby='order by nombre, fecha ';
          if($info=='mmresu'){$xorderby='order by f_lb,nombre,f_m1,nombre,f_m2,nombre,f_m3,nombre,f_m4,nombre ';}
          if($info=='asresu'){$xorderby='order by nombre ';}
          if($info=='vcresu'){$xorderby='order by nombre ';}
          if($info=='jwmdc0'){$xorderby='order by ndia,hora,fecha1,ndia ';}
          $sql_info.=$xorderby;
     }

     if(mb_stristr2('.jwrv.jwrt',$modu))
     {
          $xorderby='order by trty, fecha ';
          if($info=='rvdeta'){$xorderby='order by trty,idnum1 ';}
          if($info=='rvblog'){$xorderby='order by idnum1 ';}
          if($info=='rtdeta'){$xorderby='order by trty,idnum1 ';}
          if($info=='rtblog'){$xorderby='order by idnum1 ';}
          $sql_info.=$xorderby;
     }

     if(mb_stristr2('.jwmc',$modu))
     {
          if($info=='jwmdc0'){$xorderby='order by ndia,hora,fecha1,ndia ';}
//           if($info=='rcresu'){$xorderby='order by fecha,ptoe,numero ';}
//           if($info=='iikrdx'){$xorderby='order by item,fecha,signo desc,idnum1x ';}
//           if($info=='mcprog'){$xorderby='order by fecha,hora,trty ';}
          if($info=='mcprog'){$xorderby='order by sema,hora,trty ';}
          $sql_info.=$xorderby;
     }

     if($modu=='soft' and mb_stristr2('.sbresu.sbcale',$info))
       {
         $xorderby='order by jornada, fecha_hide, idnum1 ';
         $sql_info.=$xorderby;
       }

     if($info=='lidet2')
         {
           $xorderby='order by idnum2, iddet ';
           $sql_info.=$xorderby;
         }

     }

     
     
     // FILTROS ADICIONALES
     $sql_info0=$sql_info;
     
     if($modu=='cont')
     {
          if($info=='ttsald' or $info=='ttsaln')
          {
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';
               $sql_info.='where abs(saldo)>'.$saldo0.' ';
          }
          
          
          if($info=='ttssss')
          {
               $saldo0=0;
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';
               $sql_info.='where ';
               $sql_info.='(abs(inicial)>'.$saldo0.' or abs(debito)>'.$saldo0.' or abs(credito)>'.$saldo0.' or abs(saldo)>'.$saldo0.') ';
               
          }
          
          if($info=='ttssnn')
          {
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';
               $sql_info.='where ';
               $sql_info.='(abs(inicial)>'.$saldo0.' or abs(debito)>'.$saldo0.' or abs(credito)>'.$saldo0.' or abs(saldo)>'.$saldo0.'or dd00_hide!=0)';
          }
          
          if($info=='ttresu' and $difer==1)
          {
//                $sql_info='select * from ('.$sql_info.') ';
//                $sql_info.='as maes1 ';
//                $sql_info.='where ';
//                $sql_info.='difer !=0 ';
          }
          
     }

     
     if($modu=='vtas')
     {
          if(!empty($cobro))
          {
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';
               $sql_info.='where ('.$cobro.')>0 ';
          }
          
     }
     
     
     if($modu=='comp')
     {
          if($newp=='true')
          {
//                $fecha_newp=date('Y-m-d',strtotime($fecha1));
               
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';
               $sql_info.='where (left(enti,10) not in (select left(enti,10) from comp1 where ifnull(status,"")not in("O","X") and fecha<"'.$fecha1.'" )) ';
          }
          
     }
     
     
     if($modu=='inve' and !empty($tsaldo))
     {
          if($info=='iillcc' and $stock=='true')
          {
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';
               if($tsaldo==6) {$sql_info.='where stock<0 ';}
               if($tsaldo==8) {$sql_info.='where stock=0 ';}
               if($tsaldo==9) {$sql_info.='where stock>0 ';}
               if($tsaldo==68){$sql_info.='where stock<=0 ';}
               if($tsaldo==69){$sql_info.='where stock!=0 ';}
               if($tsaldo==98){$sql_info.='where stock>=0 ';}
          }
          
          if($info=='iiiiee' or $info=='iisxlt')
          {
               $sql_info='select * ';
               
//                $sql_info.=', vta_con/((0';
//                $sql_info.=', ((1';
//                for($cmes=12; $cmes<=12; $cmes++)
//                {$sql_info.='+stk_mes_'.$cmes.'';}
//                $sql_info.=')) ';
//                $sql_info.='as prueba1 ';
//                echo '*/*/*/'.$sql_info;
               
               $sql_info.=', vta_con_hide/((0';
               for($cmes=1; $cmes<=12; $cmes++)
               {$sql_info.='+ifnull(stk_mes_'.$cmes.'_hide,0)';}
               $sql_info.=')/12) ';
               $sql_info.='as i_rot ';
               
               $sql_info.=', convert(truncate(365/(vta_con_hide/((0';
               for($cmes=1; $cmes<=12; $cmes++)
               {$sql_info.='+ifnull(stk_mes_'.$cmes.'_hide,0)';}
               $sql_info.=')/12)),0),char)*1 ';
               $sql_info.='as d_rot ';
               
               $sql_info.='from ('.$sql_info0.') ';
               
               $sql_info.='as maes1 ';
            
//                $sql_info.='where (inicial!=0 or ingreso!=0 or egreso!=0 or stock!=0)';

               if($cost=='false')
               {
                    if(empty($tsaldo)){$sql_info.='where (inicial!=0 or ingreso!=0 or egreso!=0 or stock!=0)';}
                    if($tsaldo==6) {$sql_info.='where (inicial<0 or stock<0) ';}
                    if($tsaldo==8) {$sql_info.='where (inicial=0 or stock=0) ';}
                    if($tsaldo==9) {$sql_info.='where (inicial>0 or stock>0) ';}
                    if($tsaldo==68){$sql_info.='where (inicial<=0 or stock<=0) ';}
                    if($tsaldo==69){$sql_info.='where (inicial!=0 or stock!=0) ';}
                    if($tsaldo==98){$sql_info.='where (inicial>=0 or stock>=0) ';}
               }
               
               if($cost=='true')
               {
                    $sql_info.='where (abs(inicial_$)>"'.$saldo0.'" or abs(debito_$)>"'.$saldo0.'" or abs(credito_$)>"'.$saldo0.'" or abs(saldo_$)>"'.$saldo0.'" )';
//                     echo '==='.$saldo0;
//                     $sql_info.='where (inicial!=0 or ingreso!=0 or egreso!=0 or stock!=0)';
//                     if(empty($tsaldo)){$sql_info.='where (inicial!=0 or ingreso!=0 or egreso!=0 or stock!=0)';}
//                     if($tsaldo==6) {$sql_info.='where (inicial<0 or stock<0) ';}
//                     if($tsaldo==8) {$sql_info.='where (inicial=0 or stock=0) ';}
//                     if($tsaldo==9) {$sql_info.='where (inicial>0 or stock>0) ';}
//                     if($tsaldo==68){$sql_info.='where (inicial<=0 or stock<=0) ';}
//                     if($tsaldo==69){$sql_info.='where (inicial!=0 or stock!=0) ';}
//                     if($tsaldo==98){$sql_info.='where (inicial>=0 or stock>=0) ';}
               }
               
               
          }
         
          if(mb_stristr2('.iibode.iidocu.iisxlt',$info))
          {
               
               $sql_info='select * ';
               
               if($info=='iibode')
               {
                    $sql_info.=', vta_con_hide/((0';
                    for($cmes=1; $cmes<=12; $cmes++)
                    {$sql_info.='+ifnull(stk_mes_'.$cmes.'_hide,0)';}
                    $sql_info.=')/12) ';
                    $sql_info.='as i_rot ';

                    $sql_info.=', convert(truncate(365/(vta_con_hide/((0';
                    for($cmes=1; $cmes<=12; $cmes++)
                    {$sql_info.='+ifnull(stk_mes_'.$cmes.'_hide,0)';}
                    $sql_info.=')/12)),0),char)*1 ';
                    $sql_info.='as d_rot ';
                    
               }
               
               $sql_info.='from ('.$sql_info0.') ';
               
//                $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';
               if($tsaldo==6) {$sql_info.='where stock<0 ';}
               if($tsaldo==8) {$sql_info.='where stock=0 ';}
               if($tsaldo==9) {$sql_info.='where stock>0 ';}
               if($tsaldo==68){$sql_info.='where stock<=0 ';}
               if($tsaldo==69){$sql_info.='where stock!=0 ';}
               if($tsaldo==98){$sql_info.='where stock>=0 ';}
          }
          
//           $xgroupby3='';
//           if($info=='iiiiee'){$xgroupby3='group by item ';}
//           $sql_info.=$xgroupby3;
               
          
          
     }

     
     
//      if(mb_stristr2('.cxco.cxpa',$modu) and !empty($tsaldo) )
     if(mb_stristr2('.cxco.cxpa',$modu))
     {
//           $saldo0=.5;
          if(($info=='ccsald' or $info=='cpsald'))
          {
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';

               $sql_info.='where 1=1 ';
               
               if(!empty($vndd))
               {$sql_info.='and enti_vndd_hide like "'.$vndd.'%" ';}
               
               if(!empty($rcdd))
               {$sql_info.='and enti_rcdd_hide like "'.$rcdd.'%" ';}
               
               if(!empty($tsaldo))
               {
                    if($tsaldo==6){$sql_info.='and (saldo<'.$saldo0*(-1).' or anticipo<'.$saldo0*(-1).') ';}
                    if($tsaldo==8){$sql_info.='and (abs(saldo)<'.$saldo0.' and abs(anticipo)<'.$saldo0.') ';}
                    if($tsaldo==9){$sql_info.='and (saldo>'.$saldo0.' or anticipo>'.$saldo0.') ';}
//                     if($tsaldo==68){$sql_info.='and (saldo<='.$saldo0*(-1).' or anticipo<='.$saldo0*(-1).')';}
                    if($tsaldo==68){$sql_info.='and (saldo<=0 or anticipo<=0) ';}
                    if($tsaldo==69){$sql_info.='and (abs(saldo)>'.$saldo0.' or abs(anticipo)>'.$saldo0.') ';}
//                     if($tsaldo==98){$sql_info.='and (saldo>='.$saldo0.' or anticipo>='.$saldo0.')';}
                    if($tsaldo==98){$sql_info.='and (saldo>=0 or anticipo>=0) ';}
               }
               
               
               if($info=='ccsald' and $anti=='true')
               {
                    $nom_ttbl = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 10);
                    $sql_ttbl='create temporary table '.$nom_ttbl.' as ('.$sql_info.')';
                    $res_ttbl=msql_query($sql_ttbl,$link_data);
                    
                    
                    $tfields='';
                    $sql_dtbl='describe '.$nom_ttbl;
                    $res_dtbl=msql_query($sql_dtbl,$link_data);
                    while($aso_dtbl=msql_fetch_assoc($res_dtbl))
                    {
                         $tsuma=0;
                         $tmaxi=0;
                         if(mb_stristr2('.saldo.anticipo,cheque.protesto',$aso_dtbl['Field']) or substr($aso_dtbl['Field'],0,2)=='A_'){$tsuma=1;}
                         if(mb_stristr2('.u_movi',$aso_dtbl['Field'])){$tmaxi=1;}
                         
                         if($tsuma==1){$tfields.='sum(';}
                         if($tmaxi==1){$tfields.='max(';}
                         $tfields.=$aso_dtbl['Field'].'';
                         if($tsuma==1 or $tmaxi==1){$tfields.=') as '.$aso_dtbl['Field'].'';}
                         $tfields.=',';
                         
                    }
                    
                    
                    $sql_newi='';
                    $sql_newi.='select zona,enti,nombre,sum(saldo) as saldo, sum(anticipo) as anticipo, sum(cheque) as cheque, sum(protesto) as protesto,  ';
                    $betw1=1;
                    for($idx=0; $idx<count($dias_anti); $idx++)
                    {
                         $betw2=$dias_anti[$idx];
                         if($idx==count($dias_anti)){$betw2=9999;}
                         $sql_newi.='sum(A_'.$betw2.') as A_'.$betw2.', ';
                         $betw1=$dias_anti[$idx]+1;                    
                    }
                    $sql_newi.='vendedor, u_movi,st ';
                    $sql_newi.='from '.$nom_ttbl.' ';
                    $sql_newi.='group by left(enti,10) ';
                    
                    
                    $sql_newi='';
                    $sql_newi.='select '.$tfields.' ';
                    $sql_newi.='from '.$nom_ttbl.' ';
                    $sql_newi.='group by left(enti,10) ';
                    
                    
                    $sql_info=$sql_newi;
                    
                    
                    
               }
               
          }
          
          if(($info=='ccsacc' or $info=='cpsacc'))
          {
//                echo '<br><br>===='.$saldo0;
               
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';

               $sql_info.='where 1=1 ';
               
               if(!empty($vndd))
               {
                    $sql_info.='and enti_vndd_hide like "'.$vndd.'%" ';
               }
               
               if(!empty($tsaldo))
               {
                    if($tsaldo==6){$sql_info.='and (saldo_hide<'.$saldo0*(-1).' or anticipo<'.$saldo0*(-1).')';}
                    if($tsaldo==8){$sql_info.='and (abs(saldo_hide)<'.$saldo0.' and abs(anticipo)<'.$saldo0.')';}
                    if($tsaldo==9){$sql_info.='and (saldo_hide>'.$saldo0.' or anticipo>'.$saldo0.')';}
                    if($tsaldo==68){$sql_info.='and (saldo_hide<='.$saldo0*(-1).' or anticipo<='.$saldo0*(-1).')';}
                    if($tsaldo==69){$sql_info.='and (abs(saldo_hide)>'.$saldo0.' or abs(anticipo)>'.$saldo0.')';}
                    if($tsaldo==98){$sql_info.='and (saldo_hide>='.$saldo0.' or anticipo>='.$saldo0.')';}
               }
               
          }
          
          
          if($info=='ccddcc' or $info=='cpddcc')
          {
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';
               $sql_info.='where (abs(inicial)>'.$saldo0.' or abs(debito)>'.$saldo0.' or abs(credito)>'.$saldo0.' or abs(saldo)>'.$saldo0.')';
          }
          
          if($info=='ccresu' or $info=='cpresu' )
          {
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';
               $sql_info.='where docu not like "A%" ';
               
               if($tsaldo==6) {$sql_info.='and saldo<'.$saldo0*(-1).' ';}
               if($tsaldo==8) {$sql_info.='and abs(saldo)<='.$saldo0.' ';}
               if($tsaldo==9) {$sql_info.='and saldo>'.$saldo0.' ';}
               if($tsaldo==68){$sql_info.='and saldo<='.$saldo0*(-1).' ';}
               if($tsaldo==69){$sql_info.='and abs(saldo)>'.$saldo0.' ';}
               if($tsaldo==98){$sql_info.='and saldo>='.$saldo0.' ';}

               if($tsaldo==8) {$sql_info.='and fecha>="'.$fecha1o.'" ';}
               
               if(!empty($vndd)){$sql_info.='and vndd_hide like "%'.substr($vndd,0,10).'%" ';}
               if(!empty($rcdd)){$sql_info.='and rcdd_hide like "%'.substr($rcdd,0,10).'%" ';}
//                if(!empty($vndd)){$sql_info.='and enti_vndd_hide like "%'.substr($vndd,0,10).'%" ';}
               if(!empty($numero)){$sql_info.='and numero='.$numero.' ';}
               
          }
          
          
          if($info=='cccred' or $info=='cpcred' )
          {
               $sql_info='select * from ('.$sql_info.') ';
               $sql_info.='as maes1 ';
               $sql_info.='where docu not like "A%" ';
               
//                $sql_info.='and abs(total)!=abs(saldo) ';
               $sql_info.='and (ifnull(efecti,0) +ifnull(cheque,0) +ifnull(transf,0) +ifnull(n_cre,0) +ifnull(reten,0))>0 ';
               
//                if($tsaldo==6) {$sql_info.='and saldo<'.$saldo0*(-1).' ';}
//                if($tsaldo==8) {$sql_info.='and abs(saldo)<='.$saldo0.' ';}
//                if($tsaldo==9) {$sql_info.='and saldo>'.$saldo0.' ';}
//                if($tsaldo==68){$sql_info.='and saldo<='.$saldo0*(-1).' ';}
//                if($tsaldo==69){$sql_info.='and abs(saldo)>'.$saldo0.' ';}
//                if($tsaldo==98){$sql_info.='and saldo>='.$saldo0.' ';}
//                if($tsaldo==8) {$sql_info.='and fecha>="'.$fecha1o.'" ';}
               
               if(!empty($vndd)){$sql_info.='and vndd_hide like "%'.substr($vndd,0,10).'%" ';}
               if(!empty($rcdd)){$sql_info.='and rcdd_hide like "%'.substr($rcdd,0,10).'%" ';}
//                if(!empty($vndd)){$sql_info.='and enti_vndd_hide like "%'.substr($vndd,0,10).'%" ';}
               if(!empty($numero)){$sql_info.='and numero='.$numero.' ';}
               
          }
          
          
//           echo $sql_info;
          
          
          
          
     }
     
     
     if($info=='ttsaln' or $info=='ttssnn' or $info=='ttsscc' or $info=='ttssaa' or $info=='ttssff' or $info=='ttrrii' or $info=='ttsfcc' or $info=='ttricc')
     {
          $sql_orig=$sql_info;
          for($idx=$nivel-1; $idx>=1; $idx--)
          {
               $sql_nivel=$sql_orig;
               $sql_nivel=str_replace('select ctas_codi'.$nivel,'select ctas_codi'.$idx,$sql_nivel);
               $sql_nivel=str_replace('group by ctas_codi'.$nivel,'group by ctas_codi'.$idx, $sql_nivel);
               $sql_info.='union ';
               $sql_info.=$sql_nivel;
          }
          $sql_info.='order by ctas_hide ';
     }
     
     if($info=='ttssss' or $info=='ttsald' or $info=='ttsaln' or $info=='ttssnn' or $info=='ttssdd' or $info=='ttssmm' or $info=='ttsscc' or $info=='ttssaa' or $info=='ttssff' or $info=='ttrrii' or $info=='ttsfcc' or $info=='ttricc')
     {
          $sql_info2='';
          $sql_info2.='select ';
          $sql_info2.='ctas_tipogd as ctas_tipogd_hide,';
          $sql_info2.='ctas_hide as ctas,';
          $sql_info2.='ctas_descri as contable,';
          
//           if($empr0['empr_ctasuper']==1 or 1==1)
          {
               $sql_info2.='case ';
               $sql_info2.='when length(trim(ctas_codigo))>8 then 5 ';
               $sql_info2.='when length(trim(ctas_codigo))>6 then 4 ';
               $sql_info2.='when length(trim(ctas_codigo))>4 then 3 ';
               $sql_info2.='when length(trim(ctas_codigo))>2 then 2 ';
               $sql_info2.='when length(trim(ctas_codigo))>0 then 1 ';
               $sql_info2.='else 0 end as nivel_hide, ';
               
//                $sql_info2.='case ';
//                $sql_info2.='when length(trim(ctas_codigo))>8 then 5 ';
//                $sql_info2.='when length(trim(ctas_codigo))>6 then 4 ';
//                $sql_info2.='when length(trim(ctas_codigo))>4 then 3 ';
//                $sql_info2.='when length(trim(ctas_codigo))>2 then 2 ';
//                $sql_info2.='when length(trim(ctas_codigo))>0 then 1 ';
//                $sql_info2.='else 0 end as Xnivel, ';
               
          }
//           else
          {
//                $sql_contab.='left(ctas,1) as ctas_cod1,';
//                $sql_contab.='left(ctas,3) as ctas_cod2,';
//                $sql_contab.='left(ctas,5) as ctas_cod3,';
//                $sql_contab.='left(ctas,7) as ctas_cod4,';
//                $sql_contab.='ctas as ctas_cod5,';
          }
          
          
          
//           $sql_info2.='docu_,';
//           $sql_info2.='saldo as total,  ';
          $sql_info2.='qfull.*, ';
          
          if(mb_stristr2('.ttssff.ttrrii',$info))
          {
               if($nivel==2)
               {
               $sql_info2.='case when length(trim(ctas_codigo))>2  then saldo_hide else 0 end as saldo, ';
               $sql_info2.='case when length(trim(ctas_codigo))<=2 then saldo_hide else 0 end as total, ';
               }

               if($nivel==3)
               {
               $sql_info2.='case when length(trim(ctas_codigo))>4  then saldo_hide else 0 end as saldo, ';
               $sql_info2.='case when length(trim(ctas_codigo))<=4 then saldo_hide else 0 end as total, ';
               }

               if($nivel==4)
               {
               $sql_info2.='case when length(trim(ctas_codigo))>6  then saldo_hide else 0 end as saldo, ';
               $sql_info2.='case when length(trim(ctas_codigo))<=6 then saldo_hide else 0 end as total, ';
               }

               if($nivel==5)
               {
               $sql_info2.='case when length(trim(ctas_codigo))>8  then saldo_hide else 0 end as saldo, ';
               $sql_info2.='case when length(trim(ctas_codigo))<=8 then saldo_hide else 0 end as total, ';
               }
               
          }
          
          $sql_info2.='ctas_status as st ';
          $sql_info2.='from ('.$sql_info.') as qfull ';
          $sql_info2.='left join ctas0 on ctas_hide=ctas_codigo ';
//           $sql_info2.='left join docu0 on docu=docu_codigo';
          
//           if($info=='ttssff' or $info=='ttrrii')
//           if(1==2)
          if(mb_stristr2('.ttssff.ttrrii.ttsfcc.ttricc',$info))
          {
               $sql_info2.='where (1=1 ';
               
               if(mb_stristr2('.ttssff.ttrrii.',$info))
               {$sql_info2.='and abs(saldo_hide)>'.$saldo0.' ';}
               
//                if(mb_stristr2('.ttsfcc.ttricc',$info))
//                {$sql_info2.='and abs(saldo)>'.$saldo0.' ';}
               
               if(mb_stristr2('.ttsfcc.ttXricc',$info))
               {
                    $sql_info2.='and ( abs(inicial)>'.$saldo0.' ';
                    $sql_info2.='or abs(saldo)>'.$saldo0.' ';
                    $sql_info2.='or abs(total_hide)>'.$saldo0.' ) ';
               }
               
               if(mb_stristr2('.ttXsfcc.ttricc',$info))
               {$sql_info2.='and abs(total_hide)>'.$saldo0.' ';}
               
               
               $sql_info2.=') ';
               
               if($nivel==3){$sql_info2.='or length(trim(ctas_codigo))<=4 ';}
               if($nivel==4){$sql_info2.='or length(trim(ctas_codigo))<=6 ';}
               if($nivel==5){$sql_info2.='or length(trim(ctas_codigo))<=8 ';}
          }
          
          $sql_info=$sql_info2;
     }
     
     if($info=='vtrpm1' )
     {
          $sql_info2='';
          $sql_info2.='select ';
//           $sql_info2.='_c_,linea,item,producto,cant,';
          $sql_info2.='_c_,linea,';
          $sql_info2.='item,';
          $sql_info2.='producto,';
          $sql_info2.='cant,';
          $sql_info2.='umdd,';
          $sql_info2.='rgsn_codreg as reg_san, ';
          $sql_info2.='rgsn_nomcom as nom_com, ';
          $sql_info2.='rgsn_codpre as cod_pre, ';
          $sql_info2.='rgsn_precom as pre_com, ';
          $fields='';
          {
          // unidades x caja
          $fields.='case ';
          $fields.='when rgsn_precom like "CAJA X%" then substring(trim(rgsn_precom),7,100)*1 ';
          $fields.='else "-" end ';
          $fields.='as fxc, ';
          
          // prec_fac x caja
          $fields.='cast(case ';
//           $fields.='when rgsn_precom like "CAJA X%" then (pre_fac)*(substring(trim(rgsn_precom),7,100)*1)';
          $fields.='when rgsn_precom like "CAJA X%" and umdd not like "CAJA%" then (pre_fac)*(substring(trim(rgsn_precom),7,100)*1)';
          $fields.='else (pre_fac) end as decimal(5,2)) ';
          $fields.='as pre_fac, ';
          
          // prec_vta x caja
//           $fields.='cast(case ';
// //           $fields.='when rgsn_precom like "CAJA X%" then (pre_vta)*(substring(trim(rgsn_precom),7,100)*1)';
//           $fields.='when rgsn_precom like "CAJA X%" and umdd not like "CAJA%" then (pre_vta)*(substring(trim(rgsn_precom),7,100)*1)';
//           $fields.='else max(pre_vta) end as decimal(5,2)) ';
//           $fields.='as pre_vta, ';
//           $fields.='pvp1 as pre_vta, ';
          $fields.='cast(case ';
//           $fields.='when rgsn_precom like "CAJA X%" then (pvp1)*(substring(trim(rgsn_precom),7,100)*1)';
          $fields.='when rgsn_precom like "CAJA X%" and umdd not like "CAJA%" then (pvp1)*(substring(trim(rgsn_precom),7,100)*1)';
          $fields.='else max(pvp1) end as decimal(5,2)) ';
          $fields.='as pre_vta, ';

               
               
               
//           $fields.='sum(case when puni*(1-ifnull(dcto,0)/100)>item_puni1 then 1 else 0 end) as Obs,';
          $fields.='obs,';
          
//           // cajas
          $fields.='case ';
//           $fields.='when rgsn_precom like "CAJA X%" then (sum(cant)div(substring(trim(rgsn_precom),7,100)*1))';
          $fields.='when rgsn_precom like "CAJA X%" and umdd not like "CAJA%" then (sum(cant)div(substring(trim(rgsn_precom),7,100)*1))';
          $fields.='else sum(cant) end ';
          $fields.='as undds, ';
          
//           // fracciones
          $fields.='case ';
//           $fields.='when rgsn_precom like "CAJA X%" then mod(sum(cant),(substring(trim(rgsn_precom),7,100)*1))';
          $fields.='when rgsn_precom like "CAJA X%" and umdd not like "CAJA%" then mod(sum(cant),(substring(trim(rgsn_precom),7,100)*1))';
//           $fields.='else sum(cant) end ';
          $fields.='else 0 end ';
          $fields.='as fracc ';
          
               
          }
          $sql_info2.=$fields;          
          
          
          
          
          $sql_info2.='from ('.$sql_info.') as qfull ';
          $sql_info2.='left join rgsn0 on regi_sani=rgsn_codigo ';
//           $sql_info2.='where umdd not like "UNI%" ';
          $sql_info2.='group by item ';
          $sql_info2.='order by rgsn_nomcom,rgsn_precom,producto ';
          $sql_info=$sql_info2;
     }
     
     if(mb_stristr2($info,'resu'))
     {
          $sql_info2='';
          $sql_info2.='select qfull.*,';
          $sql_info2.='convert(cont1.idnum1,char) as idc ';
          $sql_info2.='from ('.$sql_info.') as qfull ';
          $sql_info2.='left join cont1 on qfull.idnum1_hide=cont1.idfrom ';
          $sql_info2.='where upper(cont1.mdfrom)= "'.strtoupper($modu).'" ';
          $sql_info2.='and ifnull(cont1.status,"")!= "X" ';
          
//           if(mb_stristr2('.vtas.comp.caja.bcos.dxco.dxpa.inve.prod.nomi',$modu))
//           {$sql_info=$sql_info2;}
     }
     
//      echo '*** '.$sql_info;
     
}

if($modu!='cont')
{
// $sql_info=str_replace(','.$xround.')',' ',$sql_info);
// $sql_info=str_replace('round(',' ',$sql_info);
// $sql_info=str_replace('round (','round(',$sql_info);
}


if(substr($info,0,2)=='mt')
{
     $mtlimit='limit 10000 ';
     if($empr0['empr_codigo']=='gsas'){$mtlimit='limit 3000 ';}
     if($modu=='ciex'){$mtlimit='limit 15000';}
     if($modu=='pcnt'){$mtlimit='limit 2000';}
//      if($modu=='rgsn'){$mtlimit='limit 15000';}
     $sql_info.=$mtlimit.' ';
}







?>