<?php

function set_varglob($nombre,$vdefa=''){set_var_glob(${$nombre},$nombre,$vdefa);}
function set_var_glob(&$variable, $nombre, $val_defa=null)
{
    if (!isset($GLOBALS[$nombre]))
    {
        $GLOBALS[$nombre] = $val_defa;
        $variable = $val_defa;
    }
    else
    {$variable = $GLOBALS[$nombre];}
}


function set_session($indice, $vdefa = null)
{
    // Asegura que la sesión esté activa
    if (session_status() !== PHP_SESSION_ACTIVE) {session_start();}
    if (!isset($_SESSION[$indice])) {$_SESSION[$indice] = $vdefa;}
}


function set_cookie($nombre, $vdefa=null, $timexp = 3600, $ruta = "/", $secure = false, $httpOnly = true)
{
    // Establece la cookie si no existe
    if (!isset($_COOKIE[$nombre]))
    {
        setcookie($nombre, $vdefa, time() + $timexp, $ruta, "", $secure, $httpOnly);
        $_COOKIE[$nombre] = $vdefa; // También lo establece en el superglobal $_COOKIE
    }
}





function verificarYDefinirArray(&$variable, $valorPredeterminado = []) {
    if (!isset($variable) || !is_array($variable)) {
        $variable = $valorPredeterminado;
    }
}







function set_comp1suco($docu,$idnum)
{
     
     if(empty($docu) or empty($idnum)){return;}
     if(!mb_stristr('FP10.FP03',$docu)){return;}
     
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     $empr0=data_maes('empr',$empr);
     if(empty($empr0['empr_sriats'])){return;}
     
     $docu1=data_docu($docu,$idnum);
     
     $xfecha0=date('Y-m-01',strtotime('-3 month'));
//      if($docu1['fecha']<$xfecha0){return;}
     
     
     
     $sql_comp='select * from comp1 where idnum1="'.$idnum.'" and ifnull(suco,0)*1=0 ';
     $res_comp=msql_query($sql_comp,$link_data);
     if(msql_num_rows($res_comp)==0){return;}
          
     $sql_comp ='select *,';
     $sql_comp.='abs(DATEDIFF("'.$docu1['fecha'].'",fecha)) as diasdif ';
     $sql_comp.='from comp1 ';
     $sql_comp.='where ifnull(status,"")not in("O","X") ';
     $sql_comp.='and enti like "'.substr($docu1['enti'],0,10).'%" ';
     $sql_comp.='and fecha like "'.substr($docu1['fecha'],0,7).'%" ';
     $sql_comp.='and ifnull(suco,0)*1>0 ';
     $sql_comp.='order by diasdif ';
     $res_comp=msql_query($sql_comp,$link_data);
     $comp12=msql_fetch_assoc($res_comp);
     
     if(empty($comp12['suco']))
     {
          $sql_comp ='select *,';
          $sql_comp.='abs(DATEDIFF("'.$docu1['fecha'].'",fecha)) as diasdif ';
          $sql_comp.='from comp1 ';
          $sql_comp.='where ifnull(status,"")not in("O","X") ';
          $sql_comp.='and enti like "'.substr($docu1['enti'],0,10).'%" ';
          $sql_comp.='and fecha like "'.substr($docu1['fecha'],0,4).'%" ';
          $sql_comp.='and ifnull(suco,0)*1>0 ';
          $sql_comp.='order by diasdif ';
          $res_comp=msql_query($sql_comp,$link_data);
          $comp12=msql_fetch_assoc($res_comp);
     }
     
     $sql_updt='update comp1 set suco="'.$comp12['suco'].'" ';
     $sql_updt.='where idnum1="'.$idnum.'" ';
     $sql_updt.='and ifnull(suco,0)*1=0 ';
     $res_updt=msql_query($sql_updt,$link_data);
     
//      echo '<br>=== '.$sql_updt;
     
     return;
}

function set_comp1fpag($docu,$idnum)
{
//  return;    
     if(empty($docu) or empty($idnum)){return;}
     if(!mb_stristr('FP10.FP03',$docu)){return;}
     
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     $empr0=data_maes('empr',$empr);
     if(empty($empr0['empr_sriats'])){return;}
     
     $docu1=data_docu($docu,$idnum);
     if(!empty($docu1['autornum'])){return;}
     
     $xfecha0=date('Y-m-01',strtotime('-3 month'));
//      if($docu1['fecha']<$xfecha0){return;}
     
     
     
     $sql_comp='select * from comp1 where idnum1="'.$idnum.'" and ifnull(fpag,0)*1=0 ';
     $res_comp=msql_query($sql_comp,$link_data);
     if(msql_num_rows($res_comp)==0){return;}
          
     $sql_comp ='select *,';
     $sql_comp.='abs(DATEDIFF("'.$docu1['fecha'].'",fecha)) as diasdif ';
     $sql_comp.='from comp1 ';
     $sql_comp.='where ifnull(status,"")not in("O","X") ';
     $sql_comp.='and enti like "'.substr($docu1['enti'],0,10).'%" ';
     $sql_comp.='and fecha like "'.substr($docu1['fecha'],0,7).'%" ';
     $sql_comp.='and ifnull(fpag,0)*1>0 ';
     $sql_comp.='order by diasdif ';
     $res_comp=msql_query($sql_comp,$link_data);
     $comp12=msql_fetch_assoc($res_comp);
     
     if(empty($comp12['fpag']))
     {
          $sql_comp ='select *,';
          $sql_comp.='abs(DATEDIFF("'.$docu1['fecha'].'",fecha)) as diasdif ';
          $sql_comp.='from comp1 ';
          $sql_comp.='where ifnull(status,"")not in("O","X") ';
          $sql_comp.='and enti like "'.substr($docu1['enti'],0,10).'%" ';
          $sql_comp.='and fecha like "'.substr($docu1['fecha'],0,4).'%" ';
          $sql_comp.='and ifnull(fpag,0)*1>0 ';
          $sql_comp.='order by diasdif ';
          $res_comp=msql_query($sql_comp,$link_data);
          $comp12=msql_fetch_assoc($res_comp);
     }

//      echo '<br>==== '.$sql_comp;
     
     
     $sql_updt='update comp1 set fpag="'.$comp12['fpag'].'" ';
     $sql_updt.='where idnum1="'.$idnum.'" ';
     $sql_updt.='and ifnull(fpag,0)*1=0 ';
     $res_updt=msql_query($sql_updt,$link_data);
     
//      echo '<br>==== '.$sql_updt;
     
     return;
}

function set_comp2cair($docu,$idnum)
{
     
     if(empty($docu) or empty($idnum)){return;}
     if(!mb_stristr('FP10.FP03',$docu)){return;}
     
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     $empr0=data_maes('empr',$empr);
     if(empty($empr0['empr_sriats'])){return;}
     
     $docu1=data_docu($docu,$idnum);
     if($docu1['fecha']<(date('Y')-1).'-01-01'){return;}
     
     $xfecha0=date('Y-m-01',strtotime('-3 month'));
//      if($docu1['fecha']<$xfecha0){return;}
     
     
     
     $sql_comp='select * from comp2 where idnum2="'.$idnum.'" and ifnull(cair,0)<300 ';
     $res_comp=msql_query($sql_comp,$link_data);
     if(msql_num_rows($res_comp)==0){return;}
          
     $sql_comp ='select *,';
     $sql_comp.='abs(DATEDIFF("'.$docu1['fecha'].'",fecha)) as diasdif ';
     $sql_comp.='from comp1 ';
     $sql_comp.='left join comp2 on idnum1=idnum2 ';
     $sql_comp.='where ifnull(status,"")not in("O","X") ';
     $sql_comp.='and enti like "'.substr($docu1['enti'],0,10).'%" ';
     $sql_comp.='and fecha like "'.substr($docu1['fecha'],0,7).'%" ';
//      $sql_comp.='and ifnull(cair,"")!="" ';
     $sql_comp.='and ifnull(cair,0)*1>300 ';
     $sql_comp.='order by diasdif ';
     $res_comp=msql_query($sql_comp,$link_data);
     $comp12=msql_fetch_assoc($res_comp);
     
     if(empty($comp12['cair']))
     {
          $sql_comp ='select *,';
          $sql_comp.='abs(DATEDIFF("'.$docu1['fecha'].'",fecha)) as diasdif ';
          $sql_comp.='from comp1 ';
          $sql_comp.='left join comp2 on idnum1=idnum2 ';
          $sql_comp.='where ifnull(status,"")not in("O","X") ';
          $sql_comp.='and enti like "'.substr($docu1['enti'],0,10).'%" ';
          $sql_comp.='and fecha like "'.substr($docu1['fecha'],0,4).'%" ';
//           $sql_comp.='and ifnull(cair,"")!="" ';
          $sql_comp.='and ifnull(cair,0)*1>300 ';
          $sql_comp.='order by diasdif ';
          $res_comp=msql_query($sql_comp,$link_data);
          $comp12=msql_fetch_assoc($res_comp);
     }
     
     
     $sql_updt='update comp2 set cair="'.$comp12['cair'].'" ';
     $sql_updt.='where idnum2="'.$idnum.'" ';
//      $sql_updt.='and ifnull(cair,"")="" ';
     $sql_updt.='and ifnull(cair,0)*1=0 ';
     $res_updt=msql_query($sql_updt,$link_data);
     
     
     
     
     return;
}

function set_comp2ctas($docu,$idnum)
{
     if(empty($docu) or empty($idnum)){return;}
     if(!mb_stristr('FP10.FP03',$docu)){return;}
     
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     $empr0=data_maes('empr',$empr);
     if(empty($empr0['empr_cont'])){return;}
     
     $docu1=data_docu($docu,$idnum);
     if($docu1['fecha']<(date('Y')-1).'-01-01'){return;}
     
     
     $xfecha0=date('Y-m-01',strtotime('-11 month'));
     
     $sql_comp='select * from comp2 where idnum2="'.$idnum.'" and ifnull(ctas,0)*1=0 ';
     $res_comp=msql_query($sql_comp,$link_data);
     
//      echo ' <br>== '.$idnum;
     if(msql_num_rows($res_comp)==0){return;}
     
//      echo ' == '.$idnum;
          
     $sql_comp ='select *,';
     $sql_comp.='abs(DATEDIFF("'.$docu1['fecha'].'",fecha)) as diasdif ';
     $sql_comp.='from comp1 ';
     $sql_comp.='left join comp2 on idnum1=idnum2 ';
     $sql_comp.='where ifnull(status,"")not in("O","X") ';
     $sql_comp.='and enti like "'.substr($docu1['enti'],0,10).'%" ';
     $sql_comp.='and fecha like "'.substr($docu1['fecha'],0,7).'%" ';
     $sql_comp.='and ifnull(ctas,0)*1>0 ';
     $sql_comp.='order by diasdif ';
     $res_comp=msql_query($sql_comp,$link_data);
     $comp12=msql_fetch_assoc($res_comp);

     
     if(empty($comp12['ctas']))
     {
          $sql_comp ='select *,';
          $sql_comp.='abs(DATEDIFF("'.$docu1['fecha'].'",fecha)) as diasdif ';
          $sql_comp.='from comp1 ';
          $sql_comp.='left join comp2 on idnum1=idnum2 ';
          $sql_comp.='where ifnull(status,"")not in("O","X") ';
          $sql_comp.='and enti like "'.substr($docu1['enti'],0,10).'%" ';
          $sql_comp.='and fecha like "'.substr($docu1['fecha'],0,4).'%" ';
          $sql_comp.='and ifnull(ctas,0)*1>0 ';
          $sql_comp.='order by diasdif ';
          $res_comp=msql_query($sql_comp,$link_data);
          $comp12=msql_fetch_assoc($res_comp);
     }
     
     
     if(empty($comp12['ctas']))
     {
          $sql_comp ='select *,';
          $sql_comp.='abs(DATEDIFF("'.$docu1['fecha'].'",fecha)) as diasdif ';
          $sql_comp.='from comp1 ';
          $sql_comp.='left join comp2 on idnum1=idnum2 ';
          $sql_comp.='where ifnull(status,"")not in("O","X") ';
          $sql_comp.='and enti like "'.substr($docu1['enti'],0,10).'%" ';
//           $sql_comp.='and fecha like "'.substr($docu1['fecha'],0,4).'%" ';
          $sql_comp.='and fecha >="'.date('Y-m-01',strtotime('-3 month')).'" ';
          $sql_comp.='and ifnull(ctas,0)*1>0 ';
          $sql_comp.='order by diasdif ';
          $res_comp=msql_query($sql_comp,$link_data);
          $comp12=msql_fetch_assoc($res_comp);
     }
     
     
     if(!empty($comp12['ctas']))
     {
          $sql_updt='update comp2 set ctas="'.$comp12['ctas'].'" ';
          $sql_updt.='where idnum2="'.$idnum.'" ';
          $sql_updt.='and ifnull(ctas,0)*1=0 ';
          $res_updt=msql_query($sql_updt,$link_data);
//           echo '<br>==='.$sql_updt;
          
     }
     
     return;
}

function set_cant123($docu,$idnum)
{
     if(!mb_stristr('PS.LP',substr($docu,0,2))){return;}
     
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     $empr0=data_maes('empr',$empr);
     $docu0=data_maes('docu',$docu);

     $fecha1=date('Y-m-d',strtotime('-3 month'));
     $fecha2=date('Y-m-d',strtotime('+9 month'));
     
     $tabla=$docu0['docu_tabla'];
     $tabla1=$tabla.'1';
     $tabla2=$tabla.'2';
     
     $sql_cant='select * from '.$tabla1.' ';
     $sql_cant.='left join '.$tabla2.' on idnum1=idnum2 ';
     $sql_cant.='where status not in ("O","X")';
     $sql_cant.='and fecha >="'.$fecha1.'" ';
     $sql_cant.='and docu like "'.$docu.'" ';
     $sql_cant.='and idnum1="'.$idnum.'"';
     $res_cant=msql_query($sql_cant,$link_data);
     
//      $fecha=msql_result($res_cant,0,'fecha');
//      if($fecha<$fecha0){return;}
     
     $idate=date('Y-m-d H:i:s',strtotime('-2 minutes'));
     
     while($aso_cant=msql_fetch_assoc($res_cant))
     {
          $fecha=$aso_cant['fecha'];
          $item=$aso_cant['item'];
          $cant=$aso_cant['cant'];
          $iddet=$aso_cant['iddet'];
          
          $sql_prov='';
          $sql_prov.='select *, sum(cant) as tcant from prov1 ';
          $sql_prov.='left join prov2 on idnum1=idnum2 ';
          $sql_prov.='where status not in ("O","X")';
          $sql_prov.='and docu like "PP%" ';
          $sql_prov.='and item ="'.$item.'" ';
          $sql_prov.='and fecha >="'.$fecha.'" ';
          $sql_prov.='and fecha <="'.$fecha2.'" ';
//           $sql_prov.='and idate < "'.$idate.'" ';
          $res_prov=msql_query($sql_prov,$link_data);
          
          $tcant=msql_result($res_prov,0,'tcant');
          
//           echo '<br>/*/*/'.$sql_prov;
//           echo '<br>/*/*/'.$tcant;
          
          if(!empty($tcant))
          {
               $sql_updt='';
               $sql_updt.='update '.$tabla2.' ';
               $sql_updt.='set cant2="'.$tcant.'" ';
               $sql_updt.='where idnum2="'.$idnum.'"';
               $sql_updt.='and iddet="'.$iddet.'"';
               $res_updt=msql_query($sql_updt,$link_data);
//                echo '**'.$sql_updt;
          }
          
     }

     return;
}


function set_fieldstr($tabla,$colu)
{
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     $empr0=data_maes('empr',$empr);

//      $fecha1=date('Y-m-01',strtotime('-1 month'));

     $sql_updt ='update jwib1 set ';
     $sql_updt.='nombre1=upper(nombre1), ';
     $sql_updt.='nombre2=upper(nombre2),  ';
     $sql_updt.='nombre3=upper(nombre3), ';
     $sql_updt.='nombre4=upper(nombre4) ';
     $sql_updt.='where fecha>="'.$fecha1.'" ';
     $res_updt=msql_query($sql_updt,$link_data);

     return;
     
}

function set_nombres()
{
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     $empr0=data_maes('empr',$empr);
     
     $fecha1='2000-01-01';
     $fecha1=date('Y-m-01',strtotime('-1 month'));
     
     $sql_updt ='update jwib1 set ';
     $sql_updt.='nombre1=upper(nombre1), ';
     $sql_updt.='nombre2=upper(nombre2),  ';
     $sql_updt.='nombre3=upper(nombre3), ';
     $sql_updt.='nombre4=upper(nombre4) ';
     $sql_updt.='where fecha>="'.$fecha1.'" ';
     $res_updt=msql_query($sql_updt,$link_data);
          
     
//      REPLACE(str, find_string, replace_with)     
     
     return;
}

function set_uxpre($docu='',$idnum='')
{
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     $empr0=data_maes('empr',$empr);
     $fecha1=date('Y-m-01',strtotime('-1 month'));

//      $sql_uxpre ='update comp1 left join comp2 on idnum1=idnum2 ';
//      {
//      $sql_uxpre.='set uxpre= case ';
//      $sql_uxpre.='when detalle like "%5K%" or detalle like "%5 K%" then 5 ';
//      $sql_uxpre.='else uxpre end ';
//      }          
//      $sql_uxpre.='where status=status ';
//      $sql_uxpre.='and ifnull(uxpre,0)=0 ';
//      if(empty($idnum)) {$sql_uxpre.='and fecha>="'.$fecha1.'" ';}
//      if(!empty($idnum)){$sql_uxpre.='and idnum2="'.$idnum.'" ';}
//      $res_uxpre=msql_query($sql_uxpre,$link_data);
          
          
     $sql_uxpre ='update comp1 left join comp2 on idnum1=idnum2 ';
     {    
     $sql_uxpre.='set uxpre= case ';
     $sql_uxpre.='when detalle like "%0.5K%" or detalle like "%0.5 K%" then 0.5 ';
     $sql_uxpre.='when detalle like "%0,5K%" or detalle like "%0,5 K%" then 0.5 ';
     $sql_uxpre.='when detalle like "%1K%" or detalle like "%1 K%" then 1 ';
     $sql_uxpre.='when detalle like "%10K%" or detalle like "%10 K%" then 10 ';
     $sql_uxpre.='when detalle like "%15K%" or detalle like "%15 K%" then 15 ';
     $sql_uxpre.='when detalle like "%20K%" or detalle like "%20 K%" then 20 ';
     $sql_uxpre.='when detalle like "%25K%" or detalle like "%25 K%" then 25 ';
     $sql_uxpre.='when detalle like "%30K%" or detalle like "%30 K%" then 30 ';
     $sql_uxpre.='when detalle like "%35K%" or detalle like "%35 K%" then 35 ';
     $sql_uxpre.='when detalle like "%40K%" or detalle like "%40 K%" then 40 ';
     $sql_uxpre.='when detalle like "%45K%" or detalle like "%45 K%" then 45 ';
     $sql_uxpre.='when detalle like "%50K%" or detalle like "%50 K%" then 50 ';
     
     $sql_uxpre.='when detalle like "%23K%" or detalle like "%23 K%" then 23 ';

     $sql_uxpre.='when detalle like "%0.5LT%" or detalle like "%0.5 LT%" then 0.5 ';
     $sql_uxpre.='when detalle like "%0,5LT%" or detalle like "%0,5 LT%" then 0.5 ';
     $sql_uxpre.='when detalle like "%1/2LT%" or detalle like "%1/2 LT%" then 0.5 ';
     $sql_uxpre.='when detalle like "%1LT%" or detalle like "%1 LT%" then 1 ';
     $sql_uxpre.='when detalle like "%2LT%" or detalle like "%2 LT%" then 2 ';
          
     $sql_uxpre.='when detalle like "%10L%" or detalle like "%10 L%" then 10 ';
     $sql_uxpre.='when detalle like "%20L%" or detalle like "%20 L%" then 20 ';
     $sql_uxpre.='when detalle like "%30L%" or detalle like "%30 L%" then 30 ';
     $sql_uxpre.='when detalle like "%35L%" or detalle like "%35 L%" then 35 ';
     $sql_uxpre.='when detalle like "%40L%" or detalle like "%40 L%" then 40 ';
     $sql_uxpre.='when detalle like "%50L%" or detalle like "%50 L%" then 50 ';
     $sql_uxpre.='when detalle like "%60L%" or detalle like "%60 L%" then 60 ';
     $sql_uxpre.='when detalle like "%4L%" or detalle like "%4 L%" then 4 ';
//      $sql_uxpre.='when detalle like "%5L%" or detalle like "%5 L%" then 5 ';

     $sql_uxpre.='when detalle like "%19LTS%" or detalle like "%19lts%" or detalle like "%19 LT%" then 18.92705 ';    

     $sql_uxpre.='when detalle like "%250CC%" or detalle like "%250 CC%" then 0.25';
     $sql_uxpre.='when detalle like "%300CC%" or detalle like "%300 CC%" then 0.30';
     $sql_uxpre.='when detalle like "%350CC%" or detalle like "%350 CC%" then 0.35';
     $sql_uxpre.='when detalle like "%500CC%" or detalle like "%500 CC%" then 0.50';
     $sql_uxpre.='when detalle like "%600CC%" or detalle like "%600 CC%" then 0.60';
     $sql_uxpre.='when detalle like "%750CC%" or detalle like "%750 CC%" then 0.75';

     $sql_uxpre.='when detalle like "%250ML%" or detalle like "%250 ML%" then 0.25';
     $sql_uxpre.='when detalle like "%300ML%" or detalle like "%300 ML%" then 0.30';
     $sql_uxpre.='when detalle like "%350ML%" or detalle like "%350 ML%" then 0.35';
     $sql_uxpre.='when detalle like "%500ML%" or detalle like "%500 ML%" then 0.50';
     $sql_uxpre.='when detalle like "%600ML%" or detalle like "%600 ML%" then 0.60';
     $sql_uxpre.='when detalle like "%750ML%" or detalle like "%750 ML%" then 0.75';
          
     $sql_uxpre.='when detalle like "%5G%" or detalle like "%5 G%" then 5 ';
     $sql_uxpre.='when detalle like "%10G%" or detalle like "%10 G%" then 10 ';
     $sql_uxpre.='when detalle like "%100G%" or detalle like "%100 G%" then 100 ';
     //     $sql_uxpre.='when detalle like "%250G%" or detalle like "%250 G%" then 100 ';
     
     $sql_uxpre.='when detalle like "%250GR%" or detalle like "%250 GR%" then 250 ';
     $sql_uxpre.='when detalle like "%500GR%" or detalle like "%500 GR%" then 500 ';

     $sql_uxpre.='else uxpre end ';
     }
     $sql_uxpre.='where status=status ';
     $sql_uxpre.='and ifnull(uxpre,0)=0 ';
     if(empty($idnum)) {$sql_uxpre.='and fecha>="'.$fecha1.'" ';}
     if(!empty($idnum)){$sql_uxpre.='and idnum2="'.$idnum.'" ';}
     $res_uxpre=msql_query($sql_uxpre,$link_data);
   
//      echo '<br><br>*/*/*'.$sql_uxpre;
     
}

function set_facon($docu='',$idnum='')
{
     if(empty($empr)){$empr=$_SESSION['empr_codigo'];}     
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     $empr0=data_maes('empr',$empr);
     $fecha1=date('Y-m-01',strtotime('-1 month'));
     
     if($empr0['empr_codigo']=='imcp' and 1==2)
     {
          $sql_facon ='update comp2, item0 ';
          $sql_facon.='set facon=.01 ';
          $sql_facon.='where item=item_codigo  ';
          $sql_facon.='and itemce="MPRO020"  ';
          if(!empty($idnum)){$sql_facon.='and idnum2="'.$idnum.'" ';}
          $res_facon=msql_query($sql_facon,$link_data);
     }
       
     $sql_facon ='update comp1 left join comp2 on idnum1=idnum2 left join item0 on item=item_codigo ';
     {
     $sql_facon.='set facon= case ';
     // lt->kg
     $sql_facon.='when item_umdd like "K%" and (detalle like "%LIT%" or detalle like "%LT%") then 1 ';
     // lb->kg
     $sql_facon.='when item_umdd like "K%" and (detalle like "%LB%" or detalle like "%LIB%") then 1/2.20462 ';
     // gr->kg
     $sql_facon.='when item_umdd like "K%" and (detalle like "%0GR%" or detalle like "%0 GR%") then 1000 ';
     // TM->Kg
     $sql_facon.='when item_umdd like "K%" and (umdd="TM") then 1000 ';
     // cc->lt
     $sql_facon.='when item_umdd like "L%" and (detalle like "%CC%" or detalle like "%CC%") then 1/1000 ';
     // gl->lt
     $sql_facon.='when item_umdd like "L%" and (detalle like "%GL%" or detalle like "%GAL%") then 3.78541 ';
     // lt->lt
     $sql_facon.='when item_umdd like "L%" and (detalle like "%LIT%" or detalle like "%LT%") then 1 ';
     // lt->lt
     $sql_facon.='when item_umdd like "L%" and (detalle like "%1L%" or detalle like "%1 L%") then 1 ';
     // lb->lt
     $sql_facon.='when item_umdd like "L%" and (detalle like "%LT%" or detalle like "%LB%") then 1 ';

     $sql_facon.='else facon end ';
     }          
     $sql_facon.='where status=status ';
     $sql_facon.='and ifnull(facon,0)=0 ';
     if(empty($idnum)) {$sql_facon.='and fecha>="'.$fecha1.'" ';}
     if(!empty($idnum)){$sql_facon.='and idnum2="'.$idnum.'" ';}
     $res_facon=msql_query($sql_facon,$link_data);
     
//      echo '<br>*/*/*/'.$sql_facon;
//      echo '<br>*/*/*/'.$sql_facon;

}

?>